

//Javascript functions for Southtrust Site Redesign

//isEmpty(inputStr): check whether input string is empty. good for checking textboxes

//isButtonSelected(radioButtons)

//isCheckboxChecked(checkBoxes)

//isSelectSelected(Options)

//isValidEmail

//isAlphanumeric

//isAlphabetic

//isAlphanumericWithSpaces

//isAlphabeticWithSpaces

//isNumericOfLength (s,numberLength)

//isNumeric

//isInteger

//isSame

var defaultEmptyOK = false

function isEmpty(inputStr) {
	
	if (inputStr == "" || inputStr == null) {

	return true;

	}

	return false;
			
	}




function isSame(entry1, entry2) {

	var input1 = entry1
	
	var input2 = entry2

	if (input1 != input2) {

	return false;

	}

	return true;

	}

function isButtonSelected(radioButtons) {

	var i

	for (i = 0; radioButtons && i < radioButtons.length; i++) {

	if (radioButtons[i].checked) {

	return true;

	}
	
	}

	return false;
	
	}
	
	
function isCheckboxChecked(checkBoxes){


	var i;

	for (i = 0; checkBoxes && i < checkBoxes.length; i++) {

	if (checkBoxes[i].checked) {

	return true;

	}
	
	}

	return false;

	}	

function isSelectSelected(Options){


	var i;

	for (i = 0; Options && i < Options.length; i++) {

	if (Options[i].selected&&Options[i].value!="") {
	
	
	return true;

	}
	
	}

	return false;


}



function isValidEmail(emailStr) {

if (isEmpty(emailStr)) 
       if (isValidEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isValidEmail.arguments[1] == true);


	var emailPat=/^(.+)@(.+)$/
	
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	
	var validChars="\[^\\s" + specialChars + "\]"
	
	var quotedUser="(\"[^\"]*\")"
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	
	var atom=validChars + '+'
	
	var word="(" + atom + "|" + quotedUser + ")"
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null) {

	alert("E-mail address is incorrect (check @ and .'s).");

	return false;
	
	}

	var user=matchArray[1]
	
	var domain=matchArray[2]

	if (user.match(userPat)==" ") {

    	alert("The username is not valid.");

    	return false;
    	
	}

	var IPArray=domain.match(ipDomainPat)

	if (IPArray!=null) {

	for (var i = 1; i <= 4; i++) {

	if (IPArray[i] > 255) {

	alert("Destination IP address is not valid.");

	return false;
	
	}}

    	return true;

	}

	var domainArray=domain.match(domainPat)

	if (domainArray==null) {

	alert("The domain name is not valid.");
	
	return false;
	
	}

	var atomPat=new RegExp(atom,"g")
	
	var domArr=domain.match(atomPat)
	
	var len=domArr.length

	if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 4) {

   	alert("The address must end in a two letter country, or four letter domain.");

   	return false;
   	
	}

	return true;

	}
	
	
// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isAlphabetic (s)

{   var i;


   if (isEmpty(s)) 
      if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
      else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
           // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}


function isAlphabeticWithSpaces (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabeticWithSpaces.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabeticWithSpaces.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!(isLetter(c) || c == " "))
        return false;
    }

    // All characters are letters.
    return true;
}

// This function allows spaces and some commonly used special characters
//in alphabetic strings, including :".",",","-","'","&"
function isAlphabeticAllowSpecial (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabeticAllowSpecial.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabeticAllowSpecial.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!(isLetter(c) || c == " "||c=="."||c==","||c=="'"||c=="&"||c=="-"))
        return false;
    }

    // All characters are letters.
    return true;
}


// This function allows spaces and some commonly used special characters
//used in people's names in alphabetic strings, including ".","-","'"
function isPersonName (s)

{   var i;

    if (isEmpty(s)) 
       if (isPersonName.arguments.length == 1) return defaultEmptyOK;
       else return (isPersonName.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!(isLetter(c) || c == " "||c=="."||c=="'"||c=="-"))
        return false;
    }

    // All characters are letters.
    return true;
}

// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
        {
        return false;
        
        }
    }

    // All characters are numbers or letters.
    return true;
}


//branchcode contains only number or "-"
function isBranchCode (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);
        if (! (isDigit(c)||c=='-') )
        {
        return false;
        
        }
    }
    return true;
}


// This function allows spaces in alphanumeric strings
function isAlphanumericWithSpaces (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumericWithSpaces.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumericWithSpaces.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) || c == " ") )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}



// This function allows spaces and some commonly used special characters
//in alphanumeric strings, including :".",",","-","'","&"
function isAlphanumericAllowSpecial (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumericAllowSpecial.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumericAllowSpecial.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) || c == " "||c=="."||c==","||c=="'"||c=="&"||c=="-") )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}


function isNumericOfLength (s,numberLength)
{  
 if (isEmpty(s)) 
       if (isNumericOfLength.arguments.length == 2) return defaultEmptyOK;
       else return (isNumericOfLength.arguments[2] == true);
	return (isInteger(s) && s.length == numberLength);
}

function isNumericBetween (s,startLength,endLength)
{  
 if (isEmpty(s)) 
       if (isNumericBetween.arguments.length == 3) return defaultEmptyOK;
       else return (isNumericBetween.arguments[3] == true);
	return (isInteger(s) && (s.length >= startLength&&s.length<=endLength));
}



function isNumeric (s)
{
 	if (isEmpty(s)) 
       if (isNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isNumeric.arguments[1] == true);
	
 	return (isInteger(s));

}

////////////////////////////////////////////////////////////
// some checking functions that different applications share. 
//
function checkRelationship(checkBoxes){

var i;

var no_relationship;
var yes_relationship;

for (i = 0; checkBoxes && i < checkBoxes.length; i++) {

	if (checkBoxes[i].checked && checkBoxes[i].value=="No Relationship") {
	no_relationship=true;
	}

	if (checkBoxes[i].checked && checkBoxes[i].value!="No Relationship") {
	yes_relationship=true;
	}

	if (no_relationship==true && yes_relationship==true){

	return false;
	}
}//end of for loop
return true;

} //end of function



//strip off the "," and then validate whether it is a number. 

function checkNumeric(theObject){

theObject.value=stripCharsInBag(theObject.value,",");

if (isNaN(theObject.value))
return false;
else return true;
}


function checkInteger(theObject){

theObject.value=stripCharsInBag(theObject.value,",");

theObject.value=stripDecimals(theObject.value);

if (!isInteger(theObject.value))
return false;
else return true;
}




////////////////////////////////////////////////////////////
//supporting functions

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}



// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isInteger (s)
{   var i;

     // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function checkNumeric(objName,minval, maxval,comma,period,hyphen)
{
	if (minval=="") minval = 0;
	if (maxval=="") maxval = 1000000000;
	if (comma=="") comma = ',';
	if (period=="") period = '.';
	if (hyphen=="") hyphen = '';

	var numberfield = objName;
	
	if(!numberfield)
		return false;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		//numberfield.select();
		numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, or hyphen)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
		if (!checkStr)
		return false;

	if (checkStr.value.length==0) return false;
	
	for (i = 0;  i < checkStr.value.length;  i++)
	{
	ch = checkStr.value.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length)
	{
	allValid = false;
	break;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid)
	{/*	
	alertsay = "Please enter only these values \""
	alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
	alert(alertsay);*/
	return (false);
	}

	// set the minimum and maximum
	var chkVal = allNum;
	var prsVal = parseFloat(allNum);
	//alert(prsVal);
	if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
	{/*
	alertsay = "Please enter a value greater than or "
	alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
	alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
	alert(alertsay);*/
	return (false);
	}
}



function load(file,target) {
	if (target=="parent")
		top.opener.window.location.href = file;
	else if (target=="new") {
		var handle = window.open("","popup","width="+500+",height="+500+",toolbar=0,location=1,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,outerwidth=0,outerheight=0,left=0,top=0,alwaysraised=1");
		handle.location.href = file;
	}
	else
		window.location.href = file;
}


function openit(myURL, myW, myH)
{
	window.open(myURL,"popup","width="+myW+",height="+myH+",toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,outerwidth=0,outerheight=0,left=0,top=0,alwaysraised=1");
}

function back2opener(myURL)
{
	self.opener.window.location = myURL;
	self.opener.window.focus();
	//self.close();
}

function focus2opener(myURL)
{
	self.opener.window.location = myURL;
	self.opener.window.focus();
}

function stripDecimals(s){

	var x=s.indexOf('.');

	if (x<0) return s;

	var t= s.substring(0, x);

	return t;
}

function checkAnswer(){


	if (document.MAIN_FORM.browser[1].checked){

		openWindow('/SiteAssets/forms/form/popup_browser_detect.html','pop01','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=630,height=440');

		return false;
	} 

	if (document.MAIN_FORM.printer[1].checked){

		openWindow('/Siteassets/forms/form/popup_download_info.html','pop01','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=630,height=440');

		return false;
	} 


	if (document.MAIN_FORM.disclosure[1].checked){

		openWindow('/Siteassets/forms/form/popup_elect_disclosure.html','pop01','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=630,height=440');

		return false;
	} 

	return true;
 }
 
function round(number,X) {
	// rounds number to X decimal places, defaults to 2
	X = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,X+2))/Math.pow(10,X);
}

     
 function openWindow(theURL,winName,features) {
       window.open(theURL,winName,features);
}


function checkRequiredField(theObject) {
	//alert(theObject.name + "-->"+theObject.type);
	if (theObject) {
		if (theObject.type=="text" || theObject.type=="textarea" || theObject.type=="hidden") {
			//alert(theObject.value.length);
			if (theObject.value.length>0) {
				return true;
			}
		} else if (theObject.type=="checkbox" || theObject.type=="radio") {
			if (theObject[0]) {
				for (var i = 0; i < theObject.length; i++) {
					if (theObject[i].checked && theObject[i].value.length>0) {
						return true;
					}
				}
			} else if (theObject.checked) {
				return true;
				
			}
		} else if (theObject.type=="select-one" || theObject.type=="dropdown") {
			for (i = 0; i < theObject.length; i++) {
				if (theObject[i].selected && theObject[i].value.length>0) {
					return true;
				}
			}
		} else if (!theObject.type) {	// this is for the Radio Button
			if (theObject[0]) {
				for (var i = 0; i < theObject.length; i++) {
					if (theObject[i].checked && theObject[i].value.length>0) {
						return true;
					}
				}
			} else if (theObject.checked) {
				return true;
				
			} else {
				return true;
			}
		
		} else {
			return true;
		}
	} else {
		return true;
	}
	
	//theObject.focus();
	return false;
}

function getCheckedValues (select) {
  var r = new Array();
  for (var i = 0; select && i < select.length; i++)
    if (select[i].checked)
      r[r.length] = select[i].value;
  return r;
}

function getSelectedValues (select) {
  var r = new Array();
  for (var i = 0; select && i < select.options.length; i++)
    if (select.options[i].selected)
      r[r.length] = select.options[i].value;
  return r;
}
function getSelectedTexts (select) {
  var r = new Array();
  for (var i = 0; select && i < select.options.length; i++)
    if (select.options[i].selected)
      r[r.length] = select.options[i].text;
  return r;
}
function getSelectedIndices (select) {
  var r = new Array();
  for (var i = 0; select && i < select.options.length; i++)
    if (select.options[i].selected)
      r[r.length] = i;
  return r;
}


function compareDates (value1, value2, encoding, delimiter) {
	var date1, date2;
	var month1, month2;
	var year1, year2;
	
//	if (encoding=="en") {
//		month1 = value1.substring (0, value1.indexOf (delimiter));
//		date1 = value1.substring (value1.indexOf (delimiter)+1, value1.lastIndexOf (delimiter));
//		year1 = value1.substring (value1.lastIndexOf (delimiter)+1, value1.length);

//		month2 = value2.substring (0, value2.indexOf (delimiter));
//		date2 = value2.substring (value2.indexOf (delimiter)+1, value2.lastIndexOf (delimiter));
//		year2 = value2.substring (value2.lastIndexOf (delimiter)+1, value2.length);
//	} else {
		date1 = value1.substring (0, value1.indexOf (delimiter));
		month1 = value1.substring (value1.indexOf (delimiter)+1, value1.lastIndexOf (delimiter));
		year1 = value1.substring (value1.lastIndexOf (delimiter)+1, value1.length);

		date2 = value2.substring (0, value2.indexOf (delimiter));
		month2 = value2.substring (value2.indexOf (delimiter)+1, value2.lastIndexOf (delimiter));
		year2 = value2.substring (value2.lastIndexOf (delimiter)+1, value2.length);
//	}
	
	if (year1 > year2) return 1;
	else if (year1 < year2) return -1;
	else if (month1 > month2) return 1;
	else if (month1 < month2) return -1;
	else if (date1 > date2) return 1;
	else if (date1 < date2) return -1;
	else return 1;
} 

