
//*****************************
// main javascript functions
//*****************************

var sessionID = window.name;

position = sessionID.indexOf('/');

if (position == -1)
{
	var sessionID = ((Math.random()*1000000000000000000)+1)  + '/';	// generate unique ID
	window.name = sessionID;
}


//var sessionCheckout = '<form name=checkout method=get action="https://supplyinfusion.com/main.cgi"><input type=hidden name="ID" value="' + sessionID + '"><input type=hidden name=displayType value="C"></form>';
var sessionCheckout = '<form name=checkout method=get action="main.cgi"><input type=hidden name="ID" value="' + sessionID + '"><input type=hidden name=displayType value="C"></form>';


function submitCheckout(rootDir)
{
	url = rootDir + 'main/' + sessionID + 'Checkout.html';
	location.href=url;
}


function openpopup(scriptName, number, w, h)
{

	x=(screen.availWidth-w)/2;
	y=(screen.availHeight-h)/2;

//	window.name='fixmeup';

	win2 = window.open(scriptName,number,'height='+h+',width='+w+',screenX='+x+',left='+x+',screenY='+y+',top='+y+',location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,directories=no');
	win2.focus();

}


function addToCart(script,SKU,QTY,KW, price)
{
	ID = sessionID;

	var total                 =	0;
	var grand                 =	0;
	var grand2                =	0;
	var discount              =	0;
	var total_discount        = 0;
	var special_discount      = 0;
	var volume_discount1_amount = 0;
	var volume_discount2_amount = 0;
	var volume_discount1      = 0;
	var volume_discount2      = 0;
	var case_size             = 0;
	var case_discount         = 0;
  formName                  = SKU;
	special_discount		  		=parseFloat(document[formName].special_discount.value);
	volume_discount1					=parseFloat(document[formName].volume_discount1.value);
	volume_discount2					=parseFloat(document[formName].volume_discount2.value);
	case_discount							=parseFloat(document[formName].case_discount.value);
	volume_discount1_amount		=parseInt(document[formName].volume_discount1_amount.value);
	volume_discount2_amount		=parseInt(document[formName].volume_discount2_amount.value);
	case_size									=parseInt(document[formName].case_size.value);


  save_subTot = document[formName].sub_tot.value;

	if (QTY > 0)
	{

		total_discount = total_discount + (special_discount / 100);
		total_discount_display = special_discount;

		if (QTY >= volume_discount2_amount)
		{
//			total_discount = total_discount + (volume_discount2 / 100);
//			total_discount_display += volume_discount2;
		}
		else if (QTY >= volume_discount1_amount)
		{
//			total_discount = total_discount + (volume_discount1 / 100);
//			total_discount_display +=  volume_discount1;
		}

    discount                          = (total_discount * 100);
		total_discount 		                = (1-total_discount);
		total 						                =  (QTY * price);
		grand 					                	= (total * total_discount);
 		grand2                            = grand.toFixed(2);

  	document[formName].grand.value    = '$' + grand2;
  	document[formName].sub_tot.value  = grand2;
  	document[formName].discount.value = total_discount_display ;

	}
  else
  {
  	document[formName].grand.value= '$' + 0;
  	document[formName].sub_tot.value  = 0;
  	document[formName].discount.value  = '0';
  }


  // Now actually update the database

  sub_total = grand2;

  //  fix bug where firfox submitted 0 does not result ina  deleted item

  if (QTY == 0)
  {
    QTY = -1;
  }

	url = script + '/' + ID + SKU + '/' + QTY + '/' + KW + '/' + price + '/' + sub_total + '/' + discount + '/';
//	alert(url);
//  document.write(url);

  if (window.XMLHttpRequest)	//     // branch for native XMLHttpRequest object
  {
      req = new XMLHttpRequest();
      req.onreadystatechange = processCartResponse;
      req.open("POST", url, true);
      req.send(null);

  }
  else if (window.ActiveXObject)	// branch for IE/Windows ActiveX version
  {
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req)
      {
          req.onreadystatechange = processCartResponse;
          req.open("POST", url, true);
          req.send();
      }
  }

    var element_id      = 'notify_' + SKU;
    showIt(element_id);




}


function updateCart(script,ID,SKU,QTY,KW, price)
{

	var total                 =	0;
	var grand                 =	0;
	var grand2                =	0;
	var discount              =	0;
	var total_discount        = 0;
	var special_discount      = 0;
	var volume_discount1_amount = 0;
	var volume_discount2_amount = 0;
	var volume_discount1      = 0;
	var volume_discount2      = 0;
	var case_size             = 0;
	var case_discount         = 0;
  snh                       = 0;
  formName                  = SKU;

	special_discount		  		=parseFloat(document[formName].special_discount.value);
	volume_discount1					=parseFloat(document[formName].volume_discount1.value);
	volume_discount2					=parseFloat(document[formName].volume_discount2.value);
	case_discount							=parseFloat(document[formName].case_discount.value);
	volume_discount1_amount		=parseInt(document[formName].volume_discount1_amount.value);
	volume_discount2_amount		=parseInt(document[formName].volume_discount2_amount.value);
	case_size									=parseInt(document[formName].case_size.value);

	line_qty							=document[formName].line_qty.value
	line_price						=document[formName].line_price.value;
	line_weight					  =document[formName].line_weight.value;
	line_discount				  =document[formName].line_discount.value;
	volume_discount			 	=document[formName].total_discount.value;
	line_total						=parseFloat(document[formName].line_total.value);
	total_weight					=document.total.total_weight.value;
	volume_discount				=document.total.total_discount.value;

	if (QTY > 0)
	{

		total_discount = total_discount + (special_discount / 100);
		total_discount_display = special_discount;

    discount                          = (total_discount * 100);
		total_discount 		                = (1-total_discount);
		gross_line_total 						      =  (QTY * line_price);
		net_line_total 					          = (gross_line_total * total_discount);
    save_new_line_total               = net_line_total;
 		net_line_total                    = net_line_total.toFixed(2);

  	document[formName].line_display_total.value    = '$' + net_line_total;
  	document[formName].line_display_discount.value = total_discount_display + '%';


    // get the original value of freight so that it can be subtracted from the total and then have the new freight total added to it

    oldWeight = (line_qty * line_weight);
    newWeight = (QTY * line_weight);

    newTotalWeight = (total_weight - oldWeight);
    newTotalWeight = (newTotalWeight + newWeight);

  	document.total.total_weight.value = newTotalWeight;

    if (newTotalWeight < 5)
    {
      snh = 20;
    }
    else if( newTotalWeight < 15)
    {
      snh = 25;
    }
    else if( newTotalWeight < 50)
    {
      snh = (10 + (newTotalWeight * 1.2));
    }
    else if( newTotalWeight < 100)
    {
      snh = (10 + (newTotalWeight * .9));
    }
    else
    {
      snh = (10 + (newTotalWeight * .7));
    }

	}
  else
  {
    save_new_line_total = 0;
  	document[formName].line_display_total.value= '$' + 0;
  	document[formName].line_display_discount.value  = '0';
  }


  /// get original sub-total

  subTotal = parseFloat(document.total.total_subTotal.value);

  // subtract current sub total from grand total for this line item
  newSubTotal = (subTotal - line_total);

  // add the newley calculated line item sub total to the new grand total
  newSubTotal = (newSubTotal + save_new_line_total);

  // add the newley calculated line item sub total to the new grand total
  display_grand_total = (newSubTotal + snh);



  // set the hidden fields up to reflect the new values when the ofrm is submitted

  document.total.total_subTotal.value       = newSubTotal;
  document.total.total_grandTotal.value     = display_grand_total;
  document.checkout.real_grand_total.value  = display_grand_total;

  // Redisplay it all

  document.total.display_subTotal.value = 'Sub-total: $' + newSubTotal.toFixed(2);
  document.total.display_freight.value = '+ Freight: $' + snh.toFixed(2);
  document.total.display_grandTotal.value = 'Grand Total: $' + display_grand_total.toFixed(2);
  document.checkout.display_grand_total.value = 'Grand Total: $' + display_grand_total.toFixed(2);


  // Now actually update the database

  sub_total = grand2;


  //  fix bug where firfox submitted 0 does not result ina  deleted item

  if (QTY == 0)
  {
    QTY = -1;
  }

	url = script + '/' + ID + '/' + SKU + '/' + QTY + '/' + KW + '/' + price + '/' + save_new_line_total + '/' + line_discount + '/';

  if (window.XMLHttpRequest)	//     // branch for native XMLHttpRequest object
  {
      req = new XMLHttpRequest();
      req.onreadystatechange = processCartResponse;
      req.open("POST", url, true);
      req.send(null);

  }
  else if (window.ActiveXObject)	// branch for IE/Windows ActiveX version
  {
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req)
      {
          req.onreadystatechange = processCartResponse;
          req.open("POST", url, true);
          req.send();
      }
  }

}




function updateCustomer(url, formName)
{
//document.write(url);
  if (window.XMLHttpRequest)	//     // branch for native XMLHttpRequest object
  {
      req = new XMLHttpRequest();
      req.onreadystatechange = processCartResponse;
      req.open("POST", url, true);
      req.send(null);

  }
  else if (window.ActiveXObject)	// branch for IE/Windows ActiveX version
  {
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req)
      {
          req.onreadystatechange = processCartResponse;
          req.open("POST", url, true);
          req.send();
      }
  }
 	document[formName].shipbut.value='Archived';
}



function processCartResponse()
{

  if (req.readyState == 4)
  {
      if (req.status != 200)	// only if error
      {
  			alert('Error: could not add this product to the cart...please try again');
//  			alert(req.responseText);
      }
  }
}



function searchSite()
{
	searchString = document.search.searchString.value;

	if (searchString != '')
	{
		url =  'main/' + searchString + '/' + 'search.html';
		document.location.href=url;
	}
	else
	{
		alert('You have not entered anything to search for');
		document.search.searchString.focus();
	}
}



function showIt(ID)
{
//	document.getElementById(ID).style.top=(50+document.body.scrollTop);
//	window.scrollTo(100,document.body.scrollTop)
	state=document.getElementById(ID).style.visibility="visible"
}


function hideIt(ID)
{
	state=document.getElementById(ID).style.visibility="hidden"
}



function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
