// 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 check if the Date of Birth of the mother is not greater than 13 years.
 * It returns true if the date is valid, i.e., mother age is greater than 13,
 *otherwise returns false. It takes three parameter, the day, month, and year of the
 *mother's date of birth.
 */
function idValidMotherDOB(day, month, year)
{
    var limit = new Date();
    var yearLimit = limit.getYear();

    if (yearLimit >= 100 && yearLimit <= 1999)
    {
        yearLimit=yearLimit + 1900;
        yearLimit=yearLimit-13;
    }
    else
    {
        yearLimit=yearLimit-13;
    }

    limit.setYear(yearLimit);
    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;
}


/**
 * This function checks if the Date of Birth of the mother is not greater than 18 years if 
 * she has opted in for GTG.
 * It returns true if the date is valid, i.e., mother age is greater than 18,
 *otherwise returns false. It takes three parameter, the day, month, and year of the
 *mother's date of birth.
 */
function isValidGTGMotherDOB(day, month, year)
{
    var limit = new Date();
    limit.setYear(limit.getYear() - 18);
    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;
}

/**
 *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 0.
 */
function getMonthDaysArray(year) {
  for (var i = 0; i < 12; i++) {
    if (i==0 || i==2 || i==4 || i==6 || i==7 || i==9 || i==11) {
      this[i] = 31;
    }
    if (i==3 || i==5 || i==8 || i==10) {
      this[i] = 30;
    }
    if (i==1) {
      this[i] = getDaysInFebruary(year);
    }
   } 
   return this
}


function isEmailAddr(email)
{
  var result = false;

  var theStr = new String(trim(email.value));
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

//This method will check whether two email address is equalignorecase.


function isEmailIdentical(email,reemail)
{
  var result = false;
  var strEmail= new String(trim(email.value)).toLowerCase();
  var strReEmail= new String(trim(reemail.value)).toLowerCase();

 
  if (strEmail==strReEmail)
  {
   result = true;
  }
  return result;
}


function validRequired(formField)
{
	var result = true;
	if (formField.value == "")
	{
		//alert('Please enter a value for the "' + fieldLabel +'" field.');
		result = false;
	}

	return result;
}



function validEmailRequired(formField)
{
	var result = true;
	if (formField.value == "Enter User Name")
	{
		//alert('Please enter a value for the "' + fieldLabel +'" field.');
		result = false;
	}

	return result;
}

function validPwdRequired(formField)
{
	var result = true;
	if (formField.value == "Enter Password" || 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À-Ÿœ]{1,}['|.|\-|\s]?[a-zA-ZÀ-Ÿœ]*['|.|\-|\s]?[a-zA-ZÀ-Ÿœ]*['|.|\-|\s]?[a-zA-ZÀ-Ÿœ]*['|.|\-|\s]?[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À-Ÿœ]{1,}['|.|\-|\s]?[a-zA-ZÀ-Ÿœ]*['|.|\-|\s]?[a-zA-ZÀ-Ÿœ]*['|.|\-|\s]?[a-zA-ZÀ-Ÿœ]*['|.|\-|\s]?[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);

  if (!(stringto.search(/^[0-9-]{0,30}$/) != -1)) {
      //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 isStateMilitary(formField)
{
	var state = formField.value;
	var result = false;
	var initState = state.substring(0,2);

	if ((initState=="AA") || (initState=="AE") || (initState=="AP"))
	{
		result = true;
	}
	return result;
}

function isZipMilitary(formField)
{
	var zipcode = trim(formField.value);
	var initZip = zipcode.substring(0,3);

	if ( initZip != "090" && initZip != "091" && initZip != "092" && initZip != "093" && initZip != "094" && initZip != "095" && initZip != "096" && initZip != "097" && initZip != "098" && initZip != "099" && initZip != "340" && initZip != "962" && initZip != "963" && initZip != "964" && initZip != "965" && initZip != "966")
	{
		return false;
	} 
	else
	{
		return true;
	}
	
}

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) {
	  return false;
	 }
	 for (var i=0; i < zipcode.length; i++) {
	  temp = "" + zipcode.substring(i, i+1);
	  if (temp == "-") hyphencount++;
	  if (valid.indexOf(temp) == "-1") {
	   return false;
	  }
	 
	  if ((hyphencount > 1) || ((zipcode.length==10) && ""+zipcode.charAt(5)!="-")) {
	   return false;
		}
	 }
	 return true;
}
function isZipCA(formField)
{
	
	var result = true;
  var stringto = trim(formField.value);
	
	 if (!(stringto.search(/^([A-Za-z][0-9Ool][A-Za-z][ |-]*[0-9Ool][A-Za-z][0-9Ool])$/) != -1)) {
	      //alert('Please enter valid number for the "' + fieldLabel +'" field.');
      result = false;
  }

  return result;
}	
	
function validateAddress(string){	
		 
	 valid_chars = '1234567890-/\,.#)(-:;"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZàâçéèêëîïôûùüÿáéíñóúüÀÂÇÉÈÊËÎÏÔÛÙÜŸÁÉÍÑÓÚÜœŒ ';
         var isValid = true;
		 invalid_chars = '';
          //For every character on the string.   
         for(var index = 0; index < string.length; index++) {
         	var string1 = trim(string);
         	char = string1.substr(index, 1);                        
			
         //Is it a valid character?
          if(valid_chars.indexOf(char) == -1) 
		   {
	            isValid = false;
				break;
	         		 
		  } else 
				{
			isValid = true;
		}
          //If not, is it already on the list of invalid characters?
        //   if(invalid_chars.indexOf(char) == -1) {
          //If it's not, add it.
          
          }
         return isValid;
}

function validateCity(str){

		valid_chars = new String("-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZàâçéèêëîïôûùüÿáéíñóúüÀÂÇÉÈÊËÎÏÔÛÙÜŸÁÉÍÑÓÚÜœŒ ");
         var flag = true;
		 invalid_chars = '';
        
         //For every character on the string.   
         for(var index= 0; index < str.length; index++) {
			 var str1 = trim(str);
         char = str1.substr(index, 1); 
                              
         //Is it a valid character?
          if(valid_chars.indexOf(char) == -1) {
			 			flag = false ;
						break;
		  } else {
			flag= true;
		}
          //If not, is it already on the list of invalid characters?
        //   if(invalid_chars.indexOf(char) == -1) {
          //If it's not, add it.
          
          }
         return flag;
}

           function isValidBabyYear(str){
		 
		  var valid = "0123456789";
		  for (var i=0; i < str.length; i++) {
		  temp = "" + str.substring(i, i+1);
		  if (valid.indexOf(temp) == "-1") {
		   return false;
		  }
		  }
		   var year=parseInt(str);
	 	if(year<0){
		return false;
		}
		else{ 
		return true;
		}
		}
           function isValidBabyMonth(str){
          
          var valid = "0123456789";
		  for (var i=0; i < str.length; i++) {
		  temp = "" + str.substring(i, i+1);
		  if (valid.indexOf(temp) == "-1") {
		   return false;
		  }
          }
          var month=parseInt(str);
           if( month>11){
           return false;
           }
           else {
           return true;
           }
           }
           function validateConsumerZipcode(state,zip){
		   //alert("sta"+state+zip);
		   var mil_zipcode=new Array("090","091","092","093","094","095","096","097","098","099","340","962","963","964","965","966");
		   var i = 0;
		   var flag = false;
		   var zipcode;
		   if ( "AA" == state || "AE"==state || "AP"==state) {
		   flag=true;
		   	zipcode = zip.substring(0,3);
	           for(i=0; i < mil_zipcode.length ; i++ ) {
	        	    //alert("if"+mil_zipcode[i])
	        	    if (mil_zipcode[i]==zipcode){
	           		flag = false;
	           	  }              
			   }
        
		   }
		 	      	//alert("flag=="+flag);
		   	return flag;	   	
		  
		   }
