// JavaScript Document
var g_CartSubTot=0;
var WeekDay=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var Months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var dte=new Date();
var wday=dte.getDay();
var month=dte.getMonth();
var day=dte.getDate();
var year=dte.getFullYear();
var g_CartDate=WeekDay[wday]+' '+Months[month]+' '+day+', '+year;
function SetProv()
{
  var items=GetCookie('ITEMS');
  if(items!=null)
  {
    var prov=GetCookie("PROV");
    if(prov==null) prov=6;
    document.FRMSL.Prov.selectedIndex=parseInt(prov,10);
  }
}
function UpdateProv(obj){SetCookie("PROV",obj.selectedIndex);}

function ShopListPurchase()
{
	ShopListCheckQty();
	DisplayNumCartItems();
	document.location="https://www.mysterybyte.com/checkout/";
}

function PrintTotals(subTot)
{
  var Total;
  var SubTotal;
  var Hst;
  var tax = 0.05;
  var taxamt;
  var taxtype="GST";
  var prov=GetCookie('PROV');
  if(prov==null){prov=6;}
  prov=parseInt(prov,10);
  if(prov==0){tax=0.0;taxtype="";}
  else if(prov==2||prov==8||prov==5||prov==4){tax=0.13;taxtype="HST";}
  else if(prov==6){tax=0.15;taxtype="HST";}
  else{tax=0.05;taxtype="GST";}
  SubTotal=parseFloat(subTot);
  taxamt=SubTotal*tax;
  Hst=RoundOff2(taxamt);
  SubTotal=RoundOff2(SubTotal);
  Total=RoundOff2(parseFloat(SubTotal)+taxamt);
  document.write('<div id="totals">\n');
  document.write('<span class="totals-title">SubTotal</span><span class="totals-wrap">'+SubTotal+'</span><br />\n');
  if(taxtype!="")
  {
    document.write('<span class="totals-title">'+taxtype+'</span><span class="totals-wrap">'+Hst+'</span><br />\n');
  }
  document.write('<span class="totals-title">Total</span><span class="totals-wrap">$'+Total+'</span><br />\n');
  document.write('</div>\n');
}

function OLDCreateRow()
{
  var i,qty,price,partIdx,items;
  var sItems=GetCookie('ITEMS');
  var sQty=GetCookie('QTY');
  var subTotal=0;
  items=sItems.split('/');
  qty=sQty.split('/');
  bColorFlag=2;
  for(i=0;i<items.length;i++)
  {
  	partIdx=g_PartIdx[BSearch(items[i],g_PartIdx,g_PartIdxFldCnt)+fld_PartIdx_Idx];
  	if(partIdx>=0)
    	{
    	  if(bColorFlag == 1) bColorFlag = 2;
  		  else bColorFlag = 1;
    		price=g_Part[partIdx+fld_Part_Price];
        document.writeln('<ul class="item-'+bColorFlag+'"><li class="remove"><input type="checkbox" name="ckbox" value="'+items[i]+'"></li><li class="quantity"><input type="text" name="txtQty" value="'+qty[i]+'" size="2" maxlength="2"></li><li class="ipx">'+items[i]+'</li><li class="description"><a href="/parts/'+items[i]+'/">'+g_Part[partIdx+fld_Part_Desc]+'</a></li><li class="price">$'+price+'</li><li class="total">$'+RoundOff2(qty[i]*price)+'</li></ul>');
        subTotal+=(parseFloat(price)*qty[i]);
      }
  }
  return(subTotal);
}

function CreateRow()
{
  var i,qty,price,partIdx,items,subtract,discount;
  var sItems=GetCookie('ITEMS');
  var sQty=GetCookie('QTY');
  var sCoupons=GetCookie('COUPONS');
  var weight=0;
  var subTotal=0;
  items=sItems.split('/');
  qty=sQty.split('/');
  if(sCoupons != null) {
    coupons=sCoupons.split('/');
    bColorFlag=2;
    for(i=0;i<items.length;i++)
    {
    	partIdx=g_PartIdx[BSearch(items[i],g_PartIdx,g_PartIdxFldCnt)+fld_PartIdx_Idx];
    	if(partIdx>=0)
      	{
      	  if(bColorFlag == 1) bColorFlag = 2;
    		  else bColorFlag = 1;
      		price=g_Part[partIdx+fld_Part_Price];
      		var row = '<ul class="item-'+bColorFlag+'"><li class="remove"><input type="checkbox" name="ckbox" value="'+items[i]+'"></li><li class="quantity"><input type="text" name="txtQty" value="'+qty[i]+'" size="2" maxlength="2"></li><li class="ipx">'+items[i]+'</li><li class="description"><a href="/part/'+items[i]+'/">'+g_Part[partIdx+fld_Part_Desc]+'</a></li><li class="price">$'+RoundOff2(price)+'</li><li class="total">$'+RoundOff2(qty[i]*price)+'</li></ul>';
          for(q=0;q<coupons.length;q++)
          {
            subtract = coupons[q].split('|');
            discount = parseFloat(subtract[1]);
            if (coupons[q].indexOf(items[i])!=-1)
            { 
              price = g_Part[partIdx+fld_Part_Price] - discount;
              row = '<ul class="item-'+bColorFlag+'"><li class="remove"><input type="checkbox" name="ckbox" value="'+items[i]+'"></li><li class="quantity"><input type="text" name="txtQty" value="'+qty[i]+'" size="2" maxlength="2"></li><li class="ipx">'+items[i]+'</li><li class="description"><b>Save $'+discount+'</b> <a href="/part/'+items[i]+'/">'+g_Part[partIdx+fld_Part_Desc]+'</a> (Reg. Price: $'+RoundOff2(price+discount)+')</li><li class="price">$'+RoundOff2(price)+'</li><li class="total">$'+RoundOff2(qty[i]*price)+'</li></ul>';
              break;
            }
          }
          document.writeln(row);
          subTotal+=(parseFloat(price)*qty[i]);
          weight += g_Part[partIdx+fld_Part_Weight]*qty[i];
        }
    }
  } else {
    bColorFlag=2;
    for(i=0;i<items.length;i++)
    {
    	partIdx=g_PartIdx[BSearch(items[i],g_PartIdx,g_PartIdxFldCnt)+fld_PartIdx_Idx];
    	if(partIdx>=0)
      	{
      	  if(bColorFlag == 1) bColorFlag = 2;
    		  else bColorFlag = 1;
      		price=g_Part[partIdx+fld_Part_Price];
      		var row = '<ul class="item-'+bColorFlag+'"><li class="remove"><input type="checkbox" name="ckbox" value="'+items[i]+'"></li><li class="quantity"><input type="text" name="txtQty" value="'+qty[i]+'" size="2" maxlength="2"></li><li class="ipx">'+items[i]+'</li><li class="description"><a href="/part/'+items[i]+'/">'+g_Part[partIdx+fld_Part_Desc]+'</a></li><li class="price">$'+RoundOff2(price)+'</li><li class="total">$'+RoundOff2(qty[i]*price)+'</li></ul>';
          document.writeln(row);
          subTotal+=(parseFloat(price)*qty[i]);
          weight += g_Part[partIdx+fld_Part_Weight]*qty[i];
        }
    }
  }
  SetTempCookie('weight',weight);
  return(subTotal);
}

function DisplayShopList()
{
  var items=GetCookie('ITEMS');
  if(items==null)
  {
    document.writeln('<div align="center">\n<h1>Your Shopping List Is Empty</h1>\n');
    document.writeln('<a href="/" onMouseOver="self.status=\'Continue Shopping\';return true"><B>Click Here To Continue Shopping</B></a></div>');
  }
  else
  {
    document.writeln('<ul class="title"><li class="remove">Del</li><li class="quantity">Qty</li><li class="ipx">Part #</li><li class="description">Description</li><li class="price">Price</li><li class="total">Ext. Price</li></ul>');
    g_CartSubTot=0;
    if(items!=null)
    {
      g_CartSubTot+=CreateRow();
    }
    document.writeln('<br style="clear: both;">');
    document.writeln('<div id="taxes"><b>All Prices are listed in Canadian Dollars.</b><br />\n');
    document.writeln('Show Taxes For:&nbsp;&nbsp;<SELECT onChange="UpdateProv(this)" name="Prov" style="font-size:10pt"><OPTION value="NoTax">- - -</option><OPTION value="AB">Alberta</option><OPTION value="BC">British Columbia</option><OPTION value="MB">Manitoba</option><OPTION value="NB">New Brunswick</option><OPTION value="NF">Newfoundland</option><OPTION value="NS">Nova Scotia</option><OPTION value="NT">Northwest Territory</option><OPTION value="ON">Ontario</option><OPTION value="PE">Prince Edward Island</option><OPTION value="QC">Quebec</option><OPTION value="SK">Saskatchewan</option><OPTION value="YT">Yukon</option></SELECT></div>');
    SetTempCookie('subTotal',g_CartSubTot);
    PrintTotals(g_CartSubTot);
  }
}

function GoToQuote()
{
  window.location = "/quote.php";
}

function DisplayButtons()
{
  var items=GetCookie('ITEMS');
  if(items!=null)
  {
    document.writeln('<TABLE cellpadding=0 cellspacing=8 align=center><TR><FORM><TD><input type=button name="FD" value="Continue\nShopping" style="font-size:10pt;cursor:hand;width:100px" onClick="javascript:document.location=\'/\'"></TD>');
    document.writeln('<TD><input type=button name="FD" value="Update\nQuantities" style="font-size:10pt;cursor:hand;width:100px" onClick="ShopListUpdate()"></TD>');
    document.writeln('<TD><input type=button name="FD" value="Empty\nShopping Cart" style="font-size:10pt;cursor:hand;width:100px" onClick="ShopListEmpty()"></TD>');
    document.writeln('<TD><input type=button name="FD" value="Print\nQuote" style="font-size:10pt;cursor:hand;width:100px" onClick="GoToQuote()"></TD>');
    document.writeln('<TD><input type=submit name="FD" value="Purchase\nItems" style="font-size:10pt;cursor:hand;width:100px"></TD></TR></FORM></TABLE>');
  }
}
function ShopListEmpty()
{
	DeleteCookie('QTY');
	DeleteCookie('ITEMS');
	DeleteCookie('COUPONS');
	document.location='/cart/';
	DisplayNumCartItems();
}

