var searchQuery = false;

function selectSearch(theField){
	if(!searchQuery){
		theField.value = '';
	} else {
		theField.select();
	}
}

function startSearch(theField){
	searchQuery = true;
}

function resetSearch(theField){
	if(!searchQuery){
		theField.value = 'Enter Keyword';
	}else{
		if(theField.value==""){
			theField.value = 'Enter Keyword';	
		}
	}
}

function getSubCategory(theField){
	
	var theForm = theField.form;
	var theSubject = theForm.subject
	
	if(theSubject.value!="" && theSubject.value > 0){
		getRemoteCategory(theSubject.value);
	}else{
		$("div#searchSubCategory").slideUp("fast");
	}
}

function getRemoteCategory(cid){
	$.ajax({
	   type: "POST",
	   url: "/webservice/sub_categories.asp",
	   data: "cid=" + cid,
	   success: function(msg){
		showSubCategories(msg);
	   }
	 });	
}

function showSubCategories(msg){
	$("select#sid").html(msg);
	$("div#searchSubCategory").slideDown("slow", function(){
		$("select#sid").animate({backgroundColor:"#FFBFBF"}, 500).animate({backgroundColor:"#FFFFFF"}, 1000);
	});

}


function validate(theForm){
	$("div#searchAdvValidation").hide();
	if(theForm.qValue.value=="" || theForm.qValue.value=="Enter Keyword"){
		$("div#searchValidation").fadeIn("slow");
		return false;
	}
}

function validateAdvanced(theForm){
	$("div#searchValidation").hide();
	if(theForm.subject.value=="" && theForm.format.value=="" && theForm.hours.value==""){
		$("div#searchAdvValidation").html("Please select a search value");
		$("div#searchAdvValidation").fadeIn("slow");
		return false;
	}else{
		if(theForm.format.value == "0" && theForm.subject.value == ""){
			$("div#searchAdvValidation").html("Please select a subject");
			$("div#searchAdvValidation").fadeIn("slow");
			return false;
		}else if(theForm.hours.value!="" && theForm.format.value=="" && theForm.subject.value == ""){
			$("div#searchAdvValidation").html("Please select a format");
			$("div#searchAdvValidation").fadeIn("slow");
			return false;
		}	
	}
}

function setQType(theField) {
	
	var theForm = theField.form;
	var qType = theForm.qType;
	var qValue = theForm.qValue;

	if(theField.value!=""){
		
		if(theField.name=="subject"){
			if(theForm.format.value == "" || theForm.format.value <= 0){ //if the user has chosen a format, don't override
			
				qType.value = "cType";
				qValue.value = theField.value;
				
			}else{					
				qType.value = "ce";
				qValue.value = theForm.format.value;
			}
		}
		
		if(theField.name=="format" && theField.value > 0){
			qType.value = "ce";
			qValue.value = theField.value;
		}else{
			if(theField.name=="format" && theField.value == 0){
				qType.value = "cType";
				if(theForm.subject.value != ""){
					qValue.value = theForm.subject.value;
				}
			}else if(theField.name=="format" && theField.value < 0){
				if(theForm.subject.value == "" || theForm.subject.value == "-1"){
					qType.value = "all";
				}else{
					qType.value = "cType";
					qValue.value = theForm.subject.value;
				}
			}
		}
		
	}else{
	
		if(theField.name=="subject" && theForm.format.value != "" && theForm.format.value > 0){
			qType.value = "ce";
			qValue.value = theForm.format.value;
		}
		if(theField.name=="format" && theForm.subject.value != ""){
			qType.value = "cType";
			qValue.value = theForm.subject.value;
		}	
	
	}

}