// Detecta o Browser
var agent = navigator.userAgent;
function browserDet()
{
	if (agent.indexOf('Gecko') != -1){tipo = 'Moz';}
	else if (agent.indexOf('MSIE 6.0;') != -1){tipo = 'IE'; versao = '6';}
	else if (agent.indexOf('MSIE 7.0;') != -1){tipo = 'IE'; versao = '7';}
	else if (agent.indexOf('safari') != -1) {tipo = 'Saf';}
	else if (agent.indexOf('konqueror') != -1) {tipo = 'Kon';}


	this.tipo = tipo;
	if (tipo == 'IE') { this.versao = versao; }
}
browser = new browserDet();
function isMoz(){ if (browser.tipo == 'Moz') { return true; } }
function isIE(){ if (browser.tipo == 'IE') { return true; } }
function isIE6(){ if (browser.tipo == 'IE' && browser.versao == '6') { return true; } }
function isIE7(){ if (browser.tipo == 'IE' && browser.versao == '7') { return true; } }
function isSaf(){ if (browser.tipo == 'Saf') { return true; } }
function isKon(){ if (browser.tipo == 'Kon') { return true; } }






function createPopUp(url, popName, isMaxArea, width, height, isToCenter, top, left, isScrollBars, isResizable) {
/*
	url -> pagina a ser aberta
	popName -> nome de controle do popup
	width e height -> respectiva largura e altura
	maxArea -> marcado como 'yes' utilizará a area total
	scrollbars -> 'yes' ou 'no'
	resizable -> 'yes' ou 'no'
	top e left -> coordenadas de abertura do popup na tela
	toCenter -> Indica que deve ser aberto ao centro da tela
*/
	tW = screen.width;
	tH = screen.height;
	if (isMaxArea == 'yes') { W = tW; H = tH; }
	else { W = width; H = height; }
	popArea = 'Width='+ W + ', Height='+ H +', ';


	if (isToCenter == 'yes') 
	{
		L = (tW - width) / 2;
		T = (tH - height) / 2;
	}
	else { L = left; T = top; }
	popPosition = 'top='+ T +', left='+ L +', ';

	popCaracteres = 'scrollbars='+ isScrollBars +', resizable='+ isResizable;



	window.open(url, popName, popArea + popPosition + popCaracteres);
}


function setDisplayBlock(idName) {
	document.getElementById(idName).style.display = 'block';
	showHideSel('0');
	setADD_ZIndex(idName);
}
function setDisplayNone(idName)	{
	document.getElementById(idName).style.display = 'none';
	showHideSel('1');
	setDEC_ZIndex(idName);
}

function showHideSel(def){
	var sels = document.getElementsByTagName('select');
	if (def == 0){
		tipo = 'hidden';
	}else{
		tipo = 'visible';
	}
	for(var no=0;no<sels.length;no++){
		sels[no].style.visibility = tipo;
	}

}

function setADD_ZIndex(idName) 
{
	if (document.getElementById(idName + 'DHTML').style.zIndex ==''){
		document.getElementById(idName + 'DHTML').style.zIndex = '110';
	}

	atualZIndex = document.getElementById(idName + 'DHTML').style.zIndex;

	if (atualZIndex < 100)
	{

		newZIndex = parseFloat(atualZIndex) + parseFloat(100);
		document.getElementById(idName + 'DHTML').style.zIndex = newZIndex;
	}
}
function setDEC_ZIndex(idName) 
{ 
	if (document.getElementById(idName + 'DHTML').style.zIndex ==''){
		document.getElementById(idName + 'DHTML').style.zIndex = '10';
	}

	atualZIndex = document.getElementById(idName + 'DHTML').style.zIndex;

	if (atualZIndex > 100)
	{
		newZIndex = parseFloat(atualZIndex) - parseFloat(100);
		document.getElementById(idName + 'DHTML').style.zIndex = newZIndex;
	}
}


var setTimeOver;
function hideTime(idName) { setTimeOver = setTimeout('setDisplayNone("'+idName+'")', 100); }
function clearTime() { clearTimeout(setTimeOver); }

function mandaValor(idInpt, valor){ document.getElementById(idInpt).value = valor; }

/* fotos */
function proximaFoto(){
	if (fotoCorrente<strImg.length-1){
		fotoCorrente++;
		document.images.imgThumb.src = strImg[fotoCorrente];
		escreveLnk(fotoCorrente);
	}
}

function fotoAnterior(){
	if (fotoCorrente>0){
		fotoCorrente--;
		document.images.imgThumb.src = strImg[fotoCorrente];
		escreveLnk(fotoCorrente);
	}
}

function escreveLnk(id){
	for(i=0; i<strImg.length; i++)
	{
		document.getElementById("fotos_"+i).style.display = "none";
	}
	document.getElementById("fotos_"+id).style.display = "block";
}

/* LIMPA DEFAULT CAMPOS */
function clearText(thefield){
	var valorPadrao = thefield.defaultValue;
	if (thefield.defaultValue==thefield.value){
		thefield.value = "";
	}else if (thefield.value == ""){
		thefield.value = valorPadrao;
	}
} 

function limparCombo( objCombo , strSelecione )
{
	while(objCombo.options.length > 0) 
			objCombo.options[0]=null;
	objCombo.options[objCombo.options.length] = new Option( strSelecione , '' )
}

function soNumeros(e){
     var key;
     var keychar;

     if (window.event) {
        key = window.event.keyCode;
     } else if (e) {
        key = e.which;
     } else {
        return true;
     }

     keychar = String.fromCharCode(key);

     // teclas de controle
     if ((key==null) || (key==0) || (key==8) ||
          (key==9) || (key==13) || (key==27) ) {
        return true;
     } else if ((("0123456789").indexOf(keychar) > -1)) {
        return true;     
     } else {
        return false;
     }
}


function carregaComboAjax(link, destino, strSelecione) 
{
	AjaxRequest.get(
		{	'url': link
			,'onError': function (req){	alert ( req.responseText );	}
			,'onSuccess':function(req) { 

				var itens = new String ( req.responseText );

				if (itens == 'vazio')
				{
					while(destino.options.length > 0) 
						destino.options[0]=null;
					destino.options[destino.options.length] = new Option( strSelecione , '' )
					return;
				}

				//divide em vírgula
				var vet = itens.split( ',' );

				//Limpa Combo
				while(destino.options.length > 0) 
					destino.options[0]=null;

				//Verifica se o array possui itens

				if (vet.length > 0)
				{

					destino.options[destino.options.length] = new Option( strSelecione , '' )

					for ( i = 0 ; i < vet.length ; i++ )
					{
						var strId = vet[i].split( '|' );
						if (strId[1] != '' && strId[0] != '')
						{
							destino.options[destino.options.length] = new Option( strId[1] , strId[0] )
						}
					}
				}				
			}
		}
	);
}

function saltarCampo( field , length , nextfield )
{
	if (field.value.length == length)
	{
		nextfield.focus();
	}
}

/*Conta caracteres no campo*/
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else{ 
		document.getElementById(countfield).innerHTML = maxlimit - field.value.length;
	}
}

function validarForm()
{
	var str_erro = "";
	var frmValidator = new Validator("frmCadastro");

	if (!frmValidator.isValid()) {
		str_erro = frmValidator.errMsg;
	}

	if (document.frmCadastro.cep1.value == '' || document.frmCadastro.cep1.value == '')
	{
		str_erro += "\n - O campo CEP não foi informado.";
	}else{
		if (document.frmCadastro.cep1.value.length < 5 || document.frmCadastro.cep2.value.length < 3 )
		{
			str_erro += "\n - O campo CEP não foi informado corretamente.";
		}
	}

	if (document.frmCadastro.senha.value != '' && document.frmCadastro.confsenha.value != '')
	{
		if (document.frmCadastro.senha.value != document.frmCadastro.confsenha.value )
		{
			str_erro += "\n - A confirmação de senha não confere com a senha digitada.";
		}else{
			if (document.frmCadastro.senha.value.length < 4)
			{	
				str_erro += "\n - A senha informada deve ter mais do que 3 caracteres";
			}
		}
	}

	if (document.frmCadastro.estado.value == '')
	{
		str_erro += "\n - O campo estado não foi informado.";
	}

	if (str_erro != '')
	{
		alert ('Os seguintes erros foram encontrados:\n' + str_erro);

	}else{

		document.frmCadastro.submit();

	}

}

