// JavaScript Document
/**
* 	Validação de email por expressão regular
*
*/
function emailValidation(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    
	if(typeof(mail) == "string") 
	{
        if(er.test(mail))
		{ 
			return true; 		
		}
    }
	else if(typeof(mail) == "object") 
	{
        if(er.test(mail.value))
		{
			return true;
        }
    }
	else
	{
    	return false;
    }
}
/**
* 	validação de cpf
*
*/
function CPFValidation(cpf) {
	
	erro = new String;
	if (cpf.length < 11) 
		erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
	
	
	cpf = cpf.replace(".","");
	cpf = cpf.replace(".","");
	cpf = cpf.replace("-","");
	
	var nonNumbers = /\D/;
	
	if (nonNumbers.test(cpf)) 
		erro += "A verificacao de CPF suporta apenas numeros! \n\n";
	
	if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){	
		erro += "Numero de CPF invalido!";
	}
	var a = [];
	var b = new Number;
	var c = 11;
	//
	for (i=0; i<11; i++){
	   a[i] = cpf.charAt(i);

	   if (i < 9) 
		   b += (a[i] * --c);
	}
	//
	if ((x = b % 11) < 2) { 
		a[9] = 0;
	} else { 
		a[9] = 11-x;
	}
	//
	b = 0;
	c = 11;
	//
	for (y=0; y<10; y++) 
		b += (a[y] * c--);
	//
	if ((x = b % 11) < 2) { 
		a[10] = 0; 
	} else { 
		a[10] = 11-x; 
	}
	//
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
		erro +="Número de CPF Inválido!";
	}
	//
	if (erro.length > 0){
		return false;
	}
	return true;
}
/**
* 	Validação de casos de formulário
*
*/
function caseValidate (campo,tipo) {
	var error = "";
	switch(tipo) 
	{
		case "string":
			if(campo.value.length < 2) 
			{
				campo.style.border = "1px solid #FF0000";
				campo.focus();
				return false;
			}			
		break;
		case "fone":
			if(campo.value.length < 13) 
			{
				campo.style.border = "1px solid #FF0000";
				campo.focus();
				return false;
			}							
		break;
		case "cpf":
			if(!CPFValidation(campo.value)) 
			{
				campo.style.border = "1px solid #FF0000";
				campo.focus();
				return false;
			}
		
		break;
		case "email":
		
			if(!emailValidation(campo.value)) 
			{
				campo.style.border = "1px solid #FF0000";
				campo.focus();
				return false;
			}		
			
		break;
		case "select":
			if(campo.options[campo.selectedIndex].value == 0) 
			{
				alert("Você deve selecionar um item");
				campo.style.border = "1px solid #FF0000";
				campo.focus();
				return false;				
			}				
				
		break;
		case "cep":
			if(campo.value.length < 9) 
			{
				campo.style.border = "1px solid #FF0000";
				campo.focus();
				return false;
			}							
		break;		
		default:
			return false;
		break;
	}
	
	return true;	
}
/**
* 	Valida um formulário e seus campos
*
*/
function contactFormValidation () 
{
	var formulario = $id("contact_form");
	
	var camposOb = new Array ("nome","cidade","estado","telefone","email");
	var tipos = new Array("string","string","string","string","email");
	
	var val = true;
	
	var a = null;
	
	for(a=0; a < camposOb.length; a++) {
	
		if(!caseValidate($id(camposOb[a]),tipos[a])) {
			val = false;
			break;		
		}	
	
	}
	
	if(val) {
		sendContact(formulario);
	}
}

function cadastroFormValidation () 
{
	var formulario = $id("cadastro_form");
	
	var camposOb = new Array ("nome","email","obs");
	var tipos = new Array("string","email","string");
	
	var val = true;
	
	var a = null;
	
	for(a=0; a < camposOb.length; a++) {
	
		if(!caseValidate($id(camposOb[a]),tipos[a])) {
			val = false;
			break;		
		}	
	
	}
	
	if(val) {
		formulario.submit();
	}
}