/*
   Author: Hari Khalsa
   Date: 09.26.02
   Description:
   Contains JavaScript functions for displaying the number of items in shopping bag.
   Functions are essentially an intelligent image swap to display numbers from 0 to 999.
   
   preload() 	   - loads images to page cache. images are est. 1-2k, low-weight.
   setToolBagVis() - Flags whether or not to display the tool bar's shopping bag ticker (if 0, doesn't display).
   getBagNum() 	   - gets the bag count from psp, converts to string and then to array of strings.
   setBagNum()	   - gets the digit strings from getBagNum, loops through and creats an array of characters
   				     then, evaluates the image source of char0-char3 appropriately
   addBagNum()	   - not currently used.  increments the bag count, re-triggers getBagNum and setBagNum.
   initBag()	   - called from the body onload event, calls functions: preload, getBagNum and setBagNum.

*/

//preload  bag images
//flag loaded set to false
var loaded = false;
function preload(){
	if(document.images){
		num_0 = new Image();
		num_0.src = "/store/assets/images/tools/0.gif";
		num_1 = new Image();
		num_1.src = "/store/assets/images/tools/1.gif";
		num_2 = new Image();
		num_2.src = "/store/assets/images/tools/2.gif";
		num_3 = new Image();
		num_3.src = "/store/assets/images/tools/3.gif";
		num_4 = new Image();
		num_4.src = "/store/assets/images/tools/4.gif";
		num_5 = new Image();
		num_5.src = "/store/assets/images/tools/5.gif";
		num_6 = new Image();
		num_6.src = "/store/assets/images/tools/6.gif";
		num_7 = new Image();
		num_7.src = "/store/assets/images/tools/7.gif";
		num_8 = new Image();
		num_8.src = "/store/assets/images/tools/8.gif";
		num_9 = new Image();
		num_9.src = "/store/assets/images/tools/9.gif";
		txt_Lpar = new Image();
		txt_Lpar.src = "/store/assets/images/tools/lpar.gif";
		txt_Rpar = new Image();
		txt_Rpar.src = "/store/assets/images/tools/rpar.gif";
		img_Clear = new Image();
		img_Clear.src = "/store/assets/images/clear.gif";
		//set flag loaded to true
		return (loaded = true);
	}
}



//set global bag variables
var strBagCount = "";
var arrayChar = new Array();
var arrayDig = new Array();
var showBag = false;

function setToolBagVis(){
	//alert("from setToolBagVis, intBagCount:"+intBagCount);
	if((intBagCount>0)||(intBagCount != "0")){
		showBag = true;
	}
	//alert(showBag);
}

//gets the intBagCount from the cookie read, sets values for use in setBagNum
function getBagNum(){
	//alert("value of bagCount to display function is :"+intBagCount);
	//var intBagCount = bagcount;
	var strBagCount = intBagCount.toString();
	//alert("strBagCount is: " + strBagCount);
	numDig = strBagCount.length;
	var j = 1;
	for(i=0;i<numDig;i++){
		arrayDig[i] = strBagCount.substring(i,j);
		j++;
	}
	//alert("arrayDig: "+arrayDig);
}

//sets values for character array, evaluates img. src. appropriately
function setBagNum(){
	//create values for arrayChar
	j = 0;
	arrayChar[j] = "txt_Lpar";
	j++;
	for(i=0;i<arrayDig.length;i++){
		var digImg = "num_" + arrayDig[i];
		arrayChar[j] = digImg;
		j++;
	}
	arrayChar[j] = "txt_Rpar";
	//set image source evals
	var numChar = arrayChar.length;
	if((loaded==true)&&(showBag==true)){
		for(i=0;i<=4;i++){
			if((arrayChar[i]) && (arrayChar[i] != "")){
				eval("document.images['char"+i+"'].src = "+arrayChar[i]+".src;");
				//alert("evaluated image: "+ i);
			}
			//alert(arrayChar[i]);
		}
	}
}

//not used, is for incrementing bag count
function addBagNum(){
	showBag = true;
	intBagCount++;
	getBagNum();
	setBagNum();
	//alert(arrayChar)
}

//initialized functions for bag count display
function initBag(){
	preload();
	setToolBagVis();
	getBagNum();
	setBagNum();
}
