
/**************************************************
 *                                                *
 * Written by: kent lynch                         *
 *                                                *
 **************************************************/
// NOTE CHECKBOX FIELD NAMING CONVENTION:
// check box form field naming convention: item_id1, item_id2, item_id3... (the checkboxname woudl be 'item_id')

//	total all checked boxes and save/return total 
function addTotal(theform,checkboxname,looptotal,unitprice,totalfieldname){
	var totalprice = 0.00;
	var theprice = parseInt(unitprice,10);
	var listids = "";
	// loop thru all checkbox's and add to total price if checkbox is checked
	for (var i=1; i <= looptotal; i++){
		if(theform[checkboxname+i]){		
			if(theform[checkboxname+i].checked){
				totalprice = totalprice + theprice;
				if(listids > ""){
					listids += ","
				}
				listids += String(theform[checkboxname+i].value); // this will start building up a list of items checked this works best if you use a primary key as the checkbox's value
			}
		}
	}
	var dollarprice =  formatCurrency(totalprice); 
	if(totalfieldname){
		theform[totalfieldname].value = dollarprice;
	}
	else{
		return dollarprice;
	}
}

// check all checkboxes and save/return a list of all the values
function checkAll(theform,looptotal,checkboxname,listfieldname){
	var thetotal = 0;
	var listids = "";
	for (var i=1; i <= looptotal; i++) {
		if(theform[checkboxname+i]){
			theform[checkboxname+i].checked=1;
			if(listids > ""){
				listids += ",";
			}
			listids += String(theform[checkboxname+i].value);
		}
	}
//		alert(listids);
	if(listfieldname){
		theform[listfieldname].value=listids;
	}
	else{
		return listids;
	}
}
// loop through all boxes and UNcheck them
function uncheckAll(theform,looptotal,checkboxname,listfieldname) {
	var emptylist = "";
	for (var i=1; i <= looptotal; i++) {
		if(theform[checkboxname+i]){
			theform[checkboxname+i].checked = 0;
		}
	}
	if(listfieldname){
		theform[listfieldname].value = "";
	}
	else{
		return emptylist;
	}
}
//	save/return list of all checked checkbox values
function checkboxToList(theform,checkboxname,looptotal,listfieldname){
	var listids = "";
	// loop thru all checkbox's and add to total price if checkbox is checked
	for (var i=1; i <= looptotal; i++){
		if(theform[checkboxname+i]){		
			if(theform[checkboxname+i].checked){
				if(listids > ""){
					listids += ","
				}
				listids += String(theform[checkboxname+i].value); // this will start building up a list of items checked this works best if you use a primary key as the checkbox's value
			}
		}
	}
	if(listfieldname){
		theform[listfieldname].value = listids;
	}
	else{
		return listids;
	}
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) 
		num = "0";
		cents = Math.floor((num*100+0.5)%100);
		num = Math.floor((num*100+0.5)/100).toString();
		if(cents < 10) cents = "0" + cents;
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
				num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
			return ("$" + num + "." + cents);
}

