CONST_CAD_FISICO		= 1
CONST_CAD_JURIDICO		= 2
CONST_CAD_CLI			= 3
CONST_CAD_TRANSP		= 4
CONST_CAD_FOR			= 8



function writeLayer(id,text,width,height){
	layer = "<ilayer width=\""+width+"\" id=\""+id+"_1\" height=\""+height+"\">";
	layer += "<layer id=\""+id+"_2\" width=\""+width+"\" height=\""+height+"\">";
	layer += "<div id=\""+id+"_3\" width=\""+width+"\" height=\""+height+"\">"+text+"</div>";
	layer += "</layer></ilayer>";
	document.write(layer);
}
function setText(text,id){
	if (document.all) { 
		var id3 = id+"_3"; document.all[id3].innerHTML = text;
	} else if (document.layers) {
		var obj = eval("document."+id+"_1.document."+id+"_2.document");
		obj.write(text); obj.close();
	}
}



function addItem(obj,strText,strValue,blSel,intPos){
	var newOpt,i,ArTemp,selIndex;
	selIndex = (blSel)?intPos:obj.selectedIndex;
	if(intPos == null) intPos = obj.options.length;
	newOpt = new Option(strText,strValue);
	Len = obj.options.length+1
	if (intPos > Len) return
	obj.options.length = Len
	if (intPos != Len) {
		ArTemp = new Array();
		for(i=intPos;i<obj.options.length-1;i++)
			ArTemp[i] = Array(obj.options[i].text,obj.options[i].value);
		for(i=intPos+1;i<Len;i++)
			obj.options[i] = new Option(ArTemp[i-1][0],ArTemp[i-1][1]);
	}
	obj.options[intPos] = newOpt;
	if (selIndex > intPos)
		obj.selectedIndex = selIndex+1;
	else if (selIndex == intPos) 
		obj.selectedIndex = intPos;
}

function delItem(obj,intPos){
	if(intPos > obj.length) return;
	obj.options[intPos] = null
}

function emptyObject(obj){
	obj.length = 0;
}

function isInObject(obj,strValue){
	if(obj.length == 0) return false;
	for(i=0;i<obj.length;i++) {
		if(String(obj.options[i].value) == strValue) {
			return true;		
		}
	}
	return false;
}


function Position(obj) {
    var coords = { x: 0, y: 0 };
    while (obj) {
      coords.x += obj.offsetLeft;
      coords.y += obj.offsetTop;
      obj = obj.offsetParent;
    }
	return coords;
}

function Size(obj) {
	var size = { height: 0, width: 0 };
    while (obj) {
		size.height += obj.offsetHeight;
    	size.width += obj.offsetWidth;
		obj = obj.offsetParent;
	}
	return size;
}

function FormatDate(dateStr, tipo) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var matchArray = dateStr.match(datePat); 
	if (matchArray == null) {
		alert(dateStr + " - Uma data não possui um formato correto. Verifique!")
		return false;
	}
	day = matchArray[1]; month = matchArray[3]; year = matchArray[4];
	
	if (tipo) {
		return year + '-' + month + '-' + day;
	} else {
		return day + '/' + month + '/' + year;
	}
}

function isValidDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var matchArray = dateStr.match(datePat); 
	if (matchArray == null) {
		alert(dateStr + " - Uma data não possui um formato correto. Verifique!")
		return false;
	}
	day = matchArray[1]; month = matchArray[3]; year = matchArray[4];
	if (month < 1 || month > 12) { 
		alert("O valor do mês deve ser um número entre 1 e 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("O valor do dia é um número entre 1 e 31 .");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("O mês "+month+" não possui 31 dias!")
		return false;
	}
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("Fevereiro de " + year + " não tem " + day + " dias!");
			return false;
		}
	}
	return true;
}

function dateCompare(dt1, dt2) 
{
	/* 
	Possiveis resultados da função
		dt1 < dt2 = -1
		dt1 = dt2 =  0
		dt1 > dt2 =  1
	*/

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var matchArray1 = dt1.match(datePat); 
	var matchArray2 = dt2.match(datePat);
	
	var date1 = matchArray1[4] + '' + matchArray1[3] + '' + matchArray1[1];
	var date2 = matchArray2[4] + '' + matchArray2[3] + '' + matchArray2[1];
	
	var resultado = 0;
	if((date1 - date2) > 0)
	{
		var resultado = 1;
	}
	else if((date1 - date2) < 0)
	{
		var resultado = -1;
	}
	
	return resultado;
}

function ClearNumber(fltNro){
	if(fltNro == "") return 0;
	fltNro = String(fltNro); 
	bNegative = (fltNro.charAt(0) == '-')?true:false;
	bolJaTem = false; 
	fltNovoNumero = ""	
	for(var i=fltNro.length;i>0;i--) {
		strEsteCar = fltNro.charAt(i-1)
		if((strEsteCar == "," || strEsteCar == ".") && !bolJaTem) {
			bolJaTem = true;
			fltNovoNumero = "."+fltNovoNumero
		} else if (strEsteCar >= "0" && "9" >= strEsteCar) fltNovoNumero = strEsteCar+fltNovoNumero
	}
	fltNovoNumero = parseFloat(fltNovoNumero);
	fltNovoNumero = (bNegative)?-(fltNovoNumero):fltNovoNumero;

	return fltNovoNumero;
}

function FormatCurrency (number, precision, negativeMode) {
	number = ClearNumber(number);
	if(precision == null) precision = 2
	if(negativeMode == null) negativeMode = 1
	precision = ClearNumber(precision);

	number = Math.round(number * Math.pow(10, precision))/Math.pow(10, precision);

	var bNegative = false;
	if(number < 0) {
		bNegative = true;
		number *= -1;
	}
	var pFlt = ""
	if(!isNaN(number)){
		pInt = parseInt(number); number = String(number); pt = number.indexOf(".");
		if(pt != -1) { 
			pFlt = number.substring(pt+1,number.length); 
			while(pFlt.length < precision) pFlt = pFlt+String("0");
		} else 
			while(pFlt.length < precision) pFlt = pFlt+"0";

		pInt = String(pInt);
		nPts = parseInt(pInt.length / 3);
		if(nPts != 0) { for(q=nPts;q>0;q--) { pos = pInt.length-(q*3); if(pos!=0) pInt = pInt.substring(0,pos)+"."+pInt.substring(pos,pInt.length) } }
		number = (precision <= 0)?pInt:pInt+","+pFlt; 
		if(negativeMode == 1) return (bNegative)?'('+number+')':number;
		else return (bNegative)?'-'+number:number;
	}else{
		number = 0;
		return number;
	}
}

function XMLObject (){
	// PRIVATE
	try {
		this._xml =  new ActiveXObject('MSXML');
	} catch (e) {
		alert('Não foi possível instanciar o objeto XML, por favor atualize-o');
	}
	this._root = './';
	this._error = '';
	this._limit = 100;
	// PUBLIC
	this.setRoot = function (sPath) {
		if(sPath != '')
			this._root = sPath;
	}
	this.getError = function () {
		return this._error;
	}
	this.clearError = function () {
		this._error = '';
	}
	this.setLimit = function(iLimit) {
		if(!isNaN(iLimit))
			this._limit = iLimit;
	}
	
	this.getXMLObject = function() {
		return this._xml;
	}
	
}
XMLObject.prototype.getProdutoMestre = function(CodigoMestre, Descricao) {
	var arRet = Array();
	if (!CodigoMestre) CodigoMestre = '';
	if (!Descricao) Descricao = '';
	sUrl = this._root+"comum/xml_produto_mestre.cfm?no_javascript=true&fCodigoMestre="+CodigoMestre+"&fDescricao="+Descricao+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n=0;n<oXMLRoot.children.length;n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { CodigoMestre: oRow.children.item(0).text, CodigoInterno: oRow.children.item(1).text, Descricao: oRow.children.item(2).text };
		}
	}
	return arRet;
}


XMLObject.prototype.getCarteiras = function(CodigoEmpresa) {
	var arRet = Array();
	if(isNaN(CodigoEmpresa)) {
		this._error = "Empresa inválida";
		return false; 
	}

	sUrl = this._root+"comum/xml_carteiras.cfm?no_javascript=true&fEmpresa="+CodigoEmpresa;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n=0;n<oXMLRoot.children.length;n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { 
				CodigoLocalizacao: oRow.children.item(0).text, 
				CodigoInterno: oRow.children.item(1).text, 
				CodigoAgencia: oRow.children.item(2).text,
				NumeroConta: oRow.children.item(3).text 
				};
		}
	}
	return arRet;
}

XMLObject.prototype.getCliente = function(CodigoPrincipal, CNPJ) {
	alert('Atenção: \n\nEsta aplicação chamou a função HTTP::getCliente() que não está disponível\n\nPor favor solicite suporte');
	return
/*
	var arRet = Array();
	if (!CodigoPrincipal) CodigoPrincipal = '';
	if (!CNPJ) CNPJ = '';
	sUrl = this._root+"comum/xml_clientes.cfm?no_javascript=true&fCodigoPrincipal="+CodigoPrincipal+"&fCNPJ="+CNPJ+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n=0;n<oXMLRoot.children.length;n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { CodigoRevenda: oRow.children.item(0).text, CodigoPrincipal: oRow.children.item(1).text, Descricao: oRow.children.item(2).text };
		}
	}
	return arRet;
*/
}

XMLObject.prototype.getClienteJuridico = function(CodigoPrincipal, CNPJ) {
	alert('Atenção: \n\nEsta aplicação chamou a função HTTP::getClienteJuridico() que não está disponível\n\nPor favor solicite suporte');
	return
	//return this.getCliente(CodigoPrincipal, CNPJ);
}

XMLObject.prototype.getTransportadora = function(CodigoPrincipal, CNPJ) {

	alert('Atenção: \n\nEsta aplicação chamou a função HTTP::getTransportadora() que não está disponível\n\nPor favor solicite suporte');
	return
	/*
	var arRet = Array();
	if (!CodigoPrincipal) CodigoPrincipal = '';
	if (!CNPJ) CNPJ = '';
	sUrl = this._root+"comum/xml_transportadora.cfm?no_javascript=true&fCodigoPrincipal="+CodigoPrincipal+"&fCNPJ="+CNPJ+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n=0;n<oXMLRoot.children.length;n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { CodigoTransportadora: oRow.children.item(0).text, CodigoPrincipal: oRow.children.item(1).text, Descricao: oRow.children.item(2).text };
		}
	}
	return arRet;
	*/
}

XMLObject.prototype.BuscaCadastro = function(Tipo, CodigoPrincipal, CNPJ) {
	var arRet = Array();
	if (!CodigoPrincipal) CodigoPrincipal = '';
	if (!CNPJ) CNPJ = '';

	strClienteFisico = (Tipo & CONST_CAD_FISICO)?"true":"false";
	strClienteJuridico = (Tipo & CONST_CAD_JURIDICO)?"true":"false";
	strTransportador = (Tipo & CONST_CAD_TRANSP)?"true":"false";
	strFornecedor = (Tipo & CONST_CAD_FOR)?"true":"false";

	sUrl = this._root+
		"comum/xml_busca_cadastro.cfm?no_javascript=true&"+
		"fCodigoPrincipal="+CodigoPrincipal+
		"&fCNPJ="+CNPJ+
		"&fClienteFisico="+strClienteFisico+
		"&fClienteJuridico="+strClienteJuridico+
		"&fTransportador="+strTransportador+
		"&fFornecedor="+strFornecedor+
		"&fLimit="+this._limit;
//	document.write(sUrl)
//	return false;
	
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n=0;n<oXMLRoot.children.length;n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { Codigo: oRow.children.item(0).text, CodigoPrincipal: oRow.children.item(1).text, Descricao: oRow.children.item(2).text };
		}
	}
	return arRet;
}



XMLObject.prototype.getAreas = function(CodigoEmpresa) {
	var arRet = Array();
	if (isNaN(CodigoEmpresa)) {
		return false;
		this._error = 'Código de empresa inválido';
	}
	sUrl = this._root+"comum/xml_areas.cfm?no_javascript=true&fCodigoEmpresa="+CodigoEmpresa+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n=0;n<oXMLRoot.children.length;n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { CodigoArea: oRow.children.item(0).text, Descricao: oRow.children.item(1).text };
		}
	}
	return arRet;
}

XMLObject.prototype.getModelos = function(CodigoLinha) {
	var arRet = Array();
	if (isNaN(CodigoLinha)) {
		return false;
		this._error = 'Código Familia inválido';
	}
	sUrl = this._root+"comum/xml_modelos_produtos.cfm?no_javascript=true&fCodigoFamilia="+CodigoLinha+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n = 0; n < oXMLRoot.children.length; n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { CodigoModelo: oRow.children.item(0).text, Descricao: oRow.children.item(1).text };
		}
	}
	return arRet;
}


XMLObject.prototype.getCep = function(CEP) {
	var arRet = Array();
	strCEP = CEP.toString().Clear();
	if (isNaN(strCEP) || strCEP.length < 5) {
		return false;
		this._error = 'CEP Inválido';
	}
	sUrl = this._root+"comum/xml_cep.cfm?no_javascript=true&fCEP="+CEP+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}

	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n=0;n<oXMLRoot.children.length;n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { 
				CodigoLocalidade: oRow.children.item(0).text, 
				CEP: oRow.children.item(1).text,
				TipoLogradouro: oRow.children.item(2).text,
				Logradouro: oRow.children.item(3).text,
				Bairro: oRow.children.item(4).text,
				Cidade: oRow.children.item(5).text,
				UF: oRow.children.item(6).text
			};
		}
	}
	return arRet;
}

XMLObject.prototype.getItensCotacao = function(CodigoCotacao) {
	var arRet = Array();

	sUrl = this._root+"comum/xml_busca_itenscotacao.cfm?no_javascript=true&fCodigoCotacao="+CodigoCotacao;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}

	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n = 0; n < oXMLRoot.children.length; n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { 
				NumeroNota: oRow.children.item(0).text, 
				NumeroSerie: oRow.children.item(1).text,
				Contrato: oRow.children.item(2).text
			};
		}
	}
	return arRet;
}

/* Busca os produtos relacionados ao mestre*/
XMLObject.prototype.getBuscaProdutosMestre = function(CodigoProduto, CodigoProdutoOriginal) {
	var arRet = Array();

	sUrl = this._root+"comum/xml_busca_produtosmestre.cfm?no_javascript=true&fCodigoProduto="+CodigoProduto+"&fCodigoProdutoOriginal="+CodigoProdutoOriginal;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}

	oXMLRoot = this._xml.root;
	if(oXMLRoot.children) {
		for(var n = 0; n < oXMLRoot.children.length; n++) {
			oRow = oXMLRoot.children.item(n);
			arRet[arRet.length] = { 
				CodigoProduto: oRow.children.item(0).text, 
				CodigoProdutoOriginal: oRow.children.item(1).text,
				Multiplo: oRow.children.item(2).text
			};
		}
	}
	return arRet;
}


XMLObject.prototype.InsereFavorito = function(CodigoMenu) {
	var arRet = Array();
	sUrl = this._root+"comum/xml_insere_favorito.cfm?no_javascript=true&fCodigoMenu="+CodigoMenu+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	return (oXMLRoot.children.item(0).text == 'OK')
}


XMLObject.prototype.ExcluiFavorito = function(CodigoMenu) {
	var arRet = Array();
	sUrl = this._root+"comum/xml_exclui_favorito.cfm?no_javascript=true&fCodigoMenu="+CodigoMenu+"&fLimit="+this._limit;
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	return (oXMLRoot.children.item(0).text == 'OK')
}

XMLObject.prototype.getBuscaCotacao = function(NumeroNota, Transportadora, CodigoArea, CodigoEmpresa) {
	var arRet = Array();
	// if (!NumeroNota) NumeroNota = '';
	// if (!Descricao) Descricao = '';
	sUrl = this._root+
	"comum/xml_busca_cotacaofrete.cfm?no_javascript=true" + 
	"&fNumeroNota="+NumeroNota+
	"&fCodigoTransportadora="+Transportadora+
	"&fCodigoArea="+CodigoArea+
	"&fCodigoEmpresa="+CodigoEmpresa+
	"&fLimit="+this._limit;
	
	//document.write(sUrl);
	
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}
	oXMLRoot = this._xml.root;
	if(oXMLRoot.children)
		return oRow = oXMLRoot.children.item(0).text;
	else
		return false;
}


XMLObject.prototype.getBuscaAreaEmpresa = function(CGC) {
	var arRet = Array();
	sUrl = this._root+
	"comum/xml_busca_areaempresa.cfm?no_javascript=true" + 
	"&fCgcEntrega="+CGC+
	"&fLimit="+this._limit;
	
	try {
		this._xml.url = sUrl;
	} catch(e) {
		this._error = e.description;
		return false; 
	}

	oXMLRoot = this._xml.root;
	if(oXMLRoot.children)
	{
		oRow = oXMLRoot.children;
		arRet[arRet.length] = { 
			CodigoArea: oRow.item(0).text, 
			CodigoEmpresa: oRow.item(1).text 
		};
		return arRet;
	}	
}


function ValidadorIE(IE, UF) {
	this._ret = 1;
	this.ValidaIE = function(IE, UF) {
		sUrl = this._root+"comum/xml_ie.cfm?no_javascript=true&fIE="+IE.Clear()+"&fUF="+UF;
		try {
			this._xml.url = sUrl;
		} catch(e) {
			this._error = e.description;
			return false; 
		}
		oXMLRoot = this._xml.root;
		this._ret = oXMLRoot.children.item(0).text;
	}
	this.getResult = function() {
		return this._ret;
	}
}
ValidadorIE.prototype = new XMLObject();

function HTTPRequest() {

	this._variables = new Array();
	this._path = '';

	this.clearVariables = function() {
		this._variables.length = 0;
	}
	this.setVariable = function(Name,Value) {
		aVars = this._variables;
		bFound = false;
		for(var n=0; n < aVars.length; n++) {
			if(aVars[n].name == Name) {
				aVars[n].value = Value;
				bFound = true;
			}
		}
		if(!bFound)
			aVars[aVars.length] =  {name: Name, value: Value};
		this._variables = aVars;
	}
	this.getVariableCount = function() {
		return this._variables.length;
	}
	this.setService = function(Value) {
		this._path = Value;
	}

	this.getRequest = function() {
		var Variables = ''
		for(var i=0; i<this._variables.length;i++) {
			Variables += '&'+this._variables[i].name+'='+this._variables[i].value;
		}
		return this._path + '?no_javascript=true'+Variables;
	}

	this.sendRequest = function () {
		try {
			this._xml.url = this.getRequest();
		} catch(e) {
			this._error = e.description;
			return false; 
		}
		return true;
	}
}
HTTPRequest.prototype = new XMLObject();

function Floater(layerId) {

	this.topMargin 	= 0;
	this.slideTime 	= 300;

	this.ns6 		= (!document.all && document.getElementById);
	this.ie4 		= (document.all);
	this.ns4 		= (document.layers);

	this.currentX 	= 0;
	this.currentY 	= 0;
	this.scrollTop 	= 0;

	this.A 			= 0;
	this.B 			= 0;
	this.C 			= 0;
	this.D 			= 0;

	this.style 		= null
	this.obj		= null;

	if(this.ns6 || this.ns4) {
		this.pageWidth = innerWidth;
		this.pageHeight = innerHeight;
	} else if(this.ie4) {
		this.pageWidth = document.body.clientWidth;
		this.pageHeight = document.body.clientHeight;
	}
	
	if (this.ns6) {
		this.obj = document.getElementById(layerId);
		this.style = this.obj.style;
	}
	if (this.ie4) {
		this.obj = document.all[layerId];
		this.style = this.obj.style;
	}
	if (this.ns4) {
		this.obj = document.layers[layerId];
		this.style = this.obj;
	}

	this.topMargin = parseInt(this.style.top);
	this.leftMargin = parseInt(this.style.left);

	if (this.ns4 || this.ns6) {
		this.findHt = window.innerHeight;
	} else if(this.ie4) {
		this.findHt = document.body.clientHeight;
	}

	this.setSlideTime = function(time) {
		this.slideTime = time;
	}

	this.getSlideTime = function(time) {
		return this.slideTime;
	}

	this.calcPosition = function() {
		var now = new Date();
		this.A = this.targetY - this.currentY;
		this.B = Math.PI / ( 2 * this.slideTime );
		this.C = now.getTime();
		if (Math.abs(this.A) > this.findHt) {
			this.D = this.A > 0 ? this.targetY - this.findHt : this.targetY + this.findHt;
			this.A = this.A > 0 ? this.findHt : -this.findHt;
		} else {
			this.D = this.currentY;
		}
	}

	this.setPosition = function() {

		var now = new Date();
		var newY = this.A * Math.sin( this.B * ( now.getTime() - this.C ) ) + this.D;
		newY = Math.round(newY);

		if (( this.A > 0 && newY > this.currentY ) || ( this.A < 0 && newY < this.currentY )) {
			if ( this.ie4 ) this.style.pixelTop = newY;
			if ( this.ns4 ) this.style.top = newY;
			if ( this.ns6 ) this.style.top = newY + "px";
		}

	}
	this.float = function() {
		if (this.ns4) {
			this.currentX = this.style.left;
			this.currentY = this.style.top;
			this.scrollTop = window.pageYOffset;
		} else if(this.ns6) {
			this.currentX = parseInt(this.style.left);
			this.currentY = parseInt(this.style.top);
			this.scrollTop = scrollY;
		} else if(this.ie4) {
			this.currentX = this.style.pixelLeft;
			this.currentY = this.style.pixelTop;
			this.scrollTop = document.body.scrollTop;
		}
		var newTargetY = this.scrollTop + this.topMargin;
		if (this.currentY != newTargetY ) {
			if ( newTargetY != this.targetY ) {
				this.targetY = newTargetY;
				this.calcPosition();
			}
			this.setPosition();
		} 
	}
}

Mouse = {x: 0, y: 0};
ObjectDown = null;

function fncGetMousePosition() {
	Mouse.x = event.clientX;
	Mouse.y = event.clientY;
}

function fncHandleMouseDown() {
	ObjectDown = event.srcElement;
}

function fncHandleMouseUp() {
	ObjectDown = null;
}

window.onmousemove 	= fncGetMousePosition;
document.onmousedown 	= fncHandleMouseDown;
document.onmouseup 	= fncHandleMouseUp;

String.prototype.Trim = function() {
	return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

String.prototype.Clear = function() {
	return this.replace(/[\\/.,-]/g,"");
}

String.prototype.LJustify = function(Size, Char) {
	var sText = this;
	if(Char == null) Char = ' ';
	while(sText.length < Size)
		sText += Char;	
	return sText;
}

String.prototype.RJustify = function(Size, Char) {
	var sText = this;
	if(Char == null) Char = ' ';
	while(sText.length < Size)
		sText = Char+sText;	
	return sText;
}


Array.prototype.RemoveValue = function(Value) {

	var pArray = this;
	var i=0;
	var nArray = new Array();
	for(i=0;i<pArray.length;i++)
		if(pArray[i] != Value)
			nArray[nArray.length] = pArray[i];
	return nArray;

}

Array.prototype.isIn = function(Value,Field) {
	var pArray = this;
	var j = 0;
	for(j=0;j<pArray.length;j++) {
		thisValue = pArray[j];
		if(Field != null) thisValue = eval('thisValue.'+Field);
		if(thisValue == Value)
			return true;
	}
	return false;
}

String.prototype.isCnpj = function() {
	return isCPFCNPJ(this, 2);
}
//Number.prototype.isCnpj = function() {
//	return isCPFCNPJ(this, 2);
//}

String.prototype.isCpf = function() {
	return isCPFCNPJ(this, 1);
}

//Number.prototype.isCpf = function() {
//	return isCPFCNPJ(this, 1);
//}

function isCPFCNPJ(campo,pType){
	if( campo == '' || campo == null ) return false;
	var campo_filtrado = "", valor_1 = " ", valor_2 = " ", ch = "";
	var valido = false;
	for (i = 0; i < campo.length; i++){
		ch = campo.substring(i, i + 1);
		if (ch >= "0" && ch <= "9"){
			campo_filtrado = campo_filtrado.toString() + ch.toString()
			valor_1 = valor_2;
			valor_2 = ch;
		}
		if ((valor_1 != " ") && (!valido)) valido = !(valor_1 == valor_2);
	}
	if (!valido) campo_filtrado = "12345678912";
	if (campo_filtrado.length < 11)
		for (i = 1; i <= (11 - campo_filtrado.length); i++)
			campo_filtrado = "0" + campo_filtrado;

	if(pType <= 1)
		if (   ( campo_filtrado.substring(9,11) == checkCPF( campo_filtrado.substring(0,9) ) )
			&& ( campo_filtrado.substring(11,12) == "") ) 
				return true;

	if((pType == 2) || (pType == 0))
		if (campo_filtrado.length >= 14)
			if ( campo_filtrado.substring(12,14) == checkCNPJ( campo_filtrado.substring(0,12) ) )
				return true;
	return false;
}

function checkCNPJ(vCNPJ){
	var mControle = "";
	var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
	for (i = 1 ; i <= 2 ; i++){
		mSoma = 0;
		for (j = 0 ; j < vCNPJ.length ; j++)
			mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
		if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
			mDigito = ( mSoma * 10 ) % 11;
		if (mDigito == 10 ) mDigito = 0;

		mControle1 = mControle ;
		mControle = mDigito;
		aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);

	}
	return( (mControle1 * 10) + mControle );
}

function checkCPF(vCPF){
	var mControle = ""
	var mContIni = 2, mContFim = 10, mDigito = 0;
	for (j = 1 ; j <= 2 ; j++){
		mSoma = 0;
		for (i = mContIni ; i <= mContFim ; i++)
			mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
		if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
			mDigito = ( mSoma * 10 ) % 11;
		if (mDigito == 10) mDigito = 0;
		mControle1 = mControle;
		mControle = mDigito;
		mContIni = 3;
		mContFim = 11;
	}
	return( (mControle1 * 10) + mControle );
}

/*
function modulo(str) {
	soma=0;
	ind=2;
	for(pos=str.length-1;pos>-1;pos=pos-1) {
		soma = soma + (parseInt(str.charAt(pos)) * ind);
		ind++;
		if(str.length>11) {
			if(ind>9) ind=2;
		}
	}
	resto = soma - (Math.floor(soma / 11) * 11);
	if(resto < 2) return 0;
	else return (11 - resto);
}

function _isCnpj(sCnpj) {
	sCnpj = sCnpj.replace(/[\\/.,-]/g,"");
 	if (sCnpj.length != 14) return false;
	if(!(modulo(sCnpj.substring(0,sCnpj.length - 2)).toString()+modulo(sCnpj.substring(0,sCnpj.length - 1)).toString() == sCnpj.substring(sCnpj.length - 2,sCnpj.length))) return false;
	return true;
}
*/
function FormatCNPJ(teclapres, objFrm) {
	var tecla = teclapres.keyCode;
	if (( tecla >= 35 && tecla <= 40) || ( tecla >= 45 && tecla <= 46) || tecla == 8) return;
	vr = objFrm.value;
	vr = vr.replace(/[\\/.,-]/g,"");
	tam = vr.length + 1;
	if (tecla != 8 && tecla != 9)
		if (tam > 2 && tam < 5)objFrm.value = vr.substr( 0, tam - 2  ) + '.' + vr.substr(tam - 2, tam);
		if (tam > 7 && tam < 9)objFrm.value = vr.substr(0, 2 ) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, 3);
		if (tam > 11 && tam < 14)objFrm.value = vr.substr(0, 2 ) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, 3) + '/' + vr.substr(8, 4);
		if (tam > 13 && tam <= 14)objFrm.value = vr.substr(0, 2 ) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, 3) + '/' + vr.substr(8, 4) + '-' + vr.substr(12, 2);	
}

function FormataCpf(campo,tammax,teclapres) {
 	var tecla = teclapres.keyCode;
		 // vr = document.form[campo].value;
		 vr = event.srcElement.value;
		 vr = vr.replace( "/", "" );
		 vr = vr.replace( "/", "" );
		 vr = vr.replace( ",", "" );
		 vr = vr.replace( ".", "" );
		 vr = vr.replace( ".", "" );
		 vr = vr.replace( ".", "" );
		 vr = vr.replace( ".", "" );
		 vr = vr.replace( "-", "" );
		 vr = vr.replace( "-", "" );
		 vr = vr.replace( "-", "" );
		 vr = vr.replace( "-", "" );
		 vr = vr.replace( "-", "" );
		 tam = vr.length;

 if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

 if (tecla == 8 ){ tam = tam - 1 ; }
  
 if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
  if ( tam <= 2 ){ 
    event.srcElement.value = vr ; }
   if ( (tam > 2) && (tam <= 5) ){
    event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
   if ( (tam >= 6) && (tam <= 8) ){
    event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
   if ( (tam >= 9) && (tam <= 11) ){
    event.srcElement.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
   if ( (tam >= 12) && (tam <= 14) ){
    event.srcElement.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
   if ( (tam >= 15) && (tam <= 17) ){
    event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ;}
 }  
}


function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {end = dc.length;}
	return unescape(dc.substring(begin + prefix.length, end));
}

// Não permite a digitação de mais de "X" (len) caracteres no text
function fncCheckLen(obj,len){
	if(obj.value.length > len-1){
		obj.value = obj.value.substring(0,obj.value.length-1);
	}
}


