/************************************************
This file contains JavaScript and DHTML for the Shopping Cart utilities.
If you do not know the system or JavaScript, DO NOT edit this file.


************************************************/

//Validation for the admin tool
function validateCartForm(){
	var err="";
	var fixedPrice="Yes";
	var itemContact;
	var contactEmail;
	var price = document.forms[0].price.value.trim();

	if(document.forms[0].fixed[1].checked) {
		fixedPrice="No";	
	}	
	
	if (document.forms[0].item.value.trim() == ""){
		err+="Please enter an Item Name.\n";
	}
	if (!isUSCurrency(price)){
		err+="Please enter a valid price.\n";
	}
	if (document.forms[0].notes.value.indexOf("\"") != -1){
		err+= "Notes cannot contain the double quotes character ( \" ).  Please use single quotes ( ' ) instead)";
	}

	//Handles the E-mail select form option
	var selectedValue = document.forms[0].emailSelect[document.forms[0].emailSelect.selectedIndex].value;
	if (selectedValue=="Other"){
		contactEmail = document.forms[0].emailText.value;
		document.forms[0].itemContact.value = document.forms[0].emailText.value;
		if (!validateEmail(contactEmail,true)){
			err+="Please enter a valid e-mail address or select another option\n";
		}	
		
	}
	else if (selectedValue=="editMode"){
		contactEmail = document.forms[0].emailText.value.trim();
		if (contactEmail!=""){
			if (!validateEmail(contactEmail,false)){
				err+="Please enter a valid e-mail address or select another option\n";
			}
			else {
				document.forms[0].itemContact.value = contactEmail;
			}			
		}
	}
	else {
		document.forms[0].emailText.value = selectedValue;
		contactEmail = document.forms[0].itemContact.value;
	}



	if (err!=""){
		alert(err);
		return;
	}
	
	
	else{
		var confirm=window.confirm("Please Confirm the Item Information:\n\n" +
		"Item: " + document.forms[0].item.value.trim() + "\n" +
		"Price: $" + document.forms[0].price.value.trim() + "\n" +
		"Fixed Price: " + fixedPrice + "\n" +
		"Item Contact: " + contactEmail  + "\n" +
		"User Instructions: " + document.forms[0].notes.value.trim());
		if (confirm){
			document.forms[0].submit();
		}
	}
}


String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
}

//Validates currencies
function isUSCurrency(sString){
	return RegExp(/^\$?[0-9\,]+(\.\d{2})?$/).test(String(sString).replace(/^\s+|\s+$/g, ""));
}

function modifyItem(action, ID, name){
	if (action=="deleteItem"){
		var confirm=window.confirm("Are you sure you want to delete " + name + "?");
		if (confirm){
			document.forms[1].action.value="deleteItem";
			document.forms[1].itemID.value=ID;
			document.forms[1].submit();
		}
	}
	if (action=="editItem"){
		document.forms[1].action.value="editItem";
		document.forms[1].itemID.value=ID;
		document.forms[1].submit();
	}
}

function validateEmail(str, isReq){
	if (isReq==true || str.trim() !=""){

		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;			
	}

}

function toggleEmailSelect(){
	if (document.forms[0].emailSelect[document.forms[0].emailSelect.selectedIndex].value=="Other"){
		document.getElementById('emailText').style.visibility="visible";
	}
	else{
		document.getElementById('emailText').style.visibility="hidden";
		document.forms[0].itemContact.value = document.forms[0].emailSelect[document.forms[0].emailSelect.selectedIndex].value;
	}
}



function itemSwitch(){
	var id = document.cartForm.item.options[document.cartForm.item.selectedIndex].value;
	if (id==0){
		document.cartForm.reset();
		document.cartForm.price.disabled=false;
		document.getElementById("itemNotes").innerHTML=""; 
	}
	else {
		var idTag = "item-" + id;
                var attContact="itemContact";
                var attPrice="defaultPrice";
                var attFixed="fixedPrice";
                var itemNotes="itemNotes";
                
            
		var itemContact = document.getElementById(idTag).getAttribute(attContact);		
		var defaultPrice= formatCurrency(document.getElementById(idTag).getAttribute(attPrice));
		
		var isFixed = document.getElementById(idTag).getAttribute(attFixed);
                
                priceBox = document.cartForm.price;	

                document.cartForm.priceNum.value = stripCurrency(defaultPrice); 
	        priceBox.value = defaultPrice;
	      
		if (isFixed==1){
			priceBox.disabled=true;
		}
		if (isFixed==0){
			priceBox.disabled=false;
		}
		document.getElementById("itemNotes").innerHTML= document.getElementById(idTag).getAttribute(itemNotes);
		document.cartForm.itemContact.value = itemContact;
		
	}
}



// Code for the Shopping Cart Client Side
function addToCart(){
	var err="";
	if (document.cartForm.item.selectedIndex ==0){
		err += "Please select an item to add to your shopping cart.\n";
	}
	
	var price = document.cartForm.price.value;
	var priceVal=stripCurrency(price);
	if ((isNaN(priceVal)) ||(priceVal < 1) || (!isUSCurrency(price))){
		err += "Please enter a valid amount\n";
	}
	
	else{
		document.cartForm.priceNum.value = stripCurrency(price);
	}

	if (err!=""){alert(err);}
	else {
		var itemDesc = document.cartForm.item.options[document.cartForm.item.selectedIndex].text;
		var notes= document.cartForm.userNotes.value;
		var qty = document.cartForm.qty.options[document.cartForm.qty.selectedIndex].value;
		var price = document.cartForm.price.value;
		var itemID = document.cartForm.item.options[document.cartForm.item.selectedIndex].value;
		var itemContact = document.cartForm.itemContact.value;
		var priceNum = document.cartForm.priceNum.value;
		var url="http://stantonstshul.com/scripts/shoppingcart/cart_preview.php?priceNum=" + priceNum  + "&itemContact= " + itemContact + "&newItem=" + itemDesc + "&qty=" + qty + "&price=" +price+ "&id="+itemID + "&notes=" + notes + "&action=add&PHPSESSID=cart";
		document.getElementById('cartframe').src = url;
	
		
		document.cartForm.reset();
	}
}
function removeItem(itemNum){
	var url="http://stantonstshul.com/scripts/shoppingcart/cart_preview.php?action=remove&itemNum=" + itemNum;
	location.href = url;
}


function formatCurrencyForm(){
	document.cartForm.price.value = formatCurrency(document.cartForm.price.value);
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function stripCurrency(amount){	
	amount = amount.replace(/,/g,"");
	if (amount.indexOf('$') == 0){
		return amount.substr(1, amount.length);
	}
	else {
		return amount;
	}
}




	