d = document;
noShippingStates = /((AL)|(AR)|(KS)|(MD)|(MA)|(MS)|(PA)|(UT))/i
validShippingStates = /(AK)|(AZ)|(CA)|(CO)|(CT)|(DC)|(DE)|(FL)|(GA)|(HI)|(IA)|(ID)|(IL)|(IN)|(KY)|(LA)|(ME)|(MI)|(MN)|(MO)|(MT)|(NC)|(ND)|(NE)|(NH)|(NJ)|(NM)|(NV)|(NY)|(OH)|(OK)|(OR)|(RI)|(SC)|(SD)|(TN)|(TX)|(VA)|(VT)|(WA)|(WI)|(WV)|(WY)/i

pickUp = false;


function ship2BillCopy() {
	if(validShippingStates.test(d.getElementById('bState').value) || pickUp) {
		d.getElementById('sName').value = d.getElementById('bName').value;
		d.getElementById('sCompany').value = d.getElementById('bCompany').value;
		d.getElementById('sAddress').value = d.getElementById('bAddress').value;
		d.getElementById('sCity').value = d.getElementById('bCity').value;
		d.getElementById('sState').value = d.getElementById('bState').value;
		d.getElementById('sZip').value = d.getElementById('bZip').value;
		d.getElementById('sPhone').value = d.getElementById('bPhone').value;
		d.getElementById('sEmail').value = d.getElementById('email').value;
		subTtl();
	} else if (noShippingStates.test(d.getElementById('bState').value)) {
		alert('Salexis Wines cannot ship via standard carrier to AL, AR, KS, MD, MA, MS, PA or UT. Please contact us at (707) 256-3434 if you would like to arrange alternate means of receiving your wine.');
		d.getElementById('bState').focus();
		d.getElementById('bState').select();
		return false;
	} else {
		alert('Please use a valid 2 letter state code');
		d.getElementById('bState').focus();
		d.getElementById('bState').select();
		return false;
	}
}
function setShipAddress(that) {
	if (that.checked) {
		d.getElementById('sState').value='CA';
	}else if (d.getElementById('sState').value.toLowerCase()=='ca') {
		d.getElementById('sState').focus();
		d.getElementById('sState').select();
	}
}

function checkPickUp() {
	pickUp = d.getElementById('pickUp').checked;
	f = d.orderForm;
	subTtl();
	if(pickUp) {
		
		f.sName.disabled = true;
		f.sCompany.disabled = true;
		f.sAddress.disabled = true;
		f.sCity.disabled = true;
		f.sState.disabled = true;
		f.sZip.disabled = true;
		f.sPhone.disabled = true;
		f.sEmail.disabled = true;
		f.sName.style.background = 'silver';
		f.sCompany.style.background = 'silver';
		f.sAddress.style.background = 'silver';
		f.sCity.style.background = 'silver';
		f.sState.style.background = 'silver';
		f.sZip.style.background = 'silver';
		f.sPhone.style.background = 'silver';
		f.sEmail.style.background = 'silver';
		d.getElementById('1dayShip').disabled = true;
		d.getElementById('2dayShip').disabled = true;
		d.getElementById('5dayShip').disabled = true;
		f.shippingEstimate.disabled = true;
	} else {
		f.sName.disabled = false;
		f.sCompany.disabled = false;
		f.sAddress.disabled = false;
		f.sCity.disabled = false;
		f.sState.disabled = false;
		f.sZip.disabled = false;
		f.sPhone.disabled = false;
		f.sEmail.disabled = false;
		f.sName.style.background = 'white';
		f.sCompany.style.background = 'white';
		f.sAddress.style.background = 'white';
		f.sCity.style.background = 'white';
		f.sState.style.background = 'white';
		f.sZip.style.background = 'white';
		f.sPhone.style.background = 'white';
		f.sEmail.style.background = 'white';
		d.getElementById('1dayShip').disabled = false;
		d.getElementById('2dayShip').disabled = false;
		d.getElementById('5dayShip').disabled = false;
		f.shippingEstimate.disabled = false;
	}
}
function checkShipAddress() {
	if(!pickUp) {
		f=d.orderForm;
		sStateValue = d.getElementById('sState').value.toLowerCase()
		returnToState = false;
		if (noShippingStates.test(sStateValue)) {
			alert('Salexis Wines cannot ship via standard carrier to AL, AR, KS, MD, MA, MS, PA, or UT. Please contact us at (707) 256-3434 if you would like to arrange alternate means of receiving your wine.');
			returnToState = true
		} else if(!validShippingStates.test(sStateValue)) {
			alert('Please use a valid 2 letter state code');
			returnToState = true
		} 
		if(returnToState) {
			t = setTimeout('f.sState.focus();f.sState.select();',250);
			return false;	
		}
	}
	subTtl();
}



function sendAll() {
	f = d.orderForm;
	clientEmail = f.email.value;
	billingPhone = f.bPhone.value;
	shippingPhone = f.sPhone.value;
	if(!f.bName.value) {
		alert('Billing name is required.');
		f.bName.focus();
	} else if(!f.bAddress.value) {
		alert("Billing address is required.");
		f.bAddress.focus();
	} else if(!f.bCity.value) {
		alert("Billing city is required.");
		f.bCity.focus();
	} else if(!validZipCode.test(f.bZip.value)) {
		alert("Please enter a valid 5 or 9 digit billing zip code.");
		f.bZip.focus();
		f.bZip.select();
	} else if(!validPhoneNumber.test(billingPhone)) {
		alert("Please enter a valid billing phone number.");
		f.bPhone.focus();
		f.bPhone.select();
	} else if(!validEmailAddress.test(clientEmail)) {
		alert("Please enter a valid email address.");
		f.email.focus();
		f.email.select();
	} else if(!f.ccName.value) {
		alert("Name of card is required");
		f.ccName.focus();
	} else if(!f.ccNumber.value) {
		alert("Please enter a valid credit card number.");
		f.ccNumber.focus();
	} else if(!f.cvc.value) {
		alert("Please enter a valid credit card security code.");
		f.cvc.focus();
	} else if(!pickUp) {
		if(!f.sName.value) {
			alert("Shipping Name is required.");
			f.sName.focus();
		} else if(!f.sAddress.value) {
			alert("Shipping Address is required.");
			f.sAddress.focus();
		} else if(!f.sCity.value) {
			alert("Shipping City is required.");
			f.sCity.focus();
		} else if(!validZipCode.test(f.bZip.value)) {
			alert("Valid Shipping Zip Code is required.");
			f.sZip.focus();
			f.sZip.select();
		} else if(!validPhoneNumber.test(shippingPhone)) {
			alert("Please enter a valid shipping phone number.");
			f.sPhone.focus();
			f.sPhone.select();
		} else if(!validEmailAddress.test(f.sEmail.value)) {
			alert("Please enter a valid shipping email address.");
			f.sEmail.focus();
			f.sEmail.select();
		} else if(!d.getElementById('1dayShip').checked && !d.getElementById('2dayShip').checked && !d.getElementById('5dayShip').checked) {
			alert("Please select a shipping method. Salexis reccomends 2 Day Air");
			d.getElementById('2dayShip').focus();
		} else if(!f.orderTerms.checked) {
			alert("You must agree to the order terms");
			f.orderTerms.focus();
		} else {
	//		alert(f.comments.value)
			f.submit();
		}
	} else if(!f.orderTerms.checked) {
		alert("You must agree to the order terms");
		f.orderTerms.focus();
	} else {
//		alert(f.comments.value)
		f.submit();
	}
}

function toggleDisplay(obj) {
	if(typeof(obj) == 'string') obj = d.getElementById(obj);
	
	//if(obj.style.display === undefined) obj.style.display = 'block';
	if(obj.style.display == 'none') {
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none';
	}
}


function toggleVisibility(obj) {
	if(typeof(obj) == 'string') obj = d.getElementById(obj);
	
	//if(obj.style.visibility === undefined) obj.style.visibility = 'block';
	if(obj.style.visibility == 'hidden') {
		obj.style.visibility = 'visible';
	} else {
		obj.style.visibility = 'hidden';
	}
}

function toggleMessageBlock(that) {
	msgBlock = d.getElementById('messageBlock');
	if (that.checked) {
		msgBlock.disabled = false;
	} else {
		msgBlock.disabled = true;
	}
}

function navDown(that) {
	imgName = that.src
	newImgName = imgName.replace('Up','Down');
	that.src = newImgName;
}


function navUp(that) {
	imgName = that.src
	newImgName = imgName.replace('Down','Up');
	that.src = newImgName;
}

function loadImages() {
	d.images[d.images.length] = "http://salexiswines.com/images/homeDown.gif"
	d.images[d.images.length] = "http://salexiswines.com/images/aboutUsDown.gif"
	d.images[d.images.length] = "http://salexiswines.com/images/contactUsDown.gif"
	d.images[d.images.length] = "http://salexiswines.com/images/ourWinemakerDown.gif"
	d.images[d.images.length] = "http://salexiswines.com/images/winemakerNotesDown.gif"
	d.images[d.images.length] = "http://salexiswines.com/images/orderOnlineDown.gif"
	d.images[d.images.length] = "http://salexiswines.com/images/wineClubDown.gif"
	d.images[d.images.length] = "http://salexiswines.com/images/whereAvailableDown.gif"
}

function isEmail(strng) {
	var emailFilter=/^.+@.+\..{2,3,4,6}$/;
	if (!(emailFilter.test(strng))) { 
	    return false
	    exit;
	} else {
		return true
		exit
	}
}

function openSpecSheet(specSheet) {
	specSheetPath = '/specSheets/'+specSheet;
	window.open(specSheetPath,'specSheet',"status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=790px");
}

var orderTermsWindow = 0;
function viewOrderTerms(target) {
	url = 'orderTerms.html#'+target;
	if (!orderTermsWindow || target) {
		orderTermsWindow = window.open(url,'orderTerms','width=450px,height=500px,resizable=0,scrollbars=1,location=0');
	} else if (orderTermsWindow && !orderTermsWindow.closed) {
		orderTermsWindow.focus();
	} else {
		orderTermsWindow = window.open(url,'orderTerms','width=450px,height=500px,resizable=0,scrollbars=1,location=0');
	}
}


function subTtl() {
	f=d.orderForm
	inpts = d.getElementsByTagName('input');
	selects = d.getElementsByTagName('select');
	wcMember = d.getElementById('wineClub').checked;
	if(f.sState.value.toLowerCase() == 'ca' || pickUp) shipToCA = true;
	else shipToCA = false;
	
	var subTotal = 0;
	var discount = 0;
	var caTax = 0;
	var grandTotal = 0;
	shippingPackages = new Array();
	
	for (i=0; i < selects.length-2; i=i+2) {
		if(i > 0) {
			x = 3*(i/2);
		}else{
			x = 0;
		}
		numCases = selects[i].value;
		priceCases = selects[i].id;
		costCases = numCases * priceCases;
		numBottles = selects[i+1].value;
		priceBottles = selects[i+1].id;
		costBottles = numBottles * priceBottles;
		totalCost = costCases + costBottles;
		subTotal += totalCost;
		
		packageId = inpts[x+1].value;		
		packagePercent = inpts[x+1].id;
		shippingQty = parseFloat(packagePercent)*(parseFloat(numBottles)+(parseFloat(numCases)*12));
		multiplePackages = packageId.split('|');
		for(key in multiplePackages) { 
			subPackage = multiplePackages[key];
			if(shippingPackages[subPackage]) {shippingPackages[subPackage] = shippingPackages[subPackage] + shippingQty;}
			else {shippingPackages[subPackage] = shippingQty;}
		}

		if(wcMember) {discount += totalCost * inpts[x].value;}
		totalCostFormatted = new NumberFormat(totalCost).toFormatted();
		spacing = '';
		for(s = 0 ; s < 10-totalCostFormatted.length ; s++) {spacing += ' ';}
		inpts[x+2].value = '$'+spacing+totalCostFormatted;
		
	}
		
	discountFormatted = new NumberFormat(discount).toFormatted();
	discountSpacing = '';
	for(s = 0 ; s < 10-discountFormatted.length ; s++) {discountSpacing += ' ';}
	d.getElementById('wineClubDiscount').value = '$'+discountSpacing+discountFormatted;
	
	subTotal = subTotal - discount;
	subTotalFormatted = new NumberFormat(subTotal).toFormatted();
	subTotalSpacing = '';
	for(s = 0 ; s < 10-subTotalFormatted.length ; s++) {subTotalSpacing += ' ' ;}
	d.getElementById('subTotal').value = '$'+subTotalSpacing+subTotalFormatted;
	
	if(shipToCA) {caTax = subTotal * 0.0775;}
	caTaxFormatted = new NumberFormat(caTax).toFormatted();
	caTaxSpacing = '';
	for(s = 0 ; s < 10-caTaxFormatted.length ; s++) {caTaxSpacing += ' ';}
	d.getElementById('caTaxAmt').value = '$'+caTaxSpacing+caTaxFormatted;
	
	grandTotal = subTotal + caTax;
	grandTotalFormatted = new NumberFormat(grandTotal).toFormatted();
	grandTotalSpacing = '';
	for(s = 0 ; s < 10-grandTotalFormatted.length ; s++) {grandTotalSpacing += ' ';}
	d.getElementById('grandTotal').value = '$'+grandTotalSpacing+grandTotalFormatted;

	estimateShipping();
}

function estimateShipping() {
	d.getElementById('shippingEstimate').disabled = false;
	d.getElementById('pickUpMsg').style.display = 'none';
	d.getElementById('requireZipForEstimate').style.display = 'none';
	d.getElementById('requireStateForEstimate').style.display = 'none';
	d.getElementById('5dayGroundMsg').style.display = 'none';
	d.getElementById('noGroundHiAk').style.display = 'none';
	
	f = d.orderForm;
	if(d.getElementById('1dayShip').checked) {shipType = '1dayShip'}
	else if (d.getElementById('2dayShip').checked) {shipType = '2dayShip'}
	else if (d.getElementById('5dayShip').checked) {shipType = '5dayShip'}
																									 
	if(pickUp){
		serviceType = null; 
		d.getElementById('pickUpMsg').style.display = 'inline';
		d.getElementById('shippingEstimate').value = '$      0.00';
		d.getElementById('shippingEstimate').disabled = true;
		shipType = 'pickUp';
	} 
	else if(shipType == '1dayShip') {serviceType = 'Next Day Air Saver';}
	else if(shipType == '2dayShip') {serviceType = '2 Day Air';}
	else if(shipType == '5dayShip') {serviceType = 'Ground';}
	
	
	shipToState = f.sState.value.toLowerCase();
	shipToZip = 0;
	if(shipToState == 'ca' && serviceType) {
		if(validZipCode.test(f.sZip.value)) {
			shipToZip = f.sZip.value.substring(0,3);
		} else {
			d.getElementById('requireZipForEstimate').style.display = 'inline';
		}
	} else if(shipType != '2dayShip' && (f.sState.value.toLowerCase() == 'hi' || f.sState.value.toLowerCase() == 'ak') && serviceType) {
		d.getElementById('noGroundHiAk').style.display = 'inline';
		d.getElementById('shippingEstimate').value = '$      0.00';
		d.getElementById('shippingEstimate').disabled = true
		serviceType = null; 
	}
	getString = "?state="+shipToState+"&zip="+shipToZip+"&serviceType="+serviceType+"&shipment=";
	if(serviceType) {
		if(shipToState && shipToState != 'ca') {
			for (var key in shippingPackages) {if(shippingPackages[key]){getString += key+"|"+shippingPackages[key]+",";}}
			url = "estimateShipping.html"+getString;
			loadXMLDoc(url,displayShippingEstimate);
			if(serviceType == 'Ground') {d.getElementById('5dayGroundMsg').style.display = 'inline';}
		}else if (shipToState =='ca' && shipToZip) {
			for (var key in shippingPackages) {if(shippingPackages[key]){getString += key+"|"+shippingPackages[key]+",";}}
			url = "estimateShipping.html"+getString;
			loadXMLDoc(url,displayShippingEstimate);
			if(serviceType == 'Ground') {d.getElementById('5dayGroundMsg').style.display = 'inline';}
		} else if (shipToState =='ca' && !shipToZip) {
			d.getElementById('requireZipForEstimate').style.display = 'inline';
			d.getElementById('shippingEstimate').value = '$      0.00';
			d.getElementById('shippingEstimate').disabled = true
		} else {
			d.getElementById('requireStateForEstimate').style.display = 'inline';
			d.getElementById('shippingEstimate').value = '$      0.00';
			d.getElementById('shippingEstimate').disabled = true
		}
	}
}

function displayShippingEstimate() {
		// only if req shows "complete"
	  if (req.readyState == 4) {
	  	// only if "OK"
	    if (req.status == 200) {
	    	// ...processing statements go here...
			shippingEstimate = req.responseText;
			shippingEstimateSpacing = '';
			for(s = 0 ; s < 10-shippingEstimate.length ; s++) {shippingEstimateSpacing += ' ';}
			d.getElementById('shippingEstimate').value = '$'+shippingEstimateSpacing+shippingEstimate;
	    } else {
	    	alert("There was a problem retrieving the XML data:\n" + req.statusText);
	    }
	  }
}

function chkBillingStateForShipping(that) {
	/*bState = that.value;
	allowCopyDiv = d.getElementById('copyBillingActive');
	denyCopyDiv = d.getElementById('copyBillingInActive');
	msgDiv = d.getElementById('copyBillingMsg');
	if (noShippingStates.test(that.value)) {
		msg = "Salexis cannot ship to "+that.value.toUpperCase();
		msgDiv.innerHTML = msg;
		allowCopyDiv.style.display='none';
		denyCopyDiv.style.display='inline';
	} else if (validShippingStates.test(that.value)) {
		msgDiv.innerHTML = '';
		allowCopyDiv.style.display='inline';
		denyCopyDiv.style.display='none';
	} else {
		msg = "Please enter a valid 2 letter U.S. State code";
		msgDiv.innerHTML = msg;
		allowCopyDiv.style.display='none';
		denyCopyDiv.style.display='inline';
	}*/
}
