﻿// JScript File
var request = false;
var processing = false;
 
    try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
   }

   if (!request)
     alert("Error initializing XMLHttpRequest!");
function CreateXmlHttpRequest(){
 try {
     myrequest = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       myrequest = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         myrequest = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         myrequest = false;
       }  
     }
   }

   if (!myrequest )
     alert("Error initializing XMLHttpRequest!");
     
   request = myrequest ;
}

function emptySelect(ref) {
    var select = document.getElementById(ref);
    for (i = 0; i < select.options.length; i++) {
        select.removeChild(select.options[i]);
    }
}
function getData(url, params, callback) {
    if (!processing) {
        processing = true;
        CreateXmlHttpRequest();
        //XMLHttpRequest.prototype.readyState
        request.open("GET", (url + "?" + params), true);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                callback(request.responseText);
                processing = false;
            }
        };
        request.send(null);
    }
    else {
        setTimeout(function() {
            getData(url, params, callback);
        }, 200);
    }
}

function getDistricts(ref, obj) {

    
    var mcityId = obj.value;
    var select = document.getElementById(ref);
    emptySelect(ref);
    
    //document.createElement().childNodes = null;
    try {
        /*$("#" + ref + " option").each(function() {
            $(this).remove();
        });*/

        var opt = "<option value='-1'>Loading ...</option>";
        select.innerHTML = opt;
        //$("#" + ref).append(opt);

        //$.get('/ajaxcalls.aspx', { type: "basic.district.selection", cityId: mcityId }, function(data) {
        getData('/ajaxcalls.aspx', 'type=basic.district.selection&cityId=' + mcityId, function(data) {

            
                document.getElementById(ref).options.selectedIndex = -1;
                emptySelect(ref);

                var districts = data.split("|");
                var opt = "";
                for (i = 0; i < districts.length; i++) {
                    var name = districts[i].split('::')[0];
                    var id = districts[i].split('::')[1];
                    opt += "<option value='" + id + "'>" + name + "</option>";
                }
                select.innerHTML = opt;

                document.getElementById(ref).options.selectedIndex = 0;

                if (ref.indexOf("residential") > -1)
                    getCommunities("residentialCommunity", document.getElementById(ref));
                else
                    getCommunities("commercialCommunity", document.getElementById(ref));

        });
    } catch (Error) {alert(Error);}
}
function getCommunities(ref, obj)
{

    try {
        var mdistrictId = obj.value;
        var select = document.getElementById(ref);

        emptySelect(ref);

        var opt = "<option value='-1'>Loading ...</option>";
        select.innerHTML = opt;

       // $.get('/ajaxcalls.aspx', { type: "basic.community.selection", districtId: mdistrictId }, function(data) {
        getData('/ajaxcalls.aspx', 'type=basic.community.selection&districtId=' + mdistrictId, function(data) {
            emptySelect(ref);

            var communities = data.split("|");
            var opt = "";
            for (i = 0; i < communities.length; i++) {
                var name = communities[i].split('::')[0];
                var id = communities[i].split('::')[1];
                opt += "<option value='" + id + "'>" + name + "</option>";

            }
            select.innerHTML = opt;


        });
    }
    catch (Error) { }
}
function getPrices(mcountryId, obj, mcategoryId, ref1, ref2) {

    try {
        if (obj != null && obj.checked) {
            var mserviceTypeId = obj.value;

            var select1 = document.getElementById(ref1);
            var select2 = document.getElementById(ref2);

            emptySelect(ref1);
            emptySelect(ref2);
            
            var opt = "<option value='-1'>Loading ...</option>";
            select1.innerHTML = opt;

            opt = "<option value='-1'>Loading ...</option>";
            select2.innerHTML = opt;


            //$.get('/ajaxcalls.aspx', { type: "sliders.price", countryId: mcountryId, serviceTypeId: mserviceTypeId, categoryId: mcategoryId }, function(data) {
            var params = 'type=sliders.price&countryId=' + mcountryId + '&serviceTypeId=' + mserviceTypeId + '&categoryId=' + mcategoryId;
            getData('/ajaxcalls.aspx', params, function(data) {

                emptySelect(ref1);
                emptySelect(ref2);

                opt = "<option value=''>Price From (AED)</option>";
                select1.innerHTML = opt;

                opt = "<option value=''>Price To (AED)</option>";
                select2.innerHTML = opt;

                var prices = data.split("|");
                var opt = "";
                for (i = 0; i < prices.length; i++) {

                    var label = prices[i].split('::')[0];
                    var val = prices[i].split('::')[1];

                    opt += "<option value='" + val + "'>" + label + "</option>";
                }

                select1.innerHTML += opt;
                select2.innerHTML += opt;

            });
        }
    }
    catch (Error) { }
    
}
