function searchKeyword() {
	var strText = gsdlGetValue(document.QueryForm.countries).gsdl_clean();
	if (strText.length == 0) {
		return false;
	}
	argfqv = '"' + strText + '"' + ",";
	argfqf = 'SU,TI:TX';
	argfqs = cgiarg.s + ',0';
	argfqk = cgiarg.k + ',0';
	argfqc = 'and';

	beginPostSearch();
	return false;
}

function searchHomeKeyword() {
	var strText = gsdlGetValue(document.QueryForm.kw).gsdl_clean();
	if (strText.length == 0) {
		return false;
	}
	argfqv = ',' + strText;
	argfqf = 'SU,TI:TX';
	argfqs = gsdlGetCookieInt('s', gsdlDefaults.args.s) + ',0';
	argfqk = gsdlGetCookieInt('k', gsdlDefaults.args.k) + ',0';
	argfqc = 'and,and,and,and';

	var queryForm = gsdlGetElement('QueryPostForm');
	getsearchargs(queryForm);
	gsdlInitValue(queryForm.g, 'Doc');
	queryForm.submit();
}

function appendKeyword() {
	var myText = gsdlGetValue(document.QueryForm.kw);
	var ctrl = null;
	if (cgiarg.a == 'q')
		ctrl = document.QueryForm.fqv[fqvtextidx];
	else
		ctrl = document.QueryForm.fqv;

	if (ctrl.value) {
		ctrl.value = ctrl.value + ' "' + myText + '"';
	} else {
		ctrl.value = '"' + myText + '"';
	}
}

function submitSimpleSearch() {
	var strText = gsdlGetValue(document.QueryForm.fqv).gsdl_clean();
	if (strText.length == 0) {
		return false;
	}
	argfqv = ',' + strText;
	argfqf = 'SU,TI:TX';
	argfqs = gsdlGetCookieInt('s', gsdlDefaults.args.s) + ',0';
	argfqk = gsdlGetCookieInt('k', gsdlDefaults.args.k) + ',0';
	argfqc = 'and';

	var queryForm = gsdlGetElement('QueryPostForm');
	getsearchargs(queryForm);
	gsdlInitValue(queryForm.g, 'Sec');
	queryForm.submit();
	return false;
}

function getsearchargs (theForm) {
	if (theForm != null) {
		gsdlInitValue(theForm.fqv, unescape(argfqv));
		gsdlInitValue(theForm.fqf, unescape(argfqf));
		gsdlInitValue(theForm.fqk, unescape(argfqk));
		gsdlInitValue(theForm.fqs, unescape(argfqs));
		gsdlInitValue(theForm.fqc, unescape(argfqc));
	} else {
		var args = '';
		args += '&amp;fqv=' + argfqv + '&amp;fqf=' + argfqf;
		args += '&amp;fqk=' + argfqk + '&amp;fqs=' + argfqs + '&amp;fqc=' + argfqc;

		return args;
	}
}

function getqueryargs () {
	return '&amp;fqa=1' + '&amp;q=' + argq + '&amp;fqv=' + argfqv + '&amp;fqf=' + argfqf
		 + '&amp;fqk=' + argfqk + '&amp;fqs=' + argfqs + '&amp;fqc=' + argfqc;
}

function getargac(theForm) {
	var strRet = '&amp;ac=&amp;ac1=&amp;ac2=';
	if (theForm != null) {
		if (typeof(theForm.ac) != 'undefined')
			gsdlInitValue(theForm.ac, '');
		if (typeof(theForm.ac1) != 'undefined')
			gsdlInitValue(theForm.ac1, '');
		if (typeof(theForm.ac2) != 'undefined')
			gsdlInitValue(theForm.ac2, '');
	}
	if(document.QueryForm.ac == null) return strRet;
	var strAC = gsdlGetValue(document.QueryForm.ac);
	if (strAC.length < 1) return strRet;
	var strSearchCountry = gsdlGetValue(document.QueryForm.fqv[fqvctryidx]);
	if (strSearchCountry == null || strSearchCountry.length < 1) return strRet;
	/*if (strSearchCountry == strAC) return strRet;*/
	strRet  = '&amp;ac=' + strAC + '&amp;ac2=' + strAC;
	strRet += '&amp;ac1=' + strSearchCountry;

	if (theForm != null) {
		if (typeof(theForm.ac) != 'undefined')
			gsdlInitValue(theForm.ac, strAC);
		if (typeof(theForm.ac1) != 'undefined')
			gsdlInitValue(theForm.ac1, strSearchCountry);
		if (typeof(theForm.ac2) != 'undefined')
			gsdlInitValue(theForm.ac2, strAC);
	}
	return strRet;
}

function getstdargs(theForm) {
	var value = '';
	stdarg = '';
	if (cgiarg.comp) {
		if (getQueryFormCompField() != null)
			value = gsdlGetRadioValue(getQueryFormCompField());
		else
			value = '1';
		stdarg += '&amp;comp=' + value;
		if (theForm != null) gsdlInitValue(theForm.comp, value);
	} else {
		if (cgiarg.h != '') {
			value = gsdlGetValue(document.QueryForm.h);
			if (value != cgiarg.h) {
				stdarg += '&amp;h=' + value;
				if (theForm != null) gsdlInitValue(theForm.h, value);
			}
		}
		value = '' + gsdlGetValue(document.QueryForm.fqv[fqvtextidx]);
		if (value.length > 0) value = 'Sec';
		else value = 'Doc';
		stdarg += '&amp;g=' + value;
		if (theForm != null) gsdlInitValue(theForm.g, value);
		if (typeof(document.QueryForm.j) != 'undefined' && document.QueryForm.j != null) {
			value = gsdlGetValue(document.QueryForm.j);
			if (value != cgiarg.j) {
				stdarg += '&amp;j=' + value;
				if (theForm != null) gsdlInitValue(theForm.j, value);
			}
		}
		
		if (typeof(document.QueryForm.t) != 'undefined' && document.QueryForm.t != null) {
			value = gsdlGetValue(document.QueryForm.t);
			if (value != cgiarg.t) {
				stdarg += '&amp;t=' + value;
				if (theForm != null) gsdlInitValue(theForm.t, value);
			}
		}

		if (typeof(document.QueryForm.s) != 'undefined' && document.QueryForm.s != null) {
			value = gsdlGetValue(document.QueryForm.s);
			if (value == '1') value = '0';
			else value = '1';
			stdarg += '&amp;s=' + value;
			if (theForm != null) gsdlInitValue(theForm.s, value);
		}
	}
	stdarg += '&amp;r=1';
	var date = new Date();
	value = '' + date.getTime();
	stdarg += '&amp;uq=' + value;
	if (theForm != null) gsdlInitValue(theForm.uq, value);
	value = '' + gsdlNewQueryUniqueId();
	stdarg += '&amp;qu=' + value;
	stdarg += getargac(theForm);
	if (theForm != null) gsdlInitValue(theForm.qu, value);
	return stdarg;
}

function query_initialize() {
	if (document.QueryForm.fqf != null) {
		initfqf();
		if (!cgiarg.comp) inittxti();
	}
	if (typeof(document.QueryForm.t) != 'undefined')
		gsdlInitValue(document.QueryForm.t, cgiarg.t);
	if (gsdlDefined(document.QueryForm.ac)) gsdlInitValue(document.QueryForm.ac, argac);
	initfqv();
	if (cgiarg.b == '1') {
		initfqs();
		initfqk();
		initfqc();
		initq();
	}
	if (cgiarg.comp)
		initQueryCompareCountry();
	return true;
}

// fqf - the field selection box
function initfqf() {
	var i;
	var fqf = argfqf.split(',');
	for (i = 0; i < fqf.length; ++i) {
		if (fqf[i] != '') { gsdlInitValue(document.QueryForm.fqf[i], fqf[i]); }
	}
	if (fqf.length < document.QueryForm.fqf.length) { // initialize remaining ones
		var v = 1; // the option with a null value is either at place 1 or 2
		if (document.QueryForm.fqf[0].options != null && document.QueryForm.fqf[0].options[v].value != '') ++v;
		for (i = fqf.length; i < document.QueryForm.fqf.length; ++i) {
			if (document.QueryForm.fqf[i].options != null) {
				var z=i;
				while (z >= document.QueryForm.fqf[i].options.length - 1) {
					z = z - (document.QueryForm.fqf[i].options.length - 1);
				}
				if (z >= v) ++z; // skip over the void value
				document.QueryForm.fqf[i].options[z].selected = true;
			}
		}
	}
	updatefqf();

}

function inittxti() {
	var fqf = argfqf.split(',');
	if (fqf.length > fqvtextidx) {
		gsdlInitValue(document.QueryForm.txti, fqf[fqvtextidx]);
	} else {
		gsdlInitValue(document.QueryForm.txti, document.QueryForm.fqf[fqvtextidx]);
	}
}

function updatetxti() {
	gsdlInitValue(document.QueryForm.fqf[fqvtextidx], gsdlGetValue(document.QueryForm.txti));
	updatefqf();
}

function updatefqf() {
	argfqf = '';
	argfqf += gsdlGetValue(document.QueryForm.fqf[0]);
	for (var i = 1; i < document.QueryForm.fqf.length; ++i) {
		argfqf += ',' + gsdlGetValue(document.QueryForm.fqf[i]);
	}
}


function clearfqf() {
	for (var i = 0; i < document.QueryForm.fqf.length; ++i) {
		var z = i;
		if (i >= document.QueryForm.fqf[i].options.length) z = z - document.QueryForm.fqf[i].options.length;
		document.QueryForm.fqf[i].options[z].selected = true;
	}
	updatefqf();
}

// fqc - the boolean operator selection box
function initfqc() {
	var i,j;
	fqc = argfqc.split(',');
	if (cgiarg.fqn == 2 || typeof(document.QueryForm.fqc[0]) == 'undefined') { // there will only be one fqc element
		gsdlInitValue(document.QueryForm.fqc, fqc[0]);
	}
	else {
	for (i = 0; i < fqc.length; ++i) {
			gsdlInitValue(document.QueryForm.fqc[i], fqc[i]);
	}
	}
	updatefqc();

}

function updatefqc() {
	var i;
	argfqc = '';
	if (cgiarg.fqn == 2 || typeof(document.QueryForm.fqc[0]) == 'undefined') {
		argfqc += gsdlGetValue(document.QueryForm.fqc);
	}
	else {
		argfqc += gsdlGetValue(document.QueryForm.fqc[0]);
		for (i = 1; i < document.QueryForm.fqc.length; ++i) {
			argfqc += ',' + gsdlGetValue(document.QueryForm.fqc[i]);
		}
	}
}


function clearfqc() {
	var i;
	if (cgiarg.fqn == 2) {
		document.QueryForm.fqc.options[0].selected = true;
	}
	else {
		for (i = 0; i < document.QueryForm.fqc.length; ++i) {
			document.QueryForm.fqc[i].options[0].selected = true;
		}
	}
	updatefqc();
}

// fqv - the query word/phrase text box
function initfqv() {
	var i;
	var fqv= argfqv.split(",");
	for (i=0; i < fqv.length; i++) {
		if (i == fqvtextidx) {
			var ligatureReg = new RegExp(String.fromCharCode(7527) + '(AND|OR|NOT|NEAR)' + String.fromCharCode(7527), 'g');
			gsdlInitValue(document.QueryForm.fqv[i], fqv[i].replace(ligatureReg, '$1'));
		} else {
			gsdlInitValue(document.QueryForm.fqv[i], fqv[i]);
		}
	}
	updatefqv();
}

function gsdlQueryEncode(text) {
	return text;
}

function updatefqv() {
	var i;
	argfqv = '';
	for (i = 0; i < document.QueryForm.fqv.length; ++i) {
		if (i > 0) argfqv += ',';
		argfqv += gsdlQueryEncode(mgpp_format(gsdlGetValue(document.QueryForm.fqv[i]).gsdl_clean()));
	}
}

// convert commas and spaces to plus
function mgpp_format(string) {
	var str = '' + string;
	var out = '', flag = 0;
	var j;
	for (j = 0; j < str.length; j++) {
		if (str.charAt(j) == ' ' || str.charAt(j) == ',') {
			if (flag == 0) {
				out += ' ';
				flag = 1;
			}
		}
		else {
			out += str.charAt(j);
			flag = 0;
		}
	}
	return out;
}

function clearfqv() {
	var i;
	for (i = 0; i < document.QueryForm.fqv.length; ++i) {
		document.QueryForm.fqv[i].value = '';
	}
	updatefqv();
}

// fqs - the stemming checkboxes
function initfqs() {
	var i;
	fqs = argfqs.split(',');
	for (i = 0; i < fqs.length; ++i) {
		gsdlInitValue(document.QueryForm.fqs[i], fqs[i]);
	}
	updatefqs();
}

function update_exactmatch(field_idx) {
	var sValue = gsdlGetValue(document.QueryForm.s);
	if (sValue == '1') sValue = '0';
	else sValue = '1';
	gsdlInitValue(document.QueryForm.fqs[field_idx], sValue);
	updatefqs();
}

function updatefqs() {
	argfqs = '';
	argfqs += gsdlGetValue(document.QueryForm.fqs[0]);
	var i;
	for (i = 1; i < document.QueryForm.fqs.length; ++i) {
		argfqs += ',' + gsdlGetValue(document.QueryForm.fqs[i]);
	}
}

function clearfqs() {
	var i;
	for (i = 0; i < document.QueryForm.fqs.length; ++i) {
		document.QueryForm.fqs[i].checked = false;
	}
	updatefqs();
}

// kqk - the casefolding checkboxes
function initfqk() {
	var i;
	fqk = argfqk.split(',');
	for (i = 0; i < fqk.length; ++i) {
		gsdlInitValue(document.QueryForm.fqk[i], fqk[i]);
	}
	updatefqk();
}


function updatefqk() {
	argfqk = '';
	argfqk += gsdlGetValue(document.QueryForm.fqk[0]);
	var i;
	for (i = 1; i < document.QueryForm.fqk.length; ++i) {
		argfqk += ',' + gsdlGetValue(document.QueryForm.fqk[i]);
	}
}

function clearfqk() {
	var i;
	for (i = 0; i < document.QueryForm.fqk.length; ++i) {
		document.QueryForm.fqk[i].checked = false;
	}
	updatefqk();
}

// q - the advanced query box
function initq() {
	updateq();
}

function updateq() {
	argq = '';
	argq += gsdl_encode(document.QueryForm.q.value);
}

function clearq() {
	document.QueryForm.q.value = '';
}

// convert commas and spaces to plus
function format(string) {
	var str = '' + string;
	var out = '', flag = 0;
	var j;
	for (j = 0; j < str.length; j++) {
		if (str.charAt(j) == ' ' || str.charAt(j) == ',') {
			if (flag == 0) {
				out += ' ';
				flag = 1;
			}
		}
		else {
			out += str.charAt(j);
			flag = 0;
		}
	}
	return out;
}

function submitSearch() {
	updatefqv(); updatefqf(); updatefqs(); updatefqk(); updatefqc();
	beginPostSearch();
	return false;
}

function beginPostSearch() {
	if (argfqv == ',,') 
		return false;
	var queryForm = gsdlGetElement('QueryPostForm');
	g_gsdlPreferenceState.SetFormArgs(queryForm);
	getstdargs(queryForm);
	getsearchargs(queryForm);
	queryForm.submit();
	return false;
}

function beginSearch() {
	if (argfqv == ',') 
	return false;
	gsdl_goto(gsdlQueryURL + '?' + getstdargs() + advformargs + getsearchargs());
}

function runQuery() {
	gsdl_goto(gsdlQueryURL + getstdargs() + advformargs + getqueryargs());
}

function clearForm() {
	clearfqf();
	clearfqv();

	if (cgiarg.b == '1') {
		clearfqk();
		clearfqs();
		clearfqc();
	}
}

function clearFilters() {
	var i;
	argfqv = '';
	argfqv += gsdl_encode(format(gsdlGetValue(document.QueryForm.fqv[0])));
	for (i = 1; i < document.QueryForm.fqv.length; ++i) {
		argfqv += ",";
		if (i == 1) {
			argfqv +=  gsdl_encode(format(gsdlGetValue(document.QueryForm.fqv[i]))); 
		}
	}
	updatetxti(); updatefqf(); updatefqs(); updatefqk(); updatefqc();
	beginSearch();
	return false;
}

function getStyleObject(objectId) {
	// cross-browser function to get an object's style object given its id
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
	// get a reference to the cross-browser style object and make sure the object exists
	var styleObject = getStyleObject(objectId);
	if(styleObject) {
		styleObject.visibility = newVisibility;
		return true;
	} else {
		// we couldn't find the object, so we can't change its visibility
		return false;
	}
} // changeObjectVisibility

function changeObjectText(objectId, newText) {
	var obj;
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		obj = document.getElementById(objectId);
		obj.innerHTML = newText;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		obj = document.all(objectId);
		obj.innerHTML = newText;
	} else if (document.layers) {
		obj = document.layers[objectId];
		obj.document.open();
		obj.document.write(newText);
		obj.document.close();
	}
}

function initQueryCompareCountry() {
	var i, j;
	for(i = 1; i < document.QueryForm.radio.length; ++i) {
		var objSub = eval('document.QueryForm.substances' + i);
		objSub.disabled = true;
	}
	var strCurrentValue = gsdlGetValue(document.QueryForm.fqv[fqvtextidx - 1]);
	var strValue = '';
	var objComp = getQueryFormCompField();
	gsdlSetRadioValue(objComp, cgiarg.comp);
	savedSubstances = new Array(document.QueryForm.radio.length - 1);
	for(i = 0; i < document.QueryForm.radio.length - 1; ++i) {
		var objSub = eval('document.QueryForm.substances' + i);
		savedSubstances[i] = new Array(objSub.options.length);
		for(j = 0; j < objSub.options.length; ++j)
			savedSubstances[i][j] = objSub.options[j];
	}
	for(i = 0; i < document.QueryForm.radio.length; ++i) {
		strValue = document.QueryForm.radio[i].value;
		if (strValue == strCurrentValue) {
			document.QueryForm.radio[i].checked = true;
			var objSub = eval('document.QueryForm.substances' + i);
			strValue = gsdlGetValue(document.QueryForm.fqv[fqvtextidx]);
			var re = new RegExp('^"', '');
			strValue = strValue.replace(re, '');
			re = new RegExp('"$', '');
			strValue = strValue.replace(re, '');
			if (cgiarg.comp == 1)
				gsdlInitValue(objSub, strValue);
			else
				gsdlInitSelectText(objSub, strValue);
			generateList(document.QueryForm.radio[i]);
			break;
		}
	}
	OnSelectCountry();
}

function searchWords(strText, strWhere) {
	if (typeof(strWhere) == 'undefined' || strWhere == null) strWhere = 'TI';
	strText = mgpp_format(strText).gsdl_clean();
	if (strText.length == 0) {
		return false;
	}

	var frmQueryPost = gsdlGetElement('QueryPostForm');
	g_gsdlPreferenceState.SetFormArgs(frmQueryPost);

	frmQueryPost.fqf.value = 'TI,' + strWhere + ',KE';
	frmQueryPost.fqv.value = ',' + strText + ',';
	frmQueryPost.fqk.value = '0,1,0';
	frmQueryPost.fqs.value = '0,0,0';
	frmQueryPost.fqc.value = 'and,and,and';
	frmQueryPost.g.value = 'Sec';
	var date = new Date();
	frmQueryPost.uq.value = '' + date.getTime();
	frmQueryPost.submit();

	return false;
}

function searchCountry (strCountry1, strCountry2, strSU, strKE) {
	var frmQueryPost = gsdlGetElement('QueryPostForm');
	frmQueryPost.action = gsdlURL + cgiarg.l + '/qcomp/';
	g_gsdlPreferenceState.SetFormArgs(frmQueryPost);

	frmQueryPost.comp.value = '1';
	frmQueryPost.ac.value = strCountry2;
	frmQueryPost.ac1.value = strCountry1;
	frmQueryPost.ac2.value = strCountry2;
	frmQueryPost.fqf.value = 'CU,SU,KE';
	frmQueryPost.fqv.value = strCountry1 + ',' + strSU + ',' + strKE;
	frmQueryPost.fqk.value = '0,0,1';
	frmQueryPost.fqs.value = '0,0,0';
	frmQueryPost.fqc.value = 'and,and';
	frmQueryPost.g.value = 'Sec';
	var date = new Date();
	frmQueryPost.uq.value = '' + date.getTime();
	frmQueryPost.submit();

	return false;
}

