﻿ //JScript File
        var isOpera = navigator.userAgent.indexOf("Opera") > -1;
        var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
        var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;
        var num_times_timer = 0;
        var p;
        var pickupID=0;
        var dropoffID=0;
        //var ws_url = location.protocol + "//" + location.hostname + ":8080/carhire/";
        var ws_url = "http://test.b.travelsearchresults.com/carhire/";
        var currentID = ""
        
        load()
        
        function load()
        {
            var obj = document.getElementById('hiddenISO');
            if(obj!=null)
            {
                var vars = obj.value.split('|');
                updateLocations(vars[0],vars[1]);
            }
        }
        
        function updateLocations(idVal,id)
        {
            currentID = id;
            initDDLs('Loading...',id);
			num_times_timer = 0;
			p = idVal;
			window.setTimeout(startResponse, 1000, true);
        } 

        function ajaxLoader(url,bannerID)
        {
            if (document.getElementById)
            {
                var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
            }
	        if (x) 
	        {
		        x.onreadystatechange = function()
		        {
		        	if(x.readyState == 4 && x.status == 200)
		        	{
			            if(isMoz){moz_f_show_data(x,url,bannerID);return;}
			            if(isIE){ie_f_show_data(x,url,bannerID);return;}
				    }
		        }
		        x.open("GET", url, true);
		        x.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
		        x.send(null);
            }
        }

        function startResponse()
        {
	        num_times_timer +=1;
	        document.getElementById(currentID + "span_ctr").innerHTML = num_times_timer;
	        var url = ws_url + "xml_processor.aspx?p=" + p + "&num=" + num_times_timer;
    		ajaxLoader(url,currentID);
        }

    function moz_f_show_data(x,url,id)
    {
        if(x.responseText.length > 0 && x.responseText != "ERR1")
        {
            var res = x.responseText;
            _callback1(res,id);
        }
        else
        {
            window.setTimeout(function()
            {
                num_times_timer +=1;
                document.all.span_ctr.innerHTML = num_times_timer;
                if (num_times_timer == 30 ||num_times_timer == 45 ){//alert("Sorry to have to keep you waiting, please wait a while more .... \n Thank you.")
                }
                if (num_times_timer ==350){window.location.reload();}                
                url = url.replace("num=" + (num_times_timer - 1), "num=" + num_times_timer)
                ajaxLoader(url,id);
                }, 1000, true);return;
            }
    }

    function ie_f_show_data(x,url,id)
    {
        //prompt(url,url);
        //alert(x.responseText);
        //8888888888888888888888888888
        if(x.responseText.length > 0 && x.responseText != "ERR1")
        {
            var res = x.responseText;
            _callback1(res,id);
        }
        else
        {
            window.setTimeout(function()
            {
                num_times_timer +=1;
                document.all.span_ctr.innerHTML = num_times_timer;
                if (num_times_timer == 30 ||num_times_timer == 45 ){//alert("Sorry to have to keep you waiting, please wait a while more .... \n Thank you.")
                }
                if (num_times_timer ==350){window.location.reload();}                
                url = url.replace("num=" + (num_times_timer - 1), "num=" + num_times_timer)
                ajaxLoader(url,id);
                }, 1000, true);return;
            }
        }			
              
        function _callback1(res,id) 
        {
            document.getElementById(id + "pickupDDLDiv").innerHTML= "<select id='" + id + "pickupDDL' name='" + id + "pickupDDL' onchange='storeVal1(this.value,\"" + id + "\")' class='cell' style='width:145px;'>" + res + "</select>";
            document.getElementById(id + "toLocDDLDiv").innerHTML="<select id='" + id + "toLocDDL' name='" + id + "toLocDDL' onchange='storeVal2(this.value,\"" + id + "\")' class='cell' style='width:145px;' >" + res + "</select>";
        }
        
        function initDDLs(str,id)
        {
            document.getElementById(id + "pickupDDLHolder").style.display = "none";
            document.getElementById(id + "toLocDDLHolder").style.display = "none";
            document.getElementById(id + "pickupDDLDiv").innerHTML= "<select id='" + id + "pickupDDL' name='" + id + "pickupDDL' class='cell' style='width:145px;'><option value='#'>" + str + "</option></select>";
            document.getElementById(id + "toLocDDLDiv").innerHTML="<select id='" + id + "toLocDDL' name='" + id + "toLocDDL' class='cell' style='width:145px;'><option value'#'>" + str + "</option></select>";
        }
        
        function show_pickup(bannerID){
            cal_ipt_id = bannerID + "txt_calendar";
            var parent_obj = document.getElementById("div_" + cal_ipt_id);
            var obj = document.getElementById(bannerID + 'dimmer');
            parent_obj.appendChild(obj);

            carhire_show_dimmer(bannerID);
        }
        
        function carhire_show_return(bannerID){
            //alert(bannerID);
            cal_ipt_id = bannerID + "txt_calendarRet";
            var parent_obj = document.getElementById("div_" + cal_ipt_id);
            var obj = document.getElementById(bannerID + 'dimmer');
            parent_obj.appendChild(obj);
            carhire_show_dimmer(bannerID);
        }
        
        var cal_ipt_id = "";

        function carhire_carhire_show_dimmer(bannerID){
            carhire_fill_monthyear_list(bannerID + 'sel_month_year');
            carhire_fill_day_list(false,bannerID + 'sel_month_year', bannerID + 'calendar_content',0,cal_ipt_id);
            var obj = document.getElementById(bannerID + "dimmer");
            if (obj.style.display == "none"){
                obj.style.display = "inline";
            }else{
                obj.style.display = "none";
            }
        }
        
        function submitFunc(bannerID,a,r,c,t)
        {
            var today = new Date();
            var fullPickupDate = document.getElementById(bannerID + 'txt_calendar').value.split('-');
            var selectedPickupDay = addLeadingZero(fullPickupDate[0]);
            var selectedPickupMonth = addLeadingZero(getMonthAsInt(fullPickupDate[1].toString()));
            var selectedPickupYear = fullPickupDate[2];
            var selectedPickupHour = document.getElementById(bannerID + 'fromHourDDL').value;
            var selectedPickupMins = document.getElementById(bannerID + 'fromMinsDDL').value;
            var fullDropoffDate = document.getElementById(bannerID + 'txt_calendarRet').value.split('-');
            var selectedDropoffDay = addLeadingZero(fullDropoffDate[0]);
            var selectedDropoffMonth = addLeadingZero(getMonthAsInt(fullDropoffDate[1]));
            var selectedDropoffYear = fullDropoffDate[2];
            var selectedDropoffHour = document.getElementById(bannerID + 'toHoursDDL').value;
            var selectedDropoffMins = document.getElementById(bannerID + 'toMinsDDL').value;
            
            var carGroupObj = document.getElementById(bannerID + 'carGroupDDL');
            var carGroup;
            if(carGroupObj != null){carGroup = carGroupObj.value;} else {carGroup = 0;}
            var resCountryObj = document.getElementById(bannerID + 'countryOfRes');
            var resCountry;
            if(resCountryObj != null){resCountry = resCountryObj.value;} else {resCountry= "UK";}
                        
            //if(document.getElementById(bannerID + 'countryDDL').value=='#'){alert('Please select a country.');}
            //else ->
            if(document.getElementById('hiddenISO') != null){var countryDDLISO = document.getElementById('hiddenISO').value.split('|')[0];}
            else
            {
                if(document.getElementById(bannerID + 'countryDDL').value=='#'){alert('Please select a country.');}
                else{var countryDDLISO = document.getElementById(bannerID + 'countryDDL').value;}
            }
            if(pickupID==0){alert('Please select a pickup location.');}
            else if(dropoffID==0){alert('Please select a drop off location.');}
            
            //else if(document.getElementById('carGroupDDL').value=='#'){alert('Please select a cargroup.');}
            else if(compareDates(selectedPickupDay,selectedPickupMonth,selectedPickupYear,selectedPickupHour,selectedPickupMins,today.getDate(),today.getMonth()+1,today.getFullYear(),today.getHours(),today.getMinutes()) <=0){alert('Pickup date is before todays date.');}
            else if(compareDates(selectedPickupDay,selectedPickupMonth,selectedPickupYear,selectedPickupHour,selectedPickupMins,selectedDropoffDay,selectedDropoffMonth,selectedDropoffYear,selectedDropoffHour,selectedDropoffMins ) > -1){alert('Dropoff date is before pickup date.');}
            else
            {
                window.open("http://v2.travelsearchresults.com/carhire/results.aspx?CountryID=" + countryDDLISO + "&PickupLoc=" + pickupID + "&PickupDay=" + selectedPickupDay + "&PickupMonth=" + selectedPickupMonth + "&PickupYear="+ selectedPickupYear+ "&PickupHours=" + selectedPickupHour + "&PickupMins=" + selectedPickupMins + "&DropLoc=" + dropoffID + "&DropDay=" + selectedDropoffDay + "&DropMonth=" + selectedDropoffMonth + "&DropYear=" + selectedDropoffYear + "&DropHours=" + selectedDropoffHour + "&DropMins=" + selectedDropoffMins + "&PromoCode=&Currency=GBP&CarGroup=" + carGroup + "&ResCountry=" + resCountry + "&em=" + "&a=" + a + "&r=" + r + "&t=" + t + "&c=" + c);
                //window.location.href="http://localhost:1571/tsr_carhire/results.aspx?CountryID=" + countryDDLISO + "&PickupLoc=" + pickupID + "&PickupDay=" + selectedPickupDay + "&PickupMonth=" + selectedPickupMonth + "&PickupYear="+ selectedPickupYear+ "&PickupHours=" + selectedPickupHour + "&PickupMins=" + selectedPickupMins + "&DropLoc=" + dropoffID + "&DropDay=" + selectedDropoffDay + "&DropMonth=" + selectedDropoffMonth + "&DropYear=" + selectedDropoffYear + "&DropHours=" + selectedDropoffHour + "&DropMins=" + selectedDropoffMins + "&PromoCode=&Currency=GBP&CarGroup=" + carGroup + "&ResCountry=" + resCountry + "&em=" + "&a=" + a + "&r=" + r + "&t=" + t + "&c=" + c
            }
        }
        
        function addLeadingZero(val)
        {
            var value = val.toString();
            if(value.length < 2){ return "0" + value;}
            return value
        }
        
        function compareDates(day1,month1,year1,hour1,min1,day2,month2,year2,hour2,min2)
        {
            if(year1 > year2) return 1;
            else if(year1 < year2)return -1;
            else if(month1 > month2)return 1;
            else if(month1 < month2)return -1;
            else if(day1 > day2)return 1;
            else if(day1 < day2)return -1;
            else if(hour1 > hour2)return 1;
            else if(hour1 < hour2)return -1;
            else if(min1 > min2)return 1;
            else if(min1 < min2)return -1;
            else return 0;
        }
        
        function storeVal1(val,id)
        {
            pickupID=val;
            dropoffID=val;
            document.getElementById(id + "pickupDDL").value=val;
            document.getElementById(id + "toLocDDL").value=val;
        }
        
        function storeVal2(val,id)
        {
            dropoffID=val;
            document.getElementById(id + "toLocDDL").value=val;
        }
        
        function QueryString(key)
        {
            var value = null;
            for (var i=0;i<QueryString.keys.length;i++)
            {
	            if (QueryString.keys[i]==key)
	            {
		            value = QueryString.values[i];
		            break;
		        }
            }
            return value;
        }
    
        function QueryString_Parse()
        {
            var query = window.location.search.substring(1);
            var pairs = query.split("&");
 
            for (var i=0;i<pairs.length;i++)
            {
	            var pos = pairs[i].indexOf('=');
	            if (pos >= 0)
	            {
		            var argname = pairs[i].substring(0,pos);
		            var value = pairs[i].substring(pos+1);
		            QueryString.keys[QueryString.keys.length] = argname;
		            QueryString.values[QueryString.values.length] = value;
	            }
            } 
        }

        QueryString.keys = new Array();
        QueryString.values = new Array();  
        QueryString_Parse();
        
        function getMonthAsInt(month)
        {
            for(var i=1; i < 13; i++)
            {
                if(month==month_name[i-1]){return i;}
            }
            return -1;
        }

        function getMonthAsStr(month)
        {
            return month_name[month-1];
        }

