function showWishListPopup(item_id) {
	if ($("not_available")) {
		$("describe").removeChild($("not_available"));
	}

	var item_id = getItemID(item_id);
	if (!item_id) {
		return;
	}
	$("wish_list_item_id").value = item_id;
	
	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
}

function popupFadeIn(element, op) {
	var op1 = op/10;
	var op2 = op*10;

	$('wish_list_background').style.opacity = op1;
	$('wish_list_background').style.filter = "alpha(opacity="+op2+")";
	
	if (op < 7) {
		op = op + .75;
		var timer = setTimeout(function () {popupFadeIn(element, op);}, 15);
		return;
	}

	$('wish_list_container').style.display = "block";

	var popup_width = $("wish_list_container").offsetWidth;
	var popup_height = $("wish_list_container").offsetHeight;
	var x_scroll;
	var y_scroll;
	
	if (window.pageYOffset) {
		x_scroll = window.pageXOffset;
		y_scroll = window.pageYOffset;
	} else if (document.documentElement.scrollTop) {
		x_scroll = document.documentElement.scrollLeft;
		y_scroll = document.documentElement.scrollTop;
	} else {
		x_scroll = 0;
		y_scroll = 0;
	}
	
	var viewport_dims = getViewportDims();
	var popup_left = (viewport_dims.width/2) - (popup_width/2) + x_scroll;
	var popup_top = (viewport_dims.height/2) - (popup_height/2) + y_scroll;
	
	$('wish_list_container').style.left = popup_left + "px";
	$('wish_list_container').style.top = popup_top + "px";
	
	if ($("secret_question_input")) {
		$("secret_question_input").focus();
	}
}

function getDocumentDims() {
	return {height:document.documentElement.scrollHeight+"px", width:document.documentElement.scrollWidth+"px"}
}

function getAjaxRequest() {
	this.ajax;
	if (window.XMLHttpRequest) {
		this.ajax = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return this.ajax;
}

function getExistingWishListInfo(email_addr) {
	var wish_list_info = checkForExistingWishList(email_addr);
	if (wish_list_info) {
		wish_list_info = eval("("+wish_list_info+")");
		$("existing_secret_question").innerHTML = wish_list_info.secret_question;
	} else {
		goToWishListPopup("No Wish List found for: "+ email_addr, false);
	}
}

function checkForExistingWishList(email_addr) {
	var ajax_url = "wish_list_ajax.php?flag=1&email="+email_addr;
	var request = new getAjaxRequest();
	request.open("GET", ajax_url, false);
	request.send(null);
	return request.responseText;
}

function checkNewEmailForExistingWishList(email_addr) {
	var email_exists = checkForExistingWishList(email_addr);
	if (email_exists) {
		email_exists = eval("("+email_exists+")");
		goToWishListPopup("A Wish List with this email address already exists. Please fill in the secret question to the right, or try using a different email.", false);
		$("existing_email").value = email_exists.email;
		$("existing_secret_question").innerHTML = email_exists.secret_question;
		return true;
	}
	return false;
}

function createNewWishList() {
	//the replace function is there to strip whitespace from the beginning and end of the string
	var first_name = $("wish_list_first_name").value.replace(/^\s+|\s+$/g, "");
	var last_name = $("wish_list_last_name").value.replace(/^\s+|\s+$/g, "");
	var email = $("wish_list_email").value.replace(/^\s+|\s+$/g, "");
	var secret_question = $("wish_list_secret_question").value;
	var secret_answer = $("wish_list_secret_answer").value.replace(/^\s+|\s+$/g, "");
	var item_id = $("wish_list_item_id").value;
	
	var existing = checkForExistingWishList(email);
	if (existing) {
		goToWishListPopup("A Wish List with this email address already exists.", false);
		return;
	}
	
	var missing_fields = "";
	
	if (first_name == "") {
		missing_fields += "<li>First Name</li>";	
	}
	if (last_name == "") {
		missing_fields += "<li>Last Name</li>";
	}
	if (email == "") {
		missing_fields += "<li>Email</li>";
	}
	if (secret_question == "none") {
		missing_fields += "<li>Secret Question</li>";
	}
	if (secret_answer == "") {
		missing_fields += "<li>Secret Answer</li>";
	}

	if (missing_fields != "") {
		goToWishListPopup("<b>The following required fields are missing:</b><ul style='text-align:left;'> "+ missing_fields+"</ul>", false);
		return false;
	}

	var ajax_url = "wish_list_ajax.php?flag=2&first_name="+first_name+"&last_name="+last_name+"&email="+email+"&secret_question="+secret_question+"&secret_answer="+secret_answer+"&item_id="+item_id;
	
	request = new getAjaxRequest();
	request.open("GET", ajax_url, true);
	request.send(null);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			var wish_list_id = new Number(request.responseText);
			goToWishListPopup("Item has been added to your wish list.", wish_list_id.valueOf());
		}
	}
}

function createNewWishListOnEnter(evt) {
	evt = evt || window.event;
	if (evt.keyCode == 13) {
		createNewWishList();
	} else {
		return false;
	}
}	

function addToWishList() {
	var email_addr = $("existing_email").value;
	var secret_answer = $("existing_secret_answer").value;
	var item_id = $("wish_list_item_id").value;
	var ajax_url = "wish_list_ajax.php?flag=4&email_addr="+email_addr+"&secret_answer="+secret_answer+"&item_id="+item_id;
	request = new getAjaxRequest();
	request.open("GET", ajax_url, true);
	request.send(null);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			if (request.responseText) {
				goToWishListPopup("Item has been added to your wish list.",request.responseText);
			} else {
				goToWishListPopup("Secret answer does not match our records.",false);			
			}
		}
	}
}

function addToWishListOnEnter(evt) {
	evt = evt || window.event;
	if (evt.keyCode == 13) {
		addToWishList();
	} else {
		return false;
	}
}

function goToWishListPopup(popup_text, wish_list_id) {
	var x_scroll;
	var y_scroll;
	
	if (window.pageYOffset) {
		x_scroll = window.pageXOffset;
		y_scroll = window.pageYOffset;
	} else if (document.documentElement.scrollTop) {
		x_scroll = document.documentElement.scrollLeft;
		y_scroll = document.documentElement.scrollTop;
	} else {
		x_scroll = 0;
		y_scroll = 0;
	}

	var viewport_dims = getViewportDims();
	var popup_left = (viewport_dims.width/2) + x_scroll - 136;
	var popup_top = (viewport_dims.height/2) + y_scroll - 50;

	var go_popup = document.createElement("div");
	go_popup.style.background = "url('/image/index/gradient-left.jpg') -25px 0  repeat-y";
	go_popup.style.border = "3px ridge #000";
	go_popup.style.position = "absolute";
	go_popup.style.left = popup_left+"px"; 
	go_popup.style.top = popup_top+"px"; 
	go_popup.style.zIndex = "1001";
	go_popup.style.width = "273px";
	
	var title_div = document.createElement("div");
	title_div.style.borderBottom = "1px solid #000";
	title_div.style.backgroundColor = "#ccc";
	title_div.style.height = "20px";
	title_div.style.marginBottom = "10px";
	
	var title_text = document.createElement("div");
	title_text.innerHTML = "SLMS Wish List";
	title_text.style.position = "relative";
	title_text.style.top = "3px";
	title_text.style.fontWeight = "bold";
	title_div.appendChild(title_text);

	var close_div = document.createElement("div");
	close_div.style.position = "absolute";
	close_div.style.left = "255px";
	close_div.style.top = "1px";
	close_div.style.width = "16px";
	close_div.style.height = "16px";
	close_div.style.background = "url('/image/cancel.png') left top no-repeat";
	close_div.style.cursor = "pointer";
	close_div.onclick = function () {
		if (wish_list_id) {
			closeWishListPopup();
		}
		document.getElementsByTagName("body")[0].removeChild(go_popup);
	}
	title_div.appendChild(close_div);	
	
	var text_div = document.createElement("div");
	text_div.innerHTML = popup_text;
	text_div.style.margin = "10px";
	
	var ok_button = document.createElement("img");
	ok_button.style.cursor = "pointer";
	ok_button.style.marginBottom = "5px";
	ok_button.src = "/image/wishlist/ok_btn2.gif";
	ok_button.onmouseover = function () {
		ok_button.src = "/image/wishlist/ok_btn3.gif";
	}
	ok_button.onmouseout = function () {
		ok_button.src = "/image/wishlist/ok_btn2.gif";
	}
	ok_button.onclick = function () {
		if (wish_list_id) {
			closeWishListPopup();
		}
		document.getElementsByTagName("body")[0].removeChild(go_popup);
	}

	go_popup.appendChild(title_div);
	go_popup.appendChild(text_div);
	go_popup.appendChild(ok_button);

	if (wish_list_id) {
		var link_div = document.createElement("div");
		link_div.style.textDecoration = "underline";
		link_div.innerHTML = "Go To My Wish List";
		link_div.style.cursor = "pointer";
		link_div.style.margin = "10px 0";
		link_div.onclick = function () {
			window.location = "/wishlist_view.php?wish_list_id="+wish_list_id;
		}
		go_popup.appendChild(link_div);	
	}
	
	document.getElementsByTagName("body")[0].appendChild(go_popup);
}

function closeWishListPopup() {
	$("wish_list_background").style.display = "none";
	$("wish_list_container").style.display = "none";
	clearWishListFields();
}

function clearWishListFields() {
	$("wish_list_first_name").value = "";
	$("wish_list_last_name").value = "";
	$("wish_list_email").value = "";
	$("wish_list_secret_question").value = "none";
	$("wish_list_secret_answer").value = "";
	$("existing_email").value = "";
	$("existing_secret_question").innerHTML = "";
	$("existing_secret_answer").value = "";
}

function getViewportDims() {
	if (typeof(window.innerWidth) != "undefined") {
		return {width:window.innerWidth, height:window.innerHeight}
	} else if (typeof(document.documentElement.clientWidth != "undefined")) {
		return {width:document.documentElement.clientWidth, height:document.documentElement.clientHeight}
	} else {
		return {width:document.getElementsByTagName("body")[0].clientWidth, height:document.getElementsByTagName("body")[0].clientHeight}
	}
}

function getItemID(item_id) {
	if ((!$("size")) && (!$("color"))) {
		return item_id;
	} else if (($("size").value == "*") || ($("color").value == "*")) {
		showSizeColorError();
		return false;
	} else {
		var item_size = $("size").value;
		var item_color = $("color").value;

		var request = new getAjaxRequest();
		var ajax_url = "wish_list_ajax.php?flag=3&item_id="+item_id+"&item_size="+item_size+"&item_color="+item_color;
		
		request.open("GET", ajax_url, false);
		request.send(null);
		if (request.responseText !== false) {
			if ($("not_available")) {
				$("describe").removeChild($("not_available"));
			}
			return request.responseText;
		} else {
			return false;
		}
	}
}

function showSizeColorError() {
	var size_color = document.createElement("div");
	size_color.id = "not_available";
	size_color.innerHTML = "Please choose a size / color combination.";
	$("describe").appendChild(size_color);
}

function findAWishList(search_string) {
	document.cookie = "slms_wishlist="+search_string;
	window.location.reload(true);
	return;
}

function findAWishListOnEnter(evt) {
	evt = evt || window.event;
	if (evt.keyCode == 13) {
		findAWishList($('find_wish_list').value);		
	} else {
		return false;
	}
}

function findAWishListOld(search_string) {
	var request = new getAjaxRequest();
	var ajax_url = "wish_list_ajax.php?flag=5&search_string="+search_string;
	
	request.open("GET", ajax_url, true);
	request.send(null);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			if (request.responseText) {
				var popup_text = "<div class='wish_list_text'>";
				popup_text += "<div style='font-size:18px; padding-bottom:10px;'>The following wish lists were found:</div>";
				var lists = request.responseText.split("|");
				for (l in lists) {
					var list_items = lists[l].split(",");
					
					popup_text += "<div onClick='openWishList("+list_items[0]+")' class='wish_list_link'>"+list_items[2]+" "+list_items[3]+" - "+list_items[1]+"</div>";
				}
				
				popup_text += "</div>";
				$("wish_list_tile").innerHTML = popup_text;
				showFoundWishListPopup();
			} else {
				$("wish_list_tile").innerHTML = "<div class='wish_list_text' style='font-size:18px; padding:0px 15px;'>No Results Found</div>";
				showFoundWishListPopup();			
			}
		}
	}
}

function showFoundWishListPopup() {
	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
}

function closeFoundWishListPopup() {
	$("wish_list_background").style.display = "none";
	$("wish_list_container").style.display = "none";
}

function openWishList(wish_list_id) {
	window.location = "/wishlist_view.php?wish_list_id="+wish_list_id;
}

function deleteWishList(wish_list_name, wish_list_id, wish_list_email) {
	var delete_text = document.createElement("div");
	delete_text.className = "wish_list_text";
	delete_text.id = "delete_msg";
	delete_text.style.fontSize = "20px";
	delete_text.style.paddingBottom = "20px";
	delete_text.innerHTML = "Delete the Wish List for <span>"+wish_list_name+"?</span>";

	var yes_img = document.createElement("img");
	yes_img.style.marginRight = "3px";
	yes_img.id = "yes_btn";
	yes_img.src = "/image/wishlist/yes-btn.gif";
	yes_img.onclick = function () {
		yesDeleteVerify(wish_list_id, wish_list_email);
	}
	
	var no_img = document.createElement("img");
	no_img.style.marginLeft = "3px";
	no_img.id = "no_btn";
	no_img.src = "/image/wishlist/no-btn.gif";
	no_img.onclick = function () {
		hideDeleteWishListPopup();
	}
	
	$('wish_list_tile').appendChild(delete_text);
	$('wish_list_tile').appendChild(yes_img);
	$('wish_list_tile').appendChild(no_img);
	
	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
}

function yesDeleteVerify(wish_list_id, wish_list_email) {
	hideDeleteWishListPopup();

	var wish_list_info = checkForExistingWishList(wish_list_email);

	if (wish_list_info) {
		 wish_list_info = eval("("+wish_list_info+")");
	} else {
		alert("Could not find wish list");
		return;
	}
	
	if (wishListQuestionAnswered(wish_list_email)) {
		yesDeleteWishList(wish_list_id, wish_list_email);		
		return;
	}

	var bottom_row_div = document.createElement("div");
	bottom_row_div.id = "bottom_row_div";
	bottom_row_div.style.paddingBottom = "20px";
		
	var delete_msg = getPopupMsgDiv(wish_list_info);
	var secret_question_span = getPopupMsgSpan(wish_list_info);
	var secret_question_input = getPopupSecretQuestionInput();
	secret_question_input.onkeyup = function (event) {
		event = event || window.event;
		if (event.keyCode == 13) {
			if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
				yesDeleteWishList(wish_list_id, wish_list_email);
				setWishListQuestionCookie(wish_list_email);
			} else {
				goToWishListPopup("Secret answer does not match our records.",false);
			}
		}
	}

	var submit_button = getPopupSubmitButton();
	submit_button.onclick = function () {
		if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
			yesDeleteWishList(wish_list_id, wish_list_email);
			setWishListQuestionCookie(wish_list_email);
		} else {
			goToWishListPopup("Secret answer does not match our records.",false);
		}
	}
	submit_button.onkeyup = function (event) {
		event = event || window.event;
		if (event.keyCode == 13) {
			if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
				yesDeleteWishList(wish_list_id, wish_list_email);
				setWishListQuestionCookie(wish_list_email);
			} else {
				goToWishListPopup("Secret answer does not match our records.",false);
			}
		}
	}
	
	$("wish_list_tile").appendChild(delete_msg);
	$("wish_list_tile").appendChild(bottom_row_div);
	bottom_row_div.appendChild(secret_question_span);
	bottom_row_div.appendChild(secret_question_input);
	bottom_row_div.appendChild(submit_button);

	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
}

function yesDeleteWishList(wish_list_id, wish_list_email) {
	hideDeleteWishListPopup();
	
	var yes_text = document.createElement("div");
	yes_text.className = "wish_list_text";
	yes_text.id = "delete_msg";
	yes_text.style.fontSize = "20px";
	yes_text.style.padding = "20px";
	yes_text.innerHTML = "Your Wish List is almost deleted. You will need to click the verification link sent to <span style='color:#9D0A0E;'>"+wish_list_email+"</span>"; 
	
	$('wish_list_tile').appendChild(yes_text);	

	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
	
	var wish_list_info = checkForExistingWishList(wish_list_email);
	
	if (wish_list_info) {
		wish_list_info = eval("("+wish_list_info+")");
	}
	var wish_list_name = wish_list_info.first_name + " " + wish_list_info.last_name;

	var request = getAjaxRequest();
	var ajax_url = "wish_list_ajax.php?flag=10&wish_list_email="+wish_list_email+"&wish_list_id="+wish_list_id+"&wish_list_name="+wish_list_name;
	
	request.open("GET", ajax_url, true);
	request.send(null);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			//alert(request.responseText);
		}
	}
}

function hideDeleteWishListPopup() {
	if ($("delete_msg")) {
		$("wish_list_tile").removeChild($("delete_msg"));
	}	
	if ($("yes_btn")) {
		$("wish_list_tile").removeChild($("yes_btn"));
	}
	if ($("no_btn")) {
		$("wish_list_tile").removeChild($("no_btn"));
	}
	if ($("bottom_row_div")) {
		$("wish_list_tile").removeChild($("bottom_row_div"));
	}
	if ($("share_top")) {
		$("wish_list_tile").removeChild($("share_top"));
	}
	if ($("share_left_div")) {
		$("wish_list_tile").removeChild($("share_left_div"));
	}
	if ($("share_right_div")) {
		$("wish_list_tile").removeChild($("share_right_div"));
	}
	
	$('wish_list_tile').style.height = "auto";
	$("wish_list_background").style.display = "none";
	$("wish_list_container").style.display = "none";
}

function showRemoveText(element) {
	var remove_span = element.getElementsByTagName("span");
	remove_span[0].style.display = "inline";
}
function hideRemoveText(element) {
	var remove_span = element.getElementsByTagName("span");
	remove_span[0].style.display = "none";
}

function removeFromWishList(email, item_id) {
	var wish_list_info = checkForExistingWishList(email);

	if (wish_list_info) {
		 wish_list_info = eval("("+wish_list_info+")");
	} else {
		alert("Could not find wish list");
		return;
	}
	
	if (wishListQuestionAnswered(email)) {
		var request2 = new getAjaxRequest();
		var ajax_url2 = "wish_list_ajax.php?flag=6&wish_list_id="+wish_list_info.id+"&item_id="+item_id;
		request2.open("GET", ajax_url2, false);
		request2.send(null);
		window.location.reload(true);
		return;
	}
	
	var bottom_row_div = document.createElement("div");
	bottom_row_div.id = "bottom_row_div";
	bottom_row_div.style.paddingBottom = "20px";
		
	var delete_msg = getPopupMsgDiv(wish_list_info);
	var secret_question_span = getPopupMsgSpan(wish_list_info);
	var secret_question_input = getPopupSecretQuestionInput();
	secret_question_input.onkeyup = function (event) {
		event = event || window.event;
		if (event.keyCode == 13) {
			if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
				var request2 = new getAjaxRequest();
				var ajax_url2 = "wish_list_ajax.php?flag=6&wish_list_id="+wish_list_info.id+"&item_id="+item_id;
				request2.open("GET", ajax_url2, false);
				request2.send(null);
				hideDeleteWishListPopup();
				setWishListQuestionCookie(email);
				window.location.reload(true);
			} else {
				goToWishListPopup("Secret answer does not match our records.",false);
			}
		}
	}

	var submit_button = getPopupSubmitButton();
	submit_button.onclick = function () {
		if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
			var request2 = new getAjaxRequest();
			var ajax_url2 = "wish_list_ajax.php?flag=6&wish_list_id="+wish_list_info.id+"&item_id="+item_id;
			request2.open("GET", ajax_url2, false);
			request2.send(null);
			hideDeleteWishListPopup();
			setWishListQuestionCookie(email);
			window.location.reload(true);
		} else {
			goToWishListPopup("Secret answer does not match our records.",false);
		}
	}
	submit_button.onkeyup = function (event) {
		event = event || window.event;
		if (event.keyCode == 13) {
			if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
				var request2 = new getAjaxRequest();
				var ajax_url2 = "wish_list_ajax.php?flag=6&wish_list_id="+wish_list_info.id+"&item_id="+item_id;
				request2.open("GET", ajax_url2, false);
				request2.send(null);
				hideDeleteWishListPopup();
				setWishListQuestionCookie(email);
				window.location.reload(true);
			} else {
				goToWishListPopup("Secret answer does not match our records.",false);
			}
		}
	}
	
	$("wish_list_tile").appendChild(delete_msg);
	$("wish_list_tile").appendChild(bottom_row_div);
	bottom_row_div.appendChild(secret_question_span);
	bottom_row_div.appendChild(secret_question_input);
	bottom_row_div.appendChild(submit_button);

	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
}

function changeWishListQty(email, qty, item_id) {
	var quantity = parseInt(qty);
	if (isNaN(quantity)) {
		goToWishListPopup("Quantity must be a number.",false);
		return false;
	}

	var wish_list_info = checkForExistingWishList(email);

	if (wish_list_info) {
		 wish_list_info = eval("("+wish_list_info+")");
	} else {
		alert("Could not find wish list");
		return;
	}
	
	if (wishListQuestionAnswered(email)) {
		changeWishListQtyAjax(wish_list_info, quantity, item_id);
		return;
	}

	var bottom_row_div = document.createElement("div");
	bottom_row_div.id = "bottom_row_div";
	bottom_row_div.style.paddingBottom = "20px";

	var delete_msg = getPopupMsgDiv(wish_list_info);
	var secret_question_span = getPopupMsgSpan(wish_list_info);
	var secret_question_input = getPopupSecretQuestionInput();
	
	secret_question_input.onkeyup = function (event) {
		event = event || window.event;
		if (event.keyCode == 13) {
			if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
				changeWishListQtyAjax(wish_list_info, quantity, item_id);
				setWishListQuestionCookie(email);
			} else {
				goToWishListPopup("Secret answer does not match our records.",false);
			}
		}
	}
	
	var submit_button = getPopupSubmitButton();
	submit_button.onclick = function () {
		if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
			changeWishListQtyAjax(wish_list_info, quantity, item_id);
			setWishListQuestionCookie(email);
		} else {
			goToWishListPopup("Secret answer does not match our records.",false);
		}
	}
	submit_button.onkeyup = function (event) {
		event = event || window.event;
		if (event.keyCode == 13) {
			if (secret_question_input.value.toLowerCase() == wish_list_info.secret_answer.toLowerCase()) {
				changeWishListQtyAjax(wish_list_info, quantity, item_id);
				setWishListQuestionCookie(email);
			} else {
				goToWishListPopup("Secret answer does not match our records.",false);
			}
		}
	}
	
	$("wish_list_tile").appendChild(delete_msg);
	$("wish_list_tile").appendChild(bottom_row_div);
	bottom_row_div.appendChild(secret_question_span);
	bottom_row_div.appendChild(secret_question_input);
	bottom_row_div.appendChild(submit_button);
	
	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;	
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
}

function changeWishListQtyAjax(wish_list_info, quantity, item_id) {
	var request = new getAjaxRequest();
	var ajax_url = "wish_list_ajax.php?flag=7&qty="+quantity+"&wish_list_id="+wish_list_info.id+"&item_id="+item_id;
	request.open("GET", ajax_url, false);
	request.send(null);
	hideDeleteWishListPopup();
	window.location.reload(true);
}

function getPopupMsgDiv(wish_list_info) {
	var delete_msg = document.createElement("div");
	delete_msg.className = "wish_list_text";
	delete_msg.id = "delete_msg";
	delete_msg.style.fontSize = "20px";
	delete_msg.style.padding = "20px";
	var punc = wish_list_info.last_name.charAt(wish_list_info.last_name.length - 1) == "s" ? "'" : "'s";
	delete_msg.innerHTML = "Please answer the secret question for "+wish_list_info.first_name.replace(/\\/g, "")+" "+wish_list_info.last_name.replace(/\\/g, "")+punc+"<br>Wish List:";
	return delete_msg;
}

function getPopupMsgSpan(wish_list_info) {
	var secret_question_span = document.createElement("span");
	secret_question_span.id = "secret_question";
	secret_question_span.style.fontSize = "14px";
	secret_question_span.style.fontWeight = "bold";
	secret_question_span.style.color = "#9D0A0E";
	secret_question_span.style.paddingRight = "30px";
	secret_question_span.innerHTML = wish_list_info.secret_question;
	return secret_question_span;
}

function getPopupSecretQuestionInput() {
	var secret_question_input = document.createElement("input");
	secret_question_input.id = "secret_question_input";
	secret_question_input.style.fontSize = "16px";
	secret_question_input.style.height = "24px";
	secret_question_input.style.position = "relative";
	secret_question_input.style.top = "-5px";
	secret_question_input.tabIndex = "1";
	return secret_question_input;
}

function getPopupSubmitButton() {
	var submit_button = document.createElement("img");
	submit_button.src = "/image/wishlist/submitSmall-btn.gif";
	submit_button.style.position = "relative";
	submit_button.style.top = "7px";
	submit_button.style.paddingLeft = "3px";
	submit_button.tabIndex = "2";
	return submit_button;
}

function shareWithFriends(email_list, wish_list_id, owner_email, owner_name) {
	var top_msg = document.createElement("div");
	top_msg.id = "share_top";
	top_msg.className = "wish_list_text";
	top_msg.style.fontSize = "22px";
	top_msg.style.paddingTop = "15px";
	top_msg.innerHTML = "share your wish list with your friends";

	var left_div = document.createElement("div");
	left_div.id = "share_left_div";
	left_div.style.height = "260px";
	left_div.style.width = "350px";
	left_div.style.position = "absolute";
	left_div.style.left = "30px";
	left_div.style.top = "90px";
	left_div.style.borderRight = "1px solid #999999";
	
	var image_div = document.createElement("div");
	image_div.id = "slmsLogo-wishlist";
	image_div.style.width = "271px";
	image_div.style.height = "93px";
	image_div.style.position = "absolute";
	image_div.style.top = "0";
	image_div.style.left = "30px";
	image_div.style.background = "url(/image/wishlist/slmsLogo-wishlist.gif) top left no-repeat";
	
	var disclaimer_div = document.createElement("div");
	disclaimer_div.id = "disclamer_div";
	disclaimer_div.className = "wish_list_text";
	disclaimer_div.style.color = "#9D0A0E";
	disclaimer_div.style.fontWeight = "bold";
	disclaimer_div.style.paddingRight = "10px";
	disclaimer_div.style.position = "absolute";
	disclaimer_div.style.top = "150px";
	disclaimer_div.style.left = "0";
	disclaimer_div.innerHTML = "The information you provide will NOT be used for promotional purposes."
	
	var right_div = document.createElement("div");
	right_div.id = "share_right_div";
	right_div.style.height = "260px";
	right_div.style.width = "350px";
	right_div.style.position = "absolute";
	right_div.style.left = "415px";
	right_div.style.top = "90px";
	right_div.style.textAlign = "left";	
	
	var from_email_label = document.createElement("div");
	from_email_label.id = "from_email_label";
	from_email_label.className = "wish_list_text";
	from_email_label.innerHTML = "from:";
	
	var from_email_hidden_input = document.createElement("input");
	from_email_hidden_input.id = "from_email_hidden_input";
	from_email_hidden_input.type = "hidden";
	from_email_hidden_input.value = owner_email; 

	var from_email_div = document.createElement("div");
	from_email_div.id = "from_email_address";
	from_email_div.className = "wish_list_text";
	from_email_div.innerHTML = owner_name + "  ("+owner_email+")";
	
	var to_email_label = document.createElement("div");
	to_email_label.id = "to_email_label";
	to_email_label.className = "wish_list_text";
	to_email_label.style.paddingTop = "10px";
	to_email_label.innerHTML = "to:";
	
	var to_email_input = document.createElement("input");
	to_email_input.id = "to_email_input";
	to_email_input.type = "text";
	to_email_input.style.width = "290px";
	to_email_input.value = email_list;
	
	var comma_separate = document.createElement("div");
	comma_separate.id = "comma_separate";
	comma_separate.className = "wish_list_text";
	comma_separate.innerHTML = "(separate multiple addresses with a comma)";
	
	var message_label = document.createElement("div");
	message_label.id = "message_label";
	message_label.className = "wish_list_text";
	message_label.style.paddingTop = "10px";
	message_label.innerHTML = "message:";
	
	var message_textarea = document.createElement("textarea");
	message_textarea.id = "message_textarea";
	message_textarea.style.width = "290px";
	message_textarea.style.height = "75px";
	message_textarea.style.display = "block";
	
	var share_checkbox = document.createElement("input");
	share_checkbox.id = "share_checkbox";
	share_checkbox.type = "checkbox";
	share_checkbox.style.position = "absolute";
	share_checkbox.style.top = "195px";
	share_checkbox.style.left = "-5px";

	var copy_me = document.createElement("span");
	copy_me.id = "share_copy_me";
	copy_me.className = "wish_list_text";
	copy_me.style.position = "absolute";
	copy_me.style.top = "200px";
	copy_me.style.left = "20px";
	copy_me.innerHTML = "send me a copy of this email";

	var share_submit_button = document.createElement("img");
	share_submit_button.id = "share_submit_button";
	share_submit_button.src = "/image/wishlist/submitBig-btn.gif";
	share_submit_button.style.position = "absolute";
	share_submit_button.style.top = "202px";
	share_submit_button.style.left = "210px";
	share_submit_button.onclick = function () {
		var owner_email = $("from_email_hidden_input").value;
		var send_to_email = $("to_email_input").value;
		var send_message = $("message_textarea").value;
		var send_copy_me = $("share_checkbox").checked;
		shareWithFriendsEmail(send_to_email, wish_list_id, owner_email, owner_name, send_message, send_copy_me);
	}

	// Attach elements to left div
	left_div.appendChild(image_div);
	left_div.appendChild(disclaimer_div);

	// Attach elements to right div
	right_div.appendChild(from_email_label);
	right_div.appendChild(from_email_hidden_input);
	right_div.appendChild(from_email_div);
	right_div.appendChild(to_email_label);
	right_div.appendChild(to_email_input);
	right_div.appendChild(comma_separate);
	right_div.appendChild(message_label);
	right_div.appendChild(message_textarea);
	right_div.appendChild(share_checkbox);
	right_div.appendChild(copy_me);
	right_div.appendChild(share_submit_button);
	
	$('wish_list_tile').style.height = "330px";
	
	$('wish_list_tile').appendChild(top_msg);
	$('wish_list_tile').appendChild(left_div);
	$('wish_list_tile').appendChild(right_div);

	var doc = getDocumentDims();
	$('wish_list_background').style.width = doc.width;
	$('wish_list_background').style.height = doc.height;
	$('wish_list_background').style.display = "block";
	popupFadeIn($('wish_list_background'), 0.1);
}

function shareWithFriendsEmailOnEnter(wish_list_id, owner_name, evt) {
	evt = evt || window.event;
	if (evt.keyCode == 13) {
		var owner_email = $("from_email_hidden_input").value;
		var send_to_email = $("to_email_input").value;
		var send_message = $("message_textarea").value;
		var send_copy_me = $("share_checkbox").checked;
		shareWithFriendsEmail(send_to_email, wish_list_id, owner_email, owner_name,  send_message, send_copy_me);
		return true;
	}
	return false;
}

function shareWithFriendsEmail(to_email, wish_list_id, owner_email, owner_name,  message, copy_me) {
	to_email = escape(to_email);
	message = escape(message);
	var request = new getAjaxRequest();
	var ajax_url = "wish_list_ajax.php?flag=9&owner_email="+owner_email+"&owner_name="+owner_name+"&wish_list_id="+wish_list_id+"&to_email="+to_email+"&message="+message+"&copy_me="+copy_me;
	request.open("GET", ajax_url, true);
	request.send(null);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			if (request.responseText) {
				goToWishListPopup(request.responseText,false);
			}
		}
	}
	hideDeleteWishListPopup();
}

function fromWishListToCart(item_id, qty, price, session_id, customer_id, wish_list_id) {
	var request = new getAjaxRequest();
	var ajax_url = "wish_list_ajax.php?flag=8&item_id="+item_id+"&qty="+qty+"&price="+price+"&session_id="+session_id+"&customer_id="+customer_id+"&wish_list_id="+wish_list_id;
	request.open("GET", ajax_url, true);
	request.send(null);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			goToCartPopup("Item added to cart");
		}
	}
}

function goToCartPopup(popup_text) {
	var x_scroll;
	var y_scroll;
	
	if (window.pageYOffset) {
		x_scroll = window.pageXOffset;
		y_scroll = window.pageYOffset;
	} else if (document.documentElement.scrollTop) {
		x_scroll = document.documentElement.scrollLeft;
		y_scroll = document.documentElement.scrollTop;
	} else {
		x_scroll = 0;
		y_scroll = 0;
	}

	var viewport_dims = getViewportDims();
	var popup_left = (viewport_dims.width/2) + x_scroll - 136;
	var popup_top = (viewport_dims.height/2) + y_scroll - 50;

	var go_popup = document.createElement("div");
	go_popup.style.background = "url('/image/index/gradient-left.jpg') -25px 0  repeat-y";
	go_popup.style.border = "3px ridge #000";
	go_popup.style.position = "absolute";
	go_popup.style.left = popup_left+"px"; 
	go_popup.style.top = popup_top+"px"; 
	go_popup.style.zIndex = "1001";
	go_popup.style.width = "273px";
	
	var title_div = document.createElement("div");
	title_div.style.borderBottom = "1px solid #000";
	title_div.style.backgroundColor = "#ccc";
	title_div.style.height = "20px";
	title_div.style.marginBottom = "10px";
	
	var title_text = document.createElement("div");
	title_text.innerHTML = "SLMS Wish List";
	title_text.style.position = "relative";
	title_text.style.top = "3px";
	title_text.style.fontWeight = "bold";
	title_div.appendChild(title_text);

	var close_div = document.createElement("div");
	close_div.style.position = "absolute";
	close_div.style.left = "255px";
	close_div.style.top = "1px";
	close_div.style.width = "16px";
	close_div.style.height = "16px";
	close_div.style.background = "url('/image/cancel.png') left top no-repeat";
	close_div.style.cursor = "pointer";
	close_div.onclick = function () {
		document.getElementsByTagName("body")[0].removeChild(go_popup);
		window.location.reload(true);
	}
	title_div.appendChild(close_div);	
	
	var text_div = document.createElement("div");
	text_div.innerHTML = popup_text;
	text_div.style.margin = "10px";

	go_popup.appendChild(title_div);
	go_popup.appendChild(text_div);

	var link_div = document.createElement("div");
	link_div.style.textDecoration = "underline";
	link_div.innerHTML = "Go To Cart";
	link_div.style.cursor = "pointer";
	link_div.style.margin = "10px 0";
	link_div.onclick = function () {
		window.location = "/cart/";
	}
	go_popup.appendChild(link_div);	
	
	document.getElementsByTagName("body")[0].appendChild(go_popup);
}

function delete_wishlist() {
	var query_string = window.location.search.substring(1);
	var get_vars = query_string.split("&");
	
	var delete_code = "";
	for (g in get_vars) {
		var get_pieces = get_vars[g].split("=");
		if (get_pieces[0].match("confirm")) {
			delete_code = get_pieces[1];
		}
	}
	
	var request = new getAjaxRequest();
	var ajax_url = "wish_list_ajax.php?flag=11&delete_code="+delete_code;

	request.open("GET", ajax_url, true);
	request.send(null);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			if (request.responseText) {
				$("delete_text").innerHTML = "Your wish list has been deleted.";
			} else {
				$("delete_text").innerHTML = "There was a problem with deleting your wish list.<br>Please contact customer service to delete this wish list.";
			}
		}
	}
}

function setWishListQuestionCookie(email_address) {
	document.cookie = "slms_sq="+email_address;
}

function wishListQuestionAnswered(email_address) {
	var cooks = document.cookie.split(";");
	var cookie_pieces;
	for (c in cooks) {
		cookie_pieces = cooks[c].split("=");
		if (cookie_pieces[0].match("slms_sq")) {
			if (cookie_pieces[1].match(email_address)) {
				return true;
			}
		}
	}
	return false;
}