//--------------------------------------------------------------
function awSearch(sID,text)
	{
	if ((text == "") || (text == "Search") || (text == "Keywords")) 
		{
		//document.forms[0].action = rootpath + "/Default.aspx?sID=" + sID;
		document.location = rootpath + "/Default.aspx?sID=" + sID;
		}
	else 
		{
		//document.forms[0].action = rootpath + "/Default.aspx?sID=" + sID + "&Search=" + text;
		document.location = rootpath + "/Default.aspx?sID=" + sID + "&Search=" + text;
		}
	//document.forms[0].submit();
	}
	
//--------------------------------------------------------------
function awAdvSearch(sID,text,secs,cats)
	{
	if ((text == "") || (text == "Search") || (text == "Keywords")) 
		{
		//document.forms[0].action = rootpath + "/Default.aspx?sID=" + sID;
		document.location = rootpath + "/Default.aspx?sID=" + sID;
		}
	else 
		{
		s = "&Search=" + text
		if (secs != "") {s = s + "&SectionList=" + secs;}
		if (cats != "") {s = s + "&CategoryList=" + cats;}
		//document.forms[0].action = rootpath + "/Default.aspx?sID=" + sID + s;
		document.location = rootpath + "/Default.aspx?sID=" + sID + s;
		}
	//document.forms[0].submit();
	}
	
//--------------------------------------------------------------
function awRegister(sID,text,prompt)
	{
	if ((text == "") || (text == prompt) ) 
		{
		//document.forms[0].action = rootpath + "/Default.aspx?sID=" + sID;
		document.location = rootpath + "/Default.aspx?sID=" + sID;
		}
	else 
		{
		//document.forms[0].action = rootpath + "/Default.aspx?sID=" + sID + "&Reg=" + text;
		document.location = rootpath + "/Default.aspx?sID=" + sID + "&Reg=" + text;
		}
	//document.forms[0].submit();
	}

//--------------------------------------------------------------
function awReplace(argvalue, x, y) {
	if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
		return false;  
		}    
	while (argvalue.indexOf(x) != -1) {
		var leading = argvalue.substring(0, argvalue.indexOf(x));
		var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, argvalue.length);    
		argvalue = leading + y + trailing;  
		}  
		return argvalue;
	}
//--------------------------------------------------------------
function awNewWindow(url,name,param) {
	window.open(url,name,param);
	return;
	} 
	
//--------------------------------------------------------------
// Form validation 
//--------------------------------------------------------------

	var toComplete0 = "Please enter the following details\n(you can insert 'None' in Company field):\n\n";
	var incomplete="no";

	/* 
	define fields to validate at the page
	 
	var TextboxToValidate = ["Forename", "Surname","Password"];
	var TextboxDefaultValue = ["Forename", "Surname", ""];
	var TextboxLength = [2, 3, 5];
	var TextboxAlertMsg = ["Forename (should have at least 2 symbols)", "Surname (should have at least 3 symbols)", "Password (should have at least 5 symbols)"];
	var EmailToValidate = "Email"
	var PasswordToValidate = ["Password", "Password1"];
	*/

	function awValidateForm (thisform)
	{
	
	toComplete = toComplete0;
	
	var i, j;
		for (i=0; i < TextboxToValidate.length; i++)
		{
			for (j=0; j < thisform.elements.length; j++)
			{	
				if (TextboxToValidate[i] == thisform.elements[j].name)
				{
					if ((thisform.elements[j].value == "") || (thisform.elements[j].value == TextboxDefaultValue[i]) || (thisform.elements[j].value.length < TextboxLength[i]))
					{
					toComplete+=TextboxAlertMsg[i]+"\n";
					incomplete="yes";
					}
				}
			}
		}
		checkEmail(thisform, EmailToValidate);
		checkPassword(thisform, PasswordToValidate);
		
		if (incomplete == "no")
		{
			thisform.submit();
		}
		else
		{
			alert(toComplete);
			incomplete = "no";
			toComplete = toComplete0;
			return;	
		}
	}

	function checkEmail(thisform, email)
	{
	if (email != "") {
	for (x=0; x < thisform.elements.length; x++)
	{	
		if (thisform.elements[x].name == email)
			{
			emailStr = thisform.elements[x].value;
			}
	}	
	//if (emailStr != "")
	//{
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
		if (!emailReg1.test(emailStr) && emailReg2.test(emailStr)) // if syntax is valid
			return true;
		else
			{
			toComplete+="Valid Email address\n";
			incomplete="yes";
			}
		return true;
		}
	//}
	}

	function checkPassword(thisform, pswpair)
	{
	var i;
	var psw, psw1;

	if (pswpair.length = 2) {
	for (i=0; i < thisform.elements.length; i++)
	{	
		if (thisform.elements[i].name == pswpair[0])
			{
			psw = thisform.elements[i].value;
			}
	}
	for (i=0; i < thisform.elements.length; i++)
	{	
		if (thisform.elements[i].name == pswpair[1])
			{
			psw1 = thisform.elements[i].value;
			}
	}	

	if (psw == psw1) 
		return true;
	else
		{
		toComplete+="Both password fields should be the same\n";
		incomplete="yes";
		}
	return true;
	}
	}
