
/******************************************************************

				OPERAÇÕES COM FORMULÁRIOS
				-------------------------
				Triares Informática e Tecnologia
				http://www.triares.com.br	

*******************************************************************/

//Valida data.
// str = o value do campo.
// campo = o campo ;)
function validaData(str,campo){
    	var arr = str.split("/");
        var dia = arr[0];
        var mes = arr[1];
        var ano = arr[2];

        if(mes > 0 && mes <= 12) {
                total = (mes == 02) ? (28 + ( (ano % 4) == 0) ) : 31;

                if(mes == "04"){
                    total = 30;
                }else if (mes == "06"){
                     total = 30;
                }else if(mes == "09"){
                    total = 30;
                }else if(mes == "11"){
                    total = 30;
                }

                if (!(dia > 0 && dia <= total)) {
                      	  return false;
                }
                } else {
                           return false;

                }
        return true;
}

function validaNovaSenha(){
	senha = document.getElementById("senhaNova");
	senha2 = document.getElementById("senhaRepetir");
	
	if(senha.value != senha2.value){
		alert("As senhas digitadas não são iguais");	
		return false;
	}
	
	return true;
		
	
}

function permitirNumero(e){
	var tecla=(window.event)?event.keyCode:e.which;	
    if((tecla > 47 && tecla < 58)){	
		return true;
	}else{
    	return (tecla != 8 && tecla !=13 && tecla != 0) ? false : true; 
	} 
}

function maxLength(obj, limit) {
	if (obj.value.length >= limit) {
		obj.value = obj.value.substring(0, limit-1);
	}
}

function trim(campo){
	var i = 0;
	while (campo.charCodeAt(0) == '32'){
		campo = campo.substring(i,campo.length);
		i++;
	}
	while(campo.charCodeAt(campo.length-1) == "32"){
		campo = campo.substring(0,campo.length-1);
	}	
	return campo;
}

function validarEmail(email){
	var bolReturn = false;
	var oRegEmail = /^[a-z0-9\._\-]+\@[a-z0-9\._\-]+\.[a-z]{2,3}$/i;
	bolReturn = oRegEmail.test(email);
	
	if(!bolReturn){
		return false;
	}
	return true;
}

function validaUpFlash(file){
	 var arquivo = file.split(".")[1].toLowerCase();
     if(arquivo!="swf")
     {
		return false;
     }
	 return true;
	
}

function validaUpImagem(file){
	 var arquivo = file.split(".")[1].toLowerCase();
     if((arquivo == "jpg") || (arquivo == "jpeg"))
     {
		return true;
     }
	 return false;
	
}

function validaIsNum(value){
	var reDigits = /^\d+$/;

	if (reDigits.test(value)) {
		return true
	} else {
		return false
	}
}

function validaLink(value){
	if(value.indexOf("http://") != 0){		
		return false;
	}else{
		return true;	
	}
}
	

/* ====================================================================================== */

function validarForm( form ) {
	for(i=0; i < form.elements.length; i++) {	
		if(typeof(form.elements[i].name) != "undefined") {
			var id = form.elements[i].getAttribute("id");
			var name = form.elements[i].getAttribute("name");
			var classe = form.elements[i].getAttribute("class");
			if((id != null)||(name != null)) {
				var value = trim(form.elements[i].value);
				if (name == "mensagem")
					var value = $( 'textarea.editor' ).val();
				if(classe != null){
					if(classe.indexOf("requerido") != -1 && value == "") {
						alert("Preencha todos os campos obrigatórios");
						alert(form.elements[i].getAttribute("name"));
						form.elements[i].focus();
						return false;
					}
					if(classe.indexOf("upFlash") != -1){
						if(!validaUpFlash(value)){
							alert("Tipo de arquivo inválido.\nSão aceitos somente arquivos do tipo Flash: SWF.");
							form.elements[i].focus();
							return false;
						}
					}
					if(classe.indexOf("upImagem") != -1){
						if(!validaUpImagem(value)){
							alert("Tipo de arquivo inválido.\nSão aceitos somente arquivos do tipo Flash: JPG, JPEG.");
							form.elements[i].focus();
							return false;
						}
					}
					if(classe.indexOf("pontuacao") != -1){
							if (!validaIsNum(value)){
								alert("O campo pontuação deve ser numerico!");							
								form.elements[i].focus();
								return false;
							}
					}
					if(classe.indexOf("tempo") != -1){
							if (!validaIsNum(value)){
								alert("O campo tempo deve ser numerico!");							
								form.elements[i].focus();
								return false;
							}
					}
					if(classe.indexOf("link") != -1){
							if (!validaLink(value)){
								alert("O link deve começar começar com 'http://'!");							
								form.elements[i].focus();
								return false;
							}
					}
					
				}
				if(name.indexOf("email") != -1 && value != "") {	
					if(!validarEmail(value)) {
						alert("E-mail Inválido");
						form.elements[i].focus();
						return false;
					}
				}
				if(name.indexOf("dt_") != -1){
					if(!validaData(value,form.elements[i])){
						alert("Data Inválida");
						form.elements[i].focus();
						return false;
					}
				}
			}				
		}
	}
	return true;
}

function validarEnquete(form){
	var check = false;
	for(i=0; i < form.elements.length; i++) {	
		if(typeof(form.elements[i].name) != "undefined") {
			var campo = form.elements[i];
				
			if (campo.checked == true)
				check = true;
		}
	}
	
	if(check == true){
		return true;
	}
	else{
		alert('Selecione uma opção');	
		return false;
	}
}
