/*
	Por...: Lucas Henrique Vicente
	Email.: lucashv@gmail.com
*/
var tempo = null;

function setaValor(ctrID, valor) {
	var ctr   = document.getElementById(ctrID);
	ctr.value = valor;
}

				
	
		function MM_formtCep(e,src,mask) {
if(window.event) { _TXT = e.keyCode; } 
else if(e.which) { _TXT = e.which; }
if(_TXT > 47 && _TXT < 58) { 
 var i = src.value.length; var saida = mask.substring(0,1); var texto = mask.substring(i)
 if (texto.substring(0,1) != saida) { src.value += texto.substring(0,1); } 
    return true; } else { if (_TXT != 8) { return false; } 
 else { return true; }
}
}
	

function transformaSegundos(segundos) {
	var seg, min, hora;
	hora = new String(Math.floor(segundos/3600));
	min  = new String(Math.floor(Math.floor(segundos%3600)/60));
	seg	 = new String(Math.floor(Math.floor(segundos%3600)%60));
	hora = (hora.length == 1 ? "0"+hora : hora);
	min	 = (min.length  == 1 ? "0"+min  : min);
	seg  = (seg.length  == 1 ? "0"+seg  : seg);
	return hora + ":" + min + ":" + seg;
}

function diminuiSegundos(ctrID, url) {	
	if (tempo > 0) {		
		setaValor(ctrID, transformaSegundos(tempo--));	
		setTimeout("diminuiSegundos('"+ctrID+"', '"+url+"')", 1000);
	} else {
		alert("Seu tempo acabou. O teste vai ser finalizado.");
		window.location = url;
	}
}

function ret(e) {
	var appName = window.navigator.appName;
	var keyCode;
	if (appName == "Netscape") {
		keyCode = e.which;
		if ((!(keyCode >= 48 && keyCode <= 57)) && (keyCode != 8 && keyCode != 127))
			return false;
	} else {
		keyCode = event.keyCode;
		if ((!(keyCode >= 48 && keyCode <= 57)) && (keyCode != 8 && keyCode != 127))
			return false;
	}
}

function verificaNumero(evt) {
	if (!campoNumerico(evt)) {
		return false;
	}
	return true;
}

function campoNumerico(evt) {
	var CodTecla;

	if (document.all){ CodTecla = evt.keyCode; } 
	else { CodTecla = evt.which; }
			
	//var str = obj.value

	if((CodTecla < 48 || CodTecla > 58) && 
	   (CodTecla != 46)					&&
	   (CodTecla != 8)					&&
	   (CodTecla != 0)) { 
		return false
	}

	return true
}

function valorMonetario(id, e) {
	var txtBox   = document.getElementById(id);	
	if (!campoNumerico(e)) {
		return false;
	} else {
		if (txtBox.value.length < txtBox.maxLength) {
			valorDecimal(id, e);
			
			var aux		 = txtBox.value.replace(".", "");
			for (var j=0; j<20; j++) aux = aux.replace(".", "");
			
			var parteInt = aux.split(",");		
			
			if (parteInt.length == 2) {
				var i, start;
				var strMilhar = new String(parteInt[0]);
				var milhar    = new Array();
				var nPontos   = Math.floor(parteInt[0].length / 3);	
				
				for (i=0; i<nPontos; i++) {
					start = strMilhar.length - ((i + 1) * 3);
					milhar[i] = strMilhar.substr(start, 3);					
				}
				
				if (start > 0) milhar[milhar.length] = strMilhar.substr(0, start);				
				
				if (milhar.length > 0) {
					txtBox.value = "";
					for (i=(milhar.length-1); i>=0; i--) {
						txtBox.value += milhar[i] + (i == 0 ? "" : ".");
					}
					
					txtBox.value += "," + parteInt[1];
				}
			}
			return true;
		}
	}
}

function valorDecimal(id, e) {
	var txtBox   = document.getElementById(id);
	var offSet;
	var keyCode = null;
	
	if (txtBox.value.length < txtBox.maxLength) {
		if (document.all) keyCode = e.keyCode;
		else keyCode = e.which;	
		
		if ((keyCode < 48 || keyCode > 58) && keyCode != 8) {
			return false;
		} else {
			if (txtBox.value.length >= 2) {
				if (txtBox.value.length == 4 && 
					txtBox.value.substr((txtBox.value.length - 3), 1) == "," &&
					e.keyCode == 8) 
					txtBox.value = txtBox.value.replace(",", "");
				else if (txtBox.value.length >= 2 && e.keyCode != 8)
					putComma(txtBox, 1);
				else if (txtBox.value.length >= 2 && e.keyCode == 8)
					putComma(txtBox, 3);
			}
		}
	}	
}

function putComma(ctl, precision) {
	ctl.value = ctl.value.replace(",", "");
	var begin = (ctl.value.length - precision);
	var end   = begin;
	
	if (begin >= 0) {
		var parteDec = ctl.value.substr(begin, precision);
		var parteInt = ctl.value.substr(0, end);
		ctl.value	 = parteInt + "," + parteDec;
	}
}

function mascData(fieldid, nextfieldid, evt) {
	if (campoNumerico(fieldid, evt)) {
		var field = document.getElementById(fieldid);
		if (nextfieldid != "")
			var next  = document.getElementById(nextfieldid);
		
		var reg1 = /^[0-9]{2}$/;
		var reg2 = /^[0-9]{2}\/[0-9]{2}$/;
		var reg3 = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
		
		if (reg1.test(field.value) || reg2.test(field.value)) {
			field.value += "/";
		} else if (reg3.test(field.value)) {
			if (nextfieldid != "") {
				next.focus();
			}
		}
		return true;
	} else {
		return false;
	}
}

function popUp(pPagina, pNome, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no'
	win = window.open(pPagina, pNome, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


function NewWindow(mypage, myname, w, h) {
			var win = null;
				LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
				TopPosition = (screen.height) ? (screen.height-h)/5 : 0;
				settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,resizable=yes'
				
				win = window.open(mypage,myname,settings)
				if (win.window.focus){win.window.focus();}
}		
			

