/*
	
	Reliant Realtor Referral
	JavaScript functions
	
	Auth:		Andrew Hamel - TorqueFKM
	Version:	1.0
	Created:	2008-06-11

*/

function check_frm(frm_id,params){
	/*
	params are comma separated and grouped by colons:
	field ID:field type:error
	
	Types
	------------------------------------------------
		text 			source field(string) : type : error(string)
		select			source field(string) : type : error(string)
		checkbox		source field(string) : type : error(string)
		radio			source field(string) : type : error(string)
		comp (compare)	source field(string) : type : error(string) : compare field(string)
	
	
	Ex.:  check_frm('user:text:Please enter your user name','region:select:Please select a region','email_check:comp:Your emails dont match:email');
	
	*/
	
	var error = 0;
	var error_msg = "There was an error with your submission:\n\n";
	var frm = document.getElementById(frm_id);
	
	var p = params.split(",");
	
	// Loop through the list
	for(i=0; i<p.length; i++){
		// split on the colon (:)
		var o = p[i].split(":");
			
		var thisfield = o[0];
		var thistype = o[1];
		var thismsg = o[2];
		
		switch(thistype){ // generate based on field type
			case "text" :
				
				if(frm[thisfield].value==""){ error = 1; error_msg += "- " + thismsg + ".\n"; }
				
			break;
			case "email" :
				
				if(frm[thisfield].value==""){ error = 1; error_msg += "- " + thismsg + ".\n"; }
				if(frm[thisfield].value!=""){
					if(frm[thisfield].value.indexOf("@")==-1 || frm[thisfield].value.indexOf(".")==-1){ error = 1; error_msg += "- Please enter a valid email address.\n"; }	
				}
				
			break;
			case "select" :
				
				if(frm[ frm[thisfield].selectedIndex ].value==""){ error = 1; error_msg += "- " + thismsg + ".\n"; }
				
			break;
			case "checkbox" :
			case "radio" :
				
				var ct = frm[thisfield].length;
				var checked = 0;
				
				for(j=0; j<ct; j++){
					if(frm[thisfield][j].checked==true){ checked += 1; }
				}
				
				if(checked == 0){
					error = 1; error_msg += "- " + thismsg + ".\n";
				}
				
			break;
			case "comp" :
				
				var thiscomp = o[3];
				
				if(frm[thisfield].value!=frm[thiscomp].value){ error = 1; error_msg += "- " + thismsg + ".\n"; }
				
			break;
		}
		
	}
	
	error_msg += "\nPlease correct the error(s) above and resubmit the form.\n";
	
	if(error==1){
		alert(error_msg);
		return false;
	}else{
		return true;	
	}
}
