/* 

this object is used to hook up the search panel to ajax_object 
searches as used in search_jobs includes

needs to pass search parameters then call a reload on ajax_object

*/

function ajaxJobPanel(id) {

	this.id = id;
	window["ajaxJobPanel"+id] = this;
	
	ajaxJobPanel.prototype.ua = navigator.userAgent.toLowerCase();
	if ((this.ua.indexOf("msie") != -1) && (this.ua.indexOf("opera") == -1) && (this.ua.indexOf("webtv") == -1)) this.isIE = true; 
}

ajaxJobPanel.prototype.debug = false;

ajaxJobPanel.prototype.id = '';
ajaxJobPanel.prototype.isIE = false;
ajaxJobPanel.prototype.ua = '';

ajaxJobPanel.prototype.types_filtered = new Array();

ajaxJobPanel.prototype.filters_array = new Array();
ajaxJobPanel.prototype.orders = false;
ajaxJobPanel.prototype.locations = false;

//ajaxJobPanel.prototype.filter_rows = new Array(2,0);
ajaxJobPanel.prototype.filter_rows = new Array(3,0);

// we may require
ajaxJobPanel.prototype.http_request = false;
//ajaxJobPanel.prototype.js_method = 'updateObjects'; // this will remain internal to ajax_object 
ajaxJobPanel.prototype.ajax_object = false; // reference to ajax_object (search object)

// will use ajaxObject.addScriptParam functions directly at runtime
// ajaxJobPanel.prototype.script_url = '';
// ajaxJobPanel.prototype.script_params = new Array();

ajaxJobPanel.prototype.numnavi = false;
ajaxJobPanel.prototype.navistr = false;
// ajaxJobPanel.prototype.loadindicator = false;

// ajaxJobPanel.prototype.loadindicator_image = new Array('/media/indicator.gif','/media/blank.gif');

// if reloaded from panel we want the anchor to be on the panel
ajaxJobPanel.prototype.anchor = "searchpanel";

ajaxJobPanel.prototype.snum = 0;
ajaxJobPanel.prototype.vnum = 0; // 0 will take the default from the list (ajax_object) instead

// associative array - will contain mapping between input fields and the appropriate name for script params
// use for (value in object) { field = object[value]; } to iterate
ajaxJobPanel.prototype.fields = new Object();
ajaxJobPanel.prototype.fields.keywords = 'searchstr';


ajaxJobPanel.prototype.search_button = 'searchjobs';
ajaxJobPanel.prototype.snum_adjust = 1;
ajaxJobPanel.prototype.snum_label = "snum";
ajaxJobPanel.prototype.viewnum = "0";

ajaxJobPanel.prototype.getXMLHttpObject = function() {

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		this.http_request = new XMLHttpRequest();
		if (this.http_request.overrideMimeType) {
			this.http_request.overrideMimeType('application/xml');
		}
		
		return true;
		
	} else if (window.ActiveXObject) { // IE
		try {
			this.http_request = new ActiveXObject("Msxml2.XMLHTTP");

			return true;

		} catch (e) {
			try {
				this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				
				return true;

			} catch (e) { this.http_request = false; }
		}
	}

}

ajaxJobPanel.prototype.init = function() {
	
	this.addOnClickHandlers();
}

ajaxJobPanel.prototype.clearLocations = function() {
	this.locations = new Array();
}
ajaxJobPanel.prototype.addLocation = function(locid) {
	this.locations[this.locations.length] = locid;
}
ajaxJobPanel.prototype.resetScriptParams = function(key,param) {
	this.script_params = new Array();
}

ajaxJobPanel.prototype.setScriptParam = function(key,param) {
	this.script_params[this.script_params.length] = new Array(key,param);
}

ajaxJobPanel.prototype.setScriptParamForKey = function(key,param) {
	
	var index = -1;
	
	for (var i=0;i<this.script_params.length;i++) {
		
		if (this.script_params[i][0] == key) {
			index = i;
		}
	}

	if (index > -1) {
		this.script_params[index][1] = param;
	}
	else {
		this.setScriptParam(key,param);
	}
}

ajaxJobPanel.prototype.getValue = function(tagname,data) {
	return this.getTextFromXMLNode(data.getElementsByTagName(tagname)[0]);
}

ajaxJobPanel.prototype.setScriptParams = function(params) {
	this.script_params = params;
}

ajaxJobPanel.prototype.doUpdate = function(input) {

	try {

		if (this.http_request.readyState == 4) {

			if (this.http_request.status == 200) {
				
				if (this.loadindicator) {
					for (var i=0; i<this.loadindicator.length; i++) {
						this.loadindicator[i].src = this.loadindicator_image[1];
					}
				}
				
				if (this.updateObjects(this.http_request.responseXML)) {
					return false;
				}
				else {
					if (input) {
						window.location = input.href;
					}						
					return true;
				}

			
			} else {
				for (var i=0; i<this.loadindicator.length; i++) {
					this.loadindicator[i].src = this.loadindicator_image[1];
				}
				
				if (input) {
					window.location = input.href;
				}
				return true;
			}
		}
	}
	catch(e) {
		// alert('Caught Exception: ' + e.description);
		if (this.loadindicator) {
			for (var i=0; i<this.loadindicator.length; i++) {
				this.loadindicator[i].src = this.loadindicator_image[1];
			}
		}
		if (input) {
			window.location = input.href;
		}
		
		return true;
	}
}

ajaxJobPanel.prototype.getTextFromXMLNode = function(node) {

	if (this.isIE) {
		return node.text;
	}
	else {
		if (node.hasChildNodes()) {
			return node.childNodes[0].nodeValue;
		}
		else {
			return "";
		}
	}
}
ajaxJobPanel.prototype.buildScriptParams = function() {

	var search = this.ajax_object;
	
	// this.resetScriptParams();
	
	if (document.getElementById('qstr')) {
		search.setScriptParamForKey('searchstr',document.getElementById('qstr').value);
	}
	
	if (document.getElementById('dh'))	{	
		search.setScriptParamForKey('direct_hire',document.getElementById('dh').value);
	}

	if (document.getElementById('jtp')) {
		var type = "";
		
		if ($('jtp') && $('jtp').checked == true) {
			type += "1";
		}
		if ($('jtt') && $('jtt').checked == true) {
			if (type.length > 0) type += ",";
			type += "2";
		}
		if ($('jttp') && $('jttp').checked == true) {
			if (type.length > 0) type += ",";
			type += "3";
		}
		if ($('jti') && $('jti').checked == true) {
			if (type.length > 0) type += ",";
			type += "4";
		}
		
		if (this.types_filtered.length > 0) {
			if (type.length == 0) {
				for (var i=0;i<this.types_filtered.length;i++) {
					if (type.length > 0) type += ",";
						type += this.types_filtered[i];
				}
			}
		}
		else if (type.length == 7) {
			type = "";
		}
		
		search.setScriptParamForKey('type_id',type);
	}
	
	if ($('cid')) {
		search.setScriptParamForKey('search_country',$('cid').options[$('cid').selectedIndex].value);
	}
	
	if (this.filters_array.length) {
		
		for (var i=0; i<this.filters_array.length; i++) {
			
			var filterstr = "";
			
			for (var j=0; j<this.filters_array[i].length; j++) {
				if (this.filters_array[i][j].className == "on") {
					
					if (filterstr.length > 0) filterstr += ",";
					var id = this.filters_array[i][j].id.split("_");
					
					filterstr += id[(id.length-1)];
				}
			}
			
			if (this.filters_array.length > 1) {
				var paramstr = 'filter_' + (i+1);
			}
			else {
				var paramstr = 'search_filter';
			}
			
			search.setScriptParamForKey(paramstr,filterstr);
		}
	}
	
	if ($('on_time')) {
		
		var jf = '0'; var jp = '0';
		
		if ($('jf').checked) {
			jf = '1';
		}
		
		if ($('jp').checked) {
			jp = '1';
		}
		
		search.setScriptParamForKey('search_full',jf);
		search.setScriptParamForKey('search_part',jp);
	}
	
	if (this.orders.length) {
		
		var order = "";
		
		for (var i=0; i<this.orders.length; i++) {
			if (this.orders[i].className == "on") {
				var id = this.orders[i].id.split("_");
				order = id[(id.length-1)];
			}
		}
		
		if (order == "0" && search.scriptParamForKey('searchstr').length == 0) {
			order = "1";
			this.setOrder("order_" + order);
		}
			
		search.setScriptParamForKey('search_order',order);
	}
	
	search.setScriptParamForKey('loc',this.locations);

}
ajaxJobPanel.prototype.doSearch = function() {

    var search = this.ajax_object;
    //panel_id = "ajaxJobPanel"+this.id;
    
	// window[panel_id].buildScriptParams();
	this.buildScriptParams();
	
	// can't copy the params array as the elements will be by reference
	var params = new Array(); 

	for (var i=0;i<search.script_params.length;i++) {
		params[i] = search.script_params[i];
	}

	params[params.length] = new Array(search.snum_label,0);

	// add panel parameters
	
	// alert(search.buildScriptUrl(params)); return false;

	return search.makeXMLRequest(search.buildScriptUrl(params),this);
}
ajaxJobPanel.prototype.addOnClickHandlers = function() {

	panel_id = "ajaxJobPanel"+this.id;
	
    if ($(this.search_button)) {
    	
    	$(this.search_button).onclick = function() {

			// we may have custom parameters inside the jobList object
			// add our panel parameters directly to the jobList object so they are remembered when paging
			
			this.blur();
			return window[panel_id].doSearch();
		}
	}
	
	this.filters_array = new Array();
	this.orders = new Array();
	
	if ($("searchorder")) {

		var orderButtons = $("searchorder").getElementsByTagName('a');
		
		for (var i=0, c=orderButtons.length; i<c; i++) {
			
			this.orders[this.orders.length] = orderButtons[i];
			
			orderButtons[i].onclick = function () { 
				
				window[panel_id].setOrder(this.id);
				this.blur();
					
				return window[panel_id].doSearch();
			}
		}
	}
	
	if ($("searchpanel")) {

		$("searchpanel").onsubmit = function () { 

			window[panel_id].doSearch();
			return false;
		}
	}
	

	if ($("in_location")) {

		var buttons = $("in_location").getElementsByTagName('a');
		
		for (var i=0, c=buttons.length; i<c; i++) {
			
			buttons[i].onclick = function () { 

				var reg = /loc(|\[\])\=([0-9]{0,})/g;	
				var matches;
				
				window[panel_id].clearLocations();
				
				while ((matches = reg.exec(this.href)) != null) {
					window[panel_id].addLocation(matches[2]);				
				}
				
				var buttons = $("in_location").getElementsByTagName('a');
		
				for (var i=0, c=buttons.length; i<c; i++) {
					buttons[i].style.textDecoration = "none";
				}
				
				this.style.textDecoration = "underline";
				this.blur();

				return window[panel_id].doSearch();
				
			}
		}
	}
	
	// cannot use the id tabpane_buttons as it has been placed on multiple divs	
	// put an id on the directhire tab and call up() instead
	//if ($("tabpane_buttons")) {

	if ($("directhire_tab")) {
		
		var tabButtons = $("directhire_tab").up().getElementsByTagName('a');
		
		for (var i=0, c=tabButtons.length; i < c; i++) {
				
			tabButtons[i].onclick = function () {

				if (this.href.search(/\bdh=1\b/) != -1) $('dh').value = '1'; else $('dh').value = '0';

				$(this).up().className = "active";
				
				var siblings = $(this).up().siblings();
				
				for (var j=0, d=siblings.length; j < d; j++) {
					if (siblings[j].tagName.toLowerCase() == "li") {
						siblings[j].className = "inactive";
					}
				}

				this.blur();
				return window[panel_id].doSearch();
			}
		}
	}
    
    // we may have more than one group of filters in the panel so get by name instead of id
	if (filter_array = document.getElementsByName("searchfilters")) {

		for (var i=0, c=filter_array.length; i<c; i++) {
			
			// if we have more than one filter group need to make the ids unique
			if (filter_array.length > 1)
				var poststr = '_' + (i+1);
			else
				var poststr = '';
			
			// store all of the items to hide in this filter group then collapse
			// the group after we have processed all the columns (lists)
			var itemsToHide = new Array();

			// if any of the filters beneath the collapse point are turned on we dont collapse
			var collapse = true;
			
			// we keep the filter groups and buttons in a property of the ajax panel object
			this.filters_array[i] = new Array();
			
			// we have more than one ul - they are in columns
			var lists = filter_array[i].getElementsByTagName('ul');
			
			for (var j=0, d=lists.length; j<d; j++) {
				
				var row = 0;
				
				var items = lists[j].getElementsByTagName('li');
				
				for (var k=0, e=items.length; k<e; k++) {
					
					// we must manually extend the dom node for IE
					var button = $(items[k]).down();
				
					// if button is on and beneath the visible rows make sure this filter group isn't collapsed
					if (row > this.filter_rows[i] && button.className == 'on') {
						collapse = false;
					}
					
					// we store the filter groups and buttons inside the ajax panel object
					this.filters_array[i][this.filters_array[i].length] = button;
					
					// add the onclick to the filter button
					button.onclick = function () { 
						
						// call the toggleClass method of the ajax panel object on this button
						window[panel_id].toggleClass(this);
						this.blur();
						
						return window[panel_id].doSearch();
					}
					
					// store the list items that are to be hidden when collapsing the filter group
					if (row > this.filter_rows[i]) {
						itemsToHide[itemsToHide.length] = items[k];
						
					}

					row++;
				}

			}

			// draw the more/less button for filter groups larger than collapse point
			
			if (itemsToHide.length) {
			
				if (div = $('showfilterscontainer' + poststr)) {
					
					div.setStyle({ display: 'block' });
					button = div.appendChild(new Element('a', { 'id': 'showfilters' + poststr }).update("More"));
				
					if (button) {
						// set up custom properties for inside the onclick
						button.containerid = "searchfilters" + poststr;
						button.buttonid = "showfilterscontainer" + poststr;
						button.myFilterNum = i;
										
						button.onclick = function () {
							window[panel_id].showFilters(this);
						}
					}
			
					if (collapse == true) {
						for (var j=0; j<itemsToHide.length; j++) {
							this.toggleClass(itemsToHide[j]);
							
						}
					}
					else {
						this.toggleClassAndLabel(d,document.getElementById("showfilters" + poststr));
					}
				}
			}
			
		} // end for filter group
		
	} // end if filter_array = searchfilters


}
ajaxJobPanel.prototype.showFilters = function (a) {

	if (filters = $(a.containerid)) {

		// we have more than one ul - they are in columns
		var lists = filters.getElementsByTagName('ul');

		for (var i=0, c=lists.length; i<c; i++) {

			var items = lists[i].getElementsByTagName('li');
			
			for (var j=0, d=items.length; j<d; j++) {
				// toggle show/hide for each list item beneath the collapse point
				if (j > this.filter_rows[a.myFilterNum]) {
					this.toggleClass(items[j]);
				}
			}
		}
		
		// toggle more/less button
		this.toggleClassAndLabel($(a.buttonid),$(a.id));
	}
}

ajaxJobPanel.prototype.toggleClass = function (a) {
	if (a.className == 'on') {
		ajaxJobPanel.changeObjectClass(a,'off');
		if (a.tagName == 'a' || a.tagName == 'A') a.href = a.href.replace("off","on");
	}
	else {
		ajaxJobPanel.changeObjectClass(a,'on');
		if (a.tagName == 'a' || a.tagName == 'A') a.href = a.href.replace("on","off");
	}
}

ajaxJobPanel.prototype.toggleClassAndLabel = function (div,a) {
	if (div.className == 'on') {
		ajaxJobPanel.changeObjectClass(div,'off')
		a.innerHTML = 'less';
	}
	else {
		ajaxJobPanel.changeObjectClass(div,'on')
		a.innerHTML = 'more';
	}
}
ajaxJobPanel.prototype.setOrder = function (a) {

	for (var i=0;i<this.orders.length;i++) {
		
		if (this.orders[i].id == a) {
			ajaxJobPanel.changeObjectClass(this.orders[i],'on');
		}
		else {
			ajaxJobPanel.changeObjectClass(this.orders[i],'off');
		}
	}
}

ajaxJobPanel.changeObjectClass = function(obj, new_style) {
    obj.className = new_style;
}
