function AbrePagina(Foto, tamx, tamy) {
	window.open(Foto,"","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=no,width=" + tamx + ",height=" + tamy)
}
function addFoto(galeria_id, id){
	var w = 400;
	var h = 300;
	var url = (id) ? "admin/galerias/fotos_crud/"+galeria_id+"/exibir/"+id : "admin/galerias/fotos_crud/"+galeria_id+"/criar/";
	var win = new Window({className: "alphacube", 
						  width:w, 
						  height:h, 
						  zIndex: 100, 
						  resizable: true, 
						  title: "Editar/Inserir Foto", 
						  draggable:true, 
						  wiredDrag: true, 
						  url: url,
						  destroyOnClose: true
						  }) 
	win.setConstraint(true, {left:0, right:20, top: 0, bottom:0});
	win.showCenter();
	return false;
}

function reloadIframe(galeria_id){
	//parent.fotos.location.href = "admin/galerias/fotos/"+galeria_id;
	parent.fotos.location.reload();
}

function checkAll(obj, form){
	var inputs = form.getElementsByTagName('input');
	var status = (obj.checked) ? 'checked' : false ;
	for(i=0; i<inputs.length; i++){
		if(inputs[i].type == 'checkbox' && inputs[i].name != obj.name){
			inputs[i].checked = status;
		}
	}
}

function showPage(id){
	var w = window.screen.availWidth-100;
	var h = window.screen.availHeight-250;
	var url = (id) ? "admin/conteudo/page/exibir/"+id : "admin/conteudo/page/criar/" ;
	var win = new Window({className: "alphacube", 
						  width:w, 
						  height:h, 
						  zIndex: 100, 
						  resizable: true, 
						  title: "Editar conte&uacute;do", 
						  draggable:true, 
						  wiredDrag: true, 
						  url: url,
						  destroyOnClose: true
						  }) 
	win.setConstraint(true, {left:0, right:20, top: 0, bottom:0});
	win.showCenter();
	return false;
}

function showText(id){
	var w = window.screen.availWidth-100;
	var h = window.screen.availHeight-250;
	var url = (id) ? "admin/texto/carrega/"+id : "admin/conteudo/page/criar/" ;
	var win = new Window({className: "alphacube", 
						  width:w, 
						  height:h, 
						  zIndex: 100, 
						  resizable: true, 
						  title: "Editar conte&uacute;do", 
						  draggable:true, 
						  wiredDrag: true, 
						  url: url,
						  destroyOnClose: true
						  }) 
	win.setConstraint(true, {left:0, right:20, top: 0, bottom:0});
	win.showCenter();
	return false;
}

/* Popula o combo de cidades de acordo com o combo de estado */
function ckbSubSecoes(id){
	if(id > 0 && !isNaN(id)){
		var url = 'admin/functions/populaSubSecoes/'+id;
		var pars = '';
		var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: montaCkbSubSecoes }); 
	} else {
		$('subsecao_id').options.length = 0;
		$('subsecao_id').selectedIndex = 0;
		$('subsecao_id').options[0] = new Option('Selecione a seção', '');
	}
}
function montaCkbSubSecoes(request){
	itens = request.responseXML.getElementsByTagName("subsecoes");
	itens = request.responseXML.getElementsByTagName("item");
	if(itens.length > 0){
		$('subsecao_id').options.length = 0;
	    $('subsecao_id').selectedIndex = 0;
		for(i=0; i<itens.length; i++){
			var nome = pegaValor(itens[i].getElementsByTagName('titulo')[0]);
			var id = pegaValor(itens[i].getElementsByTagName('id')[0]);
			if(id == '' || id == null || id == 'null'){
				id = '';
			}
			$('subsecao_id').options[i] = new Option(nome, id);
		}
	} else {
		$('subsecao_id').options.length = 0;
	    $('subsecao_id').selectedIndex = 0;
		$('subsecao_id').options[0] = new Option('Esta secao possui subsecoes', '');
	}
}

function pegaValor(no){
    if(no.childNodes.length>0){
            return no.firstChild.nodeValue; //Tem filho
      } else {
            try{
                  return no.nodeValue;
            } catch (e) {
            return "";
        }
      }
}


/** 
 * Funo que formata um campo do formulrio de 
 * acordo com a mscara informada... 
 *
 * Parmetros: 
 *  => objForm (o Objeto Form);
 *  => strField (string contendo o nome do textbox); 
 *  => sMask (mascara que define o formato que o dado ser apresentado, 
 *            usando o algarismo "9" para definir nmeros e o smbolo "!" para 
 *            qualquer caracter... 
 *  => evtKeyPress (evento);
 *
 * Uso..: <input type="textbox" name="xxx" 
 * onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);"> 
 * 
 * Observao: As mscaras podem ser representadas como os exemplos abaixo: 
 * CEP -> 99.999-999  
 * CPF -> 999.999.999-99 
 * RG -> 99.999.999-9
 * CNPJ -> 99.999.999/9999-99 
 * Data -> 99/99/9999 
 * Tel Resid -> (99) 999-9999 
 * Tel Cel -> (99) 9999-9999 
 * Processo -> 99.999999999/999-99 
 * Inscrio Estadual -> 999.999.999-9999
 * C/C -> 999999-! 
 * E por a vai... 
 **/
function format(field, sMask, evtKeyPress) {

   var i;
   var nCount;
   var sValue;
   var fldLen;
   var mskLen;
   var bolMask;
   var sCod;
   var nTecla;

   if(document.all) { // Internet Explorer
      nTecla = evtKeyPress.keyCode; 
   }
   else 
   if(document.layers) { // Nestcape
      nTecla = evtKeyPress.which;
   }

   sValue = field.value;

   // Limpa todos os caracteres de formatao que
   // j estiverem no campo.
   sValue = sValue.toString().replace( "-", "" );
   sValue = sValue.toString().replace( "-", "" );
   sValue = sValue.toString().replace( ".", "" );
   sValue = sValue.toString().replace( ".", "" );
   sValue = sValue.toString().replace( "/", "" );
   sValue = sValue.toString().replace( "/", "" );
   sValue = sValue.toString().replace( "(", "" );
   sValue = sValue.toString().replace( "(", "" );
   sValue = sValue.toString().replace( ")", "" );
   sValue = sValue.toString().replace( ")", "" );
   sValue = sValue.toString().replace( " ", "" );
   sValue = sValue.toString().replace( " ", "" );
  
   fldLen = sValue.length;
   mskLen = sMask.length;

   i = 0;
   nCount = 0;
   sCod = "";
   mskLen = fldLen;

   while (i <= mskLen) 
   {
       bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
       bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

       if (bolMask) 
	   {  sCod += sMask.charAt(i);
          mskLen++; 
	   }
       else {
         sCod += sValue.charAt(nCount);
         nCount++;
       }
       i++;
   }

   field.value = sCod;
   if (nTecla != 9 && nTecla != 8 && nTecla != 46)  {
       if (sMask.charAt(i-1) == "9")  // apenas nmeros...
	   {   return ((nTecla > 47) && (nTecla < 58)); // nmeros de 0 a 9
	   } 
       else { // qualquer caracter...
         return true;
       } 
   }
   else {
       return true;
   }
}

/**
 * 
 */
function soNumero(event){
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	var caract = new RegExp(/^[0-9]+$/i);
	var caract = caract.test(String.fromCharCode(keyCode));

    if (keyCode != 9 && keyCode != 8 && keyCode != 46) // backspace
	if(!caract){
		keyCode=0;
		return false;
	}
}

/**
 * 
 */
function mascaraMoeda(cur,len) {
	n='__0123456789';
	d=cur.value;
	l=d.length;
	r='';

	if (l > 0)
	{   z = d.substr(0,l-1);
		s = '';
		a = 2;

		for (i=0; i < l; i++)
		{
			c = d.charAt(i);
			if (n.indexOf(c) > a)
			{   a=1;
				s+=c;
			}
		}

		l = s.length;
		t = len-1;

		if (l > t)
		{   l = t;
			s = s.substr(0,t);
		}

		if (l > 2)
		{   r = s.substr(0,l-2)+','+s.substr(l-2,2);
		}
		else	
		{   if (l == 2)
			{	 r='0, '+s;
			}
			else
			{  if (l == 1)
				{   r = '0,0'+s;
				}
			}
		}

		if (r == '')
		{   r = '0,00';
		}
		else
		{   l = r.length;
			if (l > 6)
			{   j  = l%3;
				w  = r.substr(0,j);
				wa = r.substr(j,l-j-6);
				wb = r.substr(l-6,6);
				if (j > 0)
				{   w += '.';
				}
				k = (l-j)/3-2;

				for (i=0; i < k; i++)
				{	w += wa.substr(i*3,3)+'.';
				}

				r = w+wb;
			}
		}
	}

	if (r.length <= len)
	{   cur.value=r;
	}
	else
	{   cur.value=z;
	}

	return 'ok';
}

function iframeAutoHeight(quem,heu){
    //by Micox - elmicox.blogspot.com - elmicox.com - webly.com.br
    if(navigator.appName.indexOf("Internet Explorer")>-1){ //ie sucks
        var func_temp = function(){
            var val_temp = quem.contentWindow.document.body.scrollHeight + 5
            quem.style.height = val_temp + "px";
            //alert(val_temp)        
	}
        setTimeout(function() { func_temp() },100) //ie sucks
    }else{
        var val = quem.contentWindow.document.body.parentNode.offsetHeight + 5
        quem.style.height= val + "px";
    }
}