// JavaScript Document

function showHideMe(who) {
	var o = who;
	var c = null;
	if (typeof(o) != 'object') {
		o = document.getElementById(o);
	}
	c = o.getElementsByTagName('DIV');
	c = c.namedItem('subAB');
	c.className = (c.className == 'hidden')?'visible':'hidden';
	if(c.className == 'visible') document.FF_opened = o;
}

function debug (o) {
	w = window.open('','debug','scrollbars=1,resizable=1');
	d = w.document;
	d.open();
	for (a in o) {
		d.write(a + ' => ' + o[a] + '<br />\n');
		/*d.write(a + ' => ' + o.getAttributeNode(a) + '<br />\n');*/
	}
	d.close();
}

document.FF_opened = 'pessoal';

function walkOn(tgt) {
	var t = document.getElementById(tgt);
	showHideMe(document.FF_opened);
	showHideMe(t);
}

function controla (who, state) {
	var o = who;
	var c = state;
	if (typeof(o) != 'object') {
		o = document.getElementById(o);
	}
	o.disabled = !state;
	if (o.disabled) {
		o.value = '';
	}
}


function validaForm() {
	var i;
	var p;
	var q;
	var nm;
	var test;
	var num;
	var min;
	var max;
	var errors = new Array();
	var args=validaForm.arguments;
	
	for (i=0; i<(args.length-2); i+=3) {
		test = args[i+2];
		if (typeof(args[i]) != 'object') {
			val = document.getElementById(args[i]);
		}
		else {
			val = args[i];
		}
		if ((test.charAt(0) == 'R') && (val.value == '')) {
			label = getInputLabel(val.name);
			errors.push(label.firstChild.nodeValue + ' é requerido. ');
			markField(val);
		}
		else if (val.value != '') {
			switch (test) {
				case 'isList':
				case 'RisList':
					p = val.selectedIndex;			
					v = (p>=0)?(val.options[p].value == 0):true;
					if ((p<1) && (v)) {
						label = getInputLabel(val.name);
						errors.push(label.firstChild.nodeValue + ' deve ter ao menos uma opção selecionada. ');			
						markField(val);
					}
					break;
				case 'isEmail':
				case 'RisEmail':
					if (!isEmail(val.value)) {
						label = getInputLabel(val.name);
						errors.push(label.firstChild.nodeValue + ' deve conter um endereço de e-mail. ');
						markField(val);
					}
					break;
				case 'isDate':
				case 'RisDate':
					val[0] = document.getElementById(val[0]);
					val[1] = document.getElementById(val[1]);
					val[2] = document.getElementById(val[2]);
					if (!checkDateField(val[0],val[1],val[2])) {
						label = getInputLabel(val[0].name);
						errors.push(label.firstChild.nodeValue + ' deve conter uma data válida. ');
						markField(val[0]);
						markField(val[1]);
						markField(val[2]);
					}
					break;
				case 'isNumber':
				case 'RisNumber':
					if (isNaN(val.value)) {
						label = getInputLabel(val.name);
						errors.push(label.firstChild.nodeValue + ' deve conter um número. ');
						markField(val);
					}
					break;
				default:// controla os que necessitam de parÃ¢metros
					if (test.indexOf('inRange') != -1) { 
						if (isNaN(val.value)) {
							label = getInputLabel(val.name);
							errors.push(label.firstChild.nodeValue + ' deve conter um número. ');
							markField(val);
						}
						else {
							num = parseFloat(val.value);
							p = test.indexOf(':');
							min = test.substring(8,p);
							max = test.substring(p+1);
							if (num < min || max<num) {
								label = getInputLabel(val.name);
								str = ' deve conter um número entre #MIN# e #MAX#. ';
								str = str.replace('#MIN#', min);
								str = str.replace('#MAX#', max);
								errors.push(label.firstChild.nodeValue + str);
								markField(val);
							}
						}
					}
					else if (test.indexOf('isEqual') != -1) {
						p = test.indexOf(':');
						comp = document.getElementById(test.substring(p+1));
						if (val.value != comp.value) {
							label = getInputLabel(val.name);
							label_comp = getInputLabel(comp.name);
							str = ' #A# e #B# devem ser iguais. ';
							str = str.replace('#A#', label.firstChild.nodeValue);
							str = str.replace('#B#', label_comp.firstChild.nodeValue);
							errors.push(str);
							markField(val);
							markField(comp);
						}
					}
					break;
			}
			if (val.value) {
				if ((val.value.length >= 1) && (test.indexOf('isEqual') == -1)) {
					att = val.getAttributeNode('minlength');
					if(att != null) {
						if (val.value.length < att.nodeValue) {
							label = getInputLabel(val.name);
							str = ' deve ter no mínimo #MIN# caracteres. ';
							str = str.replace('#MIN#', att.nodeValue);
							errors.push(label.firstChild.nodeValue + str);
						}
					}
				}
			}
		}
	}	
	showError (errors);
	return (errors.length == 0);
}

function checkDateField (dField, mField, yField) {
	if (mField != undefined) {
		dField = (typeof(dField) != "object")?document.getElementById(dField).value:dField.value;
		mField = (typeof(mField) != "object")?document.getElementById(mField):mField;
		mField = mField.options[mField.selectedIndex].value - 1;// mes no js inicia em 0
		yField = (typeof(yField) != "object")?document.getElementById(yField).value:yField.value;
	}
	else {
		dField = dField.value;
		dField = dField.split('/');
		mField = dField[1] - 1;// mes no js inicia em 0
		yField = dField[2];
		dField = dField[0];
	}
	
	date = new Date(yField, mField, dField);
	return (date.getDate() == dField);
}

function showError (mensagem) {
	var err = document.getElementById('error');
	switch (typeof(mensagem)) {
		case 'object':
			var list = document.createElement('UL');
			var item = null;
			for (x=0; x<mensagem.length; x++) {
				item = document.createElement('LI');
				item.appendChild(document.createTextNode(mensagem[x]));
				list.appendChild(item);
			}

			if (err.firstChild != undefined) {
				err.replaceChild(list, err.firstChild);//nodeValue = mensagem;
			}
			else {
				err.appendChild(list);
			}
			break;
		default:
			if (err.firstChild != undefined) {
				err.firstChild.nodeValue = mensagem;
			}
			else {
			    msg = document.createTextNode(mensagem);
				err.appendChild(msg);
			}
	}
	err.style.display = (mensagem)?'block':'';
}
function getInputLabel (field) {
	if (!document.lbs) {
		document.lbs = document.getElementsByTagName("LABEL");
	}
	lbs = document.lbs;
	var l = null;
	for (x=0; x<lbs.length; x++) {
		if(lbs[x].htmlFor == field)
			l = lbs[x];
	}
	return (l);
}

function markField(val) {
	//destaca o campo para facilitar localizaÃ§Ã£o
	var o = val;
	o.style.borderColor = '#FF0000';
//	debug(o.style);
}
function isEmail (str) {
	var filter= new RegExp("^.+@.+\..{2,3}$", "i");
	return (filter.test(str));
}	
function wopen () {
	a = wopen.arguments;
	window.open(a[0],a[1],a[2]);
}