var win;

function popUpWindow(URL, width, height){ 
    wleft = (screen.width - width) / 2;
    wtop = (screen.height - height) / 2;
    if(win)
    	win.close();
   	win = window.open(URL, "","width=" + width + ",height=" + height + ",left=" + wleft + ",top=" + wtop + ", scrollbars=no, toolbar=no,resizable=no");
   	win.moveTo(wleft, wtop);
    win.focus();
} 

function popUpWindowWithSBandTB(URL, width, height){ 
    wleft = (screen.width - width) / 2;
    wtop = (screen.height - height) / 2;
    if(win)
    	win.close();
   	win = window.open(URL, "","width=" + width + ",height=" + height + ",left=" + wleft + ",top=" + wtop + ", scrollbars=yes, toolbar=yes,resizable=no");
   	win.moveTo(wleft, wtop);
    win.focus();
} 

function closeAllPopups() {
	if(win)
		win.close();
}

function getInternetExplorerVersion() {
    var rv = -1; 
   	var ua = navigator.userAgent;
    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if(re.exec(ua) != null)
    	rv = parseFloat(RegExp.$1);
    return rv;
}

function browserType() {
	var browser = navigator.appName;
	if(browser == "Netscape" || (browser == "Microsoft Internet Explorer" && getInternetExplorerVersion() == 8))
		return 1;
	else
		return 0;
}

function clapon(id, style, bt1, bt2, bt3, withBt){
    if(withBt == true) {
    	if(bt1 != null)
       		document.getElementById(bt1).className = 'visible';
       	if(bt2 != null)	
        	document.getElementById(bt2).className = 'visible';
        if(bt3 != null)
        	document.getElementById(bt3).className = 'visible';
    }
    document.getElementById(id).className = style; 
} 

function clapoff(id, style, bt1, bt2, bt3, withBt) {
    if(withBt == true) {
    	if(bt1 != null)
	    	document.getElementById(bt1).className = 'hide';
	    if(bt2 != null)	
        	document.getElementById(bt2).className = 'hide';
        if(bt3 != null)	
        	document.getElementById(bt3).className = 'hide';
    }
    document.getElementById(id).className = style;
}

/* validação de datas */
function verificaDataAno(ano) {
	// data actual
	var dtActual = new Date();
	
	if(ano > dtActual.getFullYear())
		return false;
	
	return true;	
}

/* para validação da informação */
function temValor(obj, obj_tipo) {
	if(obj_tipo == "TEXT" || obj_tipo == "TEXTAREA") {
	   if(obj.value.length == 0)
		  return false;
	   else if(document.all && document.all["_"+obj.name+"_editor"] && (obj.value == '<P>&nbsp;</P>'))
		  return false;
	   else
		  return true;
	} else if(obj_tipo == "SELECT") {
	   if(obj.type != "select-multiple" && obj.selectedIndex == 0)
		  return false;
	   else if(obj.type == "select-multiple" && obj.selectedIndex == -1)
	      return false;
	   else
	   	  return true;
	}
}

function erro(form_objecto, input_objecto, object_tipo, mensagem) {
	alert(mensagem);
	if(object_tipo == "SELECT") {
		if(input_objecto[0])
		  input_objecto[0].focus();
		else
		  input_objecto.focus();
	} else if(!(document.all && document.all["_"+input_objecto.name+"_editor"])) {
	   input_objecto.focus();
	}   
	
	if(object_tipo == "TEXT" || object_tipo == "TEXTAREA") {
	   if(!(document.all && document.all["_"+input_objecto.name+"_editor"]))
	       input_objecto.select();
	}
	
	return false;
}

function limiteCampo(field, maxlimit) {
	if(field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function checkBoxSelectAll(obj, value) {
	if(obj != null) {
		if(value==true) {
			if(obj.length > 0) {
				for(var i=0; i < obj.length; i++) {
					obj[i].checked = true;
				}
			} else {
				obj.checked = true;
			}	
		} else {
			if(obj.length > 0) {
				for(var i=0; i < obj.length; i++) {
					obj[i].checked = false;
				}
			} else {
				obj.checked = false;
			}	
		}
	}
}

function checkBoxUnselectMaster(obj, master1, master2) {
	var objLength = obj.length;
	var checkCount = 0;
	if(obj != null) {
		for(var i=0; i < obj.length; i++) {
			if(obj[i].checked)
				checkCount++;
		}
	}
	if(objLength - 2 == checkCount) {
		master1.checked = true;
		master2.checked = true;
	} else {
		master1.checked = false;
		master2.checked = false;
	}		
}	