﻿function redirectToHash() {
    var url = self.location.href.toString().toLowerCase();
    if (url.match('#') != '#') {
        if (url.lastIndexOf("/") != (url.length - 1))
            url += "/";
        url = url.replace("http://" + self.location.host + "/", "");
        var parts = url.split("/");
        if (url.match('locator') == 'locator' && parts.length > 2) {
            self.location.href = 'http://' + self.location.host + '/' + parts[0] + '/#&l=' + parts[1]
        } else if (parts.length > 2) {
            var newurl = 'http://' + self.location.host + '/' + parts[0] + '/#&m=/' + cleanseUrl(url) + (url.indexOf('legend') != -1 ? '&s=' + parts[1] : '');
            self.location.href = newurl;
        }
    }
}


function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

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 eraseCookie(name) {
    createCookie(name, "", -1);
}


function gaTrackClick(href) {
    _gaq.push(['_trackPageview', href]);
}

function cleanseUrl(url) {
    url = url.toLowerCase();
    url = url.replace("legend/", "partial/").replace("boots/", "detail/");
    return url;
}

function checkKey(e, k) {
    return (e.keyCode || e.which) == 13;
}

function clearDefault(obj) {
    if ($(obj).attr('def') != 'undefined' && $(obj).val() == $(obj).attr('def'))
        $(obj).val('').removeClass('default');
}

function replenishDefault(obj) {
    if ($(obj).attr('def') != 'undefined' && $(obj).val() == '')
        $(obj).val($(obj).attr('def')).addClass('default');
}

function validateEmail(mail) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.exec(mail) != null;
}

function validate(obj, valid) {
    if (valid)
        $(obj).removeClass('error');
    else
        $(obj).addClass('error');
}

function openSendToPhone(obj) {
    $('.modal-small, #modal-small-screen').remove();
    var id = '';
    if ($(obj).attr('prod') != undefined) {
        id = $(obj).attr('prod');
    } else if ($(obj).closest('ul').parent().data('loc') != undefined) {
        var l = $(obj).closest('ul').parent().data('loc');
        id = l.Address1 + (l.Address2 != '' ? ' ' + l.Address2 : '') + ' ' + l.City + ', ' + l.State + ' ' + l.PostalCode;
    }

    $.get('/boots/SMSSend/' + id, function (data) {
        $('body').append('<div id="modal-small-screen" style="filter: alpha(opacity=0); opacity:0;" />');
        $('body').append(data);
        $('.modal-small .modal-close, #modal-small-screen').bind('click', closeSmallModal);
        $('#phoneSubmit').bind('click', validateSendToPhone);
        $('#sendToPhone input').bind('keyup', function () {
            if ($(this).val().length == $(this).attr('maxlength') && $(this).next('input').length != 0)
                $(this).next('input').focus();
        });
        adjustSmallModal();
        $(window).bind('resize', adjustSmallModal);
        $('#sendToPhone').fadeTo(500, 1, function () { $(this).css({ filter: 'none', zoom: 1 }); });
        $('#modal-small-screen').fadeTo(500, 0.8);
    });
}

function validateSendToPhone() {
    var area = $('input#AreaCode');
    var pre = $('input#Prefix');
    var suff = $('input#Number');

    logMessage(!isNaN(area.val()));

    validate(area, (area.val().length == 3 && !isNaN(area.val())));
    validate(pre, (pre.val().length == 3 && !isNaN(pre.val())));
    validate(suff, (suff.val().length == 4 && !isNaN(suff.val())));

    if ($('#sendToPhone .error').length == 0) {
        $('#phoneAjaxForm').submit();
    }
}

function closeSmallModal() {
    $(window).unbind('resize', adjustSmallModal);
    $('.modal-small, #modal-small-screen').fadeTo(500, 0, function () { $('.modal-small, #modal-small-screen').remove(); });
}

function adjustSmallModal() {
    $('.modal-small').css({ left: findX($('.modal-small'), true), top: findY($('.modal-small')) });
}

function phoneSuccessForm() {
    $("#phoneForm").hide();
    $("#phoneThankyou").show();
}
function phoneFailure() {
    $("#phoneForm").hide();
    $("#phoneFailure").show();
}

function validateContactUs() {
    var fromName = $('#fromName');
    var fromEmail = $('#fromEmail');
    var message = $('textarea#message');

    validate(fromName, (fromName.val() != '' && fromName.val() != fromName.attr('def')));
    validate(fromEmail, validateEmail(fromEmail.val()));
    validate(message, message.val() != '');

    if ($('#contactForm .error').length == 0) {
        $('#contactForm').submit();
    }
}


function validateLaces() {
    var fromName = $('#fromName');
    var address = $('#address');
    var city = $('#city');
    var state = $('#statename');
    var zip = $('#zip');
    var country = $('#country');
    var styleNumber = $('#styleNumber');
    var email = $('#email');


    validate(fromName, (fromName.val() != '' && fromName.val() != fromName.attr('def')));
    validate(address, (address.val() != '' && address.val() != address.attr('def')));
    validate(city, (city.val() != '' && city.val() != city.attr('def')));

    validate(state, (state.val() != '--Select State-' && state.val() != state.attr('def')));
    validate(zip, (zip.val() != '' && zip.val() != zip.attr('def')));
   // validate(country, (country.val() != '' && country.val() != country.attr('def')));
    validate(styleNumber, (styleNumber.val() != '' && styleNumber.val() != styleNumber.attr('def')));
    validate(email, validateEmail(email.val()));


    if ($('#lacesForm .error').length == 0) {
        $('#lacesForm').submit();
    }
}



function findX(modal, instant) {
    instant = (typeof instant == "undefined" ? false : instant);
    var w = modal.outerWidth();
    var ww = $(window).width();
    return (ww - w) / 2;
}

function findY(modal) {
    var h = modal.outerHeight();
    var wh = $(window).height();
    return (wh - h) / 2;
}

var ot = '';
function setPageTitle(title) {
    if (ot == '')
        ot = document.title;
    document.title = title;
}

//debug
function logMessage(msg) {
    if (typeof console != 'undefined' && typeof console.log != 'undefined')
        console.log(msg);
}

