/***   JAVASCRIPT FUNCTIONS   ***/

var registrantNullFlds = {
	"salutation" : "Salutation",
	"first_name" : "First Name",
	"last_name" : "Last Name",
	"the_title" : "Title",
	"organization" : "organization", 
	"telephone" : "Telephone",
	"email" : "Email Address",
	"address_1" : "Address", 
	//"address_2" : "Address 2", 
	"city" : "City", 
	"province_state" : "Province / State", 
	"postal" : "Postal Code", 
	"country" : "Country"
};
var paymentNullFlds = {
	"card_type" : "Card Type",
	"card_number" : "Credit Card Number", 
	"security_number" : "Security Number", 
	"cardholder" : "Cardholder's Name",
	"expMonth" : "Expiration Month", 
	"expYear" : "Expiration Year" 
};
var registrantOpts = {
	"are_you" : "Are you an"
};
var registrantMeals = {
	"meal_choice" : "Meal Choice"
};

 
var memberNullFlds = {
	"salutation" : "Salutation",
	"first_name" : "First Name",
	"last_name" : "Last Name",
	//"the_title" : "Title",
	"organization" : "organization", 
	"telephone" : "Telephone",
	"email" : "Email Address",
	"address_1" : "Address", 
	"city" : "City", 
	"province_state" : "Province / State", 
	"postal" : "Postal Code" 
	//"country" : "Country"
};

 
//Validates the passed form.
function validatePayment(frm) {
	var bln = true;
	//alert(frm.card_type.value);
	if (frm.card_type.value == 'other') { return true; }
	else {
		try {
			//NULL VALIDATION
			if(typeof(paymentNullFlds) != "undefined")
				if(!validateNull(frm, paymentNullFlds))
					bln = false;
				if (bln == true) 
					bln = isValidCreditCard(frm.card_type.value, frm.card_number.value);
		}
		catch(e){ bln = false; alert('An error has occurred: \n'+e.message); }
		return bln;
	}
}
 
//Validates the passed form.
function validateRegistration(frm) {
	try {
		var bln = true;
		var message = '';
		if (frm) {
			var flds = { "registration_options" : "Registration Options" };
			for(key in flds) {
				var objFld = frm.elements[key];
				if(typeof(objFld) != 'undefined') {
					var cnt = -1;
					for (var i=objFld.length-1; i>-1; i--) {
						if (objFld[i].checked) { cnt=i; i=-1; }
					}
					if (cnt == -1) message = message + "Please select one of the '"+flds[key]+"'.\n";
				}
			}
			if(message != '') { alert(message); bln = false; }
		}
	}
	catch(e){ bln = false; alert('An error has occurred: \n'+e.message); }
	return bln;
}
 
//Validates the passed form.
function validateRegistrant(frm) {
	try {
		var bln = true;
		//NULL VALIDATION
		if(typeof(registrantNullFlds) != "undefined")
			if(!validateNull(frm, registrantNullFlds))
				bln = false;
			if (bln == true) 
				bln = validateEmail(frm.email);
			if (bln == true) 
				bln = validateRegistrantBtns(frm, registrantOpts);
	}
	catch(e){ bln = false; alert('An error has occurred: \n'+e.message); }
	return bln;
}

//Check radio button selections.
function validateRegistrantBtns(frm, flds) {
	var bln = true;
	var message = '';
	for(key in flds) {
		var objFld = frm.elements[key];
		if(typeof(objFld) != 'undefined') {
			var cnt = -1;
			for (var i=objFld.length-1; i>-1; i--) {
				if (objFld[i].checked) { cnt=i; i=-1; }
			}
			if (cnt == -1) message = message + "Please select one of the '"+flds[key]+"' options.\n";
		}
	}
	if(message != '') { alert(message); bln = false; }
	if(document.getElementById('are_you_other').checked) 
		if(trim(document.getElementById('identity_other').value) == '') {
			document.getElementById('identity_other').focus();
			alert("Specify the value for the 'Other' field."); 
			bln = false;
		}
	return bln;
}

//Check radio meal selection.
function validateRegistrantMeals(frm, flds) {
	var bln = true;
	var message = '';
	for(key in flds) {
		var objFld = frm.elements[key];
		if(typeof(objFld) != 'undefined') {
				var cnt = -1;
				for ( var i=objFld.length-1; i>-1; i-- ) {
					if ( objFld[i].checked) { cnt=i; i=-1; }
				}
				if (cnt == -1) message = message + "Please select one of the '"+flds[key]+"' options.\n";
			}
	}
	if(message != '') { alert(message); bln = false; document.getElementById('meal_choice_fish').focus();}
	return bln;
}

//Validates the passed form.
function validateMembership(frm) {
	try {
		var bln = true;
		var message = '';
		if (frm) {
			var flds = { "membership_options" : "Membership Type" };
			for(key in flds) {
				var objFld = frm.elements[key];
				if(typeof(objFld) != 'undefined') {
					var cnt = -1;
					for (var i=objFld.length-1; i>-1; i--) {
						if (objFld[i].checked) { cnt=i; i=-1; }
					}
					if (cnt == -1) message = message + "Please select one of the '"+flds[key]+"'.\n";
				}
			}
			if(message != '') { alert(message); bln = false; }
		}
	}
	catch(e){ bln = false; alert('An error has occurred: \n'+e.message); }
	return bln;
}
 
//Validates the passed form.
function validateMember(frm) {
	try {
		var bln = true;
		//NULL VALIDATION
		if(typeof(memberNullFlds) != "undefined")
			if(!validateNull(frm, memberNullFlds))
				bln = false;
			if (bln == true) 
				bln = validateEmail(frm.email);
	}
	catch(e){ bln = false; alert('An error has occurred: \n'+e.message); }
	return bln;
}



function getCost(this_id, multiplier, subtotal_id) {
	var qty = document.getElementById(this_id);
	var subtotal = document.getElementById(subtotal_id);
	var grand_subtotal = document.getElementById('grandSubtotal');
	var grand_HST = document.getElementById('grandHST');
	var grand_total = document.getElementById('grandTotal');
	var total_members = document.getElementById('totalMembers');
	if (qty && subtotal && multiplier) {
		subtotal.value = parseFloat(qty.value * multiplier);
		total_members.value = parseInt(getTotalMembers());
	}
	else {
		subtotal.value = 0.00;
	}
	if (grand_subtotal) {
		grand_subtotal.value = getSubtotals();
	}
	if (grand_HST) {
		grand_HST.value = parseFloat(grand_subtotal.value * 0.08);
	}
	if (grand_total) {
		grand_total.value = parseFloat(grand_subtotal.value) + parseFloat(grand_HST.value);
	}
}
 
function getSubtotals() {
	var total_early_ccrw = document.getElementById('totalCostEarlyCCRWMembers');
	var total_early_non = document.getElementById('totalCostEarlyNonMembers');
	var total_reg = document.getElementById('totalCostRegular');
	var subtotal = 0;
	if (total_early_ccrw) {
			subtotal = subtotal + parseFloat(total_early_ccrw.value);
	}
	if (total_early_non) {
			subtotal = subtotal + parseFloat(total_early_non.value);
	}
	if (total_reg) {
			subtotal = subtotal + parseFloat(total_reg.value);
	}
	return parseFloat(subtotal);
}
 
 
// ***************************************************//

//function trim(str) 
//{ 
//	var myStr = new String(str);
//	while (myStr.substring(0,1) == ' '){ myStr = myStr.substring(1, myStr.length); } 
//	while (myStr.substring(myStr.length-1, myStr.length) == ' '){ myStr = myStr.substring(0,myStr.length-1); } 
//	return myStr; 
//}

function trim(s) {
	return s.replace(/^\s+|\s+$/g,"");
}

function ltrim(s) {
	return s.replace(/^\s+/,"");
}

function rtrim(s) {
	return s.replace(/\s+$/,"");
}

function getFocus(obj_id) {
	var obj = document.getElementById(obj_id);
	if (obj)
		obj.focus();
}
 
function isCostZero(cost_id) {
	var cost = document.getElementById(cost_id);
	if (cost) {
		return (parseFloat(cost.value) <= 0);
	}
	else {
		return false;
	}
}

function getTotalMembers() {
	var reg = document.getElementById("regularRegistration");
	var ccrw_early = document.getElementById("CCRWMembersEarly");
	var non_early = document.getElementById("nonMembersEarly");
	var total = 0;
	if (reg) {
			total = total + parseInt(reg.value);
	}
	if (ccrw_early) {
			total = total + parseInt(ccrw_early.value);
	}
	if (non_early) {
			total = total + parseInt(non_early.value);
	}
	return total;
}

function isChecked(check_id) {
	var check_obj = document.getElementById(check_id);
	if (check_obj) {
		return check_obj.value != "";
	}
}
 

