String.prototype.trim = function(c, t){
	return c = "[" + (c == undefined ? " " : c.replace(/([\^\]\\-])/g, "\\\$1")) + "]+",
	this.replace(new RegExp((t != 2 ? "^" : "") + c + (t != 1 ? "|" + c + "$" : ""), "g"), "");
};

//valida cpf
String.prototype.isCPF = function(){
	var c = this;
	if((c = c.replace(/[^\d]/g,"").split("")).length != 11) return false;
	if(new RegExp("^" + c[0] + "{11}$").test(c.join(""))) return false;
	for(var s = 10, n = 0, i = 0; s >= 2; n += c[i++] * s--);
	if(c[9] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
	for(var s = 11, n = 0, i = 0; s >= 2; n += c[i++] * s--);
	if(c[10] != (((n %= 11) < 2) ? 0 : 11 - n)) return false;
	return true;
};

//valida email
function isEmail(email) {
	return /[\w-]+@([\w-]+\.)+[\w-]+/.test(email);
}

//Exibe campo de cartão riachuelo
function showNumeroCartao(obj, value) {
	if (!!value)
		obj.form.numero.style.display = "block";
	else
		obj.form.numero.style.display = "none";
}

//Valida Formulario
function FormValidator(form) {
	var erros = new Array();

	// Possui cartão
	if(form.opcao)
		if (!form.opcao[1].checked)
			if (form.numero.value.trim().length == 0){
				erros.push("O campo Número do Cartão é obrigatório.");
	        }
		else if (form.numero.value.length < 14) {
			erros.push("O campo Número do Cartão deve conter 14 caracteres.");
		}

	//Nome
	if (form.nome.value.trim().length == 0) {
		erros.push("O campo Nome é obrigatório.");
	}

	//Email
	if (form.email.value.trim().length == 0) {
		erros.push("O campo E-mail é obrigatório.")
	}
	else if (!isEmail(form.email.value)) {
		erros.push("O campo E-mail é inválido.")
	}

	//Cpf
	if (form.cpf.value.trim().length == 0) {
		erros.push("O campo CPF é obrigatório.")
	}
	else if (!form.cpf.value.isCPF()){
		erros.push("O campo CPF é inválido.");
	}

	    //Mensagem
	if (form.mensagem.value.trim().length == 0) {
		erros.push("O campo Mensagem é obrigatório.");
	}
	
	//Captcha
	if (form.captcha.value.trim().length == 0) {
		erros.push("O campo Código de Confirmação é obrigatório.");
	}

	if (erros.length > 0) {
		alert("Verifique os seguintes itens:\n"+erros.join("\n"));
		return false;
	}
	else {
	     return true;
	}
}



// Criação de formulários
var currentform = "";
function doForms(select) {
	if (currentform != "") {
		var fm = document.getElementById(currentform);
		if (typeof (fm) != undefined) {
			fm.className = "hide";
		}
	}


	var itm = select.value;
	currentform = itm;

	if (currentform != "") {
	    if(currentform == "proposta" || currentform == "pagamento")
		   fm = document.getElementById(currentform = "extrato");
        else if(!(fm = document.getElementById(currentform)))
		   fm = document.getElementById(currentform = "pagamento");
		
		if (typeof (fm) != undefined) {
			fm.className = "show";
			var form = fm.getElementsByTagName("form")[0];
			if(form){
				form.assunto.value = select.value;
			}
		}
	}
}