/*	Form Validation Class
**
**	Created by: Daniel Hong
**	Last updated: 2007/04/06
**	
*/

function form_validate(form_obj){
	this.form_obj = form_obj;
	this.num_box_radio = 0;
	this.msg = "";
	this.response_obj = "res";
	this.required_cls_name = "req_field";
	
	this.getElementsByClassName = function(cl){
		var retnode = [];
		var myclass = new RegExp('\\b'+cl+'\\b');
		var elem = document.getElementsByTagName('*');
		
		for(i=0; i<elem.length; i++){
			var classes = elem[i].className;
			if (myclass.test(classes)) retnode.push(elem[i]);
		}
		return retnode;
	}
	
	this.validate = function(){
		reqFields = this.getElementsByClassName(this.required_cls_name);
		count = 0;
		
		for(i=0; i<reqFields.length; i++){
			if(reqFields[i].type == "radio" || reqFields[i].type == "checkbox"){
				if(reqFields[i].checked){
					count++;
				}
			}
			else{
				if(reqFields[i].value == ""){
					reqFields[i].style.cssText = "border:1px solid #d91414;";
					this.msg = "Please enter all required fields. Items in red are required.";
				}
				else{
					if(reqFields[i].name == "email" && !this.is_email(reqFields[i].value)){
						reqFields[i].style.cssText = "border:1px solid #d91414;";
						this.msg = "Please enter a valid email address.";
					}else{
						reqFields[i].style.cssText = "border:1px solid #abadb3;";
					}
				}
			}
		}
		
		if(count < (this.num_box_radio) ){
			this.msg = "Please enter all required fields. Items in red are required.";
		}
	}
	
	this.status = function(){
		if(this.msg ==""){
			document.getElementById(this.response_obj).innerHTML = this.msg;
			return true;
		}
		else{
			document.getElementById(this.response_obj).innerHTML = this.msg;
			return false;
		}
	}
	
	this.is_email = function(value){
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(value)){
			return true;
		}
		return false;
	}
}

