/******
*   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=window.open("" ,"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('Undeterminable options.'); 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 = '$' + money(prodarray[x][10]);
			return 1;
		}
	}
	document[formname].elements['price'].value = 'N/A';
	return 0;
}

function money(amount) {
      amount = Math.round(amount * 100);	
      amount = parseFloat(amount / 100);
      if( amount == parseInt(amount) )	
         return(amount + ".00");

      if((amount * 10) == parseInt(amount * 10))
         return(amount + "0");

      return(amount);

	
}

function round(number,X) {
// rounds number to X decimal places, defaults to 2
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function DynamicPrice(nodeid, prodarray){


	baseprice=prodarray['price'];
	addonprice=0;
	formname="Prod" + nodeid + "Form";
	if(document[formname].elements['quant'])
		qty=document[formname].elements['quant'].value ;
	else
		qty=1;
	if(!document[formname].elements['price']){ alert('Undeterminable options.'); return 0;} //oops form doesn't exist

	// First, check to see if we should use a different base price as the result of combos.
	if(prodarray['combos']>0)
	{
		comboprice=-1;
		combarray=prodarray['comblist'];
		for(x=0; x < combarray.length; x++)
		{
			if(check(combarray[x][0],document[formname].elements['opt0']) &&
				check(combarray[x][1],document[formname].elements['opt1']) &&
				check(combarray[x][2],document[formname].elements['opt2']) &&
				check(combarray[x][3],document[formname].elements['opt3']) &&
				check(combarray[x][4],document[formname].elements['opt4']) &&
				check(combarray[x][5],document[formname].elements['opt5']) &&
				check(combarray[x][6],document[formname].elements['opt6']) &&
				check(combarray[x][7],document[formname].elements['opt7']) &&
				check(combarray[x][8],document[formname].elements['opt8']) &&
				check(combarray[x][9],document[formname].elements['opt9']))
			{
				if(combarray[x][11] == '0') {document[formname].elements['price'].value = '--- OUT ---'; return -1;}
				comboprice = combarray[x][10];
				
			}
		}
		if(comboprice==-1){
			document[formname].elements['price'].value = 'N/A';
			return 1;
		
		}
		baseprice=comboprice;
	}	

	optarray=prodarray['optlist'];
	for(x=0; x < optarray.length; x++)
	{
		//alert('Adding: ' + prodarray[x][1][document[formname].elements['addon' + prodarray[x][0]].selectedIndex]);
		addonprice+=optarray[x][1][document[formname].elements['addon' + optarray[x][0]].selectedIndex];
	}
	//document[formname].elements['price'].value = 'N/A';
	//alert('Refreshing price. Base price is: ' + prodarray['price'] + '\n Add On price is: ' + addonprice);

	newprice=(baseprice+addonprice) * qty;

	document[formname].elements['price'].value='$' + money(newprice);
	return 0;
}



