﻿// AGL 4/10/09
// This is a method override of core jobsearch.js and should be used together;
// Method copied from core jobsearch.js
// Lid removal is commented out as it breaks basic search.

JsMain.DoSearch = function() {
	var curQuery = JsMain.CurrentQueryString();
	//alert(curQuery.QueryStringSerialize())

	// start on page 1 always
	curQuery.Set(JsConst.QS_PAGENUMBER, 1);
	// we'll sort by rel if keywords or cityname is present
	//var bSortByRel = false;
	// get the keywords
	//var bKw = false;

	if (typeof JsMain.Inputs[JsConst.INPUT_KEYWORDS] !== "undefined") {
		var kw = JsUtil.Trim(JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_KEYWORDS]));
		var prompt = JsUtil.Trim(JsMain.GetPrompt(JsConst.INPUT_KEYWORDS));
		if (kw.length > 0 && kw != prompt) {
			curQuery.Set(JsConst.QS_KEYWORDS, kw);
			//bSortByRel = true;
			//bKw = true;

			// check JTO if any
			if (typeof JsMain.Inputs[JsConst.INPUT_JTO] !== "undefined") {
				var jto = JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_JTO]);
				if (jto) {
					curQuery.Set(JsConst.QS_JTO, "1");
				}
				else
					curQuery.Clear(JsConst.QS_JTO);
			}

			// check Exact Phrase if any
			if (typeof JsMain.Inputs[JsConst.INPUT_EXACT_PHRASE] !== "undefined") {
				var thisPhrase = JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_EXACT_PHRASE]);
				if (thisPhrase) {
					curQuery.Set(JsConst.QS_QUERYTYPE, JsConst.QUERYTYPE_PHRASE);
				}
				else
					curQuery.Clear(JsConst.QS_QUERYTYPE);
			}
		} else {

			curQuery.Clear(JsConst.QS_KEYWORDS);
		}

	} else {
		//alert('no keywords control!');
	}

	// Handle Exclude keywords
	if (typeof JsMain.Inputs[JsConst.INPUT_EXCLUDE_KEYWORDS] !== "undefined") {
		var xkw = JsUtil.Trim(JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_EXCLUDE_KEYWORDS]));
		var prompt = JsUtil.Trim(JsMain.GetPrompt(JsConst.INPUT_EXCLUDE_KEYWORDS));
		if (xkw.length > 0 && xkw != prompt) {
			curQuery.Set(JsConst.QS_EXCLUDE_KEYWORDS, xkw);
		} else {
			curQuery.Clear(JsConst.QS_EXCLUDE_KEYWORDS);
		}
	}

	// amarculescu: Get any category data
	if (typeof JsMain.Inputs[JsConst.INPUT_CATEGORIES] !== "undefined") {
		var categoryIds = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_CATEGORIES]);

		if (categoryIds.length > 0) {
			// clear the occ values if the user refines by category
			curQuery.Clear(JsConst.QS_OCCUPATION_ID);
			// add a multi entry containing the array			
			curQuery.SetMulti(JsConst.QS_CATEGORY_ID, categoryIds);
		}
		else {
			curQuery.Clear(JsConst.QS_CATEGORY_ID);
		}
		// clear the "fn_values" parameter, we only support it for the initial loading of the page
		// the first time the user refines the search, it is converted into "fn"
		curQuery.Clear(JsConst.QS_OCCUPATION_ID_OLD);
	}

	// amarculescu: Get any occupation data
	if (typeof JsMain.Inputs[JsConst.INPUT_OCCUPATIONS] !== "undefined") {
		var occupationIds = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_OCCUPATIONS]);

		if (occupationIds.length > 0) {
			// clear the fn values if the user refines by occupation
			curQuery.Clear(JsConst.QS_CATEGORY_ID);
			// add a multi entry containing the array			
			curQuery.SetMulti(JsConst.QS_OCCUPATION_ID, occupationIds);
		}
		else {
			curQuery.Clear(JsConst.QS_OCCUPATION_ID);
		}
		// clear the "fn_values" parameter, we only support it for the initial loading of the page
		// the first time the user refines the search, it is converted into "occ"
		curQuery.Clear(JsConst.QS_OCCUPATION_ID_OLD);
	}

	// amarculescu: Get any industry data
	if (typeof JsMain.Inputs[JsConst.INPUT_INDUSTRIES] !== "undefined") {
		var industryIds = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_INDUSTRIES]);

		if (industryIds.length > 0) {
			// add a multi entry containing the array			
			curQuery.SetMulti(JsConst.QS_INDUSTRY_ID, industryIds);
		}
		else {
			curQuery.Clear(JsConst.QS_INDUSTRY_ID);
		}
		// clear the "indid_values" parameter, we only support it for the initial loading of the page
		// the first time the user refines the search, it is converted into "indid"
		curQuery.Clear(JsConst.QS_INDUSTRY_ID_OLD);
	}

	if (typeof JsMain.Inputs[JsConst.INPUT_JOBTYPES] !== "undefined") {
		var jobTypes = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_JOBTYPES]);

		if (jobTypes.length > 0) {
			// add a multi entry containing the array			
			curQuery.SetMulti(JsConst.QS_JOBTYPE, jobTypes);
		}
		else {
			curQuery.Clear(JsConst.QS_JOBTYPE);
		}
		// may need to handle/clear the "job_status" parameter???
		//curQuery.Clear(JsConst.QS_INDUSTRY_ID_OLD);
	}

	var bZip = false;
	var bCity = false;
	var bState = false;
	var bCountry = false;
	var bContinent = false;
	var bLidSelected = false;

	var bLocationCriteriaSet = false;
	var selectedTab = null;
	var SELECTED_CITY = '1';
	var SELECTED_ZIP = '2';
	var SELECTED_LOCATIONS = '3';
	var SELECTED_CONTINENT = '4';
	var SELECTED_INTERNTNL = '5';

	//If selectedTab == null, assumes channel is not using client-side
	//toggling of location views
	if (JsMain.Inputs[JsConst.INPUT_SELECTED_SEARCH_TAB] != null) {
		selectedTab = JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_SELECTED_SEARCH_TAB]);
	}

	//Order of precedence for setting location search criteria: location, zip, city/stateINPUT_LOCATION_CNTNT : "locationcontinent",
	var locationWidgetValues = null;
	if ((JsMain.Inputs[JsConst.INPUT_LOCATIONS] != null || JsMain.Inputs[JsConst.INPUT_LOCATION_CNTNT] != null || JsMain.Inputs[JsConst.INPUT_LOCATION_INTNL] != null)
		&& (selectedTab == SELECTED_LOCATIONS || selectedTab == SELECTED_CONTINENT || selectedTab == SELECTED_INTERNTNL || selectedTab == null)) {
		if (selectedTab == SELECTED_LOCATIONS || selectedTab == null) {
			if (JsMain.Inputs[JsConst.INPUT_LOCATIONS] != null) {
				locationWidgetValues = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_LOCATIONS]);
				bCountry = true;
				curQuery.Set(JsConst.QS_COUNTRY, JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_CNTRY]));
			}
		}
		else if (selectedTab == SELECTED_CONTINENT) {
			if (JsMain.Inputs[JsConst.INPUT_LOCATION_CNTNT] != null) {
				locationWidgetValues = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_LOCATION_CNTNT]);
				bContinent = true;
				curQuery.Set(JsConst.QS_CONTINENT, JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_CNTNT]));
			}
		}
		else if (selectedTab == SELECTED_INTERNTNL) {
			if (JsMain.Inputs[JsConst.INPUT_LOCATION_INTNL] != null) {
				locationWidgetValues = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_LOCATION_INTNL]);
				bContinent = true;
				curQuery.Set(JsConst.QS_CONTINENT, "AF,AS,AU,CA,EU,ME,NA,SA");
			}
		}
		// cope with hardcoded locations for UK channels
		// hash that prevents us from duplicate IDs when a single location is mapped to multiple IDs
		var locationIDs = new Array();
		// array that enables us to tell to the Search page which hardcoded location have been selected
		// Note: this is necessary for refinements of locations mapped to multiple IDs, because these
		// locations cannot be infered from the lid query string parameter.
		var locationNameHashes = new Array();

		for (var i = 0; i < locationWidgetValues.length; i++) {
			var itemValue = locationWidgetValues[i];

			if (itemValue.indexOf(';') < 0) {
				// this is a standard location id
				// to make the array items unique, check for a previous entry
				if (locationIDs[itemValue] == null) {
					locationIDs[itemValue] = itemValue;
				}
			}
			else {
				// this is a location mapped to several location IDs, we need to parse it
				var hashLength = itemValue.indexOf('_');

				var locationNameHash = itemValue.substring(0, hashLength);
				// and the multiple coma separated IDs
				var strLocationIDs = itemValue.substring(hashLength + 1);

				// first collect the multiple ids
				var singleLocationIDs = strLocationIDs.split(';');
				for (var j = 0; j < singleLocationIDs.length; j++) {
					var singleLocationID = singleLocationIDs[j];
					if (locationIDs[singleLocationID] == null) {
						locationIDs[singleLocationID] = singleLocationID;
					}
				}

				// now record the originating location name
				// also prevent duplicating
				if (locationNameHashes[locationNameHash] == null) {
					locationNameHashes.push(locationNameHash);
				}
			}
		}


		// Convert the locationIDs hash to an index based array that can be used in curQuery.SetMulti
		var locationIDsValues = new Array();
		for (var key in locationIDs) {
			locationIDsValues.push(locationIDs[key]);
		}

		if (locationIDsValues.length > 0) {
			// add a multi entry containing the array	
			curQuery.SetMulti(JsConst.QS_LID, locationIDsValues);
			bLidSelected = true;
			bLocationCriteriaSet = true;
		}

// AGL
//		else {
//			curQuery.Clear(JsConst.QS_LID);
//		}


		// clear the "lid_values" parameter, we only support it for the initial loading of the page
		// the first time the user refines the search, it is converted into "lid"
		curQuery.Clear(JsConst.QS_LID_OLD);

		// HACK for the channels using hardcoded values with multiple IDs
		// pass the location name hashes to the QS, if any, so that the Search page can perform the refinement
		if (locationNameHashes.length > 0) {
			curQuery.SetMulti(JsConst.QS_LNHASH, locationNameHashes);
		}
		else {
			curQuery.Clear(JsConst.QS_LNHASH);
		}
	}

	//If location not found, get zip
	if (!bLocationCriteriaSet && JsMain.Inputs[JsConst.INPUT_ZIP] != null && (selectedTab == SELECTED_ZIP || selectedTab == null)) {
		// get the zip
		//JPF March '07 Ticket  DEV001768778
		var z = "";
		// Make sure zip is visible before using its value
		if (JsMain.IsVisible(JsMain.Inputs[JsConst.INPUT_ZIP])) {
			z = JsUtil.Trim(JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_ZIP]));
		}
		if (z.length > 0 && z != JsUtil.Trim(JsMain.GetPrompt(JsConst.INPUT_ZIP))) {
			curQuery.Set(JsConst.QS_ZIP, z);
			bZip = true;
			bLocationCriteriaSet = true;
		}
		else {
			curQuery.Clear(JsConst.QS_ZIP);
		}
	}

	//If location or zip not found, get city/state	
	if (!bLocationCriteriaSet && (selectedTab == SELECTED_CITY || selectedTab == null)) {
		// get the cityname
		if (JsMain.Inputs[JsConst.INPUT_CITY] != null) {
			var c = "";
			// Make sure cityname is visible before using its value
			if (JsMain.IsVisible(JsMain.Inputs[JsConst.INPUT_CITY])) {
				c = JsUtil.Trim(JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_CITY]));
			}
			var prompt = JsUtil.Trim(JsMain.GetPrompt(JsConst.INPUT_CITY));
			if (c.length > 0 && c != prompt) {
				curQuery.Set(JsConst.QS_CITY, c);
				//bSortByRel = true;
				bCity = true;
				bLocationCriteriaSet = true;
			} else {
				curQuery.Clear(JsConst.QS_CITY);
			}
		}
		// get the stateID
		if (typeof JsMain.Inputs[JsConst.INPUT_STATE] !== "undefined") {
			var sid = 0;
			// Make sure stateID is visible before using its value
			if (JsMain.IsVisible(JsMain.Inputs[JsConst.INPUT_STATE])) {
				sid = JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_STATE]);
			}
			if ((sid - 0) > 0) {
				curQuery.Set(JsConst.QS_STATEID, sid);
				bState = true;
				bLocationCriteriaSet = true;
			}
			else
				curQuery.Clear(JsConst.QS_STATEID);
		}
		if (typeof JsMain.Inputs[JsConst.INPUT_EUROSTATE] !== "undefined") {
			var sid = 0;
			if (JsMain.Inputs[JsConst.INPUT_EUROSTATE] !== null) {
				sid = JsUtil.GetObjectValue(JsMain.Inputs[JsConst.INPUT_EUROSTATE]);
			}
			if ((sid - 0) > 0) {
				curQuery.Set(JsConst.QS_STATEID, sid);
				bState = true;
				bLocationCriteriaSet = true;
			}
			else
				curQuery.Clear(JsConst.QS_STATEID);
		}

	}

	// here we clear current query, because it contains data from previous request
	if (!bZip) {
		curQuery.Clear(JsConst.QS_ZIP);
	}

	if (!bCity) {
		curQuery.Clear(JsConst.QS_CITY);
	}

	if (!bState) {
		curQuery.Clear(JsConst.QS_STATEID);
	}

// AGL
//	if (!bLidSelected) {
//		curQuery.Clear(JsConst.QS_LID);
//	}

	if (!bCountry) {
		curQuery.Clear(JsConst.QS_COUNTRY);
	}

	if (!bContinent) {
		curQuery.Clear(JsConst.QS_CONTINENT);
	}

	if (bState || bCity || bZip) {
		curQuery.Clear(JsConst.QS_LOCGENRE);
	}


	//Find Radius Drop Down Control and make sure it is visible before using its value
	var bRadius = false;
	if (typeof JsMain.Inputs[JsConst.INPUT_RADIUS] !== "undefined" &&
			JsMain.Inputs[JsConst.INPUT_RADIUS].style.display !== "none" && (bCity || bZip)) {
		var r = JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_RADIUS]);
		// be sure radius was selected
		if (r < 1) r = JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_DEFAULT_RADIUS]);
		curQuery.Set(JsConst.QS_RADIUS, r);
		var ru = JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_RADIUS_UNITS]);
		if (ru != null & ru.length > 0) curQuery.Set(JsConst.QS_RADIUS_UNITS, ru);
		bRadius = true;
	}
	else {
		//alert('No Radius Drop Down Found;\')
		curQuery.Clear(JsConst.QS_RADIUS);
	}

	// Get the company name
	if (typeof JsMain.Inputs[JsConst.INPUT_COMPANY_NAME] !== "undefined") {
		var coName = JsUtil.Trim(JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_COMPANY_NAME]));
		var prompt = JsUtil.Trim(JsMain.GetPrompt(JsConst.INPUT_COMPANY_NAME));
		if (coName.length > 0 && coName != prompt) {
			curQuery.Set(JsConst.QS_COMPANY_NAME, coName);
		} else {
			curQuery.Clear(JsConst.QS_COMPANY_NAME);
		}
	}


	// if any of city,state,or zip was used, clear out lid=
	if (bCity || bState || bZip) {
		curQuery.Clear(JsConst.QS_LID);
	}

	// Set NationWide Jobs lid=42886
//	if (typeof JsMain.Inputs[JsConst.INPUT_NATIONWIDELID] !== "undefined") {
//		var jclid = JsUtil.Trim(JsUtil.GetElementValue(JsMain.Inputs[JsConst.INPUT_NATIONWIDELID]));
//		var bNationWideJobs = false;
//		if (jclid.indexOf("42886") > -1) {
//			curQuery.Set(JsConst.QS_LID, jclid);
//			bNationWideJobs = true;
//		}
//	}

	// clear out the MLT fields
	curQuery.Clear(JsConst.QS_MLTDOCID);
	curQuery.Clear(JsConst.QS_MLTFIELD);

	// clear the sort value - let the app choose the default
	curQuery.Clear(JsConst.QS_SORT);

	//clear the Greybox view mode if not registered
	/*if(JsMain.IsSaveSearch)   
	{
	curQuery.Set( JsConst.QS_GREYBOXVIEW, 'Add' );
		    
	if ( typeof JsMain.Inputs[JsConst.INPUT_UPDATE_AGENTID] !== "undefined" ) {
	var agentID = JsUtil.Trim( JsUtil.GetElementValue( JsMain.Inputs[JsConst.INPUT_UPDATE_AGENTID] ) );
	if(agentID && agentID.length>0) curQuery.Set( JsConst.QS_AGENTID, agentID ); 
	}

	WTLog( new Array(['DCS.dcsuri', 'Search_SaveSearch.lyr']) );
	}
	else{
	curQuery.Clear( JsConst.QS_GREYBOXVIEW );	
	}*/

	if (JsMain.REValue == null) {
		JsMain.REValue = 0; //0,Refine Search Form
	}
	// go to the new page
	JsMain.GotoPage(curQuery);
}