/* 
carHireContol.js
Auhtor S Goyet
Last modified 30/4/2004
DESCRIPTION:controls for shopbot car hire form
*/ 
var cal = new CalendarPopup();
cal.setReturnFunction("setDepDate");
cal.setMonthNames(month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12);
cal.setDayHeaders(day1,day2,day3,day4,day5,day6,day7);
cal.setWeekStartDay(1);
cal.setTodayLabel(todayLabel);

var cal2 = new CalendarPopup();
cal2.setReturnFunction("setRetDate");
cal2.setMonthNames(month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12);
cal2.setDayHeaders(day1,day2,day3,day4,day5,day6,day7);
cal2.setWeekStartDay(1);
cal2.setTodayLabel(todayLabel); 

function ctrlForm(form)
{
  //  document.searchForm.pickUpCountry.value = document.searchForm.COUNTRY.value;
  // document.searchForm.pickUpTown.value = document.searchForm.REGION.value;
  //document.searchForm.pickUpAdress.value = document.searchForm.CITY.value;
  //document.searchForm.pickUpAirportCode.value = document.searchForm.vtravelCITY.value;


  if (form.carTypeFormatted != null){
  var carTypeName2 = form.carType.options[form.carType.selectedIndex].text;
  form.carTypeFormatted.value=carTypeName2;
  }


    
  var now = new Date();
  var month = now.getMonth()+1; 
  var year = now.getFullYear(); 
  var day = now.getDate();
  selectedDepDay = form.dayDeparture.options[form.dayDeparture.selectedIndex].value;
  selectedDepMonth = form.monthDeparture.options[form.monthDeparture.selectedIndex].value;
  selectedDepYear = form.yearDeparture.options[form.yearDeparture.selectedIndex].value;
  selectedRetDay = form.dayReturn.options[form.dayReturn.selectedIndex].value;
  selectedRetMonth = form.monthReturn.options[form.monthReturn.selectedIndex].value;
  selectedRetYear = form.yearReturn.options[form.yearReturn.selectedIndex].value;
  
  selectedDepDate = new Date(selectedDepYear, selectedDepMonth -1, selectedDepDay);
  selectedRetDate = new Date(selectedRetYear, selectedRetMonth -1, selectedRetDay);
  
  today = new Date();
  tableJJ=[31,29,31,30,31,30,31,31,30,31,30,31];
  
  //check if dates are correct (if not selected from calendar)
  if (selectedDepDay>tableJJ[selectedDepMonth-1] || selectedRetDay>tableJJ[selectedRetMonth-1])
    {
      alert (alert1);
      return false;
    }
  
  selectedDepTime=selectedDepDate.getTime() + 24*3600*1000;	
  selectedRetTime=selectedRetDate.getTime() + 24*3600*1000;
  
  //check if departure date is correct
  if ((today.getTime() > selectedDepTime) ||  (today.getTime() > selectedRetTime))
    {
      alert (alert2);
      return false;
    }
  
  //check if departure date < arrival date
  if (selectedRetTime < selectedDepTime)
    {
      alert (alert3);
      return false;
    }
  
  var testLocation="|"+document.searchForm.pickUpCountry.options[document.searchForm.pickUpCountry.selectedIndex].value+"|"+document.searchForm.pickUpTown.options[document.searchForm.pickUpTown.selectedIndex].value+"|"+document.searchForm.pickUpAdress.options[document.searchForm.pickUpAdress.selectedIndex].value+"|";
  if ( (testLocation.indexOf("------") >=0) || (testLocation.indexOf("Select") >=0) || (testLocation.indexOf("||") >=0) )
    {
      alert(alertLoc);
      return(false);
    }
 form.waitingTravelPopup.value = "yes";

  return true;
}

function currentYear()
{
  var now = new Date(); 
     var year = now.getYear(); 
     if(year < 999) year+=1900;
     return year;
}


function setDepDate(y,m,d)
{
  var year = currentYear();
  document.searchForm.dayDeparture.selectedIndex = d-1;
  document.searchForm.monthDeparture.selectedIndex = m-1;
  if (y == year) document.searchForm.yearDeparture.selectedIndex = 0;
  if (y == year+1) document.searchForm.yearDeparture.selectedIndex = 1;
    setMonth();
    setYear();
    if (dayCheck == "yes")
      {
	getDepWeekDay(d,m,y);
      }
}
function setRetDate(y,m,d)
{	
  var year = currentYear();
  document.searchForm.dayReturn.selectedIndex = d-1;
  document.searchForm.monthReturn.selectedIndex = m-1;
  if (y == year) document.searchForm.yearReturn.selectedIndex = 0;
  if (y == year+1) document.searchForm.yearReturn.selectedIndex = 1;
    if (dayCheck == "yes")
      {
	getRetWeekDay(d,m,y);
      }
}

function setMonth()
{
  document.searchForm.monthReturn.selectedIndex=document.searchForm.monthDeparture.selectedIndex;
    if (dayCheck == "yes")
      {
        updateWeekDay();
      }
}
                   
function setYear()
{
    document.searchForm.yearReturn.selectedIndex=document.searchForm.yearDeparture.selectedIndex  ;
    if (dayCheck == "yes")
      {
        updateWeekDay();
      }
}


function updateWeekDay()
{
  
  var dd = document.searchForm.dayDeparture.value;
  var dm = document.searchForm.monthDeparture.value;
  var dy = document.searchForm.yearDeparture.value;

  if (dy%4 == 0)
     var DMonthDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  else
     var DMonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
                                                                                                                                                            
  if (dd > DMonthDays[dm-1])
  {
        dd = DMonthDays[dm-1];
        document.searchForm.dayDeparture.value = dd;
  }

 
  var rd = document.searchForm.dayReturn.value;
  var rm = document.searchForm.monthReturn.value;
  var ry = document.searchForm.yearReturn.value;

  if (ry%4 == 0)
     var RMonthDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  else
     var RMonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
                                                                                                                                                            
  if (rd > RMonthDays[rm-1])
  {
        rd = RMonthDays[rm-1];
        document.searchForm.dayReturn.value = rd;
  }

  getDepWeekDay(dd,dm,dy);
  getRetWeekDay(rd,rm,ry);
 }

function getDepWeekDay(d,m,y)
{
  if (typeof (document.searchForm.wdday)!="undefined"){
        var testdate = new Date(y,m-1,d);
        var weekday  =  testdate.getDay();
        //alert(d + "/" + "/" + m + "/" + y + " Date" + testdate + " " + weekday);
        if (weekday == 0)
                document.searchForm.wdday.value = Sun;
        if (weekday == 1)
                document.searchForm.wdday.value = Mon;
        if (weekday == 2)
                document.searchForm.wdday.value = Tue;
        if (weekday == 3)
                document.searchForm.wdday.value = Wed;
        if (weekday == 4)
                document.searchForm.wdday.value = Thu;
        if (weekday == 5)
                document.searchForm.wdday.value = Fri;
        if (weekday == 6)
                document.searchForm.wdday.value = Sat;
      }                                                                                                                                                            
}
function getRetWeekDay(d,m,y)
{
  if (typeof (document.searchForm.wrday)!="undefined"){
        var testdate = new Date(y,m-1,d);
        var weekday  =  testdate.getDay();
        //alert(d + "/" + "/" + m + "/" + y + " Date" + testdate + " " + weekday);
        if (weekday == 0)
                document.searchForm.wrday.value = Sun;
        if (weekday == 1)
                document.searchForm.wrday.value = Mon;
        if (weekday == 2)
                document.searchForm.wrday.value = Tue;
        if (weekday == 3)
                document.searchForm.wrday.value = Wed;
        if (weekday == 4)
                document.searchForm.wrday.value = Thu;
        if (weekday == 5)
                document.searchForm.wrday.value = Fri;
        if (weekday == 6)
                document.searchForm.wrday.value = Sat;
  }                                                                                                                                
}


function maj_carHireDate() {
   if (document.searchForm.dayDeparture.value =="1" && document.searchForm.dayReturn.value =="1" && document.searchForm.monthDeparture.value =="1" && document.searchForm.monthReturn.value == "1")
    {
        var now = new Date();
        var tmp = now.getTime()+(86400000*7);
        now.setTime(tmp);
        var month = now.getMonth()+1;
        var year = now.getFullYear();
        var day = now.getDate();                                                                                                   
        document.searchForm.dayDeparture.options[day-1].selected=true;
        document.searchForm.monthDeparture.options[month-1].selected=true;
        document.searchForm.yearDeparture.options[year-this_year].selected=true;

        var now2 = new Date();
        tmp = now2.getTime()+(86400000*10);
        now2.setTime(tmp);
        month = now2.getMonth()+1;
        year = now2.getFullYear();
        day = now2.getDate();
        document.searchForm.dayReturn.options[day-1].selected=true;
        document.searchForm.monthReturn.options[month-1].selected=true;
        document.searchForm.yearReturn.options[year-this_year].selected=true;
	}
}

function getParameter ( queryString, parameterName ) {
var parameterName = parameterName + "=";
if ( queryString.length > 0 ) {
begin = queryString.indexOf ( parameterName );
if ( begin != -1 ) {
begin += parameterName.length;
end = queryString.indexOf ( "&" , begin );
if ( end == -1 ) {
end = queryString.length
}
strtmp = unescape ( queryString.substring ( begin, end ) );
parameter= strtmp.replace(/\+/g, " ");
 return parameter;
}
return "null";
}
}
