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 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.replace(/^\s+|\s+$/g,"").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 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 doiParser(unparsedDoi) {
	var parsedDoi = unparsedDoi.match(/10\.\S*/);
   
    if(parsedDoi != null)
        return parsedDoi;
    else
        return "";
}

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;
	}		
}	

function imageError(imageSrc) {
	imageSrc.src = "imagens/img_visualizador/tnLogo.png";
    imageSrc.onerror = "";
    
    return true;
}
