function showPopupModal(url) {
	if (window.showModalDialog) {
		window.showModalDialog(url, '_blank', 'titlebar=yes,status=no,resizable=yes,scrollbars=yes');
	} else {
		window.modal(url, '_blank', 'titlebar=yes,status=no,resizable=yes,scrollbars=yes');
		window.focus();
	}
}

function onlyNumber(evtKeyPress) {		
	if (evtKeyPress.ctrlKey)
		return true;
		
	var tecla;	
	tecla = ( evtKeyPress.which ) ? evtKeyPress.which : evtKeyPress.keyCode;		    		 	
	if (tecla > 47 && tecla < 58 || tecla > 95 && tecla < 106) { // numeros de 0 a 9								
		return true;
	}

	// backspace, esquerda, direita, delete, tabulacao
	if (tecla == 8 || tecla == 37 || tecla == 39 || tecla == 46 || tecla == 9 || tecla == 13) 
		return true;
	if ((evtKeyPress.ctrlKey && tecla == 67) ||  // ctrl + c  
		(evtKeyPress.ctrlKey && tecla == 99) ||  // ctrl + c     	
		(evtKeyPress.ctrlKey && tecla == 86) ||  // ctrl + v
		(evtKeyPress.ctrlKey && tecla == 118)) {   // ctrl + v
		return true;
	}
	return false;  
}

function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '-0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 109 && fld.value.size == 0) return true;  // Negative
	if (whichCode == 13) return true;  // Enter
	if (whichCode == 8) return true;  // Delete (Bug fixed)
	if (whichCode == 0) return true;  // Tab
	if (whichCode == 9) return true;  // Tab
	if (whichCode == 37) return true;  // Esquerda
	if (whichCode == 39) return true;  // Direita
	if (whichCode >= 96 && whichCode <= 105 ) // se eh teclado numerico
		whichCode = whichCode - 48;
	key = String.fromCharCode(whichCode);  // Get key value from key code
	
	if (strCheck.indexOf(key) == -1) return false;

	if (fld.selectionStart == 0 && fld.selectionEnd == fld.value.length) fld.value = '';
	len = fld.value.length;
	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) 
			break;
	aux = '';
	for(; i < len; i++) {
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) { 
			aux += fld.value.charAt(i);
		}	
	}		
	if (key == '-' && aux.length > 0) return false;
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

function dateFormat(component, evtKeyPress) {
	var tecla = evtKeyPress.keyCode;
	
	if (tecla == 13) {
		setToday(component);
		return false;
	}
	
	vr = component.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );

	//       home          end
	if (tecla == 35 || tecla == 36) {
		return true;
	} else if (tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) {
		if (component.selectionStart == 0 && component.selectionEnd == component.value.length) component.value = '';
		tam = vr.length + 1 - (component.selectionEnd-component.selectionStart);
		if (tam > 8) {
			evtKeyPress.returnValue = false;
		return false;
		} else if ( tam > 2 && tam < 5 ) {
			component.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		} else if ( tam >= 5 && tam <= 10 ) {
			component.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
		} 
	} else if (tecla != 8 && tecla != 37 && tecla != 39 && tecla != 46 && tecla != 9) {
		evtKeyPress.returnValue = false;
		return false;
	}
	return true;
}

function onClickBtnEasy(botaoID, func) {
	if (func) {
		createInputHidden(botaoID);
		return true
	} 
	return false;
} 

function createInputHidden(buttonID) {
	var param = document.createElement('input');
	param.type = 'hidden';
	param.name = 'button_' + buttonID; 
	param.value = 'click';
	document.getElementById(buttonID).appendChild(param);
}					

function shell(form, code) {
	var newField = document.createElement('input');
	newField.type = 'hidden'; 
	newField.name = 'easyShellCode';
	newField.value = code;
	form.appendChild(newField);
}

function cursorKey(element, event, rowCount) {
	baseId = element.id.substring(0, element.id.indexOf('_index_')+7);
	index = element.id.substring(element.id.indexOf('_index_')+7) * 1;
	if (event.keyCode == 40) {
		index++;
		if (index >= rowCount)
			index = 0;
			
		nextId = baseId + '' + index;
		document.getElementById(nextId).focus();
		document.getElementById(nextId).checked = true;
		return false;
	} else if (event.keyCode == 38) {
		index--;
		if (index < 0)
			index = rowCount-1;
			
		nextId = baseId + '' + index;
		document.getElementById(nextId).focus();
		document.getElementById(nextId).checked = true;
		return false;
	} else if (event.keyCode == 13) {
		element.click();
		return false;
	}
	return true;
}

function nextFocus(element, goNext) {
	if (goNext == false) {
		element.focus();
		if (element.select)
			element.select();
	} else {
		finded = false;
		for (i=0; i<element.form.elements.length; i++) {
			el = element.form.elements[i];
			if (el.tabIndex == (element.tabIndex+1)) {
				if (el && el.style.display != 'none') {
					finded = true;
					el.focus();
					if (el.select)
						el.select();
				}
				break;
			}
		}
		if (! finded) {
			element.focus();
			if (element.select)
				element.select();
		};
	}
}

function setToday(element) {
	today = new Date();
	day = today.getDate();
	month = today.getMonth()+1;
	year = today.getYear()+1900; //alguem me explica isso!!! soh dentro do ef que precisa fazer isso
	if (day < 10)
		day = "0" + day;
	if (month < 10)
		month = "0" + month;
	element.value = day + '/' + month + '/' + year;
}

function focus(elementID) {
	document.getElementById(elementID).focus();
	return true;
}

function removeFocus(elementID) {
	document.getElementById(elementID).blur();
	return true;
}

function mask(elementID, sMask, evtKeyPress) {
	 var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

     if(document.all) { // Internet Explorer 
        nTecla = evtKeyPress.keyCode; 
     }
     else { // Nestcape e FireFox
     	nTecla = evtKeyPress.which;
     }

     if (nTecla == 8)  // backspace
     	return true;
     
     sValue = document.getElementById(elementID).value;

     // Limpa todos os caracteres de formata??o que j? estiverem no campo.
     sValue = replaceAll(sValue.toString(), "-", "");
     sValue = replaceAll(sValue.toString(), ".", "");
     sValue = replaceAll(sValue.toString(), "/", "");
     sValue = replaceAll(sValue.toString(), "(", "");
     sValue = replaceAll(sValue.toString(), ")", "");
     sValue = replaceAll(sValue.toString(), " ", "");
     
     i = 0;
     nCount = 0;
     sCod = "";
     fldLen = sValue.length;
     mskLen = fldLen;

     while (i <= mskLen) {
       bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
       bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

       if (bolMask) {
         sCod += sMask.charAt(i);
         mskLen++;
	   }
       else {
         sCod += sValue.charAt(nCount);
         nCount++;
       }
       i++;
     }

	 document.getElementById(elementID).value = sCod;
	     
 	 if (sCod.length >= sMask.length)
 		return false;      		
 	 
     if (sMask.charAt(i-1) == "9") { // apenas n?meros...
        return (nTecla > 47 && nTecla < 58);   // n?meros de 0 a 9
     }
     return true;
}

function replaceAll(string, text, by) {
	// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replaceAll(string.substring(i + txtLength,strLength), text, by);

    return newstr;
}

function changeDefaultButton(evt, buttonID) {
	// cancel the default submit
    evt.returnValue=false;
    evt.cancel = true;
    // submit the form by programmatically clicking the specified button
    var elementID = evt.srcElement.id;
    var baseId = elementID.substring(0, elementID.lastIndexOf(':')+1);
    if (document.getElementById(baseId + buttonID) != null) {
    	document.getElementById(baseId + buttonID).click();
    }
}

/** Formata o valor currency para o tipo double (utilizado em expressões de cálculos) */
function formatToDouble(valor) {
	valor = replaceAll(valor, '.', '')
	return replaceAll(valor, ',', '.') * 1;
} 

/* Formata o valor double (valor no formato - ####.##) para o tipo currency  */
function formatToCurrency(valorDouble, milSep, decSep) {
	if (valorDouble == null || valorDouble == ''){
		return '';
	}
	var strValor = + valorDouble + '';
	var indexSeparadorDecimal = strValor.indexOf('.');
	var decimal = '';
	// separa a parte decimal
	if (indexSeparadorDecimal != -1) {
		decimal = strValor.slice(indexSeparadorDecimal + 1);
		strValor = strValor.substring(0, indexSeparadorDecimal)
	}
	// numero de casas decimais
	var totalCasasDecimais = 2;
	// verifica a quantidade de dígitos da parte decimal
	for (var i = decimal.length; i < totalCasasDecimais; i++) {
		decimal += '0';
	}
	// insere os separadores de milhar
	var aux = '';
	for (var i = strValor.length - 1, j = 1; i >= 0; i--, j++) {
		aux += strValor.charAt(i);
		if ((j % 3) == 0 && i >= 1) {
			aux += milSep;
		}
	}
	// restaura o valor para a forma original (reverse)
	strValor = '';
	for (var i = aux.length - 1; i >= 0; i--) {
		strValor += aux.charAt(i);
	}
	return strValor + decSep + decimal;
}

String.prototype.isArgument=function()
{
	return /^([a-zA-Z]){1,}=([0-9]){1,}$/.test(this);
}

function dialog(url,name,feature,isModal)
{
 if(url==null){return false;}
 url = url
 if(name==null){name=""}
 if(feature==null){feature=""};
 if(window.showModelessDialog)
 {
  	var WindowFeature = new Object();
	WindowFeature["width"] = 400;
	WindowFeature["height"]  =400;
	WindowFeature["left"]  = "";
	WindowFeature["top"]  =  "";
	WindowFeature["resizable"]  = "";

	if(feature !=null && feature!="")
	{
      feature = ( feature.toLowerCase()).split(",");
	
      for(var i=0;i< feature.length;i++)
		{
          if( feature[i].isArgument())
			{
               var featureName = feature[i].split("=")[0];
			   var featureValue = feature[i].split("=")[1];
			  
			   if(WindowFeature[featureName]!=null){WindowFeature[featureName] = featureValue; }
			}
		}
	}
 
  if(WindowFeature["resizable"]==1 || WindowFeature["resizable"]=="1" || WindowFeature["resizable"].toString().toLowerCase()=="yes"){WindowFeature["resizable"] = "resizable:1;minimize:1;maximize:1;"}
  if(WindowFeature["left"]!=""){WindowFeature["left"] ="dialogLeft:" +  WindowFeature["left"] +"px;";}
  if(WindowFeature["top"]!=""){WindowFeature["top"] ="dialogTop:" +  WindowFeature["Top"] +"px;"; }
  if(window.ModelessDialog ==null){window.ModelessDialog = new Object() ; };
  if(name!="")
  {
   if(window.ModelessDialog[name]!=null && !window.ModelessDialog[name].closed )
   {
     window.ModelessDialog[name].focus();
	 return window.ModelessDialog[name];
   }
  }
	var F = WindowFeature["left"] +WindowFeature["top"] +  "dialogWidth:"+WindowFeature["width"] +" px;dialogHeight:"+WindowFeature["height"]+"px;center:1;help:0;" + WindowFeature["resizable"] +"status:0;unadorned:0;edge: raised; ;border:thick;"
	if(isModal)
	{
		window.showModalDialog(url,self,F);
		return false;
	}
	else
	{
		window.ModelessDialog[name] = window.showModelessDialog(url,self,F);
		return window.ModelessDialog[name];
	}	
 }
 else
 {
   if(document.getBoxObjectFor)
   {
	

	 if(isModal)
	 {		 
		 var Modal = window.open(url,name,"modal=1," + feature);
		 var ModalFocus = function()
		 {
		 	if (Modal) {
				if(!Modal.closed) { 
					Modal.focus();
				}
				else{
					Modal =null;
					//window.removeEventListener(ModalFocus,"focus");
					ModalFocus = null; 
				};
			}
		 }
		 window.addEventListener( "focus",ModalFocus, false ); 
		 return false;
	 }
	 else
	 {
		return window.open(url,name,"modal=1," + feature);
	 }	 
   }
   else
   { 
     return window.open(url,name,feature);
   }
   //
 }
 return null;
}

function modal(url,feature)
{
	dialog(url,"",feature,true);
	return false;
}

function closeIFrame(frameID) {
	var element = document.getElementById('div' + frameID);
	if (element != null) {
		element.style.display='none';
		jt_DialogBox.veilShow(false);
	}	
}