/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 4;

function maxLengthTextarea(id,maxlength){
	var str=document.getElementById(id).value;
	if(str.length >maxlength )
		{
		document.getElementById(id).value = str.substr(0,maxlength);
		}
	
	}
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
/*var bracket=15
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+14)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);*/


				//var stripped = objValue.value;
				var stripped = strPhone.replace(/[\ ]/g, '');
				var charpos = stripped.search("[^0-9\-\+\(\)]"); 
				//strip out acceptable non-numeric characters
				if(strPhone.length > 0 &&  charpos >= 0) 
				{ 
					return false;
				}else{
					return true;
				}
				           
			



}
/*-------------------------------------------------------------------------------------*/
function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkpmnrstuvwxy]";                         // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "{1}" + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

function checkUKTelephone (telephoneNumber) {

  // Convert into a string and check that we were provided with something
  var telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;
  
  // Don't allow country codes to be included (assumes a leading "+")
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // Remove spaces from the telephone number to help validation
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }
  
  // Remove hyphens from the telephone number to help validation
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }  
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
	
	// Disallow numbers allocated for dramas.
	 
  // Array holds the regular expressions for the drama telephone numbers
  var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) {
    if ( tnexp[i].test(telnum) ) {
      telNumberErrorNo = 5;
      return false;
    }
	}
  
  // Finally check that the telephone number is appropriate.
  exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Telephone number seems to be valid - return the stripped telehone number  
  return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";
/*-------------------------------------------------------------------------------------*/

function check(){
	//alert('hi');
		var x=document.form1.Name.value;
		var a=document.form1.Address.value;
		var ci=document.form1.City.value;
		var em=document.form1.Email.value;
		var comm=document.form1.Comments.value;
		var zipcode=document.form1.ZipCode.value;
	
	if(document.form1.Name.value.search(/\S/) == -1 ) 
	{
		alert("Please Enter Name");
		document.form1.Name.focus();
		return false;
	}
	var ck_name = /^[A-Za-z. ]{1,50}$/;
	
	if(!ck_name.test(document.form1.Name.value))
	{
	alert("Please Enter Valid Name which is less than 50 characters ");
	document.form1.Name.focus();
	return false;
	}
	
	/*if((x.length>50))
	{
	alert("Please Enter Name which is less than 50 characters");
	document.form1.Name.focus();
	return false;
	} */
	if(document.form1.Address.value.search(/\S/) == -1 ) 
	{
		alert("Please Enter Address");
		document.form1.Address.focus();
		return false;
	}
	
		if((a.length>200))
	{
	alert("Please Enter Address which is less than 200 characters");
	document.form1.Address.focus();
	return false;
	} 
	if(document.form1.City.value.search(/\S/) == -1 ) 
	{
		alert("Please Enter City");
		document.form1.City.focus();
		return false;
	}
		if((ci.length>50))
	{
	alert("Please Enter City which is less than 50 characters");
	document.form1.City.focus();
	return false;
	} 
	if(document.form1.State.value.search(/\S/) == -1 ) 
	{
		alert("Please Enter state");
		document.form1.State.focus();
		return false;
	}
	/*var ZipCode=document.form1.ZipCode
	if(document.form1.ZipCode.value.search(/\S/) == -1 ) 
	{
		alert("Please Enter ZipCode");
		document.form1.ZipCode.focus();
		return false;
	}*/
	
	/*-----------------------------------------*/
	/*if(checkPostCode(ZipCode.value)==false){
		alert("Please Enter a Valid Zip Code")
		ZipCode.value=""
		ZipCode.focus()
		return false
	}*/
	
	/*------------------------------------------*/
	
	var Phone=document.form1.Phone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkUKTelephone(Phone.value)==false){
		alert("Please Enter a Valid UK Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}

	if(document.form1.Email.value.search(/\S/) == -1 )
	{
		alert("Please Enter Valid Email Address");
		document.form1.Email.focus();
		return false;
	}

	if((em.length>50))
	{
	alert("Please Enter Email Address  which is less than 50 characters");
	document.form1.Email.focus();
	return false;
	} 
	
	var regEmail = /^([-a-zA-Z0-9._]+@[-a-zA-Z0-9.]+(\.[-a-zA-Z0-9]+)+)$/;

	if(!regEmail.test(document.form1.Email.value))
	{
	alert("Please Enter Valid Email Address");
	document.form1.Email.focus();
	return false;
	}

	if(document.form1.Comments.value.search(/\S/) == -1 ) 
	{
		alert("Please Enter Comments");
		document.form1.Comments.focus();
		return false;
	}
		if((comm.length>500))
	{
	alert("Please Enter Comments which is less than 500 characters");
	document.form1.Comments.focus();
	return false;
	} 
return true;
}

function checkcust(openTime,closeTime,curTime){
//var times = showTime();
//alert(openTime);
var splitO = openTime.split(':');

var hourO = splitO[0];
var minuteO = splitO[1];

var splitC = closeTime.split(':');

var hourC = splitC[0];
var minuteC = splitC[1];


var splitN = curTime.split(':');

var hourN = splitN[0];
hourN = hourN*1;
var minuteN = splitN[1];
minuteN = minuteN*1;

var opm = 'am';
var vhourO = hourO;
if(hourO>12){ opm = 'pm';vhourO = parseInt(hourO)-12;}

var cpm = 'am';
var vhourC = hourC;
if(hourC>12){ cpm = 'pm';vhourC = parseInt(hourC)-12;}
//alert(hourN+hourO);
if (hourN<hourO || hourN>hourC || (hourN==hourC && minuteN>00) ) {
alert("\n You can only place order after "+vhourO+" "+opm+" and befor "+vhourC+" "+cpm+". In other case please call the restaurant for details.");
//document.frm_cart.hh.focus();
  return false;
}

	
	var regexLetter = /^[A-Za-z' ']+$/;
	if(document.frm_cart.deliverytype[0].checked!=""){
		 if(document.frm_cart.deliveryaddress.value=="")
		  {
		  alert("Please enter your Delivery address");
		  document.frm_cart.deliveryaddress.focus();
		  return false;
		  }
		  
		  var ZipCode=document.frm_cart.postcode
		  if(document.frm_cart.postcode.value=="")
		  {
		  alert("Please Enter Your Zip Code");
		  document.frm_cart.postcode.focus();
		  return false;
		  }
		  
		  if(checkPostCode(ZipCode.value)==false){
		alert("Please Enter a Valid Zip Code")
		ZipCode.value=""
		ZipCode.focus()
		return false
	}
		  
		  
		 /* if(isNaN(document.frm_cart.postcode.value)){
		   alert("Please enter valid post code");
		  document.frm_cart.postcode.focus();
		  return false;
	  }*/
	}
 if(document.frm_cart.customer_name.value=="")
  {
  alert("Please enter customer name");
  document.frm_cart.customer_name.focus();
  return false;
  }
  if(!isNaN(document.frm_cart.customer_name.value)){
    alert("Please enter valid customer name");
  	document.frm_cart.customer_name.focus();
    return false;
	  }
   var cname=document.frm_cart.customer_name.value;
   if(!regexLetter.test(cname))
	{
	 alert("Please enter valid customer name");
 	document.frm_cart.customer_name.focus();
    return false;
	}
	
var Phone=document.frm_cart.phone
 if(document.frm_cart.phone.value=="")
  {
  alert("Please enter phone number");
  document.frm_cart.phone.focus();
  return false;
  }
  
  if (checkUKTelephone(Phone.value)==false){
		alert("Please Enter a Valid UK Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
  
/*  if(isNaN(document.frm_cart.phone.value)){
   alert("Please enter valid phone number");
  document.frm_cart.phone.focus();
  return false;
	  }*/
	  
	  
	  
  if(document.frm_cart.email.value=="")
  {
  alert("Please enter your email address");
  document.frm_cart.email.focus();
  return false;
  }
  
  
  if(document.frm_cart.email.value!="")
  {
        remail=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!remail.test(document.frm_cart.email.value))
		{
		alert("Please enter valid email address");
		document.frm_cart.email.focus();
		return false;
		}
  }




   if(document.frm_cart.security_code.value=="")
  {
  alert("Please enter valid Security Code");
  document.frm_cart.security_code.focus();
  return false;
  }
  
  

 
 
 
  return true;
  
 }
 
 function showTime(){
	var Digital=new Date();
	var hours=Digital.getHours();
	var hours=Digital.getHours();
	var minutes=Digital.getMinutes();
	var seconds=Digital.getSeconds();
	var dn="PM";
	if (hours<12) dn="AM";
	if (hours>12) hours=hours-12;
	if (hours==0) hours=12;
	if (minutes<=9) minutes="0"+minutes;
	if (seconds<=9) seconds="0"+seconds;
	var ctime=hours+":"+minutes+":"+seconds+" "+dn;
		return ctime;
	//return hours;
	//return minutes;
}
 
