$(document).ready(function(){
	$.getJSON("/base/category.js", function(json){
		// NOTE: the function below is included in categorylist.js. You must include JS file in order to make function work.
		renderCategoryDropDownlist (json, 'topicList', "Topic", $('#topicList').attr('defaultid'), 'All');
	});

	$("#TechWantedExt").hide();
	$("#TechAvailableExt").hide();
	$("#DealStructurePatent").hide();
	$("#PatentClass").hide();
	$("#DealStructureTech").hide();

	var name = $("#name"),
		allFields = $([]).add(name),
		tips = $("#validateTips");

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}
	
	function checkLength(o,n,min,max) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				return false;
			} else {
				return true;
			}
	}
	
	function checkRegexp(o,regexp,n) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}
	}
		
	$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 200,
			modal: true,
			buttons: {
				'Save Search': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					//bValid = bValid && checkLength(name,"username",3,16);
//					bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
					
					if (bValid) {
						document.formAdvSearch.searchName.value = name.val();
						$(this).dialog('close');
						
						$("#formAdvSearch").submit();
					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
	});
		
	//***************************************************
	// Submit form check
	//***************************************************
	$("#formAdvSearch").submit(function() {
		return checkForm ();
	});
	
	$("#search").click(function(e) {
	});
	
	// Used in search.php only
	$("#saveSearch").click(function(e) {
		e.preventDefault();
		document.formAdvSearch.redirect.value = 'myaccount.php?tn=ss';
		document.formAdvSearch.saction.value = 'updatess';
		
		$('#dialog').dialog('open');
	});

	// Used in savedsaerchedit.php only
	$("#updateSearch").click(function(e) {
		document.formAdvSearch.saction.value = 'updatess';
	});
	
	function checkForm () {
		//Initialise variables
		var errorMsg = "";
		var errorMsgLong = "";
		
		// Require at least one search text if it is normal user or broker, 
		if ($('#TynaxDirectDeal').length == 0) {
			if ($("#searchtext").val().length==0 
					&& $("#phrase").val().length==0
					&& $("#anywords").val().length==0
					&& $("#patentclass").val().length == 0
			) { 	
				errorMsg += "\n\tSearch Criteria \t- Define at least one search criteria to search";
			}
		}
		
		//Check for a security code
//		if ($("#secure-code").val().length != 6){
//			errorMsg += "\n\tSecurity Code \t- You must enter the security code";
//		}
		
		//If there is aproblem with the form then display an error
		if ((errorMsg != "") || (errorMsgLong != "")){
			msg = ""; //"_______________________________________________________________\n\n";
			msg += "The form has not been submitted because there are problem(s) with the form.\n";
			msg += "Please correct the problem(s) and re-submit the form.\n";
			msg += "_______________________________________________________________\n\n";
			msg += "The following field(s) need to be corrected: -\n";
			
			errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
			return false;
		}
		
		return true;
	}
	
	
	// preset dealstructure to all options
	DealOptions = document.formAdvSearch.DealStructureP;			
	for(i=1; i< DealOptions.length; i++)
	{
		DealOptions[i].checked = true;
	}	

	DealOptions = document.formAdvSearch.DealStructureT;			
	for(i=1; i< DealOptions.length; i++)
	{
		DealOptions[i].checked = true;
	}	
		
	$("input[id=DealStructureP]").click(function(e) {
		ChangeDealStructure (e);
	});
	$("input[id=DealStructureT]").click(function(e) {
		ChangeDealStructure (e);
	});
});


function ChangeSearchType(form, el) {
	if (el.value == "all") {
		$("#TechWantedExt").hide();
		$("#TechAvailableExt").hide();
	}
	else if (el.value == "tw") {
		$("#TechWantedExt").show();
		$("#TechAvailableExt").hide();
		form.SubTypeTechwanted[0].checked = true;
	}
	else if (el.value == "ta") {
		$("#TechWantedExt").hide();
		$("#TechAvailableExt").show();
//		document.getElementById ("SubTypeFeatured").className = "divShow";
		form.SubTypeFeatured[0].checked = true;
		$("#DealStructurePatent").hide();
//		$("#DealStructurePatent").hide();
		$("#DealStructureTech").hide();
	}
		
}

function ChangeFeaturedSubType (form, el) {
//	alert ("featured:"+ el.value);
	if (el.value == "-1") {
			$("#PatentClass").hide();
			$("#DealStructurePatent").hide();
			$("#DealStructureTech").hide();
	}
	else if (el.value == "1") {  //patent
			$("#DealStructurePatent").show();
			$("#PatentClass").show();
			$("#DealStructureTech").hide();
//			form.DealStructureP[0].checked = true;
			
	}
	else if (el.value == "0") {  // tech transfer
			$("#DealStructurePatent").hide();
			$("#PatentClass").hide();
			$("#DealStructureTech").show();
//			form.DealStructureT[0].checked = true;
			
	}
}

// used for checkbox style Dealstructure items.
function ChangeDealStructure (e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	
	if (tg.name == "DealStructureP[]") {
		DealOptions = tg.form.DealStructureP;			
		
	} else if (tg.name == "DealStructureT[]") {
		DealOptions = tg.form.DealStructureT;			
	}
	if (tg.value == 0)
	{
		for(i=1; i< DealOptions.length; i++)
		{
			DealOptions[i].checked = tg.checked;
		}	
	}
	else
	{
		var count = 0;
		for(i=1; i< DealOptions.length; i++)
		{
			if (DealOptions[i].checked) count ++;
		}
		
		if (count == (DealOptions.length-1)) 
		{  // all are selected
			DealOptions[0].checked = true;
		}
		else
		{
			DealOptions[0].checked = false;
		}			
	}
	
}