/*
	Limita o numero máximo de caracteres a ser digitado em um Campo TEXTAREA
	show_text_len = eh para mostrar o valor num campo text	Para Usar: onKeyUp="textAreaLimit('form1','mensagem','125')"
*/
function textAreaLimit(form_name,campo,maxlimit,show_text_len) { // v0.1	if (eval("document." +form_name+ "." +campo+ ".value.length") >= maxlimit) {
		alert ("O Limite de Caracteres Foi Excedido: Permitido=" + maxlimit);
	}	if (eval("document." +form_name+ "." +campo+ ".value.length") > maxlimit)		eval("document." +form_name+ "." +campo+ ".value = document." +form_name+"." +campo+ ".value.substring(0, " +maxlimit+ ");");	else		status = eval("document." +form_name+ "." +campo+ ".value.length;") + " de " + maxlimit;
	// Insira um campo chamado <input type="text" name="len">	if (show_text_len) {		eval("document."+ form_name + ".len.value = document." + form_name + "." +campo+ ".value.length;");	}}


/*
Check all checkbox
	formName	= Form Name
	boxName		= Name of input type=checkbox
Usage:
	Put :
	<input type="checkbox" name="sellall" onClick="CheckAll('formName')">

formName = name of form

*/
function CheckAll(formName,boxName) { //v0.2
	if (!boxName) boxName = "selall";
	if (!formName) formName = "form";
	
	for (var i=0 ; i < eval("document." + formName + ".elements.length") ; i++) 
	{
		var x = eval("document." + formName + ".elements["+ i + "]");
		status=x.name;
		if (x.name != boxName)
		      x.checked = eval("document."+ formName + "." + boxName + ".checked");
	}
}

/*
Check all especific checkbox
	formName	= Form Name
	inputName	= Checkbox will be checked
	boxName		= Name of input type=checkbox
Usage:
	Put :
	<input type="checkbox" name="sellall" onClick="CheckAll('formName')">

formName = name of form

*/
function checkBoxEsp(formName,inputName, boxName) { //v0.2
	//dumpObj(obj,inputName);
	//boxName = formName.name;

	for (var i=0 ; i < eval("document." + formName + ".elements.length") ; i++) 
	{
		var x = eval("document." + formName + ".elements["+ i + "]");
		status=x.name;

		if (x.name != boxName && x.name == inputName)
		      x.checked = eval("document."+ formName + "." + boxName + ".checked");
	}
}



/*

PS: form name should be "form"
	
Date Format in Brazilian 
Usage into tag INPUT TEXT:
onKeyDown=DateFormatBr("fieldName_name",event)
	
*/
function DateFormatBr(fieldName,keyPressed) { // v0.1
	var KeyPress = keyPressed.keyCode;
	vr = document.form[fieldName].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	size = vr.length + 1;

	if ( KeyPress != 9 && KeyPress != 8 ){
		if ( size > 2 && size < 5 )
			document.form[fieldName].value = vr.substr( 0, size - 2  ) + '/' + vr.substr( size - 2, size );
		if ( size >= 5 && size <= 10 )
			document.form[fieldName].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); }
}


/*
PS: form name should be "form"

Description:	Value Format type Money in Brazilian

Usage on tag INPUT TEXT AND TEXTAREA
onKeyDown=ValueFormatBr("fieldName",<MaxSize>,event)
 - MaxSize can be a value of 0 until 17

*/

function ValueFormatBr(fieldName,maxSize,keyPressed) { //v0.1
	var tecla = keyPressed.keyCode;
	vr = document.form[fieldName].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	size = vr.length;
	if (size < maxSize && tecla != 8){ size = vr.length + 1 ; }

	if (tecla == 8 ){	size = size - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		count = 0;
		if ( size <= 2 ){ 
			count=0;
	 		document.form[fieldName].value = vr ; 
			}
	 	if ( (size > 2) && (size <= 5) ){
			count=1;
	 		document.form[fieldName].value = vr.substr( 0, size - 2 ) + ',' + vr.substr( size - 2, size ) ; 
			}
	 	if ( (size >= 6) && (size <= 8) ){
			count=2;
	 		document.form[fieldName].value = vr.substr( 0, size - 5 ) + '.' + vr.substr( size - 5, 3 ) + ',' + vr.substr( size - 2, size ) ; 
			}
	 	if ( (size >= 9) && (size <= 11) ){
			count=3;
	 		document.form[fieldName].value = vr.substr( 0, size - 8 ) + '.' + vr.substr( size - 8, 3 ) + '.' + vr.substr( size - 5, 3 ) + ',' + vr.substr( size - 2, size ) ; 
			}
	 	if ( (size >= 12) && (size <= 14) ){
			count=4;
	 		document.form[fieldName].value = vr.substr( 0, size - 11 ) + '.' + vr.substr( size - 11, 3 ) + '.' + vr.substr( size - 8, 3 ) + '.' + vr.substr( size - 5, 3 ) + ',' + vr.substr( size - 2, size ) ; 
			}
	 	if ( (size >= 15) && (size <= 17) ){
			count=5;
	 		document.form[fieldName].value = vr.substr( 0, size - 14 ) + '.' + vr.substr( size - 14, 3 ) + '.' + vr.substr( size - 11, 3 ) + '.' + vr.substr( size - 8, 3 ) + '.' + vr.substr( size - 5, 3 ) + ',' + vr.substr( size - 2, size ) ;
			}
	}
	status = size;
	if (size >= maxSize) 
	{
		document.form[fieldName].value = document.form[fieldName].value.substring(0, maxSize+count - 1);
	}
}



/*
	jumpMenu()
	
	Usage:<select>
		<option value="URL">Valor
	</select>
*/

function jumpMenu(targ,selObj,restore){ // v0.1
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


/*
	Abre janelas popup.
	
*/
function openWindow(theURL,winName,features) { //v2.0
  if(!features)
  {
      features="width=350,height=350,status=yes,scrollbars=1";
  }
  window.open(theURL,winName,features);
}


/*
	dumpObj objects and contents
	Use: dumpObj(this,NomeDoCampo)
*/
function dumpObj (obj, str) { // v0.1
var s = new Array ();
var i;
var j = 0;
  for (i in obj) {   // get all objects, and contents
    s[j] = i + ": " + obj[i];
    j = j + 1;
  }
  s.sort ();         // sort them data down
  document.open ("text/html");  // open a new document
  document.write ("<html><head>");  //  with full XHTML style
  document.write ("<title>Dump page</title>");
  document.write ("</head><body>");
  document.write ("<p><b>");  // make it kosher
  document.write (str, " contents.<br />");
  document.write (navigator.appName, "<br />");
  document.write (navigator.userAgent, "</b><br /><br />");
  for (i=0; i<j; i++) {
    document.write (s[i], "<br />");
  }
  document.write ("</p>");
  document.write ("</body></html>");
  document.close ();  // close down output stream
}



/*
Mostra mensagem, quando for escolhido mais de (n QUANTIDADE} na caixa de selecao (multiple)

	Obj 	- Use (this)
	nome	- Nome do Campo
	quantidade - Numero máximo de selecoes pode ter
Use: onBlur="escolha_n(this, NomeDoCampo, 3)"
*/

function escolha_n(Obj, nome, quantidade) { // v0.1

	//isArray(Obj);

	//ListObj(Obj.elements[3]);

	total = Obj.length;
	if (!quantidade) quantidade=3;
	count=0;
	for (var i=0; i < total ;i++) 
	{
		var x = Obj[i] //.options[i];
		if (x.type == "select-multiple" && x.name == nome)
		{
			for(j=0; j < x.length; j++)
			{
				if (x[j].selected == true) count = count + 1
			}
		}
	}
	if (count > quantidade) 
	{
		alert("Você só pode selecionar no máximo " + quantidade)
		
		// Dermarcar todos
		for (var i=0; i < total ;i++) 
		{
			var x = Obj[i] //.options[i];
			if (x.type == "select-multiple" && x.name == nome)
			{
				for(j=0; j < x.length; j++)
				{
					x[j].selected = false;
				}
			}
		}
	}
	//Obj.nome.focus();
	
}
