function insertFlash(id,src,width,height,wmode,menu,flashVars,title,flashVersion) {
  var pidObject = (id==null)? "" : 'id="'+id+'"';
  var pidEmbed = (id==null)? "" : 'name="'+id+'"'; 
  var pwmode = (wmode==null)? "" : wmode;
  var pmenu = (menu==null)? true : false;
  var pflashVars = (flashVars==null)? "" : flashVars;
  var ptitle = (title==null)? "" : title; 
  var pflashVersion = (flashVersion==null)? "6,0,29,0" : flashVersion;

  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+pflashVersion+' " width="'+width+'" height="'+height+'" pidObject  title="'+ptitle+'">');
  document.write('<param name="movie" value="../../js/'+src+'">');
  document.write ('<param name="quality" value="high">');
  document.write('<param name="flashVars" value="'+pflashVars+'">');
  document.write('<param name="wmode" value="'+pwmode+'">'); 
  document.write('<param name="menu" value="'+pmenu+'">');
  document.write('<embed src="../../js/'+src+'" quality="high" pidEmbed menu="'+pmenu+'"  wmode="'+pwmode+'" flashvars="'+pflashVars+'" pluginspage=" http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'"></embed>');
  document.write('</object>');

}

//-----------------
  function insertFlashChart(id,src,width,height,wmode,menu,flashVars,title,flashVersion,bg_color) {
  var pidObject = (id==null)? "" : 'id="'+id+'"';
  var pidEmbed = (id==null)? "" : 'name="'+id+'"'; 
  var pwmode = (wmode==null)? "" : wmode;
  var pmenu = (menu==null)? true : false;
  var pflashVars = (flashVars==null)? "" : flashVars;
  var ptitle = (title==null)? "" : title; 
  var pflashVersion = (flashVersion==null)? "8,0,0,0" : flashVersion;
  var bg_color = (bg_color==null)? "FFFFFF" : bg_color;

  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+pflashVersion+' " width="'+width+'" height="'+height+'" pidObject  title="'+ptitle+'">');
  document.write('<param name="movie" value="../../js/'+src+'">');
  document.write ('<param name="quality" value="high">');
  document.write('<param name="flashVars" value="'+pflashVars+'">');
  document.write('<param name="wmode" value="'+pwmode+'">'); 
  document.write('<param name="menu" value="'+pmenu+'">');
  document.write('<PARAM NAME=bgcolor VALUE="#'+bg_color+'">');
  document.write('<embed src="../../js/'+src+'" quality="high" pidEmbed menu="'+pmenu+'"  wmode="'+pwmode+'" flashvars="'+pflashVars+'" pluginspage=" http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" ALIGN="" swLiveConnect="true" NAME="charts" bgcolor="#'+bg_color+'"></embed>');
  document.write('</object>');

 }
//-----------------
/*
  function insertFlashChart2(id,src,width,height,wmode,menu,flashVars,title,flashVersion,bg_color,destino) {
  var pidObject = (id==null)? "" : 'id="'+id+'"';
  var pidEmbed = (id==null)? "" : 'name="'+id+'"'; 
  var pwmode = (wmode==null)? "" : wmode;
  var pmenu = (menu==null)? true : false;
  var pflashVars = (flashVars==null)? "" : flashVars;
  var ptitle = (title==null)? "" : title; 
  var pflashVersion = (flashVersion==null)? "8,0,0,0" : flashVersion;
  var bg_color = (bg_color==null)? "FFFFFF" : bg_color;
  var html = '';

  html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+pflashVersion+' " width="'+width+'" height="'+height+'" pidObject  title="'+ptitle+'">';
  html += '<param name="movie" value="../../js/'+src+'">';
  html += '<param name="quality" value="high">';
  html += '<param name="flashVars" value="'+pflashVars+'">';
  html += '<param name="wmode" value="'+pwmode+'">'; 
  html += '<param name="menu" value="'+pmenu+'">';
  html += '<PARAM NAME=bgcolor VALUE="#'+bg_color+'">';
  html += '<embed src="../../js/'+src+'" quality="high" pidEmbed menu="'+pmenu+'"  wmode="'+pwmode+'" flashvars="'+pflashVars+'" pluginspage=" http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" ALIGN="" swLiveConnect="true" NAME="charts" bgcolor="#'+bg_color+'"></embed>';
  html += '</object>';
  
  document.getElementById(destino).innerHTML += html;

 }
 */
//----------------------

function validaEmail(campo,email,ididm){
	var reEmail1 = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	var msg = [];
	if(email != ""){
	    if (!reEmail1.test(email)) {
            msg[1] = " no es una dirección válida de e-mail!";
            msg[2] = " não é um endereço válido de e-mail!";
            msg[3] = " is not a valid e-mail!";
            alert('"' + email + '"' + msg[ididm]);
		    campo.value = "";
		    campo.focus();
		    return false;
	    }else{
	        return true;
	    }
    }else{
		msg[1] = "Informe una dirección válida de e-mail!";
		msg[2] = "Informe um endereço válido de e-mail!";
		msg[3] = "Type a valid e-mail!";
		alert(msg[ididm]);
		campo.value = "";
		campo.focus();
		return false;
	}
    return false;
}

function PopupCentralizado(pagina,nome,width,height,parametros) {
	largura = screen.width;
	altura = screen.height;
	posX = (largura - width) / 2;
	posY = (altura - height) / 2;
	var janela = null;
	if (parametros=='') {
		janela = window.open(pagina, nome,'left='+posX+',top='+posY+',height='+height+',width='+width);
	} else {
		janela = window.open(pagina, nome,parametros+',left='+posX+',top='+posY+',height='+height+',width='+width);
	}
	janela.focus();
	return false;
}

function TeclaInteiro(e){
	var key;
	var keychar;
	var reg;
	
	if(window.event) {
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 
	}
	else if(e.which) {
		// netscape or firefox
		key = e.which; 
	}
	else {
		// no event, so pass through
		return true;
	}
	if((key > 47 && key < 58) || (key == 8)) // numeros de 0 a 9 OU BACKSPACE
		return true;
	else {
		return false;
	}
}

function TeclaDecimal(e){
	var key;
	var keychar;
	var reg;
	
	if(window.event) {
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 
	}
	else if(e.which) {
		// netscape or firefox
		key = e.which; 
	}
	else {
		// no event, so pass through
		return true;
	}
	if(key > 47 && key < 58) // numeros de 0 a 9
		return true;
	else {
		if (key == 8 || key == 44 || key == 46) // backspace / , / .
			return true;
		else
			return false;
	}
}
//**************************************************
function TeclaDecimal2(objeto){
	var valor = objeto.value;
	var key = "";
	var palavra = "";
	var ValidChars = "0123456789.,";
	
	for (i = 0; i < valor.length; i++){
	  key = valor.charAt(i);
	  if (ValidChars.indexOf(key) >= 0){
		   palavra += "" + key;
	  }
	}

	objeto.value = palavra;
}

function TeclaInteiro2(objeto){
	var valor = objeto.value;
	var key = "";
	var palavra = "";
	var ValidChars = "0123456789";
	
	for (i = 0; i < valor.length; i++){
	  key = valor.charAt(i);
	  if (ValidChars.indexOf(key) >= 0){
		   palavra += "" + key;
	  }
	}

	objeto.value = palavra;
}
function TeclaEnter(e){
	var key;
	var keychar;
	var reg;
	
	if(window.event){
		key = e.keyCode;
	}else if(e.which){
		key = e.which;
	}else{
		return false;
	}
	
	if((key == 13)){
		return true;
	}else{
		return false;
	}
}
//**************************************************
function countChars(num_chars, obj_mostrador, obj_texto, msg) {
	campo=obj_texto.value;
	obj_mostrador.value=campo.length;
	if (campo.length>parseInt(num_chars)) {
		alert(msg+" "+num_chars+" caracteres.");
		obj_texto.value=campo.substring(0,parseInt(num_chars));
		obj_mostrador.value=num_chars;
		return false;
	}
	return true;
}

function getElementsByClass(searchClass,node,tag) {

	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var j = 0;
	for (i = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getNowString(){
		var now = new Date();
		var ano = now.getFullYear();
		var mes = now.getMonth() + 1;
		if(mes<10) mes = "0" + mes;
		var dia = now.getDate();
		if(dia<10) dia = "0" + dia;
		var hora = now.getHours();
		if(hora<10) hora = "0" + hora;
		var minuto = now.getMinutes();
		if(minuto<10) minuto = "0" + minuto;
		var segundo = now.getSeconds();
		if(segundo<10) segundo = "0" + segundo;
		var agora = ano + "" + mes + "" + dia + "" + hora + "" + minuto + "" + segundo;
		return agora;
}

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas,bolPadZeroDec)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);
	
	//-------------------------------- bypapu -->>
	if (bolPadZeroDec && (decimalNum > 0)) {
		var iStart = tmpNumStr.indexOf(".");
		var zeros = "";
		if (iStart < 0){
			for(i=0;i<decimalNum;i++){
				zeros += "0";
			}
			tmpNumStr += "." + zeros;
		}else{
			var qtsVezes = 0;
			var auxStr = tmpNumStr.substring(iStart+1,tmpNumStr.length);
			//alert(auxStr);
			if(decimalNum > auxStr.length){
				qtsVezes = decimalNum - auxStr.length;
				for(i=0;i<qtsVezes;i++){
					zeros += "0";
				}
				tmpNumStr += zeros;
			}
		}
	}
	//-------------------------------- bypapu --<<

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

function FormatDate(fecha_in){
	if(fecha_in.length==10){
		fecha_out = fecha_in.substring(6,10) + "" + fecha_in.substring(3,5) + "" + fecha_in.substring(0,2);
	}else{
		fecha_out =  fecha_in.substring(6,8)+ "/" + fecha_in.substring(4,6) + "/" + fecha_in.substring(0,4);
	}
	return fecha_out;
}

//---------------------------------------------------------------
Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}
//---------------------------------------------------------------
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
//---------------------------------------------------------------
/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
function getMyObj(nome){
	var objeto = "";
	var IE = (document.all) ? 1 : 0;
    var DOM = 0;
    if(parseInt(navigator.appVersion) >=5) DOM=1;
    if(DOM)objeto = document.getElementById(nome);
    else if(IE) objeto = document.all[nome];
	return objeto;
}

function MyRound(number,X) {
	// rounds number to X decimal places, defaults to 2
	X = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);

}

function formataMoney(objeto){
	var numeros = "0123456789";
	var texto = objeto.value;
	var novo = "";
	var position = 0;
	for(var i=texto.length-1;i>=0;i--){
		if(numeros.indexOf(texto[i])>=0){
			if(position==2){
				novo = "," + novo;
			}else if(position > 2){
				if( ((position-2) % 3) ==0 ){
					novo = "." + novo;
				}
			}
			novo = texto[i] + novo;
			position++;
		}
	}
	objeto.value = novo;
}

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

function ValidateDate(CtrlSDate,CtrlEDate){
	
    var SDate = CtrlSDate.value;    	
    var EDate = CtrlEDate.value;
       
          
    var alertReason1 =  'A data final deve ser maior ou igual qua a data inicial!' 
    var alertReason2 =  'A data final não pode ser menor que a data inicial!';
	
    var endDate = new Date(EDate);    	
    var startDate= new Date(SDate);
     
    if(SDate != '' && EDate != '' && startDate > endDate){
	    alert(alertReason1);
	    //document.getElementById(CtrlEDate).value = "";
	    return false;
    }else if(SDate == ''){
        alert("Por favor, informe a data inicial.");
        return false;
    }else if(EDate == ''){
        alert("Por favor, informe a data final.");
        return false;
    }
	
	return true;
}

function isValidURL(url){ 
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
    if(RegExp.test(url)){ 
        return true; 
    }else{ 
        return false; 
    } 
} 