document.write('<script src="/_js/jquery-1.4.2.min.js" type="text/javascript"></script>');
document.write('<script src="/_js/jquery.jdMenu.js" type="text/javascript"></script>');
document.write('<script src="/_js/jquery.include.js" type="text/javascript"></script>');
document.write('<script src="/_js/jquery.tools.min.js" type="text/javascript"></script>');
document.write('<script src="/_js/ui.tabs.js" type="text/javascript"></script>');
document.write('<script src="/_js/jquery.colorbox-min.js" type="text/javascript"></script>');
document.write('<script src="/_js/jquery.equalheights.js" type="text/javascript"></script>');
document.write('<script src="/_js/accordion.js" type="text/javascript"></script>');
document.write('<script src="/_js/jquery.scrollable-1.0.2.min.js" type=""></script>');
document.write('<script src="/_js/ready.js" type="text/javascript"></script>');
document.write('<script src="/_js/swfobject.js" type="text/javascript"></script>');
document.write('<script src="/_js/highslide/highslide-with-gallery.js" type="text/javascript"></script>');
document.write('<script src="/_js/jquery.bubblepopup.v2.3.1.min.js" type="text/javascript"></script>');

// get date
function initArray() {
 for(i=0;i<initArray.arguments.length; i++)
  this[i] = initArray.arguments[i];
}
var isnMonths=new initArray("January","February","March","April","May","June","July","August","September","October","November","December");

today=new Date();
hrs=today.getHours();
min=today.getMinutes();
sec=today.getSeconds();
clckh=""+((hrs>12)?hrs-12:hrs); 
clckm=((min<10)?"0":"")+min;
clcks=((sec<10)?"0":"")+sec;
clck = (hrs >= 12) ? "p.m." : "a.m.";

var lastLocationSearch = {
    'jqueryDomNode': undefined
};

var zipCodeWarningBubbleConfig = {
    position: 'top',
    align: 'center',
    innerHtml: 'Please enter a 5 digit zip code.',
    innerHtmlStyle: {
        color: '#FFFFFF',
        'text-align': 'center'
    },
    themeName: 'all-black',
    themePath: '../images/jquerybubblepopup-theme'
};

var zipCodeOrCityNameWarningBubbleConfig = {
    position: 'top',
    align: 'center',
    innerHtml: "Enter a 5 digit zip code or a city in the form 'City, ST'<br />with a valid state code",
    innerHtmlStyle: {
        color: '#FFFFFF',
        'text-align': 'center'
    },
    themeName: 'all-black',
    themePath: '../images/jquerybubblepopup-theme'
};

var stnr="";
var ns="0123456789";
var a="";

//get year
function getFullYear(d) { 
 yr = d.getYear();
 if (yr < 1000)
  yr+=1900;
 return yr;
}
var footer = (' Tire Factory, Inc. 555 Mocking Bird Lane, Portland Oregon, 09809, USA')

/**
    this is a special case of the .zipCodeValidationNeeded code that executes just for the front page
    'find a location near you' CTA.  Need to make refactor concurrent with CMS HTML changes to make the CTA use the other code.
    Also have to be careful of the document.location.href assignment, that's not something done in the more generic code below
**/

function zipValidation(toValidate) {
    var result = /^\s*\d{5}\s*$/.test(toValidate);
    //    console.log("does '" + toValidate + "' match the pattern (zip validation): '" + result + "'");
    return result;
}

function isZipEnter(inField, e) {

    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.location.href = '/locations/?search=' + document.getElementById('txtLocationCTA').value; 
       /*
       if (zipValidation(document.getElementById('txtLocationCTA').value)) 
       {
        document.location.href = '/locations/?search=' + document.getElementById('txtLocationCTA').value; 
        }
        else 
        {
            if (!$('#txtLocationCTA').HasBubblePopup()) {
                $('#txtLocationCTA').CreateBubblePopup(warningBubblePopupConfig);
            }
           $('#txtLocationCTA').ShowBubblePopup();
        }*/
        return false;
    }
    /*else 
    {
        if ($('#txtLocationCTA').HasBubblePopup()) {
            $('#txtLocationCTA').RemoveBubblePopup();
        }
   
    }*/
    return true ;
}


function stateCodeValidation(toValidate) {
    var stateList = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA',
                     'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN',
                     'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'];
    var trimmed = toValidate.trim()
    var stateCode = trimmed.slice(trimmed.length - 2, trimmed.length);
    for (var i = 0; i < stateList.length; i++) {
        if (stateCode.toUpperCase() === stateList[i]) {
            return true;
        }
    }
    return false;
}

function zipCodeOrCityNameValidation(toValidate) {
    var result = zipCodeValidation(toValidate);
    if (result) {
        return true;
    }
    result = /^\s*(\w+\s*)*\w+,\s*\w{2}\s*$/.test(toValidate); // validates ' City Name, ST '
    result = result && stateCodeValidation(toValidate);
    return result;
}

function isEmailEnter(inField, e) {
    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        document.location.href = '/newsletter-signup/?email=' + document.getElementById('txtEmailCTA').value;
        return false;
    }
    return true;
}

/**
    This function manages the warning bubbles for validation
    Takes a validation function to test the value against
    Note that it uses lastLocationSearch.jqueryDomNode, so you have to set that properly elsewhere
**/
function manageValidationBubble(validationFunction, bubblePopupConfig) {
    var currentNode = lastLocationSearch.jqueryDomNode;
    var currentValue = currentNode.val();

    if (validationFunction(currentValue)) {
        return true; // returning true continues the submit
    } else {
        if (!currentNode.HasBubblePopup()) {
            currentNode.CreateBubblePopup(bubblePopupConfig);
        }
        currentNode.ShowBubblePopup();
        return false; // returning false halts the submit
    }
}

$(document).ready(function () {

    $('.zipCodeValidationNeeded').closest('form').submit(function () {
        if (lastLocationSearch !== undefined && lastLocationSearch.jqueryDomNode !== undefined) {
            if (lastLocationSearch.jqueryDomNode.hasClass('zipCodeValidationNeeded')) {
                return manageValidationBubble(zipCodeValidation, zipCodeWarningBubbleConfig);
            }
        }
    });

    $('.zipCodeOrCityValidationNeeded').closest('form').submit(function () {
        if (lastLocationSearch !== undefined && lastLocationSearch.jqueryDomNode !== undefined) {
            if (lastLocationSearch.jqueryDomNode.hasClass('zipCodeOrCityValidationNeeded')) {
                return manageValidationBubble(zipCodeOrCityNameValidation, zipCodeOrCityNameWarningBubbleConfig);
            }
        }
    });

    $('.zipCodeValidationNeeded, .zipCodeOrCityValidationNeeded').keydown(function (eventObject) {
        lastLocationSearch.jqueryDomNode = $(this);

        if (lastLocationSearch.jqueryDomNode.HasBubblePopup()) {
            lastLocationSearch.jqueryDomNode.RemoveBubblePopup();
        }
    });

    $(window).scroll(function (eventObject) {
        var mapDiv = $('.positioned-map');
        if (mapDiv[0] !== undefined) {
            var offset = mapDiv.offset();
            var cssChangePoint = 374 - 100; // mapTop y coord - 100px buffer
            var windowPageHeight = $(window).scrollTop();
            if (windowPageHeight > cssChangePoint) {
                if (mapDiv.css('position') !== 'fixed') {
                    mapDiv.css({
                        'position': 'fixed',
                        'top': '100px',
                        'left': offset.left + 'px',
                        //                    'left': '926px',
                        'right': '0px'
                    });
                }
            } else {
                if (mapDiv.css('position') !== 'absolute') {
                    mapDiv.css({
                        'position': 'absolute',
                        'top': '65px',
                        'left': '400px'
                    });
                }
            }
        }
    });

    
});
