/*
Following scripts are used by the search facility.
Functions perform Validation and string trimming 
*/

function formSubmit()
{	
//call form validate, if that returns true, post form
	if (formValidate())
	{
		//trim any leading and traiiling spaces
		//document.frmSearch.SearchValue.value = document.frmSearch.SearchValue.value.replace(/^\s*|\s*$/,'');
		document.frmSearchHeader.qt.value = Trim(document.frmSearchHeader.qt.value);
		return true;
	}
	else
	{
		return false;
	}

}
	
function formValidate()
{
	var boolIsValid = true;
	var strSearchValue = new String(document.frmSearchHeader.qt.value);
	var re  =  new RegExp ("^[a-zA-Z 0-9]+$") //Only allow Alphanumerics
	
	//Check an empty string has not been supplied
	if (strSearchValue.length < 1){
		alert("You must enter a valid search string");
		document.frmSearchHeader.qt.focus();
		document.frmSearchHeader.qt.select();
		boolIsValid = false;
		return boolIsValid;
	}
			
	//if valid chars are present return true else return false
	if (strSearchValue.match(re)){
		boolIsValid = true;
		return boolIsValid;
	}
	else
	{
		alert("Only alphabetic and numeric characters are allowed");
		boolIsValid = false;
		return boolIsValid;
	}
	
	return boolIsValid;
}	
	
function Trim(String)
{
	if (String == null)
		return (false);

	return RTrim(LTrim(String));
}


function RTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for(j = String.length - 1; j >= 0; j--)
	{
		if (String.substr(j, 1) != ' ' &&
			String.substr(j, 1) != '\t')
		break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}


function LTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for (i = 0; i < String.length; i++)
	{
		if (String.substr(i, 1) != ' ' &&
		    String.substr(i, 1) != '\t')
			break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}	


// This is called from the LiveSite Form Component and is a very simple validator 
//to check if the user entered data into the field. 

function checkLSForm(form) 
{
	// Iterate through each field in the form
	for (var i = 0; i < form.elements.length; i++) 
	{
		// break the field into its parts
		//keyvalue_Your e-mail_text_Y
		var keyParts = new Array();
		keyParts = form.elements[i].name.split( "_" );
		// Check if the field is required
		if (keyParts[3] == "Y") 
		{	
			// Check what type of field it is
			if (keyParts[2] == "text" || keyParts[2] == "textarea") 
			{	
				// Check if the field has a value
				if (form.elements[i].value == "" ) 
				{
					alert("Fill out all required fields.")
					return false
				}
			}
		}
	}
	return true;
}

function popwindow( aString )
{
	var obj = document.getElementById( 'externalAddress' );
	var curX = "500px";
	var curY = "600px";
	var outputString = "<div id='externalAddress' style='position: absolute; background: #ffffff; border: 1px solid #000000; left: " + curX + "; top: " + curY + "'>";
	outputString += "<div style='background: #000080; width: 200px;'><div style='float: right;'><a onclick='closePopup();'><img src='events_files/close.gif' alt='click here to close this popup' border='0'/></a></div></div>";
	outputString += "<div id='extAddStringHere' style='clear: both; padding: 3px 3px 2px 3px;'>" + aString + "</div>";
	outputString += "</div>";
	
	if ( obj )
	{
		var newObject = document.getElementById( 'extAddStringHere' );
		newObject.innerHTML = aString;
		obj.style.visibility = 'visible';
	}
	else
	{
		document.body.innerHTML += outputString;
	}
}

function closePopup()
{
	var obj = document.getElementById( 'externalAddress' );
	obj.style.visibility = 'hidden';
}
