﻿// JScript File



//Finds the object specified on the page and returns it through getElementById
function FindObj(n) {
  var p,i,x, 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;
}
// Functions for object handling on forms
function uncheckOthers(oCheck, position){
	//alert(oCheck + ' '  + position)
 for (var i=0; i < oCheck.length; i++) 
   if (i == position){
   		if(oCheck[i].checked)
    		oCheck[i].checked = true;
    	else
    		oCheck[i].checked = false;
   }
   else
     oCheck[i].checked = false;
}
function maxExceeded(oCheck, position, nMax){
  var nCtr = 0;
  // First check to see if checkbox is being checked or unchecked
  if (!oCheck[position].checked)
    return;
  for (var i=0; i < oCheck.length; i++) 
    if (oCheck[i].checked) 
      nCtr += 1;
  if (nCtr > nMax) {
    oCheck[position].checked = false;
    alert ('Maximum selections exceeded.');
  }  
}

function isEmpty(s){
  return ((s == null) || (s.length == 0))
}
function isWhitespace(s){  
  var i;
  // Is s empty?
  if (isEmpty(s)) return true;

  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++)
  {   
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
	
    if (whitespace.indexOf(c) == -1) return false;
  }
	
  // All characters are whitespace.
  return true;
}
function charsOccurIn (s, universe){  
  var i;
  // Search through string's characters one by one.
  // Verify that character is in universe.

  for (i = 0; i < s.length; i++)
  {   
    // Check that current character is in universe
    var c = s.charAt(i);
    if (universe.indexOf(c) == -1) 
        return false;
  }
  return true;
}
function ValidID(id) {
  var legalchr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  return charsOccurIn(id,legalchr);
}
//Following functions check for the email address
function emailCheck(strEmailToCheck, blnCheckTLD, blnDetailError) {
	/*
	the function eitehr returns the error number, in this case 0 is no error
	or returns a detailed error string, in this case empty string is no error. 
	to get error string you need to pass true as blnDetailError 
	
	whether or not to verify that the address ends in a two-letter country or well-known
	TLD pass, pass blnCheckTLD true means check otherwise pase false means do not check
	
	number	=		error string
	**********************************************************************************************
	0		= 		empty string										-- this means ok
	1		=		Email address not specified!						-- empty string passed for the email address
	2		= 		Email address seems incorrect (check @ and .'s)
	3		= 		Email address username contains invalid characters.
	4		=		Email address domain name contains invalid characters.
	5		= 		Email address username doesn't seem to be valid.
	6		=		Email address destination IP address is invalid!	-- if we have IP for the domain part
	7		= 		Email address domain name does not seem to be valid.
	8		=		Email address must end in a well-known domain or two letter country.
	9		= 		Email address is missing a hostname!
	*/
	
	if(blnCheckTLD=='undefined' || blnCheckTLD==null || blnCheckTLD=='') blnCheckTLD=false;
	if(blnDetailError=='undefined' || blnDetailError==null || blnDetailError=='') blnDetailError=false;
	if(strEmailToCheck=='undefined' || strEmailToCheck==null) strEmailToCheck='';
		
	//if email to check is blank, return error immediately
	if(strEmailToCheck == ''){
		if(blnDetailError) return 'Email address not specified!';
		else return 1;
	}

	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; ~ ` ! # $ % \ ^ & * = } { ' | ? / : \ " . [ ] */
	var specialChars="\\(\\)><@,;~`!#$%\^&*=}{'|?/:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=strEmailToCheck.match(emailPat);

	if (matchArray==null) {
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		if(blnDetailError) return "Email address seems incorrect (check @ and .'s)";
		else return 2;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			if(blnDetailError) return "Email address username contains invalid characters.";
			else return 3;
	   	}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			if(blnDetailError) return "Email address domain name contains invalid characters.";
			else return 4;
	   	}
	}
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
		if(blnDetailError) return "Email address username doesn't seem to be valid.";
		else return 5;
	}
	
	/* if the e-mail address is at an IP address (not host name) 
	make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) { //We have ourselves an IP address for email address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				if(blnDetailError) return "Email address destination IP address is invalid!";
				else return 6;
	   		}
		}
		//this is valid IP address
		if(blnDetailError) return "";
		else return 0;
	}
	
	// See if "domain" is valid
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			if(blnDetailError) return "Email address domain name does not seem to be valid.";
			else return 7;
	   	}
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	if (blnCheckTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
		if(blnDetailError) return "The email address must end in a well-known domain or two letter country.";
		else return 8;
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
		if(blnDetailError) return "Email address is missing a hostname!";
		else return 9;
	}
	
	// If we've gotten this far, everything's valid!
	if(blnDetailError) return '';
	else return 0;
}

function alltrim(s){  
  // Returns string with leading and trailing blanks removed.
  // Is s empty?
  if (isEmpty(s)) 
    return "";

  var i, n;
  
  // First find position of first nonblank character in string
  for (i = 0; s.charAt(i) == ' ' && i <= s.length;i++);
  
  // Search from end of string, find position of last nonblank character in string
  for (n = s.length; s.charAt(n-1) == ' ' && n >= 0;n--);
    
 return s.substring(i,n);
}
//formats the numeric value
function formatNumeric(number,precision) {
  var blnNeg;
  var strReturn = new String("");
  blnNeg = false;
  var num = new String(number);
  if (num == '')
	 return '';
   
  // First check for decimal point.
   
  if(num.indexOf(".") == -1) {
	   	
	 var strLeft = new String(num.substring(0, num.length));
	 var strRight = "00000000000000000000000".substring(0,precision);
	 
  }
  else {
	
    // Decimal point exists, separate right and left
	 pos = num.indexOf(".");
	 var strLeft = num.substring(0, pos);
	 if (strLeft == "")
	   strLeft = "0";
	 var strRight = num.substring(pos+1);
		
    // Correct precision to the desired level.
    // Adding zeros, or truncating where necessary
    strRight += '0000000000000000000000';
    strRight = strRight.substring(0,precision);
       	
  }  
  	
  // Add commas to the left side.
  var ctr = 0;
  var strTmp = "";
  for (var i=strLeft.length-1;i>=0;i--) {
    ctr += 1;
    if (ctr == 4) {
      strTmp += "," + strLeft.charAt(i);
      ctr = 1;
    }
    else 
      strTmp += strLeft.charAt(i);
       
  }     
  
  strLeft = "";
  for (var i=strTmp.length-1;i>=0;i--) {
    strLeft += strTmp.charAt(i)       
  }
 
  //Taking care of - at first place and , following it 
        
  if(strLeft.indexOf("-,") != -1 || strLeft.indexOf("-") != -1) {
  	blnNeg = true;
  	strLeft = strLeft.replace("-,","("); 
  	strLeft = strLeft.replace("-","(");
  }
	
 
  if (precision == 0)
    strReturn =  strLeft;
  else
    strReturn = strLeft+'.'+strRight
    
  if(blnNeg == true)
  		strReturn += ")"; 
  return strReturn;
}

function IsLeapYear(nYear){
	var nResult = 0;
	// If year is dividable by 4, then yes
	if (!(nYear % 4)){
		// Set Yes
		nResult = 1;
		// Unless, if year is dividable by 100, then No
		if (!(nYear % 100)){
			// Set No
			nResult = 0;
			// Unless, if year is dividable by 400, then Yes
			if (!(nYear % 400))
				// Set Yes
				nResult = 1;
		}
	}
	// Return result
	return (nResult == 1);
}
function checkDate(s){
	s = new String(s);
	//first checking that date comprises of 9 or 10 characters
	if (s.length < 8 || s.length > 10)
		return false;
	//now checking that 2 /'s are in the date
	s = s.split("/");
	//meaning that 2/ are present
	if (s.length == 3){
		//checking for numeric
		//alert(s[0] + " " + s[1] + " " +s[2])
		//alert(s[2] + "%4 = " + s[2]%4)
		if(charsOccurIn(s[0],"0123456789") == false)
			return false;
		else if(charsOccurIn(s[1],"0123456789") == false)
			return false;
		else if(charsOccurIn(s[2],"0123456789") == false)
			return false;
		//checking for the length of month day and year
		else if(s[0].length < 1 || s[0].length > 2 || s[1].length < 1 || s[1].length > 2 || s[2].length != 4)
			return false;
		//checking if only 0 is entered and year is greater than 1900
		else if(s[0] == 0 || s[1] == 0 || s[2] == 0 || s[2] < 1900)
			return false; 
		//month should not be greater than 12 and day 31
		else if(s[0] > 12 || s[1] > 31)
			return false;
		//month is 2 and day is greater than 29 
		else if(s[0] == 2 && s[1] >= 29){
			if (s[1] > 29)
				return false; 
			else if(!IsLeapYear(s[2]))
				return false;
			else
				return true;
		}
		else if((s[0] == 1 && s[1] >31) || +
				(s[0] == 3 && s[1] >31) || +
				(s[0] == 4 && s[1] >30) || +
				(s[0] == 5 && s[1] >31) || +
				(s[0] == 6 && s[1] >30) || +
				(s[0] == 7 && s[1] >31) || +
				(s[0] == 8 && s[1] >31) || +
				(s[0] == 9 && s[1] >30) || +
				(s[0] == 10 && s[1] >31) || +
				(s[0] == 11 && s[1] >30) || +
				(s[0] == 12 && s[1] >31)){
			return false; 
			}
		else
			return true;
	}
	else
		return  false;
}
function CheckToDateGreaterThanFromDate(strFromDate,strToDate){
	if(!checkDate(strFromDate) || !checkDate(strToDate))
		return false;
	else{
		var dFrom = new Date(strFromDate);
		var dTo = new Date(strToDate);
		
		if(dTo > dFrom)
			return true;
		else
			return false;
	}
}
function CheckToDateNotLessThanFromDate(strFromDate,strToDate){
	if(!checkDate(strFromDate) || !checkDate(strToDate))
		return false;
	else{
		var dFrom = new Date(strFromDate);
		var dTo = new Date(strToDate);
		
		if(dTo < dFrom)
			return false;
		else
			return true;
	}
}
function CheckPhone(strPhone){
	strPhone = new String(strPhone);
	var blnReturn = false;
	var cFirst = new String("");
	var cMiddle = new String("");
	var cLast = new String("");
	var cFirstLine = new String("");
	var cSecondLine = new String("");
	
	if(strPhone != '' && strPhone.length == 12){
		cFirst = strPhone.substring(0,3);
		strPhone = strPhone.substring(3,strPhone.length);
		cFirstLine = strPhone.substring(0,1);
		strPhone = strPhone.substring(1,strPhone.length);
		cMiddle = strPhone.substring(0,3);
		strPhone = strPhone.substring(3,strPhone.length);
		cSecondLine = strPhone.substring(0,1);
		strPhone = strPhone.substring(1,strPhone.length);
		cLast = strPhone.substring(0,4);
		
		if(charsOccurIn(cFirst,"0123456789") && charsOccurIn(cFirstLine,"-") && 
				charsOccurIn(cMiddle,"0123456789") && charsOccurIn(cSecondLine,"-") && 
				charsOccurIn(cLast,"0123456789"))
			blnReturn = true;
	}
	return blnReturn 
}

function CheckPhoneCountry(strPhone, cCountry){
	strPhone = new String(strPhone);
	var blnReturn = false;
	
	if(cCountry == 'US' || cCountry == 'CA'){
		var cFirst = new String("");
		var cMiddle = new String("");
		var cLast = new String("");
		var cFirstLine = new String("");
		var cSecondLine = new String("");
	
		if(strPhone != '' && strPhone.length == 12){
			cFirst = strPhone.substring(0,3);
			strPhone = strPhone.substring(3,strPhone.length);
			cFirstLine = strPhone.substring(0,1);
			strPhone = strPhone.substring(1,strPhone.length);
			cMiddle = strPhone.substring(0,3);
			strPhone = strPhone.substring(3,strPhone.length);
			cSecondLine = strPhone.substring(0,1);
			strPhone = strPhone.substring(1,strPhone.length);
			cLast = strPhone.substring(0,4);
		
			if(charsOccurIn(cFirst,"0123456789") && charsOccurIn(cFirstLine,"-") && 
					charsOccurIn(cMiddle,"0123456789") && charsOccurIn(cSecondLine,"-") && 
					charsOccurIn(cLast,"0123456789"))
				blnReturn = true;
		}
	}
	else if(cCountry == 'AU'){
		//should be like 1/2 digits for city code and then 4 digits + 4 digits for phone number
		if((strPhone.length == 9 || strPhone.length == 10) && charsOccurIn(strPhone,"0123456789"))
			blnReturn = true;
	}
	return blnReturn;
}

var USStates = "AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|" +
				"NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY|PR|VI|GU|AS";
var CanStates = "AB|BC|MB|NB|NL|NT|NS|NU|ON|PE|QC|SK|YT";
var AusStates = "ACT|NSW|NT|QLD|SA|VIC|WA";

function CheckCountryState(cState, cCountry){
	var blnReturn = false;
	if(cCountry == 'US' && USStates.indexOf(cState) != -1)
		blnReturn = true;
	else if(cCountry == 'AU' && AusStates.indexOf(cState) != -1)
		blnReturn = true;
	else if(cCountry == 'CA' && CanStates.indexOf(cState) != -1)
		blnReturn = true;

	return blnReturn;
}
function MakeZipPostalUpper(objZip){
	objZip.value = objZip.value.toUpperCase();
}
function CheckZipPostCode(objZip, cCountry, blnOnly5DigitUS_Zip){
	//n = 0 is fine
	//1 = Zip/Postal code missing
	//2 = Country Code problem
	//3 = invalid zip/postal code entered
	//4 = US zip code length problem

	var n = 0;
	
	if(isEmpty(objZip.value))
		n = 1;  //zip code is empty
	else if(isEmpty(cCountry))
		n = 2;  //Country code is empty
	else if(cCountry == 'US'){
		if(blnOnly5DigitUS_Zip){ //strictly 5 digit US Zip
			if(!charsOccurIn(objZip.value,'0123456789'))
				n = 3;
			else if(objZip.value.length != 5)
				n = 4;
		}
		else{
			if(!charsOccurIn(objZip.value,'0123456789-'))
				n = 3;
			else if(objZip.value.length != 10)
				n = 4;
		}	
	}
	else if(cCountry == 'CA'){
		//take out the space from between the 2 pieces of postal code if it is there
		//L5W 1S7 should become L5W1S7
		objZip.value = objZip.value.toUpperCase().replace(/\s/g,"");
		var strZip = objZip.value;
		
		var objPattern1 = /^[A-Z]\d[A-Z]\d[A-Z]\d$/;
		if(!objPattern1.test(strZip))
			n = 3;
	}
	else if(cCountry == 'AU'){
		var objPattern1 = /^\d{4}$/;
		if(!objPattern1.test(objZip.value))
			n = 3;
	}
	else
		n = 2; //country code passed not matched to the above 3
	
	return n;
}

//******************************************************************************
//redit card processing functions
/*
	Card Type				Prefix				Width
	American Express		34,37				15
	Discover				6011				16
	Master Card				51 to 55			16
	Visa					4					13 or 16
	
	Diners Club				36					14
	Carte Blanche			38					14
	EnRoute					2014,2149			15
	JCB						3					16
	JCB						2131,1800			15
	American Diners Club	300 to 305			14
*/

function CC_CheckCardTypeNumber(cCardType,cCardNum){	
	var blnReturn 		= true;
	var cCardNum 		= new String(cCardNum);
	var nFirst2Digits 	= parseInt(cCardNum.slice(0,2));
	var nFirst4Digits 	= parseInt(cCardNum.slice(0,4));
	var nFirstDigit 	= parseInt(cCardNum.slice(0,1));
	
	if(cCardType == "VA" && nFirstDigit != 4)    //VISA
		blnReturn = false;
	else if(cCardType == "DS" && nFirst4Digits != 6011)  //Discover
		blnReturn = false;
	else if(cCardType == "AE" && (nFirst2Digits != 34 && nFirst2Digits != 37)) //American Express
		blnReturn = false;
	else if(cCardType == "MC" && (nFirst2Digits < 51 || nFirst2Digits > 55))
		blnReturn = false;
		
	return blnReturn;
}

function CC_CheckNumber(cCardNum){
	var blnReturn = true;
	var cCardNum = new String(cCardNum);
	var nSumDigit = 0;
	var blnFirstDigit = true;
	
	for(var i=cCardNum.length -1; i >= 0; i --){
		if(blnFirstDigit){
			blnFirstDigit = false;
			nSumDigit += parseInt(cCardNum.charAt(i));
		}
		else{
			blnFirstDigit = true;
			var n = parseInt(cCardNum.charAt(i)) * 2;
			if(n > 9){
				var cDigit = new String(n);
				nSumDigit += (parseInt(cDigit.charAt(0)) + parseInt(cDigit.charAt(1)));
			}
			else
				nSumDigit += n;
		}
	}
	if(nSumDigit == 0)
		blnReturn = false;
	else if(nSumDigit%10 != 0)
		blnReturn = false;

	return blnReturn;
}

function CC_CardNumberLength(cCardType,cCardNum){
	var blnReturn = true;
	if(cCardType == 'VA'){
		if(cCardNum.length != 13 && cCardNum.length != 16)
			blnReturn = false;
		else if(!CC_CheckNumber(cCardNum))
			blnReturn = false;
	}	
	else if(cCardType == 'MC' || cCardType == 'DS'){
		if(cCardNum.length != 16)
			blnReturn = false;
		else if(!CC_CheckNumber(cCardNum))
			blnReturn = false;
	}
	else if(cCardType == 'AE'){
		if(cCardNum.length != 15)
			blnReturn = false;
		else if(!CC_CheckNumber(cCardNum))
			blnReturn = false;
	}
	else //Undefined Card Type
		blnReturn = false;
	return blnReturn;
}
//display processing message
var picProcess 	= new Image();
picProcess.src	= '/pics/img/processing_bars.gif';
function ShowAction(divObject){
		if(divObject){
			var strAction = '<div align="center"><span class="notify">Please wait, we are working on your request.</span><br><br>'+
									'<img border="0" id="impProc" name="imgProc" src="/pics/img/processing_bars.gif"></div>';
			divObject.innerHTML = strAction;
			document.images['imgProc'].src 	= picProcess.src;
		}
	}
	
var picShowHideMinus			= new Image();
var picShowHidePlus				= new Image();
picShowHideMinus.src 			= '/pics/btn/btn_action_minus.gif';
picShowHidePlus.src				= '/pics/btn/btn_action_plus.gif';
function showhideWhileLoading(CAT,n){
	var objCat 	= document.getElementById('div'+CAT);
	var objPic 	= document.getElementById('pic'+CAT);
	// n = 0 = not show
	// n = 1 = show
	if(n == 1){
		objCat.style.display="";
		if(objPic)
			document.images['pic'+CAT].src = picShowHideMinus.src;
	}else{
		objCat.style.display="none";
		if(objPic)
			document.images['pic'+CAT].src = picShowHidePlus.src;
	}
}
function showhideOnClick(CAT) { 
  	var objCat 	= document.getElementById("div"+CAT);
  	var objPic 	= document.getElementById("pic"+CAT); 
  
  	if (objCat && objCat.style.display=="none") {
		objCat.style.display="";
		//below is not working, now pre-populating the image
		//objPic.src = objPic.src.replace("btn_action_plus.", "btn_action_minus.");
		document.images['pic'+CAT].src = picShowHideMinus.src;
  	} 
  	else {
		if (objCat) { 
		  objCat.style.display="none"; 
		}
		//below is not working, now pre-populating the image
		//objPic.src = objPic.src.replace("btn_action_minus.", "btn_action_plus.");
		document.images['pic'+CAT].src = picShowHidePlus.src;
  	}
}
