/**
 * Browser issues that being dealt with via JS/jQuery are managed 
 * in their own JS files and will have their own $(document).ready().
 * This file contains only actions (specific to this page) that need to be
 * taken care of when the document has loaded. The last item in this file
 * is 
 * 
 * 	$(document).ready(function () { 
 * 		........ 
 * 	}
 *  
 */
  
    var _username = "";
    var _password = "";
    
    function handleForgotPassword() {
        // SHOW OVERLAY FORM FOR FORGOT PASSWORD
        alert("will show overlay for forgot password.  this will include an email / or username and a forgot password question.  if valid request then an email will be sent out to the user with a reset link"); 
    }
    
    function handleModalLogin() {
        // SHOW OVERLAY FORM FOR FORGOT PASSWORD
        alert("will show overlay for login. this overlay can be closed or it will go into regular authentication of login."); 
    }



    function rememberMe(email, password) {
        $.ajax({
            url: "/webservices/UserManagement.asmx/RememberMe",
            contentType: "application/json; charset=utf-8",
            global: false,
            type: "POST",
            data: "{'email':'" + email + "', 'password':'" + password + "'}",
            dataType: "json",
            success: function(response) {
                if (response.d == "success") {
                    if (location.search.split("referrer=")[1]) {
                        var referrer = location.search.split("referrer=")[1];
                        referrer.replace("#", "");
                        location = referrer;
                    } else {
                        location.reload();
                    }
                }
            }
        });
    }

    function ajaxLogin(email, password, culture, element) {
        var hiddenError = "";
        
        //If this is from the header
	    if (($(element).attr('id') == "myBlackAndDeckerLoginFormContainer") || ($(element).attr('id') == "btnLogin")) {
	        hiddenError = '#myBlackAndDeckerLoginFormContainer .headerError span';
	    }

	    if (email != "" && password != "") {
	        $.ajax({
	            url: "/webservices/UserManagement.asmx/Login",
	            contentType: "application/json; charset=utf-8",
	            global: false,
	            type: "POST",
	            data: "{'email':'" + email + "', 'password':'" + password + "', 'culture':'" + culture + "'}",
	            dataType: "json",
	            success: function(response) {
	                if (response.d.StatusCode == 0) {
	                    $(hiddenError).show();
	                } else {
	                    if (response.d.StatusCode == 1) {
	                        if (response.d.Data.DisplayName == null) {
	                            $("#registerUserNameNameContainer").show();
	                            $("#newUserNameEmailConsumerTouchPointID").val(response.d.Data.ConsumerTouchpointID);
	                            $("#newUserNameEmailConsumerEmail").val(email);
	                            $("#newUserNameEmailConsumerPW").val(password);
	                            ajaxLogout(true);
	                        } else {
	                            var rememberMeValue = $("#chkRemember").attr('checked');

	                            if (rememberMeValue) {
	                                rememberMe(email, password);
	                            } else {

	                                if (location.search.split("referrer=")[1]) {
	                                    var referrer = location.search.split("referrer=")[1];
	                                    referrer.replace("#", "");
	                                    location = referrer;
	                                } else {
	                                    location.reload();
	                                }
	                            }
	                        }
	                    }
	                }
	            }
	        });
	    } else {
	        $(hiddenError).show();
	    }
	    
	}

	function ajaxIsLoggedIn() {
	    $.ajax({
	        url: "/webservices/UserManagement.asmx/IsLoggedIn",
	        global: false,
	        type: "POST",
	        dataType: "xml",
	        success: function(xml) {
	            if ($(xml).text() === 'true') {
	                return $(xml).text();
	            }
	            else {
	                return $(xml).text();
	            }
	        }
	    });
	}

	function ajaxLogout(noReload) {
	    $.ajax({
	        url: "/webservices/UserManagement.asmx/Logout",
	        global: false,
	        type: "POST",
	        success: function() {
	            if (noReload == null) { noReload = false; }
            
	            if (!noReload && (location.href.split("/My-Black-and-Decker/")[1] || location.href.split("/my/acct/")[1])) {
	                location = "/Default.aspx";
	            } else if (!noReload) {
	                location.reload();
	            }
	        }
	    });
	}
	
	function ajaxSearch(searchTerm, culture) {
		$.ajax({
	      url: "/webservices/SiteSearch.asmx/HeaderSearch",
	      global: false,
	      type: "POST",
	      data: { searchTerm: searchTerm, culture: culture },
	      dataType: "xml",
		    success: function(xml){
			    $(xml).find("SearchResults").each(function(){
	  	        });
		        alert("Search logic will be handled in /webservices/SiteSearch.asmx/HeaderSearch");
		        
	      } // end success function
	    });
	}
	
	function validateUserLoginFields() {
	    // front-end validation done here
	    //
	    // this should include making sure all fields are filled out and that any 
	    // requirements around password or username strength are handled.  If username
	    // is to be an email there should also be a regex validation here.
	    // 
	    // the validation error message framework should be as follows:
	    // field_name:friendly message|
	    return "";
	}

	$(document).ready(function() {
	    //Domenic 02/08/10 The Following lines is being commented out because it throwing errors - we need to check to make sure these items exist before assigning click events
	    /*
	    $(".auth_modal_login").click(function() {
	    handleModalLogin();
	    });
        
	    $(".auth_label_forgot").click(function() {
	    handleForgotPassword();
	    });*/

	});
	

