var timer = "";
var openMenu = null;
var canMenuBeClosed = true;

function linkhandler(anchorRef) {
	var w = window.open(anchorRef.href, 'newwin');
	w.focus(); 
	return false;
}

function popOpenWindow(anchor, windowName, options) {
	var openWindow = window.open(anchor.href, windowName, options)
	openWindow.focus();
	return false;
}

function clearTimer(){
	window.clearTimeout(timer);
}

function tryToClose(){
	var funcToCall = "canMenuBeClosed = true"
	timer = window.setTimeout(funcToCall,400);
}

function showMenu(id, objPos){
	if(document.getElementById){
		clearTimer();
		setElementProperty('subaboutnec', 'display', 'none');
		setElementProperty('subproducts', 'display', 'none');
		setElementProperty('subproductsplasma', 'display', 'none');
		setElementProperty('subproductsinstalled', 'display', 'none');
		setElementProperty('subproductsportable', 'display', 'none');
		setElementProperty('subnewsmedia', 'display', 'none');
		setElementProperty('subpartners', 'display', 'none');
		setElementProperty('sublocateadealer', 'display', 'none');
		setElementProperty('subsolutions', 'display', 'none');
		setElementProperty('subsupportcenter', 'display', 'none');
		setElementProperty('sublibrary', 'display', 'none');
		openMenu = id;
		canMenuBeClosed = false

		var x = 0;
		var y = 0;
		
		x = getElementLeft(objPos);
		x = x + 10;
		y = getElementBottom(objPos);
		y = y - 15;
		/*if(!window.event){
			y-=1;
		}*/
		
		setElementProperty(id, 'display', 'block');
		setElementProperty(id, 'left', x + "px");
		setElementProperty(id, 'top', y + "px");

	}
}

function showSearchMenu(id, objPos,columnName){
	if (document.getElementById && getElementProperty(id,'display')=='block') {
		//display related links column information and change search link image to search
		setElementProperty(columnName, 'display', 'block');
		document.searchtoggle.src='/images/relatedlinks_search.gif';
		setElementProperty(id, 'display', 'none');
		return false;
	} else {
		if(document.getElementById){
			setElementProperty(id, 'display', 'block');
			setElementProperty(id, 'left', getElementLeft(objPos)-160 + "px");
			setElementProperty(id, 'top', getElementBottom(objPos)+20 + "px");
			//hide related links column information and change search link image to close
			setElementProperty(columnName, 'display', 'none');
			document.searchtoggle.src='/images/relatedlinks_close.gif';
			return false;
		}
	}
}


function hideMenu(id){
	setElementProperty(id, 'display', 'none');
	openMenu = null;
}

function getMousePos(event){
	var x, y;
	
	if(window.event){
		x = window.event.clientX;
		y = window.event.clientY;
		if (document.documentElement && document.documentElement.scrollTop){
			y+=document.documentElement.scrollTop;
			
		} else {
			y+=document.body.scrollTop;
			
		}
	} else {
		x = event.pageX;
		y = event.pageY;
	}

	if(openMenu != null){
		var testInside = isInside(x, y, openMenu);
		if(!testInside && canMenuBeClosed == true){
			hideMenu(openMenu);
		}
		if(testInside){
			canMenuBeClosed = true;
			clearTimer();
		}
	}
}

function isInside(xMouse, yMouse, id){
	if( (id != null) && (xMouse >= getElementLeft(id)) && (xMouse <=getElementRight(id)) && (yMouse >= (getElementTop(id))-25) && (yMouse <= getElementBottom(id)) ){
		return true;
	} else {
		return false;
	}
}

window.onload = function() {
	document.onmousemove = getMousePos;
}

function switchDisplay(elm){
	var elm = elm.parentNode.nextSibling;
	while(elm.nodeType == 3){
		elm = elm.nextSibling;
	}
	
	var elmStyle = getElementProperty(elm, "display")

	if((elmStyle == "none") || (elmStyle == "") || (elmStyle == null)){
		setElementProperty(elm, "display", "block");
		return;
	}
	if(elmStyle == "block"){
		setElementProperty(elm, "display", "none");
		return;
	}
}

/** Return the px distance from left border of the element to left border of the window **/
function getElementLeft(p_elm) {
	var x = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		x+= elm.offsetLeft;
		elm = elm.offsetParent;
	}
	return parseInt(x);
}

/** Return the px width of the element **/
function getElementWidth(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetWidth);
}

/** Return the px distances from right border of the element to left border of window **/
function getElementRight(p_elm){
	return getElementLeft(p_elm) + getElementWidth(p_elm);
}

/** Return the px distances from top border of the element to top border of the window **/
function getElementTop(p_elm) {
	var y = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		y+= elm.offsetTop;
		elm = elm.offsetParent;
	}
	return parseInt(y);
}

/** Return the px heght of the element **/
function getElementHeight(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetHeight);
}

/** Return the px distances from bottom border of the element to border top of the window **/
function getElementBottom(p_elm){
	return getElementTop(p_elm) + getElementHeight(p_elm);
}

/** Return a style property of the elemnt , it return null if does not exist **/
function getElementProperty(p_elm, p_property){
	var elm = null;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	if (elm != null){
		if(elm.style){
			elm = elm.style;
			if(elm[p_property]){
				return elm[p_property];
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
}

/** Set a property of style type of the element **/
function setElementProperty(p_elm, p_property, p_value){
	var elm = null;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	if((elm != null) && (elm.style != null)){
		elm = elm.style;
		elm[ p_property ] = p_value;
	}
}

/** related links onblur event **/
function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}
function restoreDefault(el) {
  if (el.value=="") el.value = el.defaultValue
}

function submitForm(){
 alert("etet");
 document.pageForm.submit(); 
}

/** Storefront punch through **/
function buynow(modelNumber, ProdName, Category, Weight, UOM, Price) {
	var oBody = document.body;
	var oDocumentElement = document.documentElement;
	var buynowform = document.getElementById("buynowform");
	var status = "";
	if (buynowform) {
		var addSKU = document.getElementById("addSKU");
		var addPrice = document.getElementById("addPrice");
		var addName = document.getElementById("addProdName");
		var addCategory = document.getElementById("addCategory");
		var addWeight = document.getElementById("addWeight");
		var addUOM = document.getElementById("addUOM");
		var addQty = document.getElementById("addQty");
		var addStatus = document.getElementById("addStatus");
		if (addSKU) {
			addSKU.value = modelNumber;
			addPrice.value = Price;
			addName.value = ProdName;
			addCategory.value = Category;
			addWeight.value = Weight;
			addUOM.value = UOM;
			addQty.value = 1;
			//popUnavailable();
			status = getProdStatusFromSAP (modelNumber, 1);
			addStatus.value = status;
			popProcessing();
			buynowform.submit();
		}
		
	}
	return false;
}

var processingTimer = null;
function popProcessing() {
	clearProcessingTimer();
	var globalnavContainer = document.getElementById("globalnavcontainer");
	var processingContainer = document.getElementById("processingcontainer");
	if (globalnavContainer) {
		if (processingContainer) {
			var x = 200;
			var y = 250;
			x += getElementLeft(globalnavContainer);
			y += getElementBottom(globalnavContainer);
			var oDocumentElement = document.documentElement;
			if (oDocumentElement) {
				x += oDocumentElement.scrollLeft;
				y += oDocumentElement.scrollTop;
			}
			processingContainer.innerHTML = "Processing";
			setElementProperty(processingContainer, 'left', x + "px");
			setElementProperty(processingContainer, 'top', y + "px");
			setElementProperty(processingContainer, 'display', 'inline');
			processingTimer = window.setTimeout("updateProcessingHtml()", 1000);
		}
	}
}

function clearProcessingTimer(){
	if (processingTimer != null)
		window.clearTimeout(processingTimer);
}

function updateProcessingHtml(){
	clearProcessingTimer();
	var processingContainer = document.getElementById("processingcontainer");
	if (processingContainer.innerHTML == "Processing..........")
		processingContainer.innerHTML = "Processing.";
	else
		processingContainer.innerHTML += ".";
	processingTimer = window.setTimeout("updateProcessingHtml()", 1000);	
}

function activateObject(objectLocationId, objectHeight) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		var objectLocation = document.getElementById(objectLocationId);
		if (objectLocation) {
			var objectTags = objectLocation.innerHTML;
			objectLocation.style.height = objectHeight;
			objectLocation.innerHTML = objectTags;
		}
	}
}

function popUnavailable() {
	clearProcessingTimer();
	var globalnavContainer = document.getElementById("globalnavcontainer");
	var unavailableContainer = document.getElementById("unavailablecontainer");
	if (globalnavContainer) {
		if (unavailableContainer) {
			var x = 200;
			var y = 250;
			x += getElementLeft(globalnavContainer);
			y += getElementBottom(globalnavContainer);
			var oDocumentElement = document.documentElement;
			if (oDocumentElement) {
				x += oDocumentElement.scrollLeft;
				y += oDocumentElement.scrollTop;
			}
			setElementProperty(unavailableContainer, 'left', x + "px");
			setElementProperty(unavailableContainer, 'top', y + "px");
			setElementProperty(unavailableContainer, 'display', 'inline');
			return true;
		}
	}
	alert("Our online store is unavailable while we perform systems maintenance.  We are Sorry for any inconvenience.");
}

/**** PExchange Add to Cart ****/
/*function addtocart(title,cart,rowNum) {
	var qtyVar 		= "quantity_" 	+ rowNum;
	var idVar 		= "id_" 		+ rowNum;
	var freeqtyVar	= "freeQty_" 	+ rowNum;
	var frmForQty = document.getElementById(cart);
	if (frmForQty) {
		var frmToSubmit = document.getElementById("addtocartform");
		if (frmToSubmit) {
			var qty 	= document.getElementById(qtyVar).value;
			var id 		= document.getElementById(idVar).value;
			var freeQty = document.getElementById(freeqtyVar).value;
			
			var litID 		= document.getElementById("litID");
			var litQty 		= document.getElementById("litQty");
			var litFreeQty	= document.getElementById("litFreeQty");
			
			litQty.value = qty;
			litID.value = id;
			litFreeQty.value = freeQty;
			
			frmToSubmit.submit();
		}
	}
	return false;
}
function popCartItemDetail(cartitemRef){
	var w = window.open(cartitemRef.href,'newwin','toolbar=no,height=450,width=400,scrollbars=yes');
	w.focus(); 
	return false;
}
function updateCart(cartRef){
	var pexcartform = document.getElementById("pexCart");
	if (pexcartform) {
		var checkout = document.getElementById("checkout");
		var update = document.getElementById("update");
		checkout.value = "N";
		update.value = "Y";
		pexcartform.submit();
		alert("Shopping cart has been updated.");
	}
	return false;
}
function deleteCart(rowNum,title){
	var pexcartform = document.getElementById("pexCart");
	if (pexcartform) {
		var deleteVar = "delete_" + rowNum;
		var rowToDelete = document.getElementById(deleteVar);
		rowToDelete.value = "Y";
		pexcartform.submit();
		alert(title + " deleted from shopping cart");
	}
	return false;
}
function checkoutCart(){
	var pexcartform = document.getElementById("pexCart");
	if (pexcartform) {
		var checkout = document.getElementById("checkout");
		var update = document.getElementById("update");
		checkout.value = "Y";
		update.value = "N";
		pexcartform.submit();
	}
	return false;
}
function gotocart(cartRef){
	var w = window.open(cartRef.href,'_self');
	w.focus(); 
	return false;
}
function placeOrder(){
	var checkoutcart = document.getElementById("checkoutCart");
	if (checkoutcart) {
		checkoutcart.submit();
	}
	return false;
}
function editProfile(profileRef){
	var w = window.open(profileRef.href,'_self');
	w.focus(); 
	return false;
}
function calcTotal(){
	var totalcost=0;
	var mailoptionselected=false;
	var cobrandform = document.getElementById("cobrandOrderForm");
	if (cobrandform){
		var qty 				= document.getElementById("qty").value;
		var freeqty 			= document.getElementById("litFreeQty").value;
		var cost 				= document.getElementById("Cost");
		var ordercost 			= document.getElementById("orderCost").value;
		var listpurchasecost 	= document.getElementById("listPurchaseCost").value;
		var mailingcost			= document.getElementById("mailingCost").value;
		
		for (i=0; i<document.cobrandOrderForm.mailOption.length; i++){
			if(document.cobrandOrderForm.mailOption[i].checked == true){
				var shipmethod = document.cobrandOrderForm.mailOption[i].value;
				mailoptionselected = true;
			}
		}
		if(qty == "" || qty == null || qty == 0){
			cost.value="";
			alert('please enter a quantity');
			return false;
		}
		if (mailoptionselected == false){
			alert('please select a mail option');
			return false;
		}
		var totalqty = parseFloat(qty) - parseFloat(freeqty);
		var loopcount = document.getElementById("loopCount").value;
		if(qty > 0){
			for (i=1;i<=loopcount;i++){
				var minqtyvar 	= "minqty_" + i;
				var maxqtyvar 	= "maxqty_" + i;
				var costvar   	= "cost_" + i;
				
				var minqty 		= document.getElementById(minqtyvar).value;
				var maxqty 		= document.getElementById(maxqtyvar).value;
				var unitcost   	= document.getElementById(costvar).value;
				if(qty >= minqty){
					if(qty <= maxqty){
						if(totalqty > 0){
							totalcost = (totalqty * unitcost);
							break;
						}else{
							totalcost=0;
						}
					}else{
						if(maxqty == null || maxqty == ""){
							if(totalqty > 0){
								totalcost = (totalqty * unitcost);
								break;
							}else{
								totalcost=0;
							}
						}
					}
				}
			}
		}else{
			totalcost=0;
		}
		if(totalqty > 0){
			if(shipmethod == "nec"){
				totalcost = parseFloat(totalcost) + parseFloat(totalqty * mailingcost);
			}
			totalcost = parseFloat(totalcost) + parseFloat(ordercost) + parseFloat(totalqty * listpurchasecost);
		}
		cost.value=totalcost.toFixed(2);
	}
}*/
