﻿// Javascript library for Platform Beds Home Page

function checkPostedQuery(){
    var url=unescape(window.location.href);
    
    if (url.indexOf('search=')!= -1){
    
        if (url.indexOf('#SearchResultsHere')!= -1) 
            url = url.substring(0, url.indexOf('#SearchResultsHere'));
        
        document.getElementById('searchBoxBackup').value=url.substring(url.indexOf('search=')+7);
        
        update('search=' + url.substring(url.indexOf('search=')+7) + '&page=1&show=' + getShown());
    }else{
        document.getElementById('searchBoxBackup').value="everything";
    
        update("page=1&search=" + getSearch() + "&show=" + getShown());
    }
}

function clickSearch(el){
    if(el.value=='Click here to search') el.value='';
    
//    el.onmouseout=function() {
//        if(el.value=='') el.value='Click here to search';
//    }
    
//    el.onchange=function() {
//        if(el.value=='') el.value='Click here to search';
//    }
}
//START AJAX

function update(searchString) {//this is where the search AJAX is called

    var xmlHttp = GetXmlHttpObject();

    if ( xmlHttp == null ) {
        alert ( "Your browser does not support <strong class=\"highlight\">AJAX</strong>!" );
        return;
    } 

    var url="/search.php?" + searchString;

    xmlHttp.onreadystatechange = function(){ 
        if ( xmlHttp.readyState == 4 ) {
            document.getElementById('searchWaitingBox').style.visibility = 'hidden';
            if ( xmlHttp.status == 200 ) {//
                document.getElementById('searchResults').innerHTML = xmlHttp.responseText;
            } else {
                //alert( 'The server failed to process your request' );
            }
        }
    };
    xmlHttp.open( "GET", url, true );
    xmlHttp.send( null );
} 

function GetXmlHttpObject () {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // <strong>Internet</strong> <strong>Explorer</strong>
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

//END AJAX


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function onEnter(e) {//press enter on the textbox and it searches 
    var keyCode = null;

    if (e.which)
        keyCode = e.which;
    else if (e.keyCode)
        keyCode = e.keyCode;

    if (13 == keyCode) {//if it was "enter" key (ascii 13)
        document.getElementById('searchButton').click();
        return false;
    }
    return true;
}

function getSearch(){//gets the search query from the drop downs and the search textbox
    var search = document.getElementById('searchBoxBackup');//hidden search box object
    var gender = document.getElementById('gender');
    var style = document.getElementById('style');
    var brand = document.getElementById('brand');
    
    var composite;
    
    composite = search.value + ' ';
    if (gender.value!='nothing') composite += gender.value + ' ';
    if (style.value!='nothing') composite += style.value + ' ';
    if (brand.value!='nothing') composite += brand.value;
    
    composite = composite.replace('  ',' ');
    composite = composite.trim();
    composite = composite.replace(' ','%20');
    
    return composite;
}

function getShown(){//how many we are showing per page
    var display=document.getElementById('display');//get the number of results per page object
    
    return display.options[display.selectedIndex].value;
}

