var xmlHttp = new Object();
var idCont;

function _jsPage() {
	if (_jsPage.arguments.length == 1) idCont = "tdCont";
	else idCont = _jsPage.arguments[1];
	page = _jsPage.arguments[0];

	if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest()
	else if (window.ActiveXObject) xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
	xmlHttp.onreadystatechange = _jsDone
	xmlHttp.open("GET", page, true)
	xmlHttp.send(null)
}

function _jsDone() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById(idCont).innerHTML = xmlHttp.responseText;
	}
	else {
		document.getElementById(idCont).innerHTML = "<img src=\"../img/imgCarregando.gif\" style=\"margin:2px 2px 2px 2px;\">";
	}
}

function _jsImovel(Imovel_Id) {
	popup('detalheImovel.php?Imovel_Id='+Imovel_Id,'imovel','720','550','no');
}

function _jsGeraSelect(tabName, fieldName, searchKey, searchValue, paramJs, paramCss, fieldValue, resultText, resultValue, idCont) {
	_jsPage("../inc/geraSelect.php?tabName="+tabName+"&fieldName="+fieldName+"&searchKey="+searchKey+"&searchValue="+searchValue+"&paramJs="+paramJs+"&paramCss="+paramCss+"&fieldValue="+fieldValue+"&resultText="+resultText+"&resultValue="+resultValue,idCont)
}

function popup(x,n,w,h,s) {
    var l = ( (window.screen.width  - w) / 2 );
    var t = ( (window.screen.height - h) / 2 );
   janelinha = window.open(x,n,'leftmargin=-15,topmargin=-15,marginheight=0,marginwidth=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+s+',resizable=no,top='+t+',left='+l+',screenX='+l+',screenY='+t+',copyhistory=no,height='+h+',width='+w+'');
}

function _jsPreview02(imgSrc,displayType) {
	document.getElementById("divPreview").style.display = displayType;	
	document.getElementById("imgPreview").src = imgSrc;	
}

function _jsPreview03(imgSrc,displayType,divName,imgName) {
	document.getElementById(divName).style.display = displayType;	
	document.getElementById(imgName).src = imgSrc;	
}


function _jsValContato() {
	if (document.frm1.Fale_Nome.value == '') {
		alert('Você deve preencher o seu nome completo');	
		document.frm1.Fale_Nome.focus();
		return false;
	}
	
	if (document.frm1.Fale_DDD.value == '') {
		alert('Você deve preencher o DDD do seu telefone');	
		document.frm1.Fale_DDD.focus();
		return false;
	}

	if (document.frm1.Fale_Telefone.value == '') {
		alert('Você deve preencher o número do seu telefone');	
		document.frm1.Fale_Telefone.focus();
		return false;
	}

	if (document.frm1.Fale_Email.value == '' || document.frm1.Fale_Email.value.indexOf("@") == -1 || document.frm1.Fale_Email.value.indexOf(".") == -1) {
		alert('Você deve preencher o seu e-mail corretamente');	
		document.frm1.Fale_Email.focus();
		return false;
	}

	if (document.frm1.Fale_Cidade.value == '') {
		alert('Você deve preencher o nome de sua cidade');	
		document.frm1.Fale_Cidade.focus();
		return false;
	}
	
	if (document.frm1.Fale_Estado.value == '') {
		alert('Você deve preencher o estado');	
		document.frm1.Fale_Estado.focus();
		return false;
	}

	if (document.frm1.Fale_Mensagem.value == '') {
		alert('Você deve preencher a mensagem');	
		document.frm1.Fale_Mensagem.focus();
		return false;
	}	
}

function allLeft2Right() {
  var listOri = document.getElementById("listOrigem")
  var listDest = document.getElementById("listDestino");
  for(var i = 0; i < listOri.length; i++) {
    var opcao = listOri.options[i];
    var novaOpcao = new Option(opcao.text, listDest.length);
    novaOpcao.value = opcao.value;
    listOri.remove(i);
    try {
      listDest.add(novaOpcao,null);
    } catch (e) {
      listDest.add(novaOpcao);
    }
    i--;
  }
}

function allRight2Left() {
  var listOri = document.getElementById("listOrigem")
  var listDest = document.getElementById("listDestino");
  for(var i = 0; i < listDest.length; i++) {
    var opcao = listDest.options[i];
    var novaOpcao = new Option(opcao.text, listOri.length);
    novaOpcao.value = opcao.value;
    listDest.remove(i);
    try {
      listOri.add(novaOpcao,null);
    } catch (e) {
      listOri.add(novaOpcao);
    }
    i--;
  }
}

function left2Right() {
  var listOri = document.getElementById("listOrigem")
  var listDest = document.getElementById("listDestino");
  for(var i = 0; i < listOri.length; i++) {
    var opcao = listOri.options[i];
    if(opcao.selected) {
      var novaOpcao = new Option(opcao.text, listDest.length);
      novaOpcao.value = opcao.value;
      listOri.remove(i);
      try {
        listDest.add(novaOpcao,null); //Firefox
      } catch(e) {
        listDest.add(novaOpcao); //IE
      }
      i--;
    }
  }
}

function right2Left() {
  var listOri = document.getElementById("listOrigem")
  var listDest = document.getElementById("listDestino");
  for(var i = 0; i < listDest.length; i++) {
    var opcao = listDest.options[i];
    if(opcao.selected) {
      var novaOpcao = new Option(opcao.text, listOri.length);
      novaOpcao.value = opcao.value;
      listDest.remove(i);
      try {
        listOri.add(novaOpcao,null);
      } catch (e) {
        listOri.add(novaOpcao);
      }
      i--;
    }
  }
}

function _jsDocumentos(OrderBy) {
	if (!OrderBy) document.getElementById("listOrigem").options.length = 0;
	else {
		page = "acoes.php?Acao=23&OrderBy="+OrderBy;
		if (window.XMLHttpRequest) xmlHttp = new XMLHttpRequest()
		else if (window.ActiveXObject) xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
		xmlHttp.onreadystatechange = _jsDone2
		xmlHttp.open("GET", page, true)
		xmlHttp.send(null)
	}
}

function _jsDone2() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById("listOrigem").options.length = 0;
		var varDocumentos = xmlHttp.responseText.split(";");
		for(i=0;i<=(varDocumentos.length-1);i++) {
			if (varDocumentos[i]) {
				var varDoc = varDocumentos[i].split(",")
				option = new Option(varDoc[0],varDoc[1]);
				try {
					document.getElementById("listOrigem").add(option,null);
				} catch (e) {
					document.getElementById("listOrigem").add(option);
				}
			}
		}
	}
}

function FormataValor(id,tammax,teclapres) {
   
    if(window.event) 
	{ // Internet Explorer
	 var tecla = teclapres.keyCode; 
	}
	else if(teclapres.which) 
	{ // Nestcape / firefox
	 var tecla = teclapres.which;
    }
	vr = document.getElementById(id).value;
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( ",", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	tam = vr.length;
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1; }
		if (tecla == 8 ){ tam = tam - 1; }
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){
		document.getElementById(id).value = vr; }
		if ( (tam > 2) && (tam <= 5) ){
		document.getElementById(id).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 6) && (tam <= 8) ){
		document.getElementById(id).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 9) && (tam <= 11) ){
		document.getElementById(id).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 12) && (tam <= 14) ){
		document.getElementById(id).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
		if ( (tam >= 15) && (tam <= 17) ){
		document.getElementById(id).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}
	}
}

