/** Popover Functions **/
function fireFP(h) {
	//first, close any open popovers if we can 
       if($(".ui-dialog-content").length){
       		$('.ui-dialog-content').each(function(index){
       			var id = $(this).attr("id"); id="#"+id;
       			$(id).dialog("close");
       		});
       	}
        //finally, open the forgot PW dialog
        $("#forgotPwdForm").dialog({
            height: 180,
            width: 300,
            closeOnEscape: true,
            title: "Password Recovery Form",
            modal: true,
            resizable: false,
            show: "drop",
            hide: "fade"
        });
    }
   function f() {
        var loginForms = $("form[name='createAcctForm']");
        if (loginForms.length > 1) {
            for (var i = 1; i < loginForms.length; i++) {
                $("#table_createAcct:first").remove();
            }
        }
        $("#table_createAcct").dialog({
            height: 350,
         	width: 550,
            closeOnEscape: true,
            title: "Create an Account!",
            modal: true,
            resizable: false,
            show: "drop",
            hide: "fade"
        });
    }
    function c() {
        $("#loginCreateAcct").click(function () {
            $("#div_userlogin").dialog("close");
            f();
        });
        $("#loginPO").click(function () {
            if (tmt_validateForm(document.loginForm)) {
                document.loginForm.submit();
            } else {
                return;
            }
        });
        $("#div_userlogin").dialog({
            height: 280,
            width: 600,
            closeOnEscape: true,
            title: "LOGIN",
            modal: true,
            resizable: false,
            show: "drop",
            hide: "fade"
            //,close: function () {
              //  $("#poLoginBtns").remove();
            //}
        });
    }
    function b() {
        if (tmt_validateForm(document.forgotPwdForm) == false) {
            return;
        }
        var j = $("input#fpEmail").val();
        var i = new LoginObject();
        var h = i.checkForEmail(j);
        $("#forgotPwdForm").hide("fade", {}, 180);
        if (h) {
            $("#forgotPwdForm").html("<p align='left'>Your password has been sent to <b>" + j + "</b>.<br /><br /> Be sure to save your password in a secure place. Click the button below to try logging in again.<br /><br /><a class='buttonstandard' id='loginAg1'><span>Log In</span></a></p>");
            $("#forgotPwdForm").show("fade", {}, 180);
            $("a#loginAg1").click(function () {
                $("#forgotPwdForm").dialog("close");
                c();
            });
        } else {
            if (h == false) {
                $("#forgotPwdForm").dialog("option", "height", 240);
                $("#forgotPwdForm").bind("dialogclose", function () {
                    $("#forgotPwdForm").html("Please reload the page to use this again. Thank you.<br /><br /><a class='buttonstandard' href='javascript:window.location.reload()'><span>Reload Page</span></a>");
                });
                $("#forgotPwdForm").html("<div align='left'><strong>Sorry, we cannot find a valid password which matches the E-mail address you provided.</strong><p>In an ongoing effort to ensure the most secure shopping experience we must request that ANY accounts set up prior to 10/6/2009 be recreated. Please accept our apologies for this inconvience, and click the button below to set up an account or try to log in again.<br /><br /><a class='buttonstandard' id='createAcct'><span>Sign Up Now</span></a>&nbsp;&nbsp;&nbsp;<a class='buttonstandard' id='loginAg'><span>Log In</span></a></div>");
                $("#forgotPwdForm").show("fade", {}, 180);
                $("a#createAcct").click(function () {
                    $("#forgotPwdForm").dialog("close");
                    f();
                });
                $("a#loginAg").click(function () {
                    $("#forgotPwdForm").dialog("close");
                    c();
                });
            } else {
                alert("Something went wrong. Please refresh the page and try again.");
            }
        }
    }
    function a(i) {
        if (!i) {
            var i = window.event;
        }
        i.preventDefault;
        var h = i.target.innerHTML;
		
        if (h == "sign up now") {
            f();
        } else {
			var href = window.location.toString();
           /* if (href.indexOf("method=Order")==-1 && href.indexOf("method=Products_cart")==-1 && h == "sign in!" && i.target.id =="wishlistSignIn") {
               c();//fire Login
			   
			   var origGTM = $("#loginForm #goToMethodLF").val();
			  
			   $("#loginForm #goToMethodLF").val("loginToWishlist");
			   
			   $("#div_userlogin").bind(
			   		"dialogclose",
					 function(event,ui){
					 	$("#loginForm #goToMethodLF").val(origGTM);
					 }
			   );
            }
			else*/ if(h == "sign in!") {c();}
			else {
                alert("Something went wrong. Please close the popover and/or refresh the page.");
            }
        }
    }
	
	function showTerm(term){
		if(term.indexOf("ies",term.length-3) != -1){term = term.substr(0,term.length-3)+"y";}
		else if(term.indexOf("s",term.length-1) != -1){term = term.substr(0,term.length-1);}
		else if(term.indexOf("ed",term.length-2) != -1){term = term.substr(0,term.length-2);}
			term = term + "%"; //percent is there for SQL LIKE statement in the query
		var obj = new objForwardToFriend();
		var result = obj.getGlossaryInfo(term);
		var splitResult = result.split("~~~");
		$("<div id='glossaryTerm'></div>").appendTo("body");
		$('#glossaryTerm').css("display","none");
		$('#glossaryTerm').html("<div id='term'>"+splitResult[0] + "</div><div id='description' style='font-size:13px;text-align:left;'>" + splitResult[1]+"</div><div style='float:right'><a id='closeMe' class='buttonstandard'><span>close</span></a></div>");
		$("#glossaryTerm").dialog({
			height:$("div#description").attr("height"),
				width:435,
				closeOnEscape:true,
				title:"Definition: " + splitResult[0],
				modal:true,
				resizable:false,
				show:'fold',
				hide: 'fade',
			close: function(){$('#glossaryTerm').remove();}
		});
		$(".ui-widget-overlay").click(function(){$("#glossaryTerm").dialog('close');});
		$("#closeMe").click(function(){$("#glossaryTerm").dialog('close');});
			
	}
	
	function fireSpecialsSignUp() {
        if (tmt_validateForm(document.emailForm) == false) {
            return false;
        }
        $("<div id='specialsSignUpForm'></div>").appendTo("body");
        var h = $("#efEmail").val();
        $("#specialsSignUpForm").html("<p align='left'>Fill in the fields below to complete your registration. </p><cfoutput><form id='specialForm' name='specialForm' method='post' tmt:validate='true'><table width='275'><tr><td><strong>First Name*</strong></td><td><input type='Text' id='specialsFirstName' name='firstName' class='normal' maxlength='10' tmt:required='true' tmt:message='First name is a required Field' value=''></td></tr><tr><td><strong>E-mail*</strong></td><td><input type='Text' id='specialsEmail' name='email' class='normal' value='" + h + "' tmt:required='true' tmt:pattern='email' tmt:message='Please enter a valid email'></td></tr><tr valign='top'><td align='right'><strong>Gender*</strong></td><td align='left'><input type='radio'  name='Gender2' maxlength='15' tmt:required='true' tmt:message='Please enter your gender.' value='Male'/>Male <input type='radio'  name='Gender2'  maxlength='15' value='Female'/>Female</td></tr><tr><td colspan='2' align='center'><table width='122' cellpadding='0' cellspacing='0' border='0' align='right'>	<tr>		<td><div style='float:right;'><a class='buttonstandard' href='##' id='specialFormSubmit'><span>Submit</span></a></div></td><td>&nbsp;&nbsp;</td>	</tr></table></td></tr></table></form></cfoutput>");
        $("#specialsSignUpForm").dialog({
            height: 200,
            width: 300,
            closeOnEscape: true,
            title: "Sign Up for Special Offers!",
            modal: true,
            resizable: false,
            show: "drop",
            hide: "fade",
            close: function () {
                $("#specialsSignUpForm").remove();
            }
        });
        $("#specialFormSubmit").click(function () {
            var o = $("#specialsFirstName").val();
            var k = $("input[name='Gender2']:checked").val();
            var l = $("#specialsEmail").val();
            $("#specialFormSubmit").attr("disabled", true);
            if (o == "" || k == null || l.indexOf("@") == -1 || l == "") {
                alert("Please check the fields and try again.");
                return;
            }
            var m = new SpecialsObject();
            var j = m.signUp(o, k, l);
            if (j) {
                $("#specialsSignUpForm").hide("fade", {}, 100);
                _gaq.push(['_setAccount', $("#emailFormSubmitBtn").attr("name")]);
                _gaq.push(['_trackPageview', '/popoverSuccess/EmailSignUpSuccess']);
                $("#specialsSignUpForm").html('<img src="https://track.did-it.com/n?tid=105c9cd8d2601"><div align="left"><span class="Product_OurPrice">Thank you.<br>Your request to join our email alerts program has been successfully sent.</span><br><br><a class="buttonstandard" id="specialCloseBtn"><span>continue shopping</span></a><!--- <img src="https://track.did-it.com/n?tid=24db773069b71"> ---><img src="https://27.xg4ken.com/media/redir.php?track=1&token=819265d7-472d-4878-828e-0e5b83166d78&type=email&val=0&orderId=' + l + '&promoCode=&valueCurrency=USD" width="1" height="1"></div>');
                $("#specialsSignUpForm").show("fade", {}, 100);
                $("#specialCloseBtn").click(function () {
                    $("#specialsSignUpForm").dialog("close");
                });
            } else {
                if (j == false) {
                    alert("Something went wrong. We apologize for the inconvenience. Please refresh the page and try again.");
                }
            }
        });
    }
	function fireCVCPopover(){
		var checkedCard = $("input[name='PaymentType']:checked").val();
		var imgFile ="";var htmlStr="";
		if(checkedCard=="B"||checkedCard=="M"){
		imgFile = "VisaMC.gif";
		}else if(checkedCard=="D"){ imgFile= "Discover.gif";}
		else if(checkedCard=="X"){ imgFile="Amex.gif";}
		
		if(imgFile != ""){htmlStr = "<div id='imgDialog'><img id='ccimg' src='/images/GlobalGraphics/" + imgFile + "' /></div>"}
		else{htmlStr = "<div id='imgDialog'><h3>Please choose a Payment Type, then click \'what's this?\' again to see where your CVC number is located.</h3></div>"}
		
		$(htmlStr).dialog({
			height:$("#ccimg").attr("height"),
			width:490,
			closeOnEscape:true,
			title:"CVC Number Location",
			modal:true,
			resizable:false,
			show:'fold',
			hide: 'fade',
			close: function(){$("#ccimg").remove();$("#imgDialog").empty();$("#imgDialog").remove();}
		});
	}
	function addToWL_NLI(){
		c();
		var origLoginGTM = $("#loginForm #goToMethodLF").val();
	   	$("#loginForm #goToMethodLF").val("loginAndAddToWishlist");
		
		$("#thisForm input").clone().appendTo("#clonedFields");
		$("#thisForm select").each(function(index){
			var sel = $(this).val();
			$(this).clone().appendTo("#clonedFields");

			$("#clonedFields select").eq(index).find('option[value='+sel+']').attr('selected', 'selected');
		});
		
	   	$("#div_userlogin").bind(
	   		"dialogclose",
			 function(event,ui){
			 	$("#loginForm #goToMethodLF").val(origLoginGTM);
				$("#clonedFields").empty();
			 }
	   	);
	}
	
	function login(submitType){
		c();
		if(submitType){
			var origType = $("#loginForm #goToMethodLF").val();
			$("#loginForm #goToMethodLF").val(submitType);
			
			$("#div_userlogin").bind(
		   		"dialogclose",
				 function(event,ui){
				 	$("#loginForm #goToMethodLF").val(origType);
				 }
		   	);
		}
	}

