	
/** Payment Form Functions */

function showItem(item) {
	document.getElementById(item).disabled=false;
	document.getElementById('payform_'+item+'_title').style.display='inline';
	document.getElementById('payform_'+item+'_body').style.display='inline';
}
function hideItem(item) {
	document.getElementById(item).disabled=true;
	document.getElementById('payform_'+item+'_title').style.display='none';
	document.getElementById('payform_'+item+'_body').style.display='none';
}

/** Other Useful Functions */

/**
 * Enter an Id or Element, and this function will hide it
 */
function hide(item) {
	try  {
		// get the element if an id was passed
		if (typeof(item) == "string") {
			item = $(item);
		}
		item.style.display = 'none';
	}
	catch (e) {
		// suppress errors
		alert("hide(): " + e);
	}
}

/** 
 * Enter an Id or Element and this function will show it (as an inline block)
 */
function show(item, type) {
	try  {
		// default show to block type
		if (type == null) {
			type = "block";
		}
		// get the element if an id was passed
		if (typeof(item) == "string") {
			item = $(item);
		}
		//debugObj(item);
		item.style.display = type;
	}
	catch (e) {
		// suppress errors
		//alert("show(): " + e);
	}
}

/** 
 * If the Element is Visible this will Hide it
 * If not visible then it will show it
 */
function toggleDisplay(element, type) {

	try {
		if (typeof(element) == "string") {
			element = $(element);
		}
		if (element.style.display == "none") {
			show(element, type);
		}
		else {
			hide(element);
		}
	}
	catch(e) {
		alert(typeof(element));
	}
}

/**
 * changes one class for another
 */
function switchClass(element, c1, c2) {
	try {
		if (typeof(element) == "string") {
			element = $(element);
		}
		if (element.hasClassName(c1)) {
			element.removeClassName(c1);
			element.addClassName(c2);
		}
		else if (element.hasClassName(c2)) {
			element.removeClassName(c2);
			element.addClassName(c1);
		}
	}
	catch(e) {
		alert("" + typeof(element) + ": " + element);
	}
}

/**
 * adds a charge item
 */
function toggleCharge(charge, toggle) {
	try {
		switchToCharge();
		
		// Hide the Fee Field
		hideItem('fee');
		
		// get the Amount fields
		obj = $('amount');
		
		v1 = parseFloat(obj.value);
		charge = parseFloat(charge);
		
		if(toggle) {
			obj.value = v1 + charge;
		}
		else{
			obj.value = v1 - charge;
		}
	}
	catch (ex) {
		alert(ex);
	}
}

/**
 * adds a refund item
 */
function toggleRefund(refund, fee, toggle) {
	try {
		switchToRefund();
		
		// ensure The Fee field is show
		showItem('fee');
		
		// get current amounts
		obj = $('amount'); 
		fobj = $('fee');
		v1 = parseFloat(obj.value);
		f1 = parseFloat(fobj.value);
		
		if (toggle) {
			obj.value = v1 - refund;
			fobj.value = f1 - fee;
		}
		else{
			obj.value = v1 + refund;
			fobj.value = f1 + fee;
		}
	}
	catch (ex) {
		alert(ex);
	}
}

/** gets all the variables in a form for ajax submission */
function calcCharge() {
	try {
		var value = 0;
		var items = getItemsChecked('select-class-form', true);
		var input = null;
		//debugObj(items.length);
		for (var idx=0; idx<items.length; idx++) {
			input = items[idx];
			//alert(idx + ', ' + items.length + ' $' + input.value);
			value += parseFloat(input.value);
		}
		$('topay').innerHTML = '$' + Math.floor(value*100)/100;
	}
	catch (ex) {
		alert("switchToRefund(): " + ex);
	}
}

/** gets all the variables in a form for ajax submission */
function switchToCharge() {
	try {
		found = setItemsChecked("payform", false, /^refund_[0-9]+$/);
						
		// clear payment amount
		if (found) {
			$('amount').value = 0;
			$('fee').value = 0;
			$("add_payment_button").value = "Add Charge";
		}
	}
	catch (ex) {
		alert("switchToRefund(): " + ex);
	}
}

/** gets all the variables in a form for ajax submission */
function switchToRefund() {
	try {
		found = setItemsChecked("payform", false, /^pay_[0-9]+$/);
						
		// clear payment amount
		if (found) {
			$('amount').value = 0;
			$('fee').value = 0;
			$("add_payment_button").value = "Add Refund";
		}
	}
	catch (ex) {
		alert("switchToRefund(): " + ex);
	}
}

/**
 * gets the input elements of the given status in the given form 
 * that match the associated regex
 * 
 * returns the items that match
 */
function getItemsChecked(myform, status, regex) {
	var items = new Array();
	
	try {
			
		// ensure we have an object here
		if (typeof(myform) == "string") {
			myform = $(myform);
		}
		
		// set defaults
		if (status == null) {
			status = false;
		}
		if (regex == null) {
			regex = /.?/;
		}

		for (i in myform.elements) {
			input = myform.elements[i];
			//alert(input);
			
			// get all refund items
			if (input != null && regex.test(input.id)) {
				
				// uncheck them
				if (input.checked == status) {
					items.push(input);
				}
			}
		}
	}
	catch (ex) {
		alert("getItemsChecked(): " + ex);
	}
	return items;
}

/**
 * sets the input elements to the given status in the given form 
 * that match the associated regex
 * 
 * returns the number of items changed
 */
function setItemsChecked(myform, status, regex) {
	var count = 0;
	
	try {
			
		// ensure we have an object here
		if (typeof(myform) == "string") {
			myform = $(myform);
		}
		
		// set defaults
		if (status == null) {
			status = false;
		}
		if (regex == null) {
			regex = /.?/;
		}

		for (i in myform.elements) {
			input = myform.elements[i];
			//alert(input);
			
			// get all refund items
			if (input != null && regex.test(input.id)) {
				
				// uncheck them
				if (input.checked != status) {
					input.checked = status;
				
					// found another
					count += 1;
				}
			}
		}
	}
	catch (ex) {
		alert("setItemsChecked(): " + ex);
	}
	return count;
}