﻿function showHideOtherSearch(hide) {
    if (hide)
        $('#other-search-wrapper').css('visibility', 'hidden');
    else
        $('#other-search-wrapper').css('visibility', 'visible');

    return false;
}

function ShowAvailable() {
    //window.open('http://gift-dev4.restaurant.com/available.asp', 'listings', 'scrollbars=yes,resizable=yes,width=760,height=400,left=600,top=500,screenX=600,screenY=500');
    NewWindow("/available.asp", "name", "760", "400", "yes");
}

function writesc_video() {
    WriteSCTrackingCode("", "HPv1: Watch Video", "", "");
    NewWindow("/includes/listing_video.asp", "name", "580", "360", "no");

    return false;
}

var win = null;
function NewWindow(mypage, myname, w, h, scroll) {
    LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
    TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
    settings =
        'location=0,height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',resizable,addressbar=no'

    win = window.open(mypage, myname, settings)
}

function validatebuydmm(form) {
    var mquantity;
    var mquantityLength;
    var mquantityDummy;

    var mvalueindex;

    var i;
    var j;



    mvalueindex = form.SKU.selectedIndex;
    mquantity = form.txtquantity.value;
    mquantityLength = String(mquantity).length;




    //Validate Mega Cert Quantity
    if (mquantityLength == 0) {
        alert("Please enter quantity of club to buy.");
        return false;
    }


    for (j = 0; j < String(mquantity).length; j++) {
        if ((String(mquantity).charAt(j) < "0") || (String(mquantity).charAt(j) > "9")) {
            alert("Please enter a valid quantity of club.");
            return false;
        }
    }

    if (mquantity > 500) {
        alert("Please enter a valid quantity of club to buy. (maximum 500)");
        return false;
    }

    if (mvalueindex < 1) {
        alert("Please select an option from the drop down menu.");
        return false;
    }

    return true;
}

function validatebuyCard(form) {
    var mquantity;
    var mquantityLength;
    var mquantityDummy;

    var mvalueindex;

    var i;
    var j;

    mvalueindex = form.SKU.selectedIndex;
    mquantity = form.quantity.value;
    mquantityLength = String(mquantity).length;

    //Validate Mega Cert Quantity
    if (mquantityLength == 0) {
        alert("Please enter quantity of club to buy.");
        return false;
    }

    for (j = 0; j < String(mquantity).length; j++) {
        if ((String(mquantity).charAt(j) < "0") || (String(mquantity).charAt(j) > "9")) {
            alert("Please enter a valid quantity of club.");
            return false;
        }
    }

    if (mquantity > 500) {
        alert("Please enter a valid quantity of club to buy. (maximum 500)");
        return false;
    }

    if (mvalueindex < 1) {
        alert("Please select an option from the drop down menu.");
        return false;
    }

    return true;
}

function validatebuymegacert(form) {
    var mquantity;
    var mquantityLength;
    var mquantityDummy;

    var mvalueindex;

    var i;
    var j;



    mvalueindex = form.SKU.selectedIndex;
    mquantity = form.txtquantity.value;
    mquantityLength = String(mquantity).length;




    //Validate Mega Cert Quantity
    if (mquantityLength == 0) {
        alert("Please enter quantity of megacerts to buy.");
        return false;
    }


    for (j = 0; j < String(mquantity).length; j++) {
        if ((String(mquantity).charAt(j) < "0") || (String(mquantity).charAt(j) > "9")) {
            alert("Please enter a valid quantity of megacerts.");
            return false;
        }
    }
    if (mvalueindex < 1) {
        alert("Please select a gift certificate amount from the pull down menu.");
        return false;
    }

    return true;
}

function checkForZip(form) {
    var i;
    var j;
    var ZipCode;
    var ZipCodeLength;
    ZipCode = form.zip.value;
    ZipCodeLength = String(ZipCode).length;

    //Validate the ZipCode
    if (ZipCodeLength == 0) {
        alert("Please enter zipcode to search for restaurants.");
        return false;
    }

    if (String(ZipCode).length != 5) {
        alert("Please enter zipcode to search for restaurants.");
        return false;
    }


    for (j = 0; j < String(ZipCode).length; j++) {
        if ((String(ZipCode).charAt(j) < "0") || (String(ZipCode).charAt(j) > "9")) {
            alert("Please enter zipcode to search for restaurants.");
            return false;
        }
    }
    document.cookie = 'Focuscookie=1';
    /*  
    var expireDate = new Date();
    expireDate.setHours(expireDate.getHours()-1);  
    document.cookie = 'FocusVar=1; path=/; expires=' + expireDate.toGMTString();  
    */

    WriteSCTrackingCode('', 'HPv1:Search & Buy Now', '', '');
    return true;
}


function WriteSCTrackingCode(prop7, prop14, img, desc) {
    var dvServer = s_account;
    if (dvServer == null || dvServer == '')
        dvServer = "resdev";

    desc = 'Home Page Clicked';
    var s = s_gi(dvServer);

    if ((prop7 != null && prop7 != '') && (prop14 != null && prop14 != '')) {
        s.linkTrackVars = "prop7,prop14";
        s.prop7 = prop7;
        s.prop14 = prop14;
    }
    if ((prop7 != null && prop7 != '') && (prop14 == null || prop14 == '')) {
        s.linkTrackVars = "prop7";
        s.prop7 = prop7;
    }
    if ((prop14 != null && prop14 != '') && (prop7 == null || prop7 == '')) {
        s.linkTrackVars = "prop14";
        s.prop14 = prop14;
    }


    s.tl(img, 'o', desc);
    s.prop14 = null;
    SleepForMilliseconds(500);
}

function SleepForMilliseconds(sleeptimeinmilliseconds) {
    var sleeping = true;
    var now = new Date();
    var alarm;
    var startingMSeconds = now.getTime();
    //alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + sleeptimeinmilliseconds + " ms"); 
    while (sleeping) {
        alarm = new Date();
        alarmMSeconds = alarm.getTime();
        if (alarmMSeconds - startingMSeconds > sleeptimeinmilliseconds) { sleeping = false; }
    }
    //alert("Wakeup!");
}

function submitForm() {
    document.frmEnter.submit();
}

function Zip_Click(zipval) {
    document.frmEnter.zip.value = zipval;
    WriteSCTrackingCode('', 'HPv1:Search & Buy Now', '', '');
    document.frmEnter.submit();
}

function selectState(ID, StateName) {
//    document.form[0].StateID.value = ID;
    document.frmEnter.action = "/search-map.asp?pgn=gcp&StateID=" + ID;
    WriteSCTrackingCode('', 'HPv1:State search - ' + StateName, '', '');
    submitForm();
}

function selectCity(ID) {
    //				alert(ID + " is selected");
    WriteSCTrackingCode('', 'index:L-City search', '', '');
    //document.frmEnter.action = 
}



var focusVar = 0;

//*******************************************************************************************************************/
//    inside code is used to detect browser back button.
//*******************************************************************************************************************/

// Cookie's name may be changed.
var CookieName = "tempFocus";

// Change number of seconds if needed.
var PageLoadTimeSeconds = 1;

// Replace alert(...) in function HandleNewFocus(), below, 
//    with your custom "'back' button was clicked" handling.
function HandleNewFocus() {
    //alert(document.location.host  + document.location.pathname);  


    var SearchCookie = readCookie('FocusCookie')

    if (SearchCookie) {
        if (parseInt(SearchCookie) == 0) {
            document.cookie = 'FocusCookie=1';
            focusVar = 1;
            //	   alert('Back Button clicked:'+ readCookie('FocusCookie'));
            var zipBox;
            zipBox = document.getElementsByName("zip")[0];
            zipBox.value = "Enter ZIP Code";
            zipBox.blur();
            zipBox.value = "Enter ZIP Code";
            //	   document.all("container").blur();
            //	   document.all("container").click();
        }
    }


    if (navigator.userAgent.indexOf("Safari") == -1) {
    }
    else {

        document.location.href = document.location.pathname;

        /*    
        var SearchCookie = readCookie('RecentSearchZips')
        if (SearchCookie)
        {
        if (SearchCookie.length == 5)
        { 
        document.location.href = document.location.pathname;
        //	    alert(SearchCookie); 
        }
        else
        {
        if (document.all("zip"))
        { 
        document.all("zip")focus();
        document.all("zip")blur();
        }
        }    
        }
        */
    }
}

// No customizations required in the rest of this JavaScript.

var now = new Date();
var Milliseconds = parseInt(now.getTime() / 1000);
var CurrentCookie = new String();
var FocusLaunched = false;
var BackHandled = false;

function HandleCookie() {
    if (BackHandled) { return; }
    BackHandled = true;
    var beforemillis = parseInt(CurrentCookie);
    if (beforemillis > 1) {
        var aftermillis = Milliseconds - beforemillis;
        if (aftermillis > PageLoadTimeSeconds) { HandleNewFocus(); }
    }
    else { SetCookie('loaded'); }
}

function LoadFunctions() {
    CurrentCookie = GetCookie();
    if (CurrentCookie == 'loaded') { return; }
    HandleCookie();
    SetCookie('loaded');
}

function Loaded() { LoadFunctions(); }

function Unloaded() {
    var again = new Date();
    Milliseconds = parseInt(again.getTime() / 1000);
    SetCookie(String(Milliseconds));
}

function Focused() {
    if (FocusLaunched) { return; }
    FocusLaunched = true;
    LoadFunctions();
}

function SetCookie(v) {
    CurrentCookie = v;
    document.cookie = CookieName + '=' + v;
}

function GetCookie() {
    var c_content = '';
    if (document.cookie.length > 0) {
        var c_name = CookieName + '=';
        var c_begin = document.cookie.indexOf(c_name);
        var c_end = 0;
        if (c_begin > -1) {
            c_begin += c_name.length;
            c_end = document.cookie.indexOf(";", c_begin);
            if (c_end < c_begin) { c_end = document.cookie.length; }
            c_content = document.cookie.substring(c_begin, c_end);
        }
    }
    return c_content;
}

// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (readCookie(name))
        document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

window.onload = Loaded;
window.onunload = Unloaded;
window.onfocus = Focused;


//*******************************************************************************************************************/
//				End of code of browser back button detection
//*******************************************************************************************************************/


var status = 0;

//*******************************************************************************************************************/
//			function to clear text onfocus event of inputBox and restore onblur event of inputBox 
//*******************************************************************************************************************/
function clrTxt(oElem, oEvent) {
    //			alert('event type:'+oEvent.type +' oElem.value :'+ oElem.value.len);


    if (oElem && oElem.value && oElem.defaultValue && oEvent && oEvent.type) {
        if (oEvent.type == "focus" && oElem.value == oElem.defaultValue) {
            oElem.setAttribute('maxLength', 5);
            var SearchCookie = readCookie('FocusCookie');
            //			alert('SearchCookie:'+SearchCookie + ' oEvent.type:'+oEvent.type + ' focusVar: ' + focusVar);
            if (SearchCookie) {
                //				alert('focus button entered :'+SearchCookie);
                if (parseInt(SearchCookie) == 1) {
                    Delete_Cookie('FocusCookie', '/', '');
                    if (navigator.userAgent.indexOf("MSIE") != -1) {
                        //			            alert('focus:'+ focusVar);
                        if (focusVar == 1) {
                            oElem.setAttribute('maxLength', 15);
                            oElem.value = 'Enter ZIP Code';
                            oElem.blur();
                        }
                        else {
                            focusVar = 0;
                            oElem.value = '';
                        }
                    }
                    else if (navigator.userAgent.indexOf("Firefox") != -1) {
                        oElem.value = '';
                    }
                    else {
                        oElem.value = '';
                    }
                }
                else {
                    //					document.cookie = 'FocusCookie=0';
                    oElem.value = '';
                }
            }
            else {
                //               document.cookie = 'FocusCookie=0';
                oElem.value = '';
            }
        }
        else if (oEvent.type == "blur") {

            if (oElem.value.len == 0 || oElem.value == 'undefined' || isNaN(oElem.value)) {
                oElem.value = oElem.defaultValue;
                oElem.setAttribute('maxLength', 15);
                oElem.value = "Enter ZIP Code";
                window.setInterval("delay()", 500);
            }
            //			 alert('blur');
        }
        else {
            //		 alert('hello else');
        }
    }
    else {
        oElem.value = '';
    }
}
function delay() {
}
//*******************************************************************************************************************/

function readCookie(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 ZipKeyUp() {
    document.cookie = 'FocusCookie=0';
}


//--state
function showHideStates() {
    if (document.getElementById('state_selector').style.visibility == 'visible') {
        hideStates();
    }
    else showStates();
}

function showStates() {
    document.getElementById('state_selector').style.visibility = 'visible';
}

function hideStates() {
    document.getElementById('state_selector').style.visibility = 'hidden';
}
//--city
function showHideCities() {
    if (document.getElementById('city_selector').style.visibility == 'visible') {
        hideCities();
    }
    else showCities();
}

function showCities() {
    document.getElementById('city_selector').style.visibility = 'visible';
}

function hideCities() {
    document.getElementById('city_selector').style.visibility = 'hidden';
}

