﻿// JScript File

 function  CarSearch() {
    this.dateFormat = "dd/MM/yyyy";
    this.pickupDateInputId = null;
    this.dropoffDateInputId = null;
    this.pickupHourSelectId = null;
    this.dropoffHourSelectId = null;
    this.DaysThreshold = 0;
    this.pickupLocationInputId = -1;
    this.dropoffLocationInputId = -1;
    this.difDropoffCheckId = false;
    this.supplierSelectId = -1;
    this.contractSelectId = -1;
    this.idForm = "frmMain";
    this.CLP = "";
    
    this.msgSelectPickup = "You must select a pickup location";
    this.msgSelectDropOff = "You must select a dropoff location";
    this.msgPickupDate = "You must select a pickup date location";
    this.msgDropoffDate = "You must select a pickup location";
    this.msgPickupDropoffDate = "You must select a pickup location";
    this.msgDaysThreshold = "You must select a pickup location";
    this.msgSelectContract = "-- All --";
    this.urlPost = "/rCars/searchResults.aspx";
 
    //private vars
    this.pickupDate = null;
    this.dropoffDate = null;
    this.pickupHour = null;
    this.dropoffHour = null;
    this.pickupLocation = null;
    this.dropoffLocation = null;
    this.difDropoff = null;
    this.supplier = null;
    this.contract = null;
 }
 
 
 CarSearch.prototype.loadVars = function() {
    this.pickupDate = document.getElementById(this.pickupDateInputId).value;    
    this.dropoffDate = document.getElementById(this.dropoffDateInputId).value;    
    this.pickupHour = document.getElementById(this.pickupHourSelectId).value;
    this.dropoffHour = document.getElementById(this.dropoffHourSelectId).value;
    this.pickupLocation = document.getElementById(this.pickupLocationInputId).value;    
    this.dropoffLocation = document.getElementById(this.dropoffLocationInputId).value;    
    this.difDropoff = document.getElementById(this.difDropoffCheckId).checked;    
    if (document.getElementById(this.supplierSelectId)) this.supplier = document.getElementById(this.supplierSelectId).value;    
    if (document.getElementById(this.contractSelectId)) this.contract = document.getElementById(this.contractSelectId).value;     
 }
 
 
 CarSearch.prototype.checkFields = function() {
    this.loadVars();
    var error = "";
    if (this.pickupLocation == "") error += "* " + this.msgSelectPickup;
    if (this.difDropoff && this.dropOffLocation == "") error += "* " + this.msgSelectDropOff + "\n";
    if (!isValidDate(this.pickupDate,this.dateFormat)) error += "* " + this.msgPickupDate + "\n";
    if (!isValidDate(this.dropoffDate,this.dateFormat)) error += "* " + this.msgDropoffDate + "\n";
    if (error == "") {
        var Ini = StringToDate(this.pickupDate, this.dateFormat);
        var End = StringToDate(this.dropoffDate, this.dateFormat);
		var dayTh = new Date();
		dayTh.setDate(dayTh.getDate() + this.DaysThreshold);
		dayTh.setHours(0,0,0,0);
        if (Ini < dayTh) error += "* " + this.msgDaysThreshold + "\n";		    
		if (End < Ini)  error += "* " + this.msgPickupDropoffDate + "\n";
    }
    if (error != "") {alert(error);return false;}
	return true;
 }

 CarSearch.prototype.search = function() {
   if (this.checkFields()) {
     var params = "";
     var action = "searchCar"
     var form = document.getElementById(this.idForm);
     params += "&pickupLocation=" + this.pickupLocation; 
     if (this.difDropoff) params += "&dropoffLocation=" + this.dropoffLocation; else params += "&dropoffLocation=" + this.pickupLocation; 
     params += "&pickupDate=" + this.pickupDate; 
     params += "&dropoffDate=" + this.dropoffDate; 
     params += "&pickupHour=" + this.pickupHour; 
     params += "&dropoffHour=" + this.dropoffHour;      
     if (this.supplier!= null) params += "&supplier=" + this.supplier; 
     if (this.contract!= null) params += "&contract=" + this.contract; 
     if (this.CLP!= null) params += "&CLP=" + this.CLP; 
     //alert(params);
     submit(form,this.urlPost,action,params);
   }
 }	
 
 CarSearch.prototype.loadContracts = function(idContractIni) {
	  var suppliers = document.getElementById(this.supplierSelectId);
	  var contracts = document.getElementById(this.contractSelectId);
	  var xmlDocContracts = new XMLRemoteRequest("xmlDocContracts");
	  if (suppliers && suppliers.options[suppliers.selectedIndex].value != "") {
	      xmlDocContracts.getRemoteDocument("/rCars/include/ctlCarSearch/contracts.ashx?prov=" + suppliers.options[suppliers.selectedIndex].value + "&y=" + Math.random());
          var list = xmlDocContracts.selectNodes("contratos/contrato")
          contracts.options.length = 0;
          contracts.options[0] = new Option(this.msgSelectContract,"")
          if (list.length == 0) {
            contracts.disabled = true;
          }else{
            contracts.disabled = false;
            for (var i = 0; i < list.length; i++) {
              var cod = list[i].getAttribute("codigo");
              var descr = list[i].getAttribute("codigo");
              if (descr == "") descr = cod;
              contracts.options[i+1] = new Option(descr,cod)
            }
      }
      if(contracts.options.length == 2){
        contracts.options.selectedIndex = 1;
      }else{
        for (var i = 0; i < list.length; i++) {
          if(idContractIni && list[i].getAttribute("codigo") == idContractIni) contracts.options.selectedIndex = i+1;
        }
      }
    }else{
      contracts.options.length = 0;
      contracts.options[0] = new Option(this.msgSelectContract,"")
      contracts.disabled = true;
    }
    return false; 
 }
 
	
	
