String.prototype.trim=function(){
	return this.replace(/^\s*|\s*$/g,'');
};
YAHOO.namespace("egas");
YAHOO.egas.For = function(){
	var yud = YAHOO.util.Dom;
	var yue = YAHOO.util.Event;
	var yuc = YAHOO.util.Connect;
	return {
		init: function(){
			yue.onDOMReady(this.frmAlunos, this, true);
		},
		frmAlunos: function(){
			yue.on("cadastroEmp", "submit", this.checaEnvio);
			yue.on("emp_30_fantasia", "blur", this.checaFantasia);
		},
    checaFantasia: function(){
        var fantasia = yud.get("emp_30_fantasia");
        var fantasiaok = yud.get("emp_30_fantasiaok");
        var fantasia_div = yud.get("emp_30_fantasia_div");
        var emp_30_fantasia_valid = yud.get("emp_30_fantasia_valid");
        var postData = "fantasia="+fantasia.value.trim();
        fantasia_div.className = "";
        if(fantasia.value.trim() == ""){
            yud.replaceClass(fantasia_div, "ok", "erro");
            fantasia_div.innerHTML = "Digite o Nome Fantasia";
            emp_30_fantasia_valid.value = "false";
        }else if(fantasiaok.value == fantasia.value){
            yud.addClass(fantasia_div, "ok");
            return;
        }else{
        fantasia_div.innerHTML = "<img src='"+siteurl+"/images/load.gif' align='absmiddle'> Checando <b>"+fantasia.value+"</b>";
        var sucesso = function(o){
            if(o.responseText == 1){
                fantasia_div.innerHTML = "<b>OK</b>";
                yud.removeClass(fantasia, "erro");
                yud.addClass(fantasia_div, "ok");
                fantasiaok.value = fantasia.value;
                 emp_30_fantasia_valid.value = "true";
            }else{
                fantasia_div.innerHTML = "O Nome Fantasia <b>"+fantasia.value.trim()+"</b> já está cadastrado em nosso sistema.<br />Entre em contato com nossa empresa para verificar o problema.";
                emp_30_fantasia_valid.value = "false";
                yud.addClass(fantasia, "erro");
                yud.addClass(fantasia_div, "erro");
            }
        };
        var falha = function(o){
            fantasia_div.innerHTML = "Erro: "+o.statusText;
        };
        var callback =
        {
            success:sucesso,
            failure:falha
        };
        var request = yuc.asyncRequest('POST', siteurl+"/checaFantasia.php", callback, postData);
    }
},
		checaEnvio: function(e){
			var emp_30_fantasia = yud.get("emp_30_fantasia");
			var emp_31_cep = yud.get("emp_31_cep");
			var emp_30_endereco = yud.get("emp_30_endereco");
			var emp_30_num = yud.get("emp_30_num");
			var emp_30_bairro = yud.get("emp_30_bairro");
			var emp_30_cidade = yud.get("emp_30_cidade");
			var emp_31_estado = yud.get("emp_31_estado");
			var emp_31_estado_html = yud.get("emp_31_estado_html");
			var emp_30_email = yud.get("emp_30_email");
			var emp_30_fone = yud.get("emp_30_fone");
			var cat_10_id = yud.get("cat_10_id");
			var captcha = yud.get("captcha");
			var cat_10_id_html = yud.get("cat_10_id_html");
			var emp_30_fantasia_valid = yud.get("emp_30_fantasia_valid");

			var error = false;
			if(captcha.value.trim() == ""){
				captcha.style.background = "#FF8F8F";
				error = true;
			}else{
				captcha.style.background = "#fff";
			}
			if(emp_30_fantasia.value.trim() == "" || emp_30_fantasia_valid.value == 'false'){
				emp_30_fantasia.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_30_fantasia.style.background = "#fff";
			}
			if(emp_31_cep.value.trim() == ""){
				emp_31_cep.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_31_cep.style.background = "#fff";
			}
			if(emp_30_endereco.value.trim() == ""){
				emp_30_endereco.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_30_endereco.style.background = "#fff";
			}
			if(emp_30_num.value.trim() == ""){
				emp_30_num.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_30_num.style.background = "#fff";
			}
			if(emp_30_bairro.value.trim() == ""){
				emp_30_bairro.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_30_bairro.style.background = "#fff";
			}
			if(emp_30_cidade.value.trim() == ""){
				emp_30_cidade.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_30_cidade.style.background = "#fff";
			}
			if(emp_31_estado.selectedIndex === 0){
				emp_31_estado_html.innerHTML="Selecione um estado";
				emp_31_estado_html.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_31_estado_html.style.background = "#fff";
				emp_31_estado_html.innerHTML = "";
			}
			if(emp_30_fone.value.trim()  == ""){
				emp_30_fone.style.background = "#FF8F8F";
				error = true;
			}else{
				emp_30_fone.style.background = "#fff";
			}
			if(cat_10_id.selectedIndex == 0){
				cat_10_id_html.style.background = "#FF8F8F";
				cat_10_id_html.innerHTML="Selecione uma categoria";
				error = true;
			}else{
				cat_10_id_html.style.background = "#fff";
				cat_10_id_html.innerHTML = "";
			}	
			if(emp_30_email.value.trim() == ""){
				emp_30_email.style.background = "#FF8F8F";
				error = true;
			}else{
				var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
				var mail=emp_30_email.value.trim();
				if(typeof(mail) == "string"){
					if(er.test(mail)){
						emp_30_email.style.background = "#fff";
					}else{
						error = true;
						emp_30_email.style.background = "#FF8F8F";
					}
				}else{
					error = true;
					emp_30_email.style.background = "#FF8F8F";
				}
			}
			if(error){
				yue.stopEvent(e);
				alert('Não é possível efetuar o cadastro.\n\r Verifique os campos em vermelho.');
			}
		}
	}
}();
YAHOO.egas.For.init();
