// JavaScript Document
function setCarDates()
{
    if (document.SearchForm.isSearchedOnceCars.value == "yes")
    {
    }
    else
    {
       var dd = new Date();

       dd.setDate( dd.getDate() + 1 );

       document.SearchForm.Date1.options[ dd.getDate() - 1 ].selected = true;
       document.SearchForm.Month1.options[ dd.getMonth() ].selected = true;

       for( var i=0; document.SearchForm.Year1.length > i; i++  )
       {
          if( document.SearchForm.Year1.options[i].value == dd.getFullYear() )
          {
              document.SearchForm.Year1.options[i].selected = true;
              break;
          }
       }

       dd.setDate( dd.getDate() + 1 );

       document.SearchForm.Date2.options[ dd.getDate() - 1 ].selected = true;
       document.SearchForm.Month2.options[ dd.getMonth() ].selected = true;

       for( var i=0; document.SearchForm.Year2.length > i; i++ )
       {
          if( document.SearchForm.Year2.options[i].value == dd.getFullYear() )
          {
              document.SearchForm.Year2.options[i].selected = true;
              break;
          }
       }

    }
}


function loadPickUpCountry()
{
	var countryArr = countryList.split(",");
	var arrLength = countryArr.length;

	var country = document.SearchForm.pickUpCountry;
	country.options.length = 0;

	var countryCode = "";
	var countryText = "";



	for (var i=0; i < arrLength; i++)
	{
		countryCode = countryArr[i];

		i++;
		countryText =  countryArr[i];

		var opt = new Option(countryText,countryCode);
		country.options[country.options.length] = opt;
  	}

	document.SearchForm.pickUpCountry.selectedIndex = 57;

	//set the selected country when browser back
	if (document.SearchForm.isSearchedOnceCars.value == "yes")
	{
            document.SearchForm.pickUpCountry.selectedIndex = document.SearchForm.SelectedPickupCountryIndex.value;
	}
}

function loadPickUpCity()
{
  	var selectedCountryCode = document.SearchForm.pickUpCountry.value;
	var selectedCountryText = document.SearchForm.pickUpCountry.options[document.SearchForm.pickUpCountry.selectedIndex].text;
	var sel = document.SearchForm.pickUpCity;

	if ( selectedCountryText == "Any" )
	{
		deleteAllOptions(sel);
        sel.options.length = 0;
        var opt = new Option("Any", "");
		sel.options[sel.options.length] = opt;
  	}
  	else
  	{
		var temp = eval(selectedCountryCode);
		var cityArr = temp.split(",");
		var cityCode = "";
		var cityText = "";
		var arrLength = cityArr.length;

	    deleteAllOptions(sel);

            for (var i=0; i < arrLength; i++)
		{
			cityCode = cityArr[i];

			ciytText =  cityArr[i];
			var opt = new Option(ciytText,cityCode);
			sel.options[sel.options.length] = opt;
		}



		if (document.SearchForm.isSearchedOnceCars.value == "yes")
		{
   		    document.SearchForm.pickUpCity.selectedIndex = document.SearchForm.SelectedPickupCityIndex.value;
		}
		else
		{
			document.SearchForm.pickUpCity.selectedIndex  = 0;

		}
  	}
}


function loadDropoffCountry()
{
	var countryArr = countryList.split(",");
	var arrLength = countryArr.length;

	var country = document.SearchForm.dropOffCountry;
	country.options.length = 0;

	var countryCode = "";
	var countryText = "";

	for (var i=0; i < arrLength; i++)
	{
		countryCode = countryArr[i];
		i++;
		countryText =  countryArr[i];

		var opt = new Option(countryText,countryCode);
		country.options[country.options.length] = opt;
  	}

	document.SearchForm.dropOffCountry.selectedIndex = 1;

	//set the selected country when browser back
	if (document.SearchForm.isSearchedOnceCars.value == "yes")
	{
            document.SearchForm.dropOffCountry.selectedIndex = document.SearchForm.SelectedDropoffCountryIndex.value;
	}
}

function setDropoffCountry()
{
    document.SearchForm.dropOffCountry.selectedIndex = document.SearchForm.pickUpCountry.selectedIndex;
}

function loadDropoffCity()
{
    var selectedCountryCode = document.SearchForm.dropOffCountry.value;
    var selectedCountryText = document.SearchForm.dropOffCountry.options[document.SearchForm.dropOffCountry.selectedIndex].text;
    var sel = document.SearchForm.dropOffCity;

    if ( selectedCountryText == "Any" )
    {
        deleteAllOptions(sel);
        sel.options.length = 0;
        var opt = new Option("Any", "");
        sel.options[sel.options.length] = opt;
    }
    else
    {
        var temp = eval(selectedCountryCode);
        var cityArr = temp.split(",");
        var cityCode = "";
        var cityText = "";
        var arrLength = cityArr.length;

        deleteAllOptions(sel);

        for (var i=0; i < arrLength; i++)
        {
            cityCode = cityArr[i];
            ciytText =  cityArr[i];
            var opt = new Option(ciytText,cityCode);
            sel.options[sel.options.length] = opt;
        }

	if (document.SearchForm.isSearchedOnceCars.value == "yes")
	{
            document.SearchForm.dropOffCity.selectedIndex = document.SearchForm.SelectedDropoffCityIndex.value;
	}
    }
}


function deleteAllOptions (select)
{
  select.options.length = 0;
}

function setDropOffCity()
 {
 	document.SearchForm.dropOffCity.selectedIndex  = document.SearchForm.pickUpCity.selectedIndex;

 }

 function setDropOffDate()
 {
   document.SearchForm.Date2.selectedIndex  = document.SearchForm.Date1.selectedIndex ;

 }

 function setDropOffMonth()
 {
   document.SearchForm.Month2.selectedIndex  = document.SearchForm.Month1.selectedIndex;

 }
  function setDropOffYear()
 {
   document.SearchForm.Year2.selectedIndex  = document.SearchForm.Year1.selectedIndex;

 }

 function daysInMonth( Month, Year )
  {
      var DaysInMonth = 31;
      var WhichMonth = parseInt( Month );
      if (WhichMonth == 4 || WhichMonth == 6 || WhichMonth == 9 || WhichMonth == 11 ) DaysInMonth = 30;
      if (WhichMonth == 2 && ( Year/4) != Math.floor( Year/4))	DaysInMonth = 28;
      if (WhichMonth == 2 && ( Year/4) == Math.floor( Year/4))	DaysInMonth = 29;
      return DaysInMonth;
  }


 function formReset()
 {
    document.SearchForm.reset();
 }

 function formSubmitCars()
 {
    var frm = document.SearchForm;

    var daysForSelectedMonth = daysInMonth( frm.Month1.value , frm.Year1.value );

    if( daysForSelectedMonth >= frm.Date1.value )
    {
      daysForSelectedMonth = daysInMonth( frm.Month2.value , frm.Year2.value );

      if( daysForSelectedMonth >= frm.Date2.value )
      {

        chkIn = new Date( frm.Month1.value + "/" + frm.Date1.value + "/" + frm.Year1.value );
        chkOut = new Date( frm.Month2.value + "/" + frm.Date2.value + "/" + frm.Year2.value );

        if( chkOut > chkIn )
        {
          frm.CheckIn.value=frm.Month1.value+"/"+frm.Date1.value+"/"+frm.Year1.value;
          frm.CheckOut.value=frm.Month2.value+"/"+frm.Date2.value+"/"+frm.Year2.value;
          document.SearchForm.isSearchedOnceCars.value = "yes";
          frm.SelectedPickupCityIndex.value = frm.pickUpCity.selectedIndex;
          frm.SelectedPickupCountryIndex.value = frm.pickUpCountry.selectedIndex;
          frm.SelectedDropoffCityIndex.value = frm.dropOffCity.selectedIndex
          frm.SelectedDropoffCountryIndex.value = frm.dropOffCountry.selectedIndex

          document.SearchForm.submit();
        }
        else
          alert( "Invalid check out date .");

      }
      else
        alert( "Invalid check out date .");
    }
    else
      alert( "Invalid check in date .");

  }



