
function sendRequestViaProxy(url, callback, postData) {
    var bUseProxy = true;
    if (bUseProxy) {
        postData = (postData) ? postData + "&srvUri=" + url : null;
        sendRequest("XMLHttpRequestProxyHandler.php", callback, postData);
    } else {
        sendRequest(url, callback, postData);
    }
}
function GetCountries() {//debugger;
    var controlId = "Country";
    var ret;
    ret = sendRequestViaProxy("http://www.ipix.ro/Countries.ashx", GetAsyncCountries, "type=country");
    //        if (ret != "OK") {
    //            var element = document.getElementById(controlId);
    //            element.options.length = 0;
    //			var list = new Array();
    //			list = ret.split("|");
    //			for (var i = 0; i < list.length - 1; i++) {
    //			    element.options[i] = new Option(list[i].split("::")[1], list[i].split("::")[0], true, false);
    //			}
    //		}
}

function GetAsyncCountries(result) {
    var controlId = "Country";
    var ret = result.responseText;
    if (ret != "OK") {
        var element = document.getElementById(controlId);
        element.options.length = 1;
        var list = new Array();
        list = ret.split("|");
        for (var i = 0; i < list.length - 1; i++) {
			if(list[i].split("::")[0] == querySt('Country'))
				element.options[i + 1] = new Option(list[i].split("::")[1], list[i].split("::")[0], false, true);
			else
				element.options[i + 1] = new Option(list[i].split("::")[1], list[i].split("::")[0], false, false);
        }
    }
}

function GetRegions() {//debugger;
	var countryId = document.getElementById("Country").value;
    var controlId = "Region";
    var ret;
    ret = sendRequestViaProxy("http://www.ipix.ro/Countries.ashx", GetAsyncRegions, "type=region&countryId=" + countryId);

}

function GetAsyncRegions(result) {
    var controlId = "Region";
    var ret = result.responseText;
    if (ret != "OK") {
        var element = document.getElementById(controlId);
        element.options.length = 1;
        var list = new Array();
        list = ret.split("|");
        for (var i = 0; i < list.length - 1; i++) {
            if(list[i].split("::")[0] == querySt('Region').replace(',',''))
				element.options[i + 1] = new Option(list[i].split("::")[1], list[i].split("::")[0], false, true);
			else
				element.options[i + 1] = new Option(list[i].split("::")[1], list[i].split("::")[0], false, false);
        }
    }
}

var list;

function GetCities() {//debugger;
    var regionId = document.getElementById("Region").value;
    var countryId = document.getElementById("Country").value;
    var controlId = "City";
    var ret;
	ret = sendRequestViaProxy("http://www.ipix.ro/Countries.ashx", GetAsyncCities, "type=city&regionId=" + regionId + "&countryId=" + countryId);
    //    if (ret != "OK") {
    //        var element = document.getElementById(controlId);
    //        element.options.length = 0;
    //        list = new Array();
    //        list = ret.split("|");
    //        var count = 1000;
    //        if (list.length < 1001)
    //            count = list.length - 1;
    //        for (var i = 0; i < count; i++) {
    //            element.options[i] = new Option(list[i].split("::")[1], list[i].split("::")[0], false, false);
    //        }
    //    }
	//document.getElementById('hdnRegionId').value = regionId;
}

function GetAsyncCities(result) {
    var controlId = "City";
    var ret = result.responseText;
    if (ret != "OK") {
        var element = document.getElementById(controlId);
        element.options.length = 1;
        list = new Array();
        list = ret.split("|");
        var count = 2000;
        if (list.length < 2001)
            count = list.length - 1;
        for (var i = 0; i < count; i++) {
			if(list[i].split("::")[0] == querySt('City').replace(',',''))
				element.options[i + 1] = new Option(list[i].split("::")[1], list[i].split("::")[0], false, true);
			else
				element.options[i + 1] = new Option(list[i].split("::")[1], list[i].split("::")[0], false, false);
        }
    }
}

function PopulateAll() {
    initWaterMarlk();

    if (document.getElementById("Country").style.display != 'none') {
       GetCountries();
    }
    GetRegions();
    GetCities();
	var a = document.getElementsByName("facility");
	a[0].value="Internet";
	var facilityStr = querySt('facility');
	for(var i=0; i<a.length;i++)
	{
		if(facilityStr.indexOf(a[i].value)>-1)
		{
			a[i].checked = true;
		}
	}
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	var result='';
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			result += ft[1]+',';
		}
	}
	return result;
}


function initWaterMarlk() {
    var locationCtrl = document.getElementById('Location');
    if (locationCtrl != null && locationCtrl.value == '') {
        setWatermark(locationCtrl, 'LocationWatermark');
    }
    var codeCtrl = document.getElementById('Code');
    if (codeCtrl != null && codeCtrl.value == '') {
        setWatermark(codeCtrl, 'CodeWatermark');
    }
    var fCtrl = document.getElementById('FacilityTooltip');
    if (fCtrl != null && fCtrl.options.length > 0) {
        var fWmrk = document.getElementById('FacilityWatermark');
        if (fWmrk != null) {
            fCtrl.options[0].text = fWmrk.value;
        }
    }
}

function setWatermark(myInput, wmrkName) {
    if (myInput != null && myInput.value == '') {
        if (wmrkName != null && wmrkName != '') {
            var wmrk = document.getElementById(wmrkName);
            if (wmrk != null) {
                myInput.value = wmrk.value;
                myInput.style.color = '#888';
            }
        }
    }
}

function removeWatermark(myInput, wmrkName) {
    if (myInput != null && wmrkName != null && wmrkName != '') {
        var wmrk = document.getElementById(wmrkName);
        if (wmrk != null && myInput.value == wmrk.value) {
            myInput.value = '';
            myInput.style.color = '#000';
        }
    }
}

