// rh 06/14/2007  Added to check the qty between 1 and 999

// Ok this is a add to cart function
var sep1 = "!";
var sep2 = "^";
var CartArray = 3;

// rh 11/17/05
// added this function to check the validity of qty
// we hava this function in wCartUpdate.js a wps directory
function checknum_v2( contents ) {
	if ( contents != Math.round(contents)) { return -1; }
	if (isNaN(contents) || (contents < 0)){ return -1; }
    if (((contents / contents) != 1) && (contents != 0)) { return -1; }

    // rh 06/14/2007  Added to check the qty between 1 and 999
    //if (contents > 999) { return -1; }
    if (contents <= 0 || contents > 999) { return -1; }

    return Math.round(contents);
}

// rh 11/17/05
// Added flexibility to the arguments
// additional arguments can be passed.
//function addtocart(tmpstr)
function addtocart()
{
	var tmpstr;
      var qty;
      for(ii=0; ii<addtocart.arguments.length; ii++)
      {
        if(ii==0)
        {
           tmpstr = addtocart.arguments[ii];
           qty = 1;
        }
        else if(ii==1)
           qty = addtocart.arguments[ii];
      }

//alert('qty='+qty);
      
	if( checknum_v2(qty) == -1 )
	{
		alert("Error! Only numbers are valid, and they must be less than 1000");
		return;
	}

	var PNIC = true;								// Product Not In Cart;
	var PA = new Array();							// Product Array;

	PA = tmpstr.split(sep1);

	for( var K1 = 0 ; K1 < wCookies[CartArray].length ; K1++ )
   {
   	// Look to see if the product to be added to the cart is already here;
		if(wCookies[CartArray][K1][1] == PA[1])
      {
         // ok Found a product match just need to add one more;
		  	// Add One Product;
                  // rh 11/17/05
			//wCookies[CartArray][K1][PA.length] = 1 + parseInt(wCookies[CartArray][K1][PA.length]);
			//alert("cookie qty="+wCookies[CartArray][K1][PA.length]);
			wCookies[CartArray][K1][PA.length] = qty + parseInt(wCookies[CartArray][K1][PA.length]);

			// rh 11/17/05
			// check if total qty > 999
			if(parseInt(wCookies[CartArray][K1][PA.length]) > 999)
			{
				wCookies[CartArray][K1][PA.length] = parseInt(wCookies[CartArray][K1][PA.length])-qty;
				alert("Error! Total quantities are exceeded 1000 unit in your Cart.");
				return;
			}
			PNIC = false;	// Set Flag to say that the product was here and we added it to cart;
	   }
	}

	if( PNIC == true )
   {
      // Add a new Item to the Cart;
		wCookies[CartArray][K1] = PA;    // Add the product to the Cart array  ft;
		// rh 11/17/05
		//wCookies[CartArray][K1][wCookies[CartArray][K1].length] = 1;	// Add one of that Item to the cart
		wCookies[CartArray][K1][wCookies[CartArray][K1].length] = qty;	// Add one of that Item to the cart
	}

	var tmparr = new Array();

	for( var K2 = 1; K2 < wCookies[CartArray].length; K2++)
   {
 		// alert("K2="+K2+";;wCookies[CartArray][K2]="+wCookies[CartArray][K2]);
 		// alert("1before trimming wCookies[CartArray][K2][2]="+wCookies[CartArray][K2][2]);

      // rh and dw
      // 6/30/2005 if the product description is more than 160 characters, then shorten it.
      // This will minimize the problem of over_text_limit in the cookie.
      // When that happened, the cookie will be blank out and the shopping cart reads empty.
      //
      // rh 03/02/06 Now becomes 100 chars.
		if (wCookies[CartArray][K2][2].length > 100)
			wCookies[CartArray][K2][2] = wCookies[CartArray][K2][2].substring(0,100) + '   .....';

		// alert("1after trimming wCookies[CartArray][K2][2]="+wCookies[CartArray][K2][2]);

		tmparr[K2] = wCookies[CartArray][K2].join(sep1);
	}

	tmpstr = tmparr.join(sep2);
	SetCookie( wCookies[CartArray][0],tmpstr);
	alert("Product Added To Cart");
}