var t = Array();

t['login'] = Array();
t['login']['regex'] = "\/^[a-zA-Z0-9 -]{4,12}$\/";
	
t['password'] = Array();
t['password']['regex'] = "\/^[a-zA-Z0-9]{6,12}$\/";
	
t['email'] = Array();
t['email']['regex'] = "\/^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,5}$\/";
t['email']['min'] = 5;
t['email']['max'] = 100;

t['comm_title'] = Array();
t['comm_title']['min'] = 2;
t['comm_title']['max'] = 100;

t['comm_content'] = Array();
t['comm_content']['min'] = 5;

t['contact_title'] = Array();
t['contact_title']['min'] = 2;
t['contact_title']['max'] = 100;

t['contact_content'] = Array();
t['contact_content']['min'] = 5;

t['contact_to'] = Array();
t['contact_to']['regex'] = "\/^[a-zA-Z0-9]{4,12}$\/";


function getLabel (input)
{
	var e = document.getElementsByTagName("label");
	
	for( var i=0 ; i<e.length ; i++)
	{
		if ( e[i].getAttribute("for") == input.name || input.id == e[i].getAttribute("for") ) return e[i];
	}

	return false;
}

function importRegExp(str) 
{
	str = str.substring(1);
	var pos = str.lastIndexOf('/');
	var options = str.substring(pos + 1);
	str = str.substr(0, pos);
	var reg = new RegExp(str, options);
	
	return reg;
}	

function input_verify (input) 
{
	var result = 'good';	
	var type  = input.name;
	var content;
	
	if ( input.type != "submit" )
	{
		if ( t[type] )
		{
			if ( (t[type]['min'] && input.value.length < t[type]['min']) || (t[type]['max'] && input.value.length > t[type]['max']) )
				result = 'size';
			if ( t[type]['regex'] && !input.value.match(importRegExp(t[type]['regex'])) )
				result = 'regex';
			if ( t[type]['checked'] && !input.checked )
				result = 'check';
		}
		
		if ( result != 'good' )
		{
			input.style.borderColor = 'red';
			
			if ( getLabel(input) )
				getLabel(input).style.color = 'red';	
		}
		else
		{
			input.style.borderColor = 'green';

			if ( getLabel(input) )
				getLabel(input).style.color = 'green';	
		}
	}
	
	return result;

}

function form_verify(form)
{
	var type = '';
	var ret = true;
	
	for(i=0; i<form.elements.length; i++)
	{
		if ( input_verify(form.elements[i]) != 'good' )
			ret = false;
	}
	
	if ( ret )
		form.submit();
}