//	OnlineCounsellor™ Code Copyright©2005-2010 Practical Quality Research Plus Ltd.
//	AccessManager™ Database Tables Design (see Tables List) Copyright©2001-2010 DLZnet Ltd.
//	Tables List: Action, Authority, Authscript, DataType, Permission, Role, RoleModel, RoleType, Script, Status
//	AccessManager™ Code and other tables Copyright©2005-2010 Practical Quality Research Plus Ltd.
// -------------------------------------------------------------------------------------------------------------	
//	CUSTOMERS English
// -------------------------------------------------------------------------------------------------------------
//	CASES
// -------------------------------------------------------------------------------------------------------------

	function SendOnlineCase()
	{
	var form = document.getElementById("OnlineCase");
	var errorstr = "";
											
	if (! form.body.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Message must not be blank.\n\n";				
																						
	if (form.subjectid.value == 0)
		errorstr += "Subject must be selected.\n\n";				
																						
	if (form.agerangeid.value == 0)
		errorstr += "Age range must be selected.\n\n";					
																						
	if (form.sexid.value == 0)
		errorstr += "Gender must be selected.\n\n";			
											
	if (! form.postcode.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Postcode must not be blank.\n\n";				
						
	if (errorstr != "") 
		{	
		alert("Some values given are invalid or missing.\nPlease correct them and resubmit.\n\n" + errorstr);
		return false;
		}
		
	document.OnlineCase.submit();
	return true;
	}
	
// -------------------------------------------------------------------------------------------------------------	
			
	function AssignOnlineCase()
	{
	var form = document.getElementById("OnlineCase");
	var errorstr = "";
	
	if (form.counsellorid.options[0].selected)
		errorstr += "Please select an Advisor.\n\n";			
						
	if (errorstr != "") 
		{	
		alert("Some values given are invalid or missing.\nPlease correct them and resubmit.\n\n" + errorstr);
		return false;
		}

	document.OnlineCase.statuschange.value = "Assigned";
	document.OnlineCase.submit();																
	return true;
	}	
		
// -------------------------------------------------------------------------------------------------------------	
			
	function OfferOnlineCase()
	{
	var form = document.getElementById("OnlineCase");
	var errorstr = "";
	
	if (form.supervisorid.options[0].selected)
		errorstr += "Please select a Supervisor.\n\n";		
						
	if (errorstr != "") 
		{	
		alert("Some values given are invalid or missing.\nPlease correct them and resubmit.\n\n" + errorstr);
		return false;
		}
		
	document.OnlineCase.statuschange.value = "Offered";
	document.OnlineCase.submit();
	return true;
	}
	
// -------------------------------------------------------------------------------------------------------------	

// CASENOTES
	
// -------------------------------------------------------------------------------------------------------------

	function SaveCaseNote()
	{
	var form = document.getElementById("CaseNote");
	var errorstr = "";	
	
	if(! form.body.value)
		{
		if (form.incinclusionid.value == 0)	
			{								
			if (form.inclinkid.value == 0)
				errorstr += "CaseNote text must not be blank.\n\n";		
			}
		}
				
	if (errorstr != "") 
		{	
		alert("There is an error with this CaseNote.\nPlease correct it and resubmit.\n\n" + errorstr);
		return false;
		}
	document.CaseNote.submit();
	return true;
	}
		
// -------------------------------------------------------------------------------------------------------------	

// INCLUSIONS
	
// -------------------------------------------------------------------------------------------------------------

	function SaveInclusion()
	{
	var form = document.getElementById("Inclusion");
	var errorstr = "";
	
	if (! form.title.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Inclusion title must not be blank.\n\n";			
	
	if(! form.body.value)
		{
		if (form.incinclusionid.value == 0)	
			{								
			if (form.inclinkid.value == 0)
				errorstr += "Inclusion text must not be blank.\n\n";
			}
		}
				
	if (errorstr != "") 
		{	
		alert("There is an error with this Inclusion.\nPlease correct it and resubmit.\n\n" + errorstr);
		return false;
		}
	document.Inclusion.submit();
	return true;
	}
		
// -------------------------------------------------------------------------------------------------------------	

// LINKS
	
// -------------------------------------------------------------------------------------------------------------
	function SaveLink()
	{
	var form = document.getElementById("Link");
	var errorstr = "";
	var strTitle = form.title.value
	var strURL = form.url.value
	
	if (strURL.length > 100)
		errorstr += "Link URL must be no longer than 100 characters.\n\n";		
			
	if (strTitle.length > 100)
		errorstr += "Link text must be no longer than 100 characters.\n\n";			
	
	if (! form.url.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Link URL must not be blank.\n\n";				
	
	if (! form.title.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Link text must not be blank.\n\n";				
			
	if (errorstr != "") 
		{	
		alert("Some values given are invalid or missing.\nPlease correct them and resubmit.\n\n" + errorstr);
		return false;
		}

	document.Link.submit();
	return true;
	}
		
// -------------------------------------------------------------------------------------------------------------	

// MESSAGES
	
// -------------------------------------------------------------------------------------------------------------

	function SaveMessage()
	{
	var form = document.getElementById("Message");
	var errorstr = "";		
	
	if(! form.body.value)
		{
		if (form.inclusionid.value == 0)	
			{								
			if (form.linkid.value == 0)
				errorstr += "Message text must not be blank.\n\n";
			}
		}
				
	if (errorstr != "") 
		{	
		alert("There is an error with this Message.\nPlease correct it and resubmit.\n\n" + errorstr);
		return false;
		}
		
	document.Message.submit();
	return true;
	}
	
// -------------------------------------------------------------------------------------------------------------

	function SendMessage(sender)
	{
	var form = document.getElementById("Message");
	var errorstr = "";	
	var confirmed;			
	
	if (sender != "client")
		{			
		if(! form.body.value)
			{
			if (form.inclusionid.value == 0)	
				{								
				if (form.linkid.value == 0)
					errorstr += "Message text must not be blank.\n\n";
				}
			}
		}
	else
		{
		if (! form.body.value.match(/^[\w][\w\s\W]*$/))
			errorstr += "Message text must not be blank.\n\n";		
		}
			
	if (errorstr != "") 
		{	
		alert("There is an error with this Message.\nPlease correct it and resubmit.\n\n" + errorstr);
		return false;
		}
		
	confirmed = confirm("Are you sure you want to send this Message?\n\n");
	if (confirmed)
		{
		document.Message.submit();
		return true;
		}
	else
		{
		return false;
		}
	}
		
// -------------------------------------------------------------------------------------------------------------	

// CLIENT DETAILS & PERSONAL DETAILS

// -------------------------------------------------------------------------------------------------------------
		
	function SaveClientDetails(origin)
	{
	var form = document.getElementById("ClientDetails");
	var errorstr = "";
//	var strEmail=form.email.value;
//	var at="@";
//	var dot=".";
//	var lat=strEmail.indexOf(at);
//	var ldot=strEmail.indexOf(dot);
//	var lstr=strEmail.length;
	var returntype=form.returntype.value;
	form.origin.value = origin;		
	
//	alert(returntype);
//	return false;													
		
	if (returntype == "Client")
		{
		if (! form.loginname.value.match(/^[\w][\w\s\W]*$/))
			errorstr += "Loginname must be alphanumeric.\n\n";
					
		if (! form.password.value.match(/^[a-zA-Z0-9][a-zA-Z0-9\_]*$/))
			errorstr += "Password must be letters, numbers, or underline.\n\n";
				
		if (form.password.value.length > 18)
			errorstr += "Password must be less than 19 characters.\n\n";		
				
		if (form.relativetimezone.options[0].selected)
			errorstr += "Please select a Relative Time Zone.\n\n";			
					
//		if (! form.mobile.value.match(/^\d*$/))
//			errorstr += "Mobile number must be numeric (no spaces), or\nnot entered at all.\n\n";	

//		if (strEmail)
//			{
//			if (strEmail.indexOf(at)==-1 || strEmail.indexOf(at)==0 || strEmail.indexOf(at)==lstr)
//				errorstr += "Email @ is missing or in wrong position.\n\n";
//
//			if (strEmail.indexOf(dot)==-1 || strEmail.indexOf(dot)==0 || strEmail.indexOf(dot)==lstr-1)
//				errorstr += "Email dot[.] is missing or in wrong position.\n\n";
//
//			if (strEmail.indexOf(at,(lat+1))!=-1)
//				errorstr += "Email has more than one @.\n\n";
//
//			if (strEmail.substring(lat-1,lat)==dot || strEmail.substring(lat+1,lat+2)==dot)
//				errorstr += "Email has dot[.] just before or after @.\n\n";
//
//			if (strEmail.indexOf(dot,(lat+2))==-1)
//				errorstr += "Email has no dot[.] after @.\n\n";
//		
//			if (strEmail.indexOf(" ")!=-1)
//				errorstr += "Email must not include spaces.\n\n";
//			}	
					
		if (errorstr != "") 
			{	
			alert("Some values given are invalid or missing.\nPlease correct them and resubmit.\n\n" + errorstr);
			return false;
			}
		else
			{
			document.ClientDetails.submit();
			return true;
			}
		}
	else
		{
		if (origin=="Return")
			{
			confirmation=confirm("Are you sure you want to exit with these amendments?\n\n");
			if (confirmation)
				{
			 	document.ClientDetails.submit();
				return true;
				}
			else
				{
				return false;
				}
			}
		else
			{
			document.ClientDetails.submit();
			return true;
			}
		}
	}
	
	
//-------------------------------------------------------------------------------------------------------------	
	
	function SavePersonalDetails(origin)
	{
	var form = document.getElementById("PersonalDetails");
	var errorstr = "";
	var strEmail=form.email.value;
	var at="@";
	var dot=".";
	var lat=strEmail.indexOf(at);
	var ldot=strEmail.indexOf(dot);
	var lstr=strEmail.length;
	var confirmation=false;
	form.origin.value = origin;	
	
//	alert(origin);	
	
	if (! form.fullname.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Name must be alphanumeric.\n\n";
		
	if (! form.loginname.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Loginname must be alphanumeric.\n\n";
		
	if (! form.password.value.match(/^[a-zA-Z0-9][a-zA-Z0-9\_]*$/))
		errorstr += "Password must be letters, numbers, or underline.\n\n";	
				
	if (form.password.value.length > 18)
		errorstr += "Password must be less than 19 characters.\n\n";	
		
	if (! form.phone.value.match(/^\d*$/))
		errorstr += "Phone number must be numeric (no spaces), or\nnot entered at all.\n\n";	
		
	if (! form.fax.value.match(/^\d*$/))
		errorstr += "Fax number must be numeric (no spaces), or\nnot entered at all.\n\n";	
			
	if (! form.mobile.value.match(/^\d*$/))
		errorstr += "Mobile number must be numeric (no spaces), or\nnot entered at all.\n\n";	
			
	if (form.panelsize.options[0].selected)
		errorstr += "Please select a Panel Size.\n\n";	
					
	if (form.relativetimezone.options[0].selected)
		errorstr += "Please select a Relative Time Zone.\n\n";			

	if(form.emailme.checked)
		{
		if(! form.email.value)
			errorstr += "Email address is blank.\n\n";
		}
		
	if(form.textme.checked)
		{
		if(! form.mobile.value)
			errorstr += "Mobile number is blank.\n\n";
		}

	if (strEmail)
		{
		if (strEmail.indexOf(at)==-1 || strEmail.indexOf(at)==0 || strEmail.indexOf(at)==lstr)
			errorstr += "Email @ is missing or in wrong position.\n\n";

		if (strEmail.indexOf(dot)==-1 || strEmail.indexOf(dot)==0 || strEmail.indexOf(dot)==lstr)
			errorstr += "Email dot[.] is missing or in wrong position.\n\n";

		if (strEmail.indexOf(at,(lat+1))!=-1)
			errorstr += "Email has more than one @.\n\n";

		if (strEmail.substring(lat-1,lat)==dot || strEmail.substring(lat+1,lat+2)==dot)
			errorstr += "Email has dot[.] just before or after @.\n\n";

		if (strEmail.indexOf(dot,(lat+2))==-1)
			errorstr += "Email has no dot[.] after @.\n\n";
		
		if (strEmail.indexOf(" ")!=-1)
			errorstr += "Email must not include spaces.\n\n";
		}
						
	if (errorstr != "") 
		{	
		alert("Some values given are invalid or missing.\nPlease correct them and resubmit.\n\n" + errorstr);
		return false;
		}
	else
		{
		if (origin=="Return")
			{
			confirmation=confirm("Are you sure you want to exit with these amendments?\n\n");
			if (confirmation)
				{
			 	document.PersonalDetails.submit();
				return true;
				}
			else
				{
				return false;
				}
			}
		else
			{
			document.PersonalDetails.submit();
			return true;
			}
		}
	}
	
// -------------------------------------------------------------------------------------------------------------	

// BUGREPORT 
	
// -------------------------------------------------------------------------------------------------------------
	
	function SaveBugReport()
	{
	var form = document.getElementById("BugReport");
	var errorstr = "";	
											
	if (! form.bugtitle.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Bug Title must be alphanumeric.\n\n";
											
	if (! form.loginname.value.match(/^[\w][\w\s\W]*$/))
		errorstr += "Bug Report must be alphanumeric.\n\n";
			
	if (errorstr != "") 
		{	
		alert("There is an error with this Message.\nPlease correct it and resubmit.\n\n" + errorstr);
		return false;
		}
		
	confirmed = confirm("Are you sure you want to send this Bug Report?");
	if (confirmed)
		{
		document.BugReport.submit();
		return true;
		}
	else
		{
		return false;
		}
		
	document.BugReport.submit();
	return true;
	}
	
// -------------------------------------------------------------------------------------------------------------

//	function DefStat()
//	{
//	top.status = "OnlineCounsellor™ from adnet";
//	}
		
// -------------------------------------------------------------------------------------------------------------
	
	function RegistrationVal()
	{
	var form = document.getElementById("RegistrationForm");
	var errorstr = "";
	var strUsername = form.login.value
	var strPassword = form.password.value
	
	if (strUsername.length > 16)
		errorstr += "Username must be no longer than 17 characters.\n\n";		
	
	if (strPassword.length > 16)
		errorstr += "Password must be no longer than 17 characters.\n\n";		
			
	if (! form.name.value.match(/^[a-zA-Z0-9][a-zA-Z0-9\_]*$/))
		errorstr += "Your Name must contain only letters, numbers and optionally, underlines, which must not come first.\n\n";
		
	if (! form.login.value.match(/^[a-zA-Z0-9][a-zA-Z0-9\_]*$/))
		errorstr += "Your Username must contain only letters, numbers and optionally, underlines, which must not come first.\n\n";
		
	if (! form.password.value.match(/^[a-zA-Z0-9][a-zA-Z0-9\_]*$/))
		errorstr += "Your Password must contain only letters, numbers and optionally, underlines, which must not come first.\n\n";
					
	if (errorstr != "") 
		{	
		alert("Either your Name, Username, or Password contains unacceptable characters.\nPlease make corrections as follows and try again.\n\n" + errorstr);
		return false;
		}
	else
		{
		document.RegistrationForm.submit();
		return true;
		}
	}
		
// -------------------------------------------------------------------------------------------------------------
	
	function LoginVal()
	{
	var form = document.getElementById("Login");
	var errorstr = "";
		
	if (! form.login.value.match(/^[a-zA-Z0-9][a-zA-Z0-9\_]*$/))
		{
		errorstr += "Either your Username or Password is incorrect or missing.\n\n";
		}
		
	else
		{
		 if (! form.password.value.match(/^[a-zA-Z0-9][a-zA-Z0-9\_]*$/))
			{
			errorstr += "Either your Username or Password is incorrect or missing.\n\n";
			}
		}
				
	if (errorstr != "") 
		{	
		alert(errorstr+"Please check what you have entered and try again.\n\n");
		return false;
		}

	document.Login.submit();
	return true;
	}
		
// -------------------------------------------------------------------------------------------------------------

	function FindCaseID()
	{
	var form = document.getElementById("FindCase");	
	var errorstr = "";
			
	if (! form.findcaseid.value.match(/^\d*$/))
		errorstr += "Case ID must be numeric.\n\n";	
						
	if (errorstr != "") 
		{	
		alert("Some values given are invalid or missing.\nPlease correct them and resubmit.\n\n" + errorstr);
		return false;
		}						
	
	document.FindCase.submit();
	return true;
	} 			
	
// -------------------------------------------------------------------------------------------------------------

