/**
 * Triangle Search Object
 *
 * This object is intended to help transition the Triangle search bar into a
 * more flexable format.
 *
 * Modified by Xinxin Liu on 10/25/2007, contact her for previous version as of 10/24/2007
 */
var TriangleSearch = function() {

	return {
		currentPage: '',
		oldAction: '',
		calendarPresent: function() {
			return (typeof(Calendar) != 'undefined' && Calendar.search) ? true : false;
		}
	}
}();


function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


// Configuration of each search type
var config  = {
	jobs: {
		aff: "jobs",
		name: "jobs",
		submitUrl : "http://localsearch.triangle.com/sp",
		id: "jobs"
		},
	all: {
		aff: 7,
		name: "all",
		submitUrl : "http://localsearch.triangle.com/sp",
		id: "all"
		},
	ads: {
		aff: 6,
		name: "ads",
		submitUrl : "http://localsearch.triangle.com/sp",
		id: "ads"
		},
	yellowpages: {
		aff: 1109,
		name: "yellowpages",
		submitUrl : "http://localsearch.triangle.com/sp",
		id: "yellowpages"
		},
	events: {
		aff: "events",
		name: "events",
		submitUrl : "http://localsearch.triangle.com/sp",
		id: "events"
		},
	news: {
		aff: 2,
		name: "news",
		submitUrl : "http://localsearch.triangle.com/sp",
		id: "news"
		}
	}

// search location and center city pairs
var objSearchLocation = {
		0: {city: "Raleigh", state: "NC", radius: 90},
		493: {city: "Raleigh", state: "NC", radius: 15},
		491: {city: "Elizabeth City", state: "NC", radius: 15},
        490: {city: "Greenville", state: "NC", radius: 15},
        492: {city: "Wilmington", state: "NC", radius: 15},                  
        488: {city: "Asheville", state: "NC", radius: 15},
        494: {city: "Charlotte", state: "NC", radius: 15},
		495: {city: "Columbia", state: "SC", radius: 60},
		5324: {city: "Richmond", state: "VA", radius: 60}
}
		
// Change affiliate id based on search type
function localSearch(name) {
	if (typeof config[name] == "undefined") {
		return;
	}
	removeSelected();
	TriangleSearch.currentPage = config[name].name;
	document.search.action = config[name].submitUrl;
	var frm = document.search;
	frm.aff.value = config[name].aff;
	var tab= document.getElementById (config[name].id);
	tab.className = "selected";
}


function removeSelected() {
	var i,el;
	for (i in config) {
		el = document.getElementById(config[i].id);
		if (el) {
			el.className = '';
		}
	}
}

// Format the search criteria so it will be effective with Events Calendar
function eventSearch(name,x,onImage,y) {
	TriangleSearch.currentPage = name;
	TriangleSearch.oldAction   = document.search.action;
	swapImage(name,x,onImage,y);
	document.search.action = "";
}

function search_submit() {
	if (TriangleSearch.currentPage && TriangleSearch.currentPage == 'events') {
		locationID = document.search.catId.options[document.search.catId.selectedIndex].value;
		window.location = "http://events.triangle.com/search?swhat=" + encodeURIComponent(document.search.keywords.value) + "&swhen=&swhere=" + encodeURIComponent(objSearchLocation[locationID].city + ', ' + objSearchLocation[locationID].state) + "&srad=" + encodeURIComponent(objSearchLocation[locationID].radius) + "&st=event&svt=text&search=true";
		return false;
	}
	else if (TriangleSearch.currentPage && TriangleSearch.currentPage == 'jobs') {
		locationID = document.search.catId.options[document.search.catId.selectedIndex].value;
		window.location = "http://www.careerbuilder.com/JobSeeker/Jobs/JobResults.aspx?lr=cbmc_no&siteid=mc_no012&use=all&any=&mxjobsrchcriteria_state=" + encodeURIComponent(objSearchLocation[locationID].state) + "%2C+US&mxjobsrchcriteria_rawwords=" + encodeURIComponent(document.search.keywords.value) + "&mxjobsrchcriteria_city=" + encodeURIComponent(objSearchLocation[locationID].city) + "&MXJOBSRCHCRITERIA_JobTypes=All";
		return false;
	}
	else {
		return true;
	}
}