



function trim(value){
  return value.replace(/^\s+|\s+$/,'');
}

function populatePuDate()
{
	document.SearchResultsForm.doDay.selectedIndex=document.SearchResultsForm.puDay.selectedIndex;
	document.SearchResultsForm.doMonth.selectedIndex=document.SearchResultsForm.puMonth.selectedIndex;
	document.SearchResultsForm.doYear.selectedIndex=document.SearchResultsForm.puYear.selectedIndex;
}

function setPuMultipleValues(y,m,d) {
    document.SearchResultsForm.puDay.value=d;
    document.SearchResultsForm.puMonth.value=m;
    document.SearchResultsForm.puYear.value=LZ(y);
}                                                                                                

function setDoMultipleValues(y,m,d) {
    document.SearchResultsForm.doDay.value=d;
    document.SearchResultsForm.doMonth.value=m;
    document.SearchResultsForm.doYear.value=LZ(y);
}                                                                                                

function validateSearchResultsForm(searchForm){
    var errors = "";
    
    errors = validateDriversAge(searchForm.driversAge, "Drivers Age", errors);    

    errors = validateMandatoryDroplist(searchForm.country, "Pickup Country", errors);
    errors = validateMandatoryDroplist(searchForm.city, "Pickup City", errors);
    errors = validateMandatoryDroplist(searchForm.location, "Pickup Location", errors);
    errors = validateMandatoryDroplist(searchForm.dropCountry, "Dropoff Country", errors);
    errors = validateMandatoryDroplist(searchForm.dropCity, "Dropoff City", errors);
    errors = validateMandatoryDroplist(searchForm.dropLocation, "Dropoff Location", errors);
 
    errors = validateDate(searchForm.puYear.value, searchForm.puMonth.value-1, searchForm.puDay.value, "Pickup Date", errors);
    errors = validateDate(searchForm.doYear.value, searchForm.doMonth.value-1, searchForm.doDay.value, "Dropoff Date", errors);
    errors = validateDates(
                searchForm.puYear.value, searchForm.puMonth.value-1, searchForm.puDay.value, searchForm.puHour.value, searchForm.puMinute.value, "Pickup Date", 
                searchForm.doYear.value, searchForm.doMonth.value-1, searchForm.doDay.value, searchForm.doHour.value, searchForm.doMinute.value, "Dropoff Date", errors);
    
    errors = validateCutOffDate(searchForm.puYear.value, searchForm.puMonth.value-1, searchForm.puDay.value, "Pickup Date", errors);
    
    if(errors==""){
        return true;
    } else {
        alert(errors);
        return false;
    }        
}

function validateMandatoryDroplist(droplist, name,  errors){
    if(droplist.value=="" || droplist.value==0){    
        errors = errors + name + " must be specified\n";
    }
    return errors;
}

function validateDates(yearFrom, monthFrom, dayFrom, hourFrom, minuteFrom, nameFrom, yearTo, monthTo, dayTo, hourTo, minuteTo, nameTo, errors){
    var date = new Date();
    var dateFrom = new Date(yearFrom, monthFrom, dayFrom, hourFrom, minuteFrom, 0);
    var dateTo = new Date(yearTo, monthTo, dayTo, hourTo, minuteTo, 0);

    if(dateFrom.valueOf() < date.valueOf()){
        errors = errors + nameFrom + " is before today's date\n";
    }
    
    if(dateFrom.valueOf() > dateTo.valueOf()){
        errors = errors + nameFrom + " must be before " + nameTo + "\n";
    }
    return errors;
}

function validateDate(year, month, day, name, errors){
    if(!isValidDate(year, month, day)){
        errors = errors + name + " is not a valid date\n";
    } 
    return errors;
}

function validateCutOffDate(year, month, day, name, errors){
    var cutOffDate = new Date(2005, 11, 28);
    var newDate = new Date(year, month, day);
    if(cutOffDate.valueOf()>newDate.valueOf()){
        errors = errors + "Pickup date must be after 27th December 2005\n";
    }
    return errors;
}
function isValidDate(year, month, day){  
    var tempDate = new Date(year, month, day);
    var tempYear = tempDate.getYear()<2000?tempDate.getYear()+1900:tempDate.getYear();
    return (tempYear==year && tempDate.getMonth()==month && tempDate.getDate()==day);
}    

function isNumeric(sText){
   var validChars  = "0123456789";
   var returnValue = true;
   var currentChar;
 
   for (var i = 0; i < sText.length && returnValue == true; i++) { 
      currentChar = sText.charAt(i); 
      if (validChars.indexOf(currentChar) == -1) {
         returnValue = false;
      }
   }
   return returnValue;
}

function validateDriversAge(driversAge, name, errors){
        
    
    var minDriversAge = 21;
    var maxDriversAge = 75;
    if(driversAge.value==""){
        errors = errors + name + " must be specified\n";
    } else if(!isNumeric(driversAge.value)){
        errors = errors + name + " must be a number\n";
    } else if(driversAge.value < minDriversAge) {
        errors = errors + name + " must be at least " + minDriversAge + "\n";
    } else if(driversAge.value > maxDriversAge) {
        errors = errors + name + " must be no more than " + maxDriversAge + "\n";
    }
    return errors;
}


function headingClick(checkbox, options, index){
    var element = document.getElementById(options);
    if(checkbox.checked==true){
        element.style.display='none';
        var i=0;
        while(true){
            var subelem = document.getElementById("optionCB" + index + ":" + i++);
            if(subelem==null || i>100)
                break;
            subelem.checked = false;
        }
    } else {
        element.style.display='';
    }
}

function disableSearchResultsForm(isDisabled){
    if(isDisabled){    
        document.SearchResultsForm.country.disabled=isDisabled;
        document.SearchResultsForm.city.disabled=isDisabled;
        document.SearchResultsForm.location.disabled=isDisabled;
        document.SearchResultsForm.dropCountry.disabled=isDisabled;
        document.SearchResultsForm.dropCity.disabled=isDisabled;
        document.SearchResultsForm.dropLocation.disabled=isDisabled;
    }     
    document.SearchResultsForm.puDay.disabled=isDisabled;
    document.SearchResultsForm.puMonth.disabled=isDisabled;
    document.SearchResultsForm.puYear.disabled=isDisabled;
    document.SearchResultsForm.puHour.disabled=isDisabled;
    document.SearchResultsForm.puMinute.disabled=isDisabled;

    document.SearchResultsForm.doDay.disabled=isDisabled;
    document.SearchResultsForm.doMonth.disabled=isDisabled;
    document.SearchResultsForm.doYear.disabled=isDisabled;
    document.SearchResultsForm.doHour.disabled=isDisabled;
    document.SearchResultsForm.doMinute.disabled=isDisabled;
}
function populateDroplists(dropList){
    var debug="";
    if(debug!=""){
        disableSearchResultsForm(true);
    }
    var dynaURL = "/DynaDroplist.do?country=" + document.SearchResultsForm.country.value;
    var ie5dyna = dynaURL;
    if(dropList!="country"){
        dynaURL = dynaURL + "&city=" + document.SearchResultsForm.city.value;
        ie5dyna = ie5dyna + "&city=" + document.SearchResultsForm.city.value;
        if(dropList!="city"){
            dynaURL = dynaURL + "&location=" + document.SearchResultsForm.location.value;
            ie5dyna = ie5dyna + "&location=" + escape(document.SearchResultsForm.location.value);
            if(dropList!="location"){
                dynaURL = dynaURL + "&dropCountry=" + document.SearchResultsForm.dropCountry.value;
                ie5dyna = ie5dyna + "&dropCountry=" + escape(document.SearchResultsForm.dropCountry.value);
                if(dropList!="dropCountry"){
                    dynaURL = dynaURL + "&dropCity=" + document.SearchResultsForm.dropCity.value;
                    ie5dyna = ie5dyna + "&dropCity=" + escape(document.SearchResultsForm.dropCity.value);
                    if(dropList!="dropCity"){
                        dynaURL = dynaURL + "&dropLocation=" + document.SearchResultsForm.dropLocation.value;
                        ie5dyna = ie5dyna + "&dropLocation=" + escape(document.SearchResultsForm.dropLocation.value);
                        if(dropList!="puYear"){
                            dynaURL = dynaURL + "&puYear=" + document.SearchResultsForm.puYear.value;
                            ie5dyna = ie5dyna + "&puYear=" + escape(document.SearchResultsForm.puYear.value);
                            if(dropList!="puMonthr"){
                                dynaURL = dynaURL + "&puMonth=" + document.SearchResultsForm.puMonth.value;
                                ie5dyna = ie5dyna + "&puMonth=" + escape(document.SearchResultsForm.puMonth.value);
                                if(dropList!="puDay"){
                                    dynaURL = dynaURL + "&puDay=" + document.SearchResultsForm.puDay.value;
                                    ie5dyna = ie5dyna + "&puDay=" + escape(document.SearchResultsForm.puDay.value);
                                    if(dropList!="puHour"){
                                        dynaURL = dynaURL + "&puHour=" + document.SearchResultsForm.puHour.value;
                                        ie5dyna = ie5dyna + "&puHour=" + escape(document.SearchResultsForm.puHour.value);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
	var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    var IEVerNum=6;
    if (MSIEOffset != -1){
	    IEVerNum=parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
    if (IEVerNum<5.5)
    {
		dynaURL = ie5dyna;
	}
	else
	{
		dynaURL = encodeURI(dynaURL);
	}
    if(debug!=""){    
        dynaURL = dynaURL + "&debug=true";
        alert(dynaURL);    
    }    
    document.getElementById("dynaFrame").src = dynaURL;
}     
       
function addOption(droplist, name, value){
    droplist.options[droplist.options.length] = new Option(name, value);
}

