var CFGroup = false;
var pcb;

function addEachForm2Cart(thisForm,obj){ //alert("addEachForm2Cart")
	nDocFr = document.forms.length;
	for(l=0;l<nDocFr;l++) { 
		f = document.forms[l]; 
		if(f.CHECKBOX && f.ID_NUM && f.PRICE && f.QUANTITY && f.ITOTAL){
			if(f.CHECKBOX.checked == true){ AddToCart(f); }
		}
	}
	thisForm.submit();
}

// return true/false of checkbox
function returnCheckbox(obj){  //alert("returnCheckbox")
	if(obj.checked)
		return true;
	else
		return false;
}

// checkbox checked
function checkedBox(thisForm,obj){  //alert("checkedBox")
	checked = returnCheckbox(obj);  
	parentForm = getParent(thisForm);
	if(parentForm != false)
		parentName = parentForm.name;
	if(!checked){ 
		thisForm.QUANTITY.value = ""; 
		totalPrice(thisForm);
		if(parentForm != false)
			setParent(parentForm,false);
	}else{ 
		thisForm.CHECKBOX.value = checked; 
		if(thisForm.QUANTITY.value != "") return; 
		thisForm.QUANTITY.value = 1;  
		totalPrice(thisForm); 
		if(parentForm != false){
			returnC = checkChildCheckbox(parentForm);
			setParent(parentForm,returnC);
		}
	}
}

// checked/uncheck child members
function checkGroup(parentForm, pcb){ //alert("checkGroup")
	childMember = parentForm.name +"__"; 
	nDocFr = document.forms.length;
	
	for(j=0;j<nDocFr;j++) {  
		f = document.forms[j];  
		if(f.name.substr(0,childMember.length) == childMember){ 
			if(pcb.checked == false){  
				parentForm.QUANTITY.value = "";
				f.CHECKBOX.checked = false; 
				f.QUANTITY.value = ""; 
				setChildValue(f);
			}else{ 
				parentForm.QUANTITY.value = 1;
				if(!f.QUANTITY.value){
					f.CHECKBOX.checked = true;
					f.QUANTITY.value = 1;  
					setChildValue(f); 
				}
			}
		}
	}
}

function setChildValue(thisForm){ 
	parentForm = getParent(thisForm);
	parentName = parentForm.name;
	var QItem = thisForm.QUANTITY.value; //alert(QItem);
	var TIPrice = thisForm.PRICE.value; //alert(TIPrice); 
	var TPrice = decimal(eval(QItem * TIPrice));   
	if(TPrice == "0.00" || !TPrice ){ 
		TPrice = ""; 
		thisForm.QUANTITY.value=""; 
		thisForm.CHECKBOX.checked = false;
		setParent(parentForm,false);
	}else{
		thisForm.CHECKBOX.checked = true
	} 
	return thisForm.ITOTAL.value = TPrice;
}

function totalPrice(thisForm){ //alert("totalPrice");
	parentForm = getParent(thisForm);
	if(parentForm != false)
		parentName = parentForm.name;
	var QItem = thisForm.QUANTITY.value; 
	var TIPrice = thisForm.PRICE.value; 
	var TPrice = decimal(eval(QItem * TIPrice));   
	if(TPrice == "0.00" || !TPrice ){ 
		TPrice = ""; 
		thisForm.QUANTITY.value=""; 
		thisForm.CHECKBOX.checked = false;
		if(parentForm != false) 
			setParent(parentForm,false);
	}else{
		thisForm.CHECKBOX.checked = true
		if(parentForm != false){
			returnC = checkChildCheckbox(parentForm);
			setParent(parentForm,returnC);
		}
	} 
	return thisForm.ITOTAL.value = TPrice;
}

function getParent(childForm){ 
	if(!document.forms[childForm.PARENT.value]) return false; //alert(document.forms[childForm.PARENT.value]);
	if(childForm)
		return document.forms[childForm.PARENT.value]; //childForm.PARENT.value;
}

function setParent(parentForm,value){ //alert(value)
	if(value == false){
		parentForm.CHECKBOX.checked = false;
		parentForm.QUANTITY.value = "";
	}else{
		parentForm.CHECKBOX.checked = true;
		parentForm.QUANTITY.value = 1;
	}
}

// return child checkbox value 
// return false
function checkChildCheckbox(parentForm){
	parentName = parentForm.name; 
	childMember = parentName +"__";  
	nDocFr = document.forms.length;

	for(k=0;k<nDocFr;k++) { 
		f = document.forms[k]; 
		if(f.name.substring(0,childMember.length) == childMember){ //alert(f.name.substring(0,childMember.length));
			if(f.CHECKBOX.checked == false)
				return false;
		}
	}	
}

function decimal(num) {
	string = "" + num;
	if (string.indexOf('.') == -1)
		return string + '.00';
	seperation = string.length - string.indexOf('.');
	if (seperation > 3){ 
		rd = Math.round("."+string.substring(string.length-seperation+3));
		if(rd > 0){
			string =  eval(string.substring(0,string.length-seperation+3))+.01;
			return string;
		}else{ return string.substring(0,string.length-seperation+3) };
	}else if (seperation == 2){ 
		return string + '0';
	}else if(seperation == 1){ 
		return string + '00';
	}

	return string;
}


function isDigit(ch) { //alert("isDigit")
   if (ch >= '0' && ch <= '9')
      return true;
   return false;
}

function validateDigit(obj) { //alert("validateDigit")
   value = obj.value;
   for (n = 0; n < value.length; n++)
      if ( ! isDigit(value.charAt(n))) {
         alert("Field must be numeric");
         return;
      }
}
