// removes the requirement to view a preview before adding item to the cart [Thursday, May 08, 2008]
window.bForcePreviewBeforeAddToCart = false;
// as per Matt Lowen - do not force a proof initials on all items individually
window.bForceProofSigOnAddToCart = false;
// removes the clearing of preview signiture when preview is clicked
window.bClearProofSigOnPreview = false;
// places checkbox next to items in the cart. this is unfinished
window.bAllowMultipleCartDelete = window.devlogin;
window.minCartOrder = 25;
window.useSARTpreview = true;

function doUploadImage(f){
	var cSending = f.nSendingAgentId.value;
	var cAgent = f.nAgentId.value;
	var cAddress = f.cFile.value;
	if(window.franchise.debug){trace("_local.common.js->251->" + cAddress)}
}

function LoadingLayer(){}

function showLoadingLayer(o){
	// window.tooltip.show("Loading ... ",o,-1, false);
}

	function AddPersonalInfoPhone(){
		var output = document.getElementById("personalInfo_body")
		var insertAt = document.getElementById("personalInfo_AddPhone")
		if(output && insertAt){
			var o = new Object()

		} else {
			trace("an error has prevented the new phone field from being created, reload the page and try again");
		}
	}

function SartMarkList(){
	var FootNoteSart = document.getElementById("FootNoteSart");
	for(var me in window.sartMarkList){
		var o = window.sartMarkList[me]
		var output = document.getElementById(o.name);
		if(output){
			var ajax = new Ajax();
			ajax.method = "post";
			ajax.output = output;
			ajax.add("nFranchise", o.cat);
			ajax.add("cPartNo", o.part);
			ajax.url = "/_assets/ajaxMethods/_method.ItemHasSart.asp";
			ajax.onSuccess = function(x,t){
				if(t.toLowerCase()=="true"){
					this.output.innerHTML = window.sartMarkText;
					FootNoteSart.innerHTML = window.sartMarkText + " Items feature True-View Imaging.";
				}
			}
			ajax.onFailed = function(x){}
			ajax.send();
		}
	}
}

function SetSartMark(nFranchise, cPartNo, cName){
	window.sartMarkList[window.sartMarkList.length] = {cat:nFranchise, part:cPartNo, name:cName}
}

function ItemIsSart(nFranchise, cPartNo){

}

function LoadOfficeAgentList(cCatalog, nAgentLevel, nAgentId, nOfficeId, DefaultOffice, bTeamPanel, DefaultAgentId){
	try {
		o = getElem("nOfficeId")
		if(o){
			var ajax = new Ajax()
			ajax.method = "post";
			ajax.add("nAgentLevel",ifEmpty(nAgentLevel,0));
			ajax.add("nAgentId",ifEmpty(nAgentId,0));
			ajax.add("nOfficeId",ifEmpty(nOfficeId,0));
			ajax.add("bTeamPanel",ifEmpty(bTeamPanel,false));
			ajax.add("DefaultAgentId",ifEmpty(DefaultAgentId,0));
			ajax.add("DefaultOffice",ifEmpty(DefaultOffice,0));
			ajax.output = o;
			ajax.debug = false;
			ajax.url = "/_assets/ajaxMethods/_method.OfficeOfficeByAgentId.asp";
			ajax.onSuccess = function(x, t) {
				try {
					///trace(EncodeHTML(t));
					var code = x.firstChild.childNodes[1].firstChild.nodeValue;
					if (code == "1") {
						var thelist = x.childNodes[0].childNodes[3].childNodes[1].childNodes[0].childNodes[2].childNodes;
						if (thelist.length > 1) {
							var i = 0
							var l = thelist.length;
							var me;

							this.output.length--;
							var selectedValue = 0;
							for (i; i < l; i++) {
								me = XmlToObject(thelist[i]);
								//trace(me.toString(true));
								this.output.length++;
								this.output.options[this.output.length - 1].value = me._value;
								try {
								} catch (e) { trace(e.message) }
								this.output.options[this.output.length - 1].text = ((me._label.toString().substr(0, 2) == ", ") ? replaceall(me._label, ", ", "") : (me._label));
								if (me._selected == "true") {
									this.selectedValue = me._value;
									this.output.options[this.output.length - 1].selected = "true";
								}
							}
							window.loadingAgentList = false;
							if (document.getElementById("AgentOfficeListOutput")) {
								selectedValue = getVal(this.output);
								LoadAgentManagementListForOfficeIds(selectedValue, nAgentId, nOfficeId, cCatalog, nAgentLevel);
							} else {
								UpdateAgentList(this.output.value, (bTeamPanel == true), DefaultAgentId);
							}
						} else {
							var therow = document.getElementById("nOfficeIdRow");
							if (therow) {
								therow.parentNode.removeChild(therow);
							}
							var officeVal = x.childNodes[0].childNodes[3].childNodes[1].childNodes[0].childNodes[2].childNodes[0].nodeValue
							if (officeVal) {
								LoadAgentManagementListForOfficeIds(officeVal, nAgentId, nOfficeId, cCatalog, nAgentLevel);
							}
						}
					} else if (code == "-1") {
						this.output.parentNode.removeChild(this.output);
						if (document.location.toString().toLowerCase().indexOf("mgmt/_mgmt.agentmanager.asp") != -1) {
							alert("You must be logged in as an office manager to use this Control Panel");
							document.location = "../default.asp";
						}
					} else {
						reportEmailError("_js.common.js->LoadOfficeAgentList returned: " + t, "");
					}
				} catch (e) { window.loadingAgentList = false }
			}
			ajax.onFailed = function(x){
				reportEmailError("_js.common.js->LoadOfficeAgentList.onFailed('" + bTeamPanel + "','" + nForAgentId + "','" + nForOfficeId + "') // " + e.message + "\n" + x.responseText, "silent");
			}
			window.loadingAgentList = true;
			ajax.send();
		}
	} catch(e){
		window.loadingAgentList = false;
		reportEmailError("_js.common.js->LoadOfficeAgentList('" + bTeamPanel + "','" + nForAgentId + "','" + nForOfficeId + "') // " + e.message, "silent");
	}
}



function activate_nAgentSelect(){
	var o = document.getElementById("nAgent");

	// don't allow this to function during a preview creation, else there will be errors.
	if(window.Def){
		if(window.Def.previewInProgress){
			return;
		}
	} //
	if(o.type =="select-one"){
		if(o.disabled!=true){
			SetSelect(o, window.franchise.settings.i);
		}
		o.disabled = (o.disabled!=true);
	}
}

function loadHelpTopic(cCatalog, cName, nWidth, nHeight){
	var w = window.open("/_assets/images/" + cCatalog + "/demo/" + cName + "/default.asp","helpTopicWindow", "width=" + nWidth + ", height=" + nHeight);
}


function CloseRelated(){
	if(window.RelatedItemsReturnLayer){
		window.RelatedItemsReturnLayer.parentNode.removeChild(window.RelatedItemsReturnLayer);
		window.RelatedItemsReturnLayer = undefined;
	}
}

function ShowRelatedItemsInfo(oLink, cPartNo, catId){
	CloseRelated();
	if(oLink && cPartNo!=undefined && catId!=""){

		var ajax = FreeAjax();
		ajax.url = "/_assets/ajaxMethods/_method.getRelatedProductInfoPage.asp"
		//ajax.debug = true;
		ajax.method = "post";
		ajax.add("partno", cPartNo);
		ajax.add("catid", catId);
		ajax.oLink = oLink;
		ajax.onSuccess = function(x, t){
			if(t!="NOT FOUND"){
				window.RelatedItemsReturnLayer = document.createElement("div");
				window.RelatedItemsReturnLayer.id = "RelatedItemsReturnLayer";
				window.RelatedItemsReturnLayer.className = "RelatedItemsReturnLayer";
				window.RelatedItemsReturnLayer.innerHTML = t;
				document.body.appendChild(window.RelatedItemsReturnLayer)
				var xy = window.kPosition.get(oLink);
				window.RelatedItemsReturnLayer.style.top = document.body.scrollTop;
				window.RelatedItemsReturnLayer.style.left = xy.x - window.RelatedItemsReturnLayer.offsetWidth - 10;

			AddListener(document, "click", function(e){
				var baseObject = 	window.RelatedItemsReturnLayer
				if(!window.RelatedItemsReturnLayer){return;}
				var oTarget;
				try {
					oTarget=e.srcElement;
					if(oTarget==undefined){
						oTarget = e.target;
					}
				} catch(e){oTarget=e.currentTarget;}
				try {
					if(!isParentNode(oTarget, baseObject)){
						CloseRelated()
					}
				} catch(b){}
			})
			SelectFeaturePanelInit('features',true);
		}
	}
		//ajax.onFailed = function(x){trace("failed");}
		ajax.send();
	}
}; // and function ShowRelatedItemsInfo

function SelectFeaturePanel(n, $popup) {
	if(n=="features" || n == undefined){
		try{document.getElementById("FeatureTab"+(($popup)?"_popup":"")).className = "infoTab_selected";} catch(e){}
		try{document.getElementById("TechInfoTab"+(($popup)?"_popup":"")).className = "infoTab";} catch(e){}
		try{document.getElementById("ItemFeaturesTechContent"+(($popup)?"_popup":"")).innerHTML =  document.getElementById("ItemFeaturesContent"+(($popup)?"_popup":"")).innerHTML} catch(e){}
	} else if(n=="tech"){
		try{document.getElementById("TechInfoTab"+(($popup)?"_popup":"")).className = "infoTab_selected";} catch(e){}
		try{document.getElementById("FeatureTab"+(($popup)?"_popup":"")).className = "infoTab";} catch(e){}
		try {document.getElementById("ItemFeaturesTechContent"+(($popup)?"_popup":"")).innerHTML =  document.getElementById("ItemTechInfoContent"+(($popup)?"_popup":"")).innerHTML} catch(e){}
	}
}

function SelectFeaturePanelInit(n,$popup,retry){
		try{document.getElementById("ItemFeaturesContent"+(($popup)?"_popup":"")).className = "infoTabHide";} catch(e){}
		try{document.getElementById("ItemTechInfoContent"+(($popup)?"_popup":"")).className = "infoTabHide";} catch(e){}
		SelectFeaturePanel(n);
}

// 11/30/2007:1:22 PM
function addRelatedToCart(o){

}

/**
* @created :: Wednesday, March 12, 2008
*/
	function addHelpLink(id, asObject,cXmlUrl){
		try{
			if(asObject){

			} else {
				return("&nbsp;<a id=\"" + id + "_helpLink" + "\" href=\"javascript:;\" onclick=\"return(ShowHelpContent('" + id + "', this,'" + ((cXmlUrl)?cXmlUrl:"") + "'));\" ><img src=\"/inc/images/iconHelp_16x16.gif\" id=\"" + id + "_helpImg" + "\" border=0 title=\"click for more information on this topic\" /></a>");
			}
		} catch(e){
			Catch("_js.common.js->addHelpLink(id: '" + id + "', asObject: '" + asObject + "') // ", e);
		}
	}
/** @end function addHelpLink */

function ShowHelpContent(id, link, cXmlUrl){
	if(!window.loaded){
			AddListener(window,"load",function(){
				ShowHelpContent(id,link,cXmlUrl);
			}
		);
		return;
	}
	try {
		if(window.helpTopicWindow!=undefined){
			window.helpTopicWindow.parentNode.removeChild(window.helpTopicWindow);
			window.helpTopicWindow = undefined;
		}
		if(id==undefined || id==""){return;}
		if(id){

			try {

				link.blur();
				link.firstChild.title = "";
			} catch(e){}

			var o = window.helpTopicWindow = setElem("div");
			o.link = link;
			o.id = id + "_helpWindow";
			o.style.position = "absolute";
			o.style.top = 0;
			o.style.left = 0;
			o.style.zIndex = 999;
			o.style.padding = "30px";
			o.style.backgroundColor = "#FFFFCC";

			document.body.appendChild(o);
			o.innerHTML = "Loading Help Content...";
			try {
				PositionElementToElement(o, link);
			} catch(e){}
			AddListener(document,"click", HelpWindowClickHandler);

			// get the content
			var ajax = new Ajax();
			//ajax.debug = true;
			ajax.url = "/_assets/ajaxMethods/_method.getHelpTopic.asp";
			ajax.method = "POST";
			ajax.add("topic", id);
			ajax.add("cXmlUrl",((cXmlUrl)?cXmlUrl:""))
			ajax.onSuccess = function(x,t){
				try {
					o.innerHTML = t;
					// position the help window to be above and to the left of the clicked item. if not possible, position whereever fits.
					try {
						o.style.padding = "0px";
						o.style.backgroundColor = "#FFFFFF"
						PositionElementToElement(o,link);
					} catch(e){
						Catch("",e);
					}
				} catch(e){Catch("ShowHelpContent::Success", e);}
			}
			ajax.send();

		}
	} catch(e){Catch("_js.common.js->ShowHelpContent(id: " + id + ", link: " + link.id + ") // ", e);}
}


function HelpWindowClickHandler(e){
	var source
	if(e!=undefined){
		source = getSourceFromEvent(e);
	}
	var link
	if(window.helpTopicWindow){
		link = (window.helpTopicWindow.link);
	} else {
		link = undefined;
	}
	if(	source==undefined || (
				!isParentNode(source, window.helpTopicWindow) &&
				source!=window.helpTopicWindow &&
				source!=link &&
				!isParentNode(source, link)
			)
		){
		RemoveListener(document,"click",HelpWindowClickHandler);
		ShowHelpContent();
	}
}

function recordHelpUsefullness(oForm){
	oForm.parentNode.innerHTML = "Thank you, Recording Answer."
	//trace(oForm.helpTopic.value)
	//trace(oForm.HelpWindowFeedbackFormAnswer[0].checked==true);
}
// Wednesday, March 26, 2008
function CallFloatingPassword(link, email,_onComplete){
	if(link.name == "" && link.id ==""){
		link.name = link.id = "CallFloatingPasswordLink";
	} else if(link.name == "" || link.id ==""){
		if(link.name!=""){link.id = link.name}
		if(link.id!=""){link.name = link.id}
	}
// if script file isn't loaded, load it and then call function
// otherwise call the function
	try{
		if(PasswordFlyin){
			loadFlyinLayer(link, email,_onComplete);
		}
	} catch(e){
		window.AutomaticallyRunLoadFlyinLayer = {link:link, email:email,onComplete:_onComplete}
		LoadScript("/_assets/js/Gadgets/_js.gadgets.passwordFlyin.js");
	}
}



function validatePasswordChangeForm(f){
	var r = ""
	// ' see _includes.xml.asp doUpdatePassword for info on ¿FORCERESET?
	if(f.CurrentPassword.value=="" && f.CurrentPassword.value!="¿FORCERESET?"){
		r += "\t - Enter Current Password. If you recently reset your password please enter\n";
		r += "\t    the password from the email you received with your new password\n";
		markit(f.CurrentPassword, true)
	} else {
		markit(f.CurrentPassword, false)
		if(f.NewPassword.value==""){
			r += "\t - Enter a New Password.\n";
			markit(f.NewPassword, true)
		} else {
			if(f.ReTypeNewPassword.value==""){
				r += "\t - Retype New Password.\n";
				markit(f.ReTypeNewPassword, true)
			} else {
				if(f.NewPassword.value!=f.ReTypeNewPassword.value){
					r += "\t - New Passwords must match in Both Fields.\n";
					markit(f.NewPassword, true);
					markit(f.ReTypeNewPassword, true)
				} else {
					markit(f.NewPassword, false);
					markit(f.ReTypeNewPassword, false)
				}
			}
		}
	}

	if(r!=""){
		alert("Please correct the following and resubmit:\n\n" + r);
		return(false);
	} else {
		return(true);
	}
}


/** creates a ticker which will maintain session state. this will effectivally remove any "inactivity"
* timeout. but IF the session does timeout somehow it will prevent errors.
* Scott McDonald Tuesday, May 13, 2008
*/
function SetKeepAliveTimer(){
	if(window.franchise.settings.i<=0){
		clearTimeout();
		return;
	}
	if(Ajax){
		var ajax = new Ajax();
		ajax.method = "POST"
		ajax.url = "/_assets/ajaxMethods/_method.tick.asp";
		ajax.onSuccess = function(x,t){
			var o = XmlToObject(x)
			if(o._return._nAgentId<0 || o._return._nTempId<0 || o._return._bPassword==false){
				if(window.franchise.settings.i>0){
					document.location = document.location
				}
			}
			this.close(); // cleanup ajax connection object
		}
		ajax.send();
	}
	setTimeout("SetKeepAliveTimer()",60000);
}



function getNextHighestDepth(){
	for(var me in document.elements){
		trace(me)
	}
	return("done")
}


function getHomeDir(){
	var d = window.document.location.toString();
	var arr = d.split("/")
	return(arr[3]);
}


function ExpandMenu(o){
	var text = getElem("ExpandLinkText");
	var icon = getElem("ExpandIcon");

	if(o){
		if(window.Echo__ExpandMenuSetting == false || window.Echo__ExpandMenuSetting == undefined){
			setCookie("bShowMenuAsOpen",true,true);
			window.Echo__ExpandMenuSetting = true;
			dropmenu.trigger(true);
			text.innerHTML = "Collapse All";
			icon.innerHTML = "-";
		} else {
			setCookie("bShowMenuAsOpen",false,true);
			window.Echo__ExpandMenuSetting = false;
			dropmenu.trigger(false);
			text.innerHTML = "Expand All";
			icon.innerHTML = "+";
		}
	}
}

function errorLock(message) {
	if (window.lock) {
		AddListener(window, "load", function() {
			window.lock.show(
				((message)?message:
				"An error has prevented this page from loading correctly.<br />" +
				" The webmaster has been notified. If you would like to try<br />" +
				" again, click OK, to return to the previous page click Cancel."),
				-1,
				{
					okButton: function() {
						document.location.reload();
					},
					cancelButton: function() {
						window.history.back(1);
					}
				}
			);
		});
	};
};

function bullseyeEncode(sCopy) {
	if (!sCopy) { return (""); }
	try {
		sCopy = replaceall(sCopy, "=", "#61;");
		sCopy = replaceall(sCopy, "%", "[PERCENT]");
		sCopy = replaceall(sCopy, "<br>", "");
		sCopy = replaceall(sCopy, "<br/>", "");
		sCopy = replaceall(sCopy, "<br />", "");
		sCopy = replaceall(sCopy, "''", "'");
		sCopy = replaceall(sCopy, "<", "[AMPERSAND]lt;");
		sCopy = replaceall(sCopy, ">", "[AMPERSAND]gt;");
		sCopy = replaceall(sCopy, "&amp;", "[AMPERSAND]");
		sCopy = replaceall(sCopy, "&", "[AMPERSAND]");
		sCopy = replaceall(sCopy, "+", "[PLUS]");
		return (sCopy);
	} catch (e) { Catch("lowensign.js.common.bullseyeEncode", e, true); }
	return ("");
}