var sMyPathToImg  = '/phpbasic/img/';
var glRecsPerPage = 'recsPerPage';
var glAjaxDiv     = 'ajaxDiv';
var glFieldOrder  = 'fieldOrder';
var glSortOrder   = 'sortOrder';
var glDateType    = 'dd/mm/yyyy'; 

function verifyFields() {

}

/**
 * Geral
 */
function $(sId) {
  return document.getElementById(sId);
}

/**
 * Menu
 */
function menuClick(oMenu) {
  menuOver(oMenu);

  var sMenu = oMenu.id;
  window.location.href = sMenu + '.php';
}

function menuOver(oMenu) {
  oMenu.className = 'menuOn';
}

function menuOut(oMenu) {
  oMenu.className = 'menuOut';
}

/**
 * Menu Continente
 */
function menuContinenteClick(oMenu) {
  menuContinenteOver(oMenu);

  var sMenu = oMenu.id;
  window.location.href = sMenu + '.php';
}

function menuContinenteOver(oMenu) {
  oMenu.className = 'continenteMenuOn';
}

function menuContinenteOut(oMenu) {
  oMenu.className = 'continenteMenu';
}

/**
 * Menu Diferenciais
 */
function menuDiferenciaisClick(oMenu) {
  menuDiferenciaisOver(oMenu);

  var sMenu = oMenu.id;
  window.location.href = sMenu + '.php';
}

function menuDiferenciaisOver(oMenu) {
  oMenu.className = 'diferenciaisMenuOn';
}

function menuDiferenciaisOut(oMenu) {
  oMenu.className = 'diferenciaisMenu';
}

/**
 * Verificacao do form de imobiliaria
 */
function checkForm() {
  oForm = document.getElementById('sFormImob');
  oRaza = document.getElementById('sName');
  oCnpj = document.getElementById('sCpfCnpj');
  oResp = document.getElementById('sNameResp');
  oEmai = document.getElementById('sEmail');
  oTele = document.getElementById('sTel');
  oEnde = document.getElementById('sEndereco');
  oBair = document.getElementById('sBairro');
  oMuni = document.getElementById('sMunicipio');
  oCep  = document.getElementById('sCep');
  oCrec = document.getElementById('sCreci');
  oPrai = document.getElementById('iPraiaId');
  oInte = document.getElementById('iIntencaoId');
  oDorm = document.getElementById('iDormId');
  oGara = document.getElementById('iGaragemId');
  oValo = document.getElementById('iValorId');
  oPagt = document.getElementById('iPagtoId');
  oDist = document.getElementById('iDistanciaId');
  if (oRaza.value == '' ||
    oCnpj.value == '' || 
    oResp.value == '' || 
    oEmai.value == '' ||
    oTele.value == '' ||
    oEnde.value == '' ||
    oBair.value == '' ||
    oMuni.value == '' ||
    oCep.value  == '' ||
    oCrec.value == '' ||
    oPrai.value == '' ||
    oInte.value == '' ||
    oDorm.value == '' ||
    oGara.value == '' ||
    oValo.value == '' ||
    oPagt.value == '' ||
    oDist.value == ''
     ) {
    alert('Atencao! Todos os campos devem ser preenchidos!');
  } else {
    oForm.submit();
  }
}


/*********************************************
 * Atualizando contato
 *********************************************/
function updateContato() {
  if (checkFields()) {
    if (confirm('Esse comando ira anular o atendimento anterior, passando a valer somente as alteracoes do momento com a data informada.\nCaso seja um novo atendimento para o mesmo cliente, clique em "Novo Atendimento"\n\nPara prosseguir, clique em "OK".')) {
      oForm = document.getElementById('insertContato');
      oForm.submit();
    }
  } else {
    alert('Preencha todos os campos para finalizar o atendimento!');
  }
}

function addContato() {
  if (checkFields()) {
    oForm = document.getElementById('insertContato');
    oForm.submit();
  } else {
    alert('Preencha todos os campos para finalizar o atendimento!');
  }
}

function checkFields() {
  var bRet = true;

  if (document.getElementById('sName').value == '') { bRet = false; }
  if (document.getElementById('dData').value == '') { bRet = false; }
  if (document.getElementById('dNasc').value == '') { bRet = false; }
  if (document.getElementById('sMunicipio').value == '') { bRet = false; }
  if (document.getElementById('sEmpto').value == '') { bRet = false; }
  if (document.getElementById('sDesc').value == '') { bRet = false; }

  return bRet;
}

/*********************************************
 * Deletando contato
 *********************************************/
function checkElement(oElement, sId) {
  if (oElement == null) {
    alert('Element not created: ' + sId);
  }
}

function createHidden(sFormId, sHiddenName, sHiddenValue) {
  oForm = document.getElementById(sFormId);
  checkElement(oForm, sFormId);

  oNewHidden = document.createElement("input");
  oNewHidden.setAttribute("type" , "hidden");
  oNewHidden.setAttribute("name" , sHiddenName);
  oNewHidden.setAttribute("id"   , sHiddenName);
  oNewHidden.setAttribute("value", sHiddenValue);
  oForm.appendChild(oNewHidden);
}

function deleteContato(iContatoId) {
  if (confirm('Tem certeza que deseja excluir este contato?')) {
    oForm = document.getElementById('searchContato');
    oForm.action = 'imobRestrContatoAction.php';
    document.getElementById('sType').value = 'delContato';
    document.getElementById('iContatoId').value = iContatoId;
    oForm.submit();
  }
}

/*********************************************
 * Trocando as imagens com AJAX
 *********************************************/
function changeEmptoImagem(iId, iCount, iImgMenu) {
  oAjax = new Ajax('ajaxChangeEmptoImagem', '&nbsp;');
  oAjax.targetDiv = 'oDivEmptoImagem';
  oAjax.addParam('iId'     , iId);
  oAjax.addParam('iImg'    , iCount);
  oAjax.addParam('iImgMenu', iImgMenu);
  oAjax.addParam('bAjax'   , 1);
  oAjax.callAjax();
}

/*********************************************
 * Class Ajax
 * Generates a form that may be subbmitted
 *********************************************/
var iAjaxDimX = 0;
var iAjaxDimY = 0;
function Ajax(sAction, sMsg) {
  // Attributes
  this.action = sAction;
  this.msg    = (sMsg ? sMsg : 'Carregando...');
  this.method = 'GET';
  this.params = new Array();
  this.dimensionX = 0;
  this.dinemsionY = 0;

  this.targetDiv = 'ajaxBasicDiv';

  // Methods
  this.addParam = addParam;
  this.callAjax = callAjax;
  this.callCheckAjax = callCheckAjax;
  this.setDimensions = setDimensions;
}

/**
 * Method addParam
 * Adds a parameter to the ajax call
 */
function addParam(sName, sValue) {
  this.params.push(sName + '=' + sValue);
}

/**
 * Method callAjax
 * Calls the ajax page on the ajaxBasicDiv DIV element
 */
function callAjax() {
  ajaxBlock();
  ajaxDivResponse = this.targetDiv;

  if (ajaxDivResponse != 'ajaxBasicDiv') {
    oDivBasic = $('ajaxBasicDiv');
    oDivBasic.innerHTML = this.msg;
  }

  oDiv = $(ajaxDivResponse);
  oDiv.innerHTML = this.msg;

  var sParams = '';
  for(i = 0; i < this.params.length; i++) {
    sParams += this.params[i] + '&';
  }

  var sUrl = this.action + '.php?' + sParams.substr(0, sParams.length - 1) + '&fNum=' + Math.random();

  addAjax(sUrl);

  //xmlHttp = GetXmlHttpObject(stateChanged);
  //xmlHttp.open(this.method, sUrl, true);
  //xmlHttp.send(null);
}

/**
 * Method callCheckAjax
 * Calls the ajax page, but without showing the blocker frame
 */
function callCheckAjax() {
  ajaxDivResponse = this.targetDiv;

  var sParams = '';
  for(i = 0; i < this.params.length; i++) {
    sParams += this.params[i] + '&';
  }

  var sUrl = this.action + '.php?' + sParams.substr(0, sParams.length - 1) + '&fNum=' + Math.random();

  addAjax(sUrl);

  //xmlHttp = GetXmlHttpObject(stateChanged);
  //xmlHttp.open(this.method, sUrl, true);
  //xmlHttp.send(null);
}

function setDimensions(iDimX, iDimY) {
  this.dimensionX = iDimX;
  this.dinemsionY = iDimY;
  iAjaxDimX = iDimX;
  iAjaxDimY = iDimY;
}

function ajaxBlock() {
  if (document.all){
    window.onScroll = ajaxConfigBlock;
    window.onResize = ajaxConfigBlock;
    ajaxConfigBlock();
  } else {
    ajaxConfigBlock();
    wVar = window.setInterval('ajaxConfigBlock()', 100);
  }
}

function ajaxConfigBlock() {
  if (iAjaxDimX == 0 || iAjaxDimY == 0) {
    iDivHeight = 40;
    iDivWidth  = 150;
  } else {
    iDivHeight = iAjaxDimY;
    iDivWidth  = iAjaxDimX;
  }

  oDiv = $('ajaxBasicDiv');
  oDiv.style.display    = '';
  oDiv.style.position   = 'absolute';

  if (typeof(window.pageYOffset) == 'number') {
    iWinW = window.innerWidth;
    iWinH = window.innerHeight;
    iScrW = window.pageXOffset;
    iScrH = window.pageYOffset;
  } else if (document.body && typeof(document.body.scrollTop) == 'number') {
    iWinW = document.body.clientWidth;
    iWinH = document.body.clientHeight;
    iScrW = document.body.scrollLeft;
    iScrH = document.body.scrollTop;
  } else if (document.documentElement && typeof(document.documentElement.scrollTop) == 'number') {
    iWinW = document.documentElement.clientWidth;
    iWinH = document.documentElement.clientHeight;
    iScrW = document.documentElement.scrollLeft;
    iScrH = document.documentElement.scrollTop;
  }

  oDiv.style.top        = (iWinH / 2) - (iDivHeight / 2) + iScrH;
  oDiv.style.left       = (iWinW / 2) - (iDivWidth / 2) + iScrW;
  oDiv.style.height     = iDivHeight;
  oDiv.style.width      = iDivWidth;
  oDiv.style.zIndex     = 100;
//  oDiv.style.border     = 'solid 2px black';
  oDiv.style.padding    = '5px';
//  oDiv.style.background = '#BBBBBB';
  oDiv.align            = 'center';

  /* Showing the blocker IFRAME */
  oFra = $('iframeWait');
  if (oFra) {
    oFra.style.display = '';
    oFra.style.height  = iWinH + iScrH - 5;
    oFra.style.width   = iWinW + iScrW - 20;
    oFra.style.zIndex  = 0;
  }
}

function ajaxUnblock() {
  if (document.all){
    window.onscroll = ajaxConfigUnblock;
    window.onresize = ajaxConfigUnblock;
    ajaxConfigUnblock();
  } else {
    ajaxConfigUnblock();
    clearInterval(wVar);
  }
  finishAjax();
}

function ajaxConfigUnblock() {
  oDiv = $('ajaxBasicDiv');
  oDiv.style.display = 'none';

  oFrame = $('iframeWait');
  oFrame.style.display = 'none';
}

/**
 * Usage:
 *
 * oAjax = new Ajax('helps.php');
 * oAjax.addParam('Nome', 'Valor');
 * oAjax.addParam('Nome2', 'Valor2');
 * oAjax.callAjax();
 *
 * <div id="ajaxBasicDiv" style="display: none"></div>
 */

