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 checkClasseDoc(){
	if(window.document.getElementById("cr").checked)
		return checkDisableSearch(verifyChkBDoc());
	if(window.document.getElementById("dc").checked)
		return checkDisableSearch(verifyChkBDoc());
	if(window.document.getElementById("lg").checked)
		return checkDisableSearch(verifyChkBDoc());
	if(window.document.getElementById("ot").checked)
		return checkDisableSearch(verifyChkBDoc());
	window.document.getElementById("searcherSubmitButton").disabled = true;
	return false;
}

function checkDisableSearch(_whatToCheck){
	if(_whatToCheck)
		window.document.getElementById("searcherSubmitButton").disabled = false;
	return true;
}

function checkBD(myForm){
	if(window.document.getElementById("ln").checked)
		return checkDisableSearch(verifyChkClasses());
	if(window.document.getElementById("lc").checked)
		return checkDisableSearch(verifyChkClasses());
	if(window.document.getElementById("li").checked)
		return checkDisableSearch(verifyChkClasses());
	window.document.getElementById("searcherSubmitButton").disabled = true;
	return false;
}

function verify(myForm){
	var match;
	
	if ((match = myForm.searcher.value.match(/\s*\bdoutrina:s\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.dc.checked = true;}
	if ((match = myForm.searcher.value.match(/\s*\bdoutrina:n\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.dc.checked = false;}
	if ((match = myForm.searcher.value.match(/\s*\bjurisprudencia:s\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.cr.checked = true;}
	if ((match = myForm.searcher.value.match(/\s*\bjurisprudencia:n\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.cr.checked = false;}
	if ((match = myForm.searcher.value.match(/\s*\blegislacao:s\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.lg.checked = true;}
	if ((match = myForm.searcher.value.match(/\s*\blegislacao:n\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.lg.checked = false;}
	if ((match = myForm.searcher.value.match(/\s*\boutro:s\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.ot.checked = true;}
	if ((match = myForm.searcher.value.match(/\s*\boutro:n\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.ot.checked = false;}
	if ((match = myForm.searcher.value.match(/\s*\bnacional:s\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.ln.checked = true;}
	if ((match = myForm.searcher.value.match(/\s*\bnacional:n\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.ln.checked = false;}
	if ((match = myForm.searcher.value.match(/\s*\binternacional:s\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.li.checked = true;}
	if ((match = myForm.searcher.value.match(/\s*\binternacional:n\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.li.checked = false;}
	if ((match = myForm.searcher.value.match(/\s*\bcomunitaria:s\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.lc.checked = true;}
	if ((match = myForm.searcher.value.match(/\s*\bcomunitaria:n\b/))){myForm.searcher.value = myForm.searcher.value.replace(match[0], ""); myForm.lc.checked = false;}
	myForm.searcher.value = myForm.searcher.value.trim();
	
	if ((myForm.searcher.value.trim().length==0)||(verifyChkClasses(myForm)==false)){
		myForm.searcher.focus();
		return false;
	}
	if (verifyChkBDoc(myForm)==false){
		return false;
	}
	checkNumeroDiploma(window.document.getElementById("searcher"));
}

function checkNumeroDiploma(_container){
	var match = null;
	var returnObject = {numeroDiploma: []};	
	var tempArray = [];
	var stringText = _container.value.trim();

	tempArray.push({advSearchString: "numeroDiploma:", where: "numeroDiploma"});

	for(var i = 0; i < tempArray.length; i++){
		var index = stringText.indexOf(tempArray[i].advSearchString);
		while(index != -1){
			var temp = stringText.substring(index, stringText.length);
			temp = temp.replace(/^[ ]*/,"");
			if (temp.charAt(tempArray[i].advSearchString.length) == "\""){
				temp = temp.substring(tempArray[i].advSearchString.length+1, temp.length);
				index = temp.indexOf("\"");
				if (index != -1){
					var tempString = temp.substring(0, index);
					returnObject[tempArray[i].where].push({exact: true, string: tempString, forReplace: "\""+tempString+"\""});
				}
				else{
					index = temp.indexOf(" ");
					if(index == -1){
						var tempString = temp.substring(0, temp.length);
					}
					else{
						var tempString = temp.substring(0, index);
					}
					returnObject[tempArray[i].where].push({exact: false, string: "\""+tempString, forReplace: "\""+tempString});
				}
			}
		else{
			index = temp.indexOf(" ");
			if(index == -1){
				var tempString = temp.substring(tempArray[i].advSearchString.length, temp.length);
			}
			else{
				var tempString = temp.substring(tempArray[i].advSearchString.length, index);
			}
			tempString = tempString.trim();
			returnObject[tempArray[i].where].push({exact: false, string: tempString, forReplace: tempString});
		}
		stringText = stringText.replace(tempArray[i].advSearchString+returnObject[tempArray[i].where][returnObject[tempArray[i].where].length-1].forReplace, "");	
		index = stringText.indexOf(tempArray[i].advSearchString);
		}
	}
	if (returnObject.numeroDiploma.length != 0){
		temp = window.document.getElementById("lg");
		temp.check = 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{
		temp = window.document.getElementById("lg");
		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;
	}
}

function exportar(url){
	var iframeId = "exportIframe";
	var iframe = window.document.getElementById(iframeId);
	if (iframe == null){
		iframe = window.document.createElement("iframe");
		iframe.name = iframe.id = iframeId;
		iframe.height = 0;
		iframe.width = 0;
		iframe.style.display = "none";
		iframe.src = url;
		window.document.body.appendChild(iframe);
		return;
	}
	iframe.src = url;
	return;
}
