/*===========================================================================*/
// CHANGE HISTORY (latest on top)
// 20090617 AK new
/*===========================================================================*/

//=====================================================================

function getObj(vstrControlID) {
	try {
		if (document.getElementById) {
			this.obj = document.getElementById(vstrControlID);
			this.style = document.getElementById(vstrControlID).style;
		}
		else if (document.all) {
			this.obj = document.all[vstrControlID];
			this.style = document.all[vstrControlID].style;
		}
		else if (document.layers) {
			this.obj = document.layers[vstrControlID];
			this.style = document.layers[vstrControlID];
		};
	}
	catch (exception) {
		alert(exception);
		alert(vstrControlID);
		return;
	};
};

//=====================================================================

function trim(str) {
	// replace leading spaces, trailing spaces, and that pesky xml safe space (xa0 here) with nothing for further parsing
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/\xa0/g, '');
};

//=====================================================================
 
function isPhoneNumber(s) {
	rePhoneNumber = new RegExp(/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/);
	if (!rePhoneNumber.test(s)) {
		return false;
	} else {
		return true;
	};
};

//=====================================================================

function isEMail(s) {
	var reEMail  = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
	if (reEMail.test(s)) {
		return true;
	};
	return false;
};

//=====================================================================

function isZipcode(s) {
	var reZipcode = new RegExp(/^\d{5}([\-]\d{4})?$/);
	if (reZipcode.test(s)) {
		return true;
	};
	return false;
};

//=====================================================================

function isCalendarDate(s) {
	var reCalendarDate = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{4}$/);
	if (reCalendarDate.test(s)) {
		return true;
	};
	return false;
};

//=====================================================================

function isSSN(s) {
	var reSSN = new RegExp(/^[\d]{3}-[\d]{2}-[\d]{4}$/);
	if (reSSN.test(s)) {
		return true;
	};
	return false;
};

//=====================================================================

function URLEncode(url) //Function to encode URL.
{
// The Javascript escape and unescape functions do not correspond
// with what browsers actually do...
var SAFECHARS = "0123456789" + // Numeric
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
"abcdefghijklmnopqrstuvwxyz" +
"-_.!~*'()"; // RFC2396 Mark characters
var HEX = "0123456789ABCDEF"; 

var plaintext = url;
var encoded = "";
for (var i = 0; i < plaintext.length; i++ ) {
var ch = plaintext.charAt(i);
if (ch == " ") {
encoded += "+"; // x-www-urlencoded, rather than %20
} else if (SAFECHARS.indexOf(ch) != -1) {
encoded += ch;
} else {
var charCode = ch.charCodeAt(0);
if (charCode > 255) {
alert( "Unicode Character '" 
+ ch 
+ "' cannot be encoded using standard URL encoding.\n" +
"(URL encoding only supports 8-bit characters.)\n" +
"A space (+) will be substituted." );
encoded += "+";
} else {
encoded += "%";
encoded += HEX.charAt((charCode >> 4) & 0xF);
encoded += HEX.charAt(charCode & 0xF);
}
}
} 

return encoded;
}; 

//=====================================================================

function URLDecode(url) //function decode URL
{
// Replace + with ' '
// Replace %xx with equivalent character
// Put [ERROR] in output if %xx is invalid.
var HEXCHARS = "0123456789ABCDEFabcdef"; 
var encoded = url;
var plaintext = "";
var i = 0;
while (i < encoded.length) {
var ch = encoded.charAt(i);
if (ch == "+") {
plaintext += " ";
i++;
} else if (ch == "%") {
if (i < (encoded.length-2) 
&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
plaintext += unescape( encoded.substr(i,3) );
i += 3;
} else {
alert( 'Bad escape combination near ...' + encoded.substr(i) );
plaintext += "%[ERROR]";
i++;
}
} else {
plaintext += ch;
i++;
}
} // while 

return plaintext;
}; 

//=====================================================================

function numbersOnly( myfield, e, dec ) {
	var key;
	var keychar;

	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;
	};

	keychar = String.fromCharCode(key);

	if (   (key==null) 
		|| (key==0)
		|| (key==8)
		|| (key==9)
		|| (key==13)
		|| (key==27)
	) { // control keys
		return true;
	} else if ( (("0123456789").indexOf(keychar) > -1) ) { // numbers
		return true;
	} else if ( dec && (keychar == ".") ) { // decimal point jump
		myfield.form.elements[dec].focus();
		return false;
	} else {
		return false;
	};

};

//=====================================================================

function numbersAndSlashOnly( myfield, e, dec ) {
	var key;
	var keychar;

	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;
	};

	keychar = String.fromCharCode(key);

	if (   (key==null) 
		|| (key==0)
		|| (key==8)
		|| (key==9)
		|| (key==13)
		|| (key==27)
	) { // control keys
		return true;
	} else if ( keychar == "/" ) {
		return true;
	} else if ( (("0123456789").indexOf(keychar) > -1) ) { // numbers
		return true;
	} else if ( dec && (keychar == ".") ) { // decimal point jump
		myfield.form.elements[dec].focus();
		return false;
	} else {
		return false;
	};

};

//=====================================================================

// POPUP WINDOWS
var objNamedWindow;

function windowOpener(url, name) {
	var args;
	switch (name) {
		case "help" :
			args="height=700,left=50,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=50,width=1150";
			break;
		case "window2" :
			args="height=700,left=50,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=50,width=1150";
			break;
		// case "window3" :
			// args="height=600,left=50,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=50,width=1000";
			// break;
		// case "window4" :
			// args="height=375,left=200,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,top=200,width=580";
			// break;
			
		default :
			args="height=575,left=50,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=50,width=550";
	};
	if (typeof(objNamedWindow) != "object"){
		objNamedWindow = window.open(url,name,args);
	} else {
		if (!objNamedWindow.closed){
			//objNamedWindow.focus(); // cheaper when no data changes
			objNamedWindow.location.href = url;
		} else {
			objNamedWindow = window.open(url,name,args);
		};
	};
    objNamedWindow.focus();
};

function windowCloser(objWin) {
	if (typeof(objWin) == "object"){
		objWin.close();
	};
};
//e.g. in caller:
//onClick='windowOpener("Detail.aspx?Id=<?>","windowX");'
//onUnload='windowCloser(objNamedWindow);'
// & in pop up:
function doCloseMe() {
	window.close();
};
//=====================================================================
function setFocusOnLoad(vstrControlID) {
	var blnSet = false;

	if (vstrControlID != null) {
		var objControl = new getObj(vstrControlID).obj;
		if (objControl != null) {
			objControl.focus();
			blnSet = true;
		};
	};
	
	if (!(blnSet)) {
 		//Step 1. Exit function if there is not any form
		if (document.forms.length ==0) {
			return;
		};

  		//Step 2. filter out the form element, only allow the following type get through
  		//	text							textarea
		//	checkbox						select
		//	radio							password
		//	Filter out the element which is disabled or readonly
		//	If the client browser is IE, then filter out the visible object only (CSS attribute)
		//	finally, Set focus on the first object then exit the function

		try {
			for (var j=0; j<document.forms.length;j++){
				for (var i=0; i<document.forms[j].elements.length; i++ ){
					var o=document.forms[j].elements[i]
					if (
						(o.type=='checkbox')
						||(o.type=='password')
						||(o.type=='radio')
						||(o.type=='select-multiple')
						||(o.type=='select-one')
						||(o.type=='text')
						||(o.type=='textarea')
					) {
						if ((o.readOnly!=true) && (o.disabled!=true)) {
							if ((o.style.visibility != 'hidden') && (o.style.display != 'none')) {
								o.focus();
								return;
							}; //end of ie.hidden 
						}; //end of readOnly
					}; //detect type
				}; //loop throught the element
			}; //end of form loop
		}
		catch (exception) {
			return;
		};
	};
};
//=====================================================================
function checkEnter(event) { 	
	if (event.keyCode==13) {
		submitForm();
	};
};
//=====================================================================
