// JavaScript Document



	function updateTotals( id ) {
		var i =1;
		var totalQuantity12=0, totalQuantity16=0;
		var quantity12 = 0, quantity16 = 0;
		do {
			quantity12 = document.getElementById('12quantity'+i).value;
			quantity16 = document.getElementById('16quantity'+i).value;
			//alert("q12: "+quantity12+" q16: "+quantity16);
			if( quantity12 == "") quantity12 = 0;
			if( quantity16 == "") quantity16 = 0;
			totalQuantity12 += parseInt(quantity12);
			totalQuantity16 += parseInt(quantity16);
			document.getElementById('orderTotal'+i).innerHTML = '$'+((10.50*quantity12+15.50*quantity16).toFixed(2));
			i++;
		} while( document.getElementById('12quantity'+i) != null );
		
		var totalQuantity = (totalQuantity12+totalQuantity16);
		var newTotal = (totalQuantity12*10.5+totalQuantity16*15.5).toFixed(2);
		var tax = 0;
		var state = document.getElementById('state').value;
		if( state == "TX") {
			tax = (newTotal * 0.0825).toFixed(2);
		}
		var shipping = 0;//(totalQuantity == 0 ? (0).toFixed(2) : (totalQuantity-1 + 8).toFixed(2));
		
		//alert( "12: "+totalQuantity12+" 16: "+totalQuantity16);
		if( totalQuantity12 < 12 && totalQuantity16 < 12 )
			shipping = totalQuantity-1 + 8;
			
		if( Math.floor(totalQuantity12 /12) >= 1) {
			shipping += Math.floor(totalQuantity12 / 12) * 10 + (totalQuantity12 % 12 );
			if( totalQuantity16 < 12 )
				shipping += totalQuantity16;
		}
		
		if( Math.floor(totalQuantity16 /12) >= 1) {
			shipping += Math.floor(totalQuantity16 / 12) * 10 + (totalQuantity16 % 12 );
			if( totalQuantity12 < 12 )
				shipping += totalQuantity12;
		}
		
		if( totalQuantity == 0 ) shipping = 0;
		var myDate=new Date();
		myDate.setFullYear(2010,0,1);
		var today = new Date();
		if( newTotal >= 75 && (today < myDate) ) shipping = 0;
		
		var discount = 0;
		
		if( Math.floor(totalQuantity12 / 12) >= 1 || Math.floor(totalQuantity16 / 12) >= 1 ) {
			document.getElementById('discount').style.display = "block";
			document.getElementById('discountTag').style.display = "block";
			discount = ((Math.floor(totalQuantity12 / 12)*20)+(Math.floor(totalQuantity16 / 12)*20)).toFixed(2);
			document.getElementById('discount').innerHTML = "$"+discount;
		}
		else {
			document.getElementById('discount').style.display = "none";
			document.getElementById('discountTag').style.display = "none";
		}
		
		var total = (parseFloat(newTotal) + parseFloat(tax) + parseFloat(shipping) - parseFloat(discount)).toFixed(2);
		
		document.getElementById('subtotal').innerHTML = '$'+(newTotal);
		document.getElementById('tax').innerHTML = '$'+tax;
		document.getElementById('shipping').innerHTML = '$'+shipping;
		document.getElementById('total').innerHTML = '$'+total;
	}
	
	function checkValues() {
		
		var name = document.getElementById("name").value;
		var address = document.getElementById("address").value;
		var city = document.getElementById("city").value;
		var state = document.getElementById("state").value;
		var zip = document.getElementById("zip").value;
		var phone = document.getElementById("phone").value;
		var email = document.getElementById("email").value;
		var total = document.getElementById("total").innerHTML;
		var failed = false;
		
		//alert("state: "+state);
		
		if( name == "" )
			document.getElementById("name").style.backgroundColor = "yellow";
		else 
			document.getElementById("name").style.backgroundColor = "white";
			
		if( address == "" ) 
			document.getElementById("address").style.backgroundColor = "yellow";
		else 
			document.getElementById("address").style.backgroundColor = "white";
			
		if( city == "" ) 
			document.getElementById("city").style.backgroundColor = "yellow";
		else 
			document.getElementById("city").style.backgroundColor = "white";
		
		if( state == "" ) 
			document.getElementById("state").style.backgroundColor = "yellow";
		else 
			document.getElementById("state").style.backgroundColor = "white";
		
		if( zip == "" ) 
			document.getElementById("zip").style.backgroundColor = "yellow";
		else 
			document.getElementById("zip").style.backgroundColor = "white";
		
		if( phone == "" )
			document.getElementById("phone").style.backgroundColor = "yellow";
		else 
			document.getElementById("phone").style.backgroundColor = "white";
		
		if( email == "" )
			document.getElementById("email").style.backgroundColor = "yellow";
		else 
			document.getElementById("email").style.backgroundColor = "white";
		
		if( email == "" || phone == "" || zip == "" || state == "" || city == "" || address == "" || name=="" ) 
		{
			alert("Please enter the required information.");
			failed = true;
		}
		else if( total == "$0") {
			alert("Please select some candles to order!");
			failed = true;
		}
		/*else {
			alert("Please enter the required information.");
			failed = true;
		}*/
		
		//alert( "failed: "+failed);
		
		if( !failed )
			document.order.submit();
	}
	
	function checkEmail() {
		var certificate = document.getElementById("certificate").value;
		var address = document.getElementById("address").value;
		var business = document.getElementById("business").value;
		var city = document.getElementById("city").value;
		var state = document.getElementById("state").innerHTML;
		var zip = document.getElementById("zip").value;
		var phone = document.getElementById("phone").value;
		var email = document.getElementById("email").value;
				
		if( certificate == "" )
			document.getElementById("certificate").style.backgroundColor = "yellow";
		else 
			document.getElementById("certificate").style.backgroundColor = "white";
			
		if( address == "" ) 
			document.getElementById("address").style.backgroundColor = "yellow";
		else 
			document.getElementById("address").style.backgroundColor = "white";
		
		if( business == "" )
			document.getElementById("business").style.backgroundColor = "yellow";
		else 
			document.getElementById("business").style.backgroundColor = "white";
		
		if( city == "" )
			document.getElementById("city").style.backgroundColor = "yellow";
		else 
			document.getElementById("city").style.backgroundColor = "white";
			
		if( state == "" )
			document.getElementById("state").style.backgroundColor = "yellow";
		else 
			document.getElementById("state").style.backgroundColor = "white";
		
		if( zip == "" )
			document.getElementById("zip").style.backgroundColor = "yellow";
		else 
			document.getElementById("zip").style.backgroundColor = "white";
		
		if( phone == "" )
			document.getElementById("phone").style.backgroundColor = "yellow";
		else 
			document.getElementById("phone").style.backgroundColor = "white";
		
		if( email == "" )
			document.getElementById("email").style.backgroundColor = "yellow";
		else 
			document.getElementById("email").style.backgroundColor = "white";
		
		if( certificate != "" && address != "" && business != "" && phone != "" && city != "" && state != "" && zip != "" && email != "")
			document.wholesale.submit();
		else
			alert("Please enter the required information.");
	}
	
	function checkSSL() {
		if( "http:" == window.location.protocol ) {
			window.location = "https://twistant.ipower.com"+document.location.pathname;
		}
	}
	
	function format(e) {

		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		return ((keycode > 47 && keycode < 58) || keycode == 8 || keycode == 0);

	}
	
	function checkCC() {
			
		var cn = document.getElementById("cardNumber").value;
		var failed = false;

		for(var i=0; i< 4; i++ ) {
			if( document.getElementById("ccType"+i).checked ) {
				var ccType = document.getElementById("ccType"+i).value;
			}
		}
				
				switch( ccType) {
					case "MasterCard":
						if( !mod10(cn) || (cn.length != 16) || ( ( cn.substring(0,2) != "51") && (cn.substring(0,2) != "52") && (cn.substring(0,2) != "53") && (cn.substring(0,2) != "54") && (cn.substring(0,2) != "55") )) {
							alert("MasterCard card number is not valid.");
							//failed = true;
						}
						break;
					case "Visa":
						if( !mod10(cn) || ( (cn.length != 13) && (cn.length != 16) ) || ( ( cn.substring(0,1) != "4") ) ) {
							alert("Visa card number is not valid.");
							failed = true;
						}
						break;
					case "Discover":
						if( !mod10(cn) || (cn.length != 16) || ( ( cn.substring(0,2) != "65") && (cn.substring(0,4) != "6011")  ) ) {
							alert("Discover card number is not valid.");
							failed = true;
						}
						break;
					case "American Express":
						if( !mod10(cn) || (cn.length != 15) || ( ( cn.substring(0,2) != "34") && (cn.substring(0,2) != "37" )) ) {
							alert("American Express card number is not valid.");
							failed = true;
						}
						break;
					default:
						alert("Please select a credit card.");
						failed = true;
						break;
				}
				if( document.getElementById("expirationDate").value == "" ) {
					alert("Please enter the Expiration Date.");
					failed = true;
				}
				if( document.getElementById("cardholderName").value == "" ) {
					alert("Please enter the Cardholder Name.");
					failed = true;
				}
				if( document.getElementById("cardSecurityCode").value == "" ) {
					alert("Please enter the Security Code.");
					failed = true;
				}
				
				if( !failed ) {
					document.confirm.submit();
				}		
	}
	
	function mod10( cardNumber )
	{
			var clen = new Array( cardNumber.length );
			
			var n = 0,sum = 0;
			for( n = 0; n < cardNumber.length; ++n ) {
				clen [n] = parseInt ( cardNumber.charAt(n) );
			}
			
			for( n = clen.length -2; n >= 0; n-=2 ) {
				clen [n] *= 2;
				if( clen [n] > 9 )
					clen [n]-=9;
			}
			
			
			for( n = 0; n < clen.length; ++n ) {
				sum += clen [n];
			}
			return(((sum%10)==0)?true : false);
	}


