/* jQuery No Conflict */
jQuery.noConflict();
/* Global Vars */

var root = "";


/* Functions */

function initialize(newRoot)
{
	root = newRoot;
	
}

/**********************************************************************************
Forgot Password Redirect
**********************************************************************************/
jQuery(document).ready(function()
{
    jQuery('.forgotYourPassword').click(function(){
        window.location = forgetPassword;
    });
});

/************************************************************************************
Password Input Box Switch
************************************************************************************/

jQuery(document).ready(function(){
    var textBoxToPass = jQuery('.switchTextBox'); //Variable for input text box
    var password = jQuery('.passwordText'); //Variable for input password box
    
    var textBoxToPassLL = jQuery('.switchTextBoxLL'); //Variable for input text box
    var passwordLL = jQuery('.passwordTextLL'); //Variable for input password box

    jQuery(textBoxToPass).focus(onPasswordFocus); //On Focus of Text Box run onPasswordFocus function
    jQuery(textBoxToPassLL).focus(onPasswordFocusLL); //On Focus of Text Box run onPasswordFocus function
    
    jQuery(password).blur(onPasswordBlur); // On Blur of Password Box run onPasswordBlue function
    jQuery(passwordLL).blur(onPasswordBlurLL); // On Blur of Password Box run onPasswordBlue function

    //On focus switch out text box with password box. This has to happens because IE does not know how to 
    //switch the type on focus
    function onPasswordFocus()
    {
        jQuery('.passwordText').show();
        jQuery('.switchTextBox').hide();
        jQuery('.passwordText').focus();
    }

    function onPasswordFocusLL()
    {
        jQuery('.passwordTextLL').show();
        jQuery('.switchTextBoxLL').hide();
        jQuery('.passwordTextLL').focus();
    }

    //On blur keep the password if value is filled in
    function onPasswordBlur()
    {
        if(jQuery(password).value != "" && jQuery(password).value != "Enter Password")
        {
            jQuery(password).show();
        }
        else
        {
            jQuery('passwordText').hide();
            jQuery('.switchTextBox').show();
        }
    }

    //On blur keep the password if value is filled in
    function onPasswordBlurLL()
    {
        if(jQuery(passwordLL).value != "" && jQuery(passwordLL).value != "Enter Password")
        {
            jQuery(passwordLL).show();
        }
        else
        {
            jQuery('passwordTextLL').hide();
            jQuery('.switchTextBoxLL').show();
        }
    }


});
    


/************************************************************************************
Form Field Clear
************************************************************************************/

jQuery(document).ready(function() {
      var address = "Enter Street Address";
      var zip = "ZIP Code";
      var username = "Enter Username or Email Address";
      var password = "Enter Password";
      var city = "Enter your City";
      
      jQuery('#orderNowMid select, .dealsLogin select, #dealsLogin select, .loginBox select, #locations select').change(function(){
            jQuery('#orderNowMid select option:selected, .dealsLogin select option:selected, #dealsLogin select option:selected, .loginBox select option:selected, #locations select option:selected').each(function () {
                if(jQuery(this).text() != "Please Select a State")
                {
                    jQuery('#stateLL, #stateMH, #stateDE').slideUp();   
                }
              });
      });
      
      jQuery('#orderNowMid :input[type="text"], .dealsLogin :input[type="text"], #dealsLogin :input[type="text"], .loginBox :input[type="text"], #locations :input[type="text"], #orderNowMid :input[type="password"], .dealsLogin :input[type="password"], #dealsLogin :input[type="password"], .loginBox :input[type="password"]').focus(function(){

            //Clear out error messages on focus of form field


            
            var passwordField = jQuery(this).prev().prev().hasClass('switchTextBox');
            if(passwordField != true)
            {
                if(jQuery(this).prev().attr('id') != "NewCityFAL")
                {
                    jQuery(this).prev().slideUp();
                }
                else
                {
                    return false;  
                }
            }
            
            if(jQuery(this).hasClass('switchTextBox') && jQuery(this).attr('type') == "text")
            {
                jQuery(this).prev().prev().slideUp();
            }
            
            if(jQuery(this).hasClass('switchTextBoxLL') && jQuery(this).attr('type') == "text")
            {
                jQuery(this).prev().prev().slideUp();
            }

            if(jQuery(this).prev().prev().prev().prev().prev().prev().attr('id') == "invalidAccountLL")
            {
                jQuery('#invalidAccountLL').slideUp();
            }
            if(jQuery(this).prev().prev().prev().prev().prev().prev().attr('id') == "invalidAccountMH")
            {
                jQuery('#invalidAccountMH').slideUp();
            }
            if(jQuery(this).prev().prev().prev().prev().prev().prev().attr('id') == "invalidAccountDE")
            {
                jQuery('#invalidAccountDE').slideUp();
            }            
            
            if(jQuery(this).prev().prev().prev().hasClass('cityState'))
            {
                jQuery('#cityLL, #cityMH, #cityDE').slideUp();
            }
            
            var usernameError = jQuery(this).prev().prev().html();

            if(usernameError == 'Please enter your username or email address.<BR>' || usernameError == 'Please enter your username or email address.<br>')
            {
                jQuery('#userNameErrorLL, #userNameErrorDE, #userNameErrorMH').slideUp();    
            }
            //Value is blank on focus
            if(this.value == "" || this.value == address || this.value == zip || this.value == username || this.value == city) //Do not need to clear password value because it is two different input boxes
            {
                this.value = "";
            }
            
            //On blur, if the value is left blank or is the original text, leave as original text
            jQuery(this).blur(function(){
                if(this.value == "" && jQuery(this).hasClass('streetAddressText'))
                {
                    this.value = address;
                }
                else if(this.value == "" && jQuery(this).hasClass('zipCodeText'))
                {
                    this.value = zip;
                }
                else if(this.value == "" && jQuery(this).hasClass('cityText'))
                {
                    this.value = city;
                }
                else if(this.value == "" && jQuery(this).hasClass('usernameText'))
                {
                    this.value = username;
                }
                else if(this.value == "" && jQuery(this).hasClass('passwordText'))
                {
                    jQuery('.passwordText').hide();
                    jQuery('.switchTextBox').show();
                }
            });
            
            jQuery('.passwordTextLL').blur(function(){
                if(this.value == "")
                {
                    jQuery('.passwordTextLL').hide();
                    jQuery('.switchTextBoxLL').show();
                }
            });

      });

      //On Enter, submit Forms for sign in
      jQuery('input').keypress(function(e)
      {
            if(e.which==13)
            {
                var grandParent = jQuery(this).parent().parent().attr('id');
                var zipCityGrandParent = jQuery(this).parent().parent().parent().attr('id');
                grandParent = grandParent + zipCityGrandParent;

                switch (grandParent){

                case "newCustomerLL":
                return getCookieInformationLightbox('NewLL');
                break;
                
                case "returningCustomerLL":
                return sendLoginInformationLightbox('ReturnLL'); 
                break;
                
                case "newCustomerDL":
                return getCookieInformationLightbox('NewDL');
                break;
                
                case "returningCustomerDL":
                return sendLoginInformationLightbox('ReturnDL'); 
                break;
                
                case "newCustomerMH":
                return getCookieInformation();
                break;
                
                case "returningCustomerMH":
                return sendLoginInformation(); 
                break;
                
                case "newCustomerMHorderOnlineNow":
                return getCookieInformation();
                break;
                
                case "returningCustomerMHorderOnlineNow":
                return sendLoginInformation(); 
                break;
                
                case "newCustomerDE":
                return getCookieInformation();
                break;
                
                case "returningCustomerDE":
                return sendLoginInformation(); 
                break;
                
                case "newCustomerDEcenter":
                return getCookieInformation();
                break;
                
                case "returningCustomerDEcenter":
                return sendLoginInformation(); 
                break;
                
                case "locations":                                                      
                return getCookieInformationLightbox('NewFAL');
                break;
                
                default : 
                return false;
                
                }
                
            }            
      });


});


/************************************************************************************
ZIP Code/CityState Change
************************************************************************************/

jQuery(document).ready(function(){

    //On click of object ('a.noZip') hide zip fields and show city and state fields
    jQuery('a.noZip').click(function()
    {
        //Hide Zip Code Fields
        jQuery('.zipCodeWrapper').hide();
        
        //Show city and state fields
        jQuery('.cityStateWrapper').show();
    
        //hide previous city/state errors
        jQuery('#cityMH, #cityLL, #cityDL, #cityDE, #stateMH, #stateLL, #stateDL, #stateDE').hide(); //Hide Errors
        return false;
    });

    //on click of object('a.noCityState') hide city and state fields and show zip fields
    jQuery('.noCityState').click(function()
    {
        //Show Zip code fields
        jQuery('.zipCodeWrapper').show();
        
        //Hide city and state fields
        jQuery('.cityStateWrapper').hide();
        
        //Hide all previous zip errors
        jQuery('#zipMH, #zipLL, #zipDL, #zipDE').hide(); //Hide Errors
        return false;
    });
});


// Get Cookie
function getCookie( check_name ) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ )
    {
	    // now we'll split apart each name=value pair
	    a_temp_cookie = a_all_cookies[i].split( '=' );


	    // and trim left/right whitespace while we're at it
	    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

	    // if the extracted name matches passed check_name
	    if ( cookie_name == check_name )
	    {
		    b_cookie_found = true;
		    // we need to handle case where cookie has no value but exists (no = sign, that is):
		    if ( a_temp_cookie.length > 1 )
		    {
			    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
		    }
		    // note that in cases where cookie is initialized but no value, null is returned
		    return cookie_value;
		    break;
	    }
	    a_temp_cookie = null;
	    cookie_name = '';
    }
    if ( !b_cookie_found )
    {
	    return null;
    }
}



