var strPunc = "!&:;,.=£%#@_><";
var strAll = " -+\t\"'?*" + strPunc;
function fnStrReplace(strString, strReplace, strWith)
{
	var i=0, strSource = strString;
	while ((i = strSource.indexOf(strReplace,i)) != -1)
		if(i) strSource = strSource.substring(0, i) + strWith + strSource.substring(i + strReplace.length, strSource.length)
		else strSource = strWith + strSource.substring(i + strReplace.length, strSource.length);
	return strSource;
}
function fnWildCard(strString, strWildCard)
{
	var j,k,i = 0;
	var c;
	while ((i < strString.length) && ((i = strString.indexOf(strWildCard,i)) != -1)) {
		k=0;
		for(j=i-1; j > -1; j--) {
			c = strString.charAt(j);
			if(strAll.indexOf(c) != -1) j = -1
			else k++;
		}
		if(k < 3) return "\nYou cannot use the " + strWildCard + " wildcard unless at least 3 characters precede it. e.g. nor" + strWildCard + "\n";
		i++
	}
	return '';
}
function ValidateField(strSearchText)
{
	var strText = strSearchText.value + " ";
	for(var i = 0;i < strPunc.length;i++) strText = fnStrReplace(strText,strPunc.charAt(i),' ');
	strText = fnStrReplace(strText,'\t',' ');
	strText = fnStrReplace(strText,'+ ',' ');
	strText = fnStrReplace(strText,'- ',' ');
	strText = fnStrReplace(strText,'  ',' ');
	while(strText.charAt(strText.length-1) == ' ') strText = strText.substring(0,strText.length - 1);
	while(strText.charAt(0) == ' ') strText = strText.substring(1,strText.length);
	strSearchText.value = strText
	if(strText.length) return fnWildCard(strText,'*') + fnWildCard(strText,'?');
	return '';
}

function ValidateForm(pstrFrom)
{
	var i;
	if (pstrFrom == 'searchoption')
	{

		for (i = 1; i < document.getElementsByName('subs').length; i++) {
			if(document.getElementsByName('subs')[i].checked == true)
				break;
		}
		if(i== document.getElementsByName('subs').length)
		{
			alert("Select at least one subject and proceed.");
			return false;
		}
		for (i = 1; i < document.getElementsByName('age').length; i++) {
			if(document.getElementsByName('age')[i].checked == true)
				break;
		}
		if(i== document.getElementsByName('age').length)
		{
			alert("Select at least one resource rating and proceed.");
			return false;
		}
		for (i = 1; i < document.getElementsByName('retype').length; i++) {
			if(document.getElementsByName('retype')[i].checked == true)
				break;
		}
		if(i== document.getElementsByName('retype').length)
		{
			alert("Select at least one resource type and proceed.");
			return false;
		}
	}

	var strErrors = '';
	strErrors = ValidateField(document.frmSearch.txtSearchText)
	if(!((strErrors.length) || (document.frmSearch.txtSearchText.value.length))) strErrors = '\nSorry but you did not enter any words on which to search\n';
	if (strErrors.length) {
		alert(strErrors + '\nPlease alter your search and try again.\n');
		fnGiveFocus();
		return false;
	}
	else return true;
}

function fnGiveFocus()
{
	document.frmSearch.txtSearchText.focus();
}

function go(pstrFrom) {
	if(!ValidateForm(pstrFrom)) return false;
	if(document.frmSearch.txtSearchText.value == "Enter search")
	{
		alert("Please alter your search and try again.");
		fnGiveFocus();
		return false;
	}
	document.frmSearch.submit();
	event.returnValue = false;
	//This line is included to resolve the open in new window issue
	//if this line is not there then onclick the page is not getting submitted
}