﻿var waterMarkZip = 'Search by zip code';
var waterMarkGeography = 'Search by City, State';

function CreateSearchForm(searchForm, appPath)
{    
    searchForm.className = "searchFormMainBox";
    
    searchForm.innerHTML =    
    '<div class="SearchFormTopBox" align="right">' + 
        '<img src="' + appPath + '/Images/moneymailer_coupons_close.gif" alt="close window" class="imgBox" onclick="CloseSearchForm()" />' +
    '</div>' + 
    '<table cellpadding="0" cellspacing="0" border="0" class="searchFormInputTable" width="200">' + 
        '<tr>' +
            '<td class="searchInputCell"><input id="inputZip" value="Search by zip code" class="searchFormInput" onfocus="TextBox_onfocus(inputZip, waterMarkZip)" onblur="TextBox_onblur(inputZip, waterMarkZip)" onkeypress="zipTextBox_onkeypress(event);" /></td>' + 
            '<td class="searchFormButtonCell"><img id="buttonZip" src="'+ appPath + '/Images/coupon_search.gif" alt="search zip" class="searchFormButton" onclick="searchZip_Click()" /></td></tr>' + 
    '</table>' + 
    '<div style="height: 47px"></div>' + 
    '<table cellpadding="0" cellspacing="0" border="0" class="searchFormInputTable" width="200">' +
        '<tr>' +
            '<td class="searchInputCell"><input id="inputGeography" value="Search by City, State" class="searchFormInput" onfocus="TextBox_onfocus(inputGeography, waterMarkGeography)" onblur="TextBox_onblur(inputGeography, waterMarkGeography)" onkeypress="geoTextBox_onkeypress(event);" /></td>' + 
            '<td class="searchFormButtonCell"><img id="buttonGeo" src="' + appPath + '/Images/coupon_search.gif" alt="search city/state" class="searchFormButton" onclick="searchGeo_Click()" /></td></tr>' +
    '</table>';
}

var zipImageButton = document.getElementById("buttonZip");
var geoImageButton = document.getElementById("buttonGeo");

function zipTextBox_onkeypress(e)
{
    if (IsEnterKeyPressed(e))
    {
        searchZip_Click();
    }
}

function geoTextBox_onkeypress(e)
{
    if (IsEnterKeyPressed(e))
    {    
        searchGeo_Click();
    }
}

function IsEnterKeyPressed(e)
{
	return (e.keyCode == 13);
}
   
function TextBox_onblur(TextBox, msg)
{
    if (TextBox.value.length == 0)
    {
        TextBox.value = msg;
    }
}

function TextBox_onfocus(TextBox, msg)
{
    if (TextBox.value.toUpperCase() == msg.toUpperCase())
    {
        TextBox.value = '';
    }    
}

function OpenSearchForm(appPath)
{
    var searchForm = document.getElementById("SearchForm");

    if (searchForm.innerHTML.length == 0)
    {
        CreateSearchForm(searchForm, appPath);
    }            
    searchForm.style.left = GetSearchFormX();
    searchForm.style.top = GetSearchFormY();
    searchForm.style.visibility = "visible";
    
    var shadowDiv = document.getElementById("ShadowDiv");
    shadowDiv.style.visibility = "visible";
    shadowDiv.style.height = document.body.scrollHeight;
    shadowDiv.style.width = document.body.scrollWidth;
    
    //document.body.scroll = "no";
}

function CloseSearchForm()
{
    var searchForm = document.getElementById("SearchForm");
    searchForm.style.visibility = "hidden";

    var shadowDiv = document.getElementById("ShadowDiv");
    shadowDiv.style.visibility = "hidden";
    shadowDiv.style.width = 0;
    shadowDiv.style.height = 0;
}

// search by zip
function searchZip_Click()
{
    var zip = document.getElementById("inputZip").value;
    
    var url = "http://www.hotcoupons.com/cgi-shl/cook/locality?zip=";
    url = url + zip + "&pid=MM"
    document.location.href = url;
}

// search by City, State
function searchGeo_Click()
{
    var geography = document.getElementById("inputGeography").value;

    var city = '';
    var state = '';
    if (geography.length > 0)
    {
        var snagPos = geography.indexOf(",", 0);                                        
        city = Trim(geography.substring(0, snagPos));
        state = Trim(geography.substring(snagPos + 1, geography.length));
    }
    
    var url = "http://www.hotcoupons.com/cgi-shl/cook/locality?st=";
    url = url + state + "&ci=" + city + "&pid=MM";
    document.location.href = url;
}

function ChangeSearchFormCoords()
{        
    var searchForm = document.getElementById("SearchForm");
    
    if (searchForm != null)
    {        
        searchForm.style.left = GetSearchFormX();
        searchForm.style.top = GetSearchFormY();
    }
}

function ChangeCoords()
{        
    ChangeSearchFormCoords();
    
    var shadowDiv = document.getElementById("ShadowDiv");
    var mainTable = document.getElementById("MainTable");
    if (shadowDiv.style.visibility == "visible")
    {
        shadowDiv.style.width = 0;
        shadowDiv.style.height = 0;
        // here we need to force our window to recalculate scroll sizes!
        // ....
        /*shadowDiv.style.width = document.body.scrollWidth;
        shadowDiv.style.height = document.body.scrollHeight;        */
        shadowDiv.style.width = mainTable.clientWidth;
        shadowDiv.style.height = mainTable.clientHeight;
    }
    
}

function Trim(str)
{
    return str.replace(/^\s+|\s+$/g, '');
}

function GetSearchFormX()
{    
    return (document.body.clientWidth - 381 )/ 2 + document.body.scrollLeft;
}

function GetSearchFormY()
{
    return (document.body.clientHeight - 331 )/ 2 + document.body.scrollTop;
}

window.onresize = ChangeCoords;
window.onscroll = ChangeSearchFormCoords;