// 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
//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)

{
   if(form.countrycode.value=='')
    {
      alert("Please specify the Country")
      return false;
    }

     if(form.city.value=='')
    {
      alert("Please specify the City name or Airport Code")
      return false;
    }

    //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.statecode.value=='-'|| form.statecode.value=='')
     {
       alert("Please select a State from the list as it is a mandatory field")
       return false;
     }
    }


    //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;
    }


	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;
		         }
	}
	//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(){

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("cityText").innerHTML="City";
        document.getElementById("StateText").style.display = '';
        document.getElementById("StateText").innerHTML="State";

        var countryCode = document.hotelSearchForm.countrycode.value;

        if (countryCode == "US" || countryCode == "-" ) {
              document.getElementById("st_show1").style.visibility = "visible";
              document.getElementById("statecode").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';
              //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 = 'visible';
        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";

}
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

