//Tiny lib, formsubmitter (c)Alexander Melanchenko, http://alexnd.com

//validate and submit form
//constructor args:
// 1 - form name=.. attribute string,
// 2 - array of records (field name=.. attribute string, type string, message string)
// 3 - colors ('error:clean') for highlight fields backgrounds, or 0 if not used
// 4 - colors ('error:clean') for highlight fields texts, or 0 if not used
// 5 - message type 0: none, 1: alert and focus, 2 : only alert, 3: custom function (place function reference here)
//types are: options text number email
function formsubmitter()
{
	this.formname = '';
	this.fields = new Array();
	this.bgcolor_error = '#FFD2D2';
	this.bgcolor_clean = '#FFFFFF';
	this.color_error = '#FF0000';
	this.color_clean = '#000000';
	this.use_bgcolor_change = 1;
	this.use_color_change = 0;
	this.message_type = 1;
	this.message_handler = null;
	this.form_invalid = 0;
	this.message_form_invalid_type = 0;
	this.message_form_invalid = "form invalid";
	this.form_invalid_message_handler = function(){};
	
	if('undefined'!=typeof arguments[0]) this.formname = arguments[0];
	
	if('undefined'!=typeof arguments[1]) this.fields = arguments[1];
	
	if('undefined'!=typeof arguments[2]){
		if(arguments[2]==0)
			this.use_bgcolor_change = 0;
		else{
			this.use_bgcolor_change = 1;
			if('string' == typeof arguments[2])
			{
				var t=arguments[2].split(':');
				if(undefined!=t[0]) this.bgcolor_error = t[0];
				if(undefined!=t[1]) this.bgcolor_clean = t[1];
			}
		}
	}
	
	if('undefined'!=typeof arguments[3]){
		if(arguments[2]==0)
			this.use_color_change = 0;
		else{
			this.use_color_change = 1;
			if('string' == typeof arguments[3])
			{
				var t=arguments[2].split(':');
				if(undefined!=t[0]) this.color_error = t[0];
				if(undefined!=t[1]) this.color_clean = t[1];
			}
		}
	}
	
	if('undefined'!=typeof arguments[4])
	{
		if('function' == typeof arguments[4]){
			this.message_handler = arguments[4];
			this.message_type = 2;
		}else if('number' == typeof arguments[4]){
			this.message_type = arguments[4];
		}else{
			this.message_type = 1;
		}
	}
	
	this.submit = function()
	{
		var f=document.forms[this.formname],er=this.check_fields_errors();
		if(this.form_invalid){
			return this.throw_message_form_invalid();
		}else if(er.length>0){
			return this.throw_message(er);
		}else f.submit();
	}

	this.throw_message = function(m)
	{
		if(this.message_type==1 || this.message_type==2)
		{
			var s='';
			for(var i=0; i<m.length; i++){
				s+=m[i][1]+'\n';
			}
			alert(s);
			if(this.message_type==1) document.forms[this.formname].elements[m[0][0]].focus();
			return null;
		}else if(this.message_type==3){
			if('function' == typeof this.message_handler){
				return this.message_handler(this.formname, m[0][0]);
			}
			return null;
		}else{
			return m;
		}
	}

	this.throw_message_form_invalid = function()
	{
		if(this.message_form_invalid_type==1)
		{
			if('function' == typeof this.form_invalid_message_handler){
				return this.form_invalid_message_handler(this.formname, this.message_form_invalid);
			}
			return null;
		}else{
			alert(this.message_form_invalid);
			return null;
		}
	}
	
	this.check_fields_errors = function()
	{
		var s=0, r=new Array(), f=document.forms[this.formname];
		if(undefined!=f){
		for(var i=0; i<this.fields.length; i++)
		{
			s=0;
			if(this.fields[i][1]=='options'){
				if( f.elements[this.fields[i][0]].options[f.elements[this.fields[i][0]].selectedIndex].value<1 ) s=1;
			}else if(this.fields[i][1]=='text'){
				if( f.elements[this.fields[i][0]].value=='' ) s=1;
			}else if(this.fields[i][1]=='number'){
				if( f.elements[this.fields[i][0]].value=='' ) s=1;
			}else if(this.fields[i][1]=='email'){
				if( !(f.elements[this.fields[i][0]].value!='' && f.elements[this.fields[i][0]].value.indexOf('@')>1) ) s=1;
			}
			if(s){
				var m=new Array();
				m[0]=this.fields[i][0];
				m[1]=this.fields[i][2];
				r[r.length]=m;
				this.set_field_error(this.fields[i][0]);
			}else{
				this.clean_field_error(this.fields[i][0]);
			}
		}}else this.form_invalid=1;
		return r;
	}
	
	this.set_field_error = function(n)
	{
		if(this.use_bgcolor_change) document.forms[this.formname].elements[n].style.backgroundColor=this.bgcolor_error;
		if(this.use_color_change) document.forms[this.formname].elements[n].style.color=this.color_error;
	}
	
	this.clean_field_error = function(n)
	{
		if(this.use_bgcolor_change) document.forms[this.formname].elements[n].style.backgroundColor=this.bgcolor_clean;
		if(this.use_color_change) document.forms[this.formname].elements[n].style.color=this.color_clean;
	}

}