function MM_preloadimages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadimages.arguments; 
    for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

if (document.images) {
 home_on = new Image();
 home_on.src = "../images/menu_home_on.gif";

 home_off = new Image();
 home_off.src = "../images/menu_home_off.gif";

 wines_on = new Image();
 wines_on.src = "../images/menu_wines_on.gif";

 wines_off = new Image();
 wines_off.src = "../images/menu_wines_off.gif";
 
 winemakers_on = new Image();
 winemakers_on.src = "../images/menu_winemakers_on.gif";

 winemakers_off = new Image();
 winemakers_off.src = "../images/menu_winemakers_off.gif";

 estate_vineyard_on = new Image();
 estate_vineyard_on.src = "../images/menu_estate_vineyard_on.gif";

 estate_vineyard_off = new Image();
 estate_vineyard_off.src = "../images/menu_estate_vineyard_off.gif";

 order_on = new Image();
 order_on.src = "../images/menu_order_on.gif";

 order_off = new Image();
 order_off.src = "../images/menu_order_off.gif";
 
 newsletter_on = new Image();
 newsletter_on.src = "../images/menu_newsletter_on.gif";

 newsletter_off = new Image();
 newsletter_off.src = "../images/menu_newsletter_off.gif";
 
 gallery_on = new Image();
 gallery_on.src = "../images/menu_gallery_on.gif";

 gallery_off = new Image();
 gallery_off.src = "../images/menu_gallery_off.gif";
  
 mailing_list_on = new Image();
 mailing_list_on.src = "../images/menu_mailing_list_on.gif";

 mailing_list_off = new Image();
 mailing_list_off.src = "../images/menu_mailing_list_off.gif";
 
 contact_on = new Image();
 contact_on.src = "../images/menu_contact_on.gif";

 contact_off = new Image();
 contact_off.src = "../images/menu_contact_off.gif";
 
 links_on = new Image();
 links_on.src = "../images/menu_links_on.gif";

 links_off = new Image();
 links_off.src = "../images/menu_links_off.gif";
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openWin(URL,NAME,w,h) 
{
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=no,menubar=no,status=no'
  window.open(URL,NAME,winprops)
}

function openWinScroll(URL,NAME,w,h)
{
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,menubar=no,status=no'
  window.open(URL,NAME,winprops)
}

// **************************************************
// STATUS MESSAGES
// **************************************************

// EXAMPLE:
// <A HREF="" ONMOUSEOVER="return showStatus('{ Message }')" ONMOUSEOUT="hideStatus()">

function hideStatus(){ return showStatus(' '); }
function showStatus(str){ window.status = str; return true }

// **************************************************
// NETSCAPE RESIZE FIX
// **************************************************

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);







// *************************************************
// Cart functions
// *************************************************
var iNumOfItems = 0;

function CopyFields() {
	var form = document.getElementById('orderform');
    //if (form.PickUp.checked == false)
    //{
    form.Ship_Name.value = form.Name.value;
    form.Ship_Company.value = form.Company.value;
    form.Ship_Address.value = form.Address.value;
    form.Ship_City.value = form.City.value;
    form.Ship_State.selectedIndex = form.State.selectedIndex;
    form.Ship_Zip.value = form.Zip.value;
    form.Ship_Phone.value = form.Phone.value;
    //}
}

function ChangeShipping(form) {
    if (form.PickUp.checked == true) {
        form.ShipName.disabled=true;
        form.ShipName.value="";
        form.ShipCompany.disabled=true;
        form.ShipCompany.value="";
        form.ShipAddress.disabled=true;
        form.ShipAddress.value="";
        form.ShipCity.disabled=true;
        form.ShipCity.value="";
        form.ShipState.disabled=true;
        form.ShipState.selectedIndex = 0;
        form.ShipZip.disabled=true;
        form.ShipZip.value="";
        form.ShipPhone.disabled=true;
        form.ShipPhone.value="";
    }
    if (form.PickUp.checked == false) {
        form.ShipName.disabled=false;
        form.ShipCompany.disabled=false;
        form.ShipAddress.disabled=false;
        form.ShipCity.disabled=false;
        form.ShipState.disabled=false;
        form.ShipZip.disabled=false;
        form.ShipPhone.disabled=false;
    }
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return "$" + s;
}

function StripDollar(inValue) {
	var ReplacedAmount = inValue.replace(/\$/g,'');
	return ReplacedAmount; 
}

function CalcTotal(form) {
	var temptotal = 0;
	var output = "";
	for (var i=1; i<iNumOfItems+1; i++)
	{
		var thisitem = 'product'+i;
		var thisformitem = 'prod'+i;
		thistotal = document.getElementById(thisformitem).value;
		//eval('thistotal = form.prod'+i+'.value');
		
		itemprice = parseInt(StripDollar(document.getElementById(thisitem).innerHTML));
		output += temptotal + ":" + thistotal + ":" + itemprice + "\n";
		if (form.Member.checked == true)
		{
			// Apply the discount
		}
		temptotal = (thistotal * itemprice) + temptotal;
	}

	document.getElementById('subtotal').innerHTML = CurrencyFormatted(temptotal);
}



function validate_required(value,alerttxt)
{
	if (value==null||value=="") {
		alert(alerttxt);
		return false;
	} else {
		return true;
	}
}

function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_required(document.getElementById('email').value, "Email must be filled out!") == false) {
			document.getElementById('email').focus();
			return false;
		}
		if (document.getElementById('email').value != document.getElementById('Email_Confirm').value) {
			alert("Confirm e-mail does not match!");
			document.getElementById('Email_Confirm').focus();
			return false;
		}
	}
}



function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	}
	else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

function buttonEndings() {
	if (!document.getElementsByTagName) {
		return false
	}
	
	var buttons = getElementsByClass("button");
	/* loop through all buttons and attach a child div */
	for (i=0; i < buttons.length; i++) {
		var div = document.createElement("div");
		div.className = "buttonEnding";
		insertAfter(div, buttons[i]);
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
			    oldonload();
			}
			func();
		}
	}
}

addLoadEvent(buttonEndings);