function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
	window.onload = func;
  } else {
	window.onload = function() {
	  if (oldonload)
	  {
		oldonload();
	  }
	  func();
	}
  }
}

function get_radio_ids(form) {
	var ids = Array();
	if (document[form].price_bands) {
		for (var i=0; i<document[form].price_bands.length; i++)  {
			ids[i] = document[form].price_bands[i].value;
		}
	}
	return ids;
}

stack = 0;
function caller() {
	stack++;
	setTimeout("doer()", 1000);
}

function doer() {
	if(stack == 1) {
		ajax_search(document.getElementById("page").value,true);
	  }
	stack--;
}

function load_ajax() {
	if(! getAJAXObject ) {
		return;
	}

	el = document.getElementById('aj_search');
	if(el) {
		el.onkeyup = function() { caller(); ajax_search(); }
	}

	el = document.getElementById('aj_brand_id');
	if(el) {
		el.onchange = function() { ajax_search() }
	}
	
	el = document.getElementById('qsAuthor');
	if(el) {
		
		el.onkeyup = function() { ajax_search() }
	}
	
	el = document.getElementById('qsTitle');
	if(el) {
		el.onkeyup = function() { caller(); ajax_search(); }
	}

	el = document.getElementById('cat');
	if(el) {
		el.onchange = function() { ajax_search() }
	}


	if ( document['frmAdvSearch'] ) {
		if (document['frmAdvSearch'].price_bands) {
			for (var i=0; i < document['frmAdvSearch'].price_bands.length; i++)  {
				document['frmAdvSearch'].price_bands[i].onclick = function() { ajax_search() };
			}
		}
		if ( document['frmAdvSearch'].search_type ) {
			if ( document['frmAdvSearch'].search_type.value == 'gift_finder' ) {
				var aBoxes = document.getElementById('gift_finder_inputs').getElementsByTagName('input');
				for ( var i=0; i < aBoxes.length; i++ ) {
					aBoxes[i].onclick = function() { ajax_search() };
				}
			}
		}
	}

	el = document.getElementById('aj_user_length');
	if(el) {
		el.onchange = function() { ajax_search() }
	}

	el = document.getElementById('aj_submit');
	if(el) {
		el.style.display = "none";
	}
}

function ajax_search(page,save) {
	
	//return false;

	var ajaxObject				= getAJAXObject();

	if(! ajaxObject ) {
		return;
	}

	show('ajaxProgress');

	//Cache Killer

	if (typeof page == "undefined") {
		page = encodeURI( document.getElementById("page").value );
	} else {
		document.getElementById("page").value = page;
	}
	


	var now = "uid=" + new Date().getTime();
	if ( document.getElementById('search_type') ) {
		var parameters = '';
		var aBoxes = document.getElementById('gift_finder_inputs').getElementsByTagName('input');
		for ( var i=0; i < aBoxes.length; i++ ) {
			if ( aBoxes[i].checked == true ) {
				parameters = parameters + ',' + aBoxes[i].value;
			}
		}
		parameters = parameters.substr(1) + '&search_type=gift_finder&page=' + page;
		parameters = 'gift_finder=' + parameters + "&order=" + encodeURI( document.getElementById("order").value );
	} else {
		var priceBandVal = getRadioValue('frmAdvSearch');
		var parameters = "search=" + encodeURI( document.getElementById("aj_search").value ) +
						 "&page=" + page;

						 if (document.getElementById("order")){
							parameters = parameters + "&order=" + encodeURI( document.getElementById("order").value );
						 }
						 if (document.getElementById("cat")){
							parameters = parameters + "&cat=" + encodeURI( document.getElementById("cat").value );
						 }
						 if (document.getElementById("qsAuthor")){
							parameters = parameters + "&qsAuthor=" + encodeURI( document.getElementById("qsAuthor").value );
						 }
						 if (document.getElementById("qsTitle")){
							parameters = parameters + "&qsTitle=" + encodeURI( document.getElementById("qsTitle").value );
						 }
						 /*"colour=" + encodeURI( document.getElementById("aj_colour").value ) + "&" +*/
						 if (document.getElementById("aj_brand_id")){
							parameters = parameters + "&brand_id=" + encodeURI( document.getElementById("aj_brand_id").value);
						 }
						 if (priceBandVal){
							parameters = parameters + "&price_band_id=" + priceBandVal;
						 }
						 if (document.getElementById("aj_user_length")){
							parameters = parameters + "&user_length=" + encodeURI( document.getElementById("aj_user_length").value);
						 }
						 if (save) {
							parameters += "&save=1";
						 }
						 //alert(parameters);

	}

	ajaxObject.open("POST", 'inc/ajax_search.php?' + now, true);

	ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxObject.setRequestHeader("Content-length", parameters.length);
	ajaxObject.setRequestHeader("Connection", "close");
	ajaxObject.send(parameters);


	ajaxObject.onreadystatechange = handleAJAXResponse;

	ajaxObjects.push(ajaxObject);

	return false;
}

function getRadioValue(form) {
	var val = -1;
	if (document[form].price_bands != null) {
		for (var i=0; i<document[form].price_bands.length; i++)  {
			//alert (document[form].price_bands[i].value);
			if (document[form].price_bands[i].checked)  {
				return document[form].price_bands[i].value;
			}
		}
	}
	return false;
}

