﻿// JScript File

        function body_onload()
            {
            var txtDate = new Date();
            var monthname = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
            txtDate.setDate(txtDate.getDate()+ 2)
            document.getElementById('txtDatePU').value = (txtDate.getDate()) + ' ' + monthname[txtDate.getMonth()] + ' ' + txtDate.getFullYear() ;
            txtDate.setDate(txtDate.getDate()+ 2)
            document.getElementById('txtDateDO').value = (txtDate.getDate()) + ' ' + monthname[txtDate.getMonth()] + ' ' + txtDate.getFullYear() ;
            CountryListLoad('ddlCountryRes');

            var ddlDefaultOptionPU = {
	            "0" : "Select A Pickup Location"
            }
            $('#ddlLocationPU').removeOption('0');
            $('#ddlLocationPU').addOption(ddlDefaultOptionPU, true);
            $('#ddlLocationPU').ajaxAddOption('/diyGetTopLocationListForDDL.aspx', {}, false, function() {
                var ddlAllLocationsOptionPU = {
	                "" : "...........All Locations............"
                }
                $('#ddlLocationPU').addOption(ddlAllLocationsOptionPU, false);
                //$('#txtLocationPU').val($('#ddlLocationPU:selected').text());
                $('#ddlLocationPU').ajaxAddOption('/diyGetLocationListForDDL.aspx', {}, false, function() {
                    $('#txtLocationCodePU').val($('#ddlLocationPU').val());
                });
            });
            $('#ddlLocationPU').change(function() {
                var strLocationTextPU = $('#ddlLocationPU').selectedTexts();
                $('#txtLocationPU').val(strLocationTextPU);
                $('#txtLocationCodePU').val($('#ddlLocationPU').val());
                $('#ddlLocationPU').css('color','#000');
                $('#search-warning').css('visibility', 'hidden');
                $('#search-warning').css('display', 'none');
                $('#search-warning').html('');
            });

            var ddlDefaultOptionDO = {
	            "" : "Select A Return Location"
            }
            $('#ddlLocationDO').removeOption("0");
            $('#ddlLocationDO').addOption(ddlDefaultOptionDO, true);
            $('#ddlLocationDO').ajaxAddOption('/diyGetLocationListForDDL.aspx', {}, false, function() {
                $('#txtLocationCodeDO').val($('#ddlLocationDO').val());
            });
            $('#txtLocationCodeDO').val($('#ddlLocationDO').val());
            $('#ddlLocationDO').change(function() {
                var strLocationTextDO = $('#ddlLocationDO').selectedTexts();
                $('#txtLocationDO').val(strLocationTextDO);
                $('#txtLocationCodeDO').val($('#ddlLocationDO').val());
                $('#ddlLocationDO').css('color','#000');
                $('#search-warning').css('visibility', 'hidden');
                $('#search-warning').css('display', 'none');
                $('#search-warning').html('');
            });

            var strLocationName = new String(document.location);
            var strFileNameIndex = strLocationName.lastIndexOf('/') ;
            if (strLocationName.indexOf('car-hire-') > 0) {
                strLocationName = strLocationName.substr(strFileNameIndex);
                strLocationName = strLocationName.replace(/\/car-hire-/gi,'');
                strLocationName = strLocationName.replace(/---/gi,'|||');
                strLocationName = strLocationName.replace(/-/gi,' ');
                strLocationName = strLocationName.replace(/\|\|\|/gi,' - ');
                strLocationName = strLocationName.replace(/.aspx/gi,'');
                $('#ddlLocationPU').selectOptions(strLocationName, true);
            }
            
            bindDdlTimePU()
            bindDdlTimeDO()
            
            var qs = new Querystring()
            if (qs.contains('LocationPU')) {
                if ( !qs.get('LocationPU') =='' )
                {
                //document.getElementById('txtLocationPU').value = qs.get('LocationPU').replace('+',' ');
                }
            }
            if (qs.contains('SameReturnLoc')) {
                if (qs.get('SameReturnLoc') == '0') {
                    setCheckedValue(document.forms['aspnetForm'].elements['SameReturnLoc'], '0');
                    //document.getElementById('txtLocationDO').style.visibility = 'visible';
                    //document.getElementById('txtLocationDO').style.display = 'block';
                    $('#ddlLocationDO').css('visibility','visible');
                    $('#ddlLocationDO').css('display','block');
                    if (qs.contains('LocationDO')) {
                        if ( !qs.get('LocationDO') =='' )
                        {
                        //document.getElementById('txtLocationDO').value = qs.get('LocationDO').replace('+',' ');
                        }
                    }
                }
            }
            if (qs.contains('LocationCodePU')) {
                document.getElementById('txtLocationCodePU').value = qs.get('LocationCodePU').replace(/-/g,' ');
                $('#ddlLocationPU').attr('value', qs.get('LocationCodePU').replace(/-/g,' '));
            } 
            if (qs.contains('LocationCodeDO')) {
                document.getElementById('txtLocationCodeDO').value = qs.get('LocationCodeDO').replace(/-/g,' ');
                $('#ddlLocationDO').attr('value', qs.get('LocationCodeDO').replace(/-/g,' '));
            }
            if (qs.contains('DatePU')) {
                document.getElementById('txtDatePU').value = qs.get('DatePU').replace(/-/g,' ');
            }
            if (qs.contains('DateDO')) {
                document.getElementById('txtDateDO').value = qs.get('DateDO').replace(/-/g,' ');
            }
            if (qs.contains('TimePU')) {
                document.getElementById('ddlTimePU').value = qs.get('TimePU');
            }
            if (qs.contains('TimeDO')) {
                document.getElementById('ddlTimeDO').value = qs.get('TimeDO');
            }
            if (qs.contains('CountryRes')) {
                document.getElementById('ddlCountryRes').value = qs.get('CountryRes');
            }
            if (qs.contains('DriverAge')) {
                document.getElementById('ddlDriverAge').value = qs.get('DriverAge');
            }
            if (qs.contains('CarClass')) {
                document.getElementById('ddlCarClass').value = qs.get('CarClass');
            }
        }

function getCheckedValue(radioObj) {
	if(!radioObj)
		return '';
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return '';
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return '';
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}    
            
function disableDatesBeforeToday(theDate){
    var txtDate = new Date();
    var monthname = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
    var today = new Date(txtDate.getDate() + ' ' + monthname[txtDate.getMonth()] + ' ' + txtDate.getFullYear()) ;
    if (theDate < today)
        {
        return true;
        }
        else
        {
        return false;
        }
}
            
function showdiv(id){ 
    if (document.getElementById){ 
        obj = document.getElementById(id); 
        obj.style.display = ''; 
        } 
    }

function hidediv(id){ 
    if (document.getElementById){ 
        obj = document.getElementById(id); 
        obj.style.display = 'none';
        } 
    }

function DateIsGreaterThanToday(sender, args)
  {
    var mydate = new Date(args.Value);
    var fulldatetime = new Date();
    var today = new Date(fulldatetime.getFullYear(), fulldatetime.getMonth(), fulldatetime.getDate() - 1);
    if (mydate > today) 
    {
       args.IsValid = true;
    }
    else
    {
       args.IsValid = false;
    }
  }

function RedirectCountry(ddl)
    {
    var index = ddl.selectedIndex;
    location = 'http://www.' + ddl.options[index].value;
    //alert(ddl.options[index].value);
    }

function ValidateSearchForm(btn)
    {
    
        /** TIME & COUNTRY CHECKING **/
        var divwarning = document.getElementById('search-warning');
        var ddlTimePU = document.getElementById('ddlTimePU') ;
        var ddlTimeDO = document.getElementById('ddlTimeDO') ;
        var ddlCountryRes = document.getElementById('ddlCountryRes') ;
        var onError = 0;
        var errorMessage = ''
        
        divwarning.innerHTML = '';
        divwarning.style.visibility = 'hidden';
        divwarning.style.display = 'none';
        
        ddlTimePU.style.color = "#000";
        ddlTimeDO.style.color = "#000";
        ddlCountryRes.style.color = "#000";

        if (ddlTimePU.value == '')
        {
            onError = 1;
            if (ddlTimePU.options.length <= 0) {bindDdlTimePU();}
            errorMessage = 'Please select pickup time';
            ddlTimePU.style.color = "#f00";
        };
        if (ddlTimeDO.value == '')
        {
            onError = 1;
            if (ddlTimeDO.options.length <= 0) {bindDdlTimeDO();}
            if (errorMessage == '')
            { errorMessage = 'Please select return time';}
            else
            { errorMessage = errorMessage + ', return time';}
            ddlTimeDO.style.color = "#f00";
        };
        if (ddlCountryRes.value == '')
        {
            onError = 1;
            if (ddlCountryRes.options.length <= 0) {CountryListLoad('ddlCountryRes');}
            if (errorMessage == '')
            { errorMessage = 'Please select country';}
            else
            { errorMessage = errorMessage + ', country';}
            ddlCountryRes.style.color = "#f00";
        };
        if (onError > 0) { 
            divwarning.style.visibility = 'visible';
            divwarning.style.display = 'block';
            divwarning.innerHTML = errorMessage;
            return false;
        };
        /** TIME & COUNTRY CHECKING **/
        
        var rdoSameReturnLoc = document.forms['aspnetForm'].elements['SameReturnLoc'];
        var strSameReturnLoc = getCheckedValue(rdoSameReturnLoc);
        if ( $('#txtLocationCodePU').val() != '' && $('#txtLocationCodePU').val() != '0' ) //PU is OK
        {
            if ( strSameReturnLoc == '0' ) //PU is OK AND check DO
            {
                if ( !$('#txtLocationCodeDO').val() == '' ) //PU is OK AND DO is OK
                {
                return true;
                }
                else //PU is OK AND DO is not OK
                {
                $('#ddlLocationDO').css('color', "#f00");
                $('#search-warning').css('visibility', 'visible');
                $('#search-warning').css('display', 'block');
                $('#search-warning').html('Please select a valid return location');
                return false;
                }
            }
            else //PU is OK AND do not check DO
            {
                return true;
            }
        }
            else //PU is not OK
        {
            $('#ddlLocationPU').css('color', "#f00");
            $('#search-warning').css('visibility', 'visible');
            $('#search-warning').css('display', 'block');
            $('#search-warning').html('Please select a valid pick up location');
            return false;
        }
    }
    
function SameReturnLocSelected()
    {
    $('#ddlLocationPU').css('color', "#000");
    $('#ddlLocationDO').css('visibility', 'hidden');
    $('#ddlLocationDO').css('display', 'none');
    $('#ddlLocationDO').css('color', "#000");
    $('#ddlLocationDO').selectedOptions('Return location');
    $('#txtLocationCodeDO').val('');
    $('#search-warning').css('visibility', 'hidden');
    $('#search-warning').css('display', 'none');
    $('#search-warning').html('');
    }

function OtherReturnLocSelected()
    {
    $('#ddlLocationPU').css('color', "#000");
    $('#ddlLocationDO').css('visibility', 'visible');
    $('#ddlLocationDO').css('display', 'block');
    $('#ddlLocationDO').css('color', "#000");
    $('#ddlLocationDO').selectedOptions('Return location');
    $('#txtLocationCodeDO').val('');
    $('#search-warning').css('visibility', 'hidden');
    $('#search-warning').css('display', 'none');
    $('#search-warning').html('');
    }

 var timeList = {
    '00:00' : '00:00', '00:30' : '00:30',
    '01:00' : '01:00', '01:30' : '01:30',
    '02:00' : '02:00', '02:30' : '02:30',
    '03:00' : '03:00', '03:30' : '03:30',
    '04:00' : '04:00', '04:30' : '04:30',
    '05:00' : '05:00', '05:30' : '05:30',
    '06:00' : '06:00', '06:30' : '06:30',
    '07:00' : '07:00', '07:30' : '07:30',
    '08:00' : '08:00', '08:30' : '08:30',
    '09:00' : '09:00', '09:30' : '09:30',
    '10:00' : '10:00', '10:30' : '10:30',
    '11:00' : '11:00', '11:30' : '11:30',
    '12:00' : '12:00', '12:30' : '12:30',
    '13:00' : '13:00', '13:30' : '13:30',
    '14:00' : '14:00', '14:30' : '14:30',
    '15:00' : '15:00', '15:30' : '15:30',
    '16:00' : '16:00', '16:30' : '16:30',
    '17:00' : '17:00', '17:30' : '17:30',
    '18:00' : '18:00', '18:30' : '18:30',
    '19:00' : '19:00', '19:30' : '19:30',
    '20:00' : '20:00', '20:30' : '20:30',
    '21:00' : '21:00', '21:30' : '21:30',
    '22:00' : '22:00', '22:30' : '22:30',
    '23:00' : '23:00', '23:30' : '23:30'
}
            
function bindDdlTimePU(){
    $('#ddlTimePU').addOption(timeList, false);
    $('#ddlTimePU').attr('value','09:00');
}

function bindDdlTimeDO(){
    $('#ddlTimeDO').addOption(timeList, false);
    $('#ddlTimeDO').attr('value','09:00');
}
