/* Show and Hide the refine_search options */
function sr_toggle_refine(){
	if(document.getElementById("refine_search").style.display == "none"){
		display_refine();
	}else{
		hide_refine();
	}
}

function display_refine(){
	document.getElementById("refine_search").style.display = "block";
	document.getElementById("search_target").style.display = "none";
	document.getElementById("refine_link").style.backgroundImage = "url(icn_up_arrow_blk.gif)";
}

function hide_refine(){
	document.getElementById("refine_search").style.display = "none";
	document.getElementById("search_target").style.display = "block";
	document.getElementById("refine_link").style.backgroundImage = "url(icn_right_arrow_blk.gif)";
}

/*global search functions*/
var GE_okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_ \"-";
var GE_hexchars = "0123456789ABCDEF";

function GE_toHex(n) {
	return GE_hexchars.charAt(n>>4)+GE_hexchars.charAt(n & 0xF);
}


function GE_encodeURIComponentNew(s) {
	if(s==null || s.length<=0)
		return s;
	var s = GE_utf8(s);
	var c;
	var enc = "";
	for (var i= 0; i<s.length; i++) {
		if (GE_okURIchars.indexOf(s.charAt(i))==-1)
			enc += "%"+GE_toHex(s.charCodeAt(i));
		else
			enc += s.charAt(i);
	}
	return enc;
}

function GE_utf8(wide) {
	var c, s;
	var enc = "";
	var i = 0;
	while(i<wide.length) {
		c= wide.charCodeAt(i++);
		if(c>=0xDC00 && c<0xE000)
			continue;
		if(c>=0xD800 && c<0xDC00) {
			if(i>=wide.length)
				continue;
			s= wide.charCodeAt(i++);
			if(s<0xDC00 || c>=0xDE00)
				continue;
			c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
		}
		if (c<0x80)
			enc += String.fromCharCode(c);
		else if (c<0x800)
			enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
		else if (c<0x10000)
			enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
		else
			enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
	}
	return enc;
}

GE_Main.addInitFn(
	function() {
		var formEl = $('search');
		if (formEl) {
	   		Element.observe(formEl,'submit',function(e){
	   			var tEl = formEl.textToSearch;
	   			new Insertion.After( tEl, '<input type="hidden"/>');
	   			var nEl = tEl.next();
	   			tEl.name = "";
	   			nEl.name = "textToSearch";
	   			nEl.value = GE_encodeURIComponentNew(tEl.value);
	   		}.bind(formEl));
		}
	}
);
