function checkEmptyField(field){
	field.value = trim(field.value);

    if (field.value == null || field.value == "") {          
        return true;
    }
    return false;
}

function validNameChars(field){
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'- àâäÀÂÄçÇéèêëÉÈÊËìíîïÌÍÎÏôöòÔÖÒûùÛÙ";

	var checkStr = field.value;
	var allValid = true;
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	return allValid;
}

function checkAnswer(field){
	var checkStr = field.value;
	var allValid = true;
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		if (ch == "&")
		{
			allValid = false;
			break;
		}
	}
	return allValid;
}
function trim(string){

	while (string.charAt(0) == " "){

		string = string.substring(1);
	}
	while (string.charAt(string.length - 1) == " "){

		string = string.substring(0,string.length - 1);
		}
		
	return string;
}
function checkNumericValue(field) {
    var i = 0;
    var t = field.value;
    var x = new Number(t);

    if (t == "" || t.search(" ") != -1 || x.toString() == "NaN")
    {
       return false;
    }
    return true;
}

function validateStateProvince(stpr,country){	
	isvalidProvinceState = false;
	if(trim(country)=="CA" || trim(country)=="US"){
		if(trim(stpr) == "" ||  trim(stpr) == null){
			return isvalidProvinceState;
		}
		for(i=0; i<StateProvince.length;i++){
			if(stpr == StateProvince[i][1] && country == StateProvince[i][0]){		
				isvalidProvinceState =  true;
				break;		
			} 
		}
	}else
	 	isvalidProvinceState = true;	
	
	return isvalidProvinceState;
}

function isValidEmail(email)
{ 
  var emailval = email.value
  var result = false  
  var consecutivePeriods = false;
  var splCharsIncluded = false;
  var theStr = new String(emailval)
  var index = theStr.indexOf("@");
  for(i = 0; i < theStr.length; i++){
	if(theStr.charAt(i) == '.' && theStr.charAt(i+1) == '.'){
		consecutivePeriods = true;
		break;
	}
  }  
  if(emailval.search('!') != -1 || emailval.indexOf('#') != -1 || 
	  emailval.indexOf("$") != -1 || emailval.indexOf("%") != -1 || 
	  emailval.indexOf("*") != -1 || emailval.indexOf("(") != -1 || 
	  emailval.indexOf(")") != -1 || emailval.indexOf("{") != -1 || 
	  emailval.indexOf("}") != -1  || emailval.indexOf("\\") != -1 ||
	  emailval.indexOf(" ") != -1 || !checkInvalidChars(email)
	  ){ 
		splCharsIncluded = false; 
  } else {
	splCharsIncluded = true; 
  }
  if (index > 0 && index == theStr.lastIndexOf("@")  && 
	  theStr.charAt(theStr.length-1) != '.' && 
	  !consecutivePeriods && splCharsIncluded)
  { 
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function checkInvalidChars(field) {
    var t = field.value;
    var x = new Number(t);

    if (t.search("&") != -1 ||
        t.search(";") != -1 || 
        t.search("=") != -1 || 
        t.search("<") != -1 || 
        t.search('"') != -1 || 
        t.indexOf("^") != -1 ||         
        t.search(">") != -1)
    {
        return false;
    }
    return true;
}

function checkAlphanumericWithoutLen(field)
{
	// allow ONLY alphanumeric keys, no symbols or punctuation
	// this can be altered for any "checkOK" string you desire
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
	var checkStr = field.value;
	var allValid = true;
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		
		return (false);
	} 
	
	return allValid;
}

function checkString(field, fieldValue, len, msg, validChars)
{
	// field - the whole field
	// fieldValue - subset of the field string
	// len - the length required for string
	// msg - the error message for this validation
	// validChars - String containing all the valid characters
	
	var allValid = true;
	
	if (!checkLength(field, len)) {
		allValid = false;
		//alert(msg);
		field.select();		
		field.focus();
		return (false);	
	}
	
	for (i = 0;  i < fieldValue.length;  i++)
	{
		ch = fieldValue.charAt(i);
		for (j = 0;  j < validChars.length;  j++)
		if (ch == validChars.charAt(j))
			break;
		if (j == validChars.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		//alert(msg);
		field.select();		
		field.focus();
		return (false);
	} 
	
	return allValid;
}

function isValidCdnPostalCode(field1) {

	var str = field1.value;
	var msg = "Invalid Postal Code";
	var checkAlphaStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var checkNumericStr = "0123456789";	
		
	if (str.length != 6) {
		// If the length is 7 and the middle character is a space,
		// then the space can just be removed and we can continue
		// with the check.
		if (str.length == 7 && str.charAt(3) == ' ') {
			str = str.substring(0,3) + str.substring(4);
			field1.value = str;
		}
		else {
			//alert(msg);
			field1.select();		
			field1.focus();					
			return false;
		}
	}

	for (var i = 0; i < str.length; i++) {
		var value = str.charAt(i);

		switch (i % 2) {
			case 0:
				if (!checkString(field1,value,6,msg,checkAlphaStr))
					return false;
				break;
			case 1:
				if (!checkString(field1,value,6,msg,checkNumericStr))
					return false;
				break;
		}
		
	}
	return true;
	

}

function isValidUSPostalCode(field1) {

	var checkNumeric = "0123456789";	
	var msg = "Invalid Postal Code";

	return checkString(field1,field1.value,5,msg,checkNumeric);
	
}

function checkLength(field, length) {
    var i = 0;
    var t = field.value;

    if (t == "" || t.search(" ") != -1 || t.length != length )
    {
        return false;
    }
    return true;
}

function checkNumericValueIfNotNull(field) {
    var i = 0;
    var t = field.value;
    var x = new Number(t);

	if (t != "") {
	    if (t.search(" ") != -1 || x.toString() == "NaN")
	    {
	      //  alert("Entry in this field must be digits, all-numeric with no alpha characters.");
	        field.select();
	        field.focus();
	        return false;
	    }
	} 
	return true;
}

function checkAlphabet(field)
{
	// allow ONLY alphanumeric keys, no symbols or punctuation
	// this can be altered for any "checkOK" string you desire
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var checkStr = field.value;
	var allValid = true;
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		
		return (false);
	} 
	
	return allValid;
}

function isValidDate(dd,mm,yyyy) 
 { 		
		var TMonth = new Array('1', '2', '3','4','5', '6', '7', '8','9', '10', '11', '12');
		var MonthDays = new Array('31', '28', '31', '30','31', '30', '31', '31', '30', '31', '30', '31');
     	
		curYear=parseInt(yyyy);
     	
		if (( ( (curYear % 4)==0 && (curYear % 100)!=0 ) || (curYear % 400)==0)) { 
				 MonthDays[1] = '29'; 
		} 
		else{ 
				MonthDays[1] = '28';
		} 
	
		thisMonth = parseInt(mm);
		thisDay = parseInt(dd);
		
		if (thisDay > parseInt(MonthDays[thisMonth-1]))
			return false;
		else
			return true;
} 

function removeStr(field1,remStr){
	var str = field1.value;
	
	pos = str.indexOf(remStr) 
	if (pos != -1)
	{
		str = str.substring(0,pos) + str.substring(pos+1);
		field1.value = str;
	}
}

function removeSpaces(field){
	var str = field.value;
	for (i = 0;  i < str.length;  i++){
		if (str.charAt(i) == " ")
			str = str.substring(0,i) + str.substring(i+1);
	}
	field.value = str;
}



