﻿function ddownSelectedvalue(hiddenid, drpdid) {
    document.getElementById(hiddenid).value = document.getElementById(drpdid).value;
}
function Checkboxchecking() {
    if (searchpageid) {
        var flag = 0;
        var successflag = 0;
        for (var i = 0; i < document.forms[0].length; i++) {
            if (document.forms[0].elements[i].checked) {
                flag = 1;
                return true;
            }
        }
        if (flag == 0) {
            if (document.getElementById('<%= hdnSelectedProperties.ClientID %>').value != '') {
                var hiddenproperties = document.getElementById('<%= hdnSelectedProperties.ClientID %>').value.split(',');
                for (var j = 0; j < hiddenproperties.length; j++) {
                    if (document.getElementById(hiddenproperties[j]) == null) {
                        successflag = 1;
                        return true;
                    }
                }
                if (successflag == 0) {
                    document.getElementById('<%= diverror.ClientID %>').style.display = 'block';
                    document.getElementById('<%= lblerror.ClientID %>').innerHTML = 'Please select at least one property';
                    document.getElementById('<%= diverror.ClientID %>').style.backgroundColor = '#DDE8EE';
                    window.location.href = "#<%= diverror.ClientID %>";
                }
            }
            else {
                document.getElementById('<%= diverror.ClientID %>').style.display = 'block';
                document.getElementById('<%= lblerror.ClientID %>').innerHTML = 'Please select at least one property';
                document.getElementById('<%= diverror.ClientID %>').style.backgroundColor = '#DDE8EE';
                window.location.href = "#<%= diverror.ClientID %>";
            }
            return false;
        }
    }
}
//Script for handling the prices change - Start
function currencyChanged() {
    var mylist = document.getElementById(searchpageid + "ddlCurrencyChange");
    var currencyCode = mylist.options[mylist.selectedIndex].text;
    var ratio = mylist.options[mylist.selectedIndex].value;
    var changedCode = changeCode(currencyCode);
    if (viewingSite == "uk") {
        document.cookie = "ukUserCurrencyChoice" + "=" + changedCode + "; expires=Sat, 25 Apr 2020 00:00:00 GMT; path=/";
    }
    else if (viewingSite == "uae") {
        document.cookie = "uaeUserCurrencyChoice" + "=" + changedCode + "; expires=Sat, 25 Apr 2020 00:00:00 GMT; path=/";
    }
    else if (viewingSite == "global") {
        document.cookie = "globalUserCurrencyChoice" + "=" + changedCode + "; expires=Sat, 25 Apr 2020 00:00:00 GMT; path=/";
    }
    var noofRec = document.getElementById(searchpageid + "ddlPropsperpage").value;
    for (i = 2; i < noofRec + 3; i++) {
        if (i < 10) {
            strPriceControl = searchpageid + "dgSearchResults_ctl0" + i + "_Label2";
            strPriceinGBP = searchpageid + "dgSearchResults_ctl0" + i + "_hdnPriceinGBP";
            strCurr = searchpageid + "dgSearchResults_ctl0" + i + "_txtCurrency";
            strPPre = searchpageid + "dgSearchResults_ctl0" + i + "_lblPriceModifier";
        }
        else {
            strPriceControl = searchpageid + "dgSearchResults_ctl" + i + "_Label2";
            strPriceinGBP = searchpageid + "dgSearchResults_ctl" + i + "_hdnPriceinGBP";
            strCurr = searchpageid + "dgSearchResults_ctl" + i + "_txtCurrency";
            strPPre = searchpageid + "dgSearchResults_ctl" + i + "_lblPriceModifier";
        }
        if (document.getElementById(strPPre)) {

            if (document.getElementById(strPPre).innerHTML != 'Price On Application') {
                document.getElementById(strPriceControl).innerHTML = getPrice(changedCode, document.getElementById(strPriceinGBP).value);
                document.getElementById(strCurr).innerHTML = changedCode;
            }
            else {
                document.getElementById(strPriceControl).innerHTML = '';
                document.getElementById(strCurr).innerHTML = '';
            }
        }
    }
    mylist.blur();
    mylist.style.display = 'none';
}
function changeCode(currencyCode) {
    var test2 = currencyCode.substring(0, currencyCode.lastIndexOf(')'));
    var test3 = test2.substring(currencyCode.lastIndexOf('(') + 1, currencyCode.length);
    return test3;
}
function getPrice(currencyCode, priceInGBP) {
    var faceValue;
    var mylist = document.getElementById(searchpageid + "ddlCurrencyChange");
    for (var i = 0; i < mylist.options.length; i++) {
        var text = mylist.options[i].text;

        if (text.indexOf('(' + currencyCode + ')') != -1) {
            faceValue = mylist.options[i].value;
        }
    }
    var newPrice = priceInGBP * faceValue;
    return ChangeNumber(newPrice);
}
function changeDropDownSelection() {
    var mylist = document.getElementById(searchpageid + "ddlCurrencyChange");
    var existingCurrencyCode = "";
    var cookieName = "";
    var tempCurrency = "";
    if (viewingSite == "uk") {
        cookieName = "ukUserCurrencyChoice";
        tempCurrency = getCookie(cookieName, "uk");
    }
    else if (viewingSite == "uae") {
        cookieName = "uaeUserCurrencyChoice";
        tempCurrency = getCookie(cookieName, "uae");
    }
    else if (viewingSite == "global") {
        cookieName = "globalUserCurrencyChoice";
        tempCurrency = getCookie(cookieName, "global");
    }
    if (tempCurrency) {
        existingCurrencyCode = tempCurrency;
    }
    else {
        existingCurrencyCode = "GBP";
    }

    for (var i = 0; i < mylist.options.length; i++) {
        var text = mylist.options[i].text;
        if (text.indexOf('(' + existingCurrencyCode + ')') != -1) {
            document.getElementById(searchpageid + "ddlCurrencyChange").selectedIndex = i;
        }
    }
}
function getCookie(c_name, country) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
        else {
            if (country == "uk") {
                return "GBP";
            }
            else if (country == "uae") {
                return "AED";
            }
            else if (country == "global") {
                return "GBP";
            }
        }
    }
    else {
        if (country == "uk") {
            return "GBP";
        }
        else if (country == "uae") {
            return "AED";
        }
        else if (country == "global") {
            return "GBP";
        }
    }
    return null;
}
function ChangeNumber(Num) {    
    Num = Math.round(Num) + "";
    dec = Num.indexOf(".");
    end = ((dec > -1) ? "" + Num.substring(dec, Num.length) : ".00");
    Num = "" + parseInt(Num);
    var temp1 = "";
    var temp2 = "";

    if (end.length == 2)
        end += "0";
    if (end.length == 1)
        end += "00";
    if (end == "")
        end += ".00";
    var count = 0;
    for (var k = Num.length - 1; k >= 0; k--) {
        var oneChar = Num.charAt(k);
        if (count == 3) {
            temp1 += ",";
            temp1 += oneChar;
            count = 1;
            continue;
        }
        else {
            temp1 += oneChar;
            count++;
        }
    }
    for (var k = temp1.length - 1; k >= 0; k--) {
        var oneChar = temp1.charAt(k);
        temp2 += oneChar;
    }
    return temp2;
}
function CurrencyChangeClicked() {
    if (document.getElementById(searchpageid + "ddlCurrencyChange").style.display == 'none') {
        document.getElementById(searchpageid + "ddlCurrencyChange").style.display = 'block';
    }
    else {
        document.getElementById(searchpageid + "ddlCurrencyChange").style.display = 'none';
    }
}
//Script for handling the prices change - End
function IWantto(iwantto) {
    if (iwantto == "Select") {
        return false;
    }
    else {
        return true;
    }
}
function Propertylongdescription(elementid) {
    if (document.getElementById(elementid)) {
        var longdescText = document.getElementById(elementid).innerHTML;
        document.getElementById(elementid).innerHTML = longdescText.replace(/(http\:\/\/[\w\.\-\?\!\&\=]+)/g, '<a target="_blank" style="color:#E2760A;font-weight:bold;" href="$1">$1</a>');
    }
}
function Qsselectedvalue(source, eventArgs) {
    UKCurrencyretain();
}
function QsselectedvalueResults(source, eventArgs) {
    var qsresd = document;
    if (viewingSite == "uk") {
        var qsres = ["/en-gb/search-results/?"];
    }
    else if (viewingSite == "uae") {
        var qsres = ["/en-ae/search-results/?"];
    }
    else if (viewingSite == "global") {
        var qsres = ["/en/search-results/?"];
    }
    qsres.push("br=");
    if (qsresd.getElementById(searchpageid + 'rdblIwantTo_0').checked) {
        qsres.push("Buy");
        qsres.push("&sb="); qsres.push(encodeURIComponent("gbpsaleprice:-:1"));
    }
    else {
        qsres.push("Rent");
        qsres.push("&sb="); qsres.push(encodeURIComponent("gbprentpw:-:1"));
    }
    qsres.push("&qt=");
    if (qsresd.getElementById(searchpageid + 'tbxArea').value != "Enter an area here...") {
        qsres.push(encodeURIComponent(qsresd.getElementById(searchpageid + 'tbxArea').value));
    }
    window.location.href = qsres.join("");
    qsresd = null;
    return false;
}
function clearwatermark(defaultText) {
    var box = document.getElementById(searchpageid + 'tbxArea');
    if (box.value == defaultText) {
        box.value = "";
        box = null;
    }
}
function watermark(defaultText) {
    var box = document.getElementById(searchpageid + 'tbxArea');
    var text = document.getElementById(searchpageid + 'tbxArea').value;
    if (text == defaultText || text.length == 0) {
        document.getElementById(searchpageid + 'tbxArea').value = defaultText;
    }
}
