// NebuCart - The JavaScript Shopping Cart
// by Nebulus Designs
//
// Copyright 1999/2000 all rights reserved.

// None of this script may be redistributed or sold
// without the authors express consent.
// Violations of copyright will be prosecuted.

// If you would like to use NebuCart,
// email us at nebucart@nebulus.org
// or visit http://www.nebulus.org/NebuCart

// ********************************************
// NebuCart Product/Shopper Engine	      *
// ********************************************
// DO NOT CHANGE ANYTHING BELOW THIS LINE!    *
// ********************************************

// cart engine variables
var Cart = new Array();
var tmpArray = new Array();
var chargeTax = false;
var shipDesc = "";
var cardDesc = "";
var cardName = "";
var cardNo = "";
var cardMonth = "";
var cardYear = "";
var postAction = "";
//var i;

// this is the cart object - edit to taste for
// required properties - but you'll have to change them
// in all the other functions!
function CartItem(prodID, desc, price, qty, opt){
	this.prodID = prodID;
	this.desc = desc;
	this.price = price;
	this.qty = qty;
	this.opt = getOption(prodID);
}

// shopper variables
var shopperArray = new Array();
var shipeeArray = new Array();
var shipForm = eval("parent." + mainFrameName);

// this adds items to the cart
function AddItem(ItemNo, Desc, Price){

	// operation booleans - required to ensure
	// no redundancy to the cart
	
	var madeChange = false;
	var alreadyExists = false;
	var newItem = false;
	var gotQty = false;

	var Qty;
	var inputLocation;
	var verb = "has";
	
	// get the quantity from the form - this could be passed
	// into the AddItem function, but we're doing it via form here.
	
	inputLocation = eval("parent." + mainFrameName + ".document.forms[0]." + ItemNo);
	Qty = eval("parent." + mainFrameName + ".document.forms[0]." + ItemNo + ".value");
	
	// check to see if the quantity is a number - if so, got Qty = true
	
	if(!Number(Qty) || Qty.indexOf(".") != -1){
		alert('Please enter a whole\nnumerical value for item quantity.');
	} else {
		gotQty = true;
	}
	
	if(gotQty){
		
		// if the cart has items...
		
		if(Cart.length > 0){
			for(i=0; i < Cart.length; i++){
				
				// see if the added item exists in the cart
				
				if(Cart[i].prodID == ItemNo){
					alreadyExists = true;
				
					// check to see if the quantity is different
					// if so, change the quantity
					
					if(Cart[i].qty != Qty){
						ChangeQty(ItemNo, Qty);
						madeChange = true;
					} else {
						// we didn't change quantity, so maybe we changed options
						
						var newOption = getOption(ItemNo);
						if(newOption != "" && newOption != Cart[i].opt){
							Cart[i].opt = newOption;
							
							// if we changed options, then re-display the cart							
							displayCart();
							
						} else {
							alert("Item " + Cart[i].prodID + ", (" + Cart[i].desc + ")\nalready exists in your cart.");
						}
					}
				}
			}
			
			// if we didn't change quantity and the item doesn't
			// exist in the cart, then it's a new item
			
			if(!madeChange && !alreadyExists){
				newItem = true;
			}
		} else {
			newItem = true;
		}
		
		// if it's a new item, add it to the cart
		
		if(newItem){
			// add the item to the cart, then display the cart page
			Cart[Cart.length] = new CartItem(ItemNo, Desc, Price, Qty);
			displayCart();
		}
	}
}

// delete items for the cart
function DeleteItem(itemNo){

	// temporary object to hold the delted item
	var deletedItem;
	
	for(i=0; i < Cart.length; i++){
		
		// load up the temp aray with non deleted items
		
		if(Cart[i].prodID != itemNo){
			tmpArray[tmpArray.length] = Cart[i];
		} else {
			deletedItem = Cart[i];
		}
	}
	
	// reset the cart array
	
	Cart = new Array();
	
	// refill the  empty cart with valid items
	
	for(i=0; i < tmpArray.length; i++){
		Cart[i] = tmpArray[i];
	}
	
	// reset the temp array
	
	tmpArray = new Array();
	
	// alert the user that the item's been deleted
	// or isn't in the cart.
	
	if(deletedItem){
		displayCart();	
	} else {
		alert("Item not found in cart.");
	}
}

// change the quantity of the item in the cart
function ChangeQty(itemNo, newQty){
	var OK = false;
	var newItem;
	
	for(i=0; i < Cart.length; i++){
		if(Cart[i].prodID == itemNo){
		
			// when we find the same item number, change
			// to the new quantity.
		
			Cart[i].qty = newQty;
			OK = true;
			newItem = Cart[i];
		}
	}
	
	// alert the user that things did or didn't go so well.
	
	if(OK){
		displayCart();
	} else {
		alert("Item not found in cart");
	}
}

// get product options
function getOption(itemID){
	optionVal = "";
	objVal = eval("parent." + mainFrameName + ".document.forms[0]." + itemID + "_opt");
	if(typeof objVal != "undefined"){
		switch (objVal.type){
			case "select-one":
				for(j = 0; j < objVal.length; j ++){
					if(objVal.options[j].selected){
						if(objVal.options[j].value == ""){
							optionVal = objVal.options[j].text;
						} else {
							optionVal =  objVal.options[j].value;
						}
					}	
				}
			break
			case "select-multiple":
				for(j = 0; j < objVal.length; j ++){
					if(objVal.options[j].selected){
						if(objVal.options[j].value == ""){
							currVal = objVal.options[j].text;
						} else {
							currVal = objVal.options[j].value;
						}
						if(optionVal == ""){
							optionVal = currVal;
						} else {
							optionVal = optionVal + ", " + currVal;
						}
					}
				}
			break
		}
	}
	return optionVal;
}

// reset the entire cart.
function DeleteCart(){
	if(confirm("Are you sure you want\nto clear the entire Shopping Cart?")){
		Cart = new Array();
		displayCart();
	}
}

// once you add/change/delete items, display the cart page
function displayCart(){
	// display the cart...
	var displayCart = eval("parent." + mainFrameName);
	displayCart.location = cartPage;
}

// ********************************************
// NebuCart Shopper Engine		              *
// ********************************************

// here we get the shoppers info.
function getShopper(orderOption){
	
	shopperArray[0] = shipForm.document.forms[0].fname.value;
	shopperArray[1] = shipForm.document.forms[0].lname.value;
	shopperArray[2] = shipForm.document.forms[0].email.value;
	shopperArray[3] = shipForm.document.forms[0].add1.value;
	shopperArray[4] = shipForm.document.forms[0].add2.value;
	shopperArray[5] = shipForm.document.forms[0].city.value;
	shopperArray[6] = shipForm.document.forms[0].state.value;
	shopperArray[7] = shipForm.document.forms[0].zip.value;
	shopperArray[8] = shipForm.document.forms[0].country.value;
	shopperArray[9] = shipForm.document.forms[0].phone.value;
	
	if(shipForm.document.forms[0].diffShip.checked){
		shipeeArray[0] = shipForm.document.forms[0].Sfname.value;
		shipeeArray[1] = shipForm.document.forms[0].Slname.value;
		shipeeArray[2] = shipForm.document.forms[0].Sadd1.value;
		shipeeArray[3] = shipForm.document.forms[0].Sadd2.value;
		shipeeArray[4] = shipForm.document.forms[0].Scity.value;
		shipeeArray[5] = shipForm.document.forms[0].Sstate.value;
		shipeeArray[6] = shipForm.document.forms[0].Szip.value;
		shipeeArray[7] = shipForm.document.forms[0].Scountry.value;
	}
	
	if(Validate()){
		if (shopperArray[6].toLowerCase() == myState1 || shopperArray[6].toLowerCase() == myState2){
			chargeTax = true;
		} else {
			chargeTax = false;
		}
		if(useShipOptions){
			getShipAmt();
		}		
		var checkoutLocation = eval("parent." + mainFrameName);
		if(orderOption == ""){
			checkoutLocation.location = COstep2;
		} else {
			if(orderOption.indexOf("https:") != -1){
				postAction = orderOption;
				if(securePath.charAt(securePath.length - 1) != "/"){
					securePath = securePath + "/";
				}
				checkoutLocation.location = securePath + COsecure;
			} else {
				postAction = orderOption;
				checkoutLocation.location = COform;
			}
		}
	}
}

// this validates the shopper's information...
function Validate(){
	
	var AllOk = true;
	var tmpPhone = "";
	var phoneChar = new Array("-","(",")"," ");
	var goodChar = true;
	
	if(shopperArray[0] == ""){
		alert("Please fill out the \"First Name\" field");
		AllOk = false;
	}
	if(shopperArray[1] == "" && AllOk){
		alert("Please fill out the \"Last Name\" field");
		AllOk = false;
	}
	if(AllOk){
		if(shopperArray[2] == ""){
			alert("Please fill out the \"Email\" field");
			AllOk = false;
		} else {
			if(shopperArray[2].indexOf("@") == -1){
				alert("Please enter a valid Email address.");
				AllOk = false;
			}
		}
	}
	if(shopperArray[3] == "" && AllOk){
		alert("Please fill out the \"Address\" field");
		AllOk = false;
	}
	if(shopperArray[5] == "" && AllOk){
		alert("Please fill out the \"City\" field");
		AllOk = false;
	}
	if(shopperArray[6] == "" && AllOk){
		alert("Please fill out the \"State/Provine\" field");
		AllOk = false;
	}
	if(shopperArray[7] == "" && AllOk){
		alert("Please fill out the \"Postal Code\" field");
		AllOk = false;
	}
	if(shopperArray[8] == "" && AllOk){
		alert("Please fill out the \"Country\" field");
		AllOk = false;
	}
	if(shopperArray[9] == "" && AllOk){
		if(shopperArray[9] == ""){
			alert("Please fill out the \"Phone Number\" field");
			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");
				allOk = false;
			}
		}
	}	
	if(shipForm.document.forms[0].diffShip.checked){
		if(shipeeArray[0] == "" && AllOk){
			alert("Please fill out the \"Shipping First Name\" field");
			AllOk = false;
		}
		if(shipeeArray[1] == "" && AllOk){
			alert("Please fill out the \"Shipping Last Name\" field");
			AllOk = false;
		}
		if(shipeeArray[2] == "" && AllOk){
			alert("Please fill out the \"Shipping Address\" field");
			AllOk = false;
		}
		if(shipeeArray[4] == "" && AllOk){
			alert("Please fill out the \"Shipping City\" field");
			AllOk = false;
		}
		if(shipeeArray[5] == "" && AllOk){
			alert("Please fill out the \"Shipping State/Province\" field");
			AllOk = false;
		}
		if(shipeeArray[6] == "" && AllOk){
			alert("Please fill out the \"Shipping Postal Code\" field");
			AllOk = false;
		}
		if(shipeeArray[7] == "" && AllOk){
			alert("Please fill out the \"Shipping Country\" field");
			AllOk = false;
		}
	}
	if(AllOk){
		return true;
	} else {
		return false;
	}
	
}

// this function fills out the shipping info if the
// shopper has already filled it out once.
function fillShopperForm(){
	if(shopperArray.length > 0){
		if(shopperArray[0] != ""){
			shipForm.document.forms[0].fname.value = shopperArray[0];
		}
		if(shopperArray[1] != ""){
			shipForm.document.forms[0].lname.value = shopperArray[1];
		}
		if(shopperArray[2] != ""){
			shipForm.document.forms[0].email.value = shopperArray[2];
		}
		if(shopperArray[3] != ""){
			shipForm.document.forms[0].add1.value = shopperArray[3];
		}
		if(shopperArray[4] != ""){
			shipForm.document.forms[0].add2.value = shopperArray[4];
		}
		if(shopperArray[5] != ""){
			shipForm.document.forms[0].city.value = shopperArray[5];
		}
		if(shopperArray[6] != ""){
			shipForm.document.forms[0].state.value = shopperArray[6];
		}
		if(shopperArray[7] != ""){
			shipForm.document.forms[0].zip.value = shopperArray[7];
		}
		if(shopperArray[8] != ""){
			shipForm.document.forms[0].country.value = shopperArray[8];
		}
		if(shopperArray[9] != ""){
			shipForm.document.forms[0].phone.value = shopperArray[9];
		}
	}
	if(shipeeArray.length > 0){
		if(shipeeArray[0] != ""){
			shipForm.document.forms[0].Sfname.value = shipeeArray[0];
		}
		if(shipeeArray[1] != ""){
			shipForm.document.forms[0].Slname.value = shipeeArray[1];
		}
		if(shipeeArray[2] != ""){
			shipForm.document.forms[0].Sadd1.value = shipeeArray[2];
		}
		if(shipeeArray[3] != ""){
			shipForm.document.forms[0].Sadd2.value = shipeeArray[3];
		}
		if(shipeeArray[4] != ""){
			shipForm.document.forms[0].Scity.value = shipeeArray[4];
		}
		if(shipeeArray[5] != ""){
			shipForm.document.forms[0].Sstate.value = shipeeArray[5];
		}
		if(shipeeArray[6] != ""){
			shipForm.document.forms[0].Szip.value = shipeeArray[6];
		}
		if(shipeeArray[7] != ""){
			shipForm.document.forms[0].Scountry.value = shipeeArray[7];
		}
	}	
}

function getShipAmt(){
	var optionLocation = eval("parent." + mainFrameName);
	var i = getSelectedRadio(optionLocation.document.forms[0].ship_option);
	shipAmt = shipOptions[i].substring(shipOptions[i].indexOf("|") + 1, shipOptions[i].length);
	shipDesc = shipOptions[i].substring(0,shipOptions[i].indexOf("|"));
}

function getSelectedRadio(radioGroup){
	for(i = 0; i < radioGroup.length; i ++){
		if(radioGroup[i].checked){
			return i;
		}
	}
	return 0;
}