/*
	Author: Matt Johnson
	Company: Bullhorn.co.uk
	Last Mod: 06/05/2008
*/
function performFilter() {
	var fMins = evaluateObject('filter_min');
	var fTexts = evaluateObject('filter_text');
	var fMonths = evaluateObject('filter_month');
	var filterString = '';
	if ((fMins) && (fMins != ''))
		filterString += String('&filter_min=' + String(fMins));
	if ((fTexts) && (fTexts != ''))
		filterString += String('&filter_text=' + String(fTexts));
	if ((fMonths) && (fMonths != ''))
		filterString += String('&filter_month=' + String(fMonths));
	if ((filterString) && (filterString != ''))
	window['__filter_query_addition'] = filterString;
	window['__bullhorn_page'] = 1;
	ajaxResults(false, false);
}
function alterDisplay(elementId) {
	var myEle = dGetById(elementId);
	if (myEle) {
		if (!window['pie_' + elementId]) {
			try {
				window['pie_' + elementId] = new Fx.Slide(elementId);
				window['pie_' + elementId].hide();
			} catch(ex) {}
		} else {
			window['pie_' + elementId].toggle();
		}
	}
}
var accordRst = 1;
function flipAccordion() {
	if (!window['arbitraryWindow_mobile']) {
		window['arbitraryWindow_mobile'] = new Accordion('a.arbitraryWindow_mobileAnubis', 'div.arbitraryWindow_mobileModel', { opacity: false, mode : 'horizontal' }, $('accordion'));
	} else {
		window['arbitraryWindow_mobile'].display(accordRst);
		if (accordRst == 1)
			accordRst = 0;
		else
			accordRst = 1;
	}
}
addEvent(window, 'load', flipAccordion, false);
function exposeAdvanced() {
	if (window['arbitraryWindow_mobile']) {
		window['arbitraryWindow_mobile'].display(1);
		accordRst = 0;
	}
}
var focalElement;
function refreshFields(elementName)
{
	focalElement = elementName;
	ajaxResults(false, true);
}

function reOrder(orderType) {
	updateInput('bullhorn_order',String(orderType));
	ajaxResults(false, false);
}
function alterDisplay(elementId) {
	var myEle = dGetById(elementId);
	if (myEle) {
		if (!window['pie_' + elementId]) {
			try {
				window['pie_' + elementId] = new Fx.Slide(elementId);
				window['pie_' + elementId].hide();
			} catch(ex) {}
		} else {
			window['pie_' + elementId].toggle();
		}
	}
}
function switchTab(tabElement) {
	if (tabElement) {
		var tab1 = dGetById('tab1');
		var tab2 = dGetById('tab2');
		var tab3 = dGetById('tab3');
		var tab4 = dGetById('tab4');
		var tabPage1 = dGetById('tab_page_1');
		var tabPage2 = dGetById('tab_page_2');
		var tabPage3 = dGetById('tab_page_3');
		var tabPage4 = dGetById('tab_page_4');
		tab1.className = '';
		tab2.className = '';
		tab3.className = '';
		tab4.className = '';
		tabPage1.style.display = 'none';
		tabPage2.style.display = 'none';
		tabPage3.style.display = 'none';
		tabPage4.style.display = 'none';
		tabElement.className = 'selected';
		var selTab = dGetById(String(tabElement.id).replace('tab', 'tab_page_'));
		if (selTab) {
			selTab.style.display = 'block';
		}
		window['selectedTab'] = tabElement.id;
	}
	return false;
}
function ajaxResults(isTemp, isFields) {
	var elementName = 'mobile_result';
	if (isTemp) {
		elementName = 'temp_results';
	}
	if (!isFields) {
		dGetById(elementName).innerHTML = '\<center style=\"padding-top:100px;\"\>\<table width=\"400\" border=\"0\"\>\<tr\>\<td rowspan=\"3\"\>\<img src=\"\/images\/mob_shine.gif\" \/\>\<\/td\>\<td\>\<center\>\<img src=\"\/images\/newloader.gif\" \/\>\<\/center\>\<\/td\>\<\/tr\>\<tr\>\<td\>\<h2 style=\"font-size:18px;font-weight:bold;color:#4444ff;\"\>Loading Mobile Phones\<\/h2\>\<\/td\>\<\/tr\>\<tr\>\<td\>Bullhorn is loading your selection of mobile phones, this may take a moment. Please be patient.\<\/td\>\<\/tr\>\<\/table\>\<\/center\>';
	}

	var homeSearch = '';

	var ajaxUrl = '/mobiles/?bulljax=true'
	ajaxUrl += '&bullhorn_tariff=' + dGetById('bullhorn_tariff').value;

	homeSearch = evaluateObject('bullhorn_manufacturer_X');
	if (String(homeSearch).length > 0) {
		ajaxUrl += '&bullhorn_manufacturer=' + homeSearch;
		updateInput('bullhorn_manufacturer_X','');
	} else {
		ajaxUrl += '&bullhorn_manufacturer=' + evaluateObject('bullhorn_manufacturer');
	}

	homeSearch = evaluateObject('bullhorn_model_X');
	if (String(homeSearch).length > 0) {
		ajaxUrl += '&bullhorn_model=' + homeSearch;
		updateInput('bullhorn_model_X','');
	} else {
		ajaxUrl += '&bullhorn_model=' + evaluateObject('bullhorn_model');
	}

	homeSearch = evaluateObject('bullhorn_network_X');
	if (String(homeSearch).length > 0) {
		ajaxUrl += '&bullhorn_network=' + homeSearch;
		updateInput('bullhorn_network_X','');
	} else {
		ajaxUrl += '&bullhorn_network=' + evaluateObject('bullhorn_network');
	}

	ajaxUrl += '&bullhorn_call=' + evaluateObject('bullhorn_call');
	ajaxUrl += '&bullhorn_texts=' + evaluateObject('bullhorn_texts');
	ajaxUrl += '&bullhorn_tab=' + evaluateObject(window['selectedTab']);
	ajaxUrl += '&bullhorn_order=' + evaluateObject('bullhorn_order');
	ajaxUrl += '&bullhorn_price=' + evaluateObject('bullhorn_price');
	ajaxUrl += '&bullhorn_gift=' + evaluateObject('bullhorn_gift');
	ajaxUrl += '&bullhorn_display=' + evaluateObject('bullhorn_display');
	ajaxUrl += '&bullhorn_provider=' + evaluateObject('bullhorn_provider');
	if (isFields) {
		ajaxUrl += '&fieldupdate=true';
		if (focalElement) {
			ajaxUrl += '&bullhorn_focus=' + String(focalElement);
		}
		dynamicExecute(ajaxUrl);
	} else {
		if (window['__filter_query_addition'])
			ajaxUrl += String(window['__filter_query_addition']);
		if (window['__bullhorn_page'])
			ajaxUrl += '&bullhorn_page=' + window['__bullhorn_page'];
		if (isTemp)
			ajaxUrl = '/mobiles/?bulljax=true&bullhorn_tariff=CONT&bullhorn_manufacturer=&bullhorn_call=100&bullhorn_texts=100&bullhorn_model=&bullhorn_tab=&bullhorn_network=&bullhorn_order=&bullhorn_price=&bullhorn_gift=&bullhorn_display=5&bullhorn_page=1';

		var bullJax = new BullHorn.Ajax(ajaxUrl);
		bullJax.onDraw = function(message) {
			dGetById(elementName).innerHTML = message;
			window['renderer'] = setTimeout('clearTimeout(window[\'renderer\']);if (dGetById(\'dirtyCode\')) {eval(dGetById(\'dirtyCode\').innerHTML); }', 200);
		}
		bullJax.load();
	}
}
function catchSubmit(e) {
	if (!e)
		e = window.event;
	if (e.keyCode == 13) {
		var bullhorn_search = null;
		bullhorn_search = dGetById('bullhorn_search');
		if (bullhorn_search) {
			bullhorn_search.click();
		}
		cancelBubble(e);
	}
	else
	{
		var sourceElement = null;
		if (e.srcElement) {
			sourceElement = e.srcElement;
		} else {
			sourceElement = e.target;
		}

		if (sourceElement) {
			if (sourceElement.id == 'bullhorn_tariff') {
				if (sourceElement.value == 'PAYG') {
					dGetById('bullhorn_call').selectedIndex = 0;
					dGetById('bullhorn_texts').selectedIndex = 0;
					dGetById('bullhorn_call').disabled = true;
					dGetById('bullhorn_texts').disabled = true;
				} else {
					dGetById('bullhorn_call').disabled = false;
					dGetById('bullhorn_texts').disabled = false;
				}
			}
		}

		if (sourceElement) {
			refreshFields(sourceElement.id);
		}else{
			refreshFields(null);
		}
	}
}