//*** Copyright (C) 2000 uSight.com, All Rights Reserved 

//global name of the cookie, and script that displays the shopping cart items
var COOKIE = "xyz"; //"product";
var DISPLAY_CART = "displayCart.asp";
var IE = (document.all) ? true : false;
var NE = (document.layers) ? true : false;
var debug = false;

//returns the cookie string for an individual product
function createProdStr( uid, desc, quant ){
	var result;
	//make sure all data is given set defaults if not
	if ( uid == "" ) uid = "-1";
	if ( desc == "" ) desc = "-1";
	if ( quant == "" ) quant = "1";
	
	//create and return the string
	result = uid + ":" + desc +":" + quant; 
	return result;
}

//set the cookie
function setCookie( cName, cValue )
{
	var str;
	myData = getCookie( cName );
  if ( cValue != "" )
		str = cName + "=" + escape( cValue + "|" + myData );
	document.cookie = str + "; path=/";
}

//get the cookie
function getCookie( cName )
{
	var cookieEnd, begin, cookieData;
	cookieData = "";
	//search the cookies for a matching cookie
	for ( begin = 0; begin < document.cookie.length; begin++ ){
		var cNameLength = begin + cName.length;
		if ( document.cookie.substring( begin, cNameLength ) == cName ){
			//found the cookie we want
			cookieEnd = document.cookie.indexOf( ";", cNameLength );
			if ( cookieEnd == -1 ){
				cookieEnd = document.cookie.length;
			}
			cookieData = document.cookie.substring( cNameLength, cookieEnd );
			if ( cookieData.indexOf( "=" ) != -1 ){
				cookieData = cookieData.substring( cookieData.indexOf( "=" )+1, cookieData.length );
			}
			return unescape( cookieData );
		}
	}
	return unescape( cookieData );
}

//add the item to the cart
function addItemToCart( formObj ){
	var uid = "", info = "", quant = "";
	var cookiedata = "";
	var iserror = false;

	//get the product info
	uid = ( formObj.UID ) ? formObj.UID.value : "-1";
	info = ( formObj.INFO ) ? formObj.INFO.value : "-1";
	quant = ( formObj.QUANTITY ) ? formObj.QUANTITY.value : "1";
	
	//place any javascript error checking here if needed
	if ( isNaN( quant ) ){ 
		alert( "Please Enter a valid Quantity." );
		formObj.QUANTITY.value = "";
		formObj.QUANTITY.focus();
		iserror = true;
	}
	//end any javascript error checking
	
	//format the cookie, set it
	cookiedata = createProdStr( uid, info, quant );
	setCookie( COOKIE, cookiedata );
	
	//display the shopping cart
	if ( debug == true )
		alert( document.cookie );
	if ( iserror == false )
		formObj.submit();//document.location.href = DISPLAY_CART;
}

//empty the shopping cart
function emptyCart()
{
	document.cookie = COOKIE + "=; path=/";
	document.cookie = "total=0.00";
	document.location.href = DISPLAY_CART;
}

function updateQuantity( itemNumber )
{
	if ( debug == true )
		alert("before: " + document.cookie);
	
	var item = "item" + itemNumber;
	var quantity = "quantity" + itemNumber;
	
	var StrToFind = eval( "document.REGISTER." + item + ".value" );
	var newVal = eval( "document.REGISTER." + quantity + ".value" );
	
	if ( isNaN( newVal ) ){
		alert( "Please Enter a valid Quantity" );
		eval( "document.REGISTER." + quantity + ".focus()" );
		return;
	}
	
	var cookieStr = getCookie( COOKIE );	
	var re = / /;
	StrToFind = StrToFind.replace( re, "+" );
	var beginIdx = cookieStr.indexOf( StrToFind ) ;	
	//alert( cookieStr + "\n" + StrToFind );
	if ( beginIdx == -1 ){
		alert( "Product No Longer in Cart" );
		//got a problem here StrToFind isn't in the cookie
		//cookie wasn't set properly
	} else {
		//find the |
		var indexColon;
		for( var i = beginIdx; i < cookieStr.length; i++ ){
			if ( cookieStr.charAt( i ) == '|' ){
				break;
			}
			if ( cookieStr.charAt( i ) == ':' ){
				indexColon = i;
				//by the time this is done the last colon index will be the one I want
			}
		}
		
		var newStr = cookieStr.substr( 0, indexColon + 1 );
		newStr += newVal;
		newStr += cookieStr.substr( i );
		//alert( newStr );
		document.cookie = COOKIE + "=" + escape( newStr ) + "; path=/";
	}	
	if ( debug == true )
		alert("after: " + document.cookie);
	
	document.REGISTER.submit();
}

function updateShipping() {
	var shippingIndex = document.forms[0].shipmethod.selectedIndex;
	var shippingStr = document.forms[0].shipmethod.options[shippingIndex].value;
	shippingStr = escape(shippingStr);
	document.cookie = "shipping=" + shippingStr;
	document.forms[0].submit();
}

//MADONNA TAX FIX
function removeTax( removeVal ){
	document.forms[0].californiaresident.value = removeVal == 1 ? 0 : 1;
	document.forms[0].submit();
}

