/*===========================================================================*/
// 20090722 AK Santiago tweaks: remove mailto from email display in obfus
// 20090512 AK added obfus (gui display)
// 20090513 AK specified new window dimensions for credit & residency
// 20090512 AK added obfuscate (gui functionality)
// 20090428 AK added numbersAndSlashOnly
// 20090424 AK made isPhone regex more robust (extensions and other patterns)
// 20090422 AK added isCalendarDate, isSSN
// 20090421 AK added setFocusOnLoad
// 20090416 AK allowed error display from getObj
// 20090416 AK added windowOpener, windowCloser, doCloseMe
// 20090414 AK added numbersOnly
// 20090323 AK added isZipcode
// 20090316 AK added URLencode/URLdecode
// 20090302 AK added simple isPhoneNumber, isEMail
// 20090227 AK added trim
// 20090218 AK added setID, getObj
/*===========================================================================*/

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
//=====================================================================
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//=====================================================================
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
//=====================================================================
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//=====================================================================
function showLayer(LayerName) {
	if (document.getElementById) { 
  		objLayer = document.getElementById(LayerName).style; 
  	} else {
		var doc = document.layers; 
		var style = '';
	    objLayer = eval('doc.' + LayerName + style);
	}
   	objLayer.visibility = 'visible';
}
//=====================================================================
function hideLayer(LayerName) {
	if (document.getElementById) { 
  		objLayer = document.getElementById(LayerName).style; 
  	} else {
		var doc = document.layers; 
		var style = '';
	    objLayer = eval('doc.' + LayerName + style);
	}
   	objLayer.visibility = 'hidden';
}
//=====================================================================
function fixCorpMenus() {
	moveLayer("corp_dropdown1","corp_menu_about",-20,-15);
	moveLayer("corp_dropdown2","corp_menu_services",-20,-15);	
}
//=====================================================================
function fixSalesMenus() {
	moveLayer("sales_dropdown1","sales_menu_buying",-20,-15);
}
//=====================================================================
function moveLayer(LayerName,TargetName,intX,intY) {
	elmX = getLeft(TargetName);
	elmY = getTop(TargetName);
	objLayer = document.getElementById(LayerName).style; 
	objLayer.left = elmX+intX;
	objLayer.top = elmY+intY;
}
//=====================================================================
function getLeft(obj){
	if ('string' == typeof obj) obj = document.getElementById(obj);
	var x = 0;
	while (obj != null) {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return x;
}
//=====================================================================
function getTop(obj) {
	if ('string' == typeof obj) obj = document.getElementById(obj);
	var y = 0;
	while (obj != null) {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return y;
}
//=====================================================================
function setID(vstrControlID, objID) {
	var objControl = new getObj(vstrControlID).obj;
	objControl.value = objID;
}
//=====================================================================

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 "credit" :
			args="height=700,left=50,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=50,width=1150";
			break;
		case "residency" :
			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 obfus(strName, strDomain, strStyle) {
	//strDomain = '&#64;s&#97;&#110;&#116;&#105;&#97;&#103;&#111;&#99;&#111;r&#112;&#46;c&#111;&#109;';
	//document.write('<a href="&#109;a&#105;l&#116;&#111;:' + strName + strDomain + '" class="' + strStyle + '">');
	
	//document.write('&#109;a&#105;l&#116;&#111;:' + strName + '&#64;' + strDomain.replace(/%23/g,"."));
	document.write(strName + '&#64;' + strDomain.replace(/%23/g,"."));
	//document.write(strName + strDomain + '</a>');
};
//=====================================================================
function obfuscate(sDom, sUser){
	return("mail"+"to:"+sUser+"@"+sDom.replace(/%23/g,"."));
};
//=====================================================================

