/*******************************FUNCOES GERAIS******************************************/

function byId(element){

	return document.getElementById(element)

}



function Trim(campo){

	return campo.replace(/^\s+|\s+$/g,"");

}



function validateEmail(email){

	var reEmail1 = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

	var reEmail2 = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

	var reEmail3 = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

	var reEmail = reEmail3;

	eval("reEmail = reEmail" + 3);

	if (!(reEmail.test(email))) {

		alert("O campo E-mail deve ser conter um endereco eletronico vÃ¡lido!");

		return false;

	}

	return true;

}



function retornaBairro(idCidade,idElement){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaCidades.php",

		data: 'id_cidade='+idCidade,

		success: function(msg){

			byId(idElement).disabled  = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaCidadesGeral(idCidade,idElement){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaCidadesGeral.php",

		data: 'id_cidade='+idCidade,

		success: function(msg){


			byId(idElement).disabled  = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaCidadesP(idCidade,idElement,type){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaCidadesP.php",

		data: 'id_cidade='+idCidade+'&idP='+type,

		success: function(msg){

			byId(idElement).disabled  = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaCidadesLogin(idCidade,idElement){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaCidadesLogin.php",

		data: 'id_cidade='+idCidade,

		success: function(msg){

			byId(idElement).disabled  = false;

			byId(idElement).innerHTML = msg;

		}

	});

	return false;

}





function retornaCidades(idCidade,idElement){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaCidadesBusca.php",

		data: 'id_cidade='+idCidade,

		success: function(msg){

			byId(idElement).disabled  = false;

			byId(idElement).innerHTML = msg;

		}

	});

	return false;

}



function retornaCidadesdisk(idCidadedisk,idElement){

	if (!idCidadedisk)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaCidadesBuscadisk.php",

		data: 'id_cidade='+idCidadedisk,

		success: function(msg){

			byId(idElement).disabled  = false;

			byId(idElement).innerHTML = msg;

		}

	});

	return false;

}



function retornaBairro2(idCidade,idElement){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaBairro.php",

		data: 'id_cidade='+idCidade,

		success: function(msg){

			if (byId('procura_departamento').value != '')

				return false;

				//

				byId(idElement).disabled  = false;

				byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaBairroLogin(idCidade,idElement){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaBairroLogin.php",

		data: 'id_cidade='+idCidade,

		success: function(msg){

			if(byId(idElement) == null)

				return false;

			//

			byId(idElement).disabled = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}



		}

	});

	return false;

}



function retornaBairroLogindisk(idCidadedisk,idElement){

	if (!idCidadedisk)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaBairroLogindisk.php",

		data: 'id_cidade='+idCidadedisk,

		success: function(msg){

			if(byId(idElement) == null)

				return false;

			//

			byId(idElement).disabled = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}



		}

	});

	return false;

}

function retornaRua(idBairro,idElement){

	if (!idBairro)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaRua.php",

		data: 'id_bairro='+idBairro,

		success: function(msg){

			byId(idElement).disabled = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaRuaCidade(idCidade,idElement){

	if (!idCidade)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaRua.php",

		data: 'id_cidade='+idCidade,

		success: function(msg){

			byId(idElement).disabled = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaRuaLogin(idBairro,idElement){

	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaRuaLogin.php",

		data: 'id_bairro='+idBairro,

		success: function(msg){

			byId(idElement).disabled = false;

	     	byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}



		}

	});

	return false;

}





function retornaBairrodisk(idCidadedisk,idElement){

	if (!idCidadedisk)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaCidadesdisk.php",

		data: 'id_cidade='+idCidadedisk,

		success: function(msg){

			byId(idElement).disabled  = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaBairro2disk(idCidadedisk,idElement){

	if (!idCidadedisk)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaBairrodisk.php",

		data: 'id_cidade='+idCidadedisk,

		success: function(msg){

			if (byId('procura_departamento_disk').value != '')

				return false;

				//

				byId(idElement).disabled  = false;

				byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaRuadisk(idBairrodisk,idElement){

	if (!idBairrodisk)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaRuadisk.php",

		data: 'id_bairro='+idBairrodisk,

		success: function(msg){

			byId(idElement).disabled = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function retornaRuaCidadedisk(idCidadedisk,idElement){

	if (!idCidadedisk)

		return false;



	jQuery.ajax({

		type: "get",

		url:  "ajax/retornaRuadisk.php",

		data: 'id_cidade='+idCidadedisk,

		success: function(msg){

			byId(idElement).disabled = false;

			byId(idElement).innerHTML = msg;

			if(msg){

				new Custom.init;

			}

		}

	});

	return false;

}



function ocultaAparece(element)

{

	if (byId(element).style.display == 'none')

		byId(element).style.display = '';

	else

		byId(element).style.display = 'none';

}



function Mascara(tipo, campo, teclaPress) {

	if (window.event)

	{

		var tecla = teclaPress.keyCode;

	} else {

		tecla = teclaPress.which;

	}



	var s = new String(campo.value);

	// Remove todos os caracteres Ã  seguir: ( ) / - . e espaÃ§o, para tratar a string denovo.

	s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');



	tam = s.length + 1;



	if ( tecla != 9 && tecla != 8 ) {

		switch (tipo)

		{

		case 'CPF' :

			if (tam > 3 && tam < 7)

				campo.value = s.substr(0,3) + '.' + s.substr(3, tam);

			if (tam >= 7 && tam < 10)

				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);

			if (tam >= 10 && tam < 12)

				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);

		break;



		case 'CNPJ' :



			if (tam > 2 && tam < 6)

				campo.value = s.substr(0,2) + '.' + s.substr(2, tam);

			if (tam >= 6 && tam < 9)

				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);

			if (tam >= 9 && tam < 13)

				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);

			if (tam >= 13 && tam < 15)

				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);

		break;



		case 'TEL' :

			if (tam > 2 && tam < 4)

				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);

			if (tam >= 7 && tam < 11)

				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);

		break;



		case 'DATA' :

			if (tam > 2 && tam < 4)

				campo.value = s.substr(0,2) + '/' + s.substr(2, tam);

			if (tam > 4 && tam < 11)

				campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);

		break;

		}

	}

}



/*******************************[FIM]FUNCOES GERAIS*************************************/





/*******************************FUNCOES CADASTRE-SE ******************************************/

function abilitaCadastra()

{

	if ( byId('termos_condicoes').checked )

		byId('enviacadastro').disabled = false;

	else

		byId('enviacadastro').disabled = true;

}

/*******************************[FIM]FUNCOES CADASTRE-SE ******************************************/



function validateForm(formId){

	flag = 0;

	for(i=0; i < byId(formId).elements.length; i++)

	{

		if(byId(formId).elements[i]){

			if(byId(formId).elements[i].className.search(/required/)>=0){

				if(!Trim(byId(formId).elements[i].value)){

					byId(byId(formId).elements[i].id).style.border = 'dashed 1px #FF0000'; flag++;

				}

			}

			if(byId(formId).elements[i].className.search(/required/)>=0){

				if(Trim(byId(formId).elements[i].value)){

					byId(byId(formId).elements[i].id).style.border = '';

				}

			}

			if(byId(formId).elements[i].className.search(/required-email/)>=0){

				if(!validateEmail(byId(formId).elements[i].value)){

					byId(byId(formId).elements[i].id).style.border = 'dashed 1px #FF0000'; flag++;

				}

			}

		}

	}

	if(flag){

		alert('Preencha todos os campos (*) obrigatórios.');

		return false;

	}else{

		return true;

	}

}


