﻿function getQueryVariable() {
    var str = window.location.href;
    if (str.indexOf("SearchResults") == -1) {
        
        document.cookie = "UserProperties" + "=" + "" + "; expires=Sat, 25 Apr 1920 00:00:00 GMT; path=/";
    }
} 
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);
    document.cookie = "cc" + "=" + 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 = "";
        cookieName = "cc";
        tempCurrency = getCookie(cookieName);
        
    if (tempCurrency) {
        existingCurrencyCode = tempCurrency;
    }   

    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) {
    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));
        }
        
    }
   
    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() {
    var someId = document.getElementById("changecurrency-arrow");
    if (document.getElementById(searchpageid + "ddlCurrencyChange").style.display == 'none') {
        document.getElementById(searchpageid + "ddlCurrencyChange").style.display = 'block';
        someId.setAttribute('className', "darrow") || someId.setAttribute('class', "darrow");
    }
    else {
        document.getElementById(searchpageid + "ddlCurrencyChange").style.display = 'none';
        someId.setAttribute('className', "arrow") || someId.setAttribute('class', "arrow");
    }
}
//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" class="deslinks" href="$1">$1</a>');
    }
}
getQueryVariable();


