
function getForm()
{
	return document.SearchResultsForm;
		
}

function trim(value){
  return value.replace(/^\s+|\s+$/,'');
}

function populatePuDate(dropList)
{
  //if month selected is a month gone, select next year as the year.
  if(dropList != null && dropList == 'puMonth')
  {
    var date = new Date();
    if(getForm().puMonth.value < date.getMonth()+1)
    {
      getForm().puYear.value = Number(date.getFullYear())+1;
    }
  }

	getForm().doDay.selectedIndex=getForm().puDay.selectedIndex;
	getForm().doMonth.selectedIndex=getForm().puMonth.selectedIndex;
	getForm().doYear.selectedIndex=getForm().puYear.selectedIndex;
}

function populateDoDate(dropList)
{
  //if month selected is a month gone, select next year as the year.
  if(dropList != null && dropList == 'doMonth')
  {
    var date = new Date();
    if(getForm().doMonth.value < date.getMonth()+1)
    {
      getForm().doYear.value = Number(date.getFullYear())+1;
    }
  }  
}

function setPuMultipleValues(y,m,d) {
    getForm().puDay.value=d;
    getForm().puMonth.value=m;
    getForm().puYear.value=LZ(y);
}                                                                                                

function setDoMultipleValues(y,m,d) {
    getForm().doDay.value=d;
    getForm().doMonth.value=m;
    getForm().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){   
    	
        	getForm().country.disabled=isDisabled;
        
        getForm().city.disabled=isDisabled;
        
        	getForm().location.disabled=isDisabled;
        
        
        	getForm().dropCountry.disabled=isDisabled;
        
        getForm().dropCity.disabled=isDisabled;
        
        	getForm().dropLocation.disabled=isDisabled;
        
    }     
    getForm().puDay.disabled=isDisabled;
    getForm().puMonth.disabled=isDisabled;
    getForm().puYear.disabled=isDisabled;
    getForm().puHour.disabled=isDisabled;
    getForm().puMinute.disabled=isDisabled;

    getForm().doDay.disabled=isDisabled;
    getForm().doMonth.disabled=isDisabled;
    getForm().doYear.disabled=isDisabled;
    getForm().doHour.disabled=isDisabled;
    getForm().doMinute.disabled=isDisabled;
}


function ptSetup()
{
	var e;
			
	e = document.getElementsByName('promoCity$5960');
	for(var i=0; i<e.length; i++) {
		e[i].disabled = !(((!(true&&true))&&((!false)&&(!false)))||(!(!(!false))));
	}		
			
	e = document.getElementsByName('promoCountry$5154');
	for(var i=0; i<e.length; i++) {
		e[i].disabled = !((((true&&true)&&(0))||((true||false)&&(0)))||(((false||false)||(true||false))&&((true&&false)||(false||false))));
	}		
			
	e = document.getElementsByName('promoLoc$5901');
	for(var i=0; i<e.length; i++) {
		e[i].disabled = !(((!(!true))&&((true&&true)&&(true||false)))&&(((false||true)&&(!true))||((0)||(true&&false))));
	}		
			
	e = document.getElementsByName('promoAge$7972');
	for(var i=0; i<e.length; i++) {
		e[i].disabled = !(((!(!false))||((0+(0<<1))))&&(!((false||false)||(false||false))));
	}		
	
}

function makeUrl(start, parameters)
{
	var url = start;
	for(var i=0; i < parameters.length; i++)
	{
		/*if(i==0){
			url += "?";
		}else{ */
			url += "&";
		//}
		url += parameters[i][0] + "=" + parameters[i][1];
	}
	return url;
}

function makeDynaDroplistUrl(dropList, serverUrl)
{
	var debug="";
	
    if(debug!=""){
        disableSearchResultsForm(true);
    }
    
    var dynaParams = new Array();
    
	
        
		
		
		
			dynaParams[dynaParams.length] = new Array("country", getForm().country.value);
			if(dropList!="country"){
				dynaParams[dynaParams.length] = new Array("city", getForm().city.value);
				if(dropList!="city"){
					dynaParams[dynaParams.length] = new Array("location", getForm().location.value);
					if(dropList!="location"){
						dynaParams[dynaParams.length] = new Array("dropCountry", getForm().dropCountry.value);
						if(dropList!="dropCountry"){
							dynaParams[dynaParams.length] = new Array("dropCity", getForm().dropCity.value);
							if(dropList!="dropCity"){
								dynaParams[dynaParams.length] = new Array("dropLocation", getForm().dropLocation.value);
								if(dropList!="puYear"){
									dynaParams[dynaParams.length] = new Array("puYear", getForm().puYear.value);
									if(dropList!="puMonth"){
										dynaParams[dynaParams.length] = new Array("puMonth", getForm().puMonth.value);
										if(dropList!="puDay"){
											dynaParams[dynaParams.length] = new Array("puDay", getForm().puDay.value);
											if(dropList!="puHour"){
												dynaParams[dynaParams.length] = new Array("puHour", getForm().puHour.value);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		
	
    
    /* Add debug parameter */
    if(debug!=""){
    	dynaParams[dynaParams.length] = new Array("debug", "true");
    }
    
  	/* Get IE Version number */
	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)
    {
		/* Encode each parameter */
		for(var i=0; i < dynaParams.length; i++)
		{
			dynaParams[i][1] = escape(dynaParams[i][1]);
		}
	}
	
	var serverPart = "";
    if(serverUrl!=null){
    	serverPart = serverUrl;
    }
    
  	var dynaURL = makeUrl(serverPart, dynaParams);
		
	
	
	if(IEVerNum >= 5.5)
	{
		/* encode whole url */
		dynaURL = encodeURI(dynaURL);
	}
	
	/* Show url to debug */
    if(debug!=""){    
       // alert(dynaURL);    
	   document.write(dynaURL);
    }
    
    return dynaURL;
}

var listInd=1;
function populateDroplists(dropList)
{
	listInd=parseInt(dropList); 
	if(listInd==1 || listInd==2)
	{
	 var itm=document.getElementById("dropCountry");
	 itm.selectedIndex=0;
	// itm.disabled=true;
	 itm=document.getElementById("dropCity");
	 itm.selectedIndex=0;
	 //itm.disabled=true;
	 itm=document.getElementById("dropLocation");
	 itm.selectedIndex=0;
	 //itm.disabled=true;
	 if(listInd==1)
	 {
	 itm=document.getElementById("Location");
	 itm.selectedIndex=0;
	 //itm.disabled=true;
	 }
	}
	
	if(listInd==3)
	{
	 var itm=document.getElementById("dropCountry");
	 itm.disabled=false;
	 itm=document.getElementById("dropCity");
	 itm.disabled=false;
	 itm=document.getElementById("dropLocation");
	 itm.disabled=false;
	 itm=document.getElementById("Location");
	 itm.disabled=false;
	}
	var serverUrl  ="xml_out.php?act="+dropList;
	var dynaURL = makeDynaDroplistUrl(dropList, serverUrl);
	//if(listInd==3)
	//document.write( dynaURL);  
    document.getElementById("loading").style.visibility="visible";
    
   	 loadXML(dynaURL,show);
}


function show()
{
	
	var msg="";
	var proceed="";
	 if(listInd==1)
	  popList("City");
	 else if(listInd==2)
	 {
	 popList("Location");
		 if(document.getElementById("Location").length==2)
		 {
		 popList("dropCountry");
		 popList("dropCity");
		 popList("dropLocation");
		 document.getElementById("dropCountry").value=document.getElementById("Country").value; 
		 document.getElementById("dropCity").value=document.getElementById("City").value; 
		 document.getElementById("dropLocation").value=document.getElementById("Location").value; 
		  var itm=document.getElementById("dropCountry");
		 itm.disabled=false;
		 itm=document.getElementById("dropCity");
		 itm.disabled=false;
		 itm=document.getElementById("dropLocation");
		 itm.disabled=false;
		 itm=document.getElementById("Location");
		 itm.disabled=false;
		 }
	 }
	 else if(listInd==3)
	 {
	 popList("dropCountry");
	  document.getElementById("dropCountry").value=document.getElementById("Country").value; 

	 popList("dropCity");
	 document.getElementById("dropCity").value=document.getElementById("City").value; 
	
	 popList("dropLocation");
	 document.getElementById("dropLocation").value=document.getElementById("Location").value; 
	
	 }
	 else if(listInd==4)
	 {
	 popList("dropCity");
	 document.getElementById("dropCity").value=document.getElementById("City").value; 

	 popList("dropLocation");
	 document.getElementById("dropLocation").value=document.getElementById("Location").value; 
	
	 }
	 else if(listInd==5)
	 {
	 popList("dropLocation");
	  document.getElementById("dropLocation").value=document.getElementById("Location").value; 

	 }
	 
	
	  document.getElementById("loading").style.visibility="hidden";
	 
}

function popList(listName)
{
	
	   if(document.getElementById(listName))
	   {
		   		
			var combobox =document.getElementById(listName);
			combobox.options.length = 1;
			var nodes=xdoc.getElementsByTagName(listName);
			for(var i=0;i<nodes.length;i++)
			{
			 if(listName.indexOf("Location")!=-1)
			 combobox.options[combobox.length]=new Option(nodes[i].childNodes[0].nodeValue,nodes[i].getAttribute("id"));	 
			 else
			 combobox.options[combobox.length]=new Option(nodes[i].childNodes[0].nodeValue,nodes[i].childNodes[0].nodeValue);	 
			}
			
			if(combobox.length==2) //only one item
			combobox.selectedIndex=1;
			
			if(listName=="dropLocation" && document.getElementById("City").value==document.getElementById("dropCity").value)
			{
				
			combobox.selectedIndex=document.getElementById("Location").selectedIndex;
			}
			
	   }
	   
}


function populateSecureDroplists(dropList, serverUrl)
{
	var dynaURL = makeDynaDroplistUrl(dropList, serverUrl);
	    
    var response=makeRequest(dynaURL);
    eval(response);
}     

function makeRequest(url)
{
    var req=false;
    var returnValue = "";
    if(window.XMLHttpRequest) {
     try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
    } else if(window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
       } catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                req = false;
            }
 }
    }
    if(req){
        req.open("GET", url, false);
        req.send(null);
        if (req.readyState==4) {
            returnValue = req.responseText;            
        }
    }
    return returnValue;
}       
function addOption(droplist, name, value){
    droplist.options[droplist.options.length] = new Option(name, value);
}


