var strNewsPunc = "!&:;,.=£%#@_><";
var strNewsAll = " -+\t\"'?*" + strNewsPunc;
function funcStrReplace(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 funcWildCard(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(strNewsAll.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 clearTxtbox()
{
	if (document.frmNewsSearch.txtSearchText.value == "Enter search")
	 {
		document.frmNewsSearch.txtSearchText.value = '';
	 }
}
function ValidateTextField(strSearchText)
{
	var strText = strSearchText.value + " ";
	for(var i = 0;i < strNewsPunc.length;i++) strText = funcStrReplace(strText,strNewsPunc.charAt(i),' ');
	strText = funcStrReplace(strText,'\t',' ');
	strText = funcStrReplace(strText,'+ ',' ');
	strText = funcStrReplace(strText,'- ',' ');
	strText = funcStrReplace(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 funcWildCard(strText,'*') + funcWildCard(strText,'?');
	return '';
}

function ValidateNewsForm()
{
	var strErrors = '';
	strErrors = ValidateTextField(document.frmNewsSearch.txtSearchText)
	if(!((strErrors.length) || (document.frmNewsSearch.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');
		GetFocus();
		return false;
	}
	else return true;
}

function GetFocus()
{
	document.frmNewsSearch.txtSearchText.focus();
}

function GoSearchNewspaper() 
{
	if(!ValidateNewsForm()) return false;
	if (document.frmNewsSearch.txtSearchText.value == "Enter search")
	{
		alert("Please alter your search and try again.");
		GetFocus();
		return false;
		
	}
	document.frmNewsSearch.submit();
	event.returnValue = false;
}


