//Function to clear the form example values ...
function resetForm()
{
	
	for (var i = 0; i<document.submitForm.elements.length; i++)
	{
		//Ignore LEGEND AND FIELDSET
		if(document.submitForm.elements[i].name != null)
		{
		
        		if ((document.submitForm.elements[i].name.indexOf('text') == 0) ||
			(document.submitForm.elements[i].name.indexOf('num') == 0))
			{
            			document.submitForm.elements[i].value ="";
        		}
		}
   	}

}


function handleSubmit(caller)
{
	if(validateForm() && confirm("Are you sure?"))
		return true;
	else
		return false;	
		
}


function validateForm()
{
	
	if(! EmailValid(document.submitForm.text_email.value))
	{
		alert("Invalid Email Address")
		document.submitForm.text_email.select();
		return false;
	}

	if(! PhoneValid(document.submitForm.text_phone.value))
	{
		alert("Invalid Phone Number")
		document.submitForm.text_phone.select();
		return false;
	}

	if(! NameValid(document.submitForm.text_name.value))
	{
		alert("Invalid Name")
		document.submitForm.text_name.select();
		return false;
	}

	if(! AddressValid(document.submitForm.text_address.value))
	{
		alert("Invalid Address")
		document.submitForm.text_address.select();
		return false;
	}

	
	return true;
}


/* validate email address*/
function EmailValid(EmailID)
{

   var ValidEmail = new
   	RegExp("^(([a-z_0-9-']+)|([a-z_0-9-']+[\.]([a-z_0-9-']+[\.]){0,}[a-z_0-9-']{1,}))[@]((([a-z_0-9-']+)[\.]){1,})(([a-z_0-9-']+))$","ig");

   return ValidEmail.test(EmailID);

}


/* validate phone number */
function PhoneValid(PhoneNumber)
{

	var ValidPhone = new RegExp("^([0-9 ]+)$","ig");
	return ValidPhone.test(PhoneNumber);
}

/* validate name */
function NameValid(NameID)
{

	var ValidName = new RegExp("^([a-z '\-]+)$","ig");
	return ValidName.test(NameID);
}

/* validate address */
function AddressValid(AddressID)
{

	var ValidAddress = new RegExp("^([a-z_0-9 ',.\r\n\-]+)$","ig");
	return ValidAddress.test(AddressID);
}


function handleCheck(caller)
{
	if(caller == "keyboard")
	{
 		var whichKey = String.fromCharCode(event.keyCode).toLowerCase();
		var altDown = event.altKey;

		if((altDown && whichKey == "s") && validateCheckForm())
			document.submitForm.submit();
	}
	
	if((caller == "mouse") && validateCheckForm())
		document.submitForm.submit();
}


function validateCheckForm()
{
	if(! UserIDValid(document.submitForm.text_userid.value))
	{
		alert("Invalid UserID")
		document.submitForm.text_userid.select();
		return false;
	}

	if(! PasswordValid(document.submitForm.text_pass.value))
	{
		alert("Invalid Pass Word")
		document.submitForm.text_pass.select();
		return false;
	}	

	return true;
}

/* validate user id*/
function UserIDValid(UserID)
{

	if(UserID.indexOf("@seeingdogs.org.uk") == -1)
		return false;

	var ValidUserID = new
	RegExp("^(([a-z_0-9-']+)|([a-z_0-9-']+[\.]([a-z_0-9-']+[\.]){0,}[a-z_0-9-']{1,}))[@]((([a-z_0-9-']+)[\.]){1,})(([a-z_0-9-']+))$","ig");

	
	return ValidUserID.test(UserID);   
}


/* validate password*/
function PasswordValid(pass)
{
	return pass.length >= 3;
}


function handleNews(caller)
{
	
	if(caller == "keyboard")
	{
 		var whichKey = String.fromCharCode(event.keyCode).toLowerCase();
		var altDown = event.altKey;

		if((altDown && whichKey == "s") && validateNewsForm())
			document.submitForm.submit();
			
	}
	
	if((caller == "mouse") && validateNewsForm())
		document.submitForm.submit();
}


function validateNewsForm()
{
	if(! TopicTitleValid(document.submitForm.text_topic.value))
	{
		alert("Invalid Title - Must be at least 3 characters")
		document.submitForm.text_topic.select();
		return false;
	}

	if(! TopicDetailsValid(document.submitForm.text_content.value))
	{
		alert("Invalid Topic Details  - Must be at least 20 characters")
		document.submitForm.text_content.select();
		return false;
	}
	
	return true;	
}


/* validate topic title*/
function TopicTitleValid(title)
{
	return title.length >= 3;
}

/* validate topic details*/
function TopicDetailsValid(topic)
{
	return topic.length >= 20;
}


function ConfirmNewsDelete(itemTopic, itemNo)
{
	if(confirm("Are you sure you wish to delete '" + itemTopic +"' ?"))
		window.navigate("delete_news.asp?id=" + itemNo);
}
