// Shopping Cart Engine	                      *
// ********************************************

var Cart        = new Array();
var tmpArray    = new Array();
var shipDesc    = '';
var cardDesc    = '';
var cardName    = '';
var cardNo      = '';
var cardMonth   = '';
var cardYear    = '';
var postAction  = '';
var delim       = '|';
var paymentWin;
var shopperArray = new Array();
var shipeeArray  = new Array();
var CatNo      = '';
var title      = '';
var Category   = '';
var Media      = '';



//function CartItem(RECREATE, prodID, qty,       desc,    price, opt, limit){
function CartItem(RECREATE,   CatNo, title, Category, Media){




	if(RECREATE){
		this.CatNo = CatNo;
		this.title    = title;
		this.Category  = Category;
		this.Media   = Media;

	} else {
		this.CatNo = CatNo;
		this.title    = title;
		this.Category  = Category;
		this.Media   = Media;


	}
//alert(this.CatNo);
//alert(this.title);
//alert(this.Category);
//alert(this.Media);

}

function AddItem(CatNo, title, Category, Media){
	var madeChange = false;
	var alreadyExists = false;
	var newItem = false;

 
		if(Cart.length > 0){
			for(i=0; i < Cart.length; i++){
				 //alert(i +'. ' + Cart[i].CatNo 
				 //+ ', t='       + Cart[i].title
				 //+ ' c='         + Cart[i].Category
				 //+ ' m='         + Cart[i].Media ) ;

				if(Cart[i].CatNo == CatNo){
				// alert("Item " + i + " is currently in the shopping cart.");
					var newCategory = Category;
					var newMedia    = Media;

					if(newCategory != Cart[i].Category){
						madeChange = false;
						alreadyExists = false;
					} else if(Cart[i].Media != Media){
						alreadyExists = true;
						break;
					} else {
						alreadyExists = true;
						alert('Item ' + Cart[i].CatNo + ' ' + Cart[i].title + '\n already exists in your clipboard.');
						break;
					}
				}
			}
			if(!madeChange && !alreadyExists){
				newItem = true;
			}
		} else {
			newItem = true;
		}
		if(newItem){
			Cart[Cart.length] = new CartItem(false, CatNo, title, Category, Media);

			if(supressCart){
				alert('# ' + Cart[Cart.length - 1].CatNo
				+ '\n '       + Cart[Cart.length - 1].title
				+ '\n    CATEGORY: '  + Cart[Cart.length - 1].Category 
				+ '\n           MEDIA: '  + Cart[Cart.length - 1].Media
				+ '\n' + AddedText );

				cartToCookie();
			} else {
				displayCart();
			}
		}
}


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


function DeleteItem(arrayNum){
	var deletedItem;
	tmpArray = new Array();
	for(i=0; i < Cart.length; i++){
		if(i != arrayNum){
			tmpArray[tmpArray.length] = Cart[i];
		} else {
			deletedItem = Cart[i];


			Cart[i].CatNo = "" ;
			Cart[i].title    = "" ;
			Cart[i].Category   = "" ;
			Cart[i].Media  = "" ;
			Cart[i]        = "" ;
			}
	}
	Cart = new Array();
	for(i=0; i < tmpArray.length; i++){
		Cart[i] = tmpArray[i];
	}
	tmpArray = new Array();
	if(deletedItem){
		if(supressCart && String(location).indexOf(cartPage) == -1){
			cartToCookie();
			alert('Item ' + Cart[arrayNum].prodID + ', (' + Cart[arrayNum].desc + ')\nhas been deleted from your cart.');
		} else {
			displayCart();
		}
	} else {
		alert('Item not found in cart.');
	}
}


function UpdateItem(arrayNum){
	oldQty = Cart[arrayNum].qty;
	newQty = getValue(Cart[arrayNum].prodID,arrayNum);
	if(newQty == 0){
		DeleteItem(arrayNum);
		return true;
	} else if(!Number(newQty) || newQty < 0){
		alert('Please enter a whole\nnumerical value.');
		return false;
	} else if(newQty != oldQty){
		ChangeQty(arrayNum,newQty);
	}
}

function ChangeQty(arrayNum, newQty){
	var OK = false;
	var underLimit = true;
	if(newQty > Number(Cart[arrayNum].limit) && Cart[arrayNum].limit != ''){
		alert('You may not order more than ' + Cart[arrayNum].limit + '\nof these during a session.\nYour quantity will be changed\nto the maximum amount.');
		Cart[arrayNum].qty = Cart[arrayNum].limit;
	} else {
		Cart[arrayNum].qty = newQty;
	}

	if(supressCart && String(location).indexOf(cartPage) == -1){
		cartToCookie();
		alert('Item ' + Cart[arrayNum].prodID + ', (' + Cart[arrayNum].desc + ')\nhas been updated in your cart.');
	} else {
		displayCart();
	}
}

function getValue(itemID,valType){
	
	objVal =  EVAL("document.form." +  itemID + "_" + valType) ;

//	objVal = eval("document.frmAdmin." + itemID + "_" + valType);

	if(Number(valType)){return objVal.value; }
	return objVal;
}



function DeleteCart(){
	if(confirm('Are you sure you want\nto clear the entire Clipboard?')){
		Cart = new Array();
		displayCart();
	}
}

function displayCart(){
	cartToCookie();
	document.location = cartPage;
}


function fillShopperfrmAdmin(){
	shopperArray = getCookieVal(myStoreName + '_shopper');
	if(shopperArray == null || shopperArray == ''){
			shopperArray = new Array();
	}
	shipeeArray = getCookieVal(myStoreName + '_shipee');
	if(shipeeArray == null || shipeeArray == ''){
			shipeeArray = new Array();
	}
	if(shopperArray.length > 0){
		if(shopperArray[0] != ''){
			document.frmAdmin.fname.value = shopperArray[0];
		}
		if(shopperArray[1] != ''){
			document.frmAdmin.lname.value = shopperArray[1];
		}
		if(shopperArray[2] != ''){
			document.frmAdmin.email.value = shopperArray[2];
		}
		if(shopperArray[3] != ''){
			document.frmAdmin.add1.value = shopperArray[3];
		}
		if(shopperArray[4] != ''){
			document.frmAdmin.add2.value = shopperArray[4];
		}
		if(shopperArray[5] != ''){
			document.frmAdmin.city.value = shopperArray[5];
		}
		if(shopperArray[6] != ''){
			document.frmAdmin.state.value = shopperArray[6];
		}
		if(shopperArray[7] != ''){
			document.frmAdmin.zip.value = shopperArray[7];
		}
		if(shopperArray[8] != ''){
			document.frmAdmin.country.value = shopperArray[8];
		}
		if(shopperArray[9] != ''){
			document.frmAdmin.phone.value = shopperArray[9];
		}
	}
	if(shipeeArray.length > 0){
		if(shipeeArray[0] != ''){
			document.frmAdmin.Sfname.value = shipeeArray[0];
		}
		if(shipeeArray[1] != ''){
			document.frmAdmin.Slname.value = shipeeArray[1];
		}
		if(shipeeArray[2] != ''){
			document.frmAdmin.Sadd1.value = shipeeArray[2];
		}
		if(shipeeArray[3] != ''){
			document.frmAdmin.Sadd2.value = shipeeArray[3];
		}
		if(shipeeArray[4] != ''){
			document.frmAdmin.Scity.value = shipeeArray[4];
		}
		if(shipeeArray[5] != ''){
			document.frmAdmin.Sstate.value = shipeeArray[5];
		}
		if(shipeeArray[6] != ''){
			document.frmAdmin.Szip.value = shipeeArray[6];
		}
		if(shipeeArray[7] != ''){
			document.frmAdmin.Scountry.value = shipeeArray[7];
		}
		if(shipeeArray[8] != ''){
			document.frmAdmin.Semail.value = shipeeArray[8];
		}
	}
}






function fillShopperForm(){
	shopperArray = getCookieVal(myStoreName + '_shopper');
	if(shopperArray == null || shopperArray == ''){
			shopperArray = new Array();
	}
	shipeeArray = getCookieVal(myStoreName + '_shipee');
	if(shipeeArray == null || shipeeArray == ''){
			shipeeArray = new Array();
	}
	if(shopperArray.length > 0){
		if(shopperArray[0] != ''){
			document.form.fname.value = shopperArray[0];
		}
		if(shopperArray[1] != ''){
			document.form.lname.value = shopperArray[1];
		}
		if(shopperArray[2] != ''){
			document.form.email.value = shopperArray[2];
		}
		if(shopperArray[3] != ''){
			document.form.add1.value = shopperArray[3];
		}
		if(shopperArray[4] != ''){
			document.form.add2.value = shopperArray[4];
		}
		if(shopperArray[5] != ''){
			document.form.city.value = shopperArray[5];
		}
		if(shopperArray[6] != ''){
			document.form.state.value = shopperArray[6];
		}
		if(shopperArray[7] != ''){
			document.form.zip.value = shopperArray[7];
		}
		if(shopperArray[8] != ''){
			document.form.country.value = shopperArray[8];
		}
		if(shopperArray[9] != ''){
			document.form.phone.value = shopperArray[9];
		}
	}
	if(shipeeArray.length > 0){
		if(shipeeArray[0] != ''){
			document.form.Sfname.value = shipeeArray[0];
		}
		if(shipeeArray[1] != ''){
			document.form.Slname.value = shipeeArray[1];
		}
		if(shipeeArray[2] != ''){
			document.form.Sadd1.value = shipeeArray[2];
		}
		if(shipeeArray[3] != ''){
			document.form.Sadd2.value = shipeeArray[3];
		}
		if(shipeeArray[4] != ''){
			document.form.Scity.value = shipeeArray[4];
		}
		if(shipeeArray[5] != ''){
			document.form.Sstate.value = shipeeArray[5];
		}
		if(shipeeArray[6] != ''){
			document.form.Szip.value = shipeeArray[6];
		}
		if(shipeeArray[7] != ''){
			document.form.Scountry.value = shipeeArray[7];
		}
		if(shipeeArray[8] != ''){
			document.form.Semail.value = shipeeArray[8];
		}
	}
}

function Validate(orderOption, secureWin){
	var countrySel;
	var AllOk     = true;
	var tmpPhone  = '';
	var phoneChar = new Array('-','(',')',' ');
	var goodChar  = true;
	countrySel = document.form.country;
	shopperArray[0] = document.form.fname.value;
	shopperArray[1] = document.form.lname.value;
	shopperArray[2] = document.form.email.value;
	shopperArray[3] = document.form.add1.value;
	shopperArray[4] = document.form.add2.value;
	shopperArray[5] = document.form.city.value;
	shopperArray[6] = document.form.state.value;
	shopperArray[7] = document.form.zip.value;
	if(countrySel.selectedIndex < 0 || countrySel.selectedIndex == 0){
		alert('Please select your country.');
		AllOk = false;
	} else {
		shopperArray[8] = countrySel.options[countrySel.selectedIndex].text;
	}
	shopperArray[9] = document.form.phone.value;
	for(w = 0; w < shopperArray.length; w++){
		shopperArray[w] = shopperArray[w].trim();
	}
	if(getAltShipping){
		if(document.form.diffShip.checked){
			countrySel = document.form.Scountry;
			shipeeArray[0] = document.form.Sfname.value;
			shipeeArray[1] = document.form.Slname.value;
			shipeeArray[2] = document.form.Sadd1.value;
			shipeeArray[3] = document.form.Sadd2.value;
			shipeeArray[4] = document.form.Scity.value;
			shipeeArray[5] = document.form.Sstate.value;
			shipeeArray[6] = document.form.Szip.value;
			shipeeArray[8] = document.form.Semail.value;
			if(countrySel.selectedIndex < 0 || countrySel.selectedIndex == 0){
				alert('Please select your country.');
				AllOk = false;
			} else {
				shipeeArray[7] = countrySel.options[countrySel.selectedIndex].text;
			}
			for(w = 0; w < shipeeArray.length; w++){
				shipeeArray[w] = shipeeArray[w].trim();
			}
		}
	}
	if(shopperArray[0] == '' && AllOk){
		alert('Please fill out the "First Name" field');
		document.form.fname.focus();
		AllOk = false;
	}
	if(shopperArray[1] == '' && AllOk){
		alert('Please fill out the "Last Name" field');
		document.form.lname.focus();
		AllOk = false;
	}
	if(AllOk){
		if(shopperArray[2] == ''){
			alert('Please fill out the "Email" field');
			document.form.email.focus();
			AllOk = false;
		} else {
			if(shopperArray[2].indexOf('@') == -1){
				alert('Please enter a valid Email address.');
				document.form.email.focus();
				AllOk = false;
			}
		}
	}
	if(shopperArray[3] == '' && AllOk){
		alert('Please fill out the "Address" field');
		document.form.add1.focus();
		AllOk = false;
	}
	if(shopperArray[5] == '' && AllOk){
		alert('Please fill out the "City" field');
		document.form.city.focus();
		AllOk = false;
	}
	if(shopperArray[8].toLowerCase() == 'us' ||
	   shopperArray[8].toLowerCase() == 'usa' ||
	   shopperArray[8].toLowerCase() == 'united states' ||
	   shopperArray[8].toLowerCase() == 'unitedstates'){
		if(shopperArray[6] == '' && AllOk){
			alert('Please fill out the "State/Province" field');
			document.form.state.focus();
			AllOk = false;
		}
		if(shopperArray[7] == '' && AllOk){
			alert('Please fill out the "Postal Code" field');
			document.form.zip.focus();
			AllOk = false;
		}
	}
	if(shopperArray[8] == '' && AllOk){
		alert('Please fill out the "Country" field');
		document.form.country.focus();
		AllOk = false;
	}
	if(shopperArray[9] == '' && AllOk){
		if(shopperArray[9] == ''){
			alert('Please fill out the "Phone Number" field');
			document.form.phone.focus();
			AllOk = false;
		} else {
			for(i = 0; i < shoppArray[9].length; i ++){
				for(j = 0; j < phoneChar.length; j ++){
					if(shopperArray[9].charAt(i) != phoneChar[j]){
						goodChar = true;
					} else {
						goodChar = false;
					}
				}
				if(goodChar){
					tmpPhone = tmpPhone + shopperArray[9].charAt(i);
				}
			}
			if(!Number(tmpPhone)){
				alert('Please enter a valid Phone number');
				document.form.phone.focus();
				allOk = false;
			}
		}
	}
	if(getAltShipping){
		if(document.form.diffShip.checked){
			if(shipeeArray[0] == '' && AllOk){
				alert('Please fill out the "Shipping First Name" field');
				document.form.Sfname.focus();
				AllOk = false;
			}
			if(shipeeArray[1] == '' && AllOk){
				alert('Please fill out the "Shipping Last Name" field');
				document.form.Slname.focus();
				AllOk = false;
			}
			if(shipeeArray[8] == '' && AllOk){
				alert('Please fill out the "Shipping Email" field');
				document.form.Semail.focus();
				AllOk = false;
			}
			if(shipeeArray[2] == '' && AllOk){
				alert('Please fill out the "Shipping Address" field');
				document.form.Sadd1.focus();
				AllOk = false;
			}
			if(shipeeArray[4] == '' && AllOk){
				alert('Please fill out the "Shipping City" field');
				document.form.Scity.focus();
				AllOk = false;
			}
			if(shipeeArray[5] == '' && AllOk){
				alert('Please fill out the "Shipping State/Province" field');
				document.form.Sstate.focus();
				AllOk = false;
			}
			if(shipeeArray[6] == '' && AllOk){
				alert('Please fill out the "Shipping Postal Code" field');
				document.form.Szip.focus();
				AllOk = false;
			}
			if(shipeeArray[7] == '' && AllOk){
				alert('Please fill out the "Shipping Country" field');
				document.form.Scountry.focus();
				AllOk = false;
			}
		} else {
			shippeeArray = new Array();
			killCookie(myStoreName + '_shipee');
		}
	}
	if(AllOk){
		setCookie(myStoreName + '_shopper',shopperArray.join(delim),eval(customerTime),cookiePath,unsecureDomain);
		if(shipeeArray.length > 0){
			setCookie(myStoreName + '_shipee',shipeeArray.join(delim),null,cookiePath,unsecureDomain);
		}
		routeToPayment(orderOption, secureWin);
	}
}







function routeToPayment(orderOption, secureWin){
	if(useShipOptions){
		getShipDetails();
	}
	killCookie(myStoreName + '_shipDesc');
	killCookie(myStoreName + '_shipAmt');
	killCookie(myStoreName + '_shipPerItem');
	setCookie(myStoreName  + '_shipDesc'    ,shipDesc      ,eval(cartTime),cookiePath,unsecureDomain);
	setCookie(myStoreName  + '_shipAmt'     ,shipAmt       ,eval(cartTime),cookiePath,unsecureDomain);
	setCookie(myStoreName  + '_shipPerItem' ,shipPerItem   ,eval(cartTime),cookiePath,unsecureDomain);
	if(orderOption == '' || orderOption == null){
		document.location = COprintVerify;
	} else {
		if(secureWin && securePath != ''){
			alert('Secure Ordering is\nnot offered in\nthe Demo.');
		} else {
			document.location = orderOption;
		}
	}
}







function cartToCookie(){
 // write cookie details
	for (cc = 0; cc < 10000; cc++){
		cookieVal = getCookieVal(myStoreName + '_item' + cc);
		if(cookieVal != '' && cookieVal != null){
			killCookie(myStoreName + '_item' + cc);
		} else {
			break;
		}
	}
	if(Cart.length > 0){
		for(cc = 0; cc < Cart.length; cc++){
			itemContents  = '';
			itemContents += Cart[cc].CatNo + delim;
			itemContents += Cart[cc].title    + delim;
			itemContents += Cart[cc].Category   + delim;
			itemContents += Cart[cc].Media  + delim;
			setCookie(myStoreName + '_item' + cc,itemContents,eval(cartTime),cookiePath,unsecureDomain);
		}
	}
}

function cookieToCart(){
 // read cookie details
	shopperArray = getCookieVal(myStoreName + '_shopper');
	if(shopperArray == null || shopperArray == ''){
			shopperArray = new Array();
	}
	shipeeArray = getCookieVal(myStoreName + '_shipee');
	if(shipeeArray == null || shipeeArray == ''){
			shipeeArray = new Array();
	}
	shipDesc = getCookieVal(myStoreName + '_shipDesc');
	tmpShipAmt = getCookieVal(myStoreName + '_shipAmt');
	if(tmpShipAmt != null && tmpShipAmt != ''){
		shipAmt = tmpShipAmt;
	}
	tmpShipPerItem = getCookieVal(myStoreName + '_shipPerItem');
	if(tmpShipPerItem != null && tmpShipPerItem != ''){
		tmpShipPerItem = (tmpShipPerItem == 'true') ? true : false;
		shipPerItem    = tmpShipPerItem;
	}
	if(shipPercent || useShipRules){
		shipPerItem = false;
	}
	for (cc = 0; cc < 10000; cc++){
		cookieVal = getCookieVal(myStoreName + '_item' + cc);
		if(cookieVal != '' && cookieVal != null){
			itemVal  = getCookieVal(myStoreName + '_item' + cc);
			Cart[cc] = new CartItem(true,itemVal[0],itemVal[1],itemVal[2],itemVal[3],itemVal[4],itemVal[5]);
		} else {
			break;
		}
	}
}

function getCookie(key){
	theCookie = '';
	if(document.cookie && document.cookie.indexOf(key) != -1){
		theCookie = document.cookie;
		theCookie = theCookie.substring(theCookie.indexOf(key),theCookie.length);
		if(theCookie.indexOf(';') != -1){
			theCookie = theCookie.substring(0,theCookie.indexOf(';'));
		}
		return theCookie;
	}
}

function getCookieVal(key){
	theCookie = getCookie(key);
	if(theCookie != '' && theCookie != null){
		theVal = theCookie.substring(theCookie.indexOf(key) + key.length + 1,theCookie.length);
		if(theVal.indexOf(delim) != -1){
			theVal = theVal.split(delim);
		}
		return theVal;
	} else {
		return '';
	}
}

function setCookie(key,val,exp,path,site){

	theCookie = key + '=' + val;
	if(exp != '' && exp != null){
		theCookie += ';expires=' + exp.toGMTString();
	}
	if(path != '' && path != null){
		theCookie += ';path=' + path;
	}
	if(site != '' && site != null){
		theCookie += ';domain=' + site;
	}
	currLocation = String(location).toLowerCase();
	if(currLocation.indexOf('https') != -1){
		theCookie += ';secure';
	}
	document.cookie = theCookie;

}


function killCookie(key){
	setCookie(key,'',setExp(0,0,0,0,0,0,-2),cookiePath,unsecureDomain);
}


//---------------------------------------------------------------------||
// FUNCTION:    DeleteCookie                                           ||
// PARAMETERS:  Cookie name                                            ||
// RETURNS:     null                                                   ||
// PURPOSE:     Removes a cookie from users browser.                   ||
// USES:        global variable: myStoreName (set in settings file)    ||
//                               -----------                           ||
// SYNTAX:      deleteCookie(myStoreName);                             ||
//---------------------------------------------------------------------||

function DeleteCookie(NameOfCookie)
{
 // The function simply checks if the cookie is set.  If so, expiredate is set to Jan. 1st 1970
	if(confirm('Are you sure you want\nto delete the entire Cookie?'))
	{
	  Cart = new Array();
	  displayCart();
	  if(getCookie(NameOfCookie))
	   { document.cookie = NameOfCookie + "= ; expires= Thu, 01-Jan-70 00:00:01 GMT"; }
	}


}


//---------------------------------------------------------------------||



function setExp(S,M,H,D,W,Mo,Y){
	exp = new Date();
	sec = 1000;
	min = 60  * sec;
	hr  = 60  * min;
	day = 24  * hr;
	wk  = 7   * day;
	mo  = 30  * day;
	yr  = 365 * day;
	theLen  = exp.getTime();
	theLen += (S * sec) + (M * min) + (H * hr);
	theLen += (D * day) + (W * wk) + (Mo * mo);
	theLen += (Y * yr);
	exp.setTime(theLen);
	return exp;
}

function trim(){
	tmpTxt = this.toString();

	while(tmpTxt.charAt(0) == ' '){
		tmpTxt = tmpTxt.substring(1,tmpTxt.length);
	}
	while(tmpTxt.charAt(tmpTxt.length - 1) == ' '){
		tmpTxt = tmpTxt.substring(0,tmpTxt.length-1);
	}
	return tmpTxt;
}


String.prototype.trim = trim;

if(String(location).indexOf('checkedOut') != -1){
	Cart = new Array();
	cartToCookie();
} else {
	cookieToCart();
}
