/* JCart by CIDEX Computer Systems
 * Copyright (c) 1997 - CIDEX Computer Systems, All Rights Reserved
 * revised for Empire Coffee and Tea - 1999
 *
 * revised to add cart tracking - 2004
 */
 
// Installation Specific Data:
// Cart Message Type -- info msg displayed when change made to cart
// either "none", "alert", "status", "alert,status"
var cartinfotype = "alert,status"

// set up static page vars
// Nav 2 cannot update button value so use longer value
var btnlbl = "Add/Remove from Cart"
if ( nav3comp() ) 
	btnlbl = "Remove from Cart"

function nav3comp() {
	// check for navigator version 2 or old MSIE
	var ver = parseFloat( navigator.appVersion.substring(0) )
	var xspot = navigator.appVersion.indexOf("MSIE")
	if ( xspot > -1 ) {
		// MSIE - use their version directly
		ver = parseFloat( navigator.appVersion.substring(xspot+1) )
	}
	return ( ver >= 3 )
}

// display info about the cart
function cartmsg() {
	// display info box about the cart
	document.write( '<CENTER><TABLE BORDER=1 WIDTH="75%" CELLPADDING=5 BGCOLOR="#FFFF66"><TR><TD>' )
	document.write( '<CENTER>The Picture Palace Shopping Cart System</CENTER>' )
	document.write( '<HR ALIGN="CENTER" WIDTH="80%" NOSHADE>' )
	document.write( 'This page uses our new <A HREF="/cart/cartinfo.html">Shopping Cart</A> system.' )
	document.write( 'You can order anytime just by clicking the box next to each item.  When done you can shop around our site or <A HREF="/cgi-bin/chkout.cgi">Buy</A> your selection.' )
	//document.write( 'This enables you to make your selections on this page, and have them automatically transferred to our <A HREF="#checkout">Check Out</A> page when you are ready to complete your order.' )
	//document.write( '<HR ALIGN="CENTER" WIDTH="80%" NOSHADE>' )
	//document.write( 'You can order anytime just by clicking the box next to each item.  When done you can shop around our site or hit the Buy button.' )
	document.write( '</TD></TR></TABLE></CENTER>' )
}

function cartinfo( ptitle, pmode ) {
	// msg(s) when item is added/removed from he cart
	var paction = ( pmode == 0 ? "has been removed from your order" : "has been added to your order" )
	if ( cartinfotype.indexOf( "status" ) > -1 ) window.status = "\"" + ptitle + "\" " + paction
	if ( cartinfotype.indexOf( "alert" ) > -1 ) {
		if ( pmode == 0 ) {
			alert( "\n\"" + ptitle + "\" " + paction )
		} else {
			alert( "\n\"" + ptitle + "\" " + paction + "\n\n" +
				"* To remove this item from your order: click the green buy oval again.\n" +
				"* To buy more than one of this item: change the quantity when you Checkout.\n\n"+
				"Remember: nothing is final yet.You can change anything at Checkout!" )
		}
	}
}

function setorder( xtxt ) {
	// modify value of our cookie
	if ( xtxt.substring( 0, 1 ) == "*" )
		xtxt = xtxt.substring( 1 )
	if ( xtxt.length == 0 )
		document.cookie = "order=; path=/; expires=Friday, 01-Jan-88 00:00:01 GMT"
	else
		document.cookie = "order=" + xtxt + "; path=/;"
}

function getorder() {
	var retstr = "*"								// dummy value to placate Nav 2
	var xstart = document.cookie.indexOf( "order=" )+0	// and force numeric
	if ( xstart > -1 ) {							// cookie exists 
		xstart += 6             					// set index of beginning of value
		if ( xstart < document.cookie.length ) {			// avoid blank cookies
			xend = document.cookie.indexOf(";", xstart)  	// set index of end of cookie value
	        if ( xend == -1 ) 
				xend = document.cookie.length
	        retstr = document.cookie.substring( xstart, xend )
		}
 	}
	return retstr
}

function dolstr( num ) {
	var xnum, tmp, dol, cnt
	xnum = (num*100)+0							// force pennies
	// string constructor added ver 3, force to string ns 2 way
	//eval( 'tmp = "' + Math.round( Math.abs( xnum )) +'"' )
	tmp = "0" + Math.round( Math.abs( xnum ))					
	dol = ( xnum < 100 ? "0" : tmp.substring( 1, tmp.length-2) )	// skip init "0" (that we added)
	cnt = "00" + tmp.substring( tmp.length-2, tmp.length )
	return  dol + "." + cnt.substring( cnt.length-2, cnt.length )
}

function provitem( pitem ) {
	// proofing
	var uchar = pitem.charAt(0);
	var okstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	if ( okstr.indexOf( uchar ) < 0 ) {
		alert( "Error in Item\nItem:" + pitem )
		return false
	}
	return true
}

function chkitem( pitem, ptitle, pcost ) {
	//if ( !provitem( pitem )) return;

	document.write( '<INPUT TYPE="CHECKBOX" NAME="' + pitem + '" ONCLICK="checkcitem( this, \'' + escape( ptitle ) + '\',' + pcost + ' )">' )
	
	var frm = document.forms.length-1;
	var thisfrm = document.forms[frm];
	
	var elm = thisfrm.elements.length-1;
	var thiselm = thisfrm.elements[elm];
	
	var order = getorder();
	//var chkstat = false;
	//if ( order.indexOf( "^^" + pitem ) > -1 )
	//	chkstat = true;
	//document.forms[frm].elements[elm].checked = chkstat
	//thiselm.checked = chkstat;
	thiselm.checked = ( order.indexOf( "^^" + pitem ) > -1 );
}

function btnitem( pitem, ptitle, pcost ) {
	//if ( !provitem( pitem )) return;
	// use global btnlbl
	// always write longer text then adjust value of button to shorter text
	document.write( '<INPUT TYPE="BUTTON" NAME="' + pitem + '" VALUE="' + btnlbl + '" ONCLICK=\'checkbitem( this, "' + escape( ptitle ) + '",' + pcost + ' )\'>' )
	var frm = document.forms.length-1;
	var thisfrm = document.forms[frm];
	
	var elm = thisfrm.elements.length-1;
	var thiselm = thisfrm.elements[elm];
	
	var order = getorder()
	//var frm = document.forms.length-1
	//var elm = document.forms[frm].elements.length-1
	//if ( order.indexOf( "^^"+document.forms[frm].elements[elm].name ) == -1 )
	//	document.forms[frm].elements[elm].value = "Add to Cart"
	if ( order.indexOf( "^^"+pitem ) == -1 )
		thiselm.value = "Add to Cart"
}

function itemstatus( ctrl, ptitle ) {
	var order = getorder()
	if ( order.indexOf( "^^"+ctrl.name ) == -1 ) {
		return 'Add "' + unescape( ptitle ) + '" to your Order';
	} 
	return 'Remove "' + unescape( ptitle ) + '" from your Order';
}

function imgitem( pitem, ptitle, pimage, pwidth, pheight, pcost, pweight ) {
	// input type image dosen't support onclick events in netscape
	// just use a regular href
	document.write( '<A HREF="#" ONMOUSEOVER="status=itemstatus( this, \'' + escape( ptitle ) + '\' );return true;" ONMOUSEOUT="status=\'\';return true;" ONCLICK=\'return checkiitem( "' + pitem +'", "' + escape( ptitle ) + '",' + pcost + ', ' + pweight +')\'>' +
					'<IMG NAME="' + pitem +'" SRC="' + pimage + '" BORDER="0" WIDTH="' + pwidth + '" HEIGHT="' + pheight + '">' +
					'</A>' );
}

function chkcell( pitem, ptitle, pcost ) {
	document.write( '<TD ALIGN="CENTER">' )
	chkitem( pitem, ptitle, pcost )
	document.write( '</TD>' )
}

function griditem( pitem, ptitle, pcost ) {
	document.write( '<TR>' )
	document.write( '<TD ALIGN="Center">' )
	chkitem( pitem, ptitle, pcost )
	document.write( '</TD>' )
	// variable items here...
	if ( griditem.arguments.length == 3 ) {
		document.write( '<TD>' + ptitle + '</TD>' )
	} else {
		document.write( '<TD>' + griditem.arguments[3] + '</TD>' )
	}
	for ( var ctr = 4; ctr < griditem.arguments.length; ctr++ ) {
		document.write( '<TD>' + griditem.arguments[ctr] +'</TD>' )
	}
	document.write( '<TD>$' + dolstr( pcost ) +'</TD></TR>' )
}

function additem( pname, pesctitle, pcost, pweight ) {

	var xquan = "1"
    var xtxt = "^^" + pname + "^" + pesctitle + "^" + xquan + "^" +pcost + "^" + pweight
	var ptitle = unescape( pesctitle )
	var order = getorder()
	// add item - make sure not already there (but shouldn't be if checker is working)
	if ( order.indexOf( "^^"+pname ) == -1 ) {
		// new! - check for room
		if ( order.length + xtxt.length > 3800) {	// save room for overflow and other items in cookie
			alert( "\nYour \"Shopping Cart\" is Full\nNo More Items Can Be Added Until You Check-Out")
		} else {
			setorder( order + xtxt )
			logitem( pname, pesctitle, xquan )
			cartinfo( ptitle, 1 )
			return true
		}
	} else {
	    alert( "\n" + navigator.appName + " Browser Error!\n" + ptitle + "\nis already in your order, screen updated" )
	}
	return false
}

function removeitem( pname, pesctitle ) {		
	var order = getorder()
	var itemstart = order.indexOf( "^^" + pname )
	var ptitle = unescape( pesctitle )
	if ( itemstart == -1 ) {
		// not there
		alert( "\n" + navigator.appName + " Browser Error!\n" + ptitle + "\nwas not in your order, screen updated" )
	} else {
		// remove item
		var itemend = order.indexOf( "^^", itemstart+2 )
		if ( itemend == -1 ) 			// strip to end
			itemend = order.length
		setorder( order.substring( 0, itemstart) + order.substring( itemend, order.length ))
		logitem( pname, pesctitle, 0 )
		cartinfo( ptitle, 0 )
		return true
	}
	return false
}
	
function logitem( pID, pName, pQuan ) {
	var newloc =  "/cgi-bin/addtocart.cgi?N=" + pID + "&T=" + pName + "&Q=" + pQuan;
	location = newloc;
}

function checkcitem( ctrl, ptitle, pcost ) {
    if (ctrl.checked) {
		if ( additem( ctrl.name, ptitle, pcost ))
			ctrl.checked = true
    } else {
		if ( removeitem( ctrl.name, ptitle ))
			ctrl.checked = false
    }
}

function checkbitem( ctrl, ptitle, pcost ) {
	var order = getorder()
	if ( order.indexOf( "^^" + ctrl.name ) == -1 ) {
		if ( additem( ctrl.name, ptitle, pcost ))
			ctrl.value = "Remove from Cart"
    } else {
		if ( removeitem( ctrl.name, ptitle ))
			ctrl.value = "Add to Cart"
    }
}

function checkiitem( pname, ptitle, pcost, pweight ) {
	var order = getorder()
	if ( order.indexOf( "^^" + pname ) == -1 ) {
		additem( pname, ptitle, pcost, pweight )
    } else {
		removeitem( pname, ptitle )
    }
	return false;
}

// EOF
