function ckEmail(value) {
	return /^[a-z0-9._-]+@[a-z0-9-]+(\.[a-z0-9]{2,4})+$/i.test(value)
}

Array.prototype.remove = function(item) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == item) {
			this.splice(i,1);
			break;
		}
	}
}

var nomeUsuario = 'fNome';
var emailUsuario = 'fEmail';
var nomeAmigo = 'fNomeAmigo';
var emailAmigo = 'fEmailAmigo';

function ckForm() {
	var nome = $(nomeUsuario);
	var email = $(emailUsuario);

	var campos = [];
	var outrosCampos = [];
	var oErrors = 3;
	
	if (nome.value == "")
		campos.push(nome.id);
	
	if (!ckEmail(email.value))
		campos.push(email.id);

	for (var i = 1; i <= 3; i++) {
		var aN = $(nomeAmigo+i);
		var aE = $(emailAmigo+i);
		
		isEmail = ckEmail(aE.value);

		if (aN.value || aE.value)
			outrosCampos.push({
				'nome':		aN.value,
				'email':	aE.value,
				'isEmail':	isEmail
			});

		if (aN.value != "" || isEmail)
			oErrors--;

		aN.value = "";
		aE.value = "";
		aN.className = "";
		aE.className = "";
	}
	
	nome.className = "";
	email.className = "";
	
	var cpCount = 1;
	var nomeCount = 0;
	var emailCount = 0;
	for (var i = 0; i < outrosCampos.length; i++) {
		var aN = nomeAmigo+cpCount;
		var aE = emailAmigo+cpCount;

		$(aN).value = outrosCampos[i].nome;
		$(aE).value = outrosCampos[i].email;

		if (outrosCampos[i].nome == "") {
			campos.remove(aN);
			campos.push(aN);
		} else {
			if (outrosCampos[i].email == "") {
				campos.remove(aE);
				campos.push(aE);
			}
		}

		if (!outrosCampos[i].isEmail) {
			campos.remove(aE);
			campos.push(aE);
		} else {
			if (outrosCampos[i].nome == "") {
				campos.remove(aN);
				campos.push(aN);
			}
		}

		cpCount++;
	}

	//default verification
	if (oErrors == 3) {
		campos.remove(nomeAmigo + '1');
		campos.push(nomeAmigo + '1');
		campos.remove(emailAmigo + '1');
		campos.push(emailAmigo + '1');		
	}
	
	if (campos.length) alert('Verifique se os campos destacados foram preenchidos corretamente:');

	for (var i = 0; i < campos.length; i++)
		$(campos[i]).className = "alert";

	return campos.length == 0;
}

function restrictChars(event) {
	return DFonlyThisChars(false,true,' ??????????????????????????????????????????????.?',event);
}

function setForm() {
	var f = getElm('fWebcards');

	var nMaxL = document.createAttribute("maxlength");
		nMaxL.value = '50';

	var eMaxL = document.createAttribute("maxlength");
		eMaxL.value = '80';	

	$(nomeUsuario).onkeypress = restrictChars;
	$(nomeUsuario).attributes.setNamedItem(nMaxL);
	$(emailUsuario).attributes.setNamedItem(eMaxL);

	for (var i = 1; i <= 3; i++) {
		var nMaxL = document.createAttribute("maxlength");
			nMaxL.value = '50';
		var eMaxL = document.createAttribute("maxlength");
			eMaxL.value = '80';	

		var aNome = $(nomeAmigo + i);
			aNome.onkeypress = restrictChars;
			aNome.attributes.setNamedItem(nMaxL);
			
		var aEmail = $(emailAmigo + i);
			aEmail.attributes.setNamedItem(eMaxL);
	}

	$("fMensagem").onkeypress = function(event) { return DFtextareaMaxLen(this, 280, event) }
}
