﻿var webUrl = "http://www.mobilephonedealsearch.co.uk/";

//alert(webUrl);
function GetAJAX() {
    var xmlHttpObj;
    try {
        xmlHttpObj = new XMLHttpRequest();
        return xmlHttpObj;
    }
    catch (e) {
        try {
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
            return xmlHttpObj;
        }
        catch (e) {
            try {
                xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
                return xmlHttpObj;
            }
            catch (e) {
                alert("Sorry, Your browser does not support AJAX!");
                return null;
            }
        }
    }
}


function AJAXSearch(selection) {
    var xmlHttpHandset = GetAJAX();
    var d = "", h = "", m = "", n = "";


    if (document.getElementById("slmanufacturers")) {
        m = document.getElementById("slmanufacturers").value;
    }
    if (document.getElementById("slmodels")) {
        h = document.getElementById("slmodels").value;
    }
    if (document.getElementById("slnetworks")) {
        n = document.getElementById("slnetworks").value;
    }

    if (document.getElementById("sldealtype")) {
        d = document.getElementById("sldealtype").value;
        if (d == "") {
            n = "";
            h = "";
            m = "";
        }
    }
    if (selection == "sldealtype") {
        n = "";
        h = "";
        m = "";
    }

    //alert(document.getElementById("slmanufacturers"));

    var url = webUrl + "ajax-top-search.aspx?s=" + selection + "&d=" + d + "&h=" + h + "&m=" + m + "&n=" + n + "&k=" + Math.random();
    //alert(url);
    xmlHttpHandset.open("GET", url, true);
    xmlHttpHandset.onreadystatechange = function() {
        if (xmlHttpHandset.readyState == 4 && xmlHttpHandset.status == 200) {
            document.getElementById("dvSearch").innerHTML = xmlHttpHandset.responseText;
        }
        else {

            document.getElementById("dvSearch").innerHTML = "<select name='slmanufacturers' class='txt-field' id='slmanufacturers'><option selected='selected' value=''>Loading...</option></select><select name='slmodels' class='txt-field' id='slmodels'><option selected='selected' value=''>Loading...</option></select><select name='slnetworks' class='txt-field' id='slnetworks'><option selected='selected' value=''>Loading...</option></select>";
        }
    }
    xmlHttpHandset.send(null);
}

function GoToSearch() {
    document.getElementById("__VIEWSTATE").value = "";

    if (document.getElementById("slmodels") != null && document.getElementById("slmodels").value != "") {
        var x = document.getElementById("slmodels")
        var url = GetHandsetUrl(x.options[x.selectedIndex].text, document.getElementById("sldealtype").value);
        document.forms[0].action = url;
    }
    else {
        document.forms[0].action = webUrl + "search.aspx";
    }
}



function GetHandsetUrl(_handsetName, _dealType) {
    pattern = '(nokia|apple|samsung|sony ericsson|lg|blackberry|motorola|htc|acer|other|acer|three|hp|t-mobile|other|three|hp|alcatel|binatone|dell|doro|huwei|inq1|inq|o2|zte|vodafone|orange) (.*)';
    _handsetName = _handsetName.toLowerCase();
    var txt = new RegExp(pattern, 'g');
    var url;
    if (_dealType == "contract")
        url = _handsetName.replace(txt, webUrl + '$1/$2-deals.html');
    else if (_dealType == "payg")
        url = _handsetName.replace(txt, webUrl + '$1/$2-payg.html');
    else if (_dealType == "sim-free")
        url = _handsetName.replace(txt, webUrl + '$1/$2-sim-free.html');
    else
        url = _handsetName.replace(txt, webUrl + '$1/$2-deals.html');

    return url.replace(/ /g, '-');
}


function DropdownFilters(hst, mf, hs, nt) {

    //if (fg != "") {
    //fg = fg.replace("+", "$");
    //}
    if (hst != "") {

        var xmlHttpHandset = GetAJAX();

        var url = webUrl + "ajax/get-search-results-mcs.aspx?hst=" + hst + "&mf=" + mf + "&hs=" + hs + "&nt=" + nt + "&k=" + Math.random();
        //alert(url);

        xmlHttpHandset.open("GET", url, true);
        xmlHttpHandset.onreadystatechange = function() {
            if (xmlHttpHandset.readyState == 4 && xmlHttpHandset.status == 200) {
                document.getElementById("dvSearchbox").innerHTML = xmlHttpHandset.responseText;
                //alert(xmlHttpHandset.responseText);
            }
            else {
                if (hst == "c") {
                    document.getElementById("dvSearchbox").innerHTML = "<div class=\"blk\">Search Your Mobile Now</div><select name=\"ddlHandSetType\" id=\"ddlHandSetType\"><option value=\"\">--Loading--</option></select><span id=\"spman\"><select name=\"ddlManufacturer\" id=\"ddlManufacturer\"><option value=\"\">--Loading--</option></select></span><span id=\"spmodel\"><select name=\"ddlHandset\" id=\"ddlHandset\"><option value=\"\">--Loading--</option></select></span><span id=\"spnet\"><select name=\"ddlNetwork\" id=\"ddlNetwork\"></select><option value=\"\">Loaging</option></select></span>"
                }
                if (hst == "p") {
                    document.getElementById("dvSearchbox").innerHTML = "<div class=\"blk\">Search Your Mobile Now</div><select name=\"ddlHandSetType\" id=\"ddlHandSetType\"><option value=\"\">--Loading--</option></select><span id=\"spman\"><select name=\"ddlManufacturer\" id=\"ddlManufacturer\"><option value=\"\">--Loading--</option></select></span><span id=\"spmodel\"><select name=\"ddlHandset\" id=\"ddlHandset\"><option value=\"\">--Loading--</option></select></span><span id=\"spnet\"><select name=\"ddlNetwork\" id=\"ddlNetwork\"><option value=\"\">Loaging</option></select></span>"
                }
                if (hst == "s") {
                    document.getElementById("dvSearchbox").innerHTML = "<div class=\"blk\">Search Your Mobile Now</div><select name=\"ddlHandSetType\" id=\"ddlHandSetType\"><option value=\"\">--Loading--</option></select><span id=\"spman\"><select name=\"ddlManufacturer\" id=\"ddlManufacturer\"><option value=\"\">--Loading--</option></select></span><span id=\"spmodel\"><select name=\"ddlHandset\" id=\"ddlHandset\"><option value=\"\">--Loading--</option></select></span>"
                }
            }
        }
        xmlHttpHandset.send(null);
    }
    else {
        return false;
    }
}

function SearchType(dt) {
    var strSearchUrl = "";
    var mf = document.forms[0].ddlManufacturer;
    mft = mf.options[mf.selectedIndex].text;

    var hs = document.forms[0].ddlHandset;
    hst = hs.options[hs.selectedIndex].text;

    if (dt == 'c')   //---------Contract Search
    {
        var nt = document.forms[0].ddlNetwork;
        ntt = nt.options[nt.selectedIndex].text;
        var cm = document.forms[0].ddlCLength;
        cmt = cm.options[cm.selectedIndex].text;
        var mr = document.forms[0].ddlLineRental;
        mrt = mr.options[mr.selectedIndex].text;
        var fg = document.forms[0].FreeGift;
        fgt = fg.options[fg.selectedIndex].text;
        dt = "contract";
        strSearchUrl = webUrl + dt + "-mobile-phones.html";
        if (mf.value != "") {
            if (mft == "Other")
                mft = "all";
            strSearchUrl = webUrl + mft.replace(" ", "-").toLowerCase() + "-mobile-phones.html";
        }
        if (mf.value == "" && nt.value != "" && cm.value == "" && mr.value == "" && fg.value == "") {
            strSearchUrl = webUrl + "networks/" + ntt.replace(/ /g, "-").toLowerCase() + "-phone-deals.html";
        }
        if (mf.value != "" && nt.value != "" && cm.value == "" && mr.value == "" && fg.value == "") {
            strSearchUrl = webUrl + "networks/" + ntt.replace(/ /g, "-").toLowerCase() + "-phone-deals.html";
        }
        if ((mf.value == "" || nt.value == "") && (cm.value != "" || mr.value != "" || fg.value != "")) {
            strSearchUrl = webUrl + "search.html";
        }
        if (mf.value == "" && hs.value == "" && nt.value == "" && cm.value == "" && mr.value == "" && fg.value == "") {
            strSearchUrl = webUrl + dt + "-mobile-phones.html";
        }
        if (hs.value != "") {
            strSearchUrl = webUrl + hst.replace(/ /g, "-").toLowerCase() + "-deals.html";
        }

    }
    else if (dt == 'p') {
        var nt = document.forms[0].ddlNetwork;
        ntt = nt.options[nt.selectedIndex].text;

        var pr = document.forms[0].ddlPriceRange;
        prt = pr.options[pr.selectedIndex].text;

        dt = "payg";
        strSearchUrl = "pay-as-you-go.html";
        if (mf.value != "") {
            if (mft == "Other")
                mft = "all";
            strSearchUrl = webUrl + mft.replace(" ", "-").toLowerCase() + "-mobile-phones.html";
        }
        if (nt.value != "" || pr.value != "") {
            strSearchUrl = webUrl + "networks/" + ntt.replace(/ /g, "-").toLowerCase() + "-phone-deals.html";
        }

        if (nt.value == "" && pr.value != "") {
            strSearchUrl = webUrl + "search.html";
        }

        if (mf.value == "" && hs.value == "" && nt.value == "" && pr.value == "") {
            strSearchUrl = webUrl + "pay-as-you-go.html";
        }

        if (hs.value == "" && pr.value != "") {
            strSearchUrl = webUrl + "search.html";
        }

        if (hs.value != "") {
            strSearchUrl = webUrl + hst.replace(/ /g, "-").toLowerCase() + "-deals.html";
        }
    }

    else if (dt == 's') {
        var pr = document.forms[0].ddlPriceRange;
        prt = pr.options[pr.selectedIndex].text;

        dt = "sim-free";
        strSearchUrl = "sim-free-phones.html";
        if (mf.value != "") {
            if (mft == "Other")
                mft = "all";
            strSearchUrl = webUrl + mft.replace(" ", "-").toLowerCase() + "-mobile-phones.html";
        }

        if (hs.value != "") {
            strSearchUrl = webUrl + hst.replace(/ /g, "-").toLowerCase() + "-deals.html";
        }

        if (pr.value != "") {
            strSearchUrl = webUrl + "search.html";
        }
        if (mf.value == "" && hs.value == "" && pr.value == "") {
            strSearchUrl = webUrl + "sim-free-phones.html";
        }
    }

    if (strSearchUrl != "") {
        document.aspnetForm.action = strSearchUrl;
        document.aspnetForm.submit();

        return true;
    }
    return false;
}



function ShowHideDealTab(tab) {
    var divFeaturbtn = document.getElementById("divFeaturbtn");
    var divDescriptionbtn = document.getElementById("divDescriptionbtn");
    var divDescription = document.getElementById("divDescription");
    var divFeatur = document.getElementById("divFeatur");

    if (tab == "description") {
        divFeaturbtn.style.display = "block";
        divFeatur.style.display = "none";
        divDescriptionbtn.style.display = "none";
        divDescription.style.display = "block";
    }
    if (tab == "feature") {
        divFeaturbtn.style.display = "none";
        divFeatur.style.display = "block";
        divDescriptionbtn.style.display = "block";
        divDescription.style.display = "none";
    }
}


function goToDealPage(page) {
    if (document.getElementById("ddlHandSetType") && document.getElementById("ctl00_cph_hdnHandSetType"))
        document.getElementById("ddlHandSetType").value = document.getElementById("ctl00_cph_hdnHandSetType").value;


    if (document.getElementById("ddlManufacturer") != null && document.getElementById("ctl00_cph_hdnManufacturer") != null)
        document.getElementById("ddlManufacturer").options[0] = new Option("-- Manufacturer --", document.getElementById("ctl00_cph_hdnManufacturer").value, true, false);


    if (document.getElementById("ddlHandset") != null && document.getElementById("ctl00_cph_hdnHandset") != null)
        document.getElementById("ddlHandset").options[0] = new Option("-- Select Handset --", document.getElementById("ctl00_cph_hdnHandset").value, true, false);
    else
        document.getElementById("ddlHandset").options[0] = new Option("-- Select Handset --", "-- Select Handset --", true, false);


    if (document.getElementById("ddlNetwork") != null && document.getElementById("ctl00_cph_hdnNetwork") != null)
        document.getElementById("ddlNetwork").options[0] = new Option("-- Network --", document.getElementById("ctl00_cph_hdnNetwork").value, "-- Network --", true, false);

    if (document.getElementById("ddlCLength") != null && document.getElementById("ctl00_cph_hdnCLength") != null)
        document.getElementById("ddlCLength").options[0] = new Option("-- Length --", document.getElementById("ctl00_cph_hdnCLength").value, "-- Length --", true, false);

    if (document.getElementById("ddlLineRental") != null && document.getElementById("ctl00_cph_hdnLineRental") != null)
        document.getElementById("ddlLineRental").options[0] = new Option("-- Line Rental --", document.getElementById("ctl00_cph_hdnLineRental").value, "-- Line Rental --", true, false);

    if (document.getElementById("FreeGift") && document.getElementById("ctl00_cph_hdnFreeGift"))
        document.getElementById("FreeGift").options[0] = new Option("-- Free Gifts --", document.getElementById("ctl00_cph_hdnFreeGift").value, true, false);

    document.aspnetForm.action = page;
    document.aspnetForm.submit();

    return true;
}

function getDealPage(page) {
    document.location = webUrl + page + ".html";
}

function AddRedirection(netId, cMonths) {    
    if (document.getElementsByTagName) {
        var links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++) {
            if (links[i].className.match("deallink")) {
                links[i].onclick = function() {
                    document.getElementById("ctl00_cph_hidContractMonths").value = cMonths;
                    document.getElementById("slnetworks").options[0] = new Option("Networks", netId, true, true)
                    document.forms[0].action = this.getAttribute("href");
                    document.getElementById("__VIEWSTATE").value = "";
                    document.forms[0].submit();
                    return false;
                };
            }
        }
    }
}
