var ajaxGet		= null;
var varAjax		= '';
var carregado	= 0;
var ajaxGetTxt	= 0;
var ajaxPost	= false;
var setIntv		= null;
var setIntvAjax = null;
var dupCounter	= 0;
var loadCounter = 0;
var ajaxErr		= 0;
var ajaxFinish  = null;
var paccont		= 0;
var iniEdit		= null;
var counterCal	= 0;
var counterEditor	= 0;
var loadImgPopUp	= null;
var loadImgPopUp2	= null;
var imgLoadP		= null;
var imgLoadP2		= null;
var dupCounter		= null;
var dupCounter2		= null;
var uploadImgCounter = null;
var uploadImagens	 = null;
var arrImagens		 = null;
// Carrega imagem load para ajax
	myImage = new Image()
	myImage.src = "view/default/images/ajax-loader.gif" 
// Carrega imagem load para ajax
function recuperaVarAjax(variavel){
	varAjax = variavel;
	return varAjax;
}


// DUPLICA OBJETO
	function duplicateHTML(paste, baseid, extraText){			
			if(document.getElementById(paste)){
				dupCounter++;
				var destination = document.getElementById(paste);
				var newentry = document.createElement('div');

				newentry.value='';
				if(baseid)
				{
					newid = baseid+dupCounter;
					newentry.innerHTML = extraText;
					newentry.id=newid;
				}

				destination.appendChild(newentry);
			}
	}
	function duplicateHtmlObj(copy, paste, baseid){
		if(document.getElementById(copy)){

			dupCounter2++;
			var type = document.getElementById(copy).nodeName; // get the tag name of the source copy.

			var destination = document.getElementById(paste);
			var source      = document.getElementById(copy).cloneNode(true);

			var newentry = document.createElement(type);

			newentry.appendChild(source);
			newentry.value='';
			if(baseid)
			{
				newid = baseid+dupCounter2;
				TEXT = '<div style="margin:0 auto; text-align:right;"><input type="button" value="X" align="right" onClick="document.getElementById(\''+newid+'\').innerHTML = \'\'"></div>\n'
				newentry.innerHTML = TEXT+newentry.innerHTML.replace(new RegExp(baseid, 'g'), newid);
				newentry.id=newid;
			}

			destination.appendChild(newentry);
		}
	}

// DUPLICA OBJETO





// AJAX GET
function loadAjaxGet(url, idWrite, opcao, newUrl)
{
	ajaxFinish  = 0;
	ajaxGetTxt  = 0;
	ajaxGet = null;
	var texto = null;
	var minhastring = -1;
	var divIdName		= idWrite; 
	var element			= document.getElementById(idWrite);
	var divCarregando	= document.getElementById('divCarregando');
	var pgAltura		= document.body.scrollHeight>480 ? document.body.scrollHeight : 480;
	var pgLargura		= document.body.scrollWidth;


	 //if(!opcao){
		 if(divCarregando)
		 {
			divCarregando.style.display = "block";
			divCarregando.innerHTML		= "<div style='z-index:1000; position:absolute; margin-top:0px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:"+pgAltura+"px; filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70; background:#F1F1F1;' align='center'></div><div style='z-index:1001; position:absolute; margin-top:0"+(pgAltura/2)+"px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:30px; text-align:center; background-image:url(view/default/images/ajax-loader.gif); background-repeat:no-repeat; background-position:center center'>&nbsp;</div>";
		 }
	 //}
	//mostra carregando
	loadCounter	= 0;
	setIntv = setInterval(function ()
	{
		if((loadCounter>0 && ajaxFinish==1) || (ajaxFinish ==1 && ajaxErr == 1)){
			divCarregando.style.display = "none";
			divCarregando.innerHTML = "";
			clearInterval(setIntv);
		}
		else
			loadCounter++;
	}, 100);	
	
	// DEFINE OBJETO A SER USADO
		if(window.ActiveXObject)
			ajaxGet = new ActiveXObject("Microsoft.XMLHTTP")
		else
			if(window.XMLHttpRequest)
				var ajaxGet = new XMLHttpRequest();
			else
				ajaxGet = null;
	// ! DEFINE OBJETO A SER USADO

	if (ajaxGet)
	{	
		ajaxGet.onreadystatechange = function(){
			if (ajaxGet.readyState==4)
			{
				// if OK
				if (ajaxGet.status==200)
				{
					ajaxFinish = 1;
					retorno		= (ajaxGet.responseText)
					texto		= unescape(retorno);
					minhastring = texto.indexOf("OK");

					recuperaVarAjax(texto);
					
					if(minhastring>=0)
						ajaxGetTxt = 1;
					else{
						if(texto.indexOf("ERR", 0)>=0 || texto == "")
							ajaxGetTxt = 2;
						else
							ajaxGetTxt = 0;
					}

					if(opcao=='alert')
						alert(texto);
					else {
						if(opcao=='insertInto')
							duplicateHTML(divId, 'jogaDentro', texto)
						else
						{
							if(opcao == "redir" && newUrl!="")
							{
								alert(texto)
								loadAjaxGet(newUrl, idWrite)
							}
							else
								element.innerHTML=texto;
							
						}
					}
				}
				else{
					ajaxFinish	= 1;
					ajaxErr		= 1;
					alert("Problemas ao tentar abrir a URL: \n Erro:" + ajaxGet.statusText);
				}
		  }
	  }
	  
	  ajaxGet.open("GET",url,true)
	  ajaxGet.send(null)
	}else
	{
		alert("Verifique se seu navegador aceita objetos XML");  
	}
}
// AJAX GET

// AJAX POST
function loadAjaxPost(url, parameters, idWrite, opcao) {
	ajaxFinish  = 0;
	ajaxGetTxt  = 0;
	ajaxPost = null;
	var minhastring = -1;
	var divIdName		= idWrite; 
	var element			= document.getElementById(idWrite);
	var divCarregando	= document.getElementById('divCarregando');
	var pgAltura		= document.body.scrollHeight>480 ? document.body.scrollHeight : 480;
	var pgLargura		= document.body.scrollWidth;


	 //if(!opcao){
		 if(divCarregando)
		 {
			divCarregando.style.display = "block";
			divCarregando.innerHTML		= "<div style='z-index:10; position:absolute; margin-top:0px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:"+pgAltura+"px; filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70; background:#F1F1F1;' align='center'></div><div style='z-index:11; position:absolute; margin-top:0"+(pgAltura/2)+"px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:30px; text-align:center; background-image:url(view/default/images/ajax-loader.gif); background-repeat:no-repeat; background-position:center center'>&nbsp;</div>";
		 }
	 //}
	//mostra carregando
	loadCounter	= 0;
	setIntv = setInterval(function ()
	{
		if((loadCounter>0 && ajaxFinish==1) || (ajaxFinish ==1 && ajaxErr == 1)){
			divCarregando.style.display = "none";
			divCarregando.innerHTML = "";
			clearInterval(setIntv);
		}
		else
			loadCounter++;
	}, 100);	
	
	// DEFINE OBJETO A SER USADO
		if(window.ActiveXObject)
			ajaxPost = new ActiveXObject("Microsoft.XMLHTTP")
		else
			if(window.XMLHttpRequest)
				var ajaxPost = new XMLHttpRequest();
			else
				ajaxPost = null;
	// ! DEFINE OBJETO A SER USADO

	if (ajaxPost)
	{	
		ajaxPost.onreadystatechange = function(){
			if (ajaxPost.readyState==4)
			{
				// if OK
				if (ajaxPost.status==200)
				{
					ajaxFinish = 1;
					retorno		= (ajaxPost.responseText)
					texto		= unescape(retorno);
					minhastring = texto.indexOf("OK");

					recuperaVarAjax(texto);
					
					if(minhastring>=0)
						ajaxGetTxt = 1;
					else{
						if(texto.indexOf("ERR", 0)>=0 || texto=="")
							ajaxGetTxt = 2;
						else
							ajaxGetTxt = 0;
					}

					if(opcao=='alert')
						alert(texto);
					else {
						if(opcao=='insertInto')
							duplicateHTML(divId, 'jogaDentro', texto)
						else
							element.innerHTML=texto;
					}
				}
				else{
					ajaxFinish	= 1;
					ajaxErr		= 1;
					alert("Problemas ao tentar abrir a URL: \n Erro:" + ajaxPost.statusText);
				}
		  }
	  }
	  
	 	ajaxPost.open('POST', url, true);
		ajaxPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxPost.setRequestHeader("Content-length", parameters.length);
		ajaxPost.setRequestHeader("Connection", "close");
		ajaxPost.send(parameters);
	}else
	{
		alert("Verifique se seu navegador aceita objetos XML");  
	}
}
// AJAX POST

// DELETA ITEM
function removeItem(url, divMain){
	if(carregado==0 || paccont>=30){
			paccont=0;
			carregado = 1;
			loadAjaxGet(url, '', 'alert');
			setIntvAjax = setInterval("zeraRemoveItem('"+divMain+"')", 1000);
	}else
		alert('Aguarde! Outro processo está em Andamento');
}
		
	function zeraRemoveItem(divId){
		if(paccont>30){
			alert('Não Foi possível Completar sua requisição');
			clearInterval(setIntvAjax);
			carregado = 0;
			ajaxGetTxt = 0;
			paccont =0;
		}else{
			if(ajaxFinish==1  && ajaxGetTxt==1){
				clearInterval(setIntvAjax);
				carregado	= 0;
				ajaxGetTxt	= 0;
				paccont		= 0;
				document.getElementById(divId).style.display = 'none'
				
			}
			else{
				if(ajaxGetTxt==2)
				{
					clearInterval(setIntvAjax);
					carregado	= 0;
					ajaxGetTxt	= 0;
					paccont		= 0;
				}else
					paccont++;
			}
		}
	}
// DELETA ITEM

function aumentadiv(divM, w, h)
{
	document.getElementById(divM).style.width = (w-10)+'px';
	document.getElementById(divM).style.height = h+'px';

}
function fechaDiv(divM, divLimpa)
{
	document.getElementById(divM).style.display = 'none';
	document.getElementById('divJanela').style.display = 'none';
	document.getElementById(divLimpa).innerHTML = '';
}

function fechaPopUp(divM, divLimpa, url, divAcao){
	fechaDiv(divM, divLimpa);
	loadAjaxGet(url, divAcao);	
}


function abreDiv(divM)
{
	
	aumentadiv(divM, document.body.scrollWidth, document.body.scrollHeight);
	document.getElementById(divM).style.display = 'block';

	aumentadiv('divJanela', document.body.scrollWidth, document.body.scrollHeight);
	document.getElementById('divJanela').style.display = 'block';
}
function abrePopUp(link, divAcao, divJanela, titulo)
{
	document.getElementById('divNoMeio').innerHTML = titulo;
	abreDiv(divJanela);
	loadAjaxGet(link, divAcao);
}

function abreImgPopUp(link, divAcao, divJanela, titulo)
{
	if(document.getElementById('divNoMeio'))
		document.getElementById('divNoMeio').innerHTML = titulo;
	if(divJanela)
		abreDiv(divJanela);
	myImage = new Image();
	myImage.src = link;
	
	var pgAltura		= document.body.scrollHeight>480 ? document.body.scrollHeight : 480;
	var pgLargura		= document.body.scrollWidth;
	
	divLoadImg = document.getElementById(divAcao);

	if(divLoadImg)
	{
		//divCarregando.style.display = "block";
		divLoadImg.innerHTML		= "<div style='z-index:10; position:absolute; margin-top:0px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:"+pgAltura+"px; filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70; background:#F1F1F1;' align='center'></div><div style='z-index:11; position:absolute; margin-top:0"+(pgAltura/2)+"px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:30px; text-align:center; background-image:url(view/default/images/ajax-loader.gif); background-repeat:no-repeat; background-position:center center'>&nbsp;</div>";
	}

	imgLoadP = myImage;
	loadImgPopUp = setInterval("zeraAbreImgPopUp('"+divAcao+"')", 500);
}

function zeraAbreImgPopUp(divAcao){
	if(imgLoadP.complete)
	{
		document.getElementById(divAcao).innerHTML = '<img id="ampliaIMG">';
		document.getElementById('ampliaIMG').src =  imgLoadP.src
		clearInterval(loadImgPopUp);
	}
}

function abreImgPopUp2(link, divJanela)
{
	var i;
	myImage = new Image()
	myImage.src = link

	// CARREGANDO ...
	var pgAltura		= document.body.scrollHeight>480 ? document.body.scrollHeight : 480;
	var pgLargura		= document.body.scrollWidth;
	
	divLoadImg = document.getElementById(divJanela);

	if(divLoadImg)
		divLoadImg.innerHTML		= "<div style='z-index:10; position:absolute; margin-top:0px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:"+pgAltura+"px; filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70; background:#F1F1F1;' align='center'></div><div style='z-index:11; position:absolute; margin-top:0"+(pgAltura/2)+"px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:30px; text-align:center; background-image:url(view/default/images/ajax-loader.gif); background-repeat:no-repeat; background-position:center center'>&nbsp;</div>";
	// CARREGANDO ...

	imgLoadP2 = myImage;
	loadImgPopUp2 = setInterval("zeraAbreImgPopUp2('"+divJanela+"')", 500);

}
function zeraAbreImgPopUp2(divJanela){
	if(imgLoadP2.complete)
	{
		clearInterval(loadImgPopUp2);

		document.getElementById(divJanela).innerHTML = '<img id="ampliaIMG">';
		document.getElementById('ampliaIMG').src =  imgLoadP2.src
		
		inserirConteudo = '<div>';
		inserirConteudo += '<div style="text-align:right;"><img src="view/default/images/fechar.gif" style="cursor:pointer;" onClick="document.getElementById(\''+divJanela+'\').innerHTML = \'\';"/></div>';
		inserirConteudo += '<img id="ampliaIMG">';
		inserirConteudo += '</div>';
		document.getElementById(divJanela).innerHTML = inserirConteudo;
		imgID = document.getElementById('ampliaIMG');
		imgID.src =  imgLoadP2.src
		//imgID.style.width = '100%';
	}
}

function abreImgPopUp3(link, divAcao, objIns)
{
	myImage = new Image();
	myImage.src = link;


	if(objIns!="")
	{
		var pgLargura = document.getElementById(objIns).style.width ? document.getElementById(objIns).style.width : document.getElementById(objIns).width;
		var pgAltura  = document.getElementById(objIns).style.height ? document.getElementById(objIns).style.height : document.getElementById(objIns).height;
	}else
	{	
		var pgAltura		= document.body.scrollHeight>480 ? document.body.scrollHeight : 480;
		var pgLargura		= document.body.scrollWidth;
	}


	pgAltura = pgAltura.replace("px", "");
	pgLargura = pgLargura.replace("px", "")

	divLoadImg = document.getElementById(divAcao);

	
	if(divLoadImg && (pgAltura>0 && pgLargura>0))
		divLoadImg.innerHTML		= "<div style='z-index:10; position:absolute; margin-top:0px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:"+pgAltura+"px; filter:alpha(opacity=70);-moz-opacity:.70;opacity:.70; background:#F1F1F1;' align='center'></div><div style='z-index:11; position:absolute; margin-top:"+(pgAltura/2)+"px; margin-left:0px; color:#FF0000; width:"+pgLargura+"px; height:30px; text-align:center; background-image:url(view/default/images/ajax-loader.gif); background-repeat:no-repeat; background-position:center center'>&nbsp;</div>";
	
	imgLoadP = myImage;
	loadImgPopUp = setInterval("zeraAbreImgPopUp3('"+divAcao+"')", 500);
}

function zeraAbreImgPopUp3(divAcao){
	if(imgLoadP.complete)
	{
		document.getElementById(divAcao).innerHTML = '<img id="ampliaIMG">';
		document.getElementById('ampliaIMG').src =  imgLoadP.src
		clearInterval(loadImgPopUp);
	}
}


// SUBMIT DE FORMULARIO GENERICO
	function submitPost(form, url, divIns, md5true)
	{
			md5true = md5true!=false ? true : false;

			formulario = document.getElementById(form);
			var stringPost = '';
			var erro = 0;
			for (i=0;i<formulario.elements.length;i++){
				if(formulario.elements[i].type!="submit"){
					if(formulario.elements[i].type == "radio"){
						if(formulario.elements[i].checked == true)
							stringPost += formulario.elements[i].id+'='+(formulario.elements[i].value)+"&";
					}else{				
						if(formulario.elements[i].type=="select-multiple")
						{
							for(j=0; j<formulario.elements[i].length; j++)
							{
								if(formulario.elements[i][j].selected==true)
									stringPost += formulario.elements[i].id+'['+j+']'+'='+(formulario.elements[i][j].value)+"&";
							}
						}else{
							if(formulario.elements[i].value!=""){
								if((formulario.elements[i].id.indexOf('senha')>=0 || formulario.elements[i].id.indexOf('pass')>=0) && md5true==true)
									stringPost += formulario.elements[i].id+'='+hex_md5(formulario.elements[i].value)+"&";	
								else
									stringPost += formulario.elements[i].id+'='+(formulario.elements[i].value)+"&";
							}else{
								erro = 1; break;
							}
						}
					}
				}
			}
		
			if(erro==0)
			{
				loadAjaxPost(url, stringPost, divIns)
			}else
				alert('Todos os campos são obrigatórios!');
	}
	
	// Carrega Calendario
	function loadCalendario(input)
	{
		calendIntv = null;
		calendIntv = setInterval("zeraLoadCalendario('"+input+"')", 1000);
	}
	function zeraLoadCalendario(input){
		if(counterCal>10)
		{
			counterCal = 0;
			clearInterval(calendIntv);
			//alert('Nao Foi Possivel Carregar o Calendario!');
		}
		else
		{
			if(document.getElementById(input))
			{
				Calendar.setup(
					{ 
						inputField : input, 
						align : 'Tr'
					}
				);
				counterCal = 0;
				clearInterval(calendIntv);
			}else
				counterCal++;
		}
	}
	// Carrega Calendario
	// Carrega Editor
	function loadEditor(editor)
	{
		iniEdit = null;		
		iniEdit = setInterval("zeraLoadEditor('"+editor+"')", 1000);
	}
	function zeraLoadEditor(editor){
		if(counterCal>10)
		{
			alert('Nao Foi Possivel Carregar o Calendario!');
			counterEditor = 0;
			clearInterval(iniEdit);
		}
		else
		{
			if(document.getElementById(editor))
			{
				

				xinha_editors = null;
				xinha_init    = null;
				xinha_config  = null;
				xinha_plugins = null;
				
				xinha_plugins = xinha_plugins ? xinha_plugins : 
				[
				"InsertAnchor",
				"ImageManager",
				"InsertPicture",
				"CharacterMap",
				"ExtendedFileManager",
				"TableOperations",
				"FindReplace",
				"ListType"
				

				]; 
				if (!HTMLArea.loadPlugins(xinha_plugins, xinha_init)) {
					return; 
				} 
				xinha_editors = xinha_editors ? xinha_editors : 
						[
							editor
						]; 
				xinha_config = xinha_config ? xinha_config : new HTMLArea.Config(); 
				
				xinha_editors = HTMLArea.makeEditors(xinha_editors, xinha_config, xinha_plugins); 
				HTMLArea.startEditors(xinha_editors);
				
				counterEditor = 0;
				clearInterval(iniEdit);

				return false;
			}else
				counterEditor++;
		}
	}
	function isArray(obj) {
		if (obj.constructor.toString().indexOf("Array") == -1)
			return false;
		else
			return true;
	}	

	function validaMail(docId){
		mailValido = 1;
		
		if(isArray(docId))
			docIds = docId;
		else
			docIds = new Array(docId)

		for (i=0; i<docIds.length;i++ )
		{
			if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById(docIds[i]).value))) 
			{
				alert("E-mail Inválido");
				document.getElementById(docId[i]).focus();
				mailValido = 0;
				return false;
				break;
			} 
		}
	}
	function js_array_search( searchS, arraySA ) {
		 var I = 0;
		 var minI = 0;
		 var maxI = arraySA.length - 1;
		 //
		 var s = "";
		 //
		 var foundB = false;
		 //
		 I = minI - 1;
		 while ( ( I <= maxI ) && ( !( foundB ) ) ) {
		  I = I + 1;
		  s = arraySA[ I ];
		  foundB = ( searchS == s );
		  }
		 if ( foundB ) {
		  return( I );
		 }
		 else {
		  return(-1); // some negative number indicating not found
		 }
	}
	// Carrega Editor
	
	//MD5
	/*
	 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
	 * Digest Algorithm, as defined in RFC 1321.
	 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
	 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
	 * Distributed under the BSD License
	 * See http://pajhome.org.uk/crypt/md5 for more info.
	 */

	/*
	 * Configurable variables. You may need to tweak these to be compatible with
	 * the server-side, but the defaults work in most cases.
	 */
	var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
	var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
	var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

	/*
	 * These are the functions you'll usually want to call
	 * They take string arguments and return either hex or base-64 encoded strings
	 */
	function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
	function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
	function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
	function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
	function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
	function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

	/*
	 * Perform a simple self-test to see if the VM is working
	 */
	function md5_vm_test()
	{
	  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
	}

	/*
	 * Calculate the MD5 of an array of little-endian words, and a bit length
	 */
	function core_md5(x, len)
	{
	  /* append padding */
	  x[len >> 5] |= 0x80 << ((len) % 32);
	  x[(((len + 64) >>> 9) << 4) + 14] = len;

	  var a =  1732584193;
	  var b = -271733879;
	  var c = -1732584194;
	  var d =  271733878;

	  for(var i = 0; i < x.length; i += 16)
	  {
		var olda = a;
		var oldb = b;
		var oldc = c;
		var oldd = d;

		a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
		d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
		c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
		b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
		a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
		d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
		c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
		b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
		a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
		d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
		c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
		b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
		a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
		d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
		c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
		b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

		a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
		d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
		c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
		b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
		a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
		d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
		c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
		b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
		a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
		d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
		c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
		b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
		a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
		d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
		c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
		b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

		a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
		d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
		c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
		b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
		a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
		d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
		c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
		b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
		a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
		d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
		c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
		b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
		a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
		d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
		c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
		b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

		a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
		d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
		c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
		b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
		a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
		d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
		c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
		b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
		a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
		d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
		c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
		b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
		a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
		d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
		c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
		b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

		a = safe_add(a, olda);
		b = safe_add(b, oldb);
		c = safe_add(c, oldc);
		d = safe_add(d, oldd);
	  }
	  return Array(a, b, c, d);

	}

	/*
	 * These functions implement the four basic operations the algorithm uses.
	 */
	function md5_cmn(q, a, b, x, s, t)
	{
	  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
	}
	function md5_ff(a, b, c, d, x, s, t)
	{
	  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
	}
	function md5_gg(a, b, c, d, x, s, t)
	{
	  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
	}
	function md5_hh(a, b, c, d, x, s, t)
	{
	  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
	}
	function md5_ii(a, b, c, d, x, s, t)
	{
	  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
	}

	/*
	 * Calculate the HMAC-MD5, of a key and some data
	 */
	function core_hmac_md5(key, data)
	{
	  var bkey = str2binl(key);
	  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

	  var ipad = Array(16), opad = Array(16);
	  for(var i = 0; i < 16; i++)
	  {
		ipad[i] = bkey[i] ^ 0x36363636;
		opad[i] = bkey[i] ^ 0x5C5C5C5C;
	  }

	  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
	  return core_md5(opad.concat(hash), 512 + 128);
	}

	/*
	 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
	 * to work around bugs in some JS interpreters.
	 */
	function safe_add(x, y)
	{
	  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
	  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
	  return (msw << 16) | (lsw & 0xFFFF);
	}

	/*
	 * Bitwise rotate a 32-bit number to the left.
	 */
	function bit_rol(num, cnt)
	{
	  return (num << cnt) | (num >>> (32 - cnt));
	}

	/*
	 * Convert a string to an array of little-endian words
	 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
	 */
	function str2binl(str)
	{
	  var bin = Array();
	  var mask = (1 << chrsz) - 1;
	  for(var i = 0; i < str.length * chrsz; i += chrsz)
		bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
	  return bin;
	}

	/*
	 * Convert an array of little-endian words to a string
	 */
	function binl2str(bin)
	{
	  var str = "";
	  var mask = (1 << chrsz) - 1;
	  for(var i = 0; i < bin.length * 32; i += chrsz)
		str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
	  return str;
	}

	/*
	 * Convert an array of little-endian words to a hex string.
	 */
	function binl2hex(binarray)
	{
	  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
	  var str = "";
	  for(var i = 0; i < binarray.length * 4; i++)
	  {
		str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
			   hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
	  }
	  return str;
	}

	/*
	 * Convert an array of little-endian words to a base-64 string
	 */
	function binl2b64(binarray)
	{
	  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	  var str = "";
	  for(var i = 0; i < binarray.length * 4; i += 3)
	  {
		var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
					| (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
					|  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
		for(var j = 0; j < 4; j++)
		{
		  if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
		  else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
		}
	  }
	  return str;
	}
	//MD5

	// abre POP-UP
	function Abre(url, width, height, rola)	{
		if (rola==""){rola="yes"}
			if(!window.open(url,"_blank","resizable=no,toolbar=no,status=no,top=" + (window.screen.height - height)/2 + ",left=" + (window.screen.width - width)/2 + ",menubar=no,scrollbars=" + rola + ",width=" + width + ",height=" + height))
			{
				alert('Verifique se seu navegador está com bloqueador de pop-ups ativado!');
			}
	}
	// abre POP-UP

	/*função para abrir o flash sem dar erro*/
function montaFlash(destino, flavez, width, height, trans, scale){
	var myFlash = 
		'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" align="middle">'+
		'<param name="allowScriptAccess" value="sameDomain" />'+
		'<param name="movie" value="'+flavez+'" />' +
		'<param name="menu" value="false" />'
	;
	if (trans == "yes" || trans == 1){
		myFlash += '<param name="wmode" value="transparent" />';
	}
	if (scale == "yes"){
		myFlash += '<param name="scale" value="noscale" />';
	}
	myFlash +=
		'<param name="quality" value="high" />'+
		'<param name="bgcolor" value="#ffffff" />'+
		'<embed src="'+flavez+'" quality="high" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="empresas" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"';
	if (trans == "yes" || trans == 1)
		myFlash +='	wmode="transparent" ';
	
	myFlash +='	/>'+'</object>';

	document.getElementById(destino).innerHTML = myFlash;
}
/*função para abrir o flash sem dar erro*/

// MONTA OBJETO MEDIA PLAYER SEM ERROS

function montaMPlayer(docid, src, showcontrols, autostart, width, height)
{
	var showcontrols = 1 ? 'true' : 'false'; 
	var autostart	 = 1 ? 'true' : 'false';
	var myMP ='';
	myMP = '<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" id="player_'+docid+'"  width="'+width+'" height="'+height+'">\n';
	myMP += '	<param name="url" value="'+src+'" /> \n';
	myMP += '		<param name="src" value="'+src+'" /> \n';
	myMP += '	<param name="showcontrols" value="'+showcontrols+'" />\n'
	myMP += '	<param name="autostart" value="'+autostart+'" />\n'
	myMP += '  <!--[if !IE]>-->\n'
	myMP += '	<object type="video/x-ms-wmv" data="'+src+'" width="'+width+'" height="'+height+'">\n'
	myMP += '	<param name="src" value="'+src+'" />\n'
	myMP += '	<param name="autostart" value="'+autostart+'" />\n'
	myMP += '	<param name="controller" value="'+showcontrols+'" />\n'
	myMP += '	</object>\n'
	myMP += '   <!--<![endif]-->\n'    
	myMP += '</object>';

	document.getElementById(docid).innerHTML = myMP;
}
// MONTA OBJETO MEDIA PLAYER SEM ERROS

//script que carrega as subcategorias de uma categoria (do formulário frmCadCate.html)
function loadSubCate(idMae,numSubCate){
	if(numSubCate>0){		
		var div = document.getElementById("div"+idMae);
			
		if(div.style.display=="none"){
			div.style.display = "";			
		}else{
			div.style.display = "none";
		}			
	
		loadAjaxGet("index.php?page=galerias/categorias&act=carregaSubCate&idMae="+idMae, "div"+idMae);
	}
}

//script que carrega as subcategorias de uma categoria (do formulário frmListaCate.html)
function loadSubCate_lista(idMae,numSubCate){
	if(numSubCate>0){		
		var div = document.getElementById("div"+idMae);
			
		if(div.style.display=="none"){
			div.style.display = "";			
		}else{
			div.style.display = "none";
		}			
	
		loadAjaxGet("index.php?page=galerias/categorias&act=carregaSubCate_lista&idMae="+idMae, "div"+idMae);
	}
}

//valida o frmCadCate.html
function validaFrmCadCate(form){
	if(form.categoria.value==""){
		alert("Informe a categoria que deseja cadastrar!");
		form.categoria.focus();
		return false;
	}
	return true;
}

//confirmação de exclusão de cate frmListaCate.html
function confirmDelCate(){
	if(confirm('Atencao! Se essa categoria for deletada, todas galerias cadastras nela desaparecerao. Deseja continuar?')){		
		return true;
	}else{
		return false;
	}
}

function mostraAltSubCate(){
	document.getElementById("divCateAntiga").style.display = "none";
	document.getElementById("divCategorias").style.display = "";
	
	document.getElementById("cateMae_antiga").checked = false;
	document.getElementById("cateMae_nova").checked = true;
}
