var msJobTitleSql = "";
var msLocationSql = "";
var msTypeSql = "";
var msSearchSql = "";
var mbExecuted = false;

function buildCriteriaSql(selectedValue, isChecked, fieldName, sqlString){
	var sSqlFragment = fieldName + " = '" + selectedValue + "' OR ";
	
	if(isChecked){
		//first check to see if the sqlfragment already exists in the sql string
		if(sqlString.indexOf(sSqlFragment) == -1){
			//fragment does not exist so add to the sql string
			sqlString = sqlString + sSqlFragment;
		}
	}else{
		//remove the sql criteria for the unselected option
		while(sqlString.indexOf(sSqlFragment) != -1){
			sqlString = sqlString.replace(sSqlFragment, "");				
		}		
	}	

	return sqlString;
}

function buildJobTitleSql(selectedValue, isChecked, setAll, saveValue){	
	mbExecuted = false;
	if(saveValue){				
		xajax_saveSelectedValue(selectedValue, isChecked);
	}
	
	showLoadingTicker();			
		
	if(setAll == true){		
		var colFields = document.getElementsByName("jobTitleCheckbox");
		for(iIndex = 0; iIndex < colFields.length; iIndex++){								
			if(colFields[iIndex].value == selectedValue){	
				colFields[iIndex].checked = isChecked;
			}
		}
	}	
	msJobTitleSql = buildCriteriaSql(selectedValue, isChecked, "job_attribute.AttValue", msJobTitleSql);
}

function buildLocationSql(selectedValue, isChecked, setAll, saveValue){	
	mbExecuted = false;	
	if(saveValue){
		xajax_saveSelectedValue(selectedValue, isChecked);
	}
	showLoadingTicker();	
	
	if(setAll == true){
		var colFields = document.getElementsByName("locationCheckbox");
		for(iIndex = 0; iIndex < colFields.length; iIndex++){								
			if(colFields[iIndex].value == selectedValue){
				colFields[iIndex].checked = isChecked;
			}
		}		
	}
	msLocationSql = buildCriteriaSql(selectedValue, isChecked, "jobs.Area", msLocationSql);
}

function buildCompleteSql(execute){		
	var bNoCriteria = true;		
	var sJobSector = document.getElementById("jobSector").value;
	var sCommonSql = "SELECT DISTINCT `jobs`.`JobTitle`, `jobs`.`Area`, `jobs`.`MinSal`, `jobs`.`MaxSal`, `jobs`.`JobId`, `jobs`.`PublishedJobDescription` FROM jobs INNER JOIN `job_attribute` ON `jobs`.`JobId` = `job_attribute`.`JobID`";
	var sSelectJobsSql = sCommonSql + " WHERE ";
	if(msJobTitleSql == ""){
		sSelectJobsSql += "(`job_attribute`.`AttValue` =  '" + sJobSector + "') AND ";
	}
	var sDefaultJobsSql = sCommonSql + " WHERE (`job_attribute`.`AttValue` =  '" + sJobSector + "')";
			
	//add job titles sql
	if(msJobTitleSql != ""){			
		sSelectJobsSql = sSelectJobsSql + " (" + msJobTitleSql + ")";
		sSelectJobsSql = sSelectJobsSql.substr(0, sSelectJobsSql.length - 4) + ")";
		bNoCriteria = false;
	}
	
	//add the location sql
	if(msLocationSql != ""){
		if(msJobTitleSql == ""){
			sSelectJobsSql = sDefaultJobsSql;
		}
		sSelectJobsSql = sSelectJobsSql + " AND (" + msLocationSql + ")";
		sSelectJobsSql = sSelectJobsSql.substr(0, sSelectJobsSql.length - 4) + ")";
		bNoCriteria = false;
	}
	
	if(bNoCriteria){
		//use the default sql to retrieve just jobs for the sector
		sSelectJobsSql = sDefaultJobsSql;
	}		
	
	if(msJobTitleSql == "" && msLocationSql == ""){		
		//sSelectJobsSql = sSelectJobsSql + " OR (`jobs`.`Published` =  'DO NOT DISPLAY RESULTS')";			
	}
	//alert(execute);
	if(execute){
		msSearchSql = sSelectJobsSql;
		//alert(sSelectJobsSql);
		if(!mbExecuted){
			executeSearch(sSelectJobsSql);		
		}
		mbExecuted = true;
	}
	
	hideLoadingTicker();
}

function selectAll(fieldType){	
	switch(fieldType){
		case "jobTitle":		
			var colFields = document.getElementsByName("jobTitleCheckbox[]");
			var oSelectAllSpan = document.getElementById("jobTitleSelectAll");
			
			var oSelectAllSpan2 = document.getElementsByName("jobTitleSelectAll")[1];
			break;
			
		case "location":
			var colFields = document.getElementsByName("locationCheckbox[]");
			var oSelectAllSpan = document.getElementById("locationSelectAll");
			
			var oSelectAllSpan2 = document.getElementsByName("locationSelectAll")[1];		
			break;	
	}	
	
	if(oSelectAllSpan.innerHTML == "(Select All)"){			
		//turn on all checkboxes				
		for(iIndex = 0; iIndex < colFields.length; iIndex++){								
			colFields[iIndex].checked = true;			
		}	
		//set the select all text
		oSelectAllSpan.innerHTML = "(Un-Select All)";
		if(oSelectAllSpan2){
			oSelectAllSpan2.innerHTML = "(Un-Select All)";
		}
	}else{
			
		//set the text
		oSelectAllSpan.innerHTML = "(Select All)"
		if(oSelectAllSpan2){
			oSelectAllSpan2.innerHTML = "(Select All)";
		}
		//turn off all checkboxes
		for(iIndex = 0; iIndex < colFields.length; iIndex++){
			colFields[iIndex].checked = false;							
		}			
	}			
}

function displayGlobalSearchResults(){				
	//display results		
	document.getElementById("contentCell").style.display = "none";
	if(document.getElementById("contentCell1")){
		document.getElementById("contentCell1").style.display = "none";
	}						
	if(document.getElementById("homePageImage")){
		document.getElementById("homePageImage").style.display = "none";
	}
	document.getElementById("refineSearchTop").style.display = "none";				
	document.getElementById("refineSearchBottom").style.display = "none";				
	document.getElementById("globalSearchResultsCell").style.display = "block";					
	document.getElementById("searchResultsPanel").style.display = "block";		
}