
function trim(str) {
  while (str.charAt(str.length - 1)==" ")
    str = str.substring(0, str.length - 1);
  while (str.charAt(0)==" ")
    str = str.substring(1, str.length);
  return str;
}


function IsEmpty(aTextField) {
	if(aTextField==null) return true;
	if(aTextField.value==null) return true;
	if(trim(aTextField.value).length==0) return true;
    return false;
}	

function isValidUsername(s)
{   
	var i;

	if ((s.length < 4) || (s.length > 32)) return false;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (isLetter(c)) continue;
		if (i == 0) return false; // first sign must be letter
		if (isDigit(c)) continue; 
		if (c == ".") continue; 
		if (c == "-") continue; 
		if (c == "_") continue; 
		return false;
    }
    return true;
}

function echeck(str) { 

	var at="@" 
	var dot="." 
	var lat=str.indexOf(at) 
	var lstr=str.length 
	var ldot=str.indexOf(dot) 
	if (str.indexOf(at)==-1){ 
	alert("A valid e-mail address is required. Please amend and retry") 
	return false 
	} 
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ 
	alert("A valid e-mail address is required. Please amend and retry") 
	return false 
	} 
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ 
	alert("A valid e-mail address is required. Please amend and retry") 
	return false 
	} 
	
	if (str.indexOf(at,(lat+1))!=-1){ 
	alert("Invalid E-mail ID") 
	return false 
	} 
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ 
	alert("A valid e-mail address is required. Please amend and retry") 
	return false 
	} 
	
	if (str.indexOf(dot,(lat+2))==-1){ 
	alert("A valid e-mail address is required. Please amend and retry") 
	return false 
	} 
	
	if (str.indexOf(" ")!=-1){ 
	alert("A valid e-mail address is required. Please amend and retry") 
	return false 
	} 
	
	return true 
} 


function ValidateFeedback() 
{
	if (IsEmpty(document.forms.contactFrm.name)) 
	{
		alert('Please enter your Name') 
		document.forms.contactFrm.name.focus(); 
		return false;
	}
			
	if (IsEmpty(document.forms.contactFrm.email))
	{
		alert('Please enter your email Address') 
		document.forms.contactFrm.email.focus(); 
		return false;
	}
		
	if (echeck(document.forms.contactFrm.email.value)==false)
	{ 
		document.forms.contactFrm.email.focus() 
		return false 
	}
				
	if (IsEmpty(document.forms.contactFrm.enquiry))
	{
		alert('Please enter your enquiry') 
		document.forms.contactFrm.enquiry.focus(); 
		return false;
	}

	//alert('Thank you. Your query has been submitted and you will now be redirected to the main page.');
	return true;
}

function ValidateRegistration() 
{
	if (IsEmpty(document.forms.formRegister.org_name)) 
	{
		alert('Please enter the name of your organisation') 
		document.forms.formRegister.org_name.focus(); 
		return false;
	}
	if (IsEmpty(document.forms.formRegister.first_name)) 
	{
		alert('Please enter your first name') 
		document.forms.formRegister.first_name.focus(); 
		return false;
	}
	if (IsEmpty(document.forms.formRegister.last_name)) 
	{
		alert('Please enter your surname') 
		document.forms.formRegister.last_name.focus(); 
		return false;
	}
	if (IsEmpty(document.forms.formRegister.phone)) 
	{
		alert('Please enter a contact telephone number') 
		document.forms.formRegister.phone.focus(); 
		return false;
	}
	if (IsEmpty(document.forms.formRegister.email)) 
	{
		alert('Please enter a valid email address.') 
		document.forms.formRegister.email.focus(); 
		return false;
	}
	if (echeck(document.forms.formRegister.email.value)==false)
	{ 
		document.forms.formRegister.email.focus() 
		return false 
	}
	if (IsEmpty(document.forms.formRegister.address1)) 
	{
		alert('Please enter the postal address of your organisation') 
		document.forms.formRegister.address1.focus(); 
		return false;
	}
	if (IsEmpty(document.forms.formRegister.city)) 
	{
		alert('Please enter the city where your organisation is located') 
		document.forms.formRegister.city.focus(); 
		return false;
	}
	if (IsEmpty(document.forms.formRegister.postcode)) 
	{
		alert('Please enter the postcode of your organisation') 
		document.forms.formRegister.postcode.focus(); 
		return false;
	}
	if (IsEmpty(document.forms.formRegister.username)) 
	{
		alert('Please indicate your preferred school username') 
		document.forms.formRegister.username.focus(); 
		return false;
	}
	if (!isValidUsername(document.forms.formRegister.username.value)) 
	{
		alert('Please enter a username between 4 and 32 characters long,\nusing only the characters [a..z, A..Z, 0..9, -, _]\nand starting with a letter.') 
		document.forms.formRegister.username.focus(); 
		return false;
	}
			
	//alert('Thank you. Your query has been submitted and you will now be redirected to the main page.');
	return true;
}
