var nav = navigator.appName.substring(0,3).toUpperCase();

function getCaretPos(obj){
	var iCaretPos = 0;
	if (nav=="MIC"){
		var oSel = document.selection.createRange ();
		oSel.moveStart ('character', -obj.value.length);
		iCaretPos = oSel.text.length;
	}
	else{
		iCaretPos = obj.selectionEnd;
	}
	return iCaretPos;
}
	
function setCaretPos(obj, iCaretPos){
	if (nav=="MIC"){				
		var r = obj.createTextRange();
		r.move('character', iCaretPos);
		r.select();
	}
	else{
		obj.selectionStart = iCaretPos;
		obj.selectionEnd = iCaretPos;
		obj.focus();
	}
}

function Mask(obj){
	this.obj = obj;
	this.cod = {
		'9': /[0-9]/,
		'a': /[A-Za-z]/,
		'*': /[A-Za-z0-9]/
	}
	
	this.mask = function(txt, mask){		
		var l = mask.length;
		var k = txt.length;
		var slot = "9a*";	
		var r = "";
		var j = 0;	
		for (var i=0; i<l; i++){
			var c = mask.charAt(i);
			if (slot.indexOf(c)!=-1){			
				if (j<k){
					var u = txt.charAt(j);
					if (!this.cod[c].test(u)){					
						return "";
					}			
					r += u;				
					j++;								
				}
				else{
					r += "_";
				}
			}
			else{
				r += c;
			}		
		}
		return r;
	}
	
	this.unMask = function(txt){				
		var l = txt.length;		
		var e = /[a-zA-Z0-9]/;
		var r = "";	
		for (var i=0; i<l; i++){
			var c = txt.charAt(i);		
			if (e.test(c)){
				r += c;
			}
		}	
		return r;
	}
	
	this.onKeyPress = function(e){
		var key = (nav=="MIC"?event.keyCode:e.which);
		if ((key==8)||(key==0)){
			return true;
		}
		
		var msk = this.obj.getAttribute("mask");
		var	chr = String.fromCharCode(key);			
		var pos = getCaretPos(this.obj);		
		
		var txt = obj.value;
		if (txt==""){
			txt = chr;
		}
		else{		
			txt = txt.substring(0, pos)+chr+txt.substring(pos, txt.length);
		}		
		txt = this.mask(this.unMask(txt), msk);
		
		if (txt==""){ return false; }
	
		var l = txt.length;
		
		obj.value = txt;
	
		var slot = "9a*";	
	
		var dx = ((slot.indexOf(msk.charAt(pos))==-1)?1:0);	
	
		pos++;
		for (var i=pos; i<l; i++){
			if (slot.indexOf(msk.charAt(i))!=-1){
				p = i;
				break;
			}
		}	
		if (pos<l){	
			setCaretPos(this.obj, pos+dx);		
		}		
		return false;
	}
	
	this.setValue = function(){
	}
	
	this.init = function(){
		var self = this;		
		this.obj.onkeypress = function(e){
			return self.onKeyPress(e);
		}
	}	
}

function TcXhr(){
	try{
   		var x = new XMLHttpRequest();
	}
	catch(ee){
		try{
			var x = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				var x = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E){
				var x = false;
			}
		}
	}
	return x;
}

function TcLoad(obj, url){
	obj.length = 0;	
	obj.options[0]=new Option(" -- Aguarde ... -- ","0");	
	var x = TcXhr();	
	x.open("GET", url, true);
	x.onreadystatechange=function() {
		if (x.readyState==4){
			obj.length = 0;
            eval(x.responseText);			
			for(var i=0;i<dados.length;i++){
				var name  = unescape(dados[i][0]);
				var value = unescape(dados[i][1]);
                obj.options[obj.options.length] = new Option(name, value);
			}
			obj.selectedIndex = sel;
		}
	}
    x.send(null);
}

function TcInit(){	
	var tags = ["textarea", "input", "select"];	
	for (var j=0; j<=tags.length; j++){		
		var ipt = document.body.getElementsByTagName(tags[j]);	 	 
		var len = ipt.length;		
		for (var i=0; i<len; i++){
			var t = ipt[i].getAttribute('orbtype');
			if (t){
				eval("window."+ipt[i].name+" = new "+t+"(ipt[i]);");
				eval("window."+ipt[i].name+".init();");
			}
		}
	}
}

function TcSubmit(f){
	var tags = ["textarea", "input"];	
	for (var j=0; j<=tags.length; j++){		
		var ipt = document.body.getElementsByTagName(tags[j]);	 	 
		var len = ipt.length;	 
		for (var i=0; i<len; i++){
			var t = ipt[i].getAttribute('orbtype');
			if (t){
				eval(ipt[i].name+".setValue();");				
			}
		}
	 }	 
	var l = f.length;
	for (var j=0; j<l; j++){				
		var t = f[j].getAttribute('notnull');
		if (t){
			if (f[j].value==""){
				alert(t);
				f[j].focus();
				return false;
			}
		}		
	}
	return true;	
}


function orb_send(){
	this.location.href=root+"/enviar.htm?link_pagina="+escape(this.location.href);
}

function orb_print(){
	window.print();
}

window.onload = TcInit;
