
// 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;
}

/**
 * This function checks if the Date of Birth of the user is not greater than 18 years.
 * Returns true if date is valid, false otherwise. Accepts the day, month and year as parameters.
 */
function isNotUnderEighteen(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();
	//alert("motherDOB"+motherDOB);
	//alert("limit"+limit);
    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 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 && year!=-1) {
       aName[i] = ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 
     }
    }

    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);
  }
}

//Validate email domain for string.
function validateEmailDomain(email) {

	var returnVal = false;    
    var index = email.lastIndexOf('.') + 1;
	var email_domain = email.substr(index);
    var domains = new Array(24);
	domains[0] = "biz";
	domains[1] = "com";
	domains[2] = "coop";
	domains[3] = "edu";
	domains[4] = "gov";
	domains[5] = "info";
	domains[6] = "int";
	domains[7] = "jobs";
	domains[8] = "mil";
	domains[9] = "mobi";
	domains[10] = "museum";
	domains[11] = "name";
	domains[12] = "net";
	domains[13] = "org";
	domains[14] = "pro";
	domains[15] = "tel";
	domains[16] = "travel";
	domains[17] = "pro";
	domains[18] = "post";
	domains[19] = "geo";
	domains[20] = "ca";
	domains[21] = "us";
	domains[22] = "fr";

	if(index != 0){
		for(i = 0; i < domains.length && returnVal == false; i++) {
			if(email_domain == domains[i]) {
				returnVal = true;
			}
		}
	}

return returnVal;
}

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;
}

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 = /^(\()?[1-9]\d{2}(\))?(\s|-|.)?\d{3}(\s|-)?\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 == "-" || 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)!="-" && ""+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 = trim(s);
	 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);
			var alphaFlag = isLetter(c);
			var digitFlag = isDigit(c);
			if(i%2 == 0 && !alphaFlag) {
				return false;
			}
			if(i%2 == 1 && !digitFlag) {
				return false;
			}
	 	}
	 	return true;
	 }else if(zipcode.length==7) {
	 	for(var i=0; i<zipcode.length; i++){
		 	var c = zipcode.charAt(i);
			var alphaFlag = isLetter(c);
			var digitFlag = isDigit(c);
			if(i==3 && c!=' ' && c!='-'){
				return false;
			}else{
				if((i==0 || i==2 || i==5) && !alphaFlag){
					return false;
				}
				if((i==1 || i==4 || i==6) && !digitFlag) {
					return false;
				}
	 		}
	 	}
	 	return true;
	 }
	 return true;
}//end of function isZipCA()


function isLetter (c)
{   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"));
}


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;
}

function countWords(paragraph_text){
	var total_words;
    if (paragraph_text.length < 2) {
		total_words = 0;
    }
    var paragraph_text = paragraph_text + " ";
	var clean_text = paragraph_text;
	clean_text = clean_text.replace(/[ ]*([!\-,.\'\":;()])/gi, "$1 ");
	clean_text = clean_text.replace(/[\n\t\r]+/gi, " ");
	clean_text = clean_text.replace(/[ ]+/gi, " ");
    var word_list = clean_text.split(" ");
    var total_words = word_list.length - 1;
    return total_words;
}

function getTextForNWords(paragraph_text, max_words) {
	var clean_text = paragraph_text + " ";
	clean_text = clean_text.replace(/[ ]*([!\-,.\'\":;()])/gi, "$1 ");
	clean_text = clean_text.replace(/[\n\t\r]+/gi, " ");
	clean_text = clean_text.replace(/[ ]+/gi, " ");
    var words_list = clean_text.split(" ");
    var text = "";
    for(i=0; i < max_words; i++) {
		text += words_list[i] + " ";
    }
    return text;
}

//Validate email domain for input field.
function validEmailDomain(email) {

	var returnVal = false;    
    var index = email.value.lastIndexOf('.') + 1;
	var email_domain = email.value.substr(index);
    var domains = new Array(24);
	domains[0] = "biz";
	domains[1] = "com";
	domains[2] = "coop";
	domains[3] = "edu";
	domains[4] = "gov";
	domains[5] = "info";
	domains[6] = "int";
	domains[7] = "jobs";
	domains[8] = "mil";
	domains[9] = "mobi";
	domains[10] = "museum";
	domains[11] = "name";
	domains[12] = "net";
	domains[13] = "org";
	domains[14] = "pro";
	domains[15] = "tel";
	domains[16] = "travel";
	domains[17] = "pro";
	domains[18] = "post";
	domains[19] = "geo";
	domains[20] = "ca";
	domains[21] = "us";
	domains[22] = "fr";

	if(index != 0){
		for(i = 0; i < domains.length && returnVal == false; i++) {
			if(email_domain == domains[i]) {
				returnVal = true;
			}
		}
	}

return returnVal;
}

function validCity(formField)
	{
			var result = true;
		  var stringto = trim(formField.value);
		  if (!(stringto.search(/^[a-zA-Z\s-.]*[\']?[a-zA-Z\s-.]*$/)!= -1)) 
		  {  
			  result = false;
		  }
    	return result;
	}
