var Cart = new Array();
function checkcookie(){
	if(document.cookie.indexOf("VCBCart=")<0){
		document.cookie="VCBCart=";
	}
	if(document.cookie.indexOf("[")<0 && document.cookie.indexOf("null")>=0){
		document.cookie="VCBCart=";
	}
	return 0;
}
function CartItem(pid, type, price, qty){
	this.pid=pid;
	this.type=type;
	this.price=price;
	this.qty=qty;
}
function alterError(value) {
	if (value<=0.99) {
		newPounds = '0';
	} 
	else{
		newPounds = parseInt(value);
	}
	newPence = parseInt((value+.0008 - newPounds)* 100);
	if (eval(newPence) <= 9) newPence='0'+newPence;
	newString = newPounds + '.' + newPence;
	return (newString);
}
function convertIt(_v)
{
  var _dollars=parseInt(_v);
  var _cents=parseInt((_v-_dollars)*100);
  var _negative=_dollars<0;
  if(_negative){_dollars=-_dollars;_cents=-_cents;}
  while(_cents.toString().length<2)_cents="0"+_cents;
  var _dA=_dollars.toString().split("");
  var _d="";
  for(var i=_dA.length-1;i>=0;i--)
  {
    var _comma="";
	if((_dA.length-i)%3==0 && i!=0)_comma=",";
    _d=_comma+_dA[i]+_d;
  }
  var _neg_sign=_negative?"-":"";
  var _result="$"+_neg_sign+_d+"."+_cents;
  return _result;
}
function buyItem(newItem,newPrice,newQuantity,newType){
	checkcookie();
	if(document.cookie.indexOf(newItem+","+newType)==-1){
		if (newPrice == "" || newPrice == "0" || newPrice == 0){
			alert("This item is not ready for sale.\nCheck back soon.")
		}
		else{
			if (newQuantity <= 0 || isNaN(newQuantity)){
				rc = alert('The quantity for this item must be a number greater than 0');
				return false;
			}
			index = document.cookie.indexOf("VCBCart");
			countbegin = (document.cookie.indexOf("=", index) + 1);
			countend = document.cookie.indexOf(";", index);
	        	if (countend == -1) {
        	    		countend = document.cookie.length;
        		}
			document.cookie="VCBCart="+document.cookie.substring(countbegin, countend)+"["+newItem+","+newType+"$"+newPrice+"#"+newQuantity+"]";
			itemcount=getitemcount();
			Cart[itemcount] = new CartItem(newItem,newType,newPrice,newQuantity)
			changecart();
			return true;
		}
	}
	else{
		tc=document.cookie;
		start=document.cookie.lastIndexOf("[",document.cookie.indexOf(newItem+","+newType));
		qstart=document.cookie.indexOf("#",document.cookie.indexOf(newItem+","+newType))+1;
		qend=document.cookie.indexOf("]",document.cookie.indexOf(newItem+","+newType));
		st=tc.substring(start,qstart);
		mi=tc.substring(qstart,qend);
		en=tc.substring(qend,qend+1);
		if(newPrice == "" || newPrice == "0" || newPrice == 0){
			alert("This item is not ready for sale.\nCheck back soon.")
		}
		else{
			if(newQuantity <= 0 || isNaN(newQuantity)) {
				rc = alert('The quantity for this item must be a number greater than 0');
				return false;
			}
			index = document.cookie.indexOf("VCBCart");
			countbegin = (document.cookie.indexOf("[", index));
			countend = document.cookie.length;
			qty=parseInt(newQuantity)+parseInt(mi);
			bas1=document.cookie.substring(countbegin,start);
			bas2=document.cookie.substring(qend+1,countend);
			document.cookie="VCBCart="+bas1+bas2+st+qty+en;
			changecart();
			return true;
		}
	}
}
function showItems(){
	if(document.cookie.indexOf("[")!=-1){
		index=document.cookie.indexOf("VCBCart");
		countbegin=(document.cookie.indexOf("=",index)+1);
		countend=document.cookie.indexOf(";", index);
		if (countend==-1){
			countend=document.cookie.length;
		}
		fulllist=document.cookie.substring(countbegin, countend);
		subtotal=0;
		totprice=0;
		totpricew=0;
		shipping=7;
		tax=0;
		document.writeln('<TABLE NAME="table" class="order" style="border-collapse: collapse" bgcolor="#ffffff"  cellpadding="5" cellspacing="0">');
		document.writeln('<TR bgcolor="cccccc"><TD width="230" align=left><b>Item</b></TD><TD width="180" align=left><b>Type</b></TD><TD width="10" align=left><b>Qty</b></TD><TD width="60" align=right><b>Cost Each</b></TD><td width="60" align=right><b>Total Cost</b><TD width="60" align=right><b>Action</b></TD></TR>');
		itemlist=0;
		for (var i = 0; i <= fulllist.length; i++){
			if (fulllist.substring(i,i+1)=='['){
				var itemstart=i+1;
			} 
			else if (fulllist.substring(i,i+1)==']'){
				itemend = i;
				thequantity = fulllist.substring(itemstart, itemend);
				itemtotal=0;
				itemtotal=(eval(theprice*thequantity));
				temptotal=itemtotal*100;
				subtotal=subtotal+itemtotal;
				tax=.089*subtotal;
				totpricew=subtotal+shipping + tax;
				totprice=subtotal+shipping;
				itemlist=itemlist+1;
				if(itemlist%2==0){
					document.writeln('<tr class="col1"><td class="col1">'+theitem+'</td><td class="col1" align=left>'+thetype+'</td><td class="col1" align=right><input class="textbox" size=3 type=text name=quant'+itemlist+' value='+thequantity+'></td><td class="col1" align=right>&#36;'+theprice+'</td><td class="col1" align=right>&#36;'+alterError(itemtotal)+'</td><td class="col1" align=right><a href="javascript:removeItem('+itemlist+')">Remove</a></td></tr>');
				}
				else{
					document.writeln('<tr class="col2"><td>'+theitem+'</td><td class="col2" align=left>'+thetype+'</td><td class="col2" align=right><input class="textbox" size=3 type=text name=quant'+itemlist+' value='+thequantity+'></td><td class="col2" align=right>&#36;'+theprice+'</td><td class="col2" align=right>&#36;'+alterError(itemtotal)+'</td><td class="col2" align=right><a href="javascript:removeItem('+itemlist+')">Remove</a>');
				}
				document.writeln('<INPUT TYPE="hidden" NAME="item'+itemlist+'" VALUE="'+theitem+'" SIZE="40" />');
				document.writeln('<INPUT TYPE="hidden" NAME="type'+itemlist+'" VALUE="'+thetype+'" SIZE="40" />');
				document.writeln('<INPUT TYPE="hidden" NAME="quantity'+itemlist+'" VALUE="'+thequantity+'" SIZE="40" />');
				document.writeln('<INPUT TYPE="hidden" NAME="price'+itemlist+'" VALUE="'+theprice+'" SIZE="40" />');
				document.writeln('<INPUT TYPE="hidden" NAME="total'+itemlist+'" VALUE="'+alterError(itemtotal)+'" SIZE="40" /	></td></tr>');
			} 
			else if(fulllist.substring(i,i+1)==','){
				theitem=fulllist.substring(itemstart,i);
				itemstart = i+1;
			} 
			else if(fulllist.substring(i,i+1)=='#'){
				theprice=fulllist.substring(itemstart,i);
				itemstart=i+1;
			}
			else if(fulllist.substring(i,i+1)=='$'){
				thetype=fulllist.substring(itemstart,i);
				itemstart=i+1;
			}
		}
		document.writeln('<tr class="coltotal"><td class="coltotal">total</td><td class="coltotal"></td><td class="coltotal"></td><td></td><td class="coltotal" align=right>&#36;'+alterError(subtotal)+'</td><td class="coltotal"></td></tr></table>');
	}
	else{
		document.writeln('<div style="height:200px;min-height:200px;">your cart is empty</div>');
	}
}
function showItemsend(){
	if(document.cookie.indexOf("[") != -1){
		index = document.cookie.indexOf("VCBCart");
		countbegin = (document.cookie.indexOf("=", index) + 1);
		countend = document.cookie.indexOf(";", index);
		if (countend == -1){
			countend = document.cookie.length;
		}
		fulllist = document.cookie.substring(countbegin, countend);
		subtotal = 0;
		totprice = 0;
		totpricew = 0;
		shipping = 7;
		tax = 0;
		document.writeln('<TABLE NAME="table" class="order" border="2" style="border-collapse: collapse" bgcolor="#ffffff" bordercolor="#ddaaff" cellpadding="5" cellspacing="0">');
		document.writeln('<TR bgcolor="cccccc"><TD width="230"><font face="MS Sans Serif"><b>Item</b></TD><TD width="120"><b>Type</b></TD><TD><b>Quantity</b></TD><TD><b>Cost Each</b></TD><td><b>Total Cost</b></TR>');
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++){
			if (fulllist.substring(i,i+1) == '['){
				var itemstart = i+1;
			} 
			else if (fulllist.substring(i,i+1) == ']'){
				itemend = i;
				thequantity = fulllist.substring(itemstart, itemend);
				itemtotal = 0;
				itemtotal = (eval(theprice*thequantity));
				subtotal = subtotal + itemtotal;
				tax = (eval(.089 * subtotal));
				totpricew = subtotal + shipping + tax;
				totprice = subtotal + shipping;
				itemlist=itemlist+1;
				document.writeln('<tr bgcolor="ffffff"><td>'+theitem+'</td><td align=left>'+thetype+'</td><td align=right>'+thequantity+'</td><td align=right>&#36;'+theprice+'</td><td align=right>&#36;'+alterError(itemtotal)+'</td></tr>');
				document.writeln('<INPUT TYPE="hidden" NAME="item'+itemlist+'" VALUE="'+theitem+'" >');
				document.writeln('<INPUT TYPE="hidden" NAME="type'+itemlist+'" VALUE="'+thetype+'" >');
				document.writeln('<INPUT TYPE="hidden" NAME="quantity'+itemlist+'" VALUE="'+thequantity+'">');
				document.writeln('<INPUT TYPE="hidden" NAME="price each'+itemlist+'" VALUE="'+theprice+'" >');
				document.writeln('<INPUT TYPE="hidden" NAME="total cost'+itemlist+'" VALUE="'+alterError(itemtotal)+'" >');
			} 	
			else if (fulllist.substring(i,i+1) == ',') {
				theitem = fulllist.substring(itemstart, i);
				itemstart = i+1;
			} 
			else if (fulllist.substring(i,i+1) == '#'){
				theprice = fulllist.substring(itemstart, i);
				itemstart = i+1;
			}
			else if (fulllist.substring(i,i+1) == '$'){
				thetype = fulllist.substring(itemstart, i);
				itemstart = i+1;
			}
		}
		document.writeln('<tr><td colspan=4 bgcolor="ffff99">Sub-Total</td><td align=right bgcolor="ffff99">&#36;'+alterError(subtotal)+'</td></tr>');
		document.writeln('<tr><td colspan=4 bgcolor="ffff99">Tax if Shipping to Washington State</td><td align=right bgcolor="ffff99">&#36;'+alterError(tax)+'</td></tr>');
		document.writeln('<tr><td colspan=4 bgcolor="ffff99">Shipping and Handling</td><td align=right bgcolor="ffff99">&#36;'+alterError(shipping)+'</td></tr>');
		document.writeln('<tr><td colspan=4 bgcolor="ff9999"><b>Total if Shipping to Washington State</b></td><td align=right bgcolor="ff9999"><b>&#36;'+alterError(totpricew)+'</b></td></tr>');
		document.writeln('<tr><td colspan=4 bgcolor="ff9999"><b>Total</b></td><td align=right bgcolor="ff9999"><b>&#36;'+alterError(totprice)+'</b></td></tr>');
		document.writeln('<INPUT TYPE="hidden" NAME="Goods Total" VALUE="'+alterError(totprice)+'" SIZE="40">');
		document.writeln('</font></TABLE>');
	}
	else{
		document.writeln('your cart is empty');
	}
}
function show(){
	var df=document.form;
	var orderitems = "";
	if(document.cookie.indexOf("[") != -1){
		index = document.cookie.indexOf("VCBCart");
		countbegin = (document.cookie.indexOf("=", index) + 1);
		countend = document.cookie.indexOf(";", index);
		if (countend == -1){
			countend = document.cookie.length;
		}
		fulllist=document.cookie.substring(countbegin, countend);
		subtotal=0;
		totprice=0;
		totpricew=0;
		shipping=7;
		tax=0;
		itemlist=0;
		for(var i=0;i<=fulllist.length;i++){
			if(fulllist.substring(i,i+1)=='['){
				var itemstart=i+1;
			} 
			else if(fulllist.substring(i,i+1)==']'){
				itemend=i;
				thequantity=fulllist.substring(itemstart,itemend);
				itemtotal=0;
				itemtotal=(eval(theprice*thequantity));
				temptotal=itemtotal*100;
				subtotal=subtotal+itemtotal;
				tax=.089*subtotal;
				totpricew=subtotal+shipping+tax;
				totprice=subtotal+shipping;
				itemlist=itemlist+1;
				orderitems=orderitems+"<tr><td align=right valign=top><a href=http://www.trinketsbyt.com/productdetails.asp?xrs=name&xwhat="+escape(theitem)+"&xextra="+escape(thetype)+">"+theitem+"</a></td><td align=left valign=top width=1>"+thetype+" </td><td align=right valign=top width=1>$</td><td align=right valign=top width=1>"+theprice+".00 </td><td align=left valign=top width=1>"+thequantity+" </td><td align=left valign=top>$</td><td align=right valign=top width=1>"+alterError(itemtotal)+"</td></tr>";
			} 
			else if(fulllist.substring(i,i+1)==','){
				theitem=fulllist.substring(itemstart,i);
				itemstart=i+1;
			} 
			else if(fulllist.substring(i,i+1)=='#'){
				theprice=fulllist.substring(itemstart,i);
				itemstart=i+1;
			}
			else if(fulllist.substring(i,i+1)=='$'){
				thetype=fulllist.substring(itemstart, i);
				itemstart=i+1;
			}
		}	
		document.form.Item_List.value=(orderitems);
		document.form.Sub_Total.value=convertIt(subtotal);
		document.form.Tax.value=convertIt(tax);
		document.form.Shipping.value=convertIt(shipping);
		document.form.Total.value=convertIt(totprice);
		document.form.WA_Total.value=convertIt(totpricew);
	}
	document.form.submit();
}
function removeItem(itemno){
	newItemList=null;
	itemlist=0;
	for(var i=0;i<=fulllist.length;i++){
		if(fulllist.substring(i,i+1)=='['){
			itemstart = i+1;
		}
		else if(fulllist.substring(i,i+1)==']'){
			itemend=i;
			theitem = fulllist.substring(itemstart,itemend);
			itemlist=itemlist+1;
			if(itemlist!=itemno){
				newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
			}
		}
	}
	index = document.cookie.indexOf("VCBCart");
	document.cookie="VCBCart="+newItemList;
	changecart();
}
function clearBasket() {
	document.cookie="VCBCart=";
	changecart();
}
function changecart(){
	window.location="http://www.trinketsbyt.com/BBASKET.aspx";	
}
function resetShoppingBasket(){
	document.cookie="VCBCart=";
}
function getitemcount(){
	if(document.cookie.indexOf("[") != -1){
		index = document.cookie.indexOf("VCBCart");
		countbegin = (document.cookie.indexOf("=", index) + 1);
		countend = document.cookie.indexOf(";", index);
		if (countend == -1){
			countend = document.cookie.length;
		}
		fulllist = document.cookie.substring(countbegin, countend);
		var itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++){
			if (fulllist.substring(i,i+1)=='['){
				var itemstart = i+1;
			}
			else if (fulllist.substring(i,i+1)==']') {
				itemend = i;
				itemlist=itemlist+1;
			}
		}
		return itemlist
	}
}
function updateBasket(){
	var i=1;
	itemlist="";
	noitems = getitemcount();
	for(i;i<=noitems;i++){
		xpid= (eval('document.form.item'+i+'.value'));
		xtype= (eval('document.form.type'+i+'.value'));
		xprice= (eval('document.form.price'+i+'.value'));
		xquantity= (eval('document.form.quant'+i+'.value'));
		Cart[i] = new CartItem(xpid,xtype,xprice,xquantity)	
		Cart[i].pid =xpid;
		Cart[i].type =xtype;
		Cart[i].price =xprice;		
		Cart[i].qty =xquantity
			if(Cart[i].qty>0){
				itemlist+="["+Cart[i].pid+","+Cart[i].type+"$"+Cart[i].price+"#"+Cart[i].qty+"]";
			}
	}
	document.cookie="VCBCart="+itemlist;
	changecart();
}
function checkdisable(ptype){
	var x=document.form;
	if(ptype=="Check" || ptype=="Money Order" || ptype==""){
		x.Card_Name.disabled=1;
		x.Card_Number.disabled=1;
		x.Exp1.disabled=1;
		x.Exp2.disabled=1;
		x.VNUM.disabled=1;
	}
	else{
		x.Card_Name.disabled=0;
		x.Card_Number.disabled=0;
		x.Exp1.disabled=0;
		x.Exp2.disabled=0;
		x.VNUM.disabled=0;
	}
}
function shipdisable(sa){
	var x=document.form;
	if(sa!="different"){
		x.Shipping_Address.value=x.Billing_Address.value;
		x.Shipping_City.value=x.Billing_City.value;
		x.Shipping_State.value=x.Billing_State.value;
		x.Shipping_Zip.value=x.Billing_Zip.value;
		x.Shipping_Address.disabled=1;
		x.Shipping_City.disabled=1;
		x.Shipping_State.disabled=1;
		x.Shipping_Zip.disabled=1;
	}
	else{
		x.Shipping_Address.disabled=0;
		x.Shipping_City.disabled=0;
		x.Shipping_State.disabled=0;
		x.Shipping_Zip.disabled=0;
	}
}

function bridaldetail(image,source){
	document.getElementById(image).src = source;
}
function fixObject(){
	if(navigator.appName.indexOf ("Microsoft") !=-1){
		if(document.all){
			theObjects = document.getElementsByTagName("object"); 
			for (var i = 0; i < theObjects.length; i++) { 
				theObjects[i].outerHTML = theObjects[i].outerHTML;
			}
		}
	}
}

if(navigator.appName.indexOf ("Microsoft") !=-1){
	window.attachEvent("onload", fixObject);
}