//﻿var _sFormFx = new Array();
var _sFormBed = new Array();


var _sFormBath = new Array();
var _sFormGarage = new Array();
var _sFormLevel = new Array();
//var _sFormStyle = new Array();
var _sFormBurlPathname = document.location.toString();

var _sFormBurlPathnamePos = _sFormBurlPathname.lastIndexOf("/");
if (_sFormBurlPathnamePos > 6) {
    _sFormBurlPathname = _sFormBurlPathname.substr(0, _sFormBurlPathnamePos + 1);
}
var _sFormBurl = _sFormBurlPathname + "search.php";


var _newLabel = "Go...";
var _sCookieName = "BIPH_sFormSearch";
var _gCookieName = "BIPH_sFormGallery";
var _sFormExpCol = null;
var totalPlans = "";

var BASEURLSAVE = _sFormBurl.replace("search.php", "hp_plan_to_save.php?id=");
var BASEURLLOAD = _sFormBurl.replace("search.php", "hp_plan_to_load.php?id=");
var BASEURLSIGNIN = _sFormBurl.replace("search.php", "signin.php");

/* GLOBAL INITIALIZE PAGE*/
var _search = window.location.search;
if (_search.toString().length > 1) {
    sCreateCookie(_sCookieName, _search, 7);
}
else {
    _search = sReadCookie(_sCookieName);
    if (_search != null && _search.toString().length > 1) {
        url = _sFormBurl + _search;
        if (document.location.pathname.indexOf("search.php") > 0) window.location = url;
    }
}


function sFormNewClick() {
    sEraseCookie(_sCookieName);
    window.location = _sFormBurl;
}
/* ---------- */

/* SHOULD BE IN COMMON.JS */
if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

function sCreateCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function sReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function sEraseCookie(name) {
    sCreateCookie(name, "", -1);
}
/* ---------- */


function gup(name) {
    var regexS = "[\\?&]" + name + "=([^&#*]*)";
    var regex = new RegExp(regexS);
    var tmpURL = window.location.href;
    var results = regex.exec(tmpURL);
    if (results == null)
        return "";
    else
        return results[1];
}

function sFormComplete(field, fieldArray) {
    var ar = gup(field).split(",");
    var boxes = document.getElementById("sForm").getElementsByTagName("input");
    for (i = 0; i < boxes.length; i++) {
        box = boxes[i];
        if (box.type == "checkbox" && box.name == field) {
            if (ar.indexOf(box.value) != -1) {
                fieldArray.push(box.value);
                box.checked = true;
                sExpandParent(box);
            }
        }
    }
}

function sFormI() {
    document.getElementById("sForm").reset();
    //sFormComplete("fx", _sFormFx);
    sFormComplete("bed", _sFormBed);
    sFormComplete("bath", _sFormBath);
    sFormComplete("garage", _sFormGarage);
    sFormComplete("level", _sFormLevel);
    //document.getElementById("sFormCol").value = gup("collection");
    //document.getElementById("sFormStyle").value = gup("styles");
    //sFormComplete("styles", _sFormStyle);
    //document.getElementById("sFormSort").value = gup("sort");
    //-- fix for ie
    sortv = gup("sort");
    if ( sortv.length == 0 )
        sortv = "model_d"; //default value
    document.getElementById("sFormSort").value = sortv;

    // -----
    //
    //document.getElementById("sFormCount").value = gup("count");
    // -- fix for ie
    scount = gup("count");
    scount = parseInt(scount);
    if ( isNaN(scount) )
        scount = 48; // default value
    document.getElementById("sFormCount").value = scount;
    // --

    document.getElementById("sqftmin").value = gup("sqftmin");
    document.getElementById("sqftmax").value = gup("sqftmax");
    //document.getElementById("widthmin").value = gup("widthmin");
    //document.getElementById("widthmax").value = gup("widthmax");
    //document.getElementById("depthmin").value = gup("depthmin");
    //document.getElementById("depthmax").value = gup("depthmax");
    var a = gup("model");
    if (a != null && a.slice(-1) == "-") a = a + "*";
    document.getElementById("aModel").value = a;
    if (window.location.search.toString().length > 1) document.getElementById("sFormNew").style.visibility = "visible";
}




function sFormC(o) {
    //update = false;
    if (o == null) return;
    if (o.type == "checkbox") {
        //update = true;
        //if (o.name == "fx") {
        //    if (o.checked == true) sFormPush(_sFormFx, o.value); else sFormPop(_sFormFx, o.value);
        //}
        //else
        if (o.name == "bed") {
            if (o.checked == true) sFormPush(_sFormBed, o.value); else sFormPop(_sFormBed, o.value);
        }
        else if (o.name == "bath") {
            if (o.checked == true) sFormPush(_sFormBath, o.value); else sFormPop(_sFormBath, o.value);
        }
        else if (o.name == "garage") {
            if (o.checked == true) sFormPush(_sFormGarage, o.value); else sFormPop(_sFormGarage, o.value);
        }
        else if (o.name == "level") {
            if (o.checked == true) sFormPush(_sFormLevel, o.value); else sFormPop(_sFormLevel, o.value);
        }
        //else if (o.name == "styles") {
        //    if (o.checked == true) sFormPush(_sFormStyle, o.value); else sFormPop(_sFormStyle, o.value);
        //}
        //else update = false;
    }
    //if (update) sFormGoClick();
    updatePlanFound();
}


function sFormPush(ar, v) {
    if (ar == null || v == null) return;
    var i = ar.indexOf(v);
    if (i == -1) ar.push(v);
}

function sFormPop(ar, v) {
    if (ar == null || v == null) return;
    var i = ar.indexOf(v);
    if (i == -1) return;
    ar.splice(i, 1);
}


function sLocationString() {
    var locationString = _sFormBurl;
    //var designerClause = "";
    var modelnum = document.getElementById("aModel") ? document.getElementById("aModel").value : "";

    if ((modelnum != "Or Enter Plan #") && (modelnum.length > 0) && (modelnum.slice(-1) != "-") && (modelnum.slice(-1) != "*")) {
        locationString = "models.php?model=" + modelnum;
    }
    else {
        //if (modelnum.slice(-1) == "-") {
        //    designerClause = "&model=" + modelnum + "*"
        //} else if (modelnum.slice(-1) == "*") {
        //    designerClause = "&model=" + modelnum;
        //}
        //var col = document.getElementById("sFormCol") ? document.getElementById("sFormCol").value : "";
        //var style = document.getElementById("sFormStyle") ? document.getElementById("sFormStyle").value : "";
        //var style = _sFormStyle.toString();
        var sqftmin = document.getElementById("sqftmin") ? document.getElementById("sqftmin").value : "";
        var sqftmax = document.getElementById("sqftmax") ? document.getElementById("sqftmax").value : "";
        var depthmin = document.getElementById("depthmin") ? document.getElementById("depthmin").value : "";
        var depthmax = document.getElementById("depthmax") ? document.getElementById("depthmax").value : "";
        var widthmin = document.getElementById("widthmin") ? document.getElementById("widthmin").value : "";
        var widthmax = document.getElementById("widthmax") ? document.getElementById("widthmax").value : "";
        locationString += "?fx=";// + _sFormFx.toString() + "&collection=" + col + "&styles=" + style + designerClause;

        if (_sFormBed.length > 0) locationString += "&bed=" + _sFormBed.toString();
        if (_sFormBath.length > 0) locationString += "&bath=" + _sFormBath.toString();
        if (_sFormGarage.length > 0) locationString += "&garage=" + _sFormGarage.toString();
        if (_sFormLevel.length > 0) locationString += "&level=" + _sFormLevel.toString();
        if (sqftmin.length > 0) locationString += "&sqftmin=" + sqftmin;
        if (sqftmax.length > 0) locationString += "&sqftmax=" + sqftmax;
        //if (depthmin.length > 0) locationString += "&depthmin=" + depthmin;
        //if (depthmax.length > 0) locationString += "&depthmax=" + depthmax;
        //if (widthmin.length > 0) locationString += "&widthmin=" + widthmin;
        //if (widthmax.length > 0) locationString += "&widthmax=" + widthmax;
    }
    return locationString;
}

function sFormGoClick(pageInc) {
    if (pageInc == null) pageInc = 0;
    if (document.getElementById("sFormButtonNext") != null && document.getElementById("sFormButtonNext").name == _newLabel) pageInc = 0;
    var pageNo = document.getElementById("sFormPageNo") ? pageNo = document.getElementById("sFormPageNo").value : 1;
    if (pageNo == null) pageNo = 1;
    pageInc = parseInt(pageInc);
    if (pageInc == -2)
        pageNo = 1;
    else
        pageNo = parseInt(pageNo) + pageInc;

    var sort = document.getElementById("sFormSort") ? document.getElementById("sFormSort").value : "";
    var count = document.getElementById("sFormCount") ? document.getElementById("sFormCount").value : "";
    var locationString = sLocationString() + "&page=" + pageNo.toString() + "&sort=" + sort.toString() + "&count=" + count.toString();

    //May want to disable page here, or show popup layer to stop users from clicking more checkboxes while new page is loading
    //var obj = document.getElementById("content_search_right");
    //if (obj != null) obj.innerHTML = "";
    document.location = locationString;
}

function sFormGoPage(event, page) {
    if (page) {
        document.getElementById("sFormButtonNext").name = _newLabel;
        document.getElementById("sFormButtonNext").value = _newLabel;
        document.getElementById("sFormButtonNext").disabled = false;
    }
    if (event.keyCode == 13) sFormGoClick();
}

function sFormExpCol(divID) {
    if (divID == null) return;
    var o = document.getElementById(divID);
    if (o == null) return;
    if (_sFormExpCol != null) {
        var el = document.getElementById(_sFormExpCol);
        if (el.id != o.id) el.style.display = 'none';
        var im = document.getElementById(_sFormExpCol.toString().replace("_sCat_", "_sArr_"));
        im.src = "images/search/rightarrow.gif";
    }

    if (o.style.display != 'block' && o.style.display != '') {
        o.style.display = 'block';
        var im = document.getElementById(divID.toString().replace("_sCat_", "_sArr_"));
        im.src = "images/search/downarrow.gif";
    }
    else {
        o.style.display = 'none';
        _sFormExpCol = null;
        var im = document.getElementById(divID.toString().replace("_sCat_", "_sArr_"));
        im.src = "images/search/rightarrow.gif";
    }
}

function sFormExpColAll() {
    var divs = document.getElementById("sForm").getElementsByTagName("div");
    for (i = 0; i < divs.length; i++) {
        div = divs[i];
        if (div.id.indexOf("_sCat_") == 0) {
            sFormExpCol(div.id)
        }
    }
}

function sExpandParent(box) {
    if (box == null) return;
    p = box.parentNode;
    while (p != null && p.nodeName.toUpperCase() != "DIV") p = p.parentNode;
    if (p != null && p.nodeName.toUpperCase() == "DIV") {
        p.style.display = 'block';
        var im = document.getElementById(p.id.toString().replace("_sCat_", "_sArr_"));
        im.src = "images/search/downarrow.gif";
    }
}

function sFormByNumberGoClick() {
    var model = document.getElementById("model").value;

    if ((model != "Or Enter Plan #") && (model.length > 0)) {
        if (model.slice(-1) == "-") {
            locationString = "search.php?model=" + model + "*";
        }
        else if (model.slice(-1) == "*") {
            locationString = "search.php?model=" + model;
        }
        else {
            locationString = "models.php?model=" + model;
        }
    }
    document.location.href = locationString;
}


function GetXmlHttpObject() {
    xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
// @todo - no favorites we don't need this
function savePlan(id) {
    var xhr = GetXmlHttpObject();
    if (xhr == null) return;
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var imgid = "_sFav_" + id;
                //var spanid = "sp_" + id;
                var anchorid = "_sAdd_" + id;
                if (xhr.responseText == "notloggedin") {
                    parent.location = BASEURLSIGNIN + "?savePlanID=" + id;
                }
                else if (xhr.responseText == "ok") {
                    document[imgid].src = "./images/search/button_infavorites.gif";
                    //document.getElementById(spanid).innerHTML = "In Favorites";
                    document.getElementById(anchorid).href = "account_prefer.asp?#savedItems";
                }
                else {
                    document[imgid].src = "./images/search/button_adding.gif";
                    document[spanid].innerHTML = "";
                }
            }
        }
    };
    var d = new Date();
    xhr.open("GET", BASEURLSAVE + id + "&nocache=" + d.getTime(), true);
    xhr.send(null);
}

function onSave(id) {
    var imgid = "_sFav_" + id;

    if (document[imgid].src.indexOf("images/search/button_addtofavorites.png") >= 0) {
        document[imgid].src = "./images/search/button_adding.gif";
        savePlan(id);
        if (document.getElementById('planSavedCount')) {
            document.getElementById('planSavedCount').innerHTML = document.getElementById('planSavedCount').innerHTML * 1 + 1;
        }
    }
}
// @todo probably don't need this
function updatePlanFound() {
    //alert(document.getElementById("sFormCol").innerHTML);
    //alert(document.getElementById("sFormCol").value);
    //alert(document.getElementById("sFormStyle").innerHTML);
    //alert(document.getElementById("sFormStyle").value);

    var xhr = GetXmlHttpObject();
    if (xhr == null) return;
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                //alert(xhr.responseText);
                var parts = xhr.responseText.split(",");
                if (parts.length == 2) {
                    if (document.getElementById("sFormEst") != null) {
                        document.getElementById("sFormEst").innerHTML = parts[0];
                        document.getElementById("sFormTotal").innerHTML = parts[1];
                    }
                    document.getElementById("spnHeaderTotal").innerHTML = parts[1];
                }
            }
        }
    };
    var d = new Date();
    var locationString = sLocationString();
    locationString = locationString.replace("search.php", "search_est.php");
    //alert(locationString + "&nocache=" + d.getTime());
    xhr.open("GET", locationString + "&nocache=" + d.getTime(), true);
    xhr.send(null);
}

//<!-- @todo we don't need this for now -->
function loadSavedPlans(planString) {
    var xhr = GetXmlHttpObject();
    if (xhr == null) return;
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var plans = xhr.responseText.split(",");
                for (i = 0; i < plans.length; i++) {
                    if (plans[i].replace(/^\s+|\s+$/g, "").length > 0) {
                        var imgid = "_sFav_" + plans[i];
                        //var spanid = "sp_" + id;
                        var anchorid = "_sAdd_" + plans[i];
                        document[imgid].src = "./images/search/button_infavorites.gif";
                        //document.getElementById(spanid).innerHTML = "In Favorites";
                        document.getElementById(anchorid).href = "account_prefer.asp?#savedItems";
                    }
                }
            }
        }
    };
    var d = new Date();
    xhr.open("GET", BASEURLLOAD + planString + "&nocache=" + d.getTime(), true);
    xhr.send(null);
}

function clearModelField(field) {
    if (field.value == "Or Enter Plan #") {
        field.value = "";
    }
}
