﻿var allitems;
var outCountry;
var outCity;
var outShop;
var inCity;
var inShop;
var Car;
function GetAreaData(Lang, ValOutCountry, ValOutCity, ValOutShop, ValInCity, ValInShop, ValCar) {
    outCountry = ValOutCountry;
    outCity = ValOutCity;
    outShop = ValOutShop;
    inCity = ValInCity;
    inShop = ValInShop;
    Car = ValCar;
    document.getElementById("CarGroup").value = ValCar;
    PageMethods.LoadAllAreaData(Lang, CallSuccess, CallFailed);
}
function CallSuccess(res) {
    loadAreaData(res);
    loadCountries();
    if (outCountry && outCountry > 0) {
        document.getElementById("outCountry").value = outCountry;
    }
    filterInOutCities(document.getElementById("outCountry").value);
    if (outCity) {
        document.getElementById("outCity").value = outCity;
    }
    filterOutShops(document.getElementById("outCity").value);
    if (outShop) {
        document.getElementById("outShop").value = outShop;
    }
    if (inCity) {
        document.getElementById("inCity").value = inCity;
    }
    filterInShops(document.getElementById("inCity").value);
    if (inShop) {
        document.getElementById("inShop").value = inShop;
    }
    if (outShop != inShop) {
        document.getElementById("difLocation").checked = true;
    }
    difLocationChange(document.getElementById("difLocation").checked);
}

function CallFailed(res) {
    alert(res.get_message());
}

function loadAreaData(data) {
    if (/^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, '').replace(/name|id|countr(y|ies)|cit(y|ies)|shop(s?)/g, "]"))) {
        allitems = eval("(" + data + ")");
    }
}
function filterCities(country) {
    var cityArray = new Array();
    for (var i = 0; i < allitems.cities.length; i++) {
        if (allitems.cities[i].country == country) {
            cityArray[cityArray.length] = allitems.cities[i];
        }
    }
    return cityArray;
}
function filterShops(city) {
    var shopArray = new Array();
    for (var i = 0; i < allitems.shops.length; i++) {
        if (allitems.shops[i].city == city) {
            shopArray[shopArray.length] = allitems.shops[i];
        }
    }
    return shopArray;
}
function filterInOutCities(country) {
    document.getElementById("inCountry").value = country;
    var basicOpt = document.getElementById("outCity").options[0];
    var basicOpt2 = document.getElementById("inCity").options[0];
    document.getElementById("outCity").options.length = 0;
    document.getElementById("outCity").options[0] = basicOpt;
    document.getElementById("outCity").options[0].selected = true;
    document.getElementById("inCity").options.length = 0;
    document.getElementById("inCity").options[0] = basicOpt2;
    document.getElementById("inCity").options[0].selected = true;
    var city = filterCities(country);
    for (var i = 0; i < city.length; i++) {
    	document.getElementById("outCity").options[document.getElementById("outCity").options.length] = new Option(escapeHTML(city[i].name), city[i].id, false, false)
        document.getElementById("inCity").options[document.getElementById("inCity").options.length] = new Option(escapeHTML(city[i].name), city[i].id, false, false)
    }
    filterOutShops(document.getElementById("outCity").value);
    filterInShops(document.getElementById("inCity").value);
    difLocationChange(document.getElementById("difLocation").checked);
    if (!Car || Car=="") {
        resetCarGroup();
    } else {
        Car = "";
    }
}
function filterOutShops(city) {
    var basicOpt = document.getElementById("outShop").options[0];
    document.getElementById("outShop").options.length = 0;
    document.getElementById("outShop").options[0] = basicOpt;
    document.getElementById("outShop").options[0].selected = true;
    var shop = filterShops(city);
    for (var i = 0; i < shop.length; i++) {
        document.getElementById("outShop").options[document.getElementById("outShop").options.length] = new Option(escapeHTML(shop[i].name), shop[i].id, false, false)
    }
    if (!document.getElementById("difLocation").checked) {
        document.getElementById("inCity").value = city;
        filterInShops(city);
    }
}
function filterInShops(city) {
    var basicOpt = document.getElementById("inShop").options[0];
    document.getElementById("inShop").options.length = 0;
    document.getElementById("inShop").options[0] = basicOpt;
    document.getElementById("inShop").options[0].selected = true;
    var shop = filterShops(city);
    for (var i = 0; i < shop.length; i++) {
        document.getElementById("inShop").options[document.getElementById("inShop").options.length] = new Option(escapeHTML(shop[i].name), shop[i].id, false, false)
    }
}

function outShopSelected(shop) {
    if (!document.getElementById("difLocation").checked) {
        document.getElementById("inShop").value = shop;
    }
}

function difLocationChange(val) {
    document.getElementById("inCity").style.display = val ? "inline" : "none";
    document.getElementById("inShop").style.display = val ? "inline" : "none";
    if (!val) {
        document.getElementById("inCity").value = document.getElementById("outCity").value;
        filterInShops(document.getElementById("inCity").value);
        document.getElementById("inShop").value = document.getElementById("outShop").value;
    }
}

function loadCountries() {
    document.getElementById("outCountry").options.length = 0;
    document.getElementById("inCountry").options.length = 0;
    for (var i = 0; i < allitems.countries.length; i++) {
        document.getElementById("outCountry").options[document.getElementById("outCountry").options.length] = new Option(escapeHTML(allitems.countries[i].name), allitems.countries[i].id, i == 0, i == 0)
        document.getElementById("inCountry").options[document.getElementById("inCountry").options.length] = new Option(escapeHTML(allitems.countries[i].name), allitems.countries[i].id, i == 0, i == 0)
    }
}

function resetCarGroup() {
    document.getElementById("CarGroup").value = "";
    document.getElementById("CarGroupName").innerHTML = "";
}

function SetDayDuration(days) {
    if (days && parseInt(days) > 0) {
        var pattern=/^((((0?[123456789])|([12][0-9])|(3[01]))\/((0?[13578])|(1[02])))|(((0?[123456789])|([12][0-9])|(30))\/((0?[469])|11))|(((0?[123456789])|([12][0-9]))\/0?2))\/20[0-9]{2}$/i;
        if (pattern.test(document.getElementById(theOutDate).value)) {
            var year=parseInt(document.getElementById(theOutDate).value.split("/")[2]);
            var month=parseInt(document.getElementById(theOutDate).value.split("/")[1])-1;
            var day=parseInt(document.getElementById(theOutDate).value.split("/")[0]);
            var hour=parseInt(document.getElementById(theOutHour).value)/100;
            var minute=parseInt(document.getElementById(theOutMinute).value);
            var DropOffDate = new Date(year, month, day, hour, minute);
            DropOffDate.setDate(DropOffDate.getDate() + parseInt(days));
            document.getElementById(theInDate).value = DropOffDate.getDate() + "/" + (DropOffDate.getMonth()+1) + "/" + DropOffDate.getFullYear()
            document.getElementById(theInHour).value = document.getElementById(theOutHour).value;
            document.getElementById(theInMinute).value = document.getElementById(theOutMinute).value;
        }
    }
}

function escapeHTML(str) 
{
	return decodeURIComponent(str.replace(/\+/g, " "));
}; 
