//<!-- ------------------------------------------------------------- -->
//<!-- RKBA!  -  RKBAbang.com  -   RKBA!   -  RKBAbang.com  -  RKBA! -->
//<!-- ------------------------------------------------------------- -->
//<!-- File Name:   cart.js                                          -->
//<!-- Description: RKBAbang.com Shopping Cart                       -->
//<!-- Created By:  USA Internet Connections & Eric Pavao            -->
//<!-- Last Edited: Apr 1, 2003                                      -->
//<!-- Copyright © 2001-2003 All rights reserved.                    -->
//<!-- No part of this file or the code it contains may be copied,   -->
//<!-- redistributed, or sold. Violators will be prosecuted.         -->
//<!-- ------------------------------------------------------------- -->
//<!-- RKBA!  -  RKBAbang.com  -   RKBA!   -  RKBAbang.com  -  RKBA! -->
//<!-- ------------------------------------------------------------- -->
///////////////////////////////////////////////////////////////////////


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 = "";

function CartItem(prodID, desc, price, qty, opt){
	this.prodID = prodID;
	this.desc = desc;
	this.price = price;
	this.qty = qty;
	this.opt = getOption(prodID);
}

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


//EP 10/05/01 ADDED AddItemInterface function for
//            variable pricing.
function AddItemInterface(ItemNo, Desc){

  var gotQty = false;
  var Qty;
  var DAmount = "0";

  Qty = eval("parent." + mainFrameName + ".document.forms[0]." + ItemNo + ".value");
  
  if (ItemNo == "DONATION"){
      DAmount = eval("parent." + mainFrameName + ".document.forms[0]." + ItemNo + ".value");
  }

  if((!Number(Qty) || Qty.indexOf(".") != -1) && (ItemNo !== "DONATION")){
    alert('Please enter a whole\nnumerical value for item quantity.');
  } else {
    gotQty = true;
  }
	
  if(gotQty){
	
    if(ItemNo=="RKBA1"){
      if(Qty<10) {
         parent.cartframe.AddItem(ItemNo, Desc, 1.00);
       } else {
	 if(Qty<50){
          parent.cartframe.AddItem(ItemNo, Desc, 0.75);
	 } else {
	     if(Qty<100){
		 parent.cartframe.AddItem(ItemNo, Desc, 0.50);
	     } else {
		 parent.cartframe.AddItem(ItemNo, Desc, 0.45);
	     }
	 }
       }
    } else {
      if(ItemNo=="RKBA2"){
        if(Qty<10) {
          parent.cartframe.AddItem(ItemNo, Desc, 1.50);
	} else {
	  if(Qty<50){
	    parent.cartframe.AddItem(ItemNo, Desc, 1.00);
	  } else {
	    parent.cartframe.AddItem(ItemNo, Desc, 0.75);
	  }
	}
      } else {
	if(ItemNo=="DONATION"){
	    parent.cartframe.AddItem(ItemNo, Desc, DAmount);
	} else {
	if(Qty<4) {
	  parent.cartframe.AddItem(ItemNo, Desc, 1.50);
	} else {
	  if(Qty<50){
	   parent.cartframe.AddItem(ItemNo, Desc, 1.00);
	  } else {
	    if(Qty<150){
	      parent.cartframe.AddItem(ItemNo, Desc, 0.75);
	    } else {
	      parent.cartframe.AddItem(ItemNo, Desc, 0.55);
	    }
	  }
	}
      }
    }
    }
  }
}

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 DAmount;
    var inputLocation;
    var verb = "has";

    inputLocation = eval("parent." + mainFrameName + ".document.forms[0]." + ItemNo);
    if (ItemNo == "DONATION") {
	Qty = "1";
	DAmount = eval("parent." + mainFrameName + ".document.forms[0]." + ItemNo + ".value");
    } else {
	Qty = eval("parent." + mainFrameName + ".document.forms[0]." + ItemNo + ".value");
	DAmount = "0";
    }

    if((!Number(Qty) || Qty.indexOf(".") != -1) && (ItemNo != "DONATION")){
	alert('Please enter a whole\nnumerical value for item quantity.');
    } else {
	gotQty = true;
    }
    if(gotQty){
		
	if(Cart.length > 0){
	    for(i=0; i < Cart.length; i++){
								
		if(Cart[i].prodID == ItemNo){
		    alreadyExists = true;
					
		    if (ItemNo=="DONATION"){
			
			if(Cart[i].price != Price){
			    ChangeAmount(ItemNo, Qty, Price);
			    madeChange = true;
			} else {
			    alert(Cart[i].price + " DONATION \nalready exists in your cart.");
			}
		    } else {
		    if(Cart[i].qty != Qty){
			//EP 10/05/01 ADDED price to change
			//ChangeQty(ItemNo, Qty);
			ChangeQty(ItemNo, Qty, Price);
			madeChange = true;
		    } else {
			var newOption = getOption(ItemNo);
			if(newOption != "" && newOption != Cart[i].opt){
			    Cart[i].opt = newOption;
			    
			    displayCart();
			    
			} else {
			    alert(Cart[i].qty + " units of item #" + Cart[i].prodID + ", (" + Cart[i].desc + ")\nalready exists in your cart.");
			}
		    }
		    }

		}
	    }
			
		
	    if(!madeChange && !alreadyExists){
		newItem = true;
	    }
	} else {
	    newItem = true;
	}
		
	// if it's a new item, add it to the cart
		
	if(newItem){
		
	    Cart[Cart.length] = new CartItem(ItemNo, Desc, Price, Qty);
	    displayCart();
	}
    }
}


function DeleteItem(itemNo){

	var deletedItem;
	
	for(i=0; i < Cart.length; i++){
		
		if(Cart[i].prodID != itemNo){
			tmpArray[tmpArray.length] = Cart[i];
		} else {
			deletedItem = Cart[i];
		}
	}
	
	// reset the cart array
	
	Cart = new Array();
	
	for(i=0; i < tmpArray.length; i++){
		Cart[i] = tmpArray[i];
	}
		
	tmpArray = new Array();
	
	if(deletedItem){
		displayCart();	
	} else {
		alert("Item not found in cart.");
	}
}

//    ChangeAmount(ItemNo, Qty, Price);
function ChangeAmount(itemNo, newQty, newPrice){
	var OK = false;
	var newItem;
	
	for(i=0; i < Cart.length; i++){
		if(Cart[i].prodID == itemNo){
			Cart[i].price = newPrice;
			OK = true;
			newItem = Cart[i];
		}
	}
	
	if(OK){
		displayCart();
	} else {
		alert("Item not found in cart");
	}
}



//EP 10/05/01 ADDED newPrice 
//function ChangeQty(itemNo, newQty){
function ChangeQty(itemNo, newQty, newPrice){
	var OK = false;
	var newItem;
	
	for(i=0; i < Cart.length; i++){
		if(Cart[i].prodID == itemNo){
			Cart[i].qty = newQty;

//EP 10/05/01 ADDED next line to change price:
			Cart[i].price = newPrice;

			OK = true;
			newItem = Cart[i];
		}
	}
	
	if(OK){
		displayCart();
	} else {
		alert("Item not found in cart");
	}
}

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;
}

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

function displayCart(){
	var displayCart = eval("parent." + mainFrameName);
	displayCart.location = cartPage;
}

function getShopper(orderOption, payWith){
	
    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;
    //EP 10/05/01 uncommented following line to add add2 back into form
    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 {
		if(payWith == "egold") {
		    postAction = orderOption;
		    checkoutLocation.location = COegold;
		} else {
		    if(payWith == "paydirect") {
			postAction = orderOption;
			checkoutLocation.location = COpaydirect;
		    } else {
			if(payWith == "goldmoney") {
			    postAction = orderOption;
			    checkoutLocation.location = COgoldmoney;
			} else {
			    if(payWith == "ebullion") {
				postAction = orderOption;
				checkoutLocation.location = COebullion;
			    } else {
				if(payWith == "paypal") {
				    postAction = orderOption;
				    checkoutLocation.location = COpaypal;
				}
			    }
			}
		    }
		}
	    }
	}
    }
}

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;
 		}

//EP 10/05/01 uncommented following lines until next comment to add add2 back into form
 		if(shipeeArray[4] == "" && AllOk){
 			alert("Please fill out the \"Shipping City\" field");
 			AllOk = false;
 		}
//EP 10/05/01 END of uncommented lines

 		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;
	}
 	
 } 

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];
		}
//EP 10/05/01 uncommented following lines until next comment to add add2 back into form
		if(shopperArray[4] != ""){
			shipForm.document.forms[0].add2.value = shopperArray[4];
		}
//EP 10/05/01 END of uncommented lines
		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];
		}
// JC bug#2
//		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;
 }
