/*
	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();
	
}

/* função chamada pelo MaskInput */
addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));         return e = null, !!d;
    }
};

/* função chamada pelo MaskInput */
removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};
MaskInput = function(f, m){ //v1.0
	function mask(e){
		var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[À-ÿ]/i, "8": /./ },
			rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
		function accept(c, rule){
			for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
				if(r & i && patterns[i].test(c))
					break;
				return i <= r || c == rule;
		}
		var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
		(!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
			r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
			: (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
			r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
	}
	for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
		addEvent(f, i, mask);
};

function setSelectValueId(formName, formField, value2sel, targ) // 17.07.2003
{
	if(targ) targ = targ + "."
	if('' == value2sel) return;

	objSelect = eval(targ + "document.getElementById('" + formField + "')");
	if(!value2sel) return;
	_values = value2sel.split("|");

	for (var i=0; i < objSelect.options.length; i++)
	{
		objSelect.options[i].selected = false; // Limpa campos
		// Setar os valores
		for(j=0; j < _values.length; j++)
		{
			if (objSelect[i].value == _values[j]) objSelect.options[i].selected = true;
		}
	}
}