
// Form field validation function

function trim(s) {
    while (s.substring(0,1) == ' ') {
      s = s.substring(1,s.length);
    }
    while (s.substring(s.length-1,s.length) == ' ') {
      s = s.substring(0,s.length-1);
    }
    return s;
}

/**
 * This function checks if the Date of Birth of the user is not greater than 13 years.
 * Returns true if date is valid, false otherwise. Accepts the day, month and year as parameters.
 */
function isNotUnderAge(day, month, year)
{
    var limit = new Date();
    limit.setYear(limit.getYear() - 13);
    limit = limit.getTime();

    var motherDOB = new Date();
    motherDOB.setYear(year);
    motherDOB.setMonth(month);
    motherDOB.setDate(day);
    motherDOB = motherDOB.getTime();

    if(motherDOB >= limit) {
      return false;
    }
    return true;
}
function isNotUnderAgeForFebreze (month, year)
{
    var limit = new Date();
    limit.setYear(limit.getYear() - 13);
    limit = limit.getTime();

    var motherDOB = new Date();
    motherDOB.setYear(year);
    motherDOB.setMonth(month);
    motherDOB.setDate(1);
    motherDOB = motherDOB.getTime();

    if(motherDOB >= limit) {
      return false;
    }
    return true;
}



/**
 *This function returns the number of days in february if year is supplied as parameter.
 */
function getDaysInFebruary (year) {
  // February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/**
 *This function returns the number of days in february if year is supplied as parameter.
 */
function getDaysInfebruary(year) {
  // February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


/**
 * This function returns an array of month days. Month January is at index 1.
 */
function getMonthDaysArray(year) {
  var aName = new Array( );


   for (var i = 1; i < 13; i++) {
     if (i==1) {
       aName[i] = 31;
     }
     if (i==4 || i==6 || i==9 || i==11) {
       aName[i] = 30;
     }
     if (i==2) {
       aName[i] = getDaysInfebruary(year);
     }
    }

    return aName
}

function validateEmail(str) {
  if (window.RegExp) {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    var reg1 = new RegExp(reg1str);
    var reg2 = new RegExp(reg2str);
    if (!reg1.test(str) && reg2.test(str)){
      return true;
    } else {
    	return false;
    }
  } else {
  	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  }
}

function isEmailAddr(email)
{
	var str = email.value;
	//alert("str : "+str)
	if (str != "")
	{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    return false
		 }
	}
	 return true		
}

function validRequired(formField)
{
	var result = true;
	if (formField.value == "")
	{
		//alert('Please enter a value for the "' + fieldLabel +'" field.');
		result = false;
	}

	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;
  str = trim(str);
	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}

	return result;
}


/*function validEmail(formField)
{
	var result = true;

	if (formField.value.length < 3) || !isEmailAddr(formField.value) )
	{
		//alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		result = false;
	}

  return result;

}*/

function validInt(formField)
{
	var result = true;
  var formFieldValue = trim(fromField.value);
 	if (result)
 	{
 		var num = parseInt(formFieldValue, 10);
 		if (isNaN(num))
 		{
 			//alert('Please enter a number for the "' + fieldLabel +'" field.');
			result = false;
		}
	}

	return result;
}
function validCharacters(formField)
{
	var result = true;
  var stringto = trim(formField.value);

  if (!(stringto.search(/^[a-zA-Z]*$/) != -1)) {
      //alert('Please enter only characters for the "' + fieldLabel +'" field.');
      result = false;
  }

  return result;
}
function validCharactersLastName(formField)
{
	var result = true;
  var stringto = trim(formField.value);

  if (!(stringto.search(/^[a-zA-Z]*[ ]*[\-]?[ ]*[a-zA-Z]*$/) != -1)) {
      //alert('Please enter only characters for the "' + fieldLabel +'" field.');
      result = false;
  }

  return result;
}

function validCharactersName(formField)
{
	var result = true;
  var stringto = trim(formField.value);

  if (!(stringto.search(/^[a-zA-Z]*[ ]*[\-]?[ ]*[a-zA-Z]*$/) != -1)) {
      //alert('Please enter only characters for the "' + fieldLabel +'" field.');
      result = false;
  }

  return result;
}


function validPhone(formField)
{
    var result = true;
    var stringto = trim(formField.value);

    var exp = "\\(?\\d{3}\\)?[- ]?\\d{3}[- ]?\\d{4}";
	var reg = new RegExp(exp);
    if (stringto != "") {
      if (!reg.test(stringto)) {
          //alert('Please enter valid number for the "' + fieldLabel +'" field.');
          result = false;
      }
    }
    return result;
}

function validZip(formField)
{
	var result = true;
  var stringto = trim(formField.value);

  if (!(stringto.search(/^[0-9]{0,10}$/) != -1)) {
      //alert('Please enter valid number for the "' + fieldLabel +'" field.');
      result = false;
  }

  return result;
}

function isZipUS(s) 
{
     /*
	  Check for correct zip code
     */
	 
	 var zipcode = trim(s);
	 var valid = "0123456789-";
	 var hyphencount = 0;
	 if (zipcode.length!=5 && zipcode.length!=10) {
		 //alert("Please enter your 5 digit or 5 digit+4 zip code.");
		 return false;
	 }
	 for (var i=0; i < zipcode.length; i++) {
		 temp = "" + zipcode.substring(i, i+1);
		 if (temp == "-") hyphencount++;
		 if (valid.indexOf(temp) == "-1") {
		 	//alert("Invalid characters in your zip code.  Please enter number values.");
		   	return false;
		 }
		 
		 if ((hyphencount > 1) || ((zipcode.length==10) && ""+zipcode.charAt(5)!="-")) {
			 	//alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			    return false;
			}
	 }
	 return true;
}

function isZipCA(s) {
     var zipcode = s;
	 var flag = "failure";
	 
	 if (zipcode.length!=6 && zipcode.length!=7) {
	 
	  	return false;
	 }
	 if(zipcode.length==6){
	 	for(var i=0; i<zipcode.length; i++){
	 		var c = zipcode.charAt(i);
	 		if(!isLetter(c) && !isDigitCA(c)) {
			alert(i);
	 			return false;
	 		}
	 	}
	 	return true;
	 }else if(zipcode.length==7) {
	 	for(var i=0; i<zipcode.length; i++){
		 	var c = zipcode.charAt(i);
			if(i==3){
				if(c!=' ' && c!='-'){
					return false;
				}
			}else if( !isLetter(c) && !isDigitCA(c) ){
	 			return false;
	 		}
	 	}
	 	return true;
	 }
	 
	 return true;
}//end of function isZipCA()

function isZipUSzero(str){
	var reg = new RegExp("^([0]{5})?([- ]?[0]{4})?$"); 
	if(reg.test(str))
		return false;
	else 
		return true;
}

function isLetter (c)
{   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


function isDigitCA (c)
{   
	return (((c >= "0") && (c <= "9")) || (c == "O") || (c == "o") || (c == "l"));
}

function isSpace(s)
{
	var whitespace = " \t\n\r"; 
	return ((s != null) && (s.length == 1) && (whitespace.indexOf(s) != -1));
}

function isWhitespaceOrHyphen(s)
{   
	if ((isSpace(s)) || (isHyphen(s)))
	{
		return true;
	}
	return false;
}

function isHyphen(c)
{   
	return (c == "-");
}

function removeWhiteSpace(str){
	if(null==str || str == "" ){
		return str;
	}
	var res = "";
	for(var i=0; i<str.length; i++){
		if(!isSpace(str.charAt(i))){
			res = res + str.charAt(i);
		}
	}
	return res;
}