/*
	===============================================================================================================
	NOTES:
	Still cannot get form fields value in Opera only:
	(*) document.formName.fieldName.value causes javascript error.
	(*) Define new function in jquery.qtip-1.0.0-rc3.min.js doesn't cause javascript error, but nothing happens.
		For example, in "qTip" function in jquery.qtip-1.0.0-rc3.min.js, in $.extend, add this code:
			submitForm: function()
			{
				return this.elements.content;
			}
		Then on form submit function, add this code:
			var obj = document.getElementById("lyrLibItem_" + pItemType + "_" + pItemID);
			if (obj) {
				if ($(obj).data("qtip")) {
					var sTooltipContent = $(obj).qtip("api");
					var sFormFieldValue = sTooltipContent.submitForm().find('input[type="text"]').attr("value"));
					alert(sFormFieldValue); // --> nothing happens
				}
			}
		This code only return a result if we put in built-in API method.
		For example, in showTooltipLogin function, in "api:", add this code:
			onHide: function() {
				alert(this.elements.content.find('input[type="text"]').attr("name"));
				alert(this.elements.content.find('input[type="text"]').attr("value"));
			}
		But there is no built-in API method that we can use to make Login/Sign Up process working correctly in JID.
	===============================================================================================================
*/


var bNeedToSaveItem_Tooltip  = false;
var bLibAjax_Tooltip		 = 0;
var oLibItemLyr_Tooltip		 = null;
var sLibItemType_Tooltip	 = "";
var iLibItemID_Tooltip		 = 0;
var sLibItemTitle_Tooltip	 = "";

var oLibItemLyr_Tooltip_Prev = null;
var bForgotPwd				 = false;
var bLibRefreshed			 = false
var bSignUpFromHL			 = false;


function signUpECommList()
{
	if (trim(document.getElementById("email-sub").value) == "" || trim(document.getElementById("email-sub").value).toUpperCase() == "EMAIL ADDRESS") {
		alert("Please enter your e-mail");
		document.getElementById("email-sub").focus();
		return false;
	}
	else if (!isValidEmail(trim(document.getElementById("email-sub").value))) {
		alert("Please enter a valid e-mail address");
		document.getElementById("email-sub").focus();
		return false;
	}
	else {
		var sParam = "txtEmail=" + escape(trim(document.getElementById("email-sub").value)) +
					 "&sendKey=" + escape(trim(document.getElementById("sendKey").value));
		var sURL   = "/include/signup_ecomm_list_process.asp?" + sParam;
		ajaxpack.postAjaxRequest(sURL, "", processSignUpECommList, "txt");
		
		return false;
	}
}

function processSignUpECommList()
{
	var oAJAX	   = ajaxpack.ajaxobj;
	var myfiletype = ajaxpack.filetype;
	
	if (oAJAX.readyState == 4) { // if request of file completed
		if (oAJAX.status == 200 || window.location.href.indexOf("http") == -1) {
			if (myfiletype == "txt") {
				if ($(document.getElementById("signup-sub")).data("qtip"))
					$(document.getElementById("signup-sub")).qtip("enable");
				
				
				var sResult = oAJAX.responseText;
				
				if (sResult == "err~~~~") {
					alert("Error when invoking AJAX function");
					
					document.getElementById("email-sub").disabled  = false;
					document.getElementById("signup-sub").disabled = false;
				}
				else if (sResult == "err~empty-email~~~") {
					alert("Please enter your e-mail");
					
					document.getElementById("email-sub").disabled  = false;
					document.getElementById("signup-sub").disabled = false;
					
					document.getElementById("email-sub").focus();
				}
				else if (sResult == "err~invalid-email~~~") {
					alert("Please enter a valid e-mail address");
					
					document.getElementById("email-sub").disabled  = false;
					document.getElementById("signup-sub").disabled = false;
					
					document.getElementById("email-sub").focus();
				}
				else if (sResult != "") {
					var arrResult = sResult.split("~");
					if (arrResult.length == 5) {
						showTooltipSignUpThankYou(unescape(trim(arrResult[4])), unescape(trim(arrResult[2])), trim(arrResult[3]));
					}
					else {
						alert("Error when invoking AJAX function");
						
						document.getElementById("email-sub").disabled  = false;
						document.getElementById("signup-sub").disabled = false;
					}
				}
				else {
					alert("Error when invoking AJAX function");
					
					document.getElementById("email-sub").disabled  = false;
					document.getElementById("signup-sub").disabled = false;
				}
			}
		}
		else {
			alert("Error when invoking AJAX function:\n\rStatus: " + oAJAX.status + "\n\rError Description:\n\r" + oAJAX.responseText);
			
			document.getElementById("email-sub").disabled  = false;
			document.getElementById("signup-sub").disabled = false;
			
			if ($(document.getElementById("signup-sub")).data("qtip"))
				$(document.getElementById("signup-sub")).qtip("enable");
		}
	}
	else {
		document.getElementById("email-sub").disabled  = true;
		document.getElementById("signup-sub").disabled = true;
		
		if ($(document.getElementById("signup-sub")).data("qtip"))
			$(document.getElementById("signup-sub")).qtip("disable");
	}
}

function showTooltipSignUpThankYou(pEmail, pUserIDEmail, pHasBeenSubscribed)
{
	var obj = document.getElementById("signup-sub");
	
	if (obj) {
		// destroy the previous tooltip
		if ($(obj).data("qtip")) $(obj).qtip("destroy");
		
		// create the tooltip
		$(obj).qtip(
		{
			// content of the tooltip window
			content: {
				// tooltip content
				text: '',
				// set tooltip content with URL
				url: "/include/signup_thankyou.asp?em=" + escape(pEmail) + "&uid=" + escape(pUserIDEmail) + "&subs=" + pHasBeenSubscribed,
				title: {
					text: '<h2 class="wintitletip">Thanks for Signing Up</h2>',	// title of the tooltip window
					button: '<img src="/i/tooltip/bCloseWin.gif">'				// show a close button in the title
				}
			},
			position: {
				// the tooltip will be positioned with it's top right hand corner touching the targets bottom left hand corner
				corner: {
					tooltip: 'topRight',		// position of the tooltip corner
					target: 'bottomLeft'		// the opposite
				},
				adjust: {
					x: 90,						// x value of the tooltip coordinate (counting from corner.tooltip)
					y: 8,						// y value of the tooltip coordinate (counting from corner.tooltip)
					screen: true				// keep the tooltip on-screen at all times
				}
			},
			show: {
				solo: true						// only show one tooltip at a time
			},
			hide: false,						// don't specify a hide event
			style: {
				padding: '0px 10px 10px 10px',	// tooltip content padding
				border: {
					width: 4,					// tooltip border width
					radius: 0,					// rounded corner
					color: '#0D6374'			// tooltip border color
				},
				title: {
					background: '#FFFFFF',		// tooltip title background
					padding: '5px'				// tooltip title padding
				},
				name: 'light',					// use the default light style
				width: 400						// set the tooltip width
			},
			api: {
				// called after the tooltip is hidden
				onHide: function() {
					// enable sign up fields
					document.getElementById("email-sub").disabled  = false;
					document.getElementById("signup-sub").disabled = false;
					
					document.getElementById("email-sub").value	   = "EMAIL ADDRESS";
					
					// the tooltip will be temporarily disabled, and will not fire any events e.g. hide, show, focus
					$(obj).qtip("disable");
				}
			}
		});
		
		// auto-show the tooltip (based on show.when --> by default is mouseover)
		$(obj).mouseover();
	}
}

// ===========================================================================

function signUpECommListHL()
{
	if (trim(document.getElementById("email-sub-hl").value) == "" || trim(document.getElementById("email-sub-hl").value).toUpperCase() == "EMAIL ADDRESS") {
		alert("Please enter your e-mail");
		document.getElementById("email-sub-hl").focus();
		return false;
	}
	else if (!isValidEmail(trim(document.getElementById("email-sub-hl").value))) {
		alert("Please enter a valid e-mail address");
		document.getElementById("email-sub-hl").focus();
		return false;
	}
	else if (trim(document.getElementById("zip-sub-hl").value).length > 10 && trim(document.getElementById("zip-sub-hl").value).toUpperCase() != "ZIP CODE (OPTIONAL)") {
		alert("Zip code cannot more than 10 characters");
		document.getElementById("zip-sub-hl").focus();
		return false;
	}
	else {
		var sParam = "txtEmail=" + escape(trim(document.getElementById("email-sub-hl").value)) +
					 "&sendKey=" + escape(trim(document.getElementById("sendKeyHL").value));
		
		var sZip = trim(document.getElementById("zip-sub-hl").value);
		if (sZip.toUpperCase() == "ZIP CODE (OPTIONAL)") sZip = "";
		
		sParam = sParam +
				 "&txtZip=" + escape(sZip);
		
		var sURL = "/include/signup_ecomm_list_process.asp?" + sParam;
		ajaxpack.postAjaxRequest(sURL, "", processSignUpECommListHL, "txt");
		
		return false;
	}
}

function processSignUpECommListHL()
{
	var oAJAX	   = ajaxpack.ajaxobj;
	var myfiletype = ajaxpack.filetype;
	
	if (oAJAX.readyState == 4) { // if request of file completed
		if (oAJAX.status == 200 || window.location.href.indexOf("http") == -1) {
			if (myfiletype == "txt") {
				if ($(document.getElementById("signup-sub-hl")).data("qtip"))
					$(document.getElementById("signup-sub-hl")).qtip("enable");
				
				
				var sResult = oAJAX.responseText;
				
				if (sResult == "err~~~~~") {
					alert("Error when invoking AJAX function");
					
					document.getElementById("email-sub-hl").disabled  = false;
					document.getElementById("zip-sub-hl").disabled	  = false;
					document.getElementById("signup-sub-hl").disabled = false;
				}
				else if (sResult == "err~empty-email~~~~") {
					alert("Please enter your e-mail");
					
					document.getElementById("email-sub-hl").disabled  = false;
					document.getElementById("zip-sub-hl").disabled	  = false;
					document.getElementById("signup-sub-hl").disabled = false;
					
					document.getElementById("email-sub-hl").focus();
				}
				else if (sResult == "err~invalid-email~~~~") {
					alert("Please enter a valid e-mail address");
					
					document.getElementById("email-sub-hl").disabled  = false;
					document.getElementById("zip-sub-hl").disabled	  = false;
					document.getElementById("signup-sub-hl").disabled = false;
					
					document.getElementById("email-sub-hl").focus();
				}
				else if (sResult != "") {
					var arrResult = sResult.split("~");
					if (arrResult.length == 6) {
						showTooltipSignUpThankYouHL(unescape(arrResult[4]), unescape(arrResult[2]), unescape(arrResult[5]), arrResult[3]);
					}
					else {
						alert("Error when invoking AJAX function");
						
						document.getElementById("email-sub-hl").disabled  = false;
						document.getElementById("zip-sub-hl").disabled	  = false;
						document.getElementById("signup-sub-hl").disabled = false;
					}
				}
				else {
					alert("Error when invoking AJAX function");
					
					document.getElementById("email-sub-hl").disabled  = false;
					document.getElementById("zip-sub-hl").disabled	  = false;
					document.getElementById("signup-sub-hl").disabled = false;
				}
			}
		}
		else {
			alert("Error when invoking AJAX function:\n\rStatus: " + oAJAX.status + "\n\rError Description:\n\r" + oAJAX.responseText);
			
			document.getElementById("email-sub-hl").disabled  = false;
			document.getElementById("zip-sub-hl").disabled	  = false;
			document.getElementById("signup-sub-hl").disabled = false;
			
			if ($(document.getElementById("signup-sub-hl")).data("qtip"))
				$(document.getElementById("signup-sub-hl")).qtip("enable");
		}
	}
	else {
		document.getElementById("email-sub-hl").disabled  = true;
		document.getElementById("zip-sub-hl").disabled	  = true;
		document.getElementById("signup-sub-hl").disabled = true;
		
		if ($(document.getElementById("signup-sub-hl")).data("qtip"))
			$(document.getElementById("signup-sub-hl")).qtip("disable");
	}
}

function showTooltipSignUpThankYouHL(pEmail, pUserIDEmail, pZip, pHasBeenSubscribed)
{
	var obj = document.getElementById("signup-sub-hl");
	
	if (obj) {
		// destroy the previous tooltip
		if ($(obj).data("qtip")) $(obj).qtip("destroy");
		
		// create the tooltip
		$(obj).qtip(
		{
			// content of the tooltip window
			content: {
				// tooltip content
				text: '',
				// set tooltip content with URL
				url: "/include/signup_thankyou.asp?hl=1&em=" + escape(pEmail) + "&uid=" + escape(pUserIDEmail) + "&zip=" + escape(pZip) + "&subs=" + pHasBeenSubscribed,
				title: {
					text: '<h2 class="wintitletip">Thanks for Signing Up</h2>',	// title of the tooltip window
					button: '<img src="/i/tooltip/bCloseWin.gif">'				// show a close button in the title
				}
			},
			position: {
				// the tooltip will be positioned with it's top right hand corner touching the targets bottom left hand corner
				corner: {
					tooltip: 'topRight',		// position of the tooltip corner
					target: 'bottomLeft'		// the opposite
				},
				adjust: {
					x: 90,						// x value of the tooltip coordinate (counting from corner.tooltip)
					y: 8,						// y value of the tooltip coordinate (counting from corner.tooltip)
					screen: true				// keep the tooltip on-screen at all times
				}
			},
			show: {
				solo: true						// only show one tooltip at a time
			},
			hide: false,						// don't specify a hide event
			style: {
				padding: '0px 10px 10px 10px',	// tooltip content padding
				border: {
					width: 4,					// tooltip border width
					radius: 0,					// rounded corner
					color: '#0D6374'			// tooltip border color
				},
				title: {
					background: '#FFFFFF',		// tooltip title background
					padding: '5px'				// tooltip title padding
				},
				name: 'light',					// use the default light style
				width: 400						// set the tooltip width
			},
			api: {
				// called after the tooltip is hidden
				onHide: function() {
					// enable sign up fields
					document.getElementById("email-sub-hl").disabled  = false;
					document.getElementById("zip-sub-hl").disabled    = false;
					document.getElementById("signup-sub-hl").disabled = false;
					
					document.getElementById("email-sub-hl").value	  = "EMAIL ADDRESS";
					document.getElementById("zip-sub-hl").value		  = "ZIP CODE (optional)";
					
					// the tooltip will be temporarily disabled, and will not fire any events e.g. hide, show, focus
					$(obj).qtip("disable");
				}
			}
		});
		
		// auto-show the tooltip (based on show.when --> by default is mouseover)
		$(obj).mouseover();
	}
}

// ===========================================================================

function signUpProfile(pBaseRef, pBaseRefSSL,pIsAjax, pItemType, pItemID, pItemTitle)
{
	var sSignUpURL;
	
	if (pIsAjax && pItemType && pItemID && pItemTitle) {
		var sParam		 = "type=" + pItemType + "&id=" + pItemID;
		var sSaveItemURL = "/content/content_save.asp?" + sParam;
		
		sSignUpURL		 = pBaseRef + "/my_library/signup.asp?et=";
		
		if (trim(document.getElementById("txtEmailTip").value) != "" && trim(document.getElementById("txtEmailTip").value).toUpperCase() != "YOUR EMAIL ADDRESS")
			sSignUpURL += escape(trim(document.getElementById("txtEmailTip").value));
		
		sSignUpURL += "&url=" + escape(sSaveItemURL);
		
		
		// hide login tooltip
		oLibItemLyr_Tooltip = document.getElementById("lyrLibItem_" + pItemType + "_" + pItemID);
		bForgotPwd			= true;
		
		if (oLibItemLyr_Tooltip) {
			if ($(oLibItemLyr_Tooltip).data("qtip"))
				$(oLibItemLyr_Tooltip).qtip("hide");
		}
		
		// reset variable
		bNeedToSaveItem_Tooltip = false;
		bLibAjax_Tooltip		= 0;
		oLibItemLyr_Tooltip		= null;
		sLibItemType_Tooltip	= "";
		iLibItemID_Tooltip		= 0;
		sLibItemTitle_Tooltip	= "";
		
		bForgotPwd				= false;
		bSignUpFromHL			= false;
		
		// redirect to forgot password page
		window.location.replace(sSignUpURL);
	}
	else {
		if (trim(document.getElementById("txtPasswordTip2").value) == "" || trim(document.getElementById("txtPasswordTip2").value).toUpperCase() == "CHOOSE A PASSWORD") {
			alert("Please choose a password");
			document.getElementById("txtPasswordTip2").focus();
			return false;
		}
		else if (trim(document.getElementById("txtPasswordTip2").value).length < 6) {
			alert("Password must be at least six characters");
			document.getElementById("txtPasswordTip2").focus();
			return false;
		}
		else if (trim(document.getElementById("txtPasswordTip2").value).indexOf('"') >= 0 || trim(document.getElementById("txtPasswordTip2").value).indexOf("<") >= 0 || trim(document.getElementById("txtPasswordTip2").value).indexOf(">") >= 0 || trim(document.getElementById("txtPasswordTip2").value).indexOf("%") >= 0) {
			alert('Password cannot contain " < > % characters');
			document.getElementById("txtPasswordTip2").focus();
			return false;
		}
		else {
			bNeedToSaveItem_Tooltip = false;
			bLibAjax_Tooltip		= 0;
			oLibItemLyr_Tooltip		= null;
			sLibItemType_Tooltip	= "";
			iLibItemID_Tooltip		= 0;
			sLibItemTitle_Tooltip	= "";
			
			if (document.getElementById("txtFromHLTip2")) {
				if (trim(document.getElementById("txtFromHLTip2").value) == "1")
					bSignUpFromHL = true
				else
					bSignUpFromHL = false;
			}
			else
				bSignUpFromHL = false;
			
			var sParam = "txtFullName=" + escape(trim(document.getElementById("txtFullNameTip").value)) +
						 "&txtPassword=" + escape(trim(document.getElementById("txtPasswordTip2").value)) +
						 "&txtEmail=" + escape(trim(document.getElementById("txtEmailTip2").value)) +
						 "&txtZip=" + escape(trim(document.getElementById("txtZipTip").value)) +
						 "&autosubs=0" +
						 "&sendKey=" + escape(trim(document.getElementById("sendKeyTip2").value));
			var sURL   = "/include/signup_profile_process.asp?" + sParam;			
			ajaxpack.postAjaxRequest(sURL, "", processSignUpProfile, "txt");
		
			return false;
		}
	}
	
}


function processSignUpProfile()
{
	var oAJAX	   = ajaxpack.ajaxobj;
	var myfiletype = ajaxpack.filetype;
	
	var objForm	   = document.frmSignUpBox;
	var objForm2   = document.frmSignUpTip;
	var objForm3   = document.frmSignUpBoxHL;
	
	var objBtn	   = null;
	var objBtn2	   = null;
	var objBtn3	   = null;
	
	var oEmail     = null;
	var oEmail2    = null;
	var oPassword  = null;
	var oPassword2 = null;
	var oFullName  = null;
	var oFullName2 = null;
	var oZip	   = null;
	var oFromHL	   = null;
	
	if (objForm) {
		objBtn  = document.getElementById("signup-sub");
		objBtn3 = document.getElementById("signup-sub");
		oEmail  = document.getElementById("email-sub");
	}
	if (objForm3 && bSignUpFromHL) {
		objBtn  = document.getElementById("signup-sub-hl");
		objBtn3 = document.getElementById("signup-sub-hl");
		oEmail  = document.getElementById("email-sub-hl");
		oZip	= document.getElementById("zip-sub-hl");
	}
	
	if (objForm2) {
		objBtn2	   = document.getElementById("btnFinish");
		oEmail2	   = document.getElementById("txtEmailTip2");
		oPassword2 = document.getElementById("txtPasswordTip2");
		oFullName2 = document.getElementById("txtFullNameTip");
		
		oFromHL	   = document.getElementById("txtFromHLTip2");
	}
	
	var sEmailDefaultValue = "EMAIL ADDRESS";
	var sPwdDefaulValue	   = "CHOOSE A PASSWORD";
	
	if (bNeedToSaveItem_Tooltip) {
		objForm  = document.frmLoginTip;
		objForm2 = document.frmLoginTip;
		
		if (objForm) {
			objBtn     = document.getElementById("btnLogin");
			objBtn2	   = document.getElementById("btnLogin");
			
			oEmail     = document.getElementById("txtEmailTip");
			oEmail2    = document.getElementById("txtEmailTip");
			oPassword  = document.getElementById("txtPasswordTip");
			oPassword2 = document.getElementById("txtPasswordTip");
		}
		objBtn3 = oLibItemLyr_Tooltip;
		
		sEmailDefaultValue = "YOUR EMAIL ADDRESS";
		sPwdDefaulValue	   = "YOUR PASSWORD";
	}
	
	
	if (oAJAX.readyState == 4) { // if request of file completed
		if (oAJAX.status == 200 || window.location.href.indexOf("http") == -1) {
			if (myfiletype == "txt") {
				if (objBtn3) {
					if ($(objBtn3).data("qtip"))
						$(objBtn3).qtip("enable");
				}
				
				
				var sResult = oAJAX.responseText;
				
				if (sResult != "") {
					var arrResult = sResult.split("~");
					
					if (arrResult[0] == "err" && arrResult.length == 4) {
						if (arrResult[1] == "empty-email") {
							if (bNeedToSaveItem_Tooltip) {
								alert("Please enter your email address");
							}
							else {
								alert("Missing e-mail address. Please try to sign up again.");
								
								if (objBtn) {
									if ($(objBtn).data("qtip"))
										$(objBtn).qtip("hide");
								}
							}
							
							oEmail.disabled = false;
							if (oZip) oZip.disabled = false;
							if (oPassword) oPassword.disabled = false;
							if (objBtn) objBtn.disabled = false;
							
							if (!bNeedToSaveItem_Tooltip) oEmail.value = sEmailDefaultValue;
							oEmail.focus();
						}
						else if (arrResult[1] == "invalid-email") {
							if (bNeedToSaveItem_Tooltip) {
								alert("Please enter a valid e-mail address");
							}
							else {
								alert("Invalid e-mail address. Please try to sign up again.");
								
								if (objBtn) {
									if ($(objBtn).data("qtip"))
										$(objBtn).qtip("hide");
								}
							}
							
							oEmail.disabled = false;
							if (oZip) oZip.disabled = false;
							if (oPassword) oPassword.disabled = false;
							if (objBtn) objBtn.disabled = false;
							
							if (!bNeedToSaveItem_Tooltip) oEmail.value = sEmailDefaultValue;
							oEmail.focus();
						}
						else if (arrResult[1] == "empty-password") {
							if (bNeedToSaveItem_Tooltip)
								alert("Please enter your password")
							else
								alert("Please choose a password");
							
							if (oFullName2)
								oFullName2.disabled = false
							else if (oEmail2)
								oEmail2.disabled	= false;
								
							oPassword2.disabled = false;
							if (objBtn2) objBtn2.disabled = false;
							
							if (!bNeedToSaveItem_Tooltip) oPassword2.value = sPwdDefaulValue;
							oPassword2.focus();
						}
						else if (arrResult[1] == "invalid-password-2") {
							alert("Password must be at least six characters");
							
							if (oFullName2)
								oFullName2.disabled = false
							else if (oEmail2)
								oEmail2.disabled	= false;
								
							oPassword2.disabled = false;
							if (objBtn2) objBtn2.disabled = false;
							
							oPassword2.focus();
						}
						else if (arrResult[1] == "invalid-password-3") {
							alert('Password cannot contain " < > % characters');
							
							if (oFullName2)
								oFullName2.disabled = false
							else if (oEmail2)
								oEmail2.disabled	= false;
								
							oPassword2.disabled = false;
							if (objBtn2) objBtn2.disabled = false;
							
							oPassword2.focus();
						}
						else if (arrResult[1] == "email-exist") {
							if (bNeedToSaveItem_Tooltip) {
								alert('You have already had an Jewish Ideas Daily account.\nPlease click "LOG IN" button to log in.');
								
								oEmail.disabled	 = false;
								oPassword.disabled = false;
								if (objBtn) objBtn.disabled = false;
							}
							else {
								alert("You have already had an Jewish Ideas Daily account");
								
								if (objBtn) {
									if ($(objBtn).data("qtip"))
										$(objBtn).qtip("hide");
								}
								
								oEmail.disabled = false;
								if (oZip) oZip.disabled = false;
								if (objBtn) objBtn.disabled = false;
								
								oEmail.value = sEmailDefaultValue;
							}
						}
						else {
							alert("Error when invoking AJAX function");
							
							if (oFullName2)
								oFullName2.disabled = false
							else if (oEmail2)
								oEmail2.disabled	= false;
								
							oPassword2.disabled = false;
							if (objBtn2) objBtn2.disabled = false;
						}
					}
					else if (arrResult[0] != "err" && arrResult.length == 4) {
						if (bNeedToSaveItem_Tooltip) {
							// hide login tooltip
							if (oLibItemLyr_Tooltip) {
								if ($(oLibItemLyr_Tooltip).data("qtip"))
									$(oLibItemLyr_Tooltip).qtip("hide");
							}
						}
						else {
							// show welcome tooltip
							var iFromHL = 0;
							if (oFromHL) {
								if (oFromHL.value == "1")
									iFromHL = 1;
							}
							
							showTooltipWelcome(iFromHL, trim(oFullName2.value), arrResult[2], arrResult[3]);
						}
					}
					else {
						alert("Error when invoking AJAX function");
						
						if (oFullName2)
							oFullName2.disabled = false
						else if (oEmail2)
							oEmail2.disabled	= false;
							
						oPassword2.disabled = false;
						if (objBtn2) objBtn2.disabled = false;
					}
				}
				else {
					alert("Error when invoking AJAX function");
					
					if (oFullName2)
						oFullName2.disabled = false
					else if (oEmail2)
						oEmail2.disabled	= false;
						
					oPassword2.disabled = false;
					if (objBtn2) objBtn2.disabled = false;
				}
			}
		}
		else {
			alert("Error when invoking AJAX function:\n\rStatus: " + oAJAX.status + "\n\rError Description:\n\r" + oAJAX.responseText);
			
			if (oFullName2)
				oFullName2.disabled = false
			else if (oEmail2)
				oEmail2.disabled	= false;
				
			oPassword2.disabled = false;
			if (objBtn2) objBtn2.disabled = false;
			
			oEmail.disabled = false;
			if (oZip) oZip.disabled = false;
			if (objBtn) objBtn.disabled = false;
			
			if (objBtn3) {
				if ($(objBtn3).data("qtip"))
					$(objBtn3).qtip("enable");
			}
		}
	}
	else {
		if (oFullName2)
			oFullName2.disabled = true
		else if (oEmail2)
			oEmail2.disabled	= true;
			
		oPassword2.disabled = true;
		if (objBtn2) objBtn2.disabled = true;
		
		oEmail.disabled = true;
		if (oZip) oZip.disabled = true;
		if (objBtn) objBtn.disabled = true;
		
		if (objBtn3) {
			if ($(objBtn3).data("qtip"))
				$(objBtn3).qtip("disable");
		}
	}
}

function showTooltipWelcome(pFromHL, pFullName, pBaseRef, pBaseRefSSL)
{
	var obj = document.getElementById("signup-sub");
	
	// disable sign up fields
	if (pFromHL == 1) {
		obj = document.getElementById("signup-sub-hl");
		
		document.getElementById("email-sub-hl").disabled  = true;
		document.getElementById("zip-sub-hl").disabled    = true;
		document.getElementById("signup-sub-hl").disabled = true;
	}
	else {
		document.getElementById("email-sub").disabled	  = true;
		document.getElementById("signup-sub").disabled    = true;
	}
	
	
	if (obj) {
		// destroy the tooltip
		if ($(obj).data("qtip")) $(obj).qtip("destroy");
		
		// create the tooltip
		$(obj).qtip(
		{
			// content of the tooltip window
			content: {
				// tooltip content
				text: '',
				// set tooltip content with URL
				url: "/include/signup_welcome.asp?hl=" + pFromHL,
				title: {
					text: '<h2 class="wintitletip">Welcome to Jewish Ideas Daily!</h2>',	// title of the tooltip window
					button: '<img src="/i/tooltip/bCloseWin.gif">'							// show a close button in the title
				}
			},
			position: {
				// the tooltip will be positioned with it's top right hand corner touching the targets bottom left hand corner
				corner: {
					tooltip: 'topRight',		// position of the tooltip corner
					target: 'bottomLeft'		// the opposite
				},
				adjust: {
					x: 90,						// x value of the tooltip coordinate (counting from corner.tooltip)
					y: 8,						// y value of the tooltip coordinate (counting from corner.tooltip)
					screen: true				// keep the tooltip on-screen at all times
				}
			},
			show: {
				solo: true						// only show one tooltip at a time
			},
			hide: false,						// don't specify a hide event
			style: {
				padding: '0px 10px 10px 10px',	// tooltip content padding
				border: {
					width: 4,					// tooltip border width
					radius: 0,					// rounded corner
					color: '#0D6374'			// tooltip border color
				},
				title: {
					background: '#FFFFFF',		// tooltip title background
					padding: '5px'				// tooltip title padding
				},
				name: 'light',					// use the default light style
				width: 410						// set the tooltip width
			},
			api: {
				// called after the tooltip is hidden
				onHide: function() {
					// enable sign up fields
					if (pFromHL == 1) {
						document.getElementById("email-sub-hl").disabled  = false;
						document.getElementById("zip-sub-hl").disabled    = false;
						document.getElementById("signup-sub-hl").disabled = false;
						
						document.getElementById("email-sub-hl").value	  = "EMAIL ADDRESS";
						document.getElementById("zip-sub-hl").value		  = "ZIP CODE (optional)";
					}
					else {
						document.getElementById("email-sub").disabled  = false;
						document.getElementById("signup-sub").disabled = false;
						
						document.getElementById("email-sub").value	 = "EMAIL ADDRESS";
					}
					
					// the tooltip will be temporarily disabled, and will not fire any events e.g. hide, show, focus
					$(obj).qtip("disable");
					
					// show welcome text, etc.
					showWelcomeText();
				}
			}
		});
		
		// auto-show the tooltip (based on show.when --> by default is mouseover)
		$(obj).mouseover();
	}
}

function hideTooltipWelcome(pFromHL)
{
	if (pFromHL == 1) {
		if ($(document.getElementById("signup-sub-hl")).data("qtip"))
			$(document.getElementById("signup-sub-hl")).qtip("hide");
	}
	else {
		if ($(document.getElementById("signup-sub")).data("qtip"))
			$(document.getElementById("signup-sub")).qtip("hide");
	}
}

function showWelcomeText()
{
	// must reload window to get correct SAVED item status
	window.location.reload();
}

// ===========================================================================

function loginFromTooltip(pIsAjax, pItemType, pItemID, pItemTitle)
{
	if (trim(document.getElementById("txtEmailTip").value) == "" || trim(document.getElementById("txtEmailTip").value).toUpperCase() == "YOUR EMAIL ADDRESS") {
		alert("Please enter your e-mail");
		document.getElementById("txtEmailTip").focus();
		return false;
	}
	else if (!isValidEmail(trim(document.getElementById("txtEmailTip").value))) {
		alert("Please enter a valid e-mail address");
		document.getElementById("txtEmailTip").focus();
		return false;
	}
	else if (trim(document.getElementById("txtPasswordTip").value) == "" || trim(document.getElementById("txtPasswordTip").value).toUpperCase() == "YOUR PASSWORD") {
		alert("Please enter your password");
		document.getElementById("txtPasswordTip").focus();
		return false;
	}
	else if (trim(document.getElementById("txtPasswordTip").value).length < 6) {
		alert("Password must be at least six characters");
		document.getElementById("txtPasswordTip").focus();
		return false;
	}
	else if (trim(document.getElementById("txtPasswordTip").value).indexOf('"') >= 0 || trim(document.getElementById("txtPasswordTip").value).indexOf("<") >= 0 || trim(document.getElementById("txtPasswordTip").value).indexOf(">") >= 0 || trim(document.getElementById("txtPasswordTip").value).indexOf("%") >= 0) {
		alert('Password cannot contain " < > % characters');
		document.getElementById("txtPasswordTip").focus();
		return false;
	}
	else {
		if (pIsAjax && pItemType && pItemID && pItemTitle) {
			bNeedToSaveItem_Tooltip = true;
			bLibAjax_Tooltip		= pIsAjax;
			oLibItemLyr_Tooltip		= document.getElementById("lyrLibItem_" + pItemType + "_" + pItemID);
			sLibItemType_Tooltip	= pItemType;
			iLibItemID_Tooltip		= pItemID;
			sLibItemTitle_Tooltip	= trim(unescape(pItemTitle));
			
			bSignUpFromHL			= false;
		}
		else {
			bNeedToSaveItem_Tooltip = false;
			bLibAjax_Tooltip		= 0;
			oLibItemLyr_Tooltip		= null;
			sLibItemType_Tooltip	= "";
			iLibItemID_Tooltip		= 0;
			sLibItemTitle_Tooltip	= "";
			
			if (document.getElementById("txtFromHLTip")) {
				if (trim(document.getElementById("txtFromHLTip").value) == "1")
					bSignUpFromHL = true
				else
					bSignUpFromHL = false;
			}
			else if (document.getElementById("txtFromHLTip2")) {
				if (trim(document.getElementById("txtFromHLTip2").value) == "1")
					bSignUpFromHL = true
				else
					bSignUpFromHL = false;
			}
			else
				bSignUpFromHL = false;
		}
	
		var sParam = "txtEmail=" + escape(trim(document.getElementById("txtEmailTip").value)) +
					 "&txtPassword=" + escape(trim(document.getElementById("txtPasswordTip").value));
		var sURL   = "/include/signup_login_process.asp?" + sParam;
		ajaxpack.postAjaxRequest(sURL, "", processLoginFromTooltip, "txt");
		
		return false;
	}
}

function processLoginFromTooltip()
{
	var oAJAX	   = ajaxpack.ajaxobj;
	var myfiletype = ajaxpack.filetype;
	
	var objBtn	   = document.getElementById("signup-sub");
	if (bNeedToSaveItem_Tooltip)
		objBtn = oLibItemLyr_Tooltip
	else (bSignUpFromHL)
		objBtn = document.getElementById("signup-sub-hl");
	
	
	if (oAJAX.readyState == 4) { // if request of file completed
		if (oAJAX.status == 200 || window.location.href.indexOf("http") == -1) {
			if (myfiletype == "txt") {
				if (objBtn) {
					if ($(objBtn).data("qtip"))
						$(objBtn).qtip("enable");
				}
				
				var sResult = oAJAX.responseText;
				
				if (sResult != "") {
					var arrResult = sResult.split("~");
					
					if (arrResult[0] == "err" && arrResult.length == 6) {
						if (arrResult[1] == "empty-email") {
							alert("Please enter your email address");
							
							document.getElementById("txtEmailTip").disabled	   = false;
							document.getElementById("txtPasswordTip").disabled = false;
							document.getElementById("btnLogin").disabled	   = false;
							
							document.getElementById("txtEmailTip").focus();
						}
						else if (arrResult[1] == "invalid-email") {
							alert("Please enter a valid e-mail address");
							
							document.getElementById("txtEmailTip").disabled	   = false;
							document.getElementById("txtPasswordTip").disabled = false;
							document.getElementById("btnLogin").disabled	   = false;
							
							document.getElementById("txtEmailTip").focus();
						}
						else if (arrResult[1] == "empty-password") {
							alert("Please enter your password");
							
							document.getElementById("txtEmailTip").disabled	   = false;
							document.getElementById("txtPasswordTip").disabled = false;
							document.getElementById("btnLogin").disabled	   = false;
							
							document.getElementById("txtPasswordTip").focus();
						}
						else if (arrResult[1] == "invalid-password-2") {
							alert("Password must be at least six characters");
							
							document.getElementById("txtEmailTip").disabled	   = false;
							document.getElementById("txtPasswordTip").disabled = false;
							document.getElementById("btnLogin").disabled	   = false;
							
							document.getElementById("txtPasswordTip").focus();
						}
						else if (arrResult[1] == "invalid-password-3") {
							alert('Password cannot contain " < > % characters');
							
							document.getElementById("txtEmailTip").disabled	   = false;
							document.getElementById("txtPasswordTip").disabled = false;
							document.getElementById("btnLogin").disabled	   = false;
							
							document.getElementById("txtPasswordTip").focus();
						}
						else if (arrResult[1] == "user-notfound") {
							alert("The e-mail address and password you entered do not match any accounts on record.\nPlease make sure that you have correctly entered the e-mail address associated with your Jewish Ideas Daily account.");
							
							document.getElementById("txtEmailTip").disabled	   = false;
							document.getElementById("txtPasswordTip").disabled = false;
							document.getElementById("btnLogin").disabled	   = false;
							
							document.getElementById("txtEmailTip").focus();
						}
						else {
							alert("Error when invoking AJAX function");
							
							document.getElementById("txtEmailTip").disabled	   = false;
							document.getElementById("txtPasswordTip").disabled = false;
							document.getElementById("btnLogin").disabled	   = false;
						}
					}
					else if (arrResult[0] != "err" && arrResult.length == 6) {
						// hide login tooltip
						if (bNeedToSaveItem_Tooltip) {
							if (oLibItemLyr_Tooltip) {
								if ($(oLibItemLyr_Tooltip).data("qtip"))
									$(oLibItemLyr_Tooltip).qtip("hide");
							}
						}
						else if (bSignUpFromHL) {
							if (document.getElementById("signup-sub-hl")) {
								if ($(document.getElementById("signup-sub-hl")).data("qtip"))
									$(document.getElementById("signup-sub-hl")).qtip("hide");
									
								// show welcome text, etc.
								showWelcomeText();
							}
						}
						else {
							if (document.getElementById("signup-sub")) {
								if ($(document.getElementById("signup-sub")).data("qtip"))
									$(document.getElementById("signup-sub")).qtip("hide");
									
								// show welcome text, etc.
								showWelcomeText();
							}
						}
					}
					else {
						alert("Error when invoking AJAX function");
						
						document.getElementById("txtEmailTip").disabled	   = false;
						document.getElementById("txtPasswordTip").disabled = false;
						document.getElementById("btnLogin").disabled	   = false;
					}
				}
				else {
					alert("Error when invoking AJAX function");
					
					document.getElementById("txtEmailTip").disabled	   = false;
					document.getElementById("txtPasswordTip").disabled = false;
					document.getElementById("btnLogin").disabled	   = false;
				}
			}
		}
		else {
			alert("Error when invoking AJAX function:\n\rStatus: " + oAJAX.status + "\n\rError Description:\n\r" + oAJAX.responseText);
			
			document.getElementById("txtEmailTip").disabled	   = false;
			document.getElementById("txtPasswordTip").disabled = false;
			document.getElementById("btnLogin").disabled	   = false;
			
			if (objBtn) {
				if ($(objBtn).data("qtip"))
					$(objBtn).qtip("enable");
			}
		}
	}
	else {
		document.getElementById("txtEmailTip").disabled	   = true;
		document.getElementById("txtPasswordTip").disabled = true;
		document.getElementById("btnLogin").disabled	   = true;
		
		if (objBtn) {
			if ($(objBtn).data("qtip"))
				$(objBtn).qtip("disable");
		}
	}
}

function forgotPasswordFromTooltip(pBaseRef, pBaseRefSSL, pIsAjax, pItemType, pItemID, pItemTitle)
{
	var sForgotURL;
	
	if (pIsAjax && pItemType && pItemID && pItemTitle) {
		// set forgot password URL
		var sParam		 = "type=" + pItemType + "&id=" + pItemID;
		var sSaveItemURL = "/content/content_save.asp?" + sParam;
		
		sForgotURL		 = pBaseRefSSL + "/my_library/forgot_password.asp?et=";
		
		if (trim(document.getElementById("txtEmailTip").value) != "" && trim(document.getElementById("txtEmailTip").value).toUpperCase() != "YOUR EMAIL ADDRESS")
			sForgotURL += escape(trim(document.getElementById("txtEmailTip").value));
		
		sForgotURL += "&url=" + escape(sSaveItemURL);
		
		
		// hide login tooltip
		oLibItemLyr_Tooltip = document.getElementById("lyrLibItem_" + pItemType + "_" + pItemID);
		bForgotPwd			= true;
		
		if (oLibItemLyr_Tooltip) {
			if ($(oLibItemLyr_Tooltip).data("qtip"))
				$(oLibItemLyr_Tooltip).qtip("hide");
		}
		
		// reset variable
		bNeedToSaveItem_Tooltip = false;
		bLibAjax_Tooltip		= 0;
		oLibItemLyr_Tooltip		= null;
		sLibItemType_Tooltip	= "";
		iLibItemID_Tooltip		= 0;
		sLibItemTitle_Tooltip	= "";
		
		bForgotPwd				= false;
		bSignUpFromHL			= false;
		
		// redirect to forgot password page
		window.location.replace(sForgotURL);
	}
	else {
		bNeedToSaveItem_Tooltip = false;
		bLibAjax_Tooltip		= 0;
		oLibItemLyr_Tooltip		= null;
		sLibItemType_Tooltip	= "";
		iLibItemID_Tooltip		= 0;
		sLibItemTitle_Tooltip	= "";
		
		// redirect to forgot password page
		sForgotURL = pBaseRefSSL + "/my_library/forgot_password.asp";
		if (document.getElementById("txtEmailTip")) {
			if (trim(document.getElementById("txtEmailTip").value) != "" && trim(document.getElementById("txtEmailTip").value).toUpperCase() != "YOUR EMAIL ADDRESS")
				sForgotURL += "?et=" + escape(trim(document.getElementById("txtEmailTip").value));
		}
			
		window.location.replace(sForgotURL);
	}
}

function showTooltipLogin(pIsAjax, pItemType, pItemID, pItemTitle)
{
	// destroy tooltip from previous "SAVE" link to avoid error undefined form field
	if (oLibItemLyr_Tooltip_Prev) {
		if ($(oLibItemLyr_Tooltip_Prev).data("qtip")) $(oLibItemLyr_Tooltip_Prev).qtip("destroy");
	}
	
	
	var obj = document.getElementById("lyrLibItem_" + pItemType + "_" + pItemID);
	
	if (obj) {
		oLibItemLyr_Tooltip_Prev = obj;
		
		
		// destroy the tooltip
		if ($(obj).data("qtip")) $(obj).qtip("destroy");
		
		// create the tooltip
		$(obj).qtip(
		{
			// content of the tooltip window
			content: {
				// tooltip content
				text: '',
				// set tooltip content with URL
				url: "/include/signup_login.asp?type=" + pItemType + "&id=" + pItemID + "&title=" + escape(pItemTitle),
				title: {
					text: '<h2 class="wintitletip">Log In to Save Articles</h2>',	// title of the tooltip window
					button: '<img src="/i/tooltip/bCloseWin.gif">'                 // show a close button in the title
				}
			},
			position: {
				// the tooltip will be positioned with it's top middle hand corner touching the targets bottom middle hand corner
				corner: {
					tooltip: 'topMiddle',		// position of the tooltip corner
					target: 'bottomMiddle'		// the opposite
				},
				adjust: {
					screen: true				// keep the tooltip on-screen at all times
				}
			},
			show: {
				when: 'click',					// specify a show event
				solo: true						// only show one tooltip at a time
			},
			hide: false,			//'unhide' ,ag- feb 24, 2010 - removed because using x button					// hide tooltip when unfocus
			style: {
				tip: true,						// apply a speech bubble tip to the tooltip at the designated tooltip corner
				padding: '0px 10px 10px 10px',	// tooltip content padding
				border: {
					width: 4,					// tooltip border width
					radius: 0,					// rounded corner
					color: '#0D6374'			// tooltip border color
				},
				title: {
					background: '#FFFFFF',		// tooltip title background
					padding: '5px'				// tooltip title padding
				},
				name: 'light',					// use the default light style
				width: 340						// set the tooltip width
			},
			api: {
				// called after the tooltip is hidden
				onHide: function() {
					// enable link
					if (pItemType == "M" && pItemTitle != "")
						obj.innerHTML = "<a href=\"javascript: saveLibItem(1, '" + pItemType + "', " + pItemID + ", '" + pItemTitle.replace(/'/g, "\\\'").replace(/"/g, "&quot;") + "')\" class=\"save\">SAVE</a> <span class=\"title\">\"" + pItemTitle + "\"</span>"
					else
						obj.innerHTML = "<a href=\"javascript: saveLibItem(1, '" + pItemType + "', " + pItemID + ", '" + pItemTitle.replace(/'/g, "\\\'").replace(/"/g, "&quot;") + "')\" class=\"save\">SAVE</a>";
						
					// the tooltip will be temporarily disabled, and will not fire any events e.g. hide, show, focus
					$(obj).qtip("disable");
					
					// save item
					if (bNeedToSaveItem_Tooltip && !bForgotPwd) {
						var iLibAjax_Tooltip = 0;
						if (bLibAjax_Tooltip) iLibAjax_Tooltip = 1;
						
						saveLibItem(1, sLibItemType_Tooltip, iLibItemID_Tooltip, sLibItemTitle_Tooltip);
						
						// reset variable
						bNeedToSaveItem_Tooltip = false;
						bLibAjax_Tooltip		= 0;
						oLibItemLyr_Tooltip		= null;
						sLibItemType_Tooltip	= "";
						iLibItemID_Tooltip		= 0;
						sLibItemTitle_Tooltip	= "";
						
						bLibRefreshed			= true;
						bSignUpFromHL			= false;
						
						// show welcome text, etc.
						//showWelcomeText(); -- AG moved to processSaveLibItem using bLibRefreshed
					}
				}
			}
		});
		
		// disable save link with #
		if (pItemType == "M" && pItemTitle != "")
			obj.innerHTML = "<a href=\"javascript: saveLibItem(1, '" + pItemType + "', " + pItemID + ", '" + pItemTitle.replace(/'/g, "\\\'").replace(/"/g, "&quot;") + "')\" class=\"save\">SAVE</a> <span class=\"title\">\"" + pItemTitle + "\"</span>"
		else
			obj.innerHTML = "<a href=\"javascript: saveLibItem(1, '" + pItemType + "', " + pItemID + ", '" + pItemTitle.replace(/'/g, "\\\'").replace(/"/g, "&quot;") + "')\" class=\"save\">SAVE</a>";
		
		// auto-show the tooltip (based on show.when)
		$(obj).click();
	}
}
