$(document).ready(function(){
//alert("k");
function stripBlanks(fld) {

var result = "";

var c = 0;

for (i=0; i<fld.length; i++) {

if (fld.charAt(i) != " " || c > 0) {

result += fld.charAt(i);

if (fld.charAt(i) != " ") c = result.length;

}

}

return result.substr(0,c);

} 

	//global vars

	var reserve_form1= $("#reserve_form1");
   /* var reserve_form2= $("#reserve_form2");*/
	
	var indate=$("#indate");

	var indateInfo=$("#indateinfo");
	
	var select_adult=$("#select_adult");

	var adultinfo=$("#adultinfo");
	
	var rtype=$("#sel_type");
	var  rtypeInfo=$("#rtypeinfo");
	
	/*var std=$("#std");
	var  stdInfo=$("#stdinfo");
	
	var del=$("#del");
	var  delInfo=$("#delinfo");
	
	var st=$("#st");
	var  stInfo=$("#stinfo");*/
	
	var fname = $("#fname");

	var fnameInfo = $("#fnameinfo");
	 var lname = $("#lname");

	var lnameInfo = $("#lnameinfo");
	var address=$("#txtaddr");

	var addressInfo=$("#addrinfo");
	var country=$("#txtcountry");
	var countryInfo=$("#countryinfo");
	
	var ph=$("#txtph");

	var phInfo=$("#phoneinfo");
	var email=$("#txtemail");

	var mailInfo = $("#emailinfo");
	
	var cemail=$("#txtcemail");

	var cmailInfo = $("#cemailinfo");
    var cname = $("#txtcname");

	var cnameInfo = $("#crdnameinfo");
	var cardnumber=$("#txtcnumber");
	var cardnumberInfo=$("#crdnumberinfo");
	
	var cardtype=$("#txtctype");
    var seCode=$("#captchaField");
	var seCodeInfo=$("#secodeinfo");
	/*var outdate=$("#outdate");

	var outdateInfo=$("#outdateinfo");
	
	
	
	var btype=$("#sel_bed");
	var  btypeInfo=$("#btypeinfo");
	
	
	
   
	
	
	
	

	

	
	*/
	
   indate.blur(validateindate);
   select_adult.blur(validateAdult);
   
   rtype.blur(validatertype);
   //std.blur(validatestd);
  // del.blur(validatedel);
   //st.blur(validatest);
   fname.blur(validatefname);
   lname.blur(validatelname);
    address.blur(validateaddress);
   country.blur(validatecountry);
   ph.blur(validateph);
   email.blur(validateemail);
   cemail.blur(validatecemail);
   cname.blur(validatecname);
   cardnumber.blur(validatecardnumber);
   seCode.blur(validateCode);
   /*outdate.blur(validateoutdate);
   
   btype.blur(validatebtype);
*/
   


   
 reserve_form1.submit(function()
	{
								   
		
								  
		cns=cname.val();
	cds=cardnumber.val();
	//alert(cns);
	//alert(cds);						   

		/*if(  validateindate()  & validateAdult()& validatertype()& validatefname()& validatelname()& validateph() & validateaddress() & validatecountry()& validateemail()& validatecemail()& validatecname() &  validatecardnumber()& validateCode() & validateMsg())*/

if(cns==''||cds=='')
{

	if( validateindate() & validateAdult() & validatertype() /*& validatestd() & validatedel() & validatest()*/& validatefname()& validatelname() & validateph() & validateaddress() & validatecountry()& validateemail() & validatecemail() & validateCode()  & validateMsg())
	{
//alert('true');
			return true
	}
		else
		{
//alert('false');
			return false;
		}
		
	}	

	
 

 
 
if(cns!=''&& cds!='')
{
	//alert(cns);

	if( validateindate() & validateAdult() & validatertype() /*& validatestd() & validatedel() & validatest()*/ & validatefname()& validatelname() & validateph() & validateaddress() & validatecountry()& validateemail() & validatecemail() & validateCode()& validatecname() &  validatecardnumber() )
	{
//alert('suc');
			return true
	}
		else
		{
//alert('errr');
			return false;
		}

	
 
}
 });
 
 
 
 /* reserve_form2.submit(function(){

		if( validatefname() & validateindate() & validateoutdate()   & validatelname()  & validateemail()& validatecemail() & validateph() & validateaddress() & validatecountry() & validatertype()& validatebtype() & validatecname() &  validatecardnumber())

			return true

		else

			return false;

	});*/
 function validateMsg()
 {
	idate=indate.val();
	adult=select_adult.val();
	fn=stripBlanks(fname.val());
	s=stripBlanks(seCode.val());
	ln=stripBlanks(lname.val());
	add=stripBlanks(address.val());
	ad=stripBlanks(country.val());
	c=stripBlanks(ph.val());
	e=email.val();
	ce=cemail.val();
	cn=cname.val();
	cd=cardnumber.val();
	
	if(idate!=''&& adult!='' && fn!='' && s!='' && ln!='' && add!='' && ad!='' && c!='' && e!='' && ce!='')
	{ 
	
	if(cn=='' || cd=='')
	{
	
	 var retVal = prompt(" We highly recommend  to provide your credit card details to gurantee your booking.\nOtherwise your booking should be confirmed 48 hours prior to booking date.\n Please enter 'yes' for confirmation.If you want to update click 'Cancel button' : ","");
  //alert(retVal);
 
 
 if(retVal==null)
 {
	 
//alert("1");	
return false;
 }
 
 else
 {
	 
	 //alert("2");
	return true; 
 }
	
	}
	
	
	}
	
	
	
	
 }
function validateCode()
{
	   //var error=true;
		 var  sFilter =/[\<\>\;\#\:\\\"\[\]]/;
		 var s=stripBlanks(seCode.val());
		 if(s==''){
		 seCode.addClass('error');
		 seCodeInfo.text("Please Enter the Security code");	
		 return false;
		 }
		 else if(s.match(sFilter)){
		 seCode.removeClass('error');
		 seCodeInfo.text("Please Enter a valid Code");	
		 return false;	 
		 }
		
		 
		 else{	
		 
		 error=false;
		 seCode.removeClass('error');
		 seCodeInfo.text("");	
		 }
		  
		
		 if(error==false){
			return true; 
		 }
		 
	}
function validateindate(){

		var  indateFilter =/[\<\>\;\#\:\\\"\[\]]/;

		idate=indate.val();

		idates=new Date(idate);

		//odate=outdate.val();

		//odates=new Date(odate);

		

		var ind=stripBlanks(indate.val());

		if(ind==''){

			indate.addClass('error');

			indateInfo.text("Please Enter Arrival Date");	

			return false;

			}
/*
		else if(ind.match(indateFilter)){

			indate.removeClass('error');

			indateInfo.text("Please Enter a valid date");	

			return false;	 

			}

		else if(odates<idates){

			outdate.addClass('error');

			indateInfo.text("Check in Date must be less than Check out Date");	

			return false;	

	

			}

		*/

		else{	

			error=false;

			indate.removeClass('error');

			indateInfo.text("");	

			}

		if(error==false){

			return true; 

			}

	}






function validateAdult()
{
var adult=select_adult.val();
if(adult=='0')
{
	select_adult.addClass('error');

			adultinfo.text("Please Select the no of Adult");	

			return false;
}

else{	

			error=false;

			select_adult.removeClass('error');

			adultinfo.text("");	

			}

		if(error==false){

			return true; 

			}



}


function checkRadio (frmName, rbGroupName) {
 var radios = document[frmName].elements[rbGroupName];
 for (var i=0; i <radios.length; i++) {
  if (radios[i].checked) {
   return true;
  }
 }
 return false;
}
function validatertype(){
	

	if(!checkRadio("reserve_form1","sel_type")){



		rtypeInfo.text("Please Select  Room Type");

			return false; 

		} 



		else{

		error=false;

		rtypeInfo.text("");	

		}

		

		 if(error==false){

			return true; 

		 }	

	} 
// var rttypess=$("#sel_type").val();	

//alert(rttypess);	
/*alert("gh");*/	
/*function validatestd()
{
	
if(rttypess=='Standard')
{
	alert("lo");

  

	if(!checkRadio("reserve_form1","std"))
	{



		stdInfo.text("Please Select  Standard Room Category");

			return false; 

		}
		

		else{

		error=false;

		stdInfo.text("");	

		}



		 if(error==false){

			return true; 

		 }
		 


	}
}
	
	function validatedel()
{
	
//if(rttypess=='Deluxe'){
	if(!checkRadio("reserve_form1","del")){



		delInfo.text("Please Select  Deluxe Room Category");

			return false; 

		} 

		else{

		error=false;

		delInfo.text("");	

		}

		 if(error==false){

			return true; 

		 }
//}

	}
	
	
function validatest()
{
	
//if(rttypess=='Suite'){
	if(!checkRadio("reserve_form1","st")){



		stInfo.text("Please Select  Suite Room Category");

			return false; 

		} 

		else{

		error=false;

		stInfo.text("");	

		}

		 if(error==false){

			return true; 

		 }
//}

	}*/
		
	
	

function validatefname(){

	   //var error=true;

		 var  fnameFilter =/[\<\>\;\#\:\\\"\[\]]/;

		 var fn=stripBlanks(fname.val());

		 if(fn==''){

		 fname.addClass('error');

		 fnameInfo.text("Please Enter Your First Name");	

		 return false;

		 }

		 else if(fn.match(fnameFilter)){

		 fname.removeClass('error');

		 fnameInfo.text("Please Enter a valid First Name");	

		 return false;	 

		 }

		 else if((fn.length<2)||(fn.length>25)){

		 fname.addClass('error');

		 fnameInfo.text("Length between 4-25 characters");	

		 return false;

		 }

		 

		 else{	

		 

		 error=false;

		 fname.removeClass('error');

		 fnameInfo.text("");	

		 }

		  if(error==false){

			return true; 

		 }

		 

	}

function validatelname(){

	   //var error=true;

		 var  lnameFilter =/[\<\>\;\#\:\\\"\[\]]/;

		 var ln=stripBlanks(lname.val());

		 if(ln==''){

		 lname.addClass('error');

		 lnameInfo.text("Please Enter Your Last Name");	

		 return false;

		 }

		 else if(ln.match(lnameFilter)){

		 lname.removeClass('error');

		 lnameInfo.text("Please Enter a valid  Last Name");	

		 return false;	 

		 }

		 

		 

		 else{	

		 

		 error=false;

		 lname.removeClass('error');

		 lnameInfo.text("");	

		 }

		  if(error==false){

			return true; 

		 }

		 

	}
function validateaddress(){

		

		var addressFilter=/[\<\>\;\#\:\\\"\[\]]/;

		add=stripBlanks(address.val());

		if(add=='')
		{
			
		address.removeClass('error');

		 addressInfo.text("Please Enter Your   Address");	

		 return false;	
		}

		  if(add.match(addressFilter)){

		 address.removeClass('error');

		 addressInfo.text("Enter a valid Address");	

		 return false;	 

		 }

		 else{	

		 error=false;

		 address.removeClass('error');

		 addressInfo.text("");	

		 }

		  if(error==false){

		return true; 

		 }

			

	}

	



function validatecountry(){

	  var ad=stripBlanks(country.val());

	   if(ad=='')

	    {

	     country.addClass('error');

		 countryInfo.text("Please Select Your Country");	

		 return false;

		 }

		  else{	
        error=false;
         country.removeClass('error');

		countryInfo.text("");	

		 }

		 if(error==false){

			return true; 

		 }
	} 

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 validateph(){



			var telnoRegxp = /^([0-9]{11})$/;

		  var cval = $("#txtph").val();

		  var c=stripBlanks(cval);

		 if(c==''){

		  ph.addClass('error');

		  phInfo.text("Please Enter Your Phone Number");	

		  return false;

		 }

		 

		 else
		
		 if(!isInteger(c)){

		 ph.removeClass('error');

		 phInfo.text("Please Enter valid  Numbers only");	

		 return false;	 

		 }

		 else{	

		 error=false;

		 ph.removeClass('error');

		 phInfo.text("");	

		 }

		  if(error==false){

			return true; 

		 }

	}
	function validateemail(){

		

		var emailFilter = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var e=email.val();

		 if(e==''){

		 email.addClass('error');

		 mailInfo.text("Please Enter Your Email");	

		 return false;

		 }

		 else if(!e.match(emailFilter)){

		 email.removeClass('error');

		 mailInfo.text("Please Enter a valid Email");	

		 return false;	 

		 }

		 else{	

		 error=false;

		 email.removeClass('error');

		 mailInfo.text("");	

		 }

		  if(error==false){

		return true; 

		 }

	}

	
	
	function validatecemail(){

		

		var cemailFilter = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var ce=cemail.val();
       var e=email.val();
		 if(e==''){

		 cemail.addClass('error');

		 cmailInfo.text("Please Re-Enter Your Email");	

		 return false;

		 }

		 else if(!ce.match(cemailFilter)){

		 cemail.removeClass('error');

		 cmailInfo.text("Please Enter a valid Email");	

		 return false;	 

		 }
		 
		  else if(ce!=e){
			 
			cemail.removeClass('error');

		   cmailInfo.text("Confirm Email is not Matching");	

		   return false;	  
			 
		 }
		 

		 else{	

		 error=false;

		 cemail.removeClass('error');

		 cmailInfo.text("");	

		 }

		  if(error==false){

		return true; 

		 }

	}


function validatecname(){

	   //var error=true;

		 var  cnameFilter =/[\<\>\;\#\:\\\"\[\]]/;

		 var cn=stripBlanks(cname.val());
       if(cn!=''){
		 if(cn.match(cnameFilter)){

		 cname.removeClass('error');

		 cnameInfo.text("Please Enter a valid  Name");	

		 return false;	 

		 }

		 if((cn.length<2)||(cn.length>25)){

		 cname.addClass('error');

		 cnameInfo.text("Length between 2-25 characters");	

		 return false;

		 }
       else{	
        error=false;

		 cname.removeClass('error');

		 cnameInfo.text("");	

		 }

		  if(error==false){

			return true; 

		 }

	   }

	}
	


var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

function checkCreditCard (cardnumber, cardname) {
     
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types as follows.
  
  //  Name:         As in the selection box of the form - must be same as user's
  //  Length:       List of possible valid lengths of the card number for the card
  //  prefixes:     List of possible prefixes for the card
  //  checkdigit:   Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [1] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [2] = {name: "DinersClub", 
               length: "14,16", 
               prefixes: "305,36,38,54,55",
               checkdigit: true};
  cards [3] = {name: "CarteBlanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305",
               checkdigit: true};
  cards [4] = {name: "American Express", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [5] = {name: "Discover", 
               length: "16", 
               prefixes: "6011,622,64,65",
               checkdigit: true};
  cards [6] = {name: "JCB", 
               length: "16", 
               prefixes: "35",
               checkdigit: true};
  cards [7] = {name: "enRoute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
  cards [8] = {name: "Solo", 
               length: "16,18,19", 
               prefixes: "6334,6767",
               checkdigit: true};
  cards [9] = {name: "Switch", 
               length: "16,18,19", 
               prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
               checkdigit: true};
  cards [10] = {name: "Maestro", 
               length: "12,13,14,15,16,18,19", 
               prefixes: "5018,5020,5038,6304,6759,6761",
               checkdigit: true};
  cards [11] = {name: "VisaElectron", 
               length: "16", 
               prefixes: "417500,4917,4913,4508,4844",
               checkdigit: true};
  cards [12] = {name: "LaserCard", 
               length: "16,17,18,19", 
               prefixes: "6304,6706,6771,6709",
               checkdigit: true};
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardnumber = cardnumber.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = cardnumber
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the length if all else was 
  // hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}

/*================================================================================================*/



	function validatecardnumber()
	{
		
	var cd=stripBlanks(cardnumber.val());
	var ct=stripBlanks(cardtype.val());	
	 /*if(cd==''){
		cardnumber.addClass('error');
		 cardnumberInfo.text("Please Enter the Card Number");	
		 return false;
	}*/
if(cd!='')	
{
 if (!checkCreditCard(cd,ct))
{
  cardnumber.addClass('error');
  cardnumberInfo.text(ccErrors[ccErrorNo]);
  
  }
   

      else{	
		 
		 error=false;
		 cardnumber.removeClass('error');
		 cardnumberInfo.text("");	
		 }
		 if(error==false)
		 {
			return true; 
		 }		
			
}
		
	}
	
	
	if(cd=='' || cn =='')
	{
	
	function validatemsg(){
	var retVal = prompt(" We highly recommend  to provide your credit card details to gurantee your booking.\nOtherwise your booking should be confirmed 48 hours prior to booking date.\n Please enter 'yes' for confirmation.If you want to update click 'Cancel button' : ","");
   
  if(retVal==null)
  { 
  return false;
  }

  else if(retVal.toLowerCase()=='yes')
   {
   
   return true;
   
  } 
  
   else 
   {
   
   return false;
   }
  
  



function submit_cform()
{
document.cform.submit();
}
}
	
	}


});
