function removefntZero(thisname)
{
  thisform=document.getElementById("oQty"+thisname);
  thisvalue=thisform.value;
  if(thisvalue==""){thisform.value=0;}
  var checkStr = thisvalue.toLowerCase();
  if(checkStr.length>1)
  {
    for (i = 0;  i < checkStr.length;  i++)
	{
	  if(checkStr.charAt(i)!="0"){break;}
	}
	thisform.value=checkStr.substring(i, checkStr.length);
  }
}

function FindCookie(cookieId)
{
  var cookieValue="";
  var strCookie=document.cookie;
  var arrCookie=strCookie.split("; ");

  for(var i=0;i<arrCookie.length;i++){
    var arr=arrCookie[i].split("=");
    if(arr[0]==cookieId){
      cookieValue=arr[1];
      break;
    }
  }
  return cookieValue;
}

//Display the items ordered.
function ShowOrderItems(cookieId)
{
  var arr=unescape(FindCookie(cookieId)).split(",");
  var j=0;
  for(var i=0;i<arr.length;i++)
  {
    if(arr[i]!="")
    {
      j++;
    }
  }
  return j;
}

function checkvalue(thisname)
{
  removefntZero(thisname);
  var reg=/^\d+$/;
  if(reg.test(thisvalue)==false){
    alert("Please enter the quantity you like to order here. \n* for nonnegative integers only *");
    window.setTimeout(function(){
      thisform.focus();
      thisform.select();
	}, 0);
  }
}

function chkNumeric(thisname){
  if(isNaN(thisname.value)==true){
    alert("Please enter a numeric here");
    window.setTimeout(function(){
      thisname.focus();
      thisname.select();
    }, 0);
  }
}

function AddItems(thisname)
{
  var oldQty=0;
  var newQty=0;
  var totalQty=0;

  thisform=document.getElementById("oQty"+thisname);
   //validate if it is a numeric.

  if(thisform.value==""){thisform.value=0;}
  var re=/\D/g.test(thisform.value);
  if(re)
  {
    alert("Please enter the quantity you like to order here. \n* for nonnegative integers only *");
    window.setTimeout(function(){
	  thisform.focus();
	  thisform.select();
	}, 0);
    exit;
  }
  newQty=parseInt(thisform.value);
  thisform.value=0;

  //if the qty is 0, exit
  if(newQty==0){
  exit;
  }

  var cookieId="AddItem";

  if(FindCookie(cookieId)=="")
  {
	  document.cookie=cookieId + "=" + escape(thisname + ":" + newQty)+";path=/";
  }
  else
  {
    var arr=unescape(FindCookie(cookieId)).split(",");
    var AddItemStr="";
    var ItemExist=false;   //check if the item is exist in cookies.
    for(var i=0;i<arr.length;i++)
    {
      if(arr[i]!="")
      {
        var arrItem=arr[i].split(":");
        if(arrItem[0]==thisname)
        {
          ItemExist=true;

          oldQty=parseInt(arrItem[1]);
          totalQty=oldQty+newQty;
          AddItemStr+=arrItem[0] + ":" + totalQty + ",";
        }
        else
        {
          AddItemStr+=arr[i]+",";
        }
      }
    }

    if(ItemExist==false)
    {
      AddItemStr+=thisname + ":" + newQty;
    }
    document.cookie=cookieId + "=" + escape(AddItemStr)+";path=/";
  }

  //alert(unescape(FindCookie(cookieId)));

  //update total amount
	var totalAmt=0;
	var txtTotalAmt=FindCookie("TotalAmt");
	if(txtTotalAmt!="" && isNaN(txtTotalAmt)==false){totalAmt=parseFloat(txtTotalAmt);}
	var uPrice=document.getElementById("uPrice"+thisname).value;
	if(isNaN(uPrice)==true){uPrice=0;}
	totalAmt+=newQty*parseFloat(uPrice);
	document.cookie="TotalAmt" + "=" + escape(totalAmt)+";path=/";
	
  document.getElementById("ItemOrdered").innerHTML="(" + ShowOrderItems(cookieId) + ") items<br />$"+ totalAmt.toFixed(2);
  document.getElementById("ItemOrdered1").innerHTML="(" + ShowOrderItems(cookieId) + ")";
}

function logout(){
  var d = new Date();
  document.cookie = "user=;path=/;expires=" + (d).toGMTString();
	document.getElementById("user1").innerHTML="<a href='login.asp' title='Log In'>Login</a> | <a href='login.asp?action=new' title='Sign up as a member'>Register</a>";
}

function getProduct(url)
{
	openHelp('helpWait1','helpWait2','helpWait3');
	
	var xmlHttp;
 
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {

    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser do not support AJAX \n Cannot calculate the freight");
        return false;
      }
    }
  }
	
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
			var myhtml=xmlHttp.responseText;
			if(myhtml=="none"){myhtml="No similar products stock for sale online.";}
			var myid="tab";
			if(url.lastIndexOf("tblDiv")>0){
				url=url.substring(url.lastIndexOf("tblDiv"),url.length);
				url=url.substring(url.indexOf("=")+1,url.length);
				if(url.indexOf("&")>0){url=url.substring(0, url.indexOf("&"));}
				if(url!=""){myid=url;}
			}
			document.getElementById(myid).innerHTML=myhtml;
			frdtbl();
			closeHelp('helpWait1','helpWait2','helpWait3');
  	}
  }
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
