
function validateEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

/**** Struts action calls to the server ****/
function getSearchResults() {
	var ss = document.getElementById("search");
	
	// TODO: Replace spaces
	
	var action = "search.do?ss="+ss.value;
	window.location = action;
}

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 loadOrderIds(inValue) {
	var action = "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 = "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 && validateEmail(emailObj.value)) {
		var action = "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 && validateEmail(emailObj.value)) {
		var action = "newsletterCancel.do?email_address="+emailObj.value;
		window.location = action;
	} else {
		alert("Invalid email address!");
	}
}

function loadProductItems(inValue) {
	var action = "maintainProductItems.do?category="+inValue;
	window.location = action;
}

function loadProductItem(inValue) {
	var catObj = document.getElementById("category");
	var action = "loadProductItem.do?itemId="+inValue+"&category="+catObj.value;
	window.location = action;
}

function saveProductItem(asNew) {
	var msg = '';
	if(asNew != null && asNew==1) {
		var itemIdObj = document.getElementById("item_id");
		itemIdObj.value = 0;
		msg = 'Save As NEW Product Item?';
	}
	
	if(msg=='' || confirm(msg)) {
		var searchForm = document.getElementById('maintainProductItemForm');
		if(searchForm != null) {
			searchForm.action = 'saveProductItem.do';
			searchForm.submit();
		}
	}
}

function saveBlankItem() {
	var searchForm = document.getElementById('maintainProductItemForm');
	if(searchForm != null) {
		searchForm.action = 'saveBlankItemDetails.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 = "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) {
	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+"checkout/checkoutStep1.do";
	//window.location = "confirmCheckout.do";
}

function addToCart() {
	var imageFile = document.getElementById('imageFile');
	if (imageFile!=null && imageFile.value!='') {
		if(    imageFile.value.lastIndexOf(".jpg")==-1
			&& imageFile.value.lastIndexOf(".jpeg")==-1
			&& imageFile.value.lastIndexOf(".bmp")==-1
			&& imageFile.value.lastIndexOf(".gif")==-1
			&& imageFile.value.lastIndexOf(".tif")==-1
			&& imageFile.value.lastIndexOf(".cdr")==-1
			&& imageFile.value.lastIndexOf(".ai")==-1
			&& imageFile.value.lastIndexOf(".eps")==-1
			&& imageFile.value.lastIndexOf(".png")==-1
			&& imageFile.value.lastIndexOf(".pdf")==-1
			&& imageFile.value.lastIndexOf(".JPG")==-1
			&& imageFile.value.lastIndexOf(".JPEG")==-1
			&& imageFile.value.lastIndexOf(".BMP")==-1
			&& imageFile.value.lastIndexOf(".GIF")==-1
			&& imageFile.value.lastIndexOf(".TIF")==-1
			&& imageFile.value.lastIndexOf(".CDR")==-1
			&& imageFile.value.lastIndexOf(".AI")==-1
			&& imageFile.value.lastIndexOf(".EPS")==-1
			&& imageFile.value.lastIndexOf(".PNG")==-1
			&& imageFile.value.lastIndexOf(".PDF")==-1
		) 
		{
		   alert("Please only upload image files!\n\nAcceptable extensions:\n\ncdr, ai, eps, png, jpg, jpeg, bmp, gif, tif, pdf");
		   return false;
		}
		//showLogoUploadOverlay();
		setTimeout("showLogoUploadOverlay();",100);
	} else {
		setTimeout("showAddToCartOverlay();",100);
	}
	
	var searchForm = document.getElementById('designForm');
	if(searchForm != null && Check(searchForm)) {
		var action = '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 viewPDF(pdfID) {
	action = "PDFServlet?pdfId="+pdfID;
	
	//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 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 setPrimaryPlanIndicator(checkbox) {
	if (checkbox!= null) {
		var buffer = "";
		buffer += "primaryPlanInd="+checkbox.checked;
		//alert(buffer);
		sndReq("setPrimaryPlanIndicator", 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 getBlankItems(categoryObj) {
	if ( categoryObj!= null && categoryObj.value.length > 0 && categoryObj.value != ' ' ) {
		if (categoryObj.value=="plaques") {
			window.location = BASE_HREF+"maintainBlankPlaques.do";
		} else {
			window.location = BASE_HREF+"maintainBlankPlates.do";
		}
	}
}

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);
	}
}

/*****************************/