// Shopping Cart JavaScript for TALtech v1.0 by Rob Williams
// 1/13/2003

///////////////// Notes ////////////////////////////////////
//
// Each html page's add to cart button will be a form consisting 
// of a button with the value of the product stock number and a
// hidden or text field containing the quantity
// 
////////////////////////////////////////////////////////////

//Error handler
function handleError() { 
	return true; 
} 

// Add a product to the shopping cart
function AddCart(ProdID,qty){

  //var qty=1;
  //var FormName = obj.name;

  var curQty=0;

    curQty = GetCookie(ProdID);
    if (curQty==null){
	  curQty=0;
    }
    /* var tStr ='';
  
    alertmsg= tStr + '\n\nAdd ' + qty + ' items to ';
    if (curQty>0){
	  alertmsg=alertmsg+ curQty+' items already in ';
    }
    alertmsg=alertmsg+'shopping cart? ';

    Answer = window.confirm(alertmsg); 

    if (Answer==true){ 
      alert( qty + ' items added. '); */
      curQty=curQty-1 +1;
      qty=qty-1 +1;
      qty=qty+ curQty;
      SetCookie(ProdID,qty, exp);
	location.href = "http://www.taltech.net/cgi-bin/showCart.pl";
    //}
}

//Add DLL to cart - a little different as is selected from dropdown box.
//Must alert user when chose All Linar + 1 that should enter 1 2D in comments box...
function AddDLL(name){
	if (name=="DLLAS1"){
		//if DLL + 1 then notify user...
		alert("Please enter your choice of 2D bar code into the 'Comments' box when placing your order. If you forget we will contact you when we process your order.");
	}
	//add item to cart
	AddCart(name,1);
}

//Remove an item from the cart
function RemFromCart(name) { 
	var today = new Date(); 	
	var expired = new Date(today.getTime() - 72 * 60 * 60 * 1000); // less 3 days
	SetCookie(name,"", expired);
	//refresh page
	window.location = window.location;
} 

//Updates the contents of the cart
function UpdateCart(){
	var numElements  = document.forms["form1"].elements.length;
	for (var i = 0; i<numElements; i++) { 
    		var e = document.forms["form1"].elements[i];
    		var etype = e.type;
    		if ((etype.indexOf('text')>-1)){
			//get current qty
			var oldQty = GetCookie(e.name);
			
			var NewQty = e.value;
			if (NewQty == 0){
				RemFromCart(e.name); //If new value is zero, delete from cart
			}
			else{
				AddCart(e.name,NewQty - oldQty); ////subtract oldQty from new Qty and add that value to cart
			}
    		}
  	}
	//refresh page
	//location.href = "http://www.taltech.com/cgi-bin/showCart.cgi";
	//window.location = window.location;
}

//view Cart
function ViewCart() { 
	//Go to checkout
	location.href = "http://www.taltech.net/cgi-bin/showCart.pl";
} 

//In some cases (e.g. Limited license AX) it may be necessary to confirm that customer has selected the correct product...
function VerifyChoice(msg,prodID) {
  var Answer = window.confirm(msg); 

  if (Answer==true){ 
	AddCart(prodID,1);
  }
}

// changes the statusbar text
function changeStatus(msg){
	window.status = msg;
}

function goBack(){
	//return to products page
	location.href = "http://www.taltech.net/products/";
}