function ifNumberPresent(str) { // check if the string contains number
	var regFormat = /\d/;
	var sPos = -1;
	
	sPos = str.search(regFormat);
	if (sPos != -1) {
		return true;
	}
	else {
		return false;
	}
}


function ifLessThanEqualToDesiredLength(str,len) { // check if the string is of desired length.
	if (str.length<=len) {
		return true;
	}
	else {
		return false;
	}
}


// check email address
function checkEmail(sEmail) {
	var regFormat = /^[A-Za-z0-9]+((-\w+)|((\.|_)\w*))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	//var regFormat = /^.+\@.+\..+$/;
	var sPos = -1;
	
	sPos = sEmail.search(regFormat);
	if (sPos != -1) {
		return true;
	}
	else {
		return false;
	}
}

// A email list is consisted of email addresses and separated by ,
function validateEmailList(_emailList) {
	//var validCharacters = /[^A-Za-z0-9\-\,\.\@\s]/;
	var validCharacters = /[^A-Za-z0-9\-\,\_\.\@\s]/;
	var emails = new Array();
	var validEmail = true;
	var badCharPos = -1;

	if (_emailList.search(validCharacters) != -1) {
		// alert("emaillist is bad");
		return false;
	}
	
	emails = _emailList.split(",");
	
	for (i=0; i < emails.length; i++) {
		validEmail = validEmail && checkEmail(trim(emails[i]));	
	}
	
	return validEmail;
}

// check us zip code format
function isUSZip(strValue) {
	// 99999 or 99999-9999

	var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
 
	//check for valid US Zipcode
	return objRegExp.test(strValue);
}

// check canadian zip code format
function checkCAZip(sZip) {
	// X1X1X1, or X1X-1X1, or X1X 1X1 
	
	var objRegExp = /(^[A-Za-z]\d[A-Za-z](\s|-)?\d[A-Za-z]\d$)/;
   
	return objRegExp.test(sZip);
}

// check if it is a leap year
function isLeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { 
			return true; 
		}
	} else {
		if ((intYear % 4) == 0) { 
			return true; 
		}
	}
	return false;
}

// PR# 1-1QR3U/WR020603-1 - STARTS
// Modifying the state validation to include US and Canadian country 
// validation individually 

function validateState(sState) {
	
	return (isCanadianProvince(sState) || isUSState(sState));
}

// PR# 1-1QR3U/WR020603-1 - ENDS

// PR# 1-1QR3U/WR020603-1 -- This method validates US States - STARTS 
function isUSState(sState) {

	var sTemp = "";
	var arrayState = new Array();

	arrayState["AL"] = "Alabama";
	arrayState["AK"] = "Alaska";
	arrayState["AS"] = "American Samoa";
	arrayState["AZ"] = "Arizona";
	arrayState["AR"] = "Arkansas";
	arrayState["CA"] = "California";
	arrayState["CO"] = "Colorado";
	arrayState["CT"] = "Connecticut";
	arrayState["DE"] = "Delaware";
	arrayState["DC"] = "District of Columbia";
	arrayState["FM"] = "Federated States of Micronesia";
	arrayState["FL"] = "Florida";
	arrayState["GA"] = "Georgia";
	arrayState["GU"] = "Guam";
	arrayState["HI"] = "Hawaii";
	arrayState["ID"] = "Idaho";
	arrayState["IL"] = "Illinois";
	arrayState["IN"] = "Indiana";
	arrayState["IA"] = "Iowa";
	arrayState["KS"] = "Kansas";
	arrayState["KY"] = "Kentucky";
	arrayState["LA"] = "Louisiana";
	arrayState["ME"] = "Maine";
	arrayState["MH"] = "Marshall Islands";
	arrayState["MD"] = "Maryland";
	arrayState["MA"] = "Massachusetts";
	arrayState["MI"] = "Michigan";
	arrayState["MN"] = "Minnesota";
	arrayState["MS"] = "Mississippi";
	arrayState["MO"] = "Missouri";
	arrayState["MT"] = "Montana";
	arrayState["NE"] = "Nebraska";
	arrayState["NV"] = "Nevada";
	arrayState["NH"] = "New Hamsphire";
	arrayState["NJ"] = "New Jersey";
	arrayState["NM"] = "New Mexico";
	arrayState["NY"] = "New York";
	arrayState["NC"] = "North Carolina";
	arrayState["ND"] = "North Dakota";
	arrayState["MP"] = "Northern Marinana Islands";
	arrayState["OH"] = "Ohio";
	arrayState["OK"] = "Oklahoma";
	arrayState["OR"] = "Oregon";
	arrayState["PW"] = "Palau";
	arrayState["PA"] = "Pennsylvania";
	arrayState["PR"] = "Puerto Rico";
	arrayState["RI"] = "Rhode Island";
	arrayState["SC"] = "South Carolina";
	arrayState["SD"] = "South Dakota";
	arrayState["TN"] = "Tennessee";
	arrayState["TX"] = "Texas";
	arrayState["UT"] = "Utah";
	arrayState["VT"] = "Vermont";
	arrayState["VI"] = "Virgin Islands";
	arrayState["VA"] = "Virginia";
	arrayState["WA"] = "Washington";
	arrayState["WV"] = "West Virginia";
	arrayState["WI"] = "Wisconsin";
	arrayState["WY"] = "Wyoming";
	arrayState["AE"] = "Armed Forces Africa/Armed Forces Canada/Armed Forces Europe/Armed Forces Middle East";
	arrayState["AA"] = "Armed Forces Americas";
	arrayState["AP"] = "Armed Forces Pacific";


	sState = trim(sState.toUpperCase());
	sTemp = arrayState[sState];
	if ((sTemp) && (sTemp != null)) {
		if (sTemp == "") {
			
			return false;
		} else {
			return true;
		}
	} else {
		
		return false;
	}
}

// PR# 1-1QR3U/WR020603-1 -- This method validates US States - ENDS



// PR# 1-1QR3U/WR020603-1 -- This method validates Canadian Province - STARTS
function isCanadianProvince(sState) {

	var sTemp = "";
	var arrayState = new Array();


	// Canadian provinces
	arrayState["AB"] = "Alberta";
	arrayState["BC"] = "British Columbia";
	arrayState["MB"] = "Manitoba";
	arrayState["NB"] = "New Brunswick";
	arrayState["NL"] = "Newfoundland and Labrador";
	arrayState["NT"] = "Northwest Territories";	
	arrayState["NS"] = "Nova Scotia";
	arrayState["NU"] = "Nunavut";
	arrayState["ON"] = "Ontario";
	arrayState["PE"] = "Prince Edward Island";
	arrayState["QC"] = "Quebec";
	arrayState["SK"] = "Saskatchewan";
	arrayState["YT"] = "Yukon Territory";
	
	sState = trim(sState.toUpperCase());
	sTemp = arrayState[sState];
	if ((sTemp) && (sTemp != null)) {
		if (sTemp == "") {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}
// PR# 1-1QR3U/WR020603-1 -- This method validates Canadian Province - ENDS


function isValidDate(sMMDDYYYY) {

	// sMMDDYYYY needs to be in the form of 00/11/2222
	
	if (sMMDDYYYY.length == 8) {
		// eg. 00112222
		sMMDDYYYY = sMMDDYYYY.substring(0, 2) + "/" + sMMDDYYYY.substring(2, 4) + "/" + sMMDDYYYY.substring(4, 8);
	}

   var iMM = parseInt(sMMDDYYYY.substring(0, 2), 10);
   var iDD = parseInt(sMMDDYYYY.substring(3, 5), 10);
   var iYYYY = parseInt(sMMDDYYYY.substring(6, 10), 10);
   var arrayDayCntOfMonth = new Array();

   arrayDayCntOfMonth[1] = 31;    // January
   if (isLeapYear(iYYYY)) {
      arrayDayCntOfMonth[2] = 29; // February with leap day
   } else { 
      arrayDayCntOfMonth[2] = 28; // February normal year
   }
   arrayDayCntOfMonth[3] = 31;    // March
   arrayDayCntOfMonth[4] = 30;    // April
   arrayDayCntOfMonth[5] = 31;    // May
   arrayDayCntOfMonth[6] = 30;    // June
   arrayDayCntOfMonth[7] = 31;    // July
   arrayDayCntOfMonth[8] = 31;    // August
   arrayDayCntOfMonth[9] = 30;    // September
   arrayDayCntOfMonth[10] = 31;   // October
   arrayDayCntOfMonth[11] = 30;   // November
   arrayDayCntOfMonth[12] = 31;   // December
   
   return (iDD <= arrayDayCntOfMonth[iMM]);
}

function isLastDayOfMonth(sMMDDYYYY) {
	var iMM = parseInt(sMMDDYYYY.substring(0, 2), 10);
	var iDD = parseInt(sMMDDYYYY.substring(3, 5), 10);
	var iYYYY = parseInt(sMMDDYYYY.substring(6, 10), 10);
	var arrayDayCntOfMonth = new Array();

	arrayDayCntOfMonth[1] = 31;     // January
	if (isLeapYear(iYYYY)) {
		arrayDayCntOfMonth[2] = 29; // February with leap day
	} else { 
		arrayDayCntOfMonth[2] = 28; // February normal year
	}
    arrayDayCntOfMonth[3] = 31;    // March
    arrayDayCntOfMonth[4] = 30;    // April
    arrayDayCntOfMonth[5] = 31;    // May
    arrayDayCntOfMonth[6] = 30;    // June
    arrayDayCntOfMonth[7] = 31;    // July
    arrayDayCntOfMonth[8] = 31;    // August
    arrayDayCntOfMonth[9] = 30;    // September
    arrayDayCntOfMonth[10] = 31;   // October
    arrayDayCntOfMonth[11] = 30;   // November
    arrayDayCntOfMonth[12] = 31;   // December

	return (iDD == arrayDayCntOfMonth[iMM]);
}

function isCurrencyFormat(s) { 
	// retired!! do not use
   var temp_value = s; 
   if (temp_value == "") { 
      s = "0.00"; 
      return; 
   } 
   var Chars = "0123456789.,"; 
   for (var i = 0; i < temp_value.length; i++) { 
       if (Chars.indexOf(temp_value.charAt(i)) == -1) { 
           alert("Invalid Character(s)\n\nOnly numbers (0-9), a comma, and a period are allowed in this field."); 
           return; 
       } 
   } 
} 

function isCurrency(s) {
	var objRegExp = /(^\$\d{1,3}(,\d{3})*\.\d{2}$)|(^\(\$\d{1,3}(,\d{3})*\.\d{2}\)$)/;

	return objRegExp.test(s);
}

// used in multibox where only one selection can be made
function checkForSingleSelection(fieldName, currentIndex)
{
	var noOfCheckBox = fieldName.length;
	if(fieldName[currentIndex].checked == true)
	{
		for (var index = 0; index < noOfCheckBox; index++) {
			if ((currentIndex != index) && (fieldName[index].checked == true)) {
				fieldName[index].checked = false;
			}
		}
	}
}

// check if s is a valid ssn format
function isSSN(s) {

	var objRegExp  = /^\d{3}\-\d{2}\-\d{4}$/;
 
  	//check for valid SSN
	return objRegExp.test(s);

}

// check if s is a valid us phone format
function isUSPhone(s) {
	//US phone pattern. 
	//Ex. (999) 999-9999 or (999)999-9999

	var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
 
	//check for valid us phone with or without space between 
	//area code
	return objRegExp.test(s); 
}

function stripUSPhone(s) {
	if(isUSPhone(s)) {
		var objRegExp  =  /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
		return objRegExp(s);
	}
}

// check if s is numberic
function isNumeric(s) {
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
	//check for numeric characters 
	return objRegExp.test(s);
}

// check if s is integer
function isInteger(s) {
	var objRegExp  = /(^-?\d\d*$)/;
 
	//check for integer characters
	return objRegExp.test(s);
}

// check if s is a us date
function validateUSDate(s) {
	// Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
	//check to see if in correct format
	if(!objRegExp.test(s)) {
		return false; //doesn't match pattern, bad date
	} else {
    	var arrayDate = s.split(RegExp.$1); //split date into month, day, year
		var intDay = parseInt(arrayDate[1],10); 
		var intYear = parseInt(arrayDate[2],10);
		var intMonth = parseInt(arrayDate[0],10);
	
		//check for valid month
		if(intMonth > 12 || intMonth < 1) {
			return false;
		}
	
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null) {
			if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
				return true; //found in lookup table, good date
		}
		
		//check for February
		var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
		if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0) {
			return true; //Feb. had valid number of days
		}
		return false; //any other values, bad date
	}
}

// Ensures the month does not have too many days 
	function isDaysValid(days, month) {
		//April,June,September,November,February
		if(month == "3" || month == "5" || month == "8" || month == "10") {
			if(days > 30) {
				return false;
			}
		} else if(month == "1") {
			if(days > 29) {
				return false;
			}
		}
		
		return true;

	}
	


//To check a dropdown value selected on not (Assuming index 0 is empty or display value with some text like "Select"
//Parms - form - Name of the Form
//Parms - field - Name of the field
//errorField - display value eg-->Please select State (Error Field value should be State)
function checkSelectedValue(form, field, errorField)
{
	if (eval(form + "." + field + ".selectedIndex") == 0)
	{
		return errorField;
	}
	return true;
}	

//To check a text value is empty or not
//Parms - form - Name of the Form
//Parms - field - Name of the field
//errorField - display value eg-->Please enter City (Error Field value should be City)
function checKBlankField(form, field, errorField) 
{
	var val = eval(form + "." + field + ".value");
	val = trim(val);
	if(val == null || val == "")
		return errorField;
	else
		return true;
}	

//To check a textfield value based on a dropdown value (For eg, user should enter a state name into a text box if he selects "others" in state dropdown
//Parms - form - Name of the Form
//Parms - field - Name of the field(Dropdown name)
//Parms - value - DropDown Value
//Parms - txtFldName - Name of the text field
//errorField - display value eg-->Please select State (Error Field value should be State)
function checKBlankFieldForDropDown(form, field, value, txtFldName, errorField) 
{
	var index = eval(form + "." + field + ".selectedIndex");
	if (eval(form + "." + field + "[" + index + "].value") == value)
	{
		var val = eval(form + "." + txtFldName + ".value");
		val = trim(val);
		if(val == null || val == "")
			return errorField;
		else
			return true;
	}
	else
		return true;
}

	//To check a textfield value based on a dropdown value (For eg, user should enter a state name into a text box if he selects "others" in state dropdown
	//Parms - form - Name of the Form
	//Parms - field - Name of the field(Dropdown name)
	//Parms - value - DropDown Value
	//Parms - txtFldName - Name of the text field
	//errorField - display value eg-->Please select State (Error Field value should be State)
	function checKBlankFieldForCheckBox(form, field, txtFldName, errorField) 
	{
		if (eval(form + "." + field + ".checked") == true)
		{
			var val = eval(form + "." + txtFldName + ".value");
			val = trim(val);
			if(val == null || val == "")
				return errorField;
			else
				return true;
		}
		else
			return true;
	}
	
	//To check a text value is empty and contains alphabets or not
	//Parms - form - Name of the Form
	//Parms - field - Name of the field
	//errorField - display value eg-->Please enter City (Error Field value should be City)
	function checKForBlankAlphabet(form, field, errorField) 
	{
		var rtnval = checKBlankField(form, field, errorField);
		if (rtnval == true )
		{
			var val = eval(form + "." + field + ".value");
			var regExp = /[^A-Za-z ]/;
			val = trim(val);
			if (regExp.test(val))
				return errorField;
			else
				return true;
		}
		else 
			return rtnval;
			
	}
	
	function formatSpaceToBlank(str) {
		var org = str;
		var search =" ";
		var replaceWith ="";
		var result="";

		var i=0;
		do { 
			i=org.indexOf(search);
			if(i!=-1){
				result = org.substring(0,i);
				result = result + replaceWith;
				result = result + org.substring(i + search.length);
				org = result;
			} 
		}while(i!=-1);

		return org;
	    }
	
	function isStateMandatoryForCountry(formName, fieldName) {
		var country = eval(formName + "." + fieldName + ".options[" + formName + "." + fieldName + ".selectedIndex].value");
		return (country=="US" || country=="CA" || country=="MX" || country=="AU" || country=="NZ");
		
	}
	
	function isZipMandatoryForCountry(formName, fieldName) {
		var country = eval(formName + "." + fieldName + ".value");
		return (country=="US" || country=="CA" || country=="MX" || country=="AU" || country=="NZ" || country=="UK" || country=="GB");
	}
	
