// Fix for defect No  458 Patch No AVI-ETR_B2C_1.0_P24_1.7

//Following Variables are used for Date.
var gd;                         

var tyear;
var tmonth;
var tday;
var syear;
var smonth;
var sday;
var lyear;
var lmonth;
var lday;
var maxDayToBook = 335;
var one_day=1000*60*60*24
var city;
var state;
var country;

//This Method Gives Today's Date.
function today(obj)
{
                  
gd= new Date(obj.year.value,obj.month.value,obj.date.value);

 tyear =gd.getFullYear();
 tmonth=gd.getMonth();
 tday  =gd.getDate();
 
}
// Trim whitespace from left and right sides of s.
            function trim(s) {
                return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
            }

//This Method Splits the Date According to Month/day/Year in Appropriate Format.


function datesplit(dt,i)
{

var sdate = dt.split("/");
       switch(i)
       {
        case 0:
			   
               return parseInt(sdate[0],10);			   
               break;
        case 1:
               return parseInt(sdate[1],10);
               break;
        case 2:
               return parseInt(sdate[2],10);
               break;
       }  

}
//This Also Validates the Check-in and Check-out Dates  as required.
//(like Check-out date must comes After the Check-in Date and Both must come after Today's Date.)
                         function startdate()
                                   {
                                     if(document.hotelSearchForm.check_out.value=='')
                                       {  
                                         syear=tyear+20;
                                         smonth=tmonth;
                                         sday=tday;
                                        
                                       }
                                       else
                                       {
                                        syear=datesplit(document.hotelSearchForm.check_out.value,2);
                                        smonth=datesplit(document.hotelSearchForm.check_out.value,0);
                                        sday=datesplit(document.hotelSearchForm.check_out.value,1);
                                        }
                                   }
                                
//This Also Validates the Check-in and Check-out Dates  as required.
//(like Check-out date must comes After the Check-in Date and Both must come after Today's Date.)
                        function enddate()
                                   {
                                      if(document.hotelSearchForm.check_in.value=='')
                                        {
                                         lyear= tyear; 
                                         lmonth=tmonth;
                                         lday=tday;
                                        }
                                      else 
                                        {
                                         lyear=datesplit(document.hotelSearchForm.check_in.value,2);
                                         lmonth=datesplit(document.hotelSearchForm.check_in.value,0);
                                         lday=datesplit(document.hotelSearchForm.check_in.value,1);
                                    
                                        }    
                                  }                               
            
//This Method is Used For Validation of Different Fields Present in the Form.
//Fix for TTPro Issue 450
function validatefield(form){
	 var str1 = document.getElementById('hotelCityBox').value;
	 var currrentMode = 'city';
	 if(form.choose_page[2].checked==true){
	  		currrentMode = 'airport code';
	   		str1 = document.getElementById('hotelAirportCodeBox').value;
	 }
	 
	 if(str1 == ''){
	 	alert("Please select the "+ currrentMode);
	    return false;
	 }
	  	
	 str1 = str1.split(",");
	 
	 if(str1.length == 1 && currrentMode == 'city' && $("div.ac_results:last").prev().find("ul > li ").get(0)){
	 	str1 = $("div.ac_results:last").prev().find("ul > li ").get(0).innerHTML;
	 	document.getElementById('hotelCityBox').value = str1;
	 	str1 = str1.split(",");
	 }else if(str1.length == 1 && currrentMode == 'airport code' && $("div.ac_results:last").find("ul > li ").get(0)){
	 	str1 = $("div.ac_results:last").find("ul > li ").get(0).innerHTML;
	 	document.getElementById('hotelAirportCodeBox').value = str1;
	 	str1 = str1.split(",");
	 }

	 if(str1.length == 1 || str1[0].indexOf("exact match") != -1 || (str1.length > 1 && str1[1] == '') ){
	 	document.getElementById('hotelCityBox').value = "";
	 	document.getElementById('hotelAirportCodeBox').value = "";
	 	alert("Please specify the valid "+ currrentMode +" details");
	    return false;
	 }
	 
	 if(form.choose_page[0].checked == true || form.choose_page[1].checked == true){
	    
	 	 //checking the mode and changes regarding city and address field
	    /* if(str1.length == 1){
	    	city = str1[0];
	      	if(document.getElementById('statecode').style.display == 'block'){
		   		country = document.getElementById('countrycode').value ;
		   		if(country !== 'US')
		   		   state = "-";
		   		else if(document.getElementById('statecode').value != "")
		   	         state = document.getElementById('statecode').value;
		   	 }
		   	 else{
		   	 	document.getElementById('message').style.display ='block';
			    document.getElementById('message').innerHTML = "* Please select valid city and state combination";
			    document.getElementById('statecode').style.display ='block';
			    document.getElementById('StateText').style.display ='block' ;
			    document.getElementById("StateText").innerHTML="State"; 
			    document.getElementById('countrycode').style.display ='block';
			    document.getElementById('CountryText').style.display ='block';
			    document.getElementById("CountryText").innerHTML="Country"; 
			    document.getElementById('statecode').focus();
			    return false;
			 }
		} */
		
		if(str1.length == 2){    
		   city = str1[0];
		   country = form.elements[trim(str1[1])].value;
		   state = "-";
		}
		
		else if(str1.length == 3) {
	    	city = str1[0];
	    	state = form.elements[trim(str1[1])].value;
	    	country = form.elements[trim(str1[2])].value;
		}
		
		form.city.value = city;
		form.countrycode.value = country;
  		form.statecode.value = state;
		
		// set default country as US if not specified
		if(form.countrycode.value=='' && country == ''){
	      form.countrycode.value = "US";
		}
		   
	    //Fixed for defect#446:While search by 'Address', the State is mandatory field on home page but it's not mandatory during change 	search.
	   if(form.choose_page[0].checked==true){
		    if(form.countrycode.value=='US'){
		      if(form.statecode.value=='-'||form.statecode.value==''){
		       alert("Please select a State from the list as it is a mandatory field")
		       return false;
		      }
		    }
	    } 
	    
	  	if(form.choose_page[1].checked==true){
	   		if((form.address && (form.address.value == '')) && (form.zip && (form.zip.value == ''))){
	   			alert('Please specify either address or zip details');
	   			return false;
	   		}
	      	if(form.zip.value != ''){
	 			var regexzip = /^[\d]{5,6}$/;       
		        if(!regexzip.test(form.zip.value)) {
			          alert("Zip code must be numeric and must be of 5 or 6 digits");
			          return false;
			   }
			} 
	    }
   		} 

	    if(form.choose_page[2].checked == true){
	    	if(str1.length == 1){
	     		city = str1[0];
		    	country	= '';
		    }else{
		    	city = str1[0];
		    	// country	= form.elements[str1[2]].value;
		    }
		    form.city.value == city;
		    // form.countrycode.value == country;
	    }
	    
	    //End of fix for defect#446
	
	    if(form.check_in.value=='')
	    {
	      alert("Please specify the Check-in date")
	      return false;
	    }
	
	    if(form.check_out.value=='')
	    {
	      alert("Please specify the Check-out date")
	      return false;
	    }
		if(form.check_in.value==form.check_out.value)
	    {
	      alert("The Check-out date must be atleast one day after the Check-in date. Please change the Check-out date.")
	      return false;
	    }
	
		//Date validations.
		var dd = new Date();
		var depdt;
		depdt = trim(form.check_in.value);
		
		var depdtarr = depdt.split("/");
		depmm = depdtarr[0]-1;
		depdd = depdtarr[1];
		depyyyy = depdtarr[2];
		var jdd = new Date(form.year.value+"",form.month.value-1,form.date.value,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
		day=jdd.getDate();
		month=jdd.getMonth();
		year=jdd.getYear();
		var depdate = new Date(depyyyy,depmm,depdd,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
	
		day = day+1;	
		var validdate = new Date(year,month,day,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
		
		//Calculate difference between the two dates, and convert to days
		var day_difference = Math.ceil((depdate.getTime()-jdd.getTime())/(one_day));
			
	    if (day_difference > maxDayToBook ) {
			alert("Please specify the Check in  date within 335 days from today's date");
			return false;
			}
		
		if (depdate < validdate ) {
			alert("Check in  date should be atleast 1 day from todays date");
			return false;
			}
		//date validation ends.
		//Child age validations.
		var children1 = form.children.value;
		for( i = 1; i <= children1; i++){
		id = "room1childage"+i;
		if( document.getElementById(id).value==0){
		   	alert("Please provide proper child age." );
		   	return false;
	   }
	}
	 var rooms = form.rooms.value;
	 if(rooms == 2){
	    var children2 = form.children2.value;
	    for( i = 1; i <= children2; i++){
	
	           id = "room2childage"+i;
	          if( document.getElementById(id).value==0){
	           	alert("Please provide proper child age." );
	           	return false;
	           }
	 }
	 }
	return true;
}

//validation for character
function validateCharacter()
{
    var code=event.keyCode;

	if((code>=65 && code<=90)||(code>=97 && code<=122)  || (code==32) || (code==13))
    {
        return true;
    }
    else
    {
        alert("Please specify the field in alphabets only");      
        return false;
    }
}
//End of fix for TTPro Issue 450

//For Hide-Show  Divisions on clicking of radio buttons
function hideDiv(){
document.getElementById('message').style.display ='none';
if(document.hotelSearchForm.choose_page[0].checked){
       
        //document.hotelSearchForm.name="hotelSearchForm";
        
        document.hotelSearchForm.action ="hotel/hotelWaitSearch.do" ;         
        
        document.getElementById("CityDiv").style.display = '';	
        
        document.getElementById("AddressDiv").style.display = 'none';	
        
        document.getElementById("CityDiv").style.visibility = 'visible';
		document.getElementById("AddressDiv").style.visibility = 'hidden';
		
     	//document.getElementById("statecode").style.visibility ='visible';
     	//document.getElementById("statecode").style.display = 'none';
      	//document.getElementById("countrycode").style.visibility = "visible";
      	//document.getElementById("countrycode").style.display = 'none';
     	//document.getElementById("cityText").innerHTML="City"; 
		// document.getElementById("StateText").style.display = '';	
     	//document.getElementById("StateText").innerHTML=""; 
       	//document.getElementById("CountryText").innerHTML=""; 
        var countryCode = document.hotelSearchForm.countrycode.value;

        //if (countryCode == "US" || countryCode == "-" ) {
 			// document.getElementById("st_show1").style.visibility = "visible";  
        	//document.getElementById("statecode").style.visibility = 'visible';  
      // } else if(countryCode && countryCode.length > 0) {
			// document.getElementById("st_show1").style.visibility = "hidden";
           // document.getElementById("statecode").style.visibility = 'hidden';
       // }
}

else if(document.hotelSearchForm.choose_page[1].checked){
        //document.hotelSearchForm.name="hotelAddressForm";
        document.hotelSearchForm.action ="hotel/hotelAddressWait.do" ; 
          
        document.getElementById("CityDiv").style.display = 'none';	
        document.getElementById("AddressDiv").style.display = '';	
         
        document.getElementById("CityDiv").style.visibility = 'hidden';
		document.getElementById("AddressDiv").style.visibility = 'visible';
		
        //document.getElementById("statecode").style.visibility = 'hidden';
        
		//document.getElementById("cityText").innerHTML="City"; 
		// document.getElementById("StateText").style.display = '';	
        // document.getElementById("StateText").innerHTML="State"; 
        
		 // document.getElementById("st_show1").style.visibility = "visible";
        // document.getElementById("statecode").style.visibility = "visible";
        // document.getElementById("statecode").style.display = 'none';
        // document.getElementById("StateText").innerHTML=""; 
       	// document.getElementById("CountryText").innerHTML=""; 
}
else if(document.hotelSearchForm.choose_page[2].checked){
       
       //document.hotelSearchForm.name="hotelSearchForm";
      
        document.hotelSearchForm.action ="hotel/hotelWaitSearch.do" ;         
       
       document.getElementById("CityDiv").style.display = 'none';	
       document.getElementById("AddressDiv").style.display = 'none';	
      
       document.getElementById("CityDiv").style.visibility = 'hidden';
	   document.getElementById("AddressDiv").style.visibility = 'hidden';
	  
       document.getElementById("statecode").style.visibility = 'hidden';
       // document.getElementById("cityText").innerHTML="Airport Code"; 
      
       document.getElementById("StateText").style.display = 'none';  
       document.getElementById("StateText").innerHTML=""; 
      
		
}
}
function displayStateForCountry(countrycode)
{

if (document.hotelSearchForm.countrycode.value == "US" || document.hotelSearchForm.countrycode.value == "-" ) {   
            
              document.getElementById("st_show1").style.visibility = "visible";
              //document.hotelSearchForm.statecode.style.visibility = 'visible';
               document.getElementById("statecode").style.visibility = "visible";
            

       } else {
            
              document.getElementById("st_show1").style.visibility = "hidden";
              //document.hotelSearchForm.statecode.style.visibility = 'hidden';
               document.getElementById("statecode").style.visibility = "hidden";  
              
             
        }

}

//End of Fix for TTPro Issue 458
