function objZipBag(){
	//properties
	this.name = "Zip and Mail";
	this.id = "z1";	
	this.bagitems = new Array();
	this.bagitemCount = 0;
	this.bagitemPointer = 0;	
	this.maxbagitems = 10;
	//methods
	this.addbagitem = addbagitem;	
	this.removebagitem = removebagitem;		
	this.showbagitems = showbagitems;			
	this.serialize = serialize;
	this.getbagitemIds = getbagitemIds;	
	this.reLoad = reLoad;	
	this.removeAll = removeAll;		
	this.updateForm = updateForm;	
	this.isInBag = isInBag;
}
function updateForm(){
	//alert(oZip.serialize());
	document.frmSearch.hdnZipBag.value = oZip.serialize();				
	//alert(document.frmSearch.hdnZipBag.value);
}
function reLoad(sSelList){
	var myArray = sSelList.split(":");
	//var myArray = sSelList.split("^");	

	for (i = 0; i < myArray.length-1; i++)		
	{
		this.addbagitem(myArray[i]);
	}
	//this.showbagitems();

	//check boxes if selected
	//alert(document.forms[0].name);
	if (document.zipandmail === undefined){
		//zipmailform doesn't exist
	}else{
		f = document.zipandmail;
		for (var ii=0; ii < f.elements.length; ii++){
			var e = f.elements[ii];
			//alert(e.name);
			if (e.name != null){	
				if (e.name == "cbZip"){
					//alert(e.name + e.value);		
					if (oZip.isInBag(e.value)) {
						e.checked = true;
					}
				}
			}
		}
	}	

}
function serialize(){
	var sRet = "";
	for (var i in this.bagitems){
		if (this.bagitems[i] != null){
			sRet += this.bagitems[i]+ ":";
			//sRet += this.bagitems[i]+ "^";			
		}
	}
	return sRet;	
}
function getbagitemIds(){
	var sRet = "";
	for (var i in this.bagitems){
		if (this.bagitems[i] != null){
			sRet += this.bagitems[i]+ ",";
		}
	}
	return sRet.substr(0,sRet.length-1);	
}
function addbagitem(ibagitemID){	
	if  (this.bagitemCount <= this.maxbagitems -1){
		this.bagitems[this.bagitemPointer] = ibagitemID;
		this.bagitemCount += 1;
		this.bagitemPointer += 1;		
	}	
	this.updateForm();
}
function removebagitem(ibagitemID){
	//alert("remove item");
	for (var i in this.bagitems){
		//alert(ibagitemID + '=' + this.bagitems[i])
		if (this.bagitems[i] == ibagitemID){
			//alert("removed");
			this.bagitems[i] = null;
			this.bagitemCount -= 1;			
			break;
		}
	}
	//this.showbagitems();
	this.updateForm();	
}
function isInBag(ibagitemID){
	var bResult = false;
	for (var i in this.bagitems){
		if (this.bagitems[i] == ibagitemID){
			bResult = true;
			break;
		}
	}
	return bResult;
}
function showbagitems(){
	var sRet = "";
	for (var i in this.bagitems){
		sRet += i + "\t" + this.bagitems[i]+ "\n";
	}
	sRet += "Length : " + this.bagitems.length;
	alert(sRet);	
	alert(document.zipandmail.cbZip.value);
	alert(document.zipandmail.SelectedFile.value);
}
function toggleZip(ibagitemID){
	//alert(ibagitemID);
	var oCB;
	if (document.getElementById)
	{
		oCB = eval('document.getElementById("cbZip_' + ibagitemID+ '")');
	}
	else if (document.all)
	{
		oCB = eval('document.all("cbZip_' + ibagitemID+ '")');	
	}
	else if (document.layers)
	{
		oCB = eval('document.layers["cbZip_' + ibagitemID+ '"]');
	}
	if (oCB.checked){
		if  (oZip.bagitemCount <= oZip.maxbagitems -1){		
			oZip.addbagitem(ibagitemID);
		}else{
			alert("Maximum number of items you can Zip and Mail is "+ oZip.maxbagitems+ "!");
			oCB.checked = false;
		}	
			
	}else{
		oZip.removebagitem(ibagitemID);
	}		
}
function add2Zip(oCB){
	//alert(oCB.value);
	var arrOCB = oCB.value.split("^");	
	/*
	var sMsg = ""
	sMsg += "Product:\t" + arrOCB[0];
	sMsg += "\nType:\t" + arrOCB[1];
	sMsg += "\nTitle:\t" + arrOCB[2].replace(/&/g," and ");
	sMsg += "\nSize:\t" + arrOCB[3];
	sMsg += "\nFileName:\t" + arrOCB[4];				
	alert(sMsg);
	//*/
	if (oCB.checked){
		if  (oZip.bagitemCount <= oZip.maxbagitems -1){		
			oZip.addbagitem(oCB.value.replace(/&/g," and "));
			//oZip.addbagitem(oCB.value);			
		}else{
			alert("Maximum number of items you can Zip and Mail is "+ oZip.maxbagitems+ "!");
			oCB.checked = false;
		}	
			
	}else{
		oZip.removebagitem(oCB.value);
	}		
}
function removeAll(){
	//this.showbagitems();
	this.bagitems = new Array();
	this.bagitemCount = 0;
	this.bagitemPointer = 0;	
	
	f = document.zipandmail;
	for (var ii=0; ii < f.elements.length; ii++){
		var e = f.elements[ii];
		if (e.name != null){	
			if (e.name == "cbZip"){
					e.checked = false;
			}
		}
	}

	document.frmSearch.hdnZipBag.value = oZip.serialize();					
	//this.showbagitems();
}
function sendZipSelection(){
	//document.getElementById("divZip").innerHTML = oZip.showbagitems();
	//document.getElementById("divZip").visible = true;
	if (oZip.bagitemCount > 0){
		document.getElementById("bZipAndMail").value = 1;	
		document.frmbagitem.sZipSelection.value = oZip.getbagitemIds();				
		document.frmbagitem.submit();	
	}else{
		alert("Please make selection by clicking on [Select] checkbox before submit!");
	}	
}
function orderZipSelection(){
	if (oZip.bagitemCount > 0){
		document.getElementById("bOrderMail").value = 1;	
		document.frmbagitem.sZipSelection.value = oZip.getbagitemIds();				
		document.frmbagitem.submit();	
	}else{
		alert("Please make selection by clicking on [Zip and Mail] checkbox before submit!");
	}	
}
function resetZipSelection(){
	//document.getElementById("divZip").innerHTML = oZip.showbagitems();
	//document.getElementById("divZip").visible = true;
	//alert("a");
	//alert(oZip.showbagitems());	
	document.getElementById("bZipAndMail").value = 0;	
	document.frmbagitem.sZipSelection.value = "";				
	oZip.removeAll();
	
	//alert(oZip.showbagitems());		
}

function goNext(iNextIndex){
	document.frmSearch.StartIndex.value = iNextIndex;				
	document.frmSearch.submit();	
}
function goNextAlpha(sNextAlpha){
	document.frmSearch.AlphaGroup.value = sNextAlpha;				
	document.frmSearch.submit();	
}


function submitZip(){
	if (oZip.bagitemCount == 0)
	{
		alert ('There are no files selected for zipping!');
		return false;
	}	
	else{
		document.zipandmail.SelectedFile.value = oZip.serialize();				
		document.zipandmail.submit();	
	}	
}
