//HC:Popup window script that is dynamic and will regain focus
// Modified link system to use this one script and pass parameters
// for lighter page weight and more flexibility.
function pop(u,n,f){
    var d = "default";
    var sf = "directories,location,menubar,resizable,scrollbars,status,toolbar";
    if (!n){n = d}
    if (!f){f = sf}
    pw = window.open(u,n,f);
    pw.focus();
}

// AP: This function changes the location of the Parent window and closes the corresponding pop-up
function changeParentWindow(url) {
    opener.location=url;
    self.close();
    opener.focus();
}

function changeParentFrameWindow(url) {
    opener.top.location.href=url;
    self.close();
    opener.focus();
}

function closeWindow() {
    self.close ()
}

// AP: Timed window function, takes time parameter in seconds
var timerID = null;
function wait(time) {
    timerID = setTimeout("closeWindow()",time*1000)
}

function changeCssClassAndStatus(docObject, cssClassName, statusText) {
    docObject.className=cssClassName;
    writeStatus(statusText);
}

	function changebgColorAndStatus(docObject, bgcolor, statusText) {
		document.getElementById(docObject.id).bgColor = bgcolor ;
		writeStatus(statusText);
	}

function writeStatus(text) {
    window.status = text;
}

	var firstNameFocus = false;
	var lastNameFocus = false;
	function nameFocus(fieldName) {
		if(fieldName.name == "rfn") {
			if(!firstNameFocus) {
				firstNameFocus = true;
				fieldName.value = "";
			}
		} else if(fieldName.name == "rln") {
			if(!lastNameFocus) {
				lastNameFocus = true;
				fieldName.value = "";
			}
		}
	}
	
	function onSearchSubmit(theForm) {
		if(theForm.rfn.value == "FIRST NAME") {
			theForm.rfn.value = "";
		}
		if(theForm.rln.value == "LAST NAME") {
			theForm.rln.value = "";
		}
		return true;
	}
	
	function hyperLinkDropDown(theForm) {
		var fieldValue = theForm.options[theForm.selectedIndex].value + "";
		if(fieldValue.length > 0) {
			location.href = fieldValue;
		}
	}
	
	function onBlmHomeSearchSubmit(theForm) {
		var fuValue = theForm.fu.value + "";
		var rfnValue = theForm.rfn.value + "";
		var rlnValue = theForm.rln.value + "";
		if(rfnValue.length < 1) {
			if(fuValue.indexOf("?") == -1) {
				theForm.fu.value = theForm.fu.value + "?rfn=";
			} else {
				theForm.fu.value = theForm.fu.value + "&rfn=";
			}
			fuValue = theForm.fu.value + "";
		}
		if(rlnValue.length < 1) {
			if(fuValue.indexOf("?") == -1) {
				theForm.fu.value = theForm.fu.value + "?rln=";
			} else {
				theForm.fu.value = theForm.fu.value + "&rln=";
			}
		}
		return true;
	}

	
	//	generic pop up window opener
	function openPromoPopUp(url){
		var w = 400, h = 400;

		if (document.all) {
			/* the following is only available after onLoad */
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		} else if (document.layers) {
			w = window.innerWidth;
			h = window.innerHeight;
		}
		
		var popW = 400, popH = 400;
		var leftPos = (w/2)-200, topPos = 200;
		window.open(url, 'home_bl_pop',  'width=' + popW + ',height='+ popH + ',top=' + topPos + ',left=' + leftPos);
	}

	//	BL Home window
	function openBLPromoPop(){
		openPromoPopUp('/bloomingdales/newyou_promo04_popup.asp');
	}
	
	//	link list drop down redirect
	function refreshPage(selectObj) {
		document.location.href=selectObj.value;
	}

	function validateCheckboxes(aForm) {
		var theForm = aForm;
		var selectedProducts = false;
		for(var i = 0; i < theForm.elements.length; i++) {
			var element = theForm.elements[i];
			var type = element.type.toLowerCase();
			if( type == "checkbox" && element.name == "selectedProduct") {
				if(element.checked) {
					selectedProducts = true;
					break;
				}
			}
		}
		if (!selectedProducts) {
			updateHtml("selectedPrductsError", "<span id=\"errorfont\">Please select at least one item to add.</span>")
			alterDisplay("selectedPrductsError", "block")
			window.scrollTo(0,0);	//	Scroll to the top of the page so they see the error messaging
		}
		return selectedProducts;
	}

	function getSelectedProductsCookie() {
		var selectProductsCookie = getCookie("selProds");
		return selectProductsCookie;
	}

	function trackCheckbox(field) {
		var cookieValue = getCookie("selProds");
		var isProdIdInCookie = false;
		var uidValueFromForm = getCatUidValue();
		var defaultCookieValue = "uid" + uidValueFromForm;

		//	Set a cookie if there isn't one
		if (null == cookieValue || "undefined" == cookieValue) {
			setCookie("selProds", defaultCookieValue, null);
			splitCookieOnDelimiter = new Array(1);
			splitCookieOnDelimiter[0] = defaultCookieValue;
			cookieValue = splitCookieOnDelimiter[0];
		} else {
			splitCookieOnDelimiter = cookieValue.split("|");
		}

		var type = field.type.toLowerCase();
		if (type == "checkbox") {
			isProdIdInCookie = isValueInArray(splitCookieOnDelimiter, field.value);
			if (field.checked) {
				//	If checkbox is checked, if the selected value is NOT in the cookie, add it w/delimiter
				if (!isProdIdInCookie) {
					setCookie("selProds", cookieValue + "|" + field.value, null);
				}
			} else {
				//	Checkbox was unchecked, if the value is in cookie, remove it
				if (isProdIdInCookie) {
					//	Put the cookie back together, so as to remove the prodId from the cookie value
					cookieValue = defaultCookieValue;
					for (i = 0; i < splitCookieOnDelimiter.length; i++) {
						var currentId = splitCookieOnDelimiter[i];
						if (currentId != field.value && i != 0) {
							cookieValue += "|" + splitCookieOnDelimiter[i];
						}
					}
					setCookie("selProds", cookieValue, null);
				}
			}
		}
	}

	function isValueInArray(aArray, aValue) {
		for (i = 0; i < aArray.length; i++) {
			var currentValue = aArray[i];
			if (currentValue == aValue) {
				return true;
			}
		}
	}

	function selectCheckboxesInCookie() {
		var cookieValue = getCookie("selProds");
		var splitCookieOnDelimiter = null;
		var uidValueFromForm = getCatUidValue();
		var defaultCookieValue = "uid" + uidValueFromForm;

		//	Set a cookie if there isn't one
		if (null == cookieValue || "undefined" == cookieValue) {
			setCookie("selProds", defaultCookieValue, null);
		} else {
			splitCookieOnDelimiter = cookieValue.split("|");
			if (defaultCookieValue != splitCookieOnDelimiter[0]) {
				setCookie("selProds", defaultCookieValue, null);
				return;
			}
		}

		if (null != splitCookieOnDelimiter && splitCookieOnDelimiter.length > 0) {
			var theForm = document['productSelection'];
			for (var i = 0; i < theForm.elements.length; i++) {
				var element = theForm.elements[i];
				var type = element.type.toLowerCase();
				if (type == "checkbox") {
					if (isValueInArray(splitCookieOnDelimiter, element.value)) {
						element.checked = true;
					}
				}
			}
		}
	}

	function getCatUidValue() {
		var theForm = document['productSelection'];
		for (var i = 0; i < theForm.elements.length; i++) {
			var element = theForm.elements[i];
			var type = element.type.toLowerCase();
			if (type == "hidden" && element.name == "uid") {
				return element.value;
			}
		}
		return "";
	}

	function printPidsSelectionsNotOnThisPage() {
		//	This block creates a string for comparison
		var theForm = document['productSelection'];
		var trackPids = "";
		for(i = 0; i < theForm.elements.length; i++) {
			var element = theForm.elements[i];
			var type = element.type.toLowerCase();
			if( type == "checkbox" && element.name == "selectedProduct") {
				trackPids += "|" + element.value;
			}
		}

		//	Get the cookie of all the pids selected
		var cookieValue = getCookie("selProds");

		//	Much have a cookie value to continue on
		if (null == cookieValue || "undefined" == cookieValue) {
			return;
		} else {
			//	split the cookie and if its size is greater than one, we have selected pids
			//	if the value is within the trackPids, than the pid is already on this page, don't print hidden pid for it
			splitCookieOnDelimiter = cookieValue.split("|");
			if (splitCookieOnDelimiter.length > 1) {
				for (i = 1; i < splitCookieOnDelimiter.length; i++) {
					if (trackPids.indexOf(splitCookieOnDelimiter[i]) == -1) {	//	not on the page, print a hidden
						document.write("<input type=\"hidden\" name=\"selectedProduct\" value=\"" + splitCookieOnDelimiter[i] + "\" />");
					}
				}
			}
		}
	}

///////////////////////////////////////////////////////////////////////////////
//	getCookie(Name) - this function taken from main.js
//
//	The following function returns a cookie value, given the name of the cookie
//
//	http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/advtopic.htm#1013101
//
///////////////////////////////////////////////////////////////////////////////
	function getCookie(name) {
		var search = name + "=";
		if (document.cookie.length > 0) { // if there are any cookies
			offset = document.cookie.indexOf(search);
			if (offset != -1) { // if cookie exists
				offset += search.length;
				// set index of beginning of value
				end = document.cookie.indexOf(";", offset);
				// set index of end of cookie value
				if (end == -1){
					end = document.cookie.length
				}
				return unescape(document.cookie.substring(offset, end))
			}
		}
	}

///////////////////////////////////////////////////////////////////////////////
//	setCookie(name, value, expire) - this function taken from main.js
//
//	Sets cookie values. Expiration date is optional
//
//	null checks expire before setting it
//
//	http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/advtopic.htm#1013101
//
///////////////////////////////////////////////////////////////////////////////
	function setCookie(name, value, expire) {
		document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
	}

///////////////////////////////////////////////////////////////////////////////
//	updateHtml(tag,value) - this function taken from main.js
//
//	Alters the layer's HTML contents
//
//	null checks before calling the innerHTML property
//
///////////////////////////////////////////////////////////////////////////////
	function updateHtml(tag,value){
		if (document.all) {	//	ie
			var ieAlterDiv = document.all(tag);
			if(null != ieAlterDiv) document.all(tag).innerHTML = value;
		} else if (document.getElementById){	//	NS 6+
			var nsAlterDiv = document.getElementById(tag);
			if(null != nsAlterDiv) document.getElementById(tag).innerHTML = value;
		}
	}

///////////////////////////////////////////////////////////////////////////////
//	alterDisplay(tag,value) - this function taken from main.js
//
//	Alters the Display property of the object (div, td, tr)
//
//	null checks before calling the display property
//
///////////////////////////////////////////////////////////////////////////////
	function alterDisplay(tag,value) {
	 	if (document.all){	//	ie
			var ieAlterDiv = document.all(tag);
			if(null != ieAlterDiv) document.all(tag).style.display = value;
		} else if (document.getElementById){	//	NS 6+
			var nsAlterDiv = document.getElementById(tag);
			if(null != nsAlterDiv) document.getElementById(tag).style.display = value;
		}
	}

