// SparrowScripts Javascript Shopping Cart System
// written by Tan Ling Wee
// last updated 4 Oct 2005
// website : www.sparrowscripts.com
// ===================================================================

	var numItems;
	var ie=document.all;
	var dom=document.getElementById;
	var ns4=document.layers;
	var Show=false;
	var EnableCart=true;
	var receiptTotalQty=0;
	var receiptTotalAmt=0;
	var receiptTotalDisc=0;

	function Item(id, description, unitPrice, qty, maxQty, discount, checked){

		this.internalId=numItems;
		this.id=id;
		this.description=description.replace(",","&comma&");
		this.unitPrice=unitPrice;
		this.qty=parseInt(qty,10);
		this.discount=validateNumber(discount,2);
		this.price=validateNumber((this.unitPrice-this.discount)*this.qty,2);
		this.maxQty=maxQty;
		this.checked=checked;

		document.cookie="*p"+this.internalId+"="+id+","+this.description+","+unitPrice+","+qty+","+maxQty+","+discount+","+checked+";path=/;";
		numItems++;

		this.updateQty=function(qty){

			receiptTotalAmt-=parseFloat(this.price);
			receiptTotalQty-=this.qty;

			if ((qty>0)&&(this.qty==0))
			{
				this.checked=false;
			}
			
			if (qty>0)
			{
				this.price=validateNumber((this.unitPrice-this.discount)*qty,2);
			}

			<!--if (this.qty>0) {-->
			<!--	document.getElementById("sscPrice"+this.internalId).innerHTML=this.price;-->
			<!--}-->

			this.qty=qty;

			receiptTotalQty+=this.qty;
			receiptTotalAmt+=parseFloat(this.price);

			if (document.getElementById("sscSubtotalQty")!=null)
			{
				document.getElementById("sscSubtotalQty").innerHTML=receiptTotalQty;
				document.getElementById("sscSubtotalAmt").innerHTML=validateNumber(receiptTotalAmt,2);
			}

			document.cookie="*p"+this.internalId+"="+id+","+this.description+","+unitPrice+","+qty+","+maxQty+","+discount+","+this.checked+";path=/;";
		}

		this.updateCheck=function(checked){
			this.checked=checked;
			document.cookie="*p"+this.internalId+"="+id+","+this.description+","+unitPrice+","+qty+","+maxQty+","+discount+","+checked+";path=/;";
		}
	}

	var items=new Array();
	var checkboxIndex;

// check if browser supports cookies

	function CheckCookieSupport() {

		var fields=new Array();
		var strCookie, iItemEnd, sItemId;

		numItems=0;

		document.cookie="path=/;";

		if (document.cookie.length<=0) {

			if (document.cookie.length<=0) {

				EnableCart=false;

				UpdateStatus("<font color='#a00000'>"+Message[92]+"</font>"); 

				document.getElementById("sscCartContent").innerHTML="<table cellpadding=10px><tr><td><font class='cart_message_header'>"+Message[90]+"</font><div align='justify' class='cart_message'><BR>"+Message[91]+"</div></td></tr></table>";
			}
		}
		else {

			strCookie=document.cookie+";"

			iItemEnd=strCookie.indexOf("; ");			
			while (iItemEnd>=0)
			{
				// parse individual item

				sItemId=strCookie.substring(0,strCookie.indexOf("="));

				if ((sItemId!="path")&&(sItemId.substring(0,2)=='*p')) {
					sItem=strCookie.substring(strCookie.indexOf("=")+1,strCookie.indexOf(";"))+",";
					for (i=0;i<=6;i++)
					{
						fields[i]=sItem.substring(0,sItem.indexOf(","));
						sItem=sItem.substring(sItem.indexOf(",")+1);
					}

					if (fields[6]=="true")
					{
						bChecked=true;
					}
					else {
						bChecked=false;
					}
					internalAddItem(fields[0],fields[1],fields[2],fields[3],fields[4],fields[5],fields[6],bChecked);
				}

				// prepare for next item

				strCookie=strCookie.substring(iItemEnd+2);
				iItemEnd=strCookie.indexOf(";");			
			}

			refreshContent();
		}
	}

	function DeleteCookie (cookieName) { 
		var exp = new Date(); 
		exp.setTime (exp.getTime() - 1);  // This cookie is history 
		document.cookie = cookieName + "=; path='/'; expires=" + exp.toGMTString(); 
	} 

// shopping cart status panel

	function UpdateStatus(strStatus) {
		if (EnableCart)
		{
			if (Show)
			{
				document.getElementById("sscStatusText").innerHTML=strStatus+ " <font class='sub_status'>"+Message[3].replace("<","&lt;").replace(">","&gt;")+"</font>";
			} else {
				document.getElementById("sscStatusText").innerHTML=strStatus+ " <font class='sub_status'>"+Message[2].replace("<","&lt;").replace(">","&gt;")+"</font>";
			}
		}
		else {
			document.getElementById("sscStatusText").innerHTML=strStatus;
		}
	}

	document.write("<table id='sscStatus' cellpadding=0 cellspacing=0><tr><td>&nbsp;<!--<img id='sscCartImage' src='"+ImagePath+"vcaddie.gif' align='top'>&nbsp;--></td><td id='sscStatusTd' class='status' onmouseover='this.className=\"status_hover\"' onmouseout='if(!Show){this.className=\"status\"}' onclick='if(!Show){showCart()}else{hideCart()}'><span id='sscStatusText'>"+Message[1]+" <font class='sub_status'>"+Message[2].replace("<","&lt;").replace(">","&gt;")+"</font></span></td></tr></table>");

// shopping cart

	document.write("<div id='sscCart' style='z-index:+999;position:absolute;visibility:hidden;'><table class='cart' cellspacing=0 width='"+cartWidth+"' bgcolor=#FFFFFF ><tr><td style='padding:3px 6px 6px 6px' align=center><table id='cart_content_table' cellpadding=0 cellspacing=0 class='cart_message_content' width='"+cartWidth+"'><tr><td><span id='sscCartContent' class='cart_message'><BR><center>"+Message[10]+"</center><BR></span></td></tr></table></td></tr><tr valign=bottom><td>");
	
	if (Message[100]!="")
	{
		document.write("<center><table class='cart_footnote' width='"+(cartWidth+2)+"' cellpadding=0 cellspacing=0><tr><td>"+Message[100]+"</td></tr></table></center><br>");
	}

	document.write("<center><input type='button' id='sscBtnRemoveChecked' class='button' value='"+Message[40]+"'  disabled='true' onclick='removeCheckedItems()'>&nbsp;<input type='button' id='sscBtnRemoveAll' class='button' value='"+Message[41]+"'  disabled='true' onclick='removeAllItems()'>&nbsp;<input type='button' disabled='true' id='sscBtnCheckOut' class='button' value='"+Message[42]+"' onclick='if (confirm(\""+Message[53]+"\")) {document.location.href=\""+checkoutPage+"\"}'>&nbsp;<input type='button' id='sscBtnHide' class='button' value='"+Message[43]+"' onclick='hideCart();document.getElementById(\"sscStatusTd\").className=\"status\";'></center></td></tr></table></div>");

	var crossobj=document.getElementById("sscCart").style;

	CheckCookieSupport();

	function refreshButtons(bRemoveChecked, bRemoveAll, bCheckOut) {

		document.getElementById("sscBtnRemoveChecked").disabled = !bRemoveChecked;
		document.getElementById("sscBtnRemoveAll").disabled = !bRemoveAll;
		document.getElementById("sscBtnCheckOut").disabled = !bCheckOut;
	}

	function refreshContent() {

		var sHTML="";
		var gotChecked=false;
		receiptTotalQty=0;
		receiptTotalAmt=0;
		receiptTotalDisc=0;

		if ((EnableCart)&&(numItems>0))
		{
			// display header

			sHTML="<table cellpadding=0 cellspacing=0 width='"+cartWidth+"' ><tr nowrap>";

			sHTML+="<td class='listing_header'><input type='checkbox' id='sscMasterCheck' onclick='MasterCheck(this)'></td>";

			if (showProductId){
				sHTML+="<td class='listing_header'>"+Message[21]+"</td>";
			}

			if (showProductDescription){
				sHTML+="<td class='listing_header' width='*'>"+Message[22]+"</td>";
			}

			if (showProductUnitPrice){
				sHTML+="<td class='listing_header'>"+Message[26]+"</td>";
			}
			
			<!--sHTML+="<td class='listing_header'></td>";-->

			if (showProductDiscount){
				sHTML+="<td class='listing_header'>"+Message[25]+"</td>";
			}

			if (showProductPrice){
				sHTML+="<td class='listing_header'>"+Message[26]+"</td>";
			}

			sHTML+="</tr>";

			checkboxIndex=0;

			for (i=0;i<numItems;i++)
			{
				if (items[i].qty>0)
				{
					sEvenOddLine=(checkboxIndex%2)+1;
					checkboxIndex++;

					receiptTotalQty+=items[i].qty;
					receiptTotalAmt+=parseFloat(items[i].price);
					receiptTotalDisc+=parseFloat(items[i].discount*items[i].qty);

					sHTML+="<tr nowrap valign=center><td class='listing_content_"+sEvenOddLine+"' align='center'><input type='checkbox' id='chk"+(checkboxIndex)+"' value='"+i+"' onclick='CheckItem(this)' "

					if (items[i].checked)
					{
						sHTML+="checked";
						gotChecked=true;
					}
					
					sHTML+="></td>";

					if (showProductId){
						sHTML+="<td class='listing_content_"+sEvenOddLine+"'><a href='/http://www.capcroatia.com/location-croatie/fiche-"+items[i].id+"-detail.html/' target='_parent'>"+items[i].id+"</a></td>";
					}

					if (showProductDescription){
						sHTML+="<td class='listing_content_"+sEvenOddLine+"' width='*'>"+items[i].description.replace("&comma&",",")+"</td>";
					}

					if (showProductUnitPrice){
						sHTML+="<td class='listing_content_"+sEvenOddLine+"' align='right'>"+items[i].unitPrice+" </td>";
					}
					
					if (showProductqty){
					sHTML+="<td class='listing_content_"+sEvenOddLine+"' align='center' style='padding:1px'><input class='input' type='hidden' id='sscQuantity"+i+"' value='"+items[i].qty+"' size=3 onblur='validateCtl(this)'></td>";
					}

					if (showProductDiscount){
						sHTML+="<td class='listing_content_"+sEvenOddLine+"' align='right'>"+items[i].discount+"</td>";
					}

					if (showProductPrice){
						sHTML+="<td class='listing_content_"+sEvenOddLine+"' align='right'><span id='sscPrice"+i+"'>"+items[i].price+" </span></td>";
					}

					sHTML+="</tr>"
				}
			}
			if (showSubtotalQty){
			sHTML+="<tr height=25 valign='center'><td class='listing_summary' colspan=4 align='right'><B>"+Message[27]+"</B></td><td align='center' class='listing_summary'><B><span id='sscSubtotalQty'>:</span></B></td>"
			}

			if (showProductDiscount)
			{
				sHTML+="<td align='right' class='listing_summary'><B><span id='sscSubtotalAmt'>"+validateNumber(receiptTotalDisc,2)+"<span></B></td>";
			}
			
			if (showSubtotalQty){
			sHTML+="<td align='right' class='listing_summary'><B><span id='sscSubtotalAmt'>"+validateNumber(receiptTotalAmt,2)+"<span></B></td></tr>";
			}
			
			sHTML+="</table>";

			if (checkboxIndex>0)
			{
				document.getElementById("sscCartContent").innerHTML=sHTML;
				refreshButtons(gotChecked, true, true);
			}
			else {
				document.getElementById("sscCartContent").innerHTML="<BR><center>"+Message[10]+"</center><BR>";
				refreshButtons(false, false, false);
			}

			UpdateItemCount();
		}
	}

	function UpdateItemCount(){

		if (EnableCart) {
			if (receiptTotalQty>0)
			{
				UpdateStatus(Message[11].replace("[numItems]","<B>"+receiptTotalQty+"</B>"));
				//document.getElementById("sscCartImage").src=ImagePath+"vcaddie.gif";
			}
			else {
				UpdateStatus(Message[10]);
				//document.getElementById("sscCartImage").src=ImagePath+"vcaddie.gif";
			}
		}
	}

	function AddItem(id, description, unitprice, quantity, maxQty, discount) {
		
		var bResult=false;

		if (EnableCart) {
			if (maxQty<quantity)
			{
				alert(Message[94].replace("[maxqty]",maxQty).replace("[description]",description));
			}
			else {
				bResult=internalAddItem(id, description, unitprice, quantity, maxQty, discount, false);
				if (bResult) {
					alert(Message[52].replace("[qty]",quantity).replace("[description]",description));
					refreshContent();
				}
			}
		} else {
			alert(Message[93]);
		}
	}

	function internalAddItem(id, description, unitprice, quantity, maxQty, discount, checked) { 

		var alreadyExists=false;

		if (EnableCart)
		{
			// check if this item already exists
			for (i=0;i<numItems;i++)
			{
				if (id==items[i].id)
				{
					alreadyExists=true;
					// modify quantity of last entry'
					if (items[i].qty+parseInt(quantity,10)>maxQty)
					{
						alert(Message[94].replace("[maxqty]",maxQty).replace("[description]",description));
						return false;
					}
					else {
						items[i].updateQty(items[i].qty+parseInt(quantity,10));
						refreshContent();
					}
				}
			}

			if(!alreadyExists){
				// add new entry
				if (checked=="true")
				{
					items[numItems]=new Item(id, description, unitprice, quantity, maxQty, discount, true);
				}
				else {
					items[numItems]=new Item(id, description, unitprice, quantity, maxQty, discount, false);
				}
				if (numItems==1)
				{
					//document.getElementById("sscCartImage").src=ImagePath+"vcaddie.gif";
				}
			}
		}

		return true;
	}

	function MasterCheck (masterControl) {

		var i;
		var j=0;
		for (i=0;i<numItems;i++){
			if (items[i].qty>0)
			{
				j++;
				document.getElementById("chk"+j).checked=masterControl.checked;
				if (masterControl.checked)
				{
					items[document.getElementById("chk"+j).value].updateCheck(true);
				}
				else {
					items[document.getElementById("chk"+j).value].updateCheck(false);
				}
			}
		}

		if (masterControl.checked)
		{
			refreshButtons(true, true, true);
		}
		else {
			refreshButtons(false, true, true);
		}
	}

	function CheckItem(chkbox) {

		var allChecked=true;
		var numChecked=0;

		if (chkbox.checked)
		{
			items[chkbox.value].updateCheck(true);
		}
		else {
			items[chkbox.value].updateCheck(false);
		}

		for (i=1;i<=checkboxIndex;i++)
		{
			if (!document.getElementById("chk"+i).checked)
			{
				allChecked=false;
			}
			else {
				numChecked++;
			}
		}
		
		document.getElementById("sscMasterCheck").checked=allChecked;
		if (numChecked>0)
		{
			refreshButtons(true, true, true);
		}
		else {
			refreshButtons(false, true, true);
		}
	}

	function removeCheckedItems(){

		if (confirm(Message[50]))
		{
			for (i=1;i<=checkboxIndex;i++)
			{
				if (document.getElementById("chk"+i).checked)
				{
					items[document.getElementById("chk"+i).value].updateQty(0);
				}
			}
		}

		refreshContent();
	}

	function removeAllItems() {

		if (confirm(Message[51])){

			for (i=numItems-1;i>=0;i--)
			{
				items[i].updateQty(0);
			}
			refreshContent();
		}
	}

	function validateCtl(ctlThis) {

		var index=parseInt(ctlThis.id.substr(11));
		var value=validateNumber(ctlThis.value,0);
		var ivalue=parseInt(value,10);
		ctlThis.value=ivalue;
		if (ivalue>items[index].maxQty)
		{
			alert(Message[94].replace("[maxqty]",items[index].maxQty).replace("[description]",items[index].description.replace("&comma&",",")));
			ctlThis.focus();
		}
		else {
			items[index].updateQty(ivalue);
			if (ivalue==0)
			{
				refreshContent();
			}

			UpdateItemCount();
		}
	}

	function validateNumber(num, decimalPos) {

		var sNewString="";
		var cntDecimal=0;
		var i;
		var tmp;

		num=num+""; //convert to string

		for (i=0;i<num.length;i++){

			ch=num.charAt(i);

			if(((ch>='0')&&(ch<='9'))||((ch=='.')&&(cntDecimal==0))){

				if ((ch=='.')&&(i==0)) {
					sNewString="0";					
				}

				if (ch=='.') {
					cntDecimal++;
				}

				if (!((ch=='.')&&(i==num.length-1))) {
					sNewString+=ch;
				}
			}
		}

		if (sNewString=="") {
			sNewString="1";
		}

		decimalCount=sNewString.indexOf(".");

		if (decimalCount>=0) {
			// decimal point found
			tmp=Math.pow(10,decimalPos);
			sNewString=(Math.round(parseFloat(sNewString)*tmp)/tmp)+"";

			if (sNewString.length-decimalCount<=decimalPos+1){
				nCount=(decimalPos-sNewString.length+decimalCount+1);
				for (i=0;i<nCount;i++){
					sNewString+="0";
				}
			}
		}
		else {
			// decimal point not found

			if (decimalPos>0) {
				sNewString+="."
				for (i=0;i<decimalPos;i++){
					sNewString+="0";
				}
			}
		}

		return sNewString+"";
	}

	function hideCart() {
		crossobj.visibility="hidden"
		showElement( 'SELECT' );
		showElement( 'APPLET' );
		Show=false;
		UpdateItemCount();
	}

	function showCart() {

		var leftpos=0;
		var toppos=0;
		var statusbar;

		ctl=document.getElementById("sscStatus");
		aTag = ctl;
		do {
			aTag = aTag.offsetParent;
			leftpos	+= aTag.offsetLeft;
			toppos += aTag.offsetTop;
		} while(aTag.tagName!="BODY");

		crossobj.left =	ctl.offsetLeft	+ leftpos; 
		crossobj.top = ctl.offsetTop +	toppos + ctl.offsetHeight + 2;
		crossobj.visibility=(dom||ie)? "visible" : "show"

		hideElement( 'SELECT', document.getElementById("sscCart") );
		hideElement( 'APPLET', document.getElementById("sscCart") );			
		Show=true;
		UpdateItemCount();
	}

	// hides <select> and <applet> objects (for IE only)
	function hideElement(elmID, overDiv){
		if( ie ){
			for( i = 0; i < document.all.tags( elmID ).length; i++ ){
				obj = document.all.tags( elmID )[i];
				if( !obj || !obj.offsetParent ){
						continue;
				}
				  // Find the element's offsetTop and offsetLeft relative to the BODY tag.
				  objLeft   = obj.offsetLeft;
				  objTop    = obj.offsetTop;
				  objParent = obj.offsetParent;
				  while( objParent.tagName.toUpperCase() != "BODY" )
				  {
					objLeft  += objParent.offsetLeft;
					objTop   += objParent.offsetTop;
					objParent = objParent.offsetParent;
				  }
				  objHeight = obj.offsetHeight;
				  objWidth = obj.offsetWidth;
				  if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
				  else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
				  else if( overDiv.offsetTop >= ( objTop + objHeight + obj.height ));
				  else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
				  else
				  {
					obj.style.visibility = "hidden";
				  }
			}
		}
	}
		 
	//unhides <select> and <applet> objects (for IE only)
	function showElement( elmID ){
		if( ie ){
			for( i = 0; i < document.all.tags( elmID ).length; i++ ){
				obj = document.all.tags( elmID )[i];
				if( !obj || !obj.offsetParent ){
						continue;
				}
				obj.style.visibility = "";
			}
		}
	}
