

/**** Struts action calls to the server ****/
function checkSearchEnter(e){ //e is event object passed from function invocation
	var characterCode; // literal character code will be stored in this variable

	if(e && e.which) { //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
		getSearchResults()
		return false
	} else {
		return true
	}

}

function getSearchResults() {
	var ss = document.getElementById("search");
	if (ss!==null && ss.value!==null && ss.value!=='' && ss.value!==' ') {
		// trim the leading and ending spaces
		var val = ss.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
		
		// Replace spaces with "-"
		val = val.replace(/ /g,'-');
	
		//var action = BASE_HREF+"search.do?ss="+val;
		var action = BASE_HREF+"engraved-award-gifts-search/"+val;
		//alert(action);
		window.location = action;
	}
}

function sortSearchResults(sortBy,ascend,ss) {
	if (ss!==null && ss.value!==null && ss.value!=='' && ss.value!==' ') {
		// Replace spaces with "-"
		ss = ss.replace(/ /g,"-");
	}
	var action = BASE_HREF+"searchSortBy.do?sortBy="+sortBy+"&ascend="+ascend+"&ss="+ss;
	//alert(action);
	window.location = action;
}

function sortProductsByPrice(sortBy,ascend,categoryType,priceAmt) {
	var action = BASE_HREF+"sortProductsByPrice.do?sortBy="+sortBy+"&ascend="+ascend+"&categoryType="+categoryType+"&priceAmt="+priceAmt;
	//alert(action);
	window.location = action;
}


function loadOrderIds(inValue) {
	var action = BASE_HREF+"loadOrderIds.do?emailAddress="+inValue;
	window.location = action;
}

function saveOrderStatus(index) {
	var orderIdObj = document.getElementById("orderId"+index);
	var emailAddressObj = document.getElementById("emailAddress"+index);
	var orderStatusObj = document.getElementById("orderStatus"+index);
	var action = BASE_HREF+"saveOrderId.do?orderId="+orderIdObj.value+"&emailAddress="+emailAddressObj.value+"&orderStatus="+orderStatusObj.value;
	window.location = action;
}

function signup() {
	var emailObj = document.getElementById("email_address");
	if (emailObj != null && emailObj.value.length > 0 && isEmail(emailObj.value)) {
		var action = BASE_HREF+"newsletterSignup.do?email_address="+emailObj.value;
		window.location = action;
	} else {
		alert("Invalid email address!");
	}
}

function cancel() {
	var emailObj = document.getElementById("email_address");
	if (emailObj != null && emailObj.value.length > 0 && isEmail(emailObj.value)) {
		var action = BASE_HREF+"newsletterCancel.do?email_address="+emailObj.value;
		window.location = action;
	} else {
		alert("Invalid email address!");
	}
}

function loadProductItems(inValue) {
	var action = BASE_HREF+"maintainProductItems.do?category="+inValue;
	window.location = action;
}

function loadProductItem(inValue) {
	var catObj = document.getElementById("category");
	var action = BASE_HREF+"loadProductItem.do?itemId="+inValue+"&category="+catObj.value;
	window.location = action;
}

function saveProductItem(asNew) {
	var msg = 'Are you sure you would like to Save the Product Item?';
	if(asNew != null && asNew==1) {
		var itemIdObj = document.getElementById("item_id");
		itemIdObj.value = 0;
		msg = 'Save As NEW Product Item?';
	}
	
	if(confirm(msg)) {
		var searchForm = document.getElementById('maintainProductItemForm');
		if(searchForm != null) {
			searchForm.action = 'saveProductItem.do';
			searchForm.submit();
		}
	}
}

function deleteProductItem(inValue) {
	if(confirm('Are you sure you would like to DELETE this Product Item?')) {
		var catObj = document.getElementById("category");
		var action = BASE_HREF+"deleteProductItem.do?itemId="+inValue+"&category="+catObj.value;
		window.location = action;
	}
}

function design(itemId, dimenIndex) {
	
		var action = BASE_HREF+"design.do?itemId="+itemId+"&dimenIndex="+dimenIndex;
		window.location = action;
}

function checkOverlay(modalWin) {
	//if (modalWin!==null && modalWin && modalWin.open && !modalWin.closed) {
		//setTimeout("checkOverlay();",1000);
	//} 
	
	hideOverlay();
	if(modalWin!=null && modalWin==true) {
		viewShoppingCart();
	}
	
}

function closeModalWindow() {
	/*
	alert(modalWin);
	if (modalWin!==null && modalWin) {
		//setTimeout("viewShoppingCart();",1000);
		
		modalWin.close();
		modalWin = null;
		//window.close();
	}
	if (window.showModalDialog) {
		//alert("IE ONLY!!");
		if (window.opener) {
			//parent window... do nothing
		} else {
			//modal window
			//alert(window);
			if (window!==null && window) {
				window.returnValue = true;
				window.close();
			}
		}
	}
	*/
}

function viewShoppingCart() {
	var action = BASE_HREF+"shoppingCart.do";
	window.location = action;
}

function confirmCheckout() {
	window.location = BASE_HREF+"confirmCheckout.do";
}

function paypalInvoice() {
	window.location = BASE_HREF+"paypalInvoice.do";
}

function addToCart() {
	var searchForm = document.getElementById('designForm');
	if(searchForm!=null) {
		var action = BASE_HREF+'addToCart.do'
		searchForm.action = action;
		searchForm.submit();
	}
}

function showAddToCartOverlay() {
	var buttonDiv = document.getElementById('addToCartDiv');
	buttonDiv.style.display = 'none';
	var buttonDiv2 = document.getElementById('addToCartDiv3');
	buttonDiv2.style.display = '';
	var imageObj = document.getElementById('imageTag');
	imageObj.src = 'images/hourglass.gif';
}

function showLogoUploadOverlay() {
	var buttonDiv = document.getElementById('addToCartDiv');
	//buttonDiv.innerHTML = "Uploading Image <img src='images/hourglass.gif' height='15px' width='15px'/>";
	buttonDiv.style.display = 'none';
	var buttonDiv2 = document.getElementById('addToCartDiv2');
	buttonDiv2.style.display = '';
	var imageObj = document.getElementById('imageTag');
	//alert(imageObj.src);
	imageObj.src = 'images/hourglass.gif';
}

/******************/



/******************
	Load views to an iFrame 
	(static pages, images, graphs, Struts actions)
*******************/
function iframeChange(newLocation) {
	if (newLocation != null && newLocation != "" && newLocation !="ignore") {
	    //alert("boo!");
	    document.getElementById('outputSelect').value = newLocation;
		var iframe = document.getElementById('skuMainViewOutput');
		if (iframe != null && newLocation != null) {
			iframe.src = newLocation;
		}
	}
}

function viewGraph(graphId) {
	action = BASE_HREF+"Graph?graphId="+graphId;
	
	//alert(action);
	window.open(action, '', 'top=25,left=25,height=440,width=755,status=no,resizable=yes,scrollbars=no,menubar=no');
}

var firstTime = true;
function swapDivInfo(divName1, divName2, graphId) {
	if(divName1 != null && divName2 != null) {
		var imageObj = document.getElementById("graphImage");
		if (firstTime) {
			if(imageObj!=null) {
				imageObj.src = "Graph?graphId="+graphId;
				firstTime = false;
			}
		}
		
		var divObj1 = document.getElementById(divName1);
		var divObj2 = document.getElementById(divName2);
		if(divObj1!=null && divObj2!=null) {
			var tempVal = divObj1.innerHTML;
			divObj1.innerHTML = divObj2.innerHTML;
			divObj2.innerHTML = tempVal;
				
			var buttonObj = document.getElementById("swapDivButton");
			if (buttonObj.value == "View Graph") {
				buttonObj.value = "View Data";
				buttonObj.title = "View Data";
			} else {
				buttonObj.value = "View Graph";
				buttonObj.title = "View Graph";
			}
		}
		
	}
}
/******************/


/********* RPC Calls *********/
function getBlankBoards() {
		sndReq("getBlankBoards", "");
}
function getBlankPlates() {
		sndReq("getBlankPlates", "");
}

function rpcRemoveRoiAttributes() {
		var buffer = "";
		//alert(buffer);
		sndReq("rpcRemoveRoiAttributes", buffer);
}

function getDesignTemplate(inObj, numLines) {
	if (confirm("Importing this template will replace your current text.")) {
		//count number of lines
		var numLines = 1;
		var lineObj = document.getElementById("plate_line_1_"+numLines);
		while (lineObj != null) {
			numLines++;
			lineObj = document.getElementById("plate_line_1_"+numLines);
		}
		numLines--;
		
		if ( inObj!= null && inObj.value.length > 0 && inObj.value != ' ' ) {
			var buffer = "";
			var newNum = inObj.value - 0;
			buffer += "designTemplateNum="+(newNum+1);
			buffer += "&numLines="+numLines;
		
			//alert(buffer);
			sndReq("getDesignTemplate", buffer);
		}
	}
}

function emailSent() {
	document.getElementById('sending').style.visibility="hidden";
	
	document.getElementById('emailSent').style.visibility="visible";
	document.getElementById("message").value="";
	
	setTimeout("hideEmailSentMessage();",15000);
}
function hideEmailSentMessage() {
	document.getElementById('sending').style.visibility="hidden";
	document.getElementById('emailSent').style.visibility="hidden";
}

function sendEmailFromContactPage() {

	var dept    = document.getElementById("contact_us_department");
	var name    = document.getElementById("fullname");
	var email   = document.getElementById("email1");
	var message = document.getElementById("message");
	
	if (dept!= null && name!= null && email!= null && message!= null) {
		document.getElementById('sending').style.visibility="visible";
		var buffer = "";
		buffer += "dept="+dept.value;
		buffer += "&name="+name.value;
		buffer += "&email="+email.value;
		buffer += "&message="+message.value;
		//alert(buffer);
		sndReq("sendEmailFromContactPage", buffer);
	}
}


function getProductItems(categoryObj, destination) {
	if ( categoryObj!= null && categoryObj.value.length > 0 && categoryObj.value != ' ' ) {
		var buffer = "";
		buffer += "category="+categoryObj.value;
		buffer += "&dest="+destination;
	
		//alert(buffer);
		sndReq("getProductItems", buffer);
	} else {
		if (document.getElementById(destination) != null) { 
			document.getElementById(destination).options.length=0;
			document.getElementById(destination).options[0]= new Option('','');
		}
	}
}

function checkOrderStatus(destination) {

	var emailObj = document.getElementById("customer_email");

	if ( CheckEmail(emailObj) ) {
		var buffer = "";
		buffer += "customerEmail="+emailObj.value;
		buffer += "&dest="+destination;
	
		//alert(buffer);
		sndReq("checkOrderStatus", buffer);
	}
}

/*****************************/
