function checkposfield(campo,bgc) {
  if (!bgc || bgc=="") bgc = fieldcolor; // from common
  if (!ereg(campo.value,"^([0-9]+,[0-9]+)?$")){
    campo.style.background= invalidcolor;
    return false;
  } else {
    campo.style.background=bgc;
    return true;
  }
}
function config_testmail() {
	mail = prompt("Digite o mail de destino");
	if (mail) {
		ajax = new Ajax.Request("configmailtest.ajax?mail=" + mail,
													 { asynchronous:true,
						                              onComplete:parseajax
						                            }
               				      );
	}
}
function startsearch(field,mod) {
	if (opened_search_divs[field]) {
		dragwindows.close(opened_search_divs[field]); // new one
	}
	id = openList('list.ajax?module='+mod+'&action=fill&action_field='+field,'Procurar',false);
	opened_search_divs[field] = id;
}
function openList(url,title,resizable,insideThisDIV,paging,whnd) {
	if (!paging)
		paging = 17;
	hasParameters = url.indexOf("?") != -1;
	if (!insideThisDIV) {
		var arrayPageScroll = document.viewport.getScrollOffsets(); // Prototype
		id = dragwindows.writewindow(false,32+arrayPageScroll[1]+(dragwindows.dgwc*10),600,420,'dgwparent',!resizable,false,setcontentsize);
		$('dgw_'+id).innerHTML = "<div style='height:18px;' id='dgwt_"+id+"'>&nbsp;"+title+"</div><div id='dgwc_"+id+"'>Loading...</div>";
		if (resizable) $('dgwc_'+id).style.overflow='auto';
		new Ajax.Updater('dgwc_'+id,url+(hasParameters?'&':'?')+'p_fim='+paging+'&cellwidth=70&whnd='+id,{asynchronous:true,
														   onComplete:parseajax});
		return id;
	} else {
		new Ajax.Updater(insideThisDIV,url+(hasParameters?'&':'?')+'p_fim='+paging+'&whnd='+whnd,{asynchronous:true,
														   onComplete:parseajax});
		if (whnd >= dragwindows.dgwc)
			dragwindows.dgwc = whnd+1;
		return whnd;
	}

}
function openOutput(url,title,resizable,Hport,height) {
	if (!Hport) Hport = 0;
	if (!height) height = 400;
	dragwindows.Cdragarea = Hport;
	var arrayPageScroll = document.viewport.getScrollOffsets(); // Prototype
	id = dragwindows.writewindow(false,32+arrayPageScroll[1]+(dragwindows.dgwc*10),560,height,'dgwparent',!resizable,false,setcontentsize);
	$('dgw_'+id).innerHTML = "<div style='height:18px;' id='dgwt_"+id+"'>"+title+"</div><div id='dgwc_"+id+"'>Loading...</div>";
	if (resizable) $('dgwc_'+id).style.overflow='auto';
	hasParameters = url.indexOf("?") != -1;
	new Ajax.Updater('dgwc_'+id,url+(hasParameters?'&':'?')+'wnhd='+id,{asynchronous:true
													  });
	dragwindows.Cdragarea = 0;
	return id;

}
function setcontentsize(hnd,id,mode) {
	width = parseInt($('dgw_'+id).style.width);
	height = parseInt($('dgw_'+id).style.height);
	try {
		$('dgwc_'+id).style.width = width+"px";
		$('dgwc_'+id).style.height = (height-18)+"px";
	} catch (e) {
	}

}
function detectfield(field,module) {
	if ($(field)) return $(field);
	else if ($(module+"_"+field)) return $(module+"_"+field);
	else return false;
}
function deleteme(module,skeys) {
	if (confirm("Deletar este item ("+module+")?")) {
  		document.location = 'delete.html?module='+module+'&'+skeys;
  	}
}
function setloadingmode() {
	Element.setOpacity('dgwparent',0.5);
	$('loadingmode_div').style.left = Math.floor(( document.documentElement.clientWidth/2)-16)+"px";
	$('loadingmode_div').style.top = Math.floor(( document.documentElement.clientHeight/2)-16)+"px";
	$('loadingmode_div').style.display = '';
}
function dgstart() {
	dragwindows.start();
	dragwindows.CeffectBlind = false;
	if ($('debugerrordiv')) setTimeout('fadedebug()',5000);
}
function fadedebug(){
	if ($('debugerrordiv') && $('debugerrordiv').style.display != 'none')
		new Effect.Fade($('debugerrordiv'));
}
function mocb(thediv) {
	thediv.style.zIndex = dragwindows.zorder++;
}
function editaction(field,keys) {
	obj = $(field);
	l = obj.options.length;
	for (c=0;c<l;c++) {
		if (obj.options[c].selected && obj.options[c].value != keys)
			obj.options[c].selected = false;
		else if (obj.options[c].value == keys)
			obj.options[c].selected = true;
	}

	if (opened_search_divs[field]) {
		dragwindows.close(opened_search_divs[field]);
	}
}
function startpreview(skeys,module,isUndo) {
	dragwindows.CdragareaW = 785;
	var arrayPageScroll = document.viewport.getScrollOffsets(); // Prototype
	id = dragwindows.writewindow(false,32+arrayPageScroll[1]+(dragwindows.dgwc*10),800,420,'dgwparent',true,false,setcontentsize);
	dragwindows.CdragareaW = 0;
	$('dgw_'+id).innerHTML = "<div style='height:18px;' id='dgwt_"+id+"'></div><div id='dgwc_"+id+"'>Loading...</div>";
	$('dgwc_'+id).style.overflow='auto';
	url = isUndo?'undosee.ajax':'preview.ajax';
	new Ajax.Updater('dgwc_'+id,url+'?module='+module+'&'+skeys+'&whnd='+id,{asynchronous:true,
													   onComplete:parseajax});
	return id;
}
function showHistory(module,skeys) {
	dragwindows.CdragareaW = 785;
	var arrayPageScroll = document.viewport.getScrollOffsets(); // Prototype
	id = dragwindows.writewindow(false,32+arrayPageScroll[1]+(dragwindows.dgwc*10),800,420,'dgwparent',true,false,setcontentsize);
	dragwindows.CdragareaW = 0;
	$('dgw_'+id).innerHTML = "<div style='height:16px;'></div><div id='dgwc_"+id+"'>Loading...</div>";
	$('dgwc_'+id).style.overflow='auto';
	new Ajax.Updater('dgwc_'+id,'history.ajax?module='+module+'&'+skeys+'&whnd='+id,{asynchronous:true,
													   onComplete:parseajax});
	return id;
}
function colapptibleFRAME(dir,fulldir,chields) {
    if (dir == "") return false; // evita loop infinito em caso de erro
    if (chields == 0) {
    	if (fulldir.substr(0,8)=='list.php') {
			id = openList(str_replace('.php','.ajax',fulldir,1),'List',false)
    	} else {
	    	setloadingmode();
			document.location = fulldir;
		}
		return true;
    }
    if (!$("dir"+dir)) {
		return true;
    }
    if ($("dir"+dir).style.display == '' || $("dir"+dir).style.display == 'block') { // is visible
		new Effect.BlindUp("dir"+dir);
    } else {
		new Effect.BlindDown("dir"+dir);
    }
    return true;
}
function gofilter(query,whnd) {
	if (whnd && whnd != "" && whnd != 0)
		new Ajax.Updater('dgwc_'+whnd,"list.ajax?"+query,{asynchronous:true,
													   onComplete:parseajax});
	else
		document.location = "list.php?"+query;
}
function selectall(frm,para,filter) {
  for (var i=0;i<frm.elements.length;i++) {
    if ((!filter && frm.elements[i].id.substring(0,2) == "id") || (filter && frm.elements[i].id.indexOf(filter) != -1)) {
      if (!frm.elements[i].disabled) frm.elements[i].checked = para;
    }
  }
}