// Over-ride string functions for some additional processing


// Trim mutliple spaces
function trimSpace (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/\s+/g, ' ');
}

// Replace IDEOGRAPHIC SPACE ("\u3000") with standard space "\u0020"
function ReplaceJPSpace (str) {
	str = this != window? this : str;
	var cul = getCookie("IntelSearch.Culture");
	if (cul != null && cul == "ja-JP") {
		return (str.replace(/\u3000/g, '\u0020'));
	} else {
		return str;
	} 
}

// Assign functions to string prototype
String.prototype.trim = trimSpace;
String.prototype.replaceJPSpace = ReplaceJPSpace;


function UncheckAll(obj, chkBoxObj)
{
	if (obj.checked) {
		// Check to see if it's a single checkbox or a checkbox array
		if (chkBoxObj.length) {
			for(var i=0;i<chkBoxObj.length;i++)
			{
 				chkBoxObj[i].checked = false;
			}
		} else {
			chkBoxObj.checked = false;
		}
	}
}

function SubmitSearch(enterTerm){

	// Set parameter value
	//if (document.gs.chkSites != null) {
	//	var sub = ConstructValue(document.gs.chkSites, ' OR ');
	//	document.gs.site.value = AppendSubCollection(sub);
	//} 
	
	
	// Removed and added to default page as this check fails anyway.
	if (document.gs.chkFileFormats != null){
		document.gs.fileType.value = ConstructValue(document.gs.chkFileFormats, ' OR ');
	}

	// Format query strings
	var stanardQ = document.gs.as_q.value.trim().replaceJPSpace();
	var exactQ = document.gs.as_epq.value.trim().replaceJPSpace();
	var aryAnyQ;
	var anyQ = document.gs.as_oq.value.trim().replaceJPSpace();
	var aryNotQ;
	var notQ = document.gs.as_eq.value.trim().replaceJPSpace();
	
	if (exactQ!= ""){
		exactQ = ' "' + exactQ + '"';
	}
	
	if (anyQ != "") {
		aryAnyQ = anyQ.split('\u0020');
		anyQ = " " + aryAnyQ.join(" OR ");
	}
		
	if (notQ != "") {
		aryNotQ = notQ.split('\u0020');
		notQ = " ";
		for (var i=0; i < aryNotQ.length; i++) {
			notQ = notQ + " NOT " + aryNotQ[i];
		}		
	}

	document.gs.q.value = stanardQ + exactQ + anyQ + notQ;
	
	if (document.gs.q.value == "") {
		alert(enterTerm);
		return false;
	} else {
		// Set number of result cookie
		document.cookie = "IntelSearch.NumofResults=" + escape(document.gs.num.value);
		return true;
	}
}


// Support functions

function ConstructValue(chkBoxObj, joinString){
	// Check to see if it's a single checkbox or a checkbox array
	if (chkBoxObj.length) {
		// build an array of values of checked boxes
		if (chkBoxObj != null) {
			var checked=new Array();
			for(i=0;i<chkBoxObj.length;i++){
				if(chkBoxObj[i].checked){
					checked[checked.length]=chkBoxObj[i].value;
				}
			}
			// Construct the value string
			if(checked.length){
				return checked.join(joinString);
			}  
		}
    } else if (chkBoxObj.checked) {
		return chkBoxObj.value;
	}
    
     return ''; 
}

function AppendSubCollection(sub) {
	var culture = getCookie("IntelSearch.Culture");
		
	if (culture != '') {
		if (sub == '') {
			return "P022_" + culture;
	    } else {
	        return "(" + sub + ") AND " + "P022_" + culture;
	    }
	} else {
	   return sub;
	}
} 


// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// Retrieve values from querystring (used when cookie is disabled by browser)
function Querystring()
{
// get the query string, ignore the ? at the front.
	var querystring=location.search.substring(1,location.search.length);

// parse out name/value pairs separated via &
	var args = querystring.split('&');

// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		name = temp.join(' ');

		temp = unescape(pair[1]).split('+');
		value = temp.join(' ');

		this[name]=value;
	}

	this.get=Querystring_get;
}


function Querystring_get(strKey,strDefault)
{
	var value=this[strKey];
	if (value==null)
	{
		value=strDefault;
	}

	return value;
}



