

// Site Search

function simpleSearch(debug, q_in) {
	var q = new Query();
	setCommonParams(q);
	q.params['site'] = defaultSearchSite;
	if (q_in == null)
		q.params['q'] = escape($F('search-text'));
	else
		q.params['q'] = escape(q_in);
	last_q = q.params['q'];

	var query = q.buildQuery();
	if (debug) {
		 alert(query);
	} else {
		$('search-result').src = searchUrl + '?' + query;
	}
}

function advancedSearch(debug, q_in) {
	var q = new Query();
	setCommonParams(q);
	
	q.params['q'] = escape(q_in == null ? $F('search-text') : q_in);
	last_q = q.params['q'];

	if ($('search-publication') != null) {
		if ($F('search-publication') == 'all')
			q.params['site'] = commonCollection;
		else
			q.params['site'] = $F('search-publication') + "_collection";
	} else {
		q.params['site'] = defaultSearchSite;
	}
	
	var pubdate = $RF('advanced-search-form', 'search-pubdate', 'all');
	if (pubdate == 'all') {
	} else if (pubdate == 'range') {
		q.params['as_q'] = 'daterange:' + $F('search-from-year') + '-' + $F('search-from-month') + '-' + $F('search-from-day') + '..' + $F('search-to-year') + '-' + $F('search-to-month') + '-' + $F('search-to-day');
	} else {
		q.params['as_q'] = 'daterange:' + dateRange[pubdate];
	}
		
	if ($('search-publication') != null && $F('search-publication') != 'all') {
		q.metadata['section'] = escape($F('search-section-' + pub));
		q.metadata['articleAuthor'] = escape($F('search-author-' + pub));
	}

	q.metadata['articleTypeName'] = escape($F('search-articleType'));

	var query = q.buildQuery();
	if (debug) {
		 alert(query);
	} else {
		$('search-result').src = searchUrl + '?' + query;
	}
}

function changePublication() {
	if ($F('search-publication') == 'all') {
		$('searchform-section').style.display = 'none';
		$('searchform-author').style.display = 'none';
	} else {
		$('searchform-section').style.display = 'block';
		$('searchform-author').style.display = 'block';

		$('search-author-' + pub).style.display = 'none';
		$('search-section-' + pub).style.display = 'none';
		pub = $F('search-publication');
		$('search-author-' + pub).style.display = 'inline';
		$('search-section-' + pub).style.display = 'inline';
		$('search-author-' + pub).selectedIndex = 0;
		$('search-section-' + pub).selectedIndex = 0;
	}
}

// Classifieds Search

function classfiedsSearch(q) {
	if (q == null)
		q = new Query();
		
	setCommonParams(q);
	q.params['q'] = escape($F('search-text'));

	if ($F('search-category') != 'all') {
		q.metadata['adCategory'] = escape(escape($F('search-category')));
	} else {
		q.metadata['adCategory'] = "AD";
	}
	
	//Workaround for bug in GSA. 
	//Now we can order by date even if query is empty.
	//This also mean that this will only work on .se sites.
	q.params['as_q'] = "-gotatest+inurl:se";
	
	q.params['as_eq'] = "inmeta:adExpires:0.." + serverTimestamp;

	var query = q.buildQuery();
	$('search-result').src =  searchUrl + '?' + query;
}

function performClassfiedsSearch(q) {
	var query = q.buildQuery();
	$('search-result').src =  searchUrl + '?' + query;
}

// Common

function Query() {
	this.params = {};
	this.metadata = {};
	this.buildQuery = function () {
		var qs  = '';
		for (var name in this.params)
			qs += (qs != '' ? '&' : '') + name + "=" + this.params[name];
		var md = '';
		for (var name in this.metadata) {
			var value = this.metadata[name];
			if (value != '' && value != 'all') {
				if (value instanceof Array && value.length > 0) {
					md += (md != '' ? '.' : '') + name + ":" + value[0];
					for (i = 1; i < value.length; i++) {
						md += '|' + name + ":" + value[i];
					}
				} else {
					md += (md != '' ? '.' : '') + name + ":" + value;
				}				
			}
		}
		if (md != '')
			qs += '&requiredfields=' + md;
		return qs;
	}
}

function $RF(formId, radioGroupId, defaultValue) {
	var el = Form.getInputs(formId, 'radio', radioGroupId).find(function(radio) { return radio.checked; });
	if (el == null)
		return defaultValue;
	return el.value;
}

function resizeSearchIFrame() {
	var iframe = $('search-result');
	try {
		h = iframe.contentWindow.document.body.scrollHeight;
	} catch(e) {
		h = minIFrameHeight;
	}
	iframe.height = h + "px";
}
