// JavaScript Document
var frmCartAction = '';
var prodIDs = '';

var redColors = new Array('#6A0101','#3E0000');

function addItem(frm){
	if (frm.qty.value=="" || frm.qty.value=="0" || Number(frm.qty.value)<1){
		frm.qty.value=1;
	}
	frm.submit();
}
function submitCartAction(){
	var args = submitCartAction.arguments;
	frm = args[0];
	action = args[1];
	if (action=='save'){
		frm.frmAction.value='cartSave,cartView';
		//alert("frmAction: "+frm.frmAction.value);
		frm.submit();
	} else if (action=='checkout'){
		//frm.action='cart_checkout.php';
		frm.frmAction.value='cartSave,cartCheckout';
		//alert("frmAction: "+frm.frmAction.value);
		frm.submit();
	} else if (action=='empty'){
		frm.frmAction.value='cartEmpty,cartView';
		confDelete = confirm("Are you sure you want to empty your shopping cart?");
		if (confDelete==true){
			frm.submit();
		} else {
			return false;
		}
	} else if (action=='coupon'){
		if (args[2]!=""){
			frm.frmAction.value='cartSave,applyCoupon,cartView';
			frm.submit();
		} else {
			return false;
		}
	} else if (action=='couponCancel'){
		if (args[2]!=""){
			frm.frmAction.value='cartSave,cancelCoupon';
			frm.submit();
		} else {
			return false;
		}
	} else if (action=='removeItem'){
		frm.frmAction.value='removeItem,cartView';
		frm.myValue.value=args[2];
		frm.submit();
	} else {
		return false;
	}
}


function checkQuantity(quan,id,frmCart){
	if (isNaN(quan.value)==false){
		quantity = Number(quan.value).toFixed(0);
		priceElm = "itemPrice_"+id;
		priceArray = document.getElementsByName(priceElm);
		price = priceArray[0].value;
		newItemPrice = eval(quantity*price).toFixed(2);
		subTotal = 0;
		cartShippingTtl = 0;
		
		for (i=0; i<frmCart.elements.length; i++){
		
			if (frmCart.elements[i].name.substr(0,9)=='itemPrice'){
				//DEFINE IDs
				id = frmCart.elements[i].name.substr(10);
				priceElm = "span_itemPrice_"+id;
				quantityElm = "quan_"+id;
				costElm = "span_subTotal_"+id;
				shipElm = "shipping_"+id;
				shipPriceElm = "shippingPrice_"+id;
				
				//DEFINE VARS FROM IDs
				price = frmCart.elements[i].value;
				quantity = document.getElementById(quantityElm).value;
				shippingPerItem = document.getElementById(shipPriceElm).value;
				newItemPrice1 = eval(quantity*price).toFixed(2);
				newItemPrice2 = eval((quantity*price)+(quantity*shippingPerItem)).toFixed(2);
				document.getElementById(costElm).innerHTML = newItemPrice2;
				newCartShipping = eval(quantity*shippingPerItem).toFixed(2);
				cartShippingTtl = cartShippingTtl.toString();
				cartShippingTtl = Number(cartShippingTtl) + Number(newCartShipping);
				cartShippingTtl = cartShippingTtl.toFixed(2);
				subTotal = subTotal.toString();
				subTotal = Number(subTotal) + Number(newItemPrice1);
				subTotal = subTotal.toFixed(2);
				intCartTotal = (Number(subTotal)+Number(cartShippingTtl)).toFixed(2);
			}
		
			if (frmCart.elements[i].name.substr(0,10)=='stackPrice'){
				//DEFINE IDs
				id = frmCart.elements[i].name.substr(11);
				priceElm = "subTotal_stack"+id;
				quantityElm = "quan_stack_"+id;
				costElm = "subTotal_stack"+id;
				shipElm = "shipping_stack"+id;
				shipPriceElm = "shippingPrice_stack"+id;
				//alert("name: '"+id+"'\n\npriceElm: '"+priceElm+"'\n\nquantityElm: '"+quantityElm+"'\n\ncostElm: '"+costElm+"'\n\nshipElm: '"+shipElm+"'\n\nshipPriceElm: '"+shipPriceElm+"\n\n");
				//alert("name: '"+id+"'\n\npriceElm: '"+document.getElementById(priceElm).innerHTML.toString()+"'\n\nquantityElm: '"+document.getElementById(quantityElm).value+"'\n\ncostElm: '"+document.getElementById(costElm).value+"'\n\nshipElm: '"+document.getElementById(shipElm).value+"'\n\nshipPriceElm: '"+document.getElementById(shipPriceElm).value+"\n\n");
				
				//DEFINE VARS FROM IDs
				price = document.getElementById(priceElm).value;
				//price = frmCart.elements[i].value;
				quantity = 1;
				shippingPerItem = document.getElementById(shipPriceElm).value;
				newItemPrice1 = eval(quantity*price).toFixed(2);
				newItemPrice2 = eval((quantity*price)+(quantity*shippingPerItem)).toFixed(2);
				document.getElementById(costElm).innerHTML = newItemPrice2;
				newCartShipping = eval(quantity*shippingPerItem).toFixed(2);
				cartShippingTtl = cartShippingTtl.toString();
				cartShippingTtl = Number(cartShippingTtl) + Number(newCartShipping);
				cartShippingTtl = cartShippingTtl.toFixed(2);
				subTotal = subTotal.toString();
				subTotal = Number(subTotal) + Number(newItemPrice1);
				subTotal = subTotal.toFixed(2);
				intCartTotal = (Number(subTotal)+Number(cartShippingTtl)).toFixed(2);
			}
		}
		document.getElementById('cartSubTotal').innerHTML=subTotal;
		document.getElementById('cartShipping').innerHTML=cartShippingTtl;
		document.getElementById('cartTotal').innerHTML=intCartTotal;
		cartSum = cartSum.toString();
                document.getElementById('cartSumElm').innerHTML=intCartTotal;

	} else {
		return;
	}
}
function loadDefaultCartVars(){
	
}

function checkCartSave(){

}


function emptyCart(){
	var empty = confirm("Are you sure you want to empty your shopping cart?");
	return empty;
}

function openResetPass(){
	var url = "cart_userResetPass.html";
	window.open(url,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=225");
}


// ****** FORM VALIDATIONS *****
function frmValidateCartLogin(frm){
	//VALIDATE REAL EMAIL - main.js
	var isEmail = validateEmailAddress(frm.email.value);
	if (isEmail==false){
		alert("This is not a valid email address!");
		frm.email.focus();
		return false;
	}
	
	
	if (frm.password.value==""){
		alert("Please enter a password");
		frm.password.focus();
		return false;
	}
	return true;	
}

function frmValidateGuest(frm){
	var isEmail = validateEmailAddress(frm.email.value);
	return isEmail;
}

function frmValidatePwdReset(frm,step){
	var pwdLen = 6;
	switch(step){
		case 1:
			var isEmail = validateEmailAddress(frm.email.value);
			if (isEmail==false){
				alert("That is not a valid email address.");
			}
			return isEmail;
			break;
		case 2:
			if (frm.security_answer.value==""){
				alert("Please enter an answer!");
				return false;
			} else {
				return true;
			}
			break;
		case 3:
			if (frm.user_code.value.length==0){
				alert("You must enter a valid verification code!");
				return false
			}
			return true;
			break;
		case 4:
			if (frm.password_1.value!=frm.password_2.value){
				alert("Your passwords do not match!\n\n"+frm.password_1.value+"\n\n"+frm.password_2.value);
				return false;
			}
			if (frm.password_1.value.length<pwdLen){
				alert("Your password must be at least "+pwdLen+" characaters long!");
				return false;
			}
			return true;
			break;
	}
}

function frmValidateCartCreateUser(frm){
	if (frm.password.value!=frm.password_confirm.value){
		alert("Your passwords do not match!");
		frm.password.focus();
		return false;
	}
	
	if (frm.password.value.length<6){
		alert("Your password must be at least 6 characters in length!");
		return false;
	}
}

function frmValidateCartEditUser(frm){
	var isEmail = validateEmailAddress(frm.email.value);
	if (isEmail==false){
		alert("This is not a valid email address!");
		frm.email.focus();
		return false;
	}
	
	if (frm.password.value=="" && frm.password_confirm.value=="" && frm.password_old.value==""){
		//nothing
	} else {
		//CHECK OLD PASSWORD
		if (frm.password_old.value==""){
			alert("You must enter your old password in order to change your password");
			return false;
		}
		
		//CHECK THAT PASSWORDS MATCH
		if (frm.password.value!=frm.password_confirm.value){
			alert("Your passwords do not match!");
			frm.password.focus();
			return false;
		}
		
		//CHECK THAT PASSWORD MEETS REQUIREMENTS
		if (frm.password.value.length<6){
			alert("Your password must be at least 6 characters in length!");
			return false;
		}
	}
	
	valid = frmValidate2(frm);
	return valid;
}

function frmCopyBillingAddress(frm,box){
	if (box.checked){
		for (i=0; i<frm.elements.length; i++){
			if (frm.elements[i].name.substr(0,4)=='bill' && frm.elements[i].name!='bill_location_name'){
				elmName = 'ship'+frm.elements[i].name.substr(4);
				frm.elements[elmName].value=frm.elements[i].value;
			}
		}
	}
}

function checkState(frm){
	//USA 840
	stateElm = new Array();
	for (i=0; i<frm.elements.length; i++){
		if (frm.elements[i].name=='bill_state' || frm.elements[i].name=='ship_state'){
			stateElm.push(new Array(frm.elements[i].name,frm.elements[i].value));
		}
	}
	for (i=0; i<stateElm.length; i++){
		type = stateElm[i][0].split("_");
		countryElmName = type[0]+"_country";
		state = stateElm[i][1].toUpperCase();
		if (state=="AE" || state=="AA" || state=="AP"){
			frm.elements[countryElmName].disabled=false;
		} else {
			frm.elements[countryElmName].value='840';
//			frm.elements[countryElmName].disabled=true;
		}
	}
}

function checkState2(frm){
	//USA 840
	stateElm = new Array();
	for (i=0; i<frm.elements.length; i++){
		if (frm.elements[i].name=='state'){
			stateElm.push(new Array(frm.elements[i].name,frm.elements[i].value));
		}
	}
	for (i=0; i<stateElm.length; i++){
		countryElmName = "country";
		state = stateElm[i][1].toUpperCase();
		if (state=="AE" || state=="AA" || state=="AP"){
			frm.elements[countryElmName].disabled=false;
		} else {
			frm.elements[countryElmName].value='840';
//			frm.elements[countryElmName].disabled=true;
		}
	}
}

function populateAddyFrm(seqnum,frm){
	for (i=0; i<frm.elements.length; i++){
		
	}
}

function frmValidateNewAddy(frm,action){
	frmAction = action.toLowerCase();
	//alert(frmAction);
	
	switch (frmAction){
		case "reset":
			frm.reset();
			return false;
			break;
		case "delete":
			elmType = frm.type.value+"_addy";
			if (frm.elements[elmType].options.length<4){
				alert("Sorry but you are not allowed to delete this address.\n\nYou must have at least one address for each shipping and billing.");
				return false;
			} else {
				userConfirm = confirm("Are you sure you want to delete the '"+frm.elements[elmType].options[frm.elements[elmType].selectedIndex].text+"' address from your profile?");
				if (userConfirm==true){
					frm.frmAction.value=frmAction;
					frm.submit();
				} else {
					return false;
				}
			}
			break;
		case "save":
			//alert("saving");
			frmValid = frmValidate2(frm);
			//alert("frmValid: "+frmValid);
			if (frmValid==true){
				frm.frmAction.value=frmAction;
				frm.submit();
			} else {
				return false;
			}
			break;
			
		case "create":
			frmValid = frmValidate2(frm);
			if (frmValid==true){
				frm.frmAction.value=frmAction;
				frm.submit();
			} else {
				return false;
			}
			break;
			
		case "insert":
			frmValid = frmValidate2(frm);
			if (frmValid==true){
				//frm.frmAction.value=frmAction;
				//frm.submit();
				return true;
			} else {
				return false;
			}
			break;
		
		default:
			return false;
			break;
	}
}

function addressDo(func,seqnum,name){
	switch(func){
		case "remove":
			validRemove = confirm("Are you sure you want to remove this address?\n\n" + name);
			if (validRemove == true){
				document.frmAddressAction.frmAction.value='remove';
				document.frmAddressAction.seqnum.value=seqnum;
				document.frmAddressAction.submit();
			}
			break;
			
		case "edit":
			document.frmAddressAction.frmAction.value='edit';
			document.frmAddressAction.seqnum.value=seqnum;
			document.frmAddressAction.submit();
			break;
			
		case "default":
			//alert(func);
			document.frmAddressAction.frmAction.value='default';
			document.frmAddressAction.seqnum.value=seqnum;
			//document.frmAddressAction.elements['type'].value=name;
			document.frmAddressAction.type.value=name;
			document.frmAddressAction.submit();
			break;
			
	}
}

function cartDo(){
	var args = cartDo.arguments;
	switch (args[0]){
		case "removeItem":
			
	}
}
