/*
Syntax:
	
	var obj = new FormValidator()
 	form_validator.set_message_id("element_id_som_mottar_feilmelding")
	obj.add_input("input_name","Melding når test mislykes",/rexexp_value_test/,"element_id_som_mottar_form_warning")


Eksempel:

	<script type="text/javascript">	
	 	var form_validator = new FormValidator()
	 	form_validator.set_message_id("form_warning")
	 	form_validator.add_input("Registrant.name","Navn er et påkrevet felt.",/./,"name_row")
	 	form_validator.add_input("Registrant.address","Sted må fylles inn.",/./,"place_row")
		
	 	var contact = form_validator.add_input("Registrant.phone","Telefon eller epost må fylles inn.",/./,"tel_row")
// Boolean phone OR epost: (standard er boolean AND)
	 	contact.add_input("Registrant.email","Epost adressen ser ikke helt riktig ut!",/^.+@.+$/,"email_row")
	 	form_validator.add_input("Registrant.lunch","Du må svare på om du vil bli med på galla middag.","checked","lunsj_row")
		form_validator.add_input("Registrant.confirmed","Du må bekrefte at uteblivelse uten beskjed belastes 300,-","checked","confirm_row")
	</script>

	<form onsubmit="return form_validator.validate(this)" method="post">
		<div id="tel_row">Telefon* <input type="text"  name="Registrant.phone" /></div>
		<input type="button" onclick="form_validator.check_submit(document.forms[0])" value="Registrer" />	
	</form>

Validator feedback i block med id form_warning:
	<div class="status_message error hidden" id="form_warning"></div>
	
*/


function FormValidator(el) {
	this.msg_id=null
	this.form=null
	this.inputs=new Array()
	
	this.validate = function(el) {
		this.form = el
		this.retval = true;
		this.textval = "";		
		for(var n=0;n<this.inputs.length;n++) {		
			input = this.inputs[n]			
			if (this.form[input.name].type == "checkbox") {
				if (!this.check_checkbox(input)) {
					this.retval = false			
				}
			} else if (this.form[input.name].length) {
				if (!this.check_radio(input)) {
					this.retval = false			
				}
			} else {
				if (!this.check_input(input)) {
					this.retval = false			
				}			
			}
		}
		
		if (!this.retval) {
			if (txt = document.getElementById(this.msg_id)) {
				txt.style.display = "block"
				txt.innerHTML = this.textval
			}
		}
		return this.retval
	}
	this.check_submit = function(a_form) {
		if (this.validate(a_form)) {
			a_form.submit()
		}
	}
	this.check_checkbox = function(input) { 
		if (input_el = this.form[input.name]) {
			if (!input_el.checked) {
				if (target = document.getElementById(input.target)) {
					target.className = "form_warning"
				}
				if (input.or_valid && this.check_input(input.or_valid)) {} else {
					this.textval += input.message+"<br />"
					return false
				}
			} else if (target = document.getElementById(input.target)) {
				target.className = ""
			}
		}	
		return true
	}
	this.check_radio = function(input) { 
		var retval = false
		if (input_el = this.form[input.name]) {
			for (var i=0; i < input_el.length; i++) {				
				if (input_el[i].checked) {
					retval = true
				}								
			};			
		}	
		if (!retval) {
			if (target = document.getElementById(input.target)) {
				target.className = "form_warning"
			}
			if (input.or_valid && this.check_input(input.or_valid)) {} else {
				this.textval += input.message+"<br />"
				return false
			}
		} else if (target = document.getElementById(input.target)) {
			target.className = ""
		}		
		return retval
	}
	this.check_input = function(input) { 
		if (input_el = this.form[input.name]) {
			if (!new String(input_el.value).match(input.condition)) {
				if (target = document.getElementById(input.target)) {
					target.className = "form_warning"
				}
				if (input.or_valid && this.check_input(input.or_valid)) {} else {
					this.textval += input.message+"<br />"
					return false
				}
			} else if (target = document.getElementById(input.target)) {
				target.className = ""
			}
		}	
		return true
	}

	this.set_message_id = function(id) {
		this.msg_id = id
	}
	this.add_input = function(name,message,condition,target) {
		valid =  new valid_input(name,message,condition,target)
		this.inputs[this.inputs.length] = valid
		return valid
	}
	
}

function valid_input(input_name,message,condition,marked_target_id) {
		this.name = input_name
		this.message = message
		this.condition = condition
		this.target = marked_target_id
		
		this.or_valid = null
		this.add_input = function(name,message,condition,target) {
			valid = new valid_input(name,message,condition,target)
			this.or_valid = valid
			return valid
		}
}
