// JavaScript Document
function SetPerson(pRequired) {
		var aValue = '';
		var aReq   = 'u_required="true" '
		var aHtml  = '';
		aField = document.getElementById("Name");
		if ((aField) && (aField.value != 'undefined'))
		  aValue = aField.value;
  aObj = document.getElementById("dvLabelName");
  if (aObj)
    aObj.innerHTML = 'Nome:&nbsp;&nbsp;';
  aObj = document.getElementById("dvFieldName");
  if (aObj) {
				aHtml = '<input name="Name" type="text" id="Name" size="50" ';
				if (pRequired) 
				  aHtml = aHtml + aReq;
				aHtml = aHtml + ' u_invalid="Campo Nome deve ser preechido!!" value="' + aValue + '" u_hint="Digite o nome do cliente" />';
    aObj.innerHTML =  aHtml;
		}
  aObj = document.getElementById("Name");
  if (aObj) {
    aObj.className="form-field-select";
    aObj.onfocus=SetFieldFocus;
    aObj.onblur=SetFieldBlur;
    aObj.onmouseover=SetFieldMouseOver;
    aObj.onmouseout=SetFieldMouseOut;
		}
		aField = document.getElementById("DocPri");
  aObj = document.getElementById("dvLabelDocPri");
  if (aObj)
    aObj.innerHTML = 'C.P.F.:&nbsp;&nbsp;';
  aObj = document.getElementById("dvFieldDocPri");
  if (aObj) {
				aHtml = '<input name="DocPri" type="text" id="DocPri" size="20" maxlength="14" ';
				if (pRequired) 
				  aHtml = aHtml + aReq;
				aHtml = aHtml + ' value="" u_hint="Digite o C.P.F. do cliente" onkeypress="MaskField(this, cpf);" u_invalid="Campo C.P.F. deve ser preechido!!" u_validate="CPF" />';
    aObj.innerHTML = aHtml;
		}
  aObj = document.getElementById("DocPri");
  if (aObj) {
    aObj.className="form-field-select";
    aObj.onfocus=SetFieldFocus;
    aObj.onblur=SetFieldBlur;
    aObj.onmouseover=SetFieldMouseOver;
    aObj.onmouseout=SetFieldMouseOut;
  }
  aObj = document.getElementById("dvLabelDocSec");
  if (aObj)
    aObj.innerHTML = 'R.G.:&nbsp;&nbsp;';
  aObj = document.getElementById("DocSec");
  if (aObj) {
 		 aObj.setAttribute("u_hint", "Digite o R.G. do cliente");
 		 aObj.setAttribute("u_validate", "Campo R.G. deve ser preenchido!!");
		}
		aObj = document.getElementById("dvFormRowManager"); 
  if (aObj)
    aObj.className = "dvFormRowInvisible";
		aObj = document.getElementById("dvFormRowBirthday");
  if (aObj)
    aObj.className = "dvFormRow";
}

function SetCompany(pRequired) {
		var aValue = '';
		var aReq   = 'u_required="true" '
		var aHtml  = '';
		aField = document.getElementById("Name");
		if ((aField) && (aField.value != 'undefined'))
		  aValue = aField.value;
  aObj = document.getElementById("dvLabelName");
  if (aObj)
    aObj.innerHTML = 'Raz&atilde;o Social:&nbsp;&nbsp;';
  aObj = document.getElementById("dvFieldName");
  if (aObj) {
				aHtml = '<input name="Name" type="text" id="Name" size="50" ';
				if (pRequired) 
				  aHtml = aHtml + aReq;
				aHtml = aHtml +  ' u_invalid="Campo Ra&atilde;o Social deve ser preechido!!" value="' + aValue + '" u_hint="Digite a Ra&atilde;o Social da Empresa" />';
    aObj.innerHTML = aHtml;
		}
  aObj = document.getElementById("Name");
  if (aObj) {
    aObj.className="form-field-select";
    aObj.onfocus=SetFieldFocus;
    aObj.onblur=SetFieldBlur;
    aObj.onmouseover=SetFieldMouseOver;
    aObj.onmouseout=SetFieldMouseOut;
		}
		aField = document.getElementById("DocPri");
  aObj = document.getElementById("dvLabelDocPri");
  if (aObj)
    aObj.innerHTML = 'C.N.P.J.:&nbsp;&nbsp;';
  aObj = document.getElementById("dvFieldDocPri");
  if (aObj) {
				aHtml = '<input name="DocPri" type="text" id="DocPri" size="20" maxlength="18" ';
				if (pRequired)
				  aHtml = aHtml + aReq;
				aHtml = aHtml + ' value="" u_hint="Digite o C.N.P.J. da empresa" onkeypress="MaskField(this, cnpj);" u_invalid="Campo C.N.P.J. deve ser preechido!!" u_validate="CNPJ" />';
    aObj.innerHTML = aHtml;
		}
  aObj = document.getElementById("DocPri");
  if (aObj) {
    aObj.className="form-field-select";
    aObj.onfocus=SetFieldFocus;
    aObj.onblur=SetFieldBlur;
    aObj.onmouseover=SetFieldMouseOver;
    aObj.onmouseout=SetFieldMouseOut;
		}
  aObj = document.getElementById("DocSec");
  if (aObj) {
 		 aObj.setAttribute("u_hint", "Digite a Iinscr&ccedil;&atilde;o Estadual da Empresa");
 		 aObj.setAttribute("u_validate", "Campo Iinscr&ccedil;&atilde;o Estadual deve ser preenchido!!");
		}
  aObj = document.getElementById("dvLabelDocSec");
  if (aObj)
    aObj.innerHTML = 'Inscr. Est.:&nbsp;&nbsp;';
  aObj = document.getElementById("dvFormRowManager");
  if (aObj)
    aObj.className = "dvFormRow";
  aObj = document.getElementById("dvFormRowBirthday");
  if (aObj)
    aObj.className = "dvFormRowInvisible";
}

function getStates(pCountry, pTarget) {
  if (((pCountry) || (pCountry!="")) && ((pTarget) || (pTarget!=""))) 
    GetAjaxLink("./getState.php?fk_country=" + pCountry, pTarget);
}

function getCities(pCountryField, pState, pTarget) {
  if (((pCountryField) || (pCountryField!="")) && ((pState) || (pState!="")) && ((pTarget) || (pTarget!=""))) {
			 var pObj = document.getElementById(pCountryField);
				if (pObj) 
      GetAjaxLink("./getCity.php?fk_country=" + pObj.value + '&fk_state=' + pState, pTarget);
		}
}

function CalcBalance(pNewValue, pTotValue, pAccValue, pTotField, pAccField, pCRDB) {
  if ((pNewValue) && (pTotValue != '') && (pAccValue != '') && (pTotField != '') && (pAccField != '') && (pCRDB != '')) {
    if ((pCRDB == 'C') || (pCRDB == 'D')) 
      aDC = pCRDB;
    else {
      var aCRDBObj = document.getElementById(pCRDB);
		  		var aDC = 'C';
				  if (aCRDBObj) {
					   if ((aCRDBObj.value == 'D') && (aCRDBObj.checked))
      		  aDC = 'D';
		  			 if ((aCRDBObj.value == 'C') && (aCRDBObj.checked))
      		  aDC = 'C';
  				}
    }
    var aTotBalance  = Number(document.getElementById(pTotValue).value);
    var aAccBalance  = Number(document.getElementById(pAccValue).value);
				var aNewBalance  = Number(GenericFloat(pNewValue.value));
				if ((aNewBalance > aTotBalance) && (aDC == 'C')) {
      alert('C&aacute;lculo do Saldo: Saldo do cliente n&atilde;o &eacute; suficiente para esta opera&ccedil;&atilde;o!!! (' + aDC + ')');
  				return false;
  		}
				if ((aNewBalance > aAccBalance) && (aDC == 'D')) {
				  alert('C&aacute;lculo do Saldo: Saldo da Conta n&atilde;o &eacute; suficiente para esta opera&ccedil;&atilde;o!!! (' + aDC + ')');
						return false;
  		}
				if ((aTotBalance != 'NaN') && (aAccBalance != 'NaN') && (aNewBalance != 'NaN')) {
					 if (aNewBalance > 0) {
  						var aTotObj = document.getElementById(pTotField);
  						if (aTotObj) {
  								if (aDC == 'D')
  						    aTotBalance = aTotBalance + aNewBalance;
  								if (aDC == 'C')
  						    aTotBalance = aTotBalance - aNewBalance;
          if (aTotObj.value)
      						aTotObj.value = Currency(String(aTotBalance.toFixed(2)));
          else
      						aTotObj.innerHTML = Currency(String(aTotBalance.toFixed(2)));
  						}
  						var aAccObj = document.getElementById(pAccField);
  						if (aAccObj) {
  								if (aDC == 'D')
  						    aAccBalance = aAccBalance - aNewBalance;
  								if (aDC == 'C')
  						    aAccBalance = aAccBalance + aNewBalance;
          if (aAccObj.value)
      						aAccObj.value = Currency(String(aAccBalance.toFixed(2))); 
          else
      						aAccObj.innerHTML = Currency(String(aAccBalance.toFixed(2)));
  						}
    			 MaskField(pNewValue, Currency);
   					return true;
						}
				} else {
				  alert('C&aacute;lculo do Saldo: Campos num&eacute;ricos inv&aacute;lidos!!! ' + '\r\n ' + pTotValue + ': ' + aTotBalance + '\r\n ' + pAccValue + ': ' + aAccBalance + '\r\n ' + pNewValue.name + ': ' + aNewBalance);
						return false;
				}
		} else {
		  alert('C&aacute;lculo do Saldo: Par&acirc;metros inv&aacute;lidos!!!');
				return false;
		}
}

function ChangeCustomerBalace(pBalance, pTotalBalance, pDBCR) {
  var aBalanceObj = document.getElementById(pBalance);
  var aTotalBalanceObj = document.getElementById(pTotalBalance);
  if ((aBalanceObj) && (aTotalBalanceObj)) {
    var aBalance      = Number(GenericFloat(aBalanceObj.value));
    var aTotalBalance = Number(GenericFloat(aTotalBalanceObj.value));
				if ((aBalance != 'NaN') && (aTotalBalance != 'NaN')) {
      if (pDBCR == 'D')
        var aNewBalance = aTotalBalance + aBalance;
      if (pDBCR == 'C')
        var aNewBalance = aTotalBalance - aBalance;
      aTotalBalanceObj.value = aNewBalance;
    }
  }
}

function setMenuClass(pClickedObj, pNewClass, pDeactivateObj) {
  if ((pClickedObj != '') && (pNewClass != '') && (pDeactivateObj != '')) {
  		var aObj = document.getElementById(pClickedObj);
  		aObj.className = pNewClass;
  		aObj = document.getElementById(pDeactivateObj);
   	aObj.className='';
		}
}

function getAjaxFromSelected(pLink, pValue, pTarget) {
  GetAjaxLink(pLink + pValue, pTarget);
}

function SetActiveMenu(pObj, pList) {
  if ((pObj) && (pList)) {
    var aList = document.getElementById(pList);
    if (aList) {
      var aLinks = [];
      aLinks = aList.getElementsByTagName('a');
      if (aLinks != null) {
        for (i = 0; i < aLinks.length; i++) {
//          if (aLinks[i].class) ver como tratar quando troca o browser pois o mozzila só usa a propriedade class
//            aLinks[i].class = 'inactive';
          aLinks[i].className = '';
          if (aLinks[i] == pObj) {
//            if (aLinks[i].class) 
//              aLinks[i].class = 'active';
            aLinks[i].className = 'active';
          }
        }
      }
    }
  }
}

function GetCitiesFromName(pObj, pTarget) {
  var aObj = document.getElementById(pObj);
  if (aObj) {
    var aLink = './SearchCity.php?DscCity=' + aObj.value;
    GetAjaxLink(aLink, pTarget);
  }
}

function GetNextTariff(pValue, pLink, pTarget) {
  GetAjaxLink(pLink + pValue, pTarget);
}
