    var BASEURL = "http://" + document.location.host + document.location.pathname + "?fields=";
    BASEURL = BASEURL.replace("search_form.asp", "search.asp");
    var BASEURLCOMMON = BASEURL.replace("search_builder.asp", "search.asp");
    BASEURLCOMMON = BASEURLCOMMON.replace("search_form.asp", "search.asp");
    var BASEURLCOUNT = BASEURLCOMMON.replace("search.asp", "search_count.asp");
    var BASEURLSAVE = BASEURLCOMMON.replace("search.asp?fields=", "hp_plan_to_save.asp?id=");
    var BASEURLSIGNIN = BASEURLCOMMON.replace("search.asp?fields=", "signin.asp");
    var myPage = 1;
    var realPage = 0;
    var lastdoSearch = null;
 
    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 onInit()
    {
        var param = gup('fields')
        param = param.replace(/%2B/g, "+");
        var fields = param.split(',');
        var boxes = document.getElementsByTagName('input');
        for(i = 0; i < boxes.length; i++)
        {
            box = boxes[i];
            if(box.type=='checkbox')
            {
                for(j = 0; j < fields.length; j++)
                {
                    if(fields[j] == box.value) box.checked = true;
                }
            }
            else if(box.type=='radio')
            {
                boxvalue = box.name + ":" + box.value;
                for(j = 0; j < fields.length; j++)
                {
                    if(fields[j] == boxvalue) box.checked = true;
                }
            }           
            else if(box.type=='text')
            {
                for(j = 0; j < fields.length; j++)
                {
                    if(fields[j].indexOf(box.name + ":") == 0)
                    {
                        box.value = fields[j].split(':')[1];
                    }
   
                }            
            }
        }
        var sels = document.getElementsByTagName('select');
        for(j = 0; j < fields.length; j++)
        {
            if(fields[j].indexOf("page:") == 0)
            {
                var parts = fields[j].split(':');
                if(parts.length == 2 && parseInt(parts[1]) > 1) myPage = parseInt(parts[1]);
            }
            else for(i = 0; i < sels.length; i++)
            {
                sel = sels[i];
                var parts = fields[j].split(':');
                if(parts.length == 2 && parts[0] == sel.id) sel.value = parts[1];
            }        
        }
        var divs = document.getElementsByTagName('div');
        for(i = 0; i < divs.length; i++)
        {
            if(divs[i].id.indexOf("refine_") == 0)
                LoadFeatures(divs[i].id);
        }
        UpdateSearchCount();
    }
    
    function expandCollapseDiv(divName, altPos)
    {
        if(typeof expandCollapseDiv.lastExpandedDiv == 'undefined' ) 
        {
            expandCollapseDiv.lastExpandedDiv = '';
        }
        var el = '';
        if(expandCollapseDiv.lastExpandedDiv != '' && expandCollapseDiv.lastExpandedDiv != divName)
        {
            el = document.getElementById(expandCollapseDiv.lastExpandedDiv);
            el.style.display = 'none';
        }

        el = document.getElementById(divName);
        if(el.style.display != 'block' )
        {
            var a = document.getElementById('a_' + divName);
            var p = a;
            var top = 0;
            var left = 0;
            while(p != null)
            {
                if(parseInt(p.offsetTop) > 0 && p.tagName != "TD")
                    top += parseInt(p.offsetTop);
                if(parseInt(p.offsetLeft) > 0)
                    left += parseInt(p.offsetLeft);
                p = p.parentNode;
            }
            if(altPos == null)
            {
                el.style.top = top - 30;
                el.style.left = left + 200;
            }
            else
            {
                el.style.top = top + 10;
                el.style.left = left;
            }            
            el.style.display = 'block';            
            expandCollapseDiv.lastExpandedDiv = divName;
        }
        else
        {
            el.style.display = 'none';
            expandCollapseDiv.lastExpandedDiv = '';
        }        
    }
    
    function WindowClear(divName)
    {
        var boxes = document.getElementById(divName).getElementsByTagName('input');
        var box, i;
        for (i = 0; i < boxes.length; i++)
        {
            box = boxes[i];
            if(box.type=='checkbox') 
            {
                if(box.checked) box.checked = false;
            }
            else if(box.type=='text')
            {
                box.value="";
            }
        }
        UpdateSearchCount();
    }
    
    function WindowCancel(divName)
    {
        expandCollapseDiv(divName);
    }
    
    function LoadFeatures(divName)
    {
        var boxes = document.getElementById(divName).getElementsByTagName('input');
        var box, i;
        var parent = document.getElementById(divName + "_options");
        while(parent.hasChildNodes())
            parent.removeChild(parent.lastChild);    
        for (i = 0; i < boxes.length; i++)
        {
            box = boxes[i];
            if(box.type=='checkbox')
            {
                if(box.checked)
                {
                    var newitem = document.createElement("li");                
                    var val = box.value;
                    if(val.indexOf('fx:') == 0 || val.indexOf('style:') == 0 || box.name == "")
                        newitem.innerHTML = box.parentNode.innerHTML.replace(/(<([^>]+)>)/ig,"").replace(/[^a-zA-Z 0-9]+/g,'');
                    else
                        newitem.innerHTML = box.value.charAt(0).toUpperCase() + box.value.substr(1).toLowerCase();
                    parent.appendChild(newitem);            
                }
            }
        }
        if(divName == "refine_rooms")
        {
            var sqftmin = document.getElementById("sqftmin");
            var sqftmax = document.getElementById("sqftmax");
            var depthmin = document.getElementById("depthmin");
            var depthmax = document.getElementById("depthmax");
            var widthmin = document.getElementById("widthmin");
            var widthmax = document.getElementById("widthmax");
            
            if(sqftmin.value != "" || sqftmax.value != "")
            {
                var newitem = document.createElement("li");
                newitem.innerHTML = sqftmin.value + " sq ft to " + sqftmax.value + " sq ft"
                parent.appendChild(newitem);
            }
            if(widthmin.value != "" || widthmax.value != "")
            {
                var newitem = document.createElement("li");
                newitem.innerHTML = "Width: " + widthmin.value + "' to " + widthmax.value + "'"
                parent.appendChild(newitem);
            }
            if(depthmin.value != "" || depthmax.value != "")
            {
                var newitem = document.createElement("li");
                newitem.innerHTML = "Depth: " + depthmin.value + "' to " + depthmax.value + "'"
                parent.appendChild(newitem);
            }            
        }
    }
    
    function WindowOK(divName)
    {
        LoadFeatures(divName);
        expandCollapseDiv(divName);
        myPage = 1;
        Search();
    }
    
    function WindowClick(doSearch)
    {
        lastdoSearch = doSearch;
        UpdateSearchCount();
    }

    function WindowClickT(obj)
    {
        var fields = obj.value.split(':');
        if(fields.length == 2)
        {
            if(fields[0] == "fx") pageTracker._trackEvent('Search', 'fx', fields[1]);
        }
        UpdateSearchCount();
    }
    
    function SearchOptionString()
    {
        var optionstring = "";
        var sels = document.getElementsByTagName('select');
        if(myPage > 1)
        {
            if(optionstring.length != 0) optionstring += ",";
            optionstring += "page:" + myPage;
        }
        for (i = 0; i < sels.length; i++)
        {
            if(optionstring.length != 0) optionstring += ",";
            optionstring += sels[i].id + ":" + sels[i].value;
        }
        var boxes = document.getElementsByTagName('input');
        for (i = 0; i < boxes.length; i++)
        {
            box = boxes[i];
            if(box.type=='checkbox')
            {
                if(box.checked)
                {
                    if(optionstring.length != 0) optionstring += ",";
                    optionstring += box.value;
                }
            }
            else if(box.type=='radio')
            {
                if(box.checked)
                {
                    if(optionstring.length != 0) optionstring += ",";
                    optionstring += box.name + ":" + box.value;
                }
            }            
            else if(box.type=='text' || box.type=='hidden')
            {
                if(box.value != "" && box.name != "pageNum")
                {
                    if(optionstring.length != 0) optionstring += ",";                    
                    if (box.name != "planNum") optionstring += box.name + ":" + box.value;
                }
            } 
        }           
        return optionstring.replace(/\+/g, "%2B");        
    }
    
    function Search()
    {   
        document.location = BASEURL + SearchOptionString();
    }

    function QuickSearch()
    {   
        document.location = BASEURL + SearchOptionString() + "&from=h";
    }


    var xmlHttp = null;
    function UpdateSearchCount()
    {
        xmlHttp=GetXmlHttpObject();
        if(xmlHttp==null) return;
        xmlHttp.onreadystatechange=stateChanged;
        var d = new Date();
        xmlHttp.open("GET",BASEURLCOUNT + SearchOptionString() + "&nocache=" + d.getTime(),true);
        xmlHttp.send(null);
    } 

    function stateChanged() 
    {
        if (xmlHttp.readyState==4)
        {
            count = parseInt(xmlHttp.responseText);
            if(count >= 0 && count <= 99999)
            {
                if(lastdoSearch != null && lastdoSearch == true)
                {
                    lastdoSearch = null;
                    if(count > 0)
                    {
                        myPage = 1;
                        Search();
                    }
                    else alert("We did not find any plans, please try another plan search");                
                }
                
                var spans = document.getElementsByTagName("span");
                for(i = 0; i < spans.length; i++)
                {
                    if(spans[i].className == "refine_count")
                    {
                        spans[i].innerHTML = count;
                    }
                }
                var divs = document.getElementsByTagName("div");
                for(i = 0; i < divs.length; i++)
                {
                    if(divs[i].className == "refine_popop_buttons")
                    {
                        var buttons = document.getElementsByTagName("input");
                        for(j = 0; j < buttons.length; j++)
                        {
                            if(buttons[j].name == "SEARCH") 
                            {
                                if(count == 0)
                                    buttons[j].disabled = true;
                                else
                                    buttons[j].disabled = false;
                            }
                        }
                    }
                }                
            }    
        }
    }

    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;
    }
    
    function ChangeOption(reset)
    {
        if(reset == true)
            myPage = 1;    
        Search();
    }
    
    function Page(op)
    {
        if(op == "prev")
        {
            if(realPage > 1)
                myPage = realPage - 1;
        }
        else if(op == "next") myPage = realPage + 1;
        Search();        
    }
    
    function GoToPage(pNum)
    {
		if (isNumeric(pNum))
		{
			myPage = pNum;
			Search();    
		}
		else
		{
			alert("Please Enter a valid page number");
		}    
    }  
    
	function isNumeric(value) {
		if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
		return true;
	}
     
    
    function savePlan(id)
    {
      var xhr = GetXmlHttpObject();
      if(xhr == null) return;
      xhr.onreadystatechange  = function()
      {
          if(xhr.readyState  == 4)
          {
              if(xhr.status  == 200)
              {
                  var imgid = "si_" + id;
                  var spanid = "sp_" + id;
                  var anchorid = "a_" + id;                  
                  if(xhr.responseText == "notloggedin")
                  {
                    parent.location = BASEURLSIGNIN + "?savePlanID=" + id;
                  }
                  else if(xhr.responseText == "ok")
                  {
                    document[imgid].src="http://www.houseplans.com/images/star.gif";
                    document.getElementById(spanid).innerHTML = "In Favorites";
                    document.getElementById(anchorid).href = "account_prefer.asp?#savedItems";                    
                  }
                  else
                  {
                    document[imgid].src="http://www.houseplans.com/images/search/search_save.png";
                    document[spanid].innerHTML = "";
                  }
              }
          }
      };
      var d = new Date();
      xhr.open("GET", BASEURLSAVE + id + "&nocache=" + d.getTime(),  true);
      xhr.send(null);
    }

    function onSave(id)
    {
      var imgid = "si_" + id;
      
      if(document[imgid].src=="http://www.houseplans.com/images/search/search_save.png")
      {
          document[imgid].src="http://www.houseplans.com/images/search/search_saving.png";
          savePlan(id);
          if (document.getElementById('planSavedCount'))
          {
            document.getElementById('planSavedCount').innerHTML = document.getElementById('planSavedCount').innerHTML *1 +1;
          }
      }
    }
