
// BROWSER DETECT
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();











// FUNCOES UTEIS

// busca em array (case sensitive)
function fncArraySearch(arrName,strSearch){
	for(var a=0;a<arrName.length;a++){ if(arrName[a]==strSearch){ return a; } }
	return -1;
}

// retorna um objeto pelo ID
function getObj(objID){
	if(document.getElementById){ return document.getElementById(objID); }
	else if(document.all){ return document.all[objID]; }
	else if(document.layers){ return document.layers[objID]; }
	return false;
}

// retorna a posio absoluta de um objeto, em pixels (left,top)
function getPos(objID) {
	if(typeof(objID.offsetParent)!="undefined"){
		for(var posX=0, posY=0; objID; objID=objID.offsetParent ){
			posX += Number(objID.offsetLeft);
			posY += Number(objID.offsetTop);
		}
		return [parseInt(posX),parseInt(posY)];
	}else{
		return [parseInt(objID.x),parseInt(objID.y)];
	}
}

// verifica se obj1  filho de obj2
function isChild(obj1,obj2){
	while(obj1){
		if(obj1==obj2){ return true; }
		obj1=obj1.parentNode;
	}
	return false;
}

// adiciona um eventHandler a um objeto
function addEventHandler(object,eventName,functionName){
	if(object.attachEvent){
		object.attachEvent("on" + eventName, functionName);
	}else if(object.addEventListener){
		object.addEventListener(eventName, functionName, true);
	}else{
		object["on" + eventName] = functionName;
	}
}

// remove um eventHandler de um objeto
function removeEventHandler(object,eventName,functionName){
	if(object.detachEvent){
		object.detachEvent("on" + eventName, functionName);
	}else if(object.removeEventListener){
		object.removeEventListener(eventName, functionName, true);
	}else{
		object["on" + eventName] = null;
	}
}

//
// getObjSize()
// Returns array with object width and height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getObjSize(objTarget){
	arrayObjSize = new Array(0,0);
	if(objTarget){ arrayObjSize = new Array(objTarget.offsetWidth,objTarget.offsetHeight); }
	return arrayObjSize;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	var xScroll, yScroll;
	if(window.innerHeight && window.scrollMaxY){
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}else if(document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}else if (document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // repete
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	}else{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if(self.innerHeight){	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}else if(document.body){ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){ pageHeight = windowHeight; }else{ pageHeight = yScroll; }
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){ pageWidth = windowWidth; }else{ pageWidth = xScroll; }
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
	var xScroll, yScroll;
	if(self.pageYOffset){
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	}else if(document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	}else if (document.body){ // all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

//
// getMousePosition()
// Returns array with x,y mouse position values.
// Core code from - quirksmode.org
//
function getMousePosition(e){
	var posx=0, posy=0;
	if(!e){ var e = window.event; }
	if(e.pageX || e.pageY){
		posx = e.pageX;
		posy = e.pageY;
	}else if(e.clientX || e.clientY){
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	arrayMousePosition = new Array(posx,posy);
	return arrayMousePosition;
}

// mostra/esconde os campos tipo SELECT (apenas no IE)
function fncSHSelects(strShowHide){
	if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<7)){
		switch(strShowHide){
		case "hidden": strShowHide="hidden"; break;
		case false: strShowHide="hidden"; break;
		case 0: strShowHide="hidden"; break;
		default: strShowHide="visible";
		}
		selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) { selects[i].style.visibility = strShowHide; }
	}
}

//
//  Returns the caret (cursor) position of the specified text field.
//  Return value range is 0-oField.length.
//
function fncGetTextCaretPos(objInput){
	var numPosIni=0, numPosEnd=0;
	// IE Support
	if(document.selection){ 
		objInput.focus(); // Set focus on the element
		var objSel = document.selection.createRange(); // To get cursor position, get empty selection range
		numLenSelect = objSel.text.length;
		objSel.moveStart("character", -objInput.value.length); // Move selection start to 0 position
		numPosEnd = objSel.text.length; // The caret position is selection length
		numPosIni = numPosEnd - numLenSelect;
	}
	// Firefox support
	else if(objInput.selectionStart || objInput.selectionStart=="0"){
		numPosIni = objInput.selectionStart;
		numPosEnd = objInput.selectionEnd;
	}
	return [numPosIni,numPosEnd];
}


//
//  Sets the caret (cursor) position of the specified text field.
//  Valid positions are 0-oField.length.
//
function fncSetTextCaretPos(objInput,numPosIni,numPosEnd){
	if(!isNaN(numPosIni)&&!isNaN(numPosEnd)){
		if(objInput.createTextRange){
			var objSel = objInput.createTextRange();
			objSel.collapse(true);
			objSel.moveStart("character", numPosIni);
			objSel.moveEnd("character", numPosEnd-numPosIni);
			objSel.select();
		}
		else if(objInput.setSelectionRange){
			objInput.focus();
			objInput.setSelectionRange(numPosIni,numPosEnd);
		}
	}
}

// insere uma string na posicao do cursor
function fncInsertAtCursor(myField,myValue){
	//IE support
	if(document.selection){
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if(myField.selectionStart || myField.selectionStart == '0'){
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0,startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}









// ##################################################
// ##################################################
// ##################################################











// HINTBOX

// cria e mostra um quadro (div) de ajuda/mensagem
function fncShowHintBox(numSize,strTexto){
	objBody = document.body;
	if(getObj("d_hint_box")){
		hintBox = getObj("d_hint_box");
	}else{
		hintBox = document.createElement("div");
		hintBox.setAttribute("id","d_hint_box");
	}
	if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<7)){
		// utiliza IFRAME para enganar o z-index das SELECTs, apenas IE<7
		if(getObj("d_hint_box_if")){
			hintBoxIf = getObj("d_hint_box_if");
		}else{
			hintBoxIf = document.createElement("iframe");
			hintBoxIf.setAttribute("id","d_hint_box_if");
			hintBoxIf.setAttribute("src","javascript:'<html></html>';");
			hintBoxIf.setAttribute("frameborder","0");
			hintBoxIf.setAttribute("scrolling","no");
		}
		objBody.appendChild(hintBoxIf);
	}
	objBody.appendChild(hintBox);
	hintBox.innerHTML = strTexto;
	
	if(isNaN(numSize)){ numSize=170; }
	if(numSize==0){ numSize=170; }
	if(numSize>2000){ numSize=2000; }
	if(numSize<100){ numSize=100; }
	hintBox.style.width = numSize+"px";
	hintBox.style.position = "absolute";
	hintBox.style.top = hintBox.style.left = "-5000px";
	hintBox.style.display = "block";
	hintBox.style.zIndex = "10000";
	
	if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<7)){
		arrHintBoxSize=getObjSize(hintBox);
		hintBoxIf.style.width = arrHintBoxSize[0]+"px";
		hintBoxIf.style.height = arrHintBoxSize[1]+"px";
		hintBoxIf.style.position = "absolute";
		hintBoxIf.style.top = hintBoxIf.style.left = "-5000px";
		hintBoxIf.style.display = "block";
		hintBoxIf.style.zIndex = "10000";
	}
	
	//fncSHSelects(0);
	addEventHandler(document,"mousemove",fncMoveHintBox);
}

// move o quadro de ajuda/mensagem conforme o mouse
function fncMoveHintBox(e){
	hintBox = getObj("d_hint_box");
	
	arrOSize=getObjSize(hintBox); // object size
	arrPSize=getPageSize(); // page size
	arrPScroll=getPageScroll(); // scroll size
	arrMPos=getMousePosition(e); // posicao do mouse
	
	pSizeW=arrPSize[2]+arrPScroll[0];
	pSizeY=arrPSize[3]+arrPScroll[1];
	sSizeY=arrPScroll[1];
	
	posX=Number(arrMPos[0])+20;
	posY=Number(arrMPos[1])-(arrOSize[1]/10)-5;
	
	if((posX+arrOSize[0]+10)>pSizeW){ posX-=arrOSize[0]+35; } // move o box para o lado esquerdo caso nao haja espaco do lado direito
	if((posY+arrOSize[1]+5)>pSizeY){ posY=pSizeY-arrOSize[1]-5; } // nao deixa o box sumir para baixo da tela (evita rolagem)
	if(posY<arrPScroll[1]+5){ posY=sSizeY+5; } // nao deixa o box sumir para cima da tela (evita rolagem)
	if(posX<5){ posX=5; }
	if(posY<5){ posY=5; }
	
	hintBox.style.left = posX+"px";
	hintBox.style.top = posY+"px";
	
	if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<7)){
		hintBoxIf = getObj("d_hint_box_if");
		hintBoxIf.style.left = posX+"px";
		hintBoxIf.style.top = posY+"px";
	}
}

// esconde o quadro de ajuda/mensagem
function fncHideHintBox(){
	hintBox = getObj("d_hint_box");
	if(hintBox){
		hintBox.innerHTML = "";
		hintBox.style.display = "none";
		hintBox = null;
	}
	if((BrowserDetect.browser=="Explorer")&&(BrowserDetect.version<7)){
		hintBoxIf = getObj("d_hint_box_if");
		if(hintBoxIf){
			hintBoxIf.style.display = "none";
			hintBoxIf = null;
		}
	}
	
	//fncSHSelects(1);
	removeEventHandler(document,"mousemove",fncMoveHintBox);
}










// ##################################################
// ##################################################
// ##################################################










// VALIDACOES, FILTROS E FORMATACOES DE ENTRADAS DE DADOS

// filtra conteudo de campos de livre digitacao
function fncFilterInit(objTarget,optFiltra){ // use: onFocus="fncFilterKey(this,1);"
	fncFilterChar(objTarget,optFiltra,true);
	addEventHandler(objTarget,"keyup",function(){ fncFilterChar(objTarget,optFiltra,true); });
	addEventHandler(objTarget,"blur",function(){ fncFilterChar(objTarget,optFiltra,false); });
}
function fncFilterChar(objTarget,optFiltra,moveCaret){
	if(objTarget){
		if(moveCaret){ // mantem o cursor na mesma posicao
			var arrCaretPos = fncGetTextCaretPos(objTarget);
			var numLength = objTarget.value.length;
		}
		
		switch(optFiltra){
		case 2: // email
			rExp = /[^0-9a-z@\._-]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /@{2,}/g; objTarget.value = objTarget.value.replace(rExp,"@"); // substitui 2 ou mais @ por apenas 1
			rExp = /\.{2,}/g; objTarget.value = objTarget.value.replace(rExp,"."); // substitui 2 ou mais . por apenas 1
			rExp = /-{2,}/g; objTarget.value = objTarget.value.replace(rExp,"-"); // substitui 2 ou mais - por apenas 1
			break;
		case 3: // data (numeros e barras)
			rExp = /[^0-9\/]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /\/{2,}/g; objTarget.value = objTarget.value.replace(rExp,"/"); // substitui 2 ou mais / por apenas 1
			break;
		case 4: // moeda (numeros e virgula)
			rExp = /[^0-9,]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /,{2,}/g; objTarget.value = objTarget.value.replace(rExp,","); // substitui 2 ou mais , por apenas 1
			break;
		case 5: // CEP (numeros e traco)
			rExp = /[^0-9-]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /-{2,}/g; objTarget.value = objTarget.value.replace(rExp,"-"); // substitui 2 ou mais - por apenas 1
			break;
		case 6: // CPF (numeros, ponto e traco)
			rExp = /[^0-9\.-]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /\.{2,}/g; objTarget.value = objTarget.value.replace(rExp,"."); // substitui 2 ou mais . por apenas 1
			rExp = /-{2,}/g; objTarget.value = objTarget.value.replace(rExp,"-"); // substitui 2 ou mais - por apenas 1
			break;
		case 7: // CNPJ (numeros, pontos, barra e traco)
			rExp = /[^0-9\.\/-]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /\.{2,}/g; objTarget.value = objTarget.value.replace(rExp,"."); // substitui 2 ou mais . por apenas 1
			rExp = /-{2,}/g; objTarget.value = objTarget.value.replace(rExp,"-"); // substitui 2 ou mais - por apenas 1
			rExp = /\/{2,}/g; objTarget.value = objTarget.value.replace(rExp,"/"); // substitui 2 ou mais / por apenas 1
			break;
		case 8: // moeda americana (numeros e ponto)
			rExp = /[^0-9\.]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /\.{2,}/g; objTarget.value = objTarget.value.replace(rExp,"."); // substitui 2 ou mais . por apenas 1
			break;
		case 9: // moeda completa (numeros, ponto e virgula)
			rExp = /[^0-9\.,]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
			rExp = /\.{2,}/g; objTarget.value = objTarget.value.replace(rExp,"."); // substitui 2 ou mais . por apenas 1
			rExp = /,{2,}/g; objTarget.value = objTarget.value.replace(rExp,","); // substitui 2 ou mais , por apenas 1
			break;
		default: // numeros
			rExp = /[^0-9]/g;
			objTarget.value = objTarget.value.replace(rExp,"");
		}
		
		if(moveCaret){ // mantem o cursor na mesma posicao
			var numOffset = numLength-objTarget.value.length;
			fncSetTextCaretPos(objTarget,arrCaretPos[0]-numOffset,arrCaretPos[1]-numOffset);
		}
	}
}



// remove espacos do comeco e fim da string
function fncTrim(strText){
	return strText.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
}

// formata data dd/mm/aaaa em campo input
function fncFormatDate(objTarget,objEvent){ // use: onKeyDown="fncFormatDate(this,event);"
	arrAccessKeys = new Array(8,46,16,33,34,35,36,37,38,39,40,116); // - tab - enter
	numEventKey=(objEvent.which)?objEvent.which:objEvent.keyCode;
	if(fncArraySearch(arrAccessKeys,numEventKey)==-1){
		if( (objTarget.value.length==2)||(objTarget.value.length==5) ){ objTarget.value+="/"; }
	}
}

// formata moeda 99,99 em campo input
function fncFormatMoney(objTarget,objEvent){ // use: onKeyUp="fncFormatMoney(this,event);"
	arrAccessKeys = new Array(13,16,33,34,35,36,37,38,39,40,116); // - enter
	numEventKey=(objEvent.which)?objEvent.which:objEvent.keyCode;
	if(fncArraySearch(arrAccessKeys,numEventKey)==-1){
		objTarget.value=objTarget.value.replace(",","");
		if(objTarget.value.length>2){
			objTarget.value=objTarget.value.substring(0,objTarget.value.length-2)+","+objTarget.value.substring(objTarget.value.length-2,objTarget.value.length);
		}
	}
}



// swap image
function fncSwapImg(objTarget,strSrc){
	if(objTarget){
		objTarget.lowsrc=objTarget.src;
		objTarget.src=strSrc;
	}
}
// restore image
function fncRestoreImg(objTarget){
	if(objTarget){
		objTarget.src=objTarget.lowsrc;
	}
}










// ##################################################
// ##################################################
// ##################################################










// RETIRA CONFIRMACAO DO FLASH

/*window.onload = function(){
	theObjects = document.getElementsByTagName("object");
	for(i=0; i<theObjects.length; i++){ theObjects[i].outerHTML=theObjects[i].outerHTML; }
}*/










// ##################################################
// ##################################################
// ##################################################










// FUNCOES ADMIN

function fncUploadWindow(strLabel,strCampo,numTipo,numLargura,numAltura,numTamanho,strAntigo){
	window.open("upload_window.php?label="+strLabel+"&campo="+strCampo+"&tipo="+numTipo+"&largura="+numLargura+"&altura="+numAltura+"&tamanho="+numTamanho+"&antigo="+strAntigo,"upload_window","width=450,height=200,top=50,left=50,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0");
}

function fncImagesWindow(strOpt){
	window.open("imagens.php?opt="+strOpt,"images_window","width=800,height=500,top=0,left=0,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1");
}

































