var firstError = null;
var errorstring = '';
var validForm = true;
var reqFields = {
	'name' : 'your name',
	'email' : 'your email address',
	'friendname' : 'your friend\'s name',
	'friendemail' : 'your friend\'s email address',
	'message' : 'your message'
};

function validate() {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms['frm_send2friend'].elements;
	for (var key in reqFields) {
		if (!x[key].value)
			writeError(x[key],'Please provide ' + reqFields[key]);
	}
	if (x['email'].value.indexOf('@') == -1)
		writeError(x['email'], 'Your email address is not valid!');
	if (x['friendemail'].value.indexOf('@') == -1)
		writeError(x['friendemail'], 'Your friend\'s email address is not valid!');
	if (!validForm)
		alert('Please correct the following:\n\n' + errorstring);
	if (firstError)
		firstError.focus();
	return validForm;
}

function writeError(obj, message) {
	validForm = false;
	if (obj.hasError) return;
	errorstring += '* ' + message + '\n';
	obj.hasError = true;
	if (!firstError) firstError = obj;
}

function accform(){
  // Hide forms
  $( 'form.accform' ).hide().end();

  // Processing
  $( 'form.accform' ).find( 'li/label' ).not( '.nos2f' ).each( function( i ){
	var labelContent = this.innerHTML;
	var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
	var labelSpan = document.createElement( 'span' );
		labelSpan.style.display = 'block';
		labelSpan.style.width = labelWidth;
		labelSpan.innerHTML = labelContent;
	this.style.display = '-moz-inline-box';
	this.innerHTML = null;
	this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $( 'form.accform' ).show().end();
}

window.onload = function () {
	document.forms['frm_send2friend'].onsubmit = function () {
		return validate();
	}
}

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', accform, false );
