// sets form action (adjust quan., or remove item(s))
function updateCart(formID,action) {
	// make sure quan has value
	//if (document.forms[formID].quan.value == "") {
		//document.forms[formID].quan.value = 0;
	//}
	
	// set form action for proc page
	document.forms[formID].cartAction.value = action;
	// submit for processing
	document.forms[formID].submit();
}

// sets form action for customer login request
// 3 action states = new account, login, guest
function custLogin(action) {
	document.custForm.action.value = action;
	document.custForm.submit();
}

// credit card validation
function validateCCNum(cardType,cardNum){	
	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTERCARD":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}	
	return result;
}

// validates email address
function echeck(str) {
	msg = "The email address you have provided is invalid.";
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert(msg)
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert(msg)
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert(msg)
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert(msg)
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert(msg)
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert(msg)
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    alert(msg)
	    return false
	 }

	return true					
}

// validate customer info submission
function validCustInfo(){
	// first name	
	if (document.Form.billFirstName.value=="") {
		document.Form.billFirstName.focus();
    		alert('Please provide your first name.');
       	return false; 
	}
	// last name	
	if (document.Form.billLastName.value=="") {
		document.Form.billLastName.focus();
    		alert('Please provide your last name.');
       	return false; 
	}
	// biling address	
	if (document.Form.billAddress1.value=="") {
		document.Form.billAddress1.focus();
    		alert('Please provide your street address.');
       	return false; 
	}
	// bill country	
	if (document.Form.billCountry.value=="") {
		document.Form.billCountry.focus();
    		alert('Please provide your country.');
       	return false; 
	}
	// bill country	
	if (document.Form.billCity.value=="") {
		document.Form.billCity.focus();
    		alert('Please provide your city.');
       	return false; 
	}
	// bill state	
	if (document.Form.billState.value=="") {
		document.Form.billState.focus();
    		alert('Please provide your state.');
       	return false; 
	}
	// bill zip	
	if (document.Form.billZip.value=="") {
		document.Form.billZip.focus();
    		alert('Please provide your zip code.');
       	return false; 
	}
	// bill phone	
	if (document.Form.billPhone.value=="") {
		document.Form.billPhone.focus();
    		alert('Please provide your phone number in the event we have questions about your order.');
       	return false; 
	}
	// bill email
	if (document.Form.billEmail.value=="") {
		document.Form.billEmail.focus();
    		alert('Please provide your email address in the event we have questions about your order.');
       	return false; 
	}
	// validate email specified
	if (echeck(document.Form.billEmail.value)==false){
		document.Form.billEmail.value=""
		document.Form.billEmail.focus()
		return false
	}
	// credit card name
	if (document.Form.cardName.value==""){ 
		document.Form.cardName.focus();
    		alert('Please provide a credit card name.');
       	return false; 
	}
	// credit card type
	if (document.Form.cardType.value==""){ 
		document.Form.cardType.focus();
    		alert('Please provide a credit card type.');
       	return false; 
	}
	// credit card number
	if (document.Form.cardNumber.value==""){ 
		document.Form.cardNumber.focus();
    		alert('Please provide a credit card number.');
       	return false; 
	}
	// credit card number
	if (document.Form.cardExpDate.value==""){ 
		document.Form.cardExpDate.focus();
    		alert('Please provide a credit card expiration date.');
       	return false; 
	}	
	
	// format cc number and date
	tmpNum = document.Form.cardNumber.value;
	rExp = / /gi;
	ccNum = tmpNum.replace(rExp,'');
	ccType = document.Form.cardType.value;
		
	// validate cc specified
	if (validateCCNum(ccType,ccNum)==false){
		if (document.Form.tickCount.value < 3){
			alert('The credit card number you entered is not valid.')
			document.Form.cardNumber.value=""
			document.Form.cardNumber.focus()
			document.Form.tickCount.value = document.Form.tickCount.value + 1;			
		}else{
			alert('We cannot validate the credit card information you have provided. Please contact the box office at (970) 927-3254.')
		}
		return false
	}	
	
	return true;
}

// populates shipping field with bill info
function toggleShipInfo(){
	if (document.Form.useBillAsShip.checked){
		document.Form.shipFirstName.value = document.Form.billFirstName.value;
		document.Form.shipLastName.value = document.Form.billLastName.value;
		document.Form.shipAddress1.value = document.Form.billAddress1.value;
		document.Form.shipAddress2.value = document.Form.billAddress2.value;
		document.Form.shipCity.value = document.Form.billCity.value;
		document.Form.shipCompany.value = document.Form.billCompany.value;
		// state menu
		for (i=0; i<document.Form.shipState.length; i++) {
			if (document.Form.shipState.options[i].value == document.Form.billState.value){
				document.Form.shipState.options[i].selected = true;
			}
		} 
		// country menu
		for (i=0; i<document.Form.shipCountry.length; i++) {
			if (document.Form.shipCountry.options[i].value == document.Form.billCountry.value){
				document.Form.shipCountry.options[i].selected = true;
			}
		}
		document.Form.shipZip.value = document.Form.billZip.value;
	} else {
		document.Form.shipFirstName.value = "";
		document.Form.shipLastName.value = "";
		document.Form.shipAddress1.value = "";
		document.Form.shipAddress2.value = "";
		document.Form.shipCity.value = "";
		document.Form.shipCompany.value = "";
		for (i=0; i<document.Form.shipState.length; i++) {
			document.Form.shipState.options[i].selected = false;
		}
		for (i=0; i<document.Form.shipCountry.length; i++) {
			document.Form.shipCountry.options[i].selected = false;
		}
		document.Form.shipZip.value = "";
	}
}

// submit form request
function submitForm(f){	
	document.forms[f].submit();
}

function viewArt(file){
	artWin=window.open('view/dspArt.cfm?img=' + file,'artWin','scrollbars=yes,width=415,height=415')
	artWin.focus();
}