/******
*   items.js   All JavaScript functions relating to items and adding *
*				 them to a cart. Also includes code for the drop down    *
*            lists.                                                  *
*                                                                    *
*		AddItem()  - handles the adding of an item                     *
*                                                                    *
*	 Iocus eStore - Copyright (c) 1999 by Tim McEwen                  *
*                                 and NewLevel Computing, Inc        *
                                                                *****/
var sCart = null;
var opts = null;


/******
*   AddItem  Handles adding the item to the cart.  If the cart       *
*            window is open it should bring it to the front and      *
*            refresh the center.                                     *
*      nodeid - the node id to add.  must be on the current page     *
                                                                *****/
function AddItem(nodeid,arrayname,flatcart) {
	window.name='Store';
	if(nodeid == 'showcart' && flatcart==1) {
		document.location="/estore.cgi/cart/show";
		return;
	} else if(nodeid == 'showcart'){
		sCart=window.open("/estore.cgi/cart/show?cartPopUp=1" ,"ShoppingCart","toolbar=0,location=0,status=1,menubar=0,scrollbars=1,width=570,height=375");
	   sCart.focus();
		return;
	}
	if(nodeid == null){
		alert("Could not add specified item, please contact support.");
		return;
	}
	formname = 'Prod' + nodeid + 'Form';

	if(arrayname != 'NoOpts'){
		pricecheck=RefreshPrice(nodeid, arrayname);
		if(pricecheck == 0) {alert('This item is not available with the options you selected.  Please select a different combination of options.'); return;}
		if(pricecheck == -1 && !backorder) {alert('The item with the options you selected is currently out of stock.  Please select a different combination of options or check back soon.'); return;}		
	}
	
	if (flatcart!=1) {
		sCart=window.open("/estore.cgi/cart/show?cartPopUp=1" ,"ShoppingCart","toolbar=0,location=0,status=1,menubar=0,scrollbars=1,width=570,height=375");
	   sCart.focus();
		document[formname].target = "ShoppingCart";
	}			
	
	document[formname].submit();


	return;
}




function check(arrayitem, selection)
{
	if(!selection) return 1;
	if(arrayitem == selection.options[selection.selectedIndex].text) return 1;
	else return 0;
}



/******
*   RefreshPrice  Refreshes the price for a particular item based    *
* 					   based on what the user has selected from the drop  *
*                 downs.                                             *
*      nodeid - the node id to refresh.  must be on the current page *
                                                                *****/
function RefreshPrice(nodeid,arrayname){
	formname="Prod" + nodeid + "Form";
	prodarray=new Array();
	prodarray=arrayname;
	if(!document[formname].elements['price']){ alert('form not found?'); return 0;} //oops form doesn't exist
	for(x=0; x < prodarray.length; x++)
	{
		if(check(prodarray[x][0],document[formname].elements['opt0']) &&
			check(prodarray[x][1],document[formname].elements['opt1']) &&
			check(prodarray[x][2],document[formname].elements['opt2']) &&
			check(prodarray[x][3],document[formname].elements['opt3']) &&
			check(prodarray[x][4],document[formname].elements['opt4']) &&
			check(prodarray[x][5],document[formname].elements['opt5']) &&
			check(prodarray[x][6],document[formname].elements['opt6']) &&
			check(prodarray[x][7],document[formname].elements['opt7']) &&
			check(prodarray[x][8],document[formname].elements['opt8']) &&
			check(prodarray[x][9],document[formname].elements['opt9']))
		{
			if(prodarray[x][11] == '0') {document[formname].elements['price'].value = '--- OUT ---'; return -1;}
			document[formname].elements['price'].value = prodarray[x][10];
			return 1;
		}
	}
	document[formname].elements['price'].value = 'N/A';	
	return 0;
}

