/*************************************************************************}
{*                                                                       *}
{* Author   : Alcindo Schleder                                           *}
{* Copyright: © 2003 by Alcindo Schleder. All rights reserved.           *}
{* Created  : 19/05/2007 - DD/MM/YYYY                                    *}
{* Modified :                                                            *}
{* Version  : 1.0.0.0                                                    *}
{* License  : you can freely use and distribute the included code        *}
{*            for any purpouse, but you cannot remove this copyright     *}
{*            notice. Send me any comments and updates, they are really  *}
{*            appreciated. This software is licensed under MPL License,  *}
{*            see http://www.mozilla.org/MPL/ for details                *}
{* Contact  : alcindo@sistemaprocessa.com.br                             *}
{*            alcindo@processa.org                                       *}
{*            alcindo@processavoip.net                                   *}
{*            http://www.sistemaprocessa.com.br                          *}
{*            http://www.processa.org                                    *}
{*            http://www.processavoip.net                                *}
{*                                                                       *}
{*************************************************************************/

// Form Functions

function SendEditorForm(pForm, pTarget) {
  if ((pForm) && (pTarget)) {
    for (i = 0; i < parent.frames.length; ++i)
       if (parent.frames[i].FCK)
         parent.frames[i].FCK.UpdateLinkedField();
    return SendAjaxForm(pForm, pForm.method, pTarget)
  } else return false;
}

function addslashes(pStr) {
  var slash = "\\&";
  var value = "&";
  pStr = pStr.replace(value, slash);
  return pStr;
}

/*
function ComparePassword(pPwd, pConf) {
	 if ((pPwd) && (pConf)) {
			 var aPwd = document.getElementById(pPwd);
			 var aConf = document.getElementById(pConf);
    if (aPwd.value != aConf.value) {
      alert('As senhas informadas s&atilde;o diferentes!! (' + aPwd.value + ' - ' + aConf.value + '). Por favor digite-as novamente!');
	     aPwd.value = '';	
	     aConf.value = '';	
      aPwd.focus;
	     return false; 
    } else
      return true;
		} else
	   return false;
}
*/

function MaskField(o,f) {
    v_obj = o;
    v_fun = f;
    setTimeout("ExecMask()",1);
}

function ExecMask() {
    v_obj.value = v_fun(v_obj.value);
}

function Phone(v){
    v=v.replace(/\D/g,"");                 
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2"); 
    v=v.replace(/(\d{4})(\d)/,"$1-$2");    
    return v;
}

function ZipCode(v) {
    v=v.replace(/D/g,"");                
    v=v.replace(/^(\d{5})(\d{3})/,"$1-$2"); 
    return v;
}

function IntegerNumber(v) {
    v=v.replace(/D/g,"");                
    v=v.replace(/[^0-9-]/g, ""); 
    return v;
}

function GenericFloat(pValue, pDecPlaces) {
	 if ((!pDecPlaces) || (pDecPlaces == '') || (pDecPlaces == 'undefined') || (!pDecPlaces.match(/[0-9]+/)))
		  pDecPlaces = '2';
		if (pDecPlaces.length > 1)
		  pDecPlaces = pDecPlaces[0];
  var aRegExp = '([0-9]+)([0-9]{' + pDecPlaces + '})';
		aRegExp = new RegExp(aRegExp);
		var s = pValue;
  pValue = pValue.replace(/([^0-9]+)/g, ""); // delete all characters from string except digits 0-9
//  pValue = pValue.replace(/([^0-9,\.]+)/g, ""); // delete all characters from string except digits 0-9, '-' and decimal point
//		var dec = aS.replace(/\w+[.,]|[^0-9]+/, ""); // get the fractional portion
//		pValue = pValue.substring(0, pValue.length - dec) + decSep + pValue.substring(pValue.length - dec, pValue.length);
  while (pValue.length < (pDecPlaces - 1))
		  pValue = '0' + pValue;
  pValue = pValue.replace(aRegExp, "$1.$2");
		pValue = pValue.replace(/([0]+(?=[\.,]))?(^[0]+(?=[1-9]))?(^[0]+(?=[0-9]))/, ""); // delete all zeros before digits 1-9 and before decimal point
//		pValue = pValue.replace(/[,]/, "."); // set character '.' to decimal point (generic float point)
	 if (s.match(/[-]/))
		  pValue = '-' + pValue;
		return pValue;
}

function Currency(v, pDP) {
	 if ((!pDP) || (pDP == '') || (pDP == 'undefined') || (!pDP.match(/[0-9]+/)))
		  pDP = '2';
		if (pDP.length > 1)
		  pDP = pDP[0];
		v = GenericFloat(v, pDP);
		v = v.replace(/[.]/, ","); // set character ',' to decimal point for brazilian currency
		s = v.replace(/(^[-])|([.,][0-9]+)/g, ""); // get the integer portion
  if (s.length < 4) 
    v=v.replace(/([-0-9]+)([\.,0-9]{2})/, "$1$2");
		else 
    if (s.length < 7) 
      v=v.replace(/([-0-9]{1,3})([0-9]{3})([\.,0-9]{2})/, "$1.$2$3");
 			else
      if (s.length < 10) 
        v=v.replace(/([-0-9]{1,3})([0-9]{3})([0-9]{3})([\.,0-9]{2})/, "$1.$2.$3$4");
    		else 
        if (s.length < 13) 
          v=v.replace(/([-0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([\.,0-9]{2})/, "$1.$2.$3.$4$5");
				  		else
          if (s.length < 15) 
            v=v.replace(/([-0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{3})([\.,0-9]{2})/, "$1.$2.$3.$4.$5$6");
										else
            v=v.replace(/([-0-9]+)([\.,0-9]{2})/, "$1$2");
		return v;
} 

function cpf(v){
    v=v.replace(/\D/g,"");                    
    v=v.replace(/(\d{3})(\d)/,"$1.$2");       
    v=v.replace(/(\d{3})(\d)/,"$1.$2");       
                                             
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2"); 
    return v;
}
 
function Data(v){
    v=v.replace(/\D/g,"");                    
    v=v.replace(/(\d{2})(\d)/,"$1/$2");       
    v=v.replace(/(\d{2})(\d)/,"$1/$2");       
                                             
    v=v.replace(/(\d{2})(\d{2})$/,"$1$2") ;
    return v;
}

function cnpj(v){
    v=v.replace(/\D/g,"");
    v=v.replace(/^(\d{2})(\d)/,"$1.$2");
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3");
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2");          
    v=v.replace(/(\d{4})(\d)/,"$1-$2");            
    return v;
}

function UpdateData(pForm, pOp, pTarget, pFile) {
  if ((!pForm) || (pForm == '') || (!pOp) || (pOp == '') || (!pTarget) || (pTarget == '')) 
    return false;
  aForm = document.getElementById(pForm);
  if (!aForm) return false;
  if ((pOp == 'opNew') && (pFile) && (pFile != ''))
    GetAjaxLink(pFile, pTarget);
  else
    if (pOp == 'opCancel')
      aForm.reset(); 
    else
      if (pOp == 'opDelete') {
        aForm.op.value='opDelete';
        SendAjaxForm(aForm, 'POST', pTarget);
      }
      else
        if ((pOp == 'opInsert') || (pOp == 'opUpdate')) 
          pForm.submit();
}

function CryptoGraphp(pDir, pCfg, pSID) {
  document.images.cryptogram.src=pDir+'/cryptographp.php?cfg="' + pCfg + '"&"' + pSID + '"&' + Math.round(Math.random(0)*1000) + 1;
}

//Formata numero tipo moeda usando o evento onKeyDown
function FormatCurrency(pObj, e) {
 	if (pObj) {
  	 if ((e.keyCode >= 48 && e.keyCode <= 58) || (e.keyCode == 8)) {
		    if (e.keyCode != 8)
  		  	 pObj.value = pObj.value + e.keyCode;
						else
  		  	 pObj.value = pObj.value.substring(0, pObj.value.length - 1);
  				MaskField(pObj, Currency);
				} else 
				  return false;
		}	else
  	 return false;
} 
// **************************************************
// * Autor : Peter M Jordan - uranking@uranking.com *
// * pagina: www.uranking.com                       *
// **************************************************

// construindo o calendario
function popdate(obj,div,tam,ddd)
{
    if (ddd) 
    {
        day = ""
        mmonth = ""
        ano = ""
        c = 1
        char = ""
        for (s=0;s<parseInt(ddd.length);s++)
        {
            char = ddd.substr(s,1)
            if (char == "/") 
            {
                c++; 
                s++; 
                char = ddd.substr(s,1);
            }
            if (c==1) day    += char
            if (c==2) mmonth += char
            if (c==3) ano    += char
        }
        ddd = mmonth + "/" + day + "/" + ano
    }
  
    if(!ddd) {today = new Date()} else {today = new Date(ddd)}
    date_Form = eval (obj)
    if (date_Form.value == "") { date_Form = new Date()} else {date_Form = new Date(date_Form.value)}
  
    ano = today.getFullYear();
    mmonth = today.getMonth ();
    day = today.toString ().substr (8,2)
  
    umonth = new Array ("Janeiro", "Fevereiro", "Mar&ccedil;o", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro")
    days_Feb = (!(ano % 4) ? 29 : 28)
    days = new Array (31, days_Feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

    if ((mmonth < 0) || (mmonth > 11))  alert(mmonth)
    if ((mmonth - 1) == -1) {month_prior = 11; year_prior = ano - 1} else {month_prior = mmonth - 1; year_prior = ano}
    if ((mmonth + 1) == 12) {month_next  = 0;  year_next  = ano + 1} else {month_next  = mmonth + 1; year_next  = ano}
    txt  = "<table bgcolor='#efefff' style='border:solid #330099; border-width:2' cellspacing='0' cellpadding='3' border='0' width='"+tam+"' height='"+tam*1.1 +"'>"
    txt += "<tr bgcolor='#FFFFFF'><td colspan='7' align='center'><table border='0' cellpadding='0' width='100%' bgcolor='#FFFFFF'><tr>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+((mmonth+1).toString() +"/01/"+(ano-1).toString())+"') class='Cabecalho_Calendario' title='Ano Anterior'><<</a></td>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+( "01/" + (month_prior+1).toString() + "/" + year_prior.toString())+"') class='Cabecalho_Calendario' title='M&ecirc;s Anterior'><</a></td>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+( "01/" + (month_next+1).toString()  + "/" + year_next.toString())+"') class='Cabecalho_Calendario' title='Pr&oacute;ximo Mês'>></a></td>"
    txt += "<td width=20% align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+((mmonth+1).toString() +"/01/"+(ano+1).toString())+"') class='Cabecalho_Calendario' title='Pr&oacute;ximo Ano'>>></a></td>"
    txt += "<td width=20% align=right><a href=javascript:force_close('"+div+"') class='Cabecalho_Calendario' title='Fechar Calend&aacute;rio'><b>X</b></a></td></tr></table></td></tr>"
    txt += "<tr><td colspan='7' align='right' bgcolor='#ccccff' class='mes'><a href=javascript:pop_year('"+obj+"','"+div+"','"+tam+"','" + (mmonth+1) + "') class='mes'>" + ano.toString() + "</a>"
    txt += " <a href=javascript:pop_month('"+obj+"','"+div+"','"+tam+"','" + ano + "') class='mes'>" + umonth[mmonth] + "</a> <div id='popd' style='position:absolute'></div></td></tr>"
    txt += "<tr bgcolor='#330099'><td width='14%' class='dia' align=center><b>Dom</b></td><td width='14%' class='dia' align=center><b>Seg</b></td><td width='14%' class='dia' align=center><b>Ter</b></td><td width='14%' class='dia' align=center><b>Qua</b></td><td width='14%' class='dia' align=center><b>Qui</b></td><td width='14%' class='dia' align=center><b>Sex<b></td><td width='14%' class='dia' align=center><b>Sab</b></td></tr>"
    today1 = new Date((mmonth+1).toString() +"/01/"+ano.toString());
    diainicio = today1.getDay () + 1;
    week = d = 1
    start = false;

    for (n=1;n<= 42;n++) 
    {
        if (week == 1)  txt += "<tr bgcolor='#efefff' align=center>"
        if (week==diainicio) {start = true}
        if (d > days[mmonth]) {start=false}
        if (start) 
        {
            dat = new Date((mmonth+1).toString() + "/" + d + "/" + ano.toString())
            day_dat   = dat.toString().substr(0,10)
            day_today  = date_Form.toString().substr(0,10)
            year_dat  = dat.getFullYear ()
            year_today = date_Form.getFullYear ()
            colorcell = ((day_dat == day_today) && (year_dat == year_today) ? " bgcolor='#FFCC00' " : "" )
            txt += "<td"+colorcell+" align=center><a href=javascript:block('"+  d + "/" + (mmonth+1).toString() + "/" + ano.toString() +"','"+ obj +"','" + div +"') class='data'>"+ d.toString() + "</a></td>"
            d ++ 
        } 
        else 
        { 
            txt += "<td class='data' align=center> </td>"
        }
        week ++
        if (week == 8) 
        { 
            week = 1; txt += "</tr>"} 
        }
        txt += "</table>"
        div2 = eval (div)
        div2.innerHTML = txt 
}
  
// funcao para exibir a janela com os meses
function pop_month(obj, div, tam, ano)
{
  txt  = "<table bgcolor='#CCCCFF' border='0' width=80>"
  for (n = 0; n < 12; n++) { txt += "<tr><td align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+("01/" + (n+1).toString() + "/" + ano.toString())+"')>" + umonth[n] +"</a></td></tr>" }
  txt += "</table>"
  popd.innerHTML = txt
}

// funcao para exibir a janela com os anos
function pop_year(obj, div, tam, umonth)
{
  txt  = "<table bgcolor='#CCCCFF' border='0' width=160>"
  l = 1
  for (n=1991; n<2012; n++)
  {  if (l == 1) txt += "<tr>"
     txt += "<td align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+(umonth.toString () +"/01/" + n) +"')>" + n + "</a></td>"
     l++
     if (l == 4) 
        {txt += "</tr>"; l = 1 } 
  }
  txt += "</tr></table>"
  popd.innerHTML = txt 
}

// funcao para fechar o calendario
function force_close(div) 
    { div2 = eval (div); div2.innerHTML = ''}
    
// funcao para fechar o calendario e setar a data no campo de data associado
function block(data, obj, div)
{ 
    force_close (div)
    obj2 = eval(obj)
    obj2.value = data 
}

function ValidateForm(pForm) {
  if (pForm) {
    for (i=0;i<pForm.elements.length;i++) {
      var aRet=ValidateFormField(pForm.elements[i], true);
      if (!aRet) return aRet;
    }
  }
}

function SetLinkButton(pButton, pLink, pTarget) {
  if (pButton) {
    if (pButton.value == '>>') {
      pButton.value = '<<';
      GetAjaxLink(pLink, pTarget);
    }
    else {
      var aTarget = document.getElementById(pTarget);
      if (aTarget) {
        for (var i=aTarget.childNodes.length; i>0; i--) { // percorre os div's criados.
          aTarget.removeChild(aTarget.childNodes.item(i-1));                
        }
        aTarget.innerHTML = '';
      }
      pButton.value = '>>';
    }
  }
}
