var RESET_COLOR = '#ffffff';
var ERROR_COLOR = '#ffff66';

var EMAIL_FIELD = 'email';

function validateForm(form_id) {
	var form = $(form_id);

	var error_count = 0;
	
	if ( !form ) {
		return false;
	}
	
	/**
	 * Take care of password and text inputs
	*/
	var inputs = form.getElementsByTagName('input');
	var i = 0;
	var len = inputs.length;


	var radio_errors = new Object();

	for ( i=0; i<len; ++i ) {
		switch ( inputs[i].type ) {
			case 'file':
			case 'password':
			case 'text': {
				inputs[i].style.backgroundColor = RESET_COLOR;

				resetError(inputs[i].name);
				
				if ( false == req(inputs[i]) ) {
					inputs[i].style.backgroundColor = ERROR_COLOR;
					error_count++;
					showError(inputs[i].name);
				}
				
				if ( -1 != inputs[i].name.indexOf(EMAIL_FIELD) ) {
					if ( false == email(inputs[i].value) ) {
						inputs[i].style.backgroundColor = ERROR_COLOR;
						error_count++;
						showError(inputs[i].name);
					}
				}
				
				break;
			}

			case 'radio': {
				if ( 'undefined' == typeof radio_errors[inputs[i].name] ) {
					radio_errors[inputs[i].name] = true;
				}

				// First, you have to see if any of the radio buttons were checked
				if ( true == reqRadio(inputs[i]) ) {
					radio_errors[inputs[i].name] = false;
				}

				break;
			}
		}
	}
	
	// Foreach radio button group, if any are unchecked, show an error
	for ( var name in radio_errors ) {
		resetError(name);
		if ( true == radio_errors[name] ) {
			error_count++;
			showError(name);
		}
	}
	
	/**
	 * Take care of selects
	*/
	var selects = form.getElementsByTagName('select');
	len = selects.length;
	for ( i=0; i<len; ++i ) {
		selects[i].style.backgroundColor = RESET_COLOR;
		resetError(selects[i].name);
		
		if ( false == req(selects[i]) ) {
			selects[i].style.backgroundColor = ERROR_COLOR;
			showError(selects[i].name);
			error_count++;
		}
	}
	
	/**
	 * And text areas
	*/
	var tas = form.getElementsByTagName('textarea');
	len = tas.length;
	for ( i=0; i<len; ++i ) {
		tas[i].style.backgroundColor = RESET_COLOR;
		resetError(tas[i].name);
		
		if ( false == req(tas[i]) ) {
			tas[i].style.backgroundColor = ERROR_COLOR;
			showError(tas[i].name);
			error_count++;
		}
	}
	
	
	if ( 0 == error_count ) {
		form.submit();
	}
}

function showError(name) {
	$('error_' + name).style.display = 'block';
}

function resetError(name) {
	var e = $('error_' + name);
	
	if ( e != null ) {
		e.style.display = 'none';
	}
}

//function $(e) { return document.getElementById(e); }


function req(e) {
	if ( null != e.getAttribute('required') ) {
		if ( 1 == e.getAttribute('required') && e.value.length > 0 ) {
			return true;
		}
	} else {
		return true;
	}
	
	return false;
	
}

function reqRadio(e) {
	if ( null != e.getAttribute('required') ) {
		if ( 1 == e.getAttribute('required') && e.checked == true ) {
			return true;
		}
	} else {
		return true;
	}

	return false;

}

function email(email) {
	var email_regex = /([a-z0-9-_.!#$%^&*~`]+)(@[a-z0-9-]+\.[a-z]+)/i;
	var regex = new RegExp(email_regex);
	
	return regex.test(email);
}
