// JavaScript Document

// returns true if the string is empty
function isEmpty(str){
  return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
//function to check valid email address

function isEmail(emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)

// Check for the Email start with number.
if ('0123456789'.indexOf(emailStr.charAt(0)) >= 0) 
{
  alert("Email address seems incorrect.");
   return false; 	
}
if ('!%&\\(\\)<>@,;:\\\\\\\"\\.\\[\\]'.indexOf(emailStr.charAt(0)) >= 0) 
{
  alert("Email address seems incorrect.");
   return false; 	
}

if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)");
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("There should not be space before first text");
    return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	   alert("Destination IP address is invalid!");
		return false;
	    }
    }
    return true;
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.");
    return false;
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain, or two letter country.");
   return false;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   //var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false;
}

// If we've gotten this far, everything's valid!
//return true;
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
  var re = /[^a-zA-Z]/g
  if (re.test(str)) return false;
  return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
  var re = /[\D]/g
  if (re.test(str)) return false;
  return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
  var re = /[^a-zA-Z0-9 ]/g
  if (re.test(str)) return false;
  return true;
}
// returns true if the string's length equals "len"
function isLength(str, len){
  return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
  return (str.length >= min)&&(str.length <= max);
}
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
  var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
  return re.test(str);
}
// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
  var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
  if (!re.test(str)) return false;
  var result = str.match(re);
  var m = parseInt(result[1]);
  var d = parseInt(result[2]);
  var y = parseInt(result[3]);
  if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
  if(m == 2){
          var days = ((y % 4) == 0) ? 29 : 28;
  }else if(m == 4 || m == 6 || m == 9 || m == 11){
          var days = 30;
  }else{
          var days = 31;
  }
  return (d >= 1 && d <= days);
}
// returns true if the string is a valid date formatted as...
// mm dd, mm/dd, mm.dd, mm-dd
function isDateNoYear(str){
  var re = /^(\d{1,2})[\/](\d{1,2})$/
  if (!re.test(str)) return false;
  var result = str.match(re);
  var m = parseInt(result[1]);
  var d = parseInt(result[2]);
  if(m < 1 || m > 12) return false;
  if(m == 2){
          var days = 29;
  }else if(m == 4 || m == 6 || m == 9 || m == 11){
          var days = 30;
  }else{
          var days = 31;
  }
  return (d >= 1 && d <= days);
}
// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
  return str1 == str2;
}
// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
  var re = /[\S]/g
  if (re.test(str)) return false;
  return true;
}
// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
  if (replacement == null) replacement = '';
  var result = str;
  var re = /\s/g
  if(str.search(re) != -1){
    result = str.replace(re, replacement);
  }
  return result;
}
// validate the form
function validateForm(f, preCheck){
  var errors = '';
  if(preCheck != null) errors += preCheck;
  var i,e,t,n,v;
  for(i=0; i < f.elements.length; i++){
    e = f.elements[i];
    if(e.optional) continue;
    t = e.type;
    n = e.name;
    v = e.value;
    if(t == 'text' || t == 'password' || t == 'textarea'){
      if(isEmpty(v)){
        errors = 'Fields cannot be empty.\n'; continue;
      }
      if(v == e.defaultValue){
        errors += n+' cannot use the default value.\n'; continue;
      }
      if(e.isAlpha){
        if(!isAlpha(v)){
          errors += n+' can only contain characters A-Z a-z.\n'; continue;
        }
      }
      if(e.isNumeric){
        if(!isNumeric(v)){
          errors += n+' can only contain characters 0-9.\n'; continue;
        }
      }
      if(e.isAlphaNumeric){
        if(!isAlphaNumeric(v)){
          errors += n+' can only contain characters A-Z a-z 0-9.\n'; continue;
        }
      }
      if(e.isEmail){
        if(!isEmail(v)){
          errors += v+' is not a valid email.\n'; continue;
        }
      }
      if(e.isLength != null){
        var len = e.isLength;
        if(!isLength(v,len)){
          errors += n+' must contain only '+len+' characters.\n'; continue;
        }
      }
      if(e.isLengthBetween != null){
        var min = e.isLengthBetween[0];
        var max = e.isLengthBetween[1];
        if(!isLengthBetween(v,min,max)){
          errors += n+' cannot contain less than '+min+' or more than '+max+' characters.\n'; continue;
        }
      }
      if(e.isPhoneNumber){
        if(!isPhoneNumber(v)){
          errors += v+' is not a valid US phone number.\n'; continue;
        }
      }
      if(e.isDate){
        if(!isDate(v)){
          errors += v+' is not a valid date.\n'; continue;
        }
      }
      if(e.isDateNoYear){
        if(!isDateNoYear(v)){
          errors += v+' is not a valid date.\n Example birthday 8/14 (M/D)'; continue;
        }
      }
      if(e.isMatch != null){
        if(!isMatch(v, e.isMatch)){
          errors += n+' does not match.\n'; continue;
        }
      }
    }
    if(t.indexOf('select') != -1){
      if(isEmpty(e.options[e.selectedIndex].value)){
        errors += n+' needs an option selected.\n'; continue;
      }
    }
    if(t == 'file'){
      if(isEmpty(v)){
        errors += n+' needs a file to upload.\n'; continue;
      }
    }
  }
  if(errors != '') alert(errors);
  return errors == '';
}

/*
The following elements are not validated...

button   type="button"
checkbox type="checkbox"
hidden   type="hidden"
radio    type="radio"
reset    type="reset"
submit   type="submit"

All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.

isEmail = true;          // valid email address
isAlpha = true;          // A-Z a-z characters only
isNumeric = true;        // 0-9 characters only
isAlphaNumeric = true;   // A-Z a-z 0-9 characters only
isLength = number;       // must be exact length
isLengthBetween = array; // [lowNumber, highNumber] must be between lowNumber and highNumber
isPhoneNumber = true;    // valid US phone number. See "isPhoneNumber()" comments for the formatting rules
isDate = true;           // valid date. See "isDate()" comments for the formatting rules
isMatch = string;        // must match string
optional = true;         // element will not be validated
*/

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// --------------------------------------------------
// ||||||||||||||||||||||||||||||||||||||||||||||||||

// All of the previous JavaScript is coded to process
// any form and should be kept in an external file if
// multiple forms are being processed.

// This function configures the previous
// form validation code for this form.
/*function configureValidation(f){
  f.fname.isAlphaNumeric = true;
  f.lname.isAlphaNumeric = true;
  f.email.isEmail = true;
  f.zip.isAlphaNumeric = true;
  f.custom_field_1.isDateNoYear = true;
  var preCheck = null;
  return validateForm(f, preCheck);
}*/

function configureValidation(form){
 var form=form;
	
	if(form.fname.value == ""){
		alert("Please enter your first name");
		form.fname.focus();
		return false;
	}
	if(form.lname.value == ""){
		alert("Please enter your last name");
		form.lname.focus();
		return false;
	}
	
	if(form.email.value == ""){
			alert("Please enter email address.");
			form.email.focus();
			return false;
		}
				
	if(isEmail(form.email.value) == false){
		form.email.focus();
		return false;
	}
	
	if(form.zip.value == ""){
		alert("Please enter zip code");
		form.zip.focus();
		return false;
	}
	form.custom_field_1.isDateNoYear = true;
	 var preCheck = null;
  return validateForm(form, preCheck);
}


function dineformvalidation(form){
	var form=form;
	
	if(form.email.value == ""){
			alert("Please enter email address.");
			form.email.focus();
			return false;
		}
				
	if(isEmail(form.email.value) == false){
		form.email.focus();
		return false;
	}
	if(form.name.value == ""){
		alert("Please enter your name");
		form.name.focus();
		return false;
	}
	if(form.phone.value == ""){
		alert("Please enter your phone");
		form.phone.focus();
		return false;
	}
	if(form.txtdinedate.value == ""){
		alert("Please enter dine date");
		form.txtdinedate.focus();
		return false;
	}
	if(form.txttime.value == ""){
		alert("Please enter dine time");
		form.txttime.focus();
		return false;
	}
	if(form.txttime.value == ""){
		alert("Please enter dine time");
		form.txttime.focus();
		return false;
	}
	if(form.txtserver.value == ""){
		alert("Please enter server name");
		form.txtserver.focus();
		return false;
	}
	
	if(form.txtguest.value == ""){
		alert("Please enter guest number");
		form.txtguest.focus();
		return false;
	}
	
	if(isNaN(form.txtguest.value)){
    	alert("Please enter valid guest number");
	    form.txtguest.focus();
		return false;
	}	
	
	if(form.txtcomments.value == ""){
		alert("Please enter comments");
		form.txtcomments.focus();
		return false;
	}
	
	if(form.captcha_input.value == ""){
		alert("Please enter security code");
		form.captcha_input.focus();
		return false;
	}
	
}


function validation(form){
	var form =form;
	if(form.name.value == ""){
    	alert("Please enter Name");
	    form.name.focus();
		return false;
	}	
	
	if(form.email.value == ""){
		alert("Please enter email address.");
	    form.email.focus();
		return false;
	}
	if(isEmail(form.email.value) == false){
    	form.email.focus();
		return false;
	}			
	if(form.txtdesireddate.value == ""){
    	alert("Please enter date");
	    form.txtdesireddate.focus();
		return false;
	}	
	if(form.txtdesireddate.value == ""){
    	alert("Please enter date");
	    form.txtdesireddate.focus();
		return false;
	}	
	if(form.starttimehours.value == ""){
    	alert("Please enter Start Time in hours");
	    form.starttimehours.focus();
		return false;
	}	
	
	if(isNaN(form.starttimehours.value)){
    	alert("Please enter valid Start Time in hours");
	    form.starttimehours.focus();
		return false;
	}	
	
	if(form.starttimemin.value == ""){
    	alert("Please enter Start Time in minute");
	    form.starttimemin.focus();
		return false;
	}
	
	if(isNaN(form.starttimemin.value)){
    	alert("Please enter valid Start Time in minute");
	    form.starttimemin.focus();
		return false;
	}	
	
	if(form.endtimehours.value == ""){
    	alert("Please enter end Time in hours");
	    form.endtimehours.focus();
		return false;
	}
	
	if(isNaN(form.endtimehours.value)){
    	alert("Please enter valid end Time in hours");
	    form.endtimehours.focus();
		return false;
	}
	
	if(form.endtimemin.value == ""){
    	alert("Please enter end Time in minute");
	    form.endtimemin.focus();
		return false;
	}	
	
	if(isNaN(form.endtimemin.value)){
    	alert("Please enter valid end Time in minute");
	    form.endtimemin.focus();
		return false;
	}	
	
	if(form.estimate.value == ""){
    	alert("Please enter guest number");
	    form.estimate.focus();
		return false;
	}
	if(form.captcha_input.value == ""){
    	alert("Please enter security code");
	    form.captcha_input.focus();
		return false;
	}
	
	/*if(isNaN(form.estimate.value)){
    	alert("Please enter valid guest number");
	    form.estimate.focus();
		return false;
	}*/
}	




	function donationformvalidation(form){
		
		if(form.email.value == ""){
		alert("Please enter email address.");
	    form.email.focus();
		return false;
	}
	
		if(isEmail(form.email.value) == false){
			form.email.focus();
			return false;
		}
		
		
		
		if(form.phone.value == ""){
			alert("Please enter your phone");
			form.phone.focus();
			return false;
		}
		
		if(form.captcha_input.value == ""){
		alert("Please enter security code");
		form.captcha_input.focus();
		return false;
		}
		
	}
	
	
		function formvalidation(form){
			
			if(form.email.value == ""){
				alert("Please enter email address.");
				form.email.focus();
				return false;
			}
	
			if(isEmail(form.email.value) == false){				
				form.email.focus();
				return false;
			}
			
			
			
			if(form.phone.value == ""){
				alert("Please enter your phone");
				form.phone.focus();
				return false;
			}
			
			if(form.captcha_input.value == ""){
				alert("Please enter security code");
				form.captcha_input.focus();
				return false;
			}
		}
		
			function mediaformvalidation(form){
				
				if(form.email.value == ""){
					alert("Please enter email address.");
					form.email.focus();
					return false;
				}
				if(isEmail(form.email.value) == false){
					form.email.focus();
					return false;
				}
				
				
				
				if(form.phone.value == ""){
					alert("Please enter your phone");
					form.phone.focus();
					return false;
				}
				
				
				if(form.captcha_input.value == ""){
				alert("Please enter security code");
				form.captcha_input.focus();
				return false;
				}
			}
			
		function chefformvalidation(form){
			
			if(form.email.value == ""){
					alert("Please enter email address.");
					form.email.focus();
					return false;
				}
				
			if(isEmail(form.email.value) == false){				
				form.email.focus();
				return false;
			}
			
			
			
			if(form.phone.value == ""){
				alert("Please enter your phone");
				form.phone.focus();
				return false;
			}
			
			if(form.captcha_input.value == ""){
				alert("Please enter security code");
				form.captcha_input.focus();
				return false;
			}
		
		}

	
		function infoformvalidation(form){
			
			if(form.email.value == ""){
					alert("Please enter email address.");
					form.email.focus();
					return false;
				}
				
			if(isEmail(form.email.value) == false){				
				form.email.focus();
				return false;
			}
			
			
			
			if(form.phone.value == ""){
				alert("Please enter your phone");
				form.phone.focus();
				return false;
			}
			
			if(form.captcha_input.value == ""){
				alert("Please enter security code");
				form.captcha_input.focus();
				return false;
			}
		
		}
		
		
/////////////Contact form validation//////////////////		
		function validcontact(form){	
		
			if(form.email.value == ""){
					alert("Please enter email address.");
					form.email.focus();
					return false;
				}
				
			if(isEmail(form.email.value) == false){				
				form.email.focus();
				return false;
			}
			
			/*if(form.fname.value == ""){
				alert("Please enter full name.");
				form.fname.focus();
				return false;
			}*/
			
			if(form.txtdinedate.value == ""){
				alert("Please enter dine date");
				form.txtdinedate.focus();
				return false;
			}
			
			if(form.captcha_input.value == ""){
				alert("Please enter security code");
				form.captcha_input.focus();
				return false;
			}
		}
