// Adds the appropriate event listeners to the object that determines which other elements/display objects need to be shown/hidden.
function HookJobType(pageType)
{
	if(pageType=="edit"){
		var elJobType = getEl("lstJobType"); 
		if(elJobType){
			if(elJobType.attachEvent){
				elJobType.attachEvent("onchange", HideShowJobTypeByVal); 
			}else if( elJobType.addEventListener ){
				elJobType.addEventListener("change", HideShowJobTypeByVal, false);
			}else{
				elJobType.onchange = HideShowJobTypeByVal;
			}
			//call now as well for page loading with existing values (edits)
			HideShowJobTypeByVal();
		}
	}else if(pageType=="view"){
		var elJobType = getEl("DDJobType")
		if(elJobType){
			HideShowJobTypeByDesc();
		}
	}else if(pageType=="results"){
		//only bother if we have a querystring otherwise nothing has been filtered
		if(location.search.length > 1){
			HideShowJobTypeByFilters();
		}
	}
	return;
}
// Get the JobType description (textual) which could be contained within HTML tags and other garbage
function HideShowJobTypeByDesc()
{
	// get the category description which may be contained within HTML tags (links to jobSearch etc) and remove spaces and html and lower case it
	var jobTypeDesc = getEl("DDJobType").innerHTML.replace(/<(.|\n)+?>/gi,"").replace(/\s+/g,"").toLowerCase();
	HideShowJobType(jobTypeDesc,"DD,DT","pre","re");	
	return;
}
// Get the jobType values (numeric) from the listbox
function HideShowJobTypeByVal()
{
	var jobTypeVal = getEl('lstJobType').value;
	HideShowJobType(jobTypeVal,"DD,DT","pre","re");	
	return;
}
// Hide Or Show the other fields depending on the value/description we have for JobType
function HideShowJobType(jobTypeVal,fixes,fixType,classMode)
{	
	ShowDebug("In HideShowJobType val = "+ jobTypeVal + " fixes = "+ fixes + " fixType = " + fixType + " classMode = " + classMode)
	switch (jobTypeVal)
	{
		case "26270": case "business":
			ToggleItem("Sector","hide",fixes,fixType,classMode);
			ToggleItem("Specialisation","hide",fixes,fixType,classMode);
			ToggleItem("IndustrySector","show",fixes,fixType,classMode);
			ToggleItem("PracticeType","hide",fixes,fixType,classMode);
			ToggleItem("PostQualification","hide",fixes,fixType,classMode);
			break;
		case "26272": case "notforprofit":
			ToggleItem("IndustrySector","hide",fixes,fixType,classMode);
			ToggleItem("Specialisation","hide",fixes,fixType,classMode);
			ToggleItem("Sector","show",fixes,fixType,classMode);
			ToggleItem("PracticeType","hide",fixes,fixType,classMode);
			ToggleItem("PostQualification","hide",fixes,fixType,classMode);
			break;	
		case "26271": case "practice": 
			//alert("found on 26271 OR practice")
			ToggleItem("Specialisation","show",fixes,fixType,classMode);
			ToggleItem("PracticeType","show",fixes,fixType,classMode);
			ToggleItem("PostQualification","show",fixes,fixType,classMode);
			ToggleItem("Sector","hide",fixes,fixType,classMode);
			ToggleItem("IndustrySector","hide",fixes,fixType,classMode);
			break;	
	}	
	return;
}


function HideShowJobTypeByFilters()
{
	var objQRY = {};
	var re = /[?&]([^=]+)(?:=([^&]*))?/g;
	var matchInfo, key;
	while(matchInfo = re.exec(location.search)){
		ShowDebug("store " + matchInfo[2] + " in key[" + matchInfo[1] + "] currently this hold = " + objQRY[matchInfo[1]]);
		key = decodeURIComponent(matchInfo[1]);
		if(objQRY[key]){
			val = decodeURIComponent(objQRY[key]);
			if(val!="" && val.length>0){
				objQRY[matchInfo[1]] = val + ","+matchInfo[2];
			}
		}else{
			objQRY[key] = decodeURIComponent(matchInfo[2]);
		}
	}
	// look for JobType params in query object and check values
	if(objQRY["JobType"]){
		var jobTypeVal = objQRY["JobType"];
		//need the numeric value from the value||desc param
		jobTypeVal = jobTypeVal.split("||")[0];
		HideShowJobType(jobTypeVal,"BrowseLinks","suf","ap");	
		return;
	}	
}

/*	Show/Hide item (mode = show/hide) by either replacing the existing class (classMode=re) or appending the appropriate class to whatevers there already (classMode=ap)
	Fixes = the list of elements to apply the class to (show/hide Element)	*/
function ToggleItem(name,mode,fixes,fixType,classMode)
{
	ShowDebug("In ToggleItem name="+name+",mode="+mode+",fixes="+fixes+",fixType="+fixType+",classMode="+classMode)

	var arrFix,x,el,name,className,elname;
	if(fixType==""){ fixType="pre";}
	if(fixes!=""&&fixes.length>0){
		arrFix = fixes.split(",");
	}else{
		arrFix = Array("");
	}
	for(x=0;x<arrFix.length;x++){
		elname="";
		if(fixType=="pre"){
			elname = arrFix[x]+name;
		}else if(fixType=="suf"){
			elname = name+arrFix[x];
		}
		className = (mode=="show") ? "ShowElement" : "HideElement";
		el = getEl(elname);
		
		if(el){ 
			if(classMode=="re"){
				//replace any class already there with this class
				el.className=className;
				ShowDebug("set " + elname + " to class = " + className);
			}else{
				//otherwise add to existing classes (non hide/show)
				if (className=="ShowElement"){
					var cA = "HideElement";
					var cB = "ShowElement";
				}else{
					var cA = "ShowElement";
					var cB = "HideElement";
				}
				ShowDebug("Swap class " + cA + " with " + cB);
				swapClass(el,cA,cB);
			}
		}else{ShowDebug("element does not exist = "+name+" so no action carried out");}
	}
	return;
}