//document.cookie="";
//clearCookie("prp");
//alert (document.cookie);




//************* VALIDATION ***************************

function validateName(sHelper) {

	if (sHelper.length == 0) sHelper = "Please enter your first and last name."

	if (document.forms[0].fullName.value.length < 1) {
		alert(sHelper) ;
		return (!1);	
	}
	return (1);
}

function validateBillingAddress(sHelper) {

	if (sHelper.length == 0) sHelper = "Please enter your mailing/billing address."

	if (document.forms[0].billingAddress.value.length < 1 || document.forms[0].billingCityStateZip.value.length < 1 ) {
		alert(sHelper) ;
		return (!1);	
	}
	return (1);
}

function validatePhone(sHelper) {

	if (sHelper.length == 0) sHelper = "Please enter your phone number, starting with the area code."

	if (document.forms[0].phone.value.length < 10) {
		alert(sHelper) ;
		return (!1);	
	}
	return (1);
}


function validateForm() {

	if (!validateName("")) return (!1);
	if (!validateBillingAddress("")) return (!1);
	if (!validatePhone("")) return (!1);
	
	return (1);
	
}



function validateNewsletterRequest() {

	if (!validateName()) return (!1);
	return (1);
}


function validateOrder() {

	if (!validateName("Please enter your first and last name as it appears on your credit card.")) return (!1);
	
	if (!validateBillingAddress("Please enter the billing address for this order.")) return (!1);

	if (!validatePhone("Please enter your phone number, starting with the area code.")) return (!1);
	
	return (1);
}





//************* CHECKOUT ***************************



function prpCCNum() {

	if (document.forms[0].PRP.checked && (document.forms[0].ccNumber.value == "")) document.forms[0].ccNumber.value = "use my card # on file";
	if (!document.forms[0].PRP.checked && (document.forms[0].ccNumber.value == "use my card # on file")) document.forms[0].ccNumber.value = "";
	
}

function clearButton() {

	document.forms[0].shipToBillingAddress.checked = false;
		
}

function loadShipping() {

	if (validateOrder() == false) document.forms[0].shipToBillingAddress.checked = false;
	
	if (document.forms[0].shipToBillingAddress.checked) {
	
		document.forms[0].shipTo.value = document.forms[0].fullName.value ;
		document.forms[0].shipAddress.value = document.forms[0].billingAddress.value ;
		document.forms[0].shipCityStateZip.value = document.forms[0].billingCityStateZip.value ;
		document.forms[0].shipPhone.value = document.forms[0].phone.value ;
	}
		
}


function updateWine(wineID, quantity){
		
	if ((wineID.indexOf("OZ") > -1) && (quantity > 2)) return;
		

	setCookie(wineID, quantity,1);
	location.reload();

}






function loadBottles() {


	
	if (document.forms[0].elements.length==0) {
		
		document.write ("<tr><td class=bodyText><font color=red>Your shopping cart is empty.  Click 'Wine Shop' to get a list of our wines.</font></style></td></tr>");
		return;
	}
	
	
	//-------------------------
	//we have items in the cart
	//-------------------------
	
	
	var myBottles = updateBottleCount();
	var myPrp=getCookie("prpFlag");
	var myState=getCookie("stateId");
	
	
	document.write ("<tr><td class=bodytext><br><b>Additional order details:</b></td><td class=bodytext align=right></td></tr>");
	

	//PRP discount
	if (myPrp == 1) {
	//	document.write ("<tr><td class=bodytext>PRP members are eligible for a 20% discount from retail price.</td><td class=bodytext align=right></td></tr>");

	}
	
	
	//non PRP (or missing PRP) discount
	else {

		//if (bottleCount >=12) {
		//	document.write ("<tr><td class=bodytext>Your order is eligible for a 12 bottle discount (10%)</td><td class=bodytext align=right></td></tr>");
		//}
//
		//else if (bottleCount >=8)  {
		//	document.write ("<tr><td class=bodytext>Tip: orders of 12 bottles or more are eligible for a discount of 10%.</td></tr>");			
//
		//}
//
		//else {
		//	document.write ("")
//
		//}
	}


	//sales tax if we know the state
	if (myState.length == 2)  {

		var taxInfo = getTaxRates(myState) 
		if (taxInfo == "0") {
			document.write ("<tr><td class=bodytext>We do not charge sales tax for shipments to " + myState + "</td><td class=bodytext align=right></td></tr>");		
		}
		else {
			if (jsIsNumeric(taxInfo.replace("%", "")) == true)  document.write ("<tr><td class=bodytext>Sales tax for " + myState + " is " + taxInfo + "</td><td></td></tr>");		
			if (jsIsNumeric(taxInfo.replace("%", "")) != true)  document.write ("<tr><td class=bodytext>Sales tax for " + myState + " ranges from " + taxInfo + " based on your zipcode.</td><td class=bodytext align=right></td></tr>");		
		}


	}


	//shipping (only if we have a shipState)
	if (myState.length == 2) {

		//document.write ("<tr><td class=bodytext>Your shipping cost is estimated to be $" + parseInt(getShipPrice(myState, myPrp, myBottles)) + ".00</td><td></td></tr>");		

	}


	
	//alert("Debugging: The current state of the cookie is: " + document.cookie);

}

function clearOrder() {

	var numberElements = document.forms[0].elements.length;
	var i;
	for (i = 0; i < numberElements; i++) { 
			
		if (document.forms[0].elements[i].type=="text") clearCookie(document.forms[0].elements[i].name);

	location.reload();
	}
	

}

function listWine(wineID, wineName, winePrice, wineUnit, canEdit) {


	if (getCookie(wineID) >=1) {
	
		document.write("<tr><td class=bodyText>");
		if  (canEdit == "true") document.write("Qty:&nbsp;<input type=text   size=3 name=" + wineID  +  " value=" + getCookie(wineID) + " onChange=updateWine('" + wineID + "',this.value)>");
		if  (canEdit != "true") document.write("Qty:&nbsp;<input type=hidden size=3 name=" + wineID  +  " value=" + getCookie(wineID) + ">" + getCookie(wineID) + "&nbsp;&nbsp;" );
		document.write("<b>&nbsp;" + wineName + "</b>&nbsp@")
		document.write (formatCurrency(winePrice));
		document.write("</td><td class=bodyText align=right>" + formatCurrency(winePrice * getCookie(wineID))  + "</td>");
		document.write("</tr>")
		
	}


}



//************* MISC ***************************


function openPrivacy() {

	objWin = window.open('http://huschvineyards.com/privacy.htm','Privacy', 'width=400,height=400,scrollbars=yes');
}



function getQS(nameString) {

	var QSValue = "";
	var fullURL = document.location + "";
	beginNameString =  fullURL.search(nameString+"=");

	if (beginNameString != -1 ) {

		var i = beginNameString + nameString.length+1;
		while (fullURL.substr(i,1) != "&" && i<=fullURL.length) {

			QSValue = QSValue + fullURL.substr(i,1);
			i++ ;
		}
	}
	return QSValue;
}



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 NZ(objToTest) {
	if ( (null == objToTest) || ("undefined" == typeof(objToTest) ) ){
		return 0;
	}
	else {
		return objToTest.value;
	}

}


//************* COOKIES ***************************


// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.

var objCookie = document.cookie;

function getCookie(name) { // use: getCookie('name');
    var index = objCookie.indexOf(name + '=');
    if (index == -1) return '';
    
    index = objCookie.indexOf('=', index) + 1;
    var endstr = objCookie.indexOf(';', index);
    if (endstr == -1) endstr = objCookie.length;
    var val = unescape(objCookie.substring(index, endstr));
    
    //protect against null
    if (val == null)  return '';
    return val;
    
}

function setCookie(name, value, daysToExpire) { 
   // typically daysToExpire = 1 day
   // but also possible daysToExpire = 180 days for long term memory
   // and to clear cookie set = -1 day
    var today = new Date();
    var expiry = new Date(today.getTime() + daysToExpire * 24 * 60 * 60 * 1000); // plus 1 days
    if (value != null && value != '')
    document.cookie=name + '=' + escape(value) + '; path=/; expires=' + expiry.toGMTString();
    objCookie = document.cookie; // update objCookie
}

