var form_xml
var form_node_arr=new Array()
var form_node_id_arr=new Array()	

function form_class(par_node,par_listpos,par_reg_panel_ob){
	
	var _me=this
	this.node=par_node

	this.form_name=this.node.getAttribute("name")
	this.form_element_node_arr=new Array()
	this.form_element_node_id_arr=new Array()
	this.submit_button_name=this.node.getAttribute("submit_name")
	this.submitted=0
	this.has_reg_panel=0
	this.submit_button_default_value=""

	if(typeof par_reg_panel_ob!="undefined" && !!par_reg_panel_ob){
		this.reg_panel_ob=par_reg_panel_ob
		this.has_reg_panel=1
		
	}

	this.get_form_elements=function(){
		if(_me.has_reg_panel==1){
			_me.reg_panel_ob.form_ob_ready(_me)
		}
		var el_nodes=_me.node.selectNodes("./field")
		if(el_nodes.length>0){
			for(var i=0;i<el_nodes.length;i++){
				//CHECK TO SEE IF AN ELEMENT EXISTS
				var el_name=el_nodes[i].getAttribute("name")
				var ob=document.forms[_me.form_name].elements[el_name]
				if(typeof ob!="undefined" && !!ob){
					var ob_type=ob.type
					if(typeof ob_type=="undefined" || !ob_type){
						var ob_type="radio"
					}
				
					_me.form_element_node_arr[el_name]=new form_element_class(el_nodes[i],ob_type,_me,_me.form_element_node_id_arr.length)
					_me.form_element_node_id_arr[_me.form_element_node_id_arr.length]=el_name
					_me.form_element_node_arr[el_name].setup_interface()
				}
																					   
			}
		}
		
		//AND GIVE SAVE BUTTON AN ACTION
		if(typeof document.forms[_me.form_name].elements[_me.submit_button_name]!="undefined" && !!document.forms[_me.form_name].elements[_me.submit_button_name]){
			
			document.forms[_me.form_name].elements[_me.submit_button_name].onclick=function(){_me.validate();return document.can_submit;}
			_me.submit_button_default_value=document.forms[_me.form_name].elements[_me.submit_button_name].value
		}
		
		window.onbeforeunload=_me.check_if_changed
		
		//window.attachEvent("onbeforeunload",function(event){_me.check_if_changed(event)})
		
		//window.onbeforeunload = unloadHandler; 
		
	}
	
	this.reset_form_elements_to_start=function(){
		if(_me.form_element_node_id_arr.length>0){
			for(var i=0;i<_me.form_element_node_id_arr.length;i++){
				_me.form_element_node_arr[_me.form_element_node_id_arr[i]].reset_to_start()
			}
		}
	}
	
	
	this.validate=function(){
		
		document.can_submit=false
		var errstr=""
		
		if( document.body.className=="yui-skin-sam"){
			
			if(typeof myEditor2!="undefined" && !!myEditor2){
		
				myEditor2.saveHTML()
			}
		}
					
		if(_me.form_element_node_id_arr.length>0){
			for(var i=0;i<_me.form_element_node_id_arr.length;i++){
				errstr+=_me.form_element_node_arr[_me.form_element_node_id_arr[i]].validate()
			}
		}

		if(typeof criteria_node_id_arr!="undefined" && !!criteria_node_id_arr){
			var not_valid_count=0
			
			for(var i=0;i<criteria_node_id_arr.length;i++){
				//alert("crit"+criteria_node_id_arr[i])
				if(typeof criteria_node_arr["crit"+criteria_node_id_arr[i]]!="undefined" && !!criteria_node_arr["crit"+criteria_node_id_arr[i]]){
					not_valid_count+=criteria_node_arr["crit"+criteria_node_id_arr[i]].check_validation()
				}
			}
			
			if(not_valid_count>0){
				errstr+="Required criteria have been highlighted"
			}
		}
		
		if(_me.has_reg_panel==0){
			if(errstr!=""){
				alert(errstr)
			}else{
				document.forms[_me.form_name].confirmsubmit.value=1
				_me.submitted=1
			}
			document.can_submit=(errstr=="")
		}else{
			_me.reg_panel_ob.form_validation_result(errstr)
		}
	}
	
	this.check_if_changed=function(){

		if(_me.form_name!="login_form"){
			
			var change_count=0
			
			var changed_elements=_me.node.selectNodes("//*[@may_have_changed=1]").length
			
			if(xmlob_loaded_arr["criteria"]==1){
				var changed_criteria=criteria_xml.selectNodes("//*[@may_have_changed=1]").length
			}else{
				var changed_criteria=0
			}
			if(typeof document.forms[_me.form_name].change_count!="undefined" && !!document.forms[_me.form_name].change_count){
				change_count=parseInt(document.forms[_me.form_name].change_count.value)
			}
			
			change_count=change_count+changed_elements+changed_criteria
			if(change_count>0 && _me.submitted==0){
				return("Your changes have not been saved.\n\nIf you leave the page without saving, your changes will be lost.")
				
			}
		}
		
	}
}
	
