/******************************
Javascript form validation
RibbitflightSchool.com
Last modified 8-13-2008
******************************/
/******************************
FORM VALIDATION
******************************/

/*main form validation -- below is an example of how to use the fields
textfields and emailfields are 2d arrays where the first position is backend field name
and the 2nd position is friendly field name for display
and the 3d position is the optional default text that may be pre existing in the field, which it can also not equal
<script>
	textfields = Array(
		['first_name', 'First Name', 'FIRST NAME'],
		['last_name', 'Last Name', 'LAST NAME'],
		['country', 'Country',''],
		['zip', 'Zip','']
	);
	emailfields = Array(
		['email', 'Email','']
	);
</script>

onsubmit="return formalize(this, textfields, emailfields)" 

*/
var formalize_alert = "";
function formalize(frm, textfields, emailfields) {
	formalize_alert = "";
	var num = 0;
	
	num += formalize_text(frm, textfields,num);
	num += formalize_email(frm, emailfields,num);

	if (formalize_alert != "") {
		alert(formalize_alert);
		return false;
	} else {
		return true;
	}
}

function formalize_text(frm, fields,num){
	with(frm) {
		var len = fields.length;
		for(var i=0; i<len; i++) {
			var fieldval = frm[fields[i][0]].value;
			if(!fieldval){
				formalize_alert += (++num) + ". Please enter " + fields[i][1] + "\n";
			}
		}
	}
	return num;
}

function formalize_email(frm, fields,num){
	var email_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	with(frm) {
		var len = fields.length;
		for(var i=0; i<len; i++) {
			var fieldval = frm[fields[i][0]].value;
			if(!fieldval){
				formalize_alert += (++num) + ". Please enter " + fields[i][1] + "\n";
			} else {
				if (!email_filter.test(fieldval)){
					formalize_alert += (++num) + ". You have entered an invalid email.\n";
				}
			}
		}
	}
	return num;
}

//checks two fields and see if they are the same -- good for password confirmation
//process blanks determines if you want to have the form return true if there are blanks or not
function formalize_fieldsmatch(frm,field1,field2,processblanks) {
	if (processblanks=="" || processblanks==undefined) processblanks = false;
	if (frm[field1].value == "" && frm[field2].value == "") {
		return processblanks;
	} else if (frm[field1].value==undefined && frm[field2].value==undefined) { 
		return processblanks;		
	} else if (frm[field1].value != frm[field2].value) {
		alert("Your fields don't match!");
		return false;
	} else {
		return true;	
	}
	
}

//function prompts people if they are sure to carry out the action
function formalize_areyousure(msg){
	if (msg=="" || msg==undefined) msg="Are you sure you wish to continue?  This cannot be undone.";
	
	var agree=confirm(msg);
	if (agree)
		return true ;
	else
		return false ;
}


function formalize_deleteid(frm, idval, id, msg) {
	if (id=="" || id==undefined) id = "del";
	var cont = formalize_areyousure(msg);
	if (cont) { 
		frm.elements[id].value = idval;
		frm.submit();
	}
}

// gets the checked value from a radio button
function formalize_getCheckedValue(radioObj) {
	if(!radioObj) return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

//clears a form - skips is an optional array of keys to skip
//skipfields = Array('submit','clear','cancel');
function clearform(frm,skips) {
	//alert(frm.name + "-" + frm.elements.length);
	var skipslen = (skips=="" || skips==undefined) ? 0 : skips.length;
	with(frm) {
		var len = frm.elements.length;
		
		for(var i=0; i<len; i++) {
			var clearit=true;
			for(var ii=0; ii<skipslen; ii++) {
				if (frm.elements[i].name == skips[ii]) clearit=false;
			}
			if (clearit) frm.elements[i].value = "";
		}
	}
}
