/********************************************************************
* Scripts for Classes
*******************************************************************/
function getOnForm(sFormId, sInputId) {
  var sVal = '';

  for (i = 0; i < document.getElementById(sFormId).elements.length; i++) {
    oElem = document.getElementById(sFormId).elements[i];
    if (oElem.id == sInputId) {
      if (oElem.type == 'checkbox') {
        sVal = oElem.checked;
      } else {
        sVal = oElem.value;
      }
    }
  }

  return sVal;

}

function getParams(sFormId) {
  var sParams = '';

  if (document.getElementById(sFormId)) {
    for (i = 0; i < document.getElementById(sFormId).elements.length; i++) {
      oElem = document.getElementById(sFormId).elements[i];
      if (oElem.type == 'checkbox') {
        sParams += oElem.id + '=' + oElem.checked + '&';
      } else if (oElem.type == 'radio' && oElem.checked) {
        sParams += oElem.id + '=' + oElem.value + '&';
      } else if (oElem.type != 'button') {
        if (oElem.type != 'radio' || oElem.checked) {
          sParams += oElem.id + '=' + oElem.value + '&';
        }
      }
    }
  }

  return sParams;
}
/********************************************************************
* Cell
*******************************************************************/
function order(sFieldId, sOrder, sFormId) {
  var sParams = '?sOrder=' + sOrder + '&sFieldId=' + sFieldId + '&';

  if (document.getElementById(glRecsPerPage + sFormId)) {
    sParams += 'iRecordsPerPage=' + document.getElementById(glRecsPerPage + sFormId).value + '&';
  }

  ajaxPaging(sFormId, sParams);
}

function makeOrderByTemp(campo, ajaxFrame, param) {
  var params = '?ordenar=true&temp=true&' + param + '&';

  if (campo != '') {
    arraOrder = campo.split(' ');
    params = params + 'order=' + arraOrder[0] + '&tipo=';
    if (arraOrder[1]) {
      params = params + arraOrder[1];
    }
  } else {
    params = params + 'order=&tipo=';
  }
  ajaxDivResponse = ajaxFrame;
  var url = './action/' + ajaxFrame + '.php' + params;
  xmlHttp = GetXmlHttpObject(stateChanged);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
}

function makeOrderBy(campo, ajaxFrame, formId) {
  var params = '?ordenar=true&temp=false&';

  if (ajaxFrame == 'listAlunoEmail') {
    if (email != "") {
      params += "email=" + email + "&";
    }
  }

  params += getParams(formId);

  if (campo != '') {
    arraOrder = campo.split(' ');
    params += 'order=' + arraOrder[0] + '&tipo=';
    if (document.getElementById('order')) {
      document.getElementById('order').value = arraOrder[0];
    }
    if (arraOrder[1]) {
      params += arraOrder[1];
      if (document.getElementById('tipo')) {
        document.getElementById('tipo').value = arraOrder[1];
      }
    }
  } else {
    params += 'order=&tipo=';
  }

  if (ajaxFrame == "listProspect" || ajaxFrame == "listAlunoEmail") {
    ajaxDivResponse = ajaxFrame;
    var url = '../action/' + ajaxFrame + '.php' + params;
    xmlHttp = GetXmlHttpObject(stateChanged);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
  } else if (ajaxFrame != "") {
    ajaxDivResponse = ajaxFrame;
    var url = './action/' + ajaxFrame + '.php' + params;
    xmlHttp = GetXmlHttpObject(stateChanged);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
  } else {
    alert('Response page not defined');
  }
}
/********************************************************************
* Row
*******************************************************************/
function gotoPageCode(sPage, sCode) {
  var sTargetPage = sPage;
  if (sPage.indexOf('?')) {
    sTargetPage += '&sCode=' + sCode;
  } else {
    sTargetPage += '?sCode=' + sCode;
  }
  document.location.href = sTargetPage;
}

function gotoPage(sPage) {
  document.location.href = sPage;
}
/********************************************************************
* Select
*******************************************************************/
function verifyFields() {

}
/********************************************************************
* TableGroup
*******************************************************************/
function minmax(sId, sType) {
  oDivMax = document.getElementById(sId + '_max');
  oDivMin = document.getElementById(sId + '_min');
  if (sType == 'MAX') {
    oDivMax.style.display = 'none';
    oDivMin.style.display = '';
  } else if (sType = 'MIN') {
    oDivMax.style.display = '';
    oDivMin.style.display = 'none';
  }
}

function OLDminmax(sId) {
  oDivMax = document.getElementById(sId + '_max');
  oDivMin = document.getElementById(sId + '_min');
  oImgMax = document.getElementById('img_' + sId + '_max');
  oImgMin = document.getElementById('img_' + sId + '_min');
  if (oDivMax.style.display = '') {
    oDivMax.style.display = 'none';
    oDivMin.style.display = '';
    oImgMax.style.display = '';
    oImgMin.style.display = 'none';
  }
  if (oDivMin.style.display = '') {
    oDivMax.style.display = '';
    oDivMin.style.display = 'none';
    oImgMax.style.display = 'none';
    oImgMin.style.display = '';
  }
}
/********************************************************************
* TableList
*******************************************************************/
function gotoPageForm(iPage, sFormId) {
  var sParams = '?iPage=' + iPage + '&';

  if (document.getElementById(glRecsPerPage + sFormId)) {
    sParams += 'iRecordsPerPage=' + document.getElementById(glRecsPerPage + sFormId).value + '&';
  }
  if (document.getElementById(glFieldOrder + sFormId)) {
    sParams += 'sFieldId=' + document.getElementById(glFieldOrder + sFormId).value + '&';
  }
  if (document.getElementById(glSortOrder + sFormId)) {
    sParams += 'sOrder=' + document.getElementById(glSortOrder + sFormId).value + '&';
  }

  ajaxPaging(sFormId, sParams);
}

function ajaxPaging(sFormId, sParams) {
  //alert('Form: ' + sFormId + '\nParams:' + sParams);
  if (document.getElementById(sFormId)) {
    sGotoPage = document.getElementById(sFormId).action;
    sParams += getParams(sFormId);

    ajaxDivResponse = glAjaxDiv + sFormId;
    //var sUrl = './ajax/' + sGotoPage + '.php' + sParams;
    var sUrl = sGotoPage + sParams;
    xmlHttp = GetXmlHttpObject(stateChanged);
    xmlHttp.open("GET", sUrl , true);
    xmlHttp.send(null);
  } else {
    alert('Form ' + sFormId + ' not found!');
  }
}
/********************************************************************
* InputFloat
*******************************************************************/
function checkFloat(evt) {
  var charCode = (evt.which) ? evt.which : event.keyCode
  //alert(charCode);
  /*
  * charCode == 32                    => nao permite espaco " "
  * charCode > 57                     => nao permite letras
  * charCode < 112 || charCode > 123  => permite F1 - F12
  * charCode < 96  || charCode > 105  => permite 0 a 9 no NumLock
  * charCode != 188                   => permite ,
  * charCode != 190                   => permite .
  * charCode != 110                   => permite , no NumLock
  * charCode != 194                   => permite . no NumLock
  */
  if (charCode == 32) {
    return false;
  }
  if (charCode > 57 && (charCode < 96 || charCode > 105) && (charCode < 112 || charCode > 123) && charCode != 188 && charCode != 190 && charCode != 110 && charCode != 194) {
    return false;
  }
  return true;
}

function formatFloatBd(obj) {
  var vBra   = "";
  var code   = 0;
  for (i = 0; i < obj.value.length; i++) {
    code = obj.value.charCodeAt(i);
    if (code > 47 && code < 58) {
      vBra += obj.value.charAt(i);
    } else if (code == 44) {
      vBra += ".";
    }
  }

  vEng = parseFloat(vBra).toFixed(2);
  vBra = "";

  for (i = 0; i < vEng.length; i++) {
    code = vEng.charCodeAt(i);
    if (code > 47 && code < 58) {
      vBra += vEng.charAt(i);
    } else if (code == 46) {
      vBra += ",";
    }
  }

  document.getElementById(obj.id).value = vBra;
}

function formatFloatUser(obj) {
  var unformat = obj.value;
  var intpart  = "";
  var decpart  = "";
  var forma    = "";
  var count    = 0;

  if (unformat != "") {
    intPart = unformat.substr(0,unformat.indexOf(","));
    decPart = unformat.substr(unformat.indexOf(",")+1,unformat.length);
  } else {
    return false;
  }

  for (i = intPart.length - 1; i >= 0; i--) {
    if (count == 3) {
      forma = obj.value.charAt(i) + "." + forma;
      count = 1;
    } else {
      forma = obj.value.charAt(i) + forma;
      count++;
    }
  }

  document.getElementById(obj.id).value = forma + "," + decPart;
}
/********************************************************************
* InputInteger
*******************************************************************/
function checkInteger(evt) {
  var charCode = (evt.which) ? evt.which : event.keyCode
  //alert(charCode);
  /*
  * charCode == 32                    => nao permite espaco " "
  * charCode > 57
  => nao permite letras
  * charCode < 112 || charCode > 123  => permite F1 - F12
  * charCode < 96  || charCode > 105  => permite 0 a 9 no NumLock
  */
  if (charCode == 32) {
    return false;
  }
  if (charCode > 57 && (charCode < 96 || charCode > 105) && (charCode < 112 || charCode > 123)) {
    return false;
  }

  return true;
}

/********************************************************************
* InputSelect
*******************************************************************/
function showHint(obj, action) {
  ajaxDivResponse = obj.id + "Hint";
  objAjax = document.getElementById(ajaxDivResponse);
  if (obj.value.length == 0) {
    objAjax.innerHTML = "";
    objAjax.style.display = "none";
    return ;
  }

  objAjax.style.display = "";
  objAjax.style.width   = obj.style.width;
  objAjax.style.left    = getTotalOffsetLeft(obj);
  objAjax.style.top     = parseInt(getTotalOffsetTop(obj)) + parseInt(obj.style.height);
  objAjax.style.zIndex  = 10;

  xmlHttp=GetXmlHttpObjectHint();
  if (xmlHttp == null) {
    alert ("Browser does not support HTTP Request");
    return ;
  }
  var url = './action/' + action + '.php';
  url = url + "?txt=" + obj.value;
  url = url + "&field=" + obj.id;
  xmlHttp.onreadystatechange = stateChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function getTotalOffsetTop(element) {
  var offset = 0;
  for(var i = element; i && i.tagName != "BODY"; i = i.offsetParent)
  offset+=i.offsetTop;
  return offset;
}

function getTotalOffsetLeft(element) {
  var offset = 0;
  for(var i = element; i && i.tagName != "BODY"; i = i.offsetParent)
  offset+=i.offsetLeft;
  return offset;
}

function setHint(text, code, field) {
  document.getElementById(field).value = text;
  hidField = field.split("show");
  hidField = hidField[1];
  document.getElementById(hidField).value = code;
  //document.getElementById(field + "Hint").style.display = "none";
}

function executeFuncaoHide(campo) {
  var comm = "document.getElementById('show" + campo + "Hint').style.display = 'none'"
  setTimeout(comm,100);
}

function mouseOverHint(field) {
  document.getElementById(field).style.cursor = "pointer";
  document.getElementById(field).style.backgroundColor = "#0000CC";
  document.getElementById(field).style.color = "white";
}

function mouseOutHint(field) {
  document.getElementById(field).style.backgroundColor = "white";
  document.getElementById(field).style.color = "black";
}
/********************************************************************
* InputTextarea
*******************************************************************/
function checkLength(sId, iMaxchr, evt) {
  var iCharCode = (evt.which) ? evt.which : event.keyCode
  /*
  * charCode == 8                     => permite backspace
  * charCode > 111 && charCode < 124  => permite F1 - F12
  */
  if (iCharCode == 8 || iCharCode == 9) {
    return true;
  }
  if (iCharCode > 111 && iCharCode < 124) {
    return true;
  }
  iTotChars = document.getElementById(sId).value.length;
  if (iTotChars > iMaxchr) {
    return false;
  } else {
    return true;
  }

}

/********************************************************************
* EOF Scripts for Classes
*******************************************************************/

/********************************
* Navigation
********************************/
/* - Conflitava com script da Continente
function createHidden(sHiddenName, sHiddenValue) {
oFormControl = document.getElementById('formControl');
oHid = document.createElement("input");
oHid.setAttribute("type", "hidden");
oHid.setAttribute("name", sHiddenName);
oHid.setAttribute("id", sHiddenName);
oHid.setAttribute("value", sHiddenValue);
oFormControl.appendChild(oHid);
}
*/
function gotoLink(goLink) {
  document.getElementById('pagina').value = goLink;
  document.getElementById('formControl').submit();
}

function setWStatus(status) {
  window.status = status;
}

function rowHover(oRow) {
  document.getElementById(oRow.id).className = 'clickableHover';
}

function rowOut(oRow) {
  document.getElementById(oRow.id).className = 'clickable';
}

/********************************
* Form Handling
********************************/

function searchForm(formId) {
  var params = '?';

  params += getParams(formId);
  gotoPage = getOnForm(formId, 'goto');

  if (gotoPage == 'listProspect') {
    ajaxDivResponse = gotoPage;
    var url = '../action/' + gotoPage + '.php' + params;
    xmlHttp = GetXmlHttpObject(stateChanged);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
  } else if (gotoPage != '') {
    ajaxDivResponse = gotoPage;
    var url = './action/' + gotoPage + '.php' + params;
    xmlHttp = GetXmlHttpObject(stateChanged);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
  } else {
    alert('Response page not defined');
  }
}

function cleanForm(formId) {
  if (document.getElementById(formId) == null) {
    return
  }
  for (i = 0; i < document.getElementById(formId).elements.length; i++) {
    elem = document.getElementById(formId).elements[i];
    if (elem.type == 'checkbox') {
      elem.checked = false;
    } else if (elem.type == 'select-one') {
      elem.selectedIndex = 0;
    } else if (elem.type != 'button' && elem.type != 'hidden') {
      elem.value = "";
    }
  }
}

function submitForm(formId) {
  if (document.getElementById(formId)) {
    document.getElementById(formId).submit();
  } else {
    alert('Error! Form: ' + formId + ' not found!');
  }
}

/********************************
* AJAX
********************************/

function stateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    if (!document.getElementById(ajaxDivResponse)) {
      alert('Div: ' + ajaxDivResponse + ' not found!');
    } else {
      if (xmlHttp.responseText != "") {
        document.getElementById(ajaxDivResponse).innerHTML = xmlHttp.responseText;
      } else {
        document.getElementById(ajaxDivResponse).style.display = "none";
      }
    }
  }
}

function GetXmlHttpObject(handler) {
  var objXmlHttp = null;
  if (navigator.userAgent.indexOf("Opera") >= 0) {
    alert("This example doesn't work in Opera") ;
    return ;
  }
  if (navigator.userAgent.indexOf("MSIE") >= 0) {
    var strName="Msxml2.XMLHTTP"
    if (navigator.appVersion.indexOf("MSIE 5.5") >= 0) {
      strName="Microsoft.XMLHTTP"
    }
    try {
      objXmlHttp = new ActiveXObject(strName);
      objXmlHttp.onreadystatechange = handler ;
      return objXmlHttp;
    } catch(e) {
      alert("Error. Scripting for ActiveX might be disabled");
      return ;
    }
  }
  if (navigator.userAgent.indexOf("Mozilla") >= 0) {
    objXmlHttp = new XMLHttpRequest();
    objXmlHttp.onload = handler;
    objXmlHttp.onerror = handler;
    return objXmlHttp;
  }
}

function GetXmlHttpObjectHint() {
  var objXMLHttp = null;
  if (window.XMLHttpRequest) {
    objXMLHttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return objXMLHttp;
}