//----------------------------------------------------------
// set up the page bits
//----------------------------------------------------------

hasResults = false;

function zipSearchInit(){
    // set up global handles to various items
    hDrop = document.getElementById('idDrop');
    hText = document.getElementById('idSearchField');
    zipGroupPosition = document.getElementById('zipGroupPos');
    // set handler for closing dropdown
    //hDrop.onblur = closeDrop;

    // set up keypress handler for text box
    hText.onkeyup = doSearch;
    hText.onfocus = typeSomething;
    hText.onblur = hideDropdown;
    //
    radius = document.getElementById('idRadius');

    // catch click event anywhere else to close droplist
    document.onclick = closeDrop;
    
    hText.value = 'Start typing...';
    //hText.focus();
}

//----------------------------------------------------------
// close the droplist and focus the input box
//----------------------------------------------------------
function closeDrop(){
    hDrop.style.display = 'none';
    hDrop.innerHTML = '';
    //hText.focus();
}

//----------------------------------------------------------
// find position of an object on the page
//----------------------------------------------------------
function findPos(obj){
    var curleft = curtop = 0;
    if (obj.offsetParent){
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent){
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

//----------------------------------------------------------
// do search with current contents of text box
//----------------------------------------------------------
function doSearch(e){
    //alert('here');
    //alert(this.value);
    if(isNaN(this.value)){
        //alert(this.value);
        this.value = '';
        //return false;
    }    
    
    req = createRequest();
    url = 'ajax_files/zipcode_search.php?s4=' + escape(hText.value);
    req.open('GET',url,true);
    req.onreadystatechange = showSearchResults;
    req.send(null);
}

//-------------------------
function typeSomething(){
    if(this.value == ''){
        hText.value = 'Start typing...';         
    }else if(!isNaN(hText.value)){
        //if(hText.target){
            doSearch(this);
        //}        
        //return false;
    }else{
        hText.value = '';
    }   
    return false;        
}

//----------------------------------

function checkSubmit(){
    //alert('submit');
    if(isNaN(hText.value)){
        hText.value = '';
        return true;
    }else{
        return true;
    }
    
}

//------------------------------------
function hideDropdown(){
    if(this.value == ''){
        hText.value = 'Start typing...';         
    }else if(!isNaN(hText.value)){
        //if(hText.target){
            //doSearch(this);
        //}        
        return false;
    }else{
        hText.value = '';
    }   
    return false;
}

//----------------------------------------------------------
// show search results
//----------------------------------------------------------
function showSearchResults(){
    if (req.readyState != 4)
        return;

    if (req.status != 200)
        return;

    // get a handle to the droplist div & clear it
    hDrop.innerHTML = '';

    // process results
    if (req.responseText == ''){
        hDrop.style.display = 'none';
    }else {
        hasResults = true;
        var mydate = new Date();
        var myrnd = mydate.getTime();

        results = eval("(" + req.responseText + ")");
        //create the UL
        var ul = document.createElement('ul');
        for (var i=0; i<results.length; i++){
            // create a new li element
            var li = document.createElement('li');
            var a = document.createElement('a');
            a.style.display = 'block';
            //a.style.paddingLeft = '5px';
            //a.style.width = '195px';
            //a.style.color = '#000';
            a.href = '#';
            //ma.id = "id" + myrnd++;
            a.id = results[i].zip;
            a.innerHTML = '<img src=\"/kiosklocator/mapicons/marker_20_white.png\" style="float:left; padding-right: 5px; padding-top:7px;">'+results[i].zip+'<br>'+results[i].city+', '+results[i].state;
            a.onclick = setAndSubmit;
            li.appendChild(a);
            ul.appendChild(li);            
        }        
        var button = document.createElement('div');
        button.style.position = 'absolute';
        button.id = 'button';
        button.appendChild(ul);        
        hDrop.appendChild(button);

        // set position of dropdown & show it
        var lPos = findPos(hText);
        //alert(lPos[1]+','+lPos[0]);
        hDrop.style.top = (lPos[1]+20)+'px';
        hDrop.style.left = lPos[0]+'px';
        //alert(hText.style.width+'-'+hDrop.style.width);
        hDrop.style.display = 'block';
    }
}
//----------------------------------------------------------
// set the value of the textbox and submit the form
//----------------------------------------------------------
function setAndSubmit(){
    hText.value = this.id;
    radius.focus();
    //document.kioskSearchForm.submit();
    return false;
}