var $e = function(obj, type, fn){
		if (obj.addEventListener){
			obj.addEventListener(type, fn, false);
		} else if (obj.attachEvent) {
			var r = obj.attachEvent("on"+type, fn);
		} else {
			alert("Handler could not be attached");
		}
	};
	
function initializeTipoDocAutoComplete(){
	var input = window.document.getElementById("tipoDoc");
	var dataSource = new YAHOO.widget.DS_XHR("includes/autoCompleteTipoDoc.asp", ["\r\n"]);
	dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
	dataSource.maxCacheEntries = 60;
	dataSource.queryMatchSubset = true;
	var tipoDoc = new YAHOO.widget.AutoComplete("tipoDoc", "autoCompleteContainer", dataSource);
	tipoDoc.maxResultsDisplayed = 10; 
	tipoDoc.delimChar = null;
	
	if (BrowserDetect.browser == "Safari" || BrowserDetect.browser == "Opera" || BrowserDetect.browser == "Firefox"){
		input.style.top = "-2px";
		window.document.getElementById("numeroDiploma").style.width = "140px";
	}
	if (BrowserDetect.browser == "Firefox"){
		window.document.getElementById("autoCompleteContainer").style.top = "23px";
		window.document.getElementById("autoCompleteContainer").style.width = "357px";
	}
	if (BrowserDetect.browser == "Safari"){
		window.document.getElementById("autoCompleteContainer").style.top = "20px";
		window.document.getElementById("autoCompleteContainer").style.width = "359px";
	}
	if (BrowserDetect.browser == "Opera"){
		window.document.getElementById("autoCompleteContainer").style.top = "20px";
		window.document.getElementById("autoCompleteContainer").style.width = "359px";
	}
	
	return;
}

function initializeDescritoresAutoComplete(){
	var input = window.document.getElementById("allD");
	var dataSource = new YAHOO.widget.DS_XHR("includes/autoCompleteDescritores.asp", ["\r\n"]);
	dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
	dataSource.maxCacheEntries = 60;
	dataSource.queryMatchSubset = true;
	var descritor = new YAHOO.widget.AutoComplete("allD", "autoCompleteDescritoresContainer", dataSource);
	descritor.maxResultsDisplayed = 10; 
	descritor.delimChar = ",";

	if (BrowserDetect.browser == "Opera"){
		window.document.getElementById("allD").style.width = "357px";
	}
	if (BrowserDetect.browser == "Safari" || BrowserDetect.browser == "Opera"){
		window.document.getElementById("autoCompleteDescritoresContainer").style.width = "359px";
	}
	if (BrowserDetect.browser == "Firefox"){
		window.document.getElementById("autoCompleteDescritoresContainer").style.width = "357px";
	}
	
	// --- Guardar o valor antigo
	input.setAttribute("oldValue", input.getAttribute("value"));
	$e(input, "blur", function(){
		var descritor = window.document.getElementById("allD");
		if (descritor.getAttribute("oldValue") != descritor.value)
			descritor.value = descritor.value.replace(/, *$/, "");
		});
	return;
}


function initialize(){
	initializeTipoDocAutoComplete();
	initializeDescritoresAutoComplete();
}

function checkDates(startDate, endDate){
	var date_parts = startDate.match(/^(\d{1,2})[-](\d{1,2})[-](\d{4})$/);
	var year = date_parts[3];
	var month = date_parts[2];
	var day = date_parts[1];    
	var sDate = new Date(year, month-1, day);
	date_parts = endDate.match(/^(\d{1,2})[-](\d{1,2})[-](\d{4})$/);
	year = date_parts[3];
	month = date_parts[2];
	day = date_parts[1];   
	var eDate = new Date(year, month-1, day);
	if (sDate > eDate)
		return false;
	return true;
}

function isValidDate(date_string) {
	var days = [31,28,31,30,31,30,31,31,30,31,30,31];
	var year;
	var month;
	var day;
	var date_parts = null;
	var rtrn = true;
	date_parts = date_string.match(/^(\d{1,2})[-](\d{1,2})[-](\d{4})$/);
	if (date_parts) {
		year = date_parts[3];
		month = date_parts[2];
		day = date_parts[1];    
	}
	if (date_parts) {
		if (1 <= month && month <= 12) {
			if (month == 2) {
				if (year % 4 != 0 ? false : ( year % 100 != 0? true: (year % 1000 != 0? false : true))) {
					rtrn = (1 <= day && day <= 29);
				} else {
					rtrn = (1 <= day && day <= 28);
				}
			} else {
				rtrn = (1 <= day && day <= days[month -1]);
			}
		}else {
			rtrn = false;
		}
	} else {
		rtrn = false;
	}
	return rtrn;
}

function checkNumeroDiploma(_container){
	if(_container.value.trim() != ""){
		var temp = window.document.getElementById("doc");
		temp.checked = true;
		temp.readOnly = true;
		temp = window.document.getElementById("lg");
		temp.check = false;
		temp.readOnly = true;
		temp = window.document.getElementById("njr");
		temp.checked = false;
		temp.readOnly = true;
		temp = window.document.getElementById("cr");
		temp.checked = false;
		temp.readOnly = true;
		temp = window.document.getElementById("dc");
		temp.checked = false;
		temp.readOnly = true;
		temp = window.document.getElementById("ot");
		temp.checked = false;
		temp.readOnly = true;
	}
	else{
		var temp = window.document.getElementById("doc");
		temp.readOnly = false;
		temp = window.document.getElementById("lg");
		temp.readOnly = false;
		temp = window.document.getElementById("njr");
		temp.readOnly = false;
		temp = window.document.getElementById("cr");
		temp.readOnly = false;
		temp = window.document.getElementById("dc");
		temp.readOnly = false;
		temp = window.document.getElementById("ot");
		temp.readOnly = false;
	}
	return;
}
    
function verifyChkClasses(myForm){
	var cr = window.document.getElementById("cr");//jurisprudencia
	var dc = window.document.getElementById("dc");//doutrina
	var lg = window.document.getElementById("lg");//legislação
	var ot = window.document.getElementById("ot");//outra
	if((cr.checked==false)&&(dc.checked==false)&&(lg.checked==false)&&(ot.checked==false)){
		return false;
	}        
	return true;
}
    
function verifyChkBDoc(myForm){
	var ln = window.document.getElementById("ln");
	var lc = window.document.getElementById("lc");
	var li = window.document.getElementById("li");
	if ( (ln.checked==false)&&(lc.checked==false)&&(li.checked==false) ){
		return false;
	}
	return true;
}

function verifyDocsAndNotes(){
	var d = window.document.getElementById("doc");
	var n = window.document.getElementById("njr");
	if ( (d.checked==false)&&(n.checked==false) ){
		return false;
	}
	return true;
}

function verifyFormData(myForm){
	var allW = myForm.allW.value;
	var notW = myForm.notW.value;
	var allD = myForm.allD.value;
	var tipoDoc = myForm.tipoDoc.value
	var phr = myForm.phr.value;
	var dateE = myForm.date_end.value;
	var dateS = myForm.date_start.value;
	var pAntes = myForm.publicadoAntes.value;
	var pDepois = myForm.publicadoDepois.value;
	
	if (myForm.storych.checked==true){
		if (isValidDate(myForm.storyDate.value)==false){
			alert("Incluir versões históricas anteriores a:\nformato inválido da data");
			myForm.storyDate.focus();
			return false;
		}
	}
	
	if (myForm.datesD.checked==true){
		if ((isValidDate(dateS)==false) || (isValidDate(dateE)==false)){
			alert("Documentos disponibilizados no SIDDAMB entre:\nformato inválido das datas");
			return false;
		}
		if (!checkDates(dateS, dateE)){
			alert("Documentos disponibilizados no SIDDAMB entre:\na primeira data introduzida deve ser anterior à segunda.");
			return false;
		}	
	}
	
	if (myForm.publicadoEntre.checked==true){
		if ((isValidDate(pAntes)==false) || (isValidDate(pDepois)==false)){
			alert("Documentos publicados entre:\nformato inválido das datas");
			return false;
		}
		if (!checkDates(pDepois, pAntes)){
			alert("Documentos publicados entre:\na primeira data introduzida deve ser anterior à segunda.");
			return false;
		}	
	}
	
	checkNumeroDiploma(window.document.getElementById("numeroDiploma"));
	if(!checkDefaults()){
		window.location = "../default.asp";
		return false;
	}
	return true;
}

function checkSearchWhat(){
	if(window.document.getElementById("doc").checked)
		return checkDisableSearch(verifyChkBDoc() && verifyChkClasses());
	if(window.document.getElementById("njr").checked)
		return checkDisableSearch(verifyChkBDoc() && verifyChkClasses());
	window.document.getElementById("searcherSubmitButton").disabled = true;
	return false;
}

function checkBD(myForm){
	if(window.document.getElementById("ln").checked)
		return checkDisableSearch(checkSearchWhat() && verifyChkClasses());
	if(window.document.getElementById("lc").checked)
		return checkDisableSearch(checkSearchWhat() && verifyChkClasses());
	if(window.document.getElementById("li").checked)
		return checkDisableSearch(checkSearchWhat() && verifyChkClasses());
	window.document.getElementById("searcherSubmitButton").disabled = true;
	return false;
}

function checkClasseDoc(){
	if(window.document.getElementById("cr").checked)
		return checkDisableSearch(verifyDocsAndNotes() && verifyChkBDoc());
	if(window.document.getElementById("dc").checked)
		return checkDisableSearch(verifyDocsAndNotes() && verifyChkBDoc());
	if(window.document.getElementById("lg").checked)
		return checkDisableSearch(verifyDocsAndNotes() && verifyChkBDoc());
	if(window.document.getElementById("ot").checked)
		return checkDisableSearch(verifyDocsAndNotes() && verifyChkBDoc());
	window.document.getElementById("searcherSubmitButton").disabled = true;
	return false;
}

function checkDisableSearch(_whatToCheck){
	if(_whatToCheck)
		window.document.getElementById("searcherSubmitButton").disabled = false;
	return true;
}

function checkDefaults(){
	if (window.document.getElementById("allW").value.trim() != "") return true;
	if (window.document.getElementById("phr").value.trim() != "") return true;
	if (window.document.getElementById("allD").value.trim() != "") return true;
	if (window.document.getElementById("pf").value.trim() != "") return true;
	if (window.document.getElementById("tipoDoc").value.trim() != "") return true;
	if (window.document.getElementById("numeroDiploma").value.trim() != "") return true;
	if (window.document.getElementById("t").value.trim() != "") return true;
	if (window.document.getElementById("s").value.trim() != "") return true;
	if (window.document.getElementById("notW").value.trim() != "") return true;
	if (!window.document.getElementById("doc").checked) return true;
	if (window.document.getElementById("njr").checked != "") return true;
	if (!window.document.getElementById("cr").checked) return true;
	if (!window.document.getElementById("dc").checked) return true;
	if (!window.document.getElementById("lg").checked) return true;
	if (!window.document.getElementById("ot").checked) return true;
	if (!window.document.getElementById("ln").checked) return true;
	if (!window.document.getElementById("lc").checked) return true;
	if (!window.document.getElementById("li").checked) return true;
	if (window.document.getElementById("storych").checked) return true;
	if (window.document.getElementById("datesD").checked) return true;
	if (window.document.getElementById("publicadoEntre").checked) return true;
	var combo = window.document.getElementById("ordenarPor");
	if (combo.options[combo.selectedIndex].value != "dataPublicacao") return true;
	combo = window.document.getElementById("ordenarComo");
	if (combo.options[combo.selectedIndex].value != "desc") return true;
	return false;
}
