function replaceSubstring(inputString, fromString, toString) 
{
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function LTrim( value ) 
{
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

function RTrim( value ) 
{
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function trim( value ) 
{
	
	return LTrim(RTrim(value));
	
}



function F_Print(obj)
{   
   window.print();
}

function F_OpenWindow(url,nome,atributos) 
{
	window.open(url,nome,atributos);
}

// Recebe uma string e converte para valor ASC
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function PopUp(URL,w,h)
{
  window.open(URL,'pop','scrollbars=auto,width=' + w + ',height=' + h)
}


// Verifica se uma string é numerica, SE não for volta o numero ANTERIOR a ela ter sido digitada.
function checkNumber(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

// Retira as string " e ' na digitação
function noQuotes(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  if (cCode == 34 || cCode == 39 )
  {
  	val.value = val.value.substring(0,strLength-1);
	  return false;
  }
}

function disableAllElements() {
	for (x=0; x < document.forms.length; x++) {
		for (y=0; y < document.forms[x].length; y++) {
			if ( document.forms[x].elements[y].controle == "true") {
				document.forms[x].elements[y].disabled = "disabled";
			}
		}
	}
}

function showDiv(div) {
	if ( document.getElementById(div).style.display == "none" ) {
		document.getElementById(div).style.display = "";
	} else {
		document.getElementById(div).style.display = "none";
	}
}

function URLEncode(plaintext) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(encoded) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while

   return plaintext;
};

function F_Foto(arg1,arg2) {
	var vImg   = eval("document.form.foto"      + arg1); //campo file da foto
	var vNome  = eval("document.form.foto_nome" + arg1); //campo com o nome da foto na alteração
	var vObj   = eval("document.form.ds_img_"   + arg1); //imagem
	vDsArqImg  = arg2;

		if(vImg.value == "" && ((vNome && vNome.value == "") && vDsArqImg == "")) {
			vObj.src = "/educa/imagens/ico_imagem.gif";
		} else {
			if(vImg.value != "") {
				img = "file:///" + vImg.value;	
				vObj.src = img;
			} else {
				vObj.src = vDsArqImg;
			}
		}
}

function F_LimparFoto(arg1,arg2) {
	var vObjDel = eval("document.form.exclu_foto_" + arg1);
	var vObj    = eval("document.form.ds_img_" + arg1);

	if(vObjDel.value == 'D') {
		vObj.src = "/educa/imagens/ico_imagem.gif";	
	}
	else {
		F_Foto(arg1,arg2,'D');
	}
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
  if (n <= 0)
     return "";
  else if (n > String(str).length)
     return str;
  else {
     var iLen = String(str).length;
     return String(str).substring(iLen, iLen - n);
  }
}

function getCheckedValue(objeto)
{
	var c_value = "";
	if (objeto.length)
	{
		for (var i=0; i < objeto.length; i++)
		{
			if (objeto[i].checked)
			{
				c_value += objeto[i].value;
				if (i < objeto.length)
				{
					c_value += ",";
				}
			}
		}
	}
	else
	{
		c_value = objeto.value;
	}
	return c_value;
}

function getCheckedValue(objeto, delimiter)
{
	var c_value = "";
	if (objeto.length)
	{
		for (var i=0; i < objeto.length; i++)
		{
			if (objeto[i].checked)
			{
				c_value += objeto[i].value;
				if (i < objeto.length)
				{
					c_value += delimiter;
				}
			}
		}
	}
	else
	{
		c_value = objeto.value;
	}
	return c_value;
}


function Asc(String)
{

	return String.charCodeAt(0);

}

function Chr(AsciiNum)
{

	return String.fromCharCode(AsciiNum)

}

function btnLimparPesquisaOnClick(form)
{
  if (form == null)
  {
    form = document.getElementById('filtro');
  }
  
  for (var i = 0; i < form.elements.length; i++)
  {
    form.elements[i].value 		   = '';
    form.elements[i].checked 	   = false;
    form.elements[i].selectedIndex = 0;
  }
  
  form.submit();
} //-- btnLimparPesquisaOnClick ------------------------------------------------
