// JavaScript Document

function readForm(cObrig) {
	
	for(var i=0;i<document.forms.length;i++){
		var f = document.forms[i];
		
		if (arguments.length > 0) f.onsubmit = new Function("return checkForm(this,'"+cObrig+"');");
		
		for(var j=0;j<f.length;j++){
			var c = f.elements[j];
			if (c.type == 'text') {
				switch(c.name.substring(0,2)) {
				case 'VR':
					//alert(c.name);
					c.onkeypress = new Function("return currencyFormat(this,'.',',',event)");
				break;
				case 'NR':
					c.attachEvent("onkeypress",function x() {AllowOnlyNumbers();});
				break;
				case 'DT':
				 c.onkeydown = new Function("return dFilter(event.keyCode, this, '##/##/####')");
				break;
				}
			}
		}
	}
}

function checkForm(cForm, cObrig) {
	
	var campos    = new Array();
	var cnomes    = new Array();
	var cnt       = 0;
	var k         = 0;
	var l         = 0;
	var sw        = 0;
	var txt_alert = '';
	
	campos[0]='';
	cnomes[0]='';
	
	for (i=0; i < cObrig.length+1; i++)  {
		if (cObrig.charAt(i) == ',') {		
			sw = 0;
			cnt++;
			campos[cnt] = '';
			cnomes[cnt] = '';
		} else if (cObrig.charAt(i) == ':') {
			sw = 1;
			campos[cnt] = campos[cnt]+'';
			cnomes[cnt] = cnomes[cnt]+'';
		} else {
			if (sw == 0) { //alert('('+i+')cmp: '+campos[cnt]);
				campos[cnt] = campos[cnt] + cObrig.charAt(i);}
			else {//alert('('+i+')nme: '+cnomes[cnt]);
				cnomes[cnt] = cnomes[cnt] + cObrig.charAt(i);}
		}
	}
	
	for(var k=cForm.length-1;k>=0;k--){
		var c = cForm.elements[k];
		if (c.type == 'text') {
			for(l=0;l<=cnt;l++){
				if(campos[l]==c.name && c.value=='') {
					focus_c = c;
					txt_alert = '\n- ' + cnomes[l] + txt_alert;
				}
			}
		}
	}
	
	if (txt_alert!='') {
		alert('Os seguintes campos são de preenchimento obrigatório: \n'+txt_alert);
		focus_c.focus();
		return false;
	} else return true;
}

/**********************************************************************************************/
/**********************************************************************************************/

function loadForm(arrayC,arrayV,arrayN) {
	
	//Varre forms no documento
	for(var i=0;i<document.forms.length;i++){
		var f = document.forms[i];

		//Varre elementos no form
		for(var j=0;j<f.length;j++){
			var fc = f.elements[j];

			//Varre vetor de campos
			for(var k=1;k<=arrayN;k++) {
				if (arrayC[k] == fc.name){
					//alert(fc.name+'::'+fc.type);
					switch(fc.type) {
					case 'text':
						fc.value = arrayV[k];
						break;
					case 'select-one':
						fc.value = arrayV[k];
						break;
					case 'checkbox':
						if(fc.value == arrayV[k])
							fc.checked = true;
						else
							fc.checked = false;
						break;
					}
				}
			}
		}
	}
}

/**********************************************************************************************/
/**********************************************************************************************/
function loadXML(xml,form) {

	var f = document.getElementById(form);
	var x = document.getElementById(xml).getElementsByTagName('registro')[0];
	if(x!=null) {
	
		//Varre elementos no form
		for(var j=0;j<f.length;j++){
			var fc = f.elements[j];
			
			if (fc.type=='radio' || fc.type=='checkbox') {
				for(var i=0;i<x.childNodes.length;i++){
					if(x.childNodes[i].nodeName==fc.name){
						if (x.childNodes[i].firstChild.nodeValue == fc.value) {
							fc.checked = true;
							//alert(x.childNodes[i].nodeName +' : '+ x.childNodes[i].firstChild.nodeValue);
						}
					}
				}
			}
		}		
	}
}

/**********************************************************************************************/
/**********************************************************************************************/

function AllowOnlyNumbers()
{
   var x =event.keyCode;
   if (!(x>=48 && x <=57))
   {
       event.returnValue=false;
   }
}

/**********************************************************************************************/
/**********************************************************************************************/

function isEmpty( inputStr ) { if ( null == inputStr || "" == inputStr ) { return true; } return false; }

/**********************************************************************************************/
/**********************************************************************************************/

function lockForm() {
	
	for(var i=0;i<document.forms.length;i++){
		var f = document.forms[i];
		for(var j=0;j<f.length;j++){
			var c = f.elements[j];
			
			if(c.type != 'button' && c.type != 'submit') {
				c.disabled = 'disabled';
			}
		}
	}
}

/**********************************************************************************************/
/**********************************************************************************************/

function currencyFormat(fld, milSep, decSep, e) {
var sep = '/';
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 == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != sep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
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 getDataIslandField(dataIsland,index){
 try{return document.getElementById(dataIsland).getElementsByTagName('registro')[0].childNodes[index].firstChild.nodeValue;}
 catch(e){}
}

/**********************************************************************************************/
/**********************************************************************************************/

function divBusca() {
	var b = document.getElementById('divBusca');
	var i = document.getElementById('imgSetaBusca');
	if(b.style.display=='none')
	{
		b.style.display = 'inline';
		i.src = 'imgs/seta_busca_cima.gif';
	}
	else
	{
		b.style.display = 'none';	
		i.src = 'imgs/seta_busca_baixo.gif';
	}	
}

/**********************************************************************************************/
/**********************************************************************************************/

function contaCaracteres(field,maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else
		document.getElementById(field.id+'_COUNT').value = maxlimit - field.value.length;
}
