﻿var menuId = '';
var lastVisible = null;
var menuFrame = null;
var hideTimeout = 100;
var menuHide = null;
var leftMapTip = null;
var rightMapTip = null;
var tipHide = null
var tipIcon = null;
var tipIconZindex = 0;

//var leftInnerText = null;
function showSubMenu(currentEl)
{
    if(currentEl!=null)
    {
        if(lastVisible!=null && lastVisible.id != currentEl.id)
        {
            menuId = lastVisible.id;
            HideHeaderCategories();
        }
        menuId = currentEl.id.replace('main-','');
        var headerSubCateg = document.getElementById(menuId);
        if(headerSubCateg)
        {
            lastVisible = headerSubCateg;
            currentEl.onmouseout = function() { HideTimeout(); }
            headerSubCateg.className = headerSubCateg.className.replace("hideDiv","");
            if(IsIE6() && menuFrame != null)
            {
                menuFrame.style.width = headerSubCateg.offsetWidth-5;
                menuFrame.style.height = headerSubCateg.offsetHeight-46;
                menuFrame.style.left = headerSubCateg.offsetLeft+2+'px';
                menuFrame.style.top = headerSubCateg.offsetTop+2+'px';                    
                menuFrame.className = menuFrame.className.replace('hideDiv',' ');
                if(menuFrame.parentElement != headerSubCateg.parentElement)
                {
                    menuFrame.parentElement.removeChild(menuFrame);
                    headerSubCateg.parentElement.appendChild(menuFrame);
                }
            }
            //adding out functions to title, description and footer divs
            AddMenuEvents(headerSubCateg);
        }
    }        
}
function AddMenuEvents(control)
{
    if(control)
    {
        control.onmouseover = function()
        {
            CancelHideTimeout();
        }
        control.onmouseout = function()
        {
            HideTimeout();
        }
    }
}
function CancelHideTimeout()
{
    if(menuHide)
	{
		window.clearTimeout(menuHide);
		menuHide = null;
	}

}
function HideTimeout()
{
    menuHide = window.setTimeout(HideHeaderCategories, hideTimeout);
}
function HideHeaderCategories()
{
    if(menuId != '')
    {
        HideUsingClass(menuId);
    }
    if(IsIE6())
    {
        if(menuFrame!=null)
        {
            menuFrame.className = menuFrame.className.replace('hideDiv',' ')+' hideDiv';
        }
    }
}
//check for internet explorer 6
function IsIE6()
{
    var ie6 = false;
    if(navigator.appVersion.indexOf('MSIE 6.0;') > 0 && navigator.appName=="Microsoft Internet Explorer")
    {
        ie6 = true;
    }
    return ie6;
}
function HideUsingClass(containerId)
{
    var el = document.getElementById(containerId);
    if(el)
    {
        el.className = el.className.replace("hideDiv","").replace("showDiv","") + " hideDiv ";
    }
}
function ShowUsingClass(containerId)
{
    var el = document.getElementById(containerId);
    if(el)
    {
        el.className = el.className.replace("hideDiv","showDiv");
    }
}
function CancelTipTimeout()
{
    if(tipHide)
    {
        window.clearTimeout(tipHide);
		tipHide = null;
    }
}
function TipTimeout()
{
    tipHide = window.setTimeout(ResetTip, hideTimeout);
}
function ResetTip()
{
    if(tipIcon)
    {
        tipIcon.style.zIndex = tipIconZindex;
        if(leftMapTip)
        {
            leftMapTip.className = leftMapTip.className.replace('hideDiv','')+' hideDiv';
        }
        if(rightMapTip)
        {
            rightMapTip.className = rightMapTip.className.replace('hideDiv','')+' hideDiv';
        }            
    }
}

function CreateMapTip(sourceElement, position, text)
{
    if(sourceElement)
    {
        if(tipIcon)
        {
            if(sourceElement.className != tipIcon.className)
            {
                ResetTip();
            }
        }
        tipIconZindex = sourceElement.style.zIndex;
        if(position == 'left')
        {
            if(leftMapTip!=null)
            {
                var links = leftMapTip.getElementsByTagName('a');
                links[0].innerHTML = text;
                links[0].href = sourceElement.href;
                links[0].title = sourceElement.title;
                leftMapTip.className = leftMapTip.className.replace('hideDiv','');
            }
            else
            {
                //debugger
                //create main container
                leftMapTip = document.createElement('div');
                leftMapTip.className = 'leftTip';
                
                var tipLink = document.createElement('a');
                tipLink.innerHTML = text;
                tipLink.setAttribute('href',sourceElement.href);
                tipLink.setAttribute('title',sourceElement.title);
                leftMapTip.appendChild(tipLink);
                
                leftMapTip.style.position = 'absolute';
                sourceElement.parentNode.appendChild(leftMapTip);
            }
            leftMapTip.style.top = sourceElement.offsetTop+2+'px';
            leftMapTip.style.left = sourceElement.offsetLeft-(text.length*8+17)+'px';
            leftMapTip.style.zIndex = 99;
            leftMapTip.style.width = text.length*8+20+'px';
            leftMapTip.onmouseover = function() { CancelTipTimeout(); }
            leftMapTip.onmouseout = function() { TipTimeout(); }
            sourceElement.style.zIndex = 100;
        }
        else
        {
            if(rightMapTip!=null)
            {
                var links = rightMapTip.getElementsByTagName('a');
                links[0].innerHTML = text;
                links[0].href = sourceElement.href;
                links[0].title = sourceElement.title;
                rightMapTip.className = rightMapTip.className.replace('hideDiv','');
            }
            else
            {
                //debugger
                //create main container
                rightMapTip = document.createElement('div');
                rightMapTip.className = 'rightTip';
                
                var tipLink = document.createElement('a');
                tipLink.innerHTML = text;
                tipLink.setAttribute('href',sourceElement.href);
                tipLink.setAttribute('title',sourceElement.title);
                rightMapTip.appendChild(tipLink);
                
                rightMapTip.style.position = 'absolute';
                sourceElement.parentNode.appendChild(rightMapTip);
            }
            rightMapTip.style.top = sourceElement.offsetTop+2+'px';
            rightMapTip.style.left = sourceElement.offsetLeft+15+'px';
            rightMapTip.style.zIndex = 99;
            rightMapTip.style.width = text.length*8+20+'px';
            rightMapTip.onmouseover = function() { CancelTipTimeout(); }
            rightMapTip.onmouseout = function() { TipTimeout(); }
            sourceElement.style.zIndex = 100;
        }
        tipIcon = sourceElement;
    }
}
function Expand(expander)
{   
    var container = document.getElementById(expander.id.replace('Expander','')+'Container');
    if(container)
    {   
        container.className = container.className.replace('hideDiv','');
    }
}
function ValidateInput()
{   
    var firstName = document.getElementById("FirstName");
    var lastName = document.getElementById("LastName");
    var email = document.getElementById("email");
    var businessName = document.getElementById("BusinessName");
    var phone = document.getElementById("PhoneNumber");
    var timeToCall = document.getElementById("BestTimeToCall");
    var errors = '';
    if(firstName && firstName.value =='')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "First Name - is required";
    }
    /*if(lastName && lastName.value =='')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "Last Name - is required";
    }
    if(email)
    {
        if(email.value =='')
        {
            if (errors!='')
            {
                errors += "<br />";
            }
            errors += "Email Address - is required";
        }
        else
        {
            if(!ValidateEmail(email.value))
            {
                if (errors!='')
                {
                    errors += "<br />";
                }
                    errors += "Email Address - is not valid";
                }
        }
    }
    if(businessName && businessName.value =='')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "Business Name - is required";
    }*/
    if(phone && phone.value =='')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "Phone Number - is required";
    }
    /*if(timeToCall && timeToCall.value == '-1')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "Best Time To Call - is required";
    }*/
    if(errors!='')
    {
        var formHolder = document.getElementById("formHolder");
        if(formHolder)
        {
            formHolder.className = "floatleft contactErrors";
        }
        var errorDiv = document.getElementById("errorDiv");
        if(errorDiv)
        {
            errorDiv.innerHTML="<p class=\"errors\">"+errors+"</p>";
            return false;
        }
    }
    else
    {
        return true;
    }
}
function ValidateEmail(address) 
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(address) == false) 
    { 
        return false;
    }
    else
    {
        return true;
    }
}
function SetMapDefault()
{
    $('#USAmapSample').attr('src','http://static.wilkinsfitness.com/Images/mapSample.jpg');
}
function SetHoverMap(mapCode)
{
    if(mapCode != '' )
    {
        $('#USAmapSample').attr('src','http://static.wilkinsfitness.com/Images/Map/mapSample-'+mapCode+'.jpg');
    }
}
function SetStatesMap()
{
    $(document).ready(function(){
        $('.popoverMain').width($(document).width());
        $('.popoverMain').height($(document).height());
        $('#showMap').click(function(){
            var windowSize = $(window).height();
            var topMargin = (windowSize - 640)/2+$(window).scrollTop();
            $('#mapInner').css('margin-top',topMargin);
            if(IsIE6())
            {
                $('select').hide();
            }
            $('#mapPopover').fadeIn();
            return false;
        });
        $('#mapClose').click(function(){
            if(IsIE6())
            {
                $('select').show();
            }
            $('#mapPopover').fadeOut();                
        });
        $('map#usaMap > area').mouseout(function(){
            UnTip();
            SetMapDefault();            
        });
    });        
}
/*Reference Menu*/
var refTimeout = 100;
var refMenu = null;
function CancelReferenceHideTimeout()
{
    if(refMenu)
    {
	    window.clearTimeout(refMenu);
	    refMenu = null;
    }
}
function ReferenceHideTimeout()
{
    refMenu = window.setTimeout(HideReferenceMenu, refTimeout);
}
function HideReferenceMenu()
{
    $('#referencesPopover').hoverFlow('mouseout').slideUp('slow');
    $('#topReferenceLink').removeClass('topLinksActive');
    $('#topReferenceLink').addClass('topLinks');
    CancelReferenceHideTimeout();
}
function ShowReferenceMenu()
{   
    $('#topReferenceLink').removeClass('topLinks');
    $('#topReferenceLink').addClass('topLinksActive');
    $('#referencesPopover').hoverFlow('mouseover').slideDown('slow');
}
function SetReferenceMenu()
{
     $(document).ready(function(){
        $('#topReferenceLink').hover(function(e){
            CancelReferenceHideTimeout(); 
            ShowReferenceMenu();               
            },function(e){
                ReferenceHideTimeout();
        });
        
        $('#referencesPopover').mouseover(function(){
            CancelReferenceHideTimeout();
        });
        $('#referencesPopover').mouseout(function(){
            ReferenceHideTimeout();
        });
        
        $('#footerReferenceLink').click(function(e){
            CancelReferenceHideTimeout(); 
            ShowReferenceMenu();               
        });
        
     });
}
/*End Reference Menu*/
/*Locations Hover*/
var locationsTimeout = 100;
var locationsHide = null;
function CancelLocationsHideTimeout()
{
    if(locationsHide)
    {
        window.clearTimeout(locationsHide);
        locationsHide = null;
    }
}
function LocationsHideTimeout()
{
    locationsHide = window.setTimeout(HideLocationsMenu, locationsTimeout);
}
function HideLocationsMenu()
{
    $('#locationsPopover').hoverFlow('mouseout').slideUp('slow');
    $('#headerLocations').removeClass('topGrayBtnActive');
    $('#headerLocations').addClass('topGrayBtn');
    /*$('#locationsRight').removeClass('right');
    $('#locationsRight').addClass('rightBorder');*/
    CancelLocationsHideTimeout();
}
function ShowLocationsMenu()
{   
    $('#headerLocations').removeClass('topGrayBtn');
    $('#headerLocations').addClass('topGrayBtnActive');
    /*$('#locationsRight').removeClass('rightBorder');
    $('#locationsRight').addClass('right');*/
    $('#locationsPopover').hoverFlow('mouseover').slideDown('slow');
}
function SetLocationsHover()
{
    $(document).ready(function(){
        $('#headerLocations').hover(function(e){
            CancelLocationsHideTimeout(); 
            ShowLocationsMenu();               
            },function(e){
                LocationsHideTimeout();
        });
        
        $('#locationsPopover').mouseover(function(){
            CancelLocationsHideTimeout();
        });
        $('#locationsPopover').mouseout(function(){
            LocationsHideTimeout();
        });
    });    
}
/*End Locations Hover*/
/*JS Errors*/
function SetJsErrors(errorList)
{
    if(errorList && errorList.length > 0)
    {
        if($('#serverMsg2').length > 0)
        {
            $('#serverMsg2').remove();
        }
        if($('#serverErr2').length == 0)
        {
            //we need to build the error container
            var errorContainer = '<div id="serverErr2" style="width:900px;" class="errorZone"><p>We were unable to process your request. Please check the following issues, and then try again.</p>';
            errorContainer+='<ul id="serverErr2UL">';
            for(var i=0; i<errorList.length;i++)
            {
                errorContainer+='<li>'+errorList[i]+'</li>';
            }
            errorContainer+='</ul></div>';
            $(errorContainer).insertBefore('#page');
            $('#page').css('padding-top','0px');
        }
        else
        {
            var errorContainer = '<p>We were unable to process your request. Please check the following issues, and then try again.</p>';
             errorContainer+='<ul id="serverErr2UL">';
            for(var i=0; i<errorList.length;i++)
            {
                errorContainer+='<li>'+errorList[i]+'</li>';
            }
            errorContainer+='</ul>';
            $('#serverErr2').html(errorContainer);
        }
    }            
}

function ValidateContactForm()
{   
    var firstName = document.getElementById("FirstName");
    var lastName = document.getElementById("LastName");
    var email = document.getElementById("email");
    var businessName = document.getElementById("BusinessName");
    var phone = document.getElementById("PhoneNumber");
    var timeToCall = document.getElementById("BestTimeToCall");
    var errors = new Array();
    if(firstName && firstName.value =='')
    {
        errors.push("First Name - this field is required");
    }
    /*if(lastName && lastName.value =='')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "Last Name - is required";
    }
    if(email)
    {
        if(email.value =='')
        {
            if (errors!='')
            {
                errors += "<br />";
            }
            errors += "Email Address - is required";
        }
        else
        {
            if(!ValidateEmail(email.value))
            {
                if (errors!='')
                {
                    errors += "<br />";
                }
                    errors += "Email Address - is not valid";
                }
        }
    }
    if(businessName && businessName.value =='')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "Business Name - is required";
    }*/
    if(phone && phone.value =='')
    {
        errors.push("Phone Number - this field is required");
    }
    /*if(timeToCall && timeToCall.value == '-1')
    {
        if (errors!='')
        {
            errors += "<br />";
        }
        errors += "Best Time To Call - is required";
    }*/
    if(errors.length > 0)
    {
        try
        {
            SetJsErrors(errors);
        }
        catch (err) { }
        return false;
    }
    else
    {
        return true;
    }
}
function ValidateResumeForm()
{
    $('.hoverErrorDiv').remove();
    $('#resumeFormErrors').hide();
    var inputFields = $('#resumeFormFields').find('input[class^="Validate["]');
    var selectFields = $('#resumeFormFields').find('select[class^="Validate["]');
    var errorMsg = '';
    inputFields.each(function(index){
        var element = $(this);
        element.hover(function(){}, function(){});
        var elClassName = element.attr('class').replace('Validate[','').replace(']','');
        if(elClassName != '')
        var validations = elClassName.split(',');
        if(validations && validations.length > 0)
        {
            for(var i=0; i<validations.length; i++)
            {
                var currentValidation = validations[i].toLowerCase();
                switch(currentValidation)
                {
                    case 'required':
                    {
                        if(element.attr('value') == '')
                        {
                            if(errorMsg!='')
                            {
                                errorMsg+='<br />';
                            }
                            errorMsg+='Value Is Required';
                            AddHoverError(element, 'Value Is Required');
                            element.css('background-color','#f57373');
                        }
                        else
                        {
                            element.unbind();
                            element.css('background-color','#fff');
                        }                        
                        break;
                    }
                    case 'email':
                    {
                        if(element.attr('value') != '')
                        {
                            if(!ValidateEmail(element.attr('value')))
                            {
                                if(errorMsg!='')
                                {
                                    errorMsg+='<br />';
                                }
                                errorMsg+='Email Value Is Not Valid';
                                AddHoverError(element, 'Email Value Is Not Valid');
                                element.css('background-color','#f57373');
                            }
                            else
                            {
                                element.unbind();
                                element.css('background-color','#fff');
                            }
                        }                                                    
                        break;
                    }
                    case 'fileinput':
                    {
                        var val = element.attr('value');
                        break;
                    }
                }
            }
        }
    });
    
    selectFields.each(function(index){
        var element = $(this);
        var elementLabel = $('label[for="'+element.attr('id')+'"]');
        var elClassName = element.attr('class').replace('Validate[','').replace(']','');
        if(elClassName != '')
        var validations = elClassName.split(',');
        if(validations && validations.length > 0)
        {
            for(var i=0; i<validations.length; i++)
            {
                var currentValidation = validations[i].toLowerCase();
                switch(currentValidation)
                {
                    case 'select':
                    {
                        if(element.attr('value') == '' || element.attr('value') == '-1')
                        {
                            if(errorMsg!='')
                            {
                                errorMsg+='<br />';
                            }
                            errorMsg+='Please Select a Value';
                            AddHoverError(element, 'Please Select a Value');
                            element.css('background-color','#f57373');
                        }
                        else
                        {
                            element.unbind();
                            element.css('background-color','#fff');
                        }
                        break;
                    }
                }
            }
        }            
    });
    
    if(errorMsg == '')
    {
        $('#resumeFormErrors').hide();
        return true;
    }
    else
    {
        $('#resumeFormErrors').show();
        $('#resumeFormFields').css('height',($('#resumeFormFields').height()+30)+'px');
        return false;
    }
}
function AddHoverError(jqElement, errMsg)
{
    if(jqElement && errMsg != '')
    {
        jqElement.hover(function(){
            if($($(this).attr('id')+'hoverErr').length <= 0)
            {
                var errorDiv = document.createElement('div');
                errorDiv.id = $(this).attr('id')+'hoverErr';
                errorDiv.className = 'hoverErrorDiv';
                errorDiv.innerHTML = errMsg;
                var pos = $(this).position();
                errorDiv.style.left = (pos.left+($(this).outerWidth()-172)) + 'px';
                errorDiv.style.top = (pos.top+22) + 'px';
                $('#resumeFormFields').append(errorDiv);
            }
            else
            {
                $($(this).attr('id')+'hoverErr').html(errMsg);
            }
            $($(this).attr('id')+'hoverErr').show();
        }, 
        function(){
            var errorDiv = $('#'+$(this).attr('id')+'hoverErr');
            if(errorDiv.length > 0)
            {
                errorDiv.hide();
                errorDiv.html('');
                errorDiv.remove();
            }
        });
    }
}
/*END JS Errors*/
function SetResumeForm(targetElement)
{
    $(document).ready(function(){
        $('.popoverMain').width($('body').width());
        $('.popoverMain').height($('body').height());
        $(targetElement).click(function(){
            var windowSize = $(window).height();
            var topMargin = (windowSize - 640)/2+$(window).scrollTop();
            $('#resumeFormInner').css('margin-top',topMargin);
            if(IsIE6())
            {
                $('select').hide();
            }
            $('#resumeFormPopover').fadeIn();
            return false;
        });
        $('#resumeFormClose').click(function(){
            $('.hoverErrorDiv').remove();
            $('#resumeFormErrors').hide();
            var inputFields = $('#resumeFormFields').find('input[class^="Validate["]');
            var selectFields = $('#resumeFormFields').find('select[class^="Validate["]');
            inputFields.css('background-color','#fff');
            selectFields.css('background-color','#fff');
            if(IsIE6())
            {
                $('select').show();
            }
            $('#resumeFormPopover').fadeOut();                
        });
    });
}
function SetResumeUpload(destination)
{
    $(document).ready(function(){
        $('#resumeOverlayTxt').click(function(){
            $(destination).click();
        });
        $('#resumeOverlayBtn').click(function(){
            $(destination).click();
        });
        $(destination).bind('change', function(){
            var newVal = $(this).attr('value');
            $('#resumeOverlayTxt').attr('value',newVal);
        });
    });
}

function SetResumeFormValidation(applyResume)
{
    if(applyResume != '')
    {   
        $(document).ready(function(){
            $(applyResume).click(function(){
                return ValidateResumeForm();
            });
        });
    }
}
