// Character constants
var Blank = 32;
var LParen = 40;
var RParen = 41;
var Dash = 45;
var Slash = 47;
var Plus = 43;
var At = 64;
var Period = 46;
var UnderScore = 95;

function CheckDigit(kc)
{
  return (kc >= 48 && kc<= 57);  
}

function CheckAlpha(kc) 
{
  return ((kc >= 65 && kc<= 90) || (kc >= 97 && kc<= 122));  
}

function CheckAlphaNumeric(kc) 
{
	return (CheckDigit(kc) || CheckAlpha(kc));    
}

function CheckSSN(kc)
{
	return (CheckDigit(kc) || (kc==Dash) );    
}

function CheckPhone(kc)
{
	return (CheckDigit(kc) || (kc==Dash) || (kc==LParen) || (kc==RParen) || (kc==Blank) );    
}

function CheckZip(kc)
{
	return (CheckDigit(kc) || (kc==Dash) );    
}

function CheckEmail(kc)
{
	return (CheckDigit(kc) || CheckAlpha(kc) || (kc==Dash) || (kc==Plus) || (kc==Period) || (kc==At) || (kc==UnderScore));    
}

function CheckDate(kc)
{
	return (CheckDigit(kc) ||  (kc==Slash));    
}

function PhoneMask (txt) {
  switch (txt.value.length) {
    case 0: 
      event.keyCode = '('.charCodeAt();
      return true;
      break;
    case 1:
    case 2:
    case 3:
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;
    case 4:
      event.keyCode = ')'.charCodeAt();
      return true;
      break;
    case 5:
      event.keyCode = ' '.charCodeAt();
      return true;
      break;
    case 6:
    case 7:
    case 8:
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;
    case 9:
      event.keyCode = '-'.charCodeAt();
      return true;
      break;
    case 10:
    case 11:
    case 12:
    case 13: 
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;
    default:
      return false;
      break;
  }
}

function SSNMask (txt) {
  switch (txt.value.length) {
    case 0: 
    case 1:
    case 2:
    case 4:
    case 5:
    case 7:
    case 8:
    case 9:
    case 10:    
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;    
    case 3:        
    case 6:            
      event.keyCode = '-'.charCodeAt();
      return true;
      break;
    default:
      return false;
      break;
  }
}

function DateMask (txt) {
  switch (txt.value.length) {
    case 0: 
    case 1:
    case 3:
    case 4:
    case 6:
    case 7:
    case 8:
    case 9:    
      return /\d/.test(String.fromCharCode(event.keyCode));
      break;    
    case 2:        
    case 5:            
      event.keyCode = '/'.charCodeAt();
      return true;
      break;
    default:
      return false;
      break;
  }
}

function DaySync(selMonth, selDay, selYear) {
	var oOption
	var intDayOrig
	var intDaysInMonth
	var intDay
	var intMonth = selMonth.value;	
	var intYear = selYear.value;				
		
	// Select Number of Days in Month
	switch (intMonth) {
		case '4':
		case '6':
		case '9':
		case '11':
			intDaysInMonth = 30;
			break;
							
		case '2':
			intDaysInMonth = (intYear % 4)==0 ? 29 : 28;
			break;				
			
		default:
			intDaysInMonth = 31;
			break;
	};
	
	//Cache original day
	intDayOrig = selDay.selectedIndex;
	
	//Remove items from Days dropdown
	selDay.length = 0;	
		
	//Add items to Days dropdown
	oOption = document.createElement("OPTION");
	oOption.text=' ';
	oOption.value=0;
	selDay.add(oOption);

	//Add days to Days dropdown	
	for (intDay=1; intDay <= intDaysInMonth ; intDay++)
	{
		oOption = document.createElement("OPTION");
		oOption.text=intDay;
		oOption.value=intDay;
		selDay.add(oOption);
	}		
	selDay.selectedIndex = (intDayOrig > intDaysInMonth ? 1 : intDayOrig);	
	
}

function PopUpWindow(URL,t, l, h, w) 
{			

	var wdetails="toolbar=no,location=no,status=no," + 
								"menubar=no, resizable=yes, scrollbars=yes, " +
								"width=" + w + ", height=" + h + ", top=" + t + ", left=" + l
			
	var aWindow=window.open(URL, "window",wdetails);			
}


function PopUpDialog(URL,t, l, h, w) 
{			

	var wdetails="toolbar=no,location=no,status=no," + 
								"menubar=no, resizable=yes, scrollbars=yes, " +
								"width=" + w + ", height=" + h + ", top=" + t + ", left=" + l
			
	var aWindow=window.showModalDialog(URL, "window",wdetails);			
}

/*	Created 4-19-2004 by DSH
		This function disables the right mouse click.
*/
function DisableRightClick()
{
	document.oncontextmenu = function(){return false}
	if(document.layers) {
			window.captureEvents(Event.MOUSEDOWN);
			window.onmousedown = function(e){
					if(e.target==document)return false;
			}
	}
	else {
			document.onmousedown = function(){return false}
	}
}
/*	Login() : Created 04-27-2005 by DSH
		This function encrypts user login information on the 
		client before hitting the database
		
		"pw" is a reference to a control on the screen whose value gets changed by this script
		
		an example of this function call is:
		   Login(document.frm_PasswordConverter.txt_1795UID, document.frm_PasswordConverter.txt_1795PassNew);
*/
function Login(un, pw)
{
	var cryptTable  = new String(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789	!@#$%^&*()`'-=[];,./?_+{}|:<>~");
	var cryptLength = new Number(cryptTable.length-1);
	var escapeChar = cryptTable.charAt(cryptLength);
	var lineFeed = "\n";
	var doubleQuote = '"';
	var clearMessage = new Number(5000);
	var hexcase = 0;
	var b64pad  = "";
	var chrsz   = 8;

	function encrypt(input, password)
	{
		input = "" + input + "_baic";
		
		//alert("Inside encrypt() Input - " + input + ", password - " + password);
		
		var inChar, inValue, outValue; var output=""; var arNumberPw = new Array(); var statusBar=0;
		var pwLength=password.length; var inLength=input.length; var stopStatus=Math.round(inLength/10);

		for (var pwIndex=0; pwIndex<pwLength; pwIndex++){arNumberPw[pwIndex]=cryptTable.indexOf(password.charAt(pwIndex));}

		for (var inIndex=0, pwIndex=0; inIndex<inLength; inIndex++, pwIndex++)
			{
			if (pwIndex==pwLength){pwIndex=0;}
			
			inChar=input.charAt(inIndex)
			inValue=cryptTable.indexOf(inChar);

			if (inValue!=-1)
				{
					outValue=arNumberPw[pwIndex] ^ inValue;
					if (outValue>=cryptLength){outValue=escapeChar+cryptTable.charAt(outValue-cryptLength);}
					else{outValue=cryptTable.charAt(outValue);}
				}	
			else if (inChar=="\r")
				{outValue=escapeChar+escapeChar;if (input.charAt(inIndex+1)=="\n"){inIndex++;}}
			else if (inChar=="\n")
				{outValue=escapeChar+escapeChar;}
			else if (inChar==doubleQuote)
				{outValue=escapeChar+"'";}
			else
				{outValue=inChar;}

			output+=outValue;

			if (inIndex>=statusBar){window.status=inIndex+"/"+inLength+" characters decrypted ("+Math.round(inIndex/inLength*100)+"%)";statusBar+=stopStatus;}
			}

		window.status=inLength+"/"+inLength+" characters encrypted (100%)";
		setTimeout("window.status=''", clearMessage);
		return output;
	}// end of encrypt() function
	
	function md5_hex_hmac(key, data) {return binl2hex(core_hmac_md5(key, data)); }
	
	function binl2hex(binarray)
	{
		var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
		var str = "";
		for(var i = 0; i < binarray.length * 4; i++)
		{
			str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
				hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
		}
		return str;
	}
	
	function str2binl(str)
	{
		var bin = Array();
		var mask = (1 << chrsz) - 1;
		for(var i = 0; i < str.length * chrsz; i += chrsz)
			bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
		return bin;
	}

	function core_hmac_md5(key, data)
	{
		var bkey = str2binl(key);
		if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

		var ipad = Array(16), opad = Array(16);
		for(var i = 0; i < 16; i++)
		{ipad[i] = bkey[i] ^ 0x36363636;	opad[i] = bkey[i] ^ 0x5C5C5C5C;}

		var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
		return core_md5(opad.concat(hash), 512 + 128);
	}
	
	function core_md5(x, len)
	{
		x[len >> 5] |= 0x80 << ((len) % 32); x[(((len + 64) >>> 9) << 4) + 14] = len;
		var a =  1732584193; var b = -271733879; var c = -1732584194; var d =  271733878;

		for(var i = 0; i < x.length; i += 16)
		{
			var olda = a;
			var oldb = b;
			var oldc = c;
			var oldd = d;

			a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
			d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
			c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
			b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
			a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
			d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
			c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
			b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
			a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
			d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
			c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
			b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
			a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
			d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
			c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
			b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

			a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
			d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
			c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
			b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
			a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
			d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
			c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
			b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
			a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
			d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
			c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
			b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
			a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
			d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
			c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
			b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

			a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
			d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
			c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
			b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
			a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
			d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
			c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
			b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
			a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
			d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
			c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
			b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
			a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
			d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
			c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
			b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

			a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
			d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
			c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
			b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
			a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
			d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
			c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
			b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
			a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
			d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
			c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
			b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
			a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
			d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
			c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
			b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

			a = safe_add(a, olda);
			b = safe_add(b, oldb);
			c = safe_add(c, oldc);
			d = safe_add(d, oldd);
		}
		return Array(a, b, c, d);
	}

	function bit_rol(num, cnt){return (num << cnt) | (num >>> (32 - cnt));}
	function md5_cmn(q, a, b, x, s, t){return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);}
	function md5_ff(a, b, c, d, x, s, t){return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);}
	function md5_gg(a, b, c, d, x, s, t){return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);}
	function md5_hh(a, b, c, d, x, s, t){return md5_cmn(b ^ c ^ d, a, b, x, s, t);}
	function md5_ii(a, b, c, d, x, s, t){return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);}
	function safe_add(x, y){var lsw = (x & 0xFFFF) + (y & 0xFFFF);var msw = (x >> 16) + (y >> 16) + (lsw >> 16);return (msw << 16) | (lsw & 0xFFFF);}

	// get encrypted version of password based on user ID key
	var npw = md5_hex_hmac((encrypt(un.value, un.value)),(encrypt(un.value, pw.value)));
	
	//alert("pw = " + npw);
	
	// set value of password field equal to encrypted value
	pw.value = npw;
	npw='';
	
	//return npw;
} // end of Login() function

/*	Created 04-27-2005 by DSH
		This function sets focus to subsequent control in document (form)
		when the target length (# of chars) has been reached
*/
function autoTab(input,length, e)
{
    //alert("Inside autoTab()");
    var isNN = (navigator.appName.indexOf("Netscape") != -1);
    var keyCode = (isNN) ? e.which : e.keyCode;
    
    /* Determine which array of keycodes to compare against based on browser */
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

    /*// FOR TESTING ==========================================================================
     var a = fnGetIndex(input), b = a+1, c = input.form.length
     alert("a (index of control event happened on) = " + a);
     alert("b (aforementioned index + 1) = " + b);
     alert("c (number of indexes/controls in form) = " + c);
     alert("b % c = " + b % c + " (focus is being set to control # " + b % c + ")");
    // ======================================================================================*/

    if(input.value.length >= length && !fnContainsElement(filter,keyCode))
    {
        // make a substring of the value in the textbox from index 0 to "1 less than the index passed as 'length'"
        input.value = input.value.slice(0, length);

        // set focus to the next control in the form -- does not have to be tabIndex
        input.form[(fnGetIndex(input)+1) % input.form.length].focus();
        
        /* -- DSH 05/12/05 -- trying something out
        // 
        var strID = input.id + "Hidden";
        var ctrlTarget = d.getelementbyid(strID);
        ctrlTarget.value = ;
        */
    }


    // Private Method # 1
    function fnContainsElement(array, code)
    {
        var blnFound = false;
        var index = 0;

        while(!blnFound && index < array.length) // array.length will always be 3 or 11 depending upon browser type
        {
            if(array[index] == code)
                blnFound = true;
            else
                index++;
        }

        return blnFound;

    } // end of fnContainsElement()


    // Private Method # 2
    function fnGetIndex(input)
    {
        var index = -1;
        var i = 0;
        var found = false;

        while (i < input.form.length && index == -1)
            if (input.form[i] == input)
                index = i;
            else
                i++;

        return index;
    } // end of fnGetIndex()

return true;
} // end of autoTab() function
/*	Created 05-12-2005 by DSH
		This function sets focus to subsequent control in document (form)
		when the target length (# of chars) has been reached
*/
function validatePhoneNumber(sender, args)
{
	/* PURPOSE : to validate the PhoneNumber property of the PhoneNumber user control on the client; 
				 a "valid" number will be 12 characters long, for example "770-717-1777", any length
				 less than 12 will be "invalid"
      
       INPUT(S)   : 1) the "sender" or source of the function call
					2) the control being validated
      
       OUTPUT     : a trimmed string with the first letter of each word capitalized
                    and the remaining letters of each word lowercase (i.e. "Chan Yin Lu")
	*/
	
	//alert("Inside validatePhoneNumber()");
	
	if(args.length < 12)
	{
		args.IsValid = false;
		return;
	}else{
		if(args.length == 12){args.IsValid = true; return;}
	}

} // end of validatePhoneNumber() function

/*	Created 05-19-2005 by DSH
		This function shows/hides a control based on the value of another control
*/
function fnShowControl(input, str_Control_Name)
{
	//alert("Inside fnShow()");
	//alert("" + str_Control_Name + "");
	
	if(!document.getElementById("" + str_Control_Name + ""))
	{
		return;
	}
	else
	{
		var control;
		control = document.GetElementByID("" + str_Control_Name + "");
	}
	
	//alert("control.id = " + control.id + "");
	//alert("input.value = " + input.value);
	
	if (input.value == "other")
	{
		control.visible="False";
	}else{
		control.visible="True";
	}
}

/*	Created 8-2-2006 by MWK
		This function shows a hidden element
*/
function ShowControl(ElementName)
{
	alert("Inside ShowControl()");
	alert("" + ElementName + "");
	
	if(!document.getElementById("" + ElementName + ""))
	{	
		alert("Cannot find Element");
		return;
	}
	else
	{
		var theElement;
		theElement = document.GetElementByID("" + ElementName + "");
		theElement.style.visibility = "visible";
		theElement.style.display = "block";
		//control.focus();
	}	
}

/*	Created 8-2-2006 by MWK
		This function hides a visible element
*/
function HideControl(str_Control_Name)
{
	//alert("Inside HideControl()");
	//alert("" + str_Control_Name + "");
	
	if(!document.getElementById(str_Control_Name))
	{
		return;
	}
	else
	{
		var control;
		control = document.GetElementByID(str_Control_Name);
		control.style.visiblity = "hidden";
		control.style.display = "none";
	}
	
}

/*	Created 7/13/2005 by MWK
	This function will round a number value to 2 decimal precision. eg: 145.456784 = 145.46
*/
function roundToPennies(n)
{
	pennies = n * 100;
	pennies = Math.round(pennies);	

	// if it's less than 10, we have to add a leading 0
	if(pennies<10)
	{
		strPennies = "0" + pennies;
	}
	else
	{
		strPennies = "" + pennies;
	}
	len = strPennies.length;

	// put the decimal point back in 2 places from the right
	return strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);
}

