﻿/////////////////////////////////////////////////////////////////////////////////
// The code in this file isn't available until after the DOM is ready 
/////////////////////////////////////////////////////////////////////////////////

$(document).ready(function(){

    // Close popups when user presses escape
    document.onkeyup = function(e){
        var eventObject = window.event ? window.event : e;
        var key = eventObject.charCode ? eventObject.charCode : eventObject.keyCode;
        if (key == 27) {
            closePopups();
        }
    };

    // Login link 
    $("a#lnkLogin").click(function(){
        showLoginPopup();
        return false;
    });
    
    // Logout link 
    $("a#lnkLogout").click(function(){
        logout();
        return false;
    });
    
    // Register link 
    $("a#lnkRegister").click(function(){
        showRegisterPopup(false);
        return false;
    });
    
    // Profile link 
    $("a#lnkProfile").click(function(){
        showProfilePopup();
        return false;
    });

    // Help link 
    $("a#lnkHelp").click(function(){
        showHelpPopup();
        return false;
    });
    
    // Email button
    $("img#btnSendEmail").click(function(){
        showEmailRecommendationPopup();
        return false;
    });
    
    // Save bundle button
    $("img#btnSaveAsBundle").click(function(){
        showSaveBundlePopup();
        return false;
    });

    // Perform tasks specific to the Recommend page (it will have div#productsList)
    if ($("div#productsList").length > 0) {

        // Set up Cart object
        Cart.addFunction = addPdfToCart;
      
        // Import bundle items into Bundles object 
        Bundles.items = bundlesData.items;
        bundlesData = null;
    
        // Wire up product list links 
        // The listitems were populated by the servere - we just need to wire them up
        wireProductListLinks();
        
        // Load "Skip Test Drive" cookie if available
        loadTestDriveCookie();
        
        // See if we can log in via credentials stored in cookies
        attemptAutomaticLogin();
    };

    /////////////////////////////////////////////////////////////////////////////////
    // Custom validation functions
    // param : <ul> that should have more than one visible <li>s
    jQuery.validator.addMethod("listNotEmpty", function(value, element, param) { 
        var numItems = param.find("li:visible").length;
        return (numItems > 0);
    }, "The list is empty.");

/*
	// Phone number validation (US) 
    jQuery.validator.addMethod("phoneUS", function(value, element) { 
		return this.optional(element) || /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/.test(value);
	}, "The phone number is not in the correct format.");
*/		
	// Password validation (strong - 5 to 8 characters, must have letters and numbers)
    jQuery.validator.addMethod("strongPassword", function(value, element) { 
		return this.optional(element) || /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{5,8})$/.test(value);
	}, "The password is not in the correct format.");

	// Password validation (strong - 5 to 8 characters, must have letters and numbers)
    jQuery.validator.addMethod("registeredEmailAddress", function(value, element, param) { 
		// Get email from passed in control
		var email = param.val();

		// Return whether it's registered
		return registeredEmailAddress(param.val());
	}, "The password is not in the correct format.");
	
});

