// Technique TI Ltda
// rah@tech-it.com.br
// version 1.0 - 01/12/1999
// version 1.1 - 20/04/2001

// return the value of obj[selectedIndex] where obj is a option button
function getSelectedOption(obj) {
  for(var i=0;i<obj.length; i++) {
	if (obj[i].checked==true) return obj[i].value;
  }
}
function unmarkAllOption(obj) {
  for(var i=0;i<obj.length; i++) {
	obj[i].checked=false;
  }
}

// se objOption[i].checked where obj[i].value match objField.value
function setSelectedOption(objField,obj) {
  for(var i=0;i<obj.length; i++) {
	if (obj[i].value==objField.value) {
		obj[i].checked=true;
		break;
	} 
  }
}

function printThisPage() {
	var Browser;
	if (navigator.appName.toUpperCase().indexOf('EXPLORER') >= 0 ) {
		var Inicio = navigator.appVersion.indexOf(';') +6;
		Browser = navigator.appVersion.substring(Inicio, navigator.appVersion.indexOf(';', Inicio) -1);		  
		if (Browser >= 5) window.print(); else alert('Para imprimir pressione Ctrl+P');
	} else if (navigator.appName.toUpperCase().indexOf('NETSCAPE') >= 0) {
		Browser = navigator.appVersion.substring(0,3);
		if (Browser >= 4.7) window.print(); else alert('Para imprimir pressione Ctrl+P');			
	} else alert('Para imprimir pressione Ctrl+P');
}

// check functions
function checkDouble(key) {
  if(event.keyCode==44) event.keyCode=46;
  if(event.keyCode!=46 && (event.keyCode<48 || event.keyCode>57)) event.keyCode=null;
}
function checkText(key) {
}
function checkLong(key) {	
	if(event.keyCode<48 || event.keyCode>57) event.keyCode=null;
}
function checkDouble(key) {	
	if(event.keyCode<48 || event.keyCode>57) {
	  if (event.keyCode==44) event.keyCode=46;
	  if (event.keyCode!=46) {
	  	event.keyCode=null;
	  }
	}
}
function checkDate(key) {	
	if(event.keyCode!=45 && (event.keyCode<47 || event.keyCode>57)) event.keyCode=null;
}
function checkTime(key) {	
	if(event.keyCode!=58 && (event.keyCode<47 || event.keyCode>57)) event.keyCode=null;
}

function isEmpty(obj) {
	if (obj.value.trim()=="") return true;
	return false;
}

function selectAllItem(listSource) {
	l1 = listSource.length;
	for ( i=0; i<l1; i++){
		listSource.options[i].selected=true;
	}
}
function moveListItem(listSource, listDest) {
	l1 = listSource.length;
	for ( i=0; i<l1; i++){
	  if (listSource.options[i].selected == true ) {
		  l2 = listDest.length;
		  achou=false;
		  for ( j=0; j<l2; j++) {
		  	if (listSource.options[i].value == listDest.options[j].value) {
		  	  achou=true;
		  	  break;
		  	}
		  }
		  if (!achou) {
		  	listDest.options[l2]= new Option(listSource.options[i].text);
		  	listDest.options[l2].value = listSource.options[i].value;
		  }
	  }
	}
	for ( i=l1 -1; i>=0; i--){
	 if (listSource.options[i].selected == true ) {
		listSource.options[i] = null;
	 }
	}
}
function moveAllListItem(listSource, listDest) {
	l1 = listSource.length;
	for ( i=0; i<l1; i++){
	  l2 = listDest.length;
	  listDest.options[l2]= new Option(listSource.options[i].text);
	  listDest.options[l2].value = listSource.options[i].value;
	}
	for ( i=l1 -1; i>=0; i--){
	listSource.options[i] = null;
	}
}
function readList(listSource, hiddenField) {
	l1 = listSource.length;
	r = "";
	for (i=0; i<l1; i++){
	 r += listSource.options[i].value;
	 if (i<(l1-1)) r +=";";
	}
	hiddenField.value = r;
}
function readListText(listSource, hiddenField) {
	l1 = listSource.length;
	r = "";
	for (i=0; i<l1; i++){
	 r += listSource.options[i].text;
	 if (i<(l1-1)) r +=";";
	}
	hiddenField.value = r;
}
function readListFull(listSource, hiddenField) {
	l1 = listSource.length;
	r = "";
	for (i=0; i<l1; i++){
	 r += listSource.options[i].value + ";" + listSource.options[i].text;
	 if (i<(l1-1)) r +=";";
	}
	hiddenField.value = r;
}

function open_help(page) {
	var janela = window.open(page, 'help', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=no,width=500,height=480');
	janela.focus();
}

function formata(campo,estilo,sonum,e) {
	if(estilo == 'cnpj')
		estilo = '##.###.###/####-##';
		
	if(estilo == 'cep')
		estilo = '#####-###';
		
	if(estilo == 'tel')
		estilo = '####-####';
		
	if(estilo == 'rg')
		estilo = '##.###.###-#';
		
	if(estilo == 'cpf')
		estilo = '###.###.###-##';
		
	if(estilo == 'data')
		estilo = '##/##/####';
		
	if(estilo == 'hora')
		estilo = '##:##:##';
		
	if(estilo == 'ie' && document.cadastro.estado.value == ""){
		document.cadastro.estado.focus();
		estilo = '#';
	}
	
	if(estilo == 'ie' && document.cadastro.estado.value == 'AC')
		estilo = '##.###.###/###-##';

	if(estilo == 'ie' && ( document.cadastro.estado.value == 'AL' || document.cadastro.estado.value == 'AP'
						|| document.cadastro.estado.value == 'CE' || document.cadastro.estado.value == 'ES'
						|| document.cadastro.estado.value == 'MA' || document.cadastro.estado.value == 'MS'
						|| document.cadastro.estado.value == 'PA' || document.cadastro.estado.value == 'PB'
						|| document.cadastro.estado.value == 'PI'))
		estilo = '##.######-#';
			
	if(estilo == 'ie' && document.cadastro.estado.value == 'AM')
		estilo = '##.###.###-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'BA')
		estilo = '###.###-##';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'DF')
		estilo = '##.######.###-##';

	if(estilo == 'ie' && (document.cadastro.estado.value == 'GO' || document.cadastro.estado.value == 'RN'))
		estilo = '##.###.###-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'MT')
		estilo = '####.######-#';
	
	if(estilo == 'ie' && document.cadastro.estado.value == 'MG')
		estilo = '###.###.###/####';
		
	if(estilo == 'ie' && (document.cadastro.estado.value == 'SP' || document.cadastro.estado.value == '0'))
		estilo = '###.###.###.###';
	
	if(estilo == 'ie' && document.cadastro.estado.value == 'PR')
		estilo = '###.#####-##';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'PE')
		estilo = '##.#.###.#######-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'RJ')
		estilo = '##.###.##-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'RS')
		estilo = '###/######-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'RO')
		estilo = '#############-#';
	
	if(estilo == 'ie' && (document.cadastro.estado.value == 'RR' || document.cadastro.estado.value == 'SE'))
		estilo = '########-#';
		
	if(estilo == 'ie' && document.cadastro.estado.value == 'SC')
		estilo = '###.###.###';

	if(estilo == 'ie' && document.cadastro.estado.value == 'TO')
		estilo = '##########-#';			


	var keycode;
	if(window.event)
		keycode = e.keyCode;
	else if(e.which)
		keycode = e.which;
	
	if(keycode == 8 || keycode == 46 || keycode == null){
		return true;
	}else{
		cnum = false;
		
		for(i=48;i<=57;i++){
			if(keycode == i)
				cnum = true;
		}
		
		for(i=96;i<=105;i++){
			if(keycode == i)
				cnum = false;
		}
	
		if(sonum && !cnum)
			return false;
	
		retorno = "";
	
		for(i=0;i<=campo.value.length;i++){
			if(i < estilo.length){
				if(estilo.charAt(i) == '#')
					retorno += campo.value.charAt(i);
				else
					retorno += estilo.charAt(i);
			}
			else{
				return false;
			}
		}
	
		campo.value = retorno;
	}
}

function noNumber(e){
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event){ // IE
		keynum = e.keyCode;
	}else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return !numcheck.test(keychar);
}

function noChar(e){
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event){ // IE
		keynum = e.keyCode;
	}else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}
	
	if(keynum == 8 || keynum == 46 || keynum == null){
		return true;
	}else{
		keychar = String.fromCharCode(keynum);
		numcheck = /\D/;
		return !numcheck.test(keychar);
	}
}

function popupException(){
	desabilitarTela("aboveGrayBox");
	setTimeout(closePopup, 60000);
}

function closePopup(){
	habilitarTela("aboveGrayBox");
}

function desabilitarTela(div) {
	try {
		var h=document.body.clientHeight;
		if (document.body.scrollHeight > document.body.clientHeight) {
			h=document.body.scrollHeight;
		}

	    document.getElementById("graybox").style.height = h+"px";
		document.getElementById("graybox").style.visibility="visible";
		document.getElementById(div).style.visibility="visible";
	} catch (e) {
	}
}

function habilitarTela(div) {
	try {
		document.getElementById("graybox").style.visibility="hidden";
		document.getElementById(div).style.visibility="hidden";
	} catch (e) {
	}
}

function showDiv(div) {
	document.getElementById(div).style.display="block";
	document.getElementById(div).style.height="auto";
}
function hideDiv(div) {
	document.getElementById(div).style.display="none";
	document.getElementById(div).style.height="1px";
}
function hideShowDiv(div) {
	if( document.getElementById(div).style.display == "none" ) {
		document.getElementById(div).style.display = "block";
	} else {
		document.getElementById(div).style.display = "none";
	}
}
