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

//global name of the cookie, and script that displays the shopping cart items
var COOKIE = "xyz"; 
var DISPLAY_CART = "displayCart.php3";
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;
}

function getCheckedColor( colorObj ){
	for ( var i = 0; i < colorObj.length; i++ ){
		if ( colorObj[i].checked == true ){
			return colorObj[i].value;
		}
	}
	return -1;
}

function addOptionToCart( formObj ){
	if ( formObj.INFO ){
		if ( formObj.COLOR ){
			formObj.INFO.value = getCheckedColor( formObj.COLOR );
		}
	}
	addItemToCart( formObj );
}

//empty the shopping cart
function emptyCart()
{
	document.cookie = COOKIE + "=; path=/";
	document.cookie = "tot=0.00; path=/";
	if ( debug == true)
		alert(document.cookie);
	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 = / /g;
	StrToFind = StrToFind.replace( re, "+" );
	var beginIdx = cookieStr.indexOf( StrToFind ) ;	
	//alert("cookie: "+ cookieStr + "\nStrToFind:" + 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();
}

function removeTax( removeOpt ){
	document.forms[0].removeVal.value = removeOpt;
	document.forms[0].submit();
}

function addToCart2(theForm) {
	var iserror = false;
	var quant = ( theForm.QUANTITY ) ? theForm.QUANTITY.value : "1";

	if ( isNaN( quant ) ){ 
		alert( "Please Enter a valid Quantity." );
		theForm.QUANTITY.value = "";
		theForm.QUANTITY.focus();
		iserror = true;
	}
	if (theForm.OPTIONS) {
		if (theForm.OPTIONS.options[theForm.OPTIONS.selectedIndex].value == "") {
		alert("Please Select an Option.");
		theForm.OPTIONS.focus();
		iserror = true;
		}
	}
	
	if ( debug == true )
		alert( document.cookie );
	if ( iserror == false )
		theForm.submit();
}

var subWin;

function popup(imageName,h,w) {
	if (!subWin || subWin.closed) {
    	subWin = window.open( imageName, "", "height=" + h + ",width=" + w + ",scrollbars=1" );
    } else {
        subWin.close();
        subWin = window.open( imageName, "", "height=" + h + ",width=" + w + ",scrollbars=1" );
     } 
}
