

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function Bmi() 
	{	
		var ErrorText='';
		var Weight= trimBlank(document.QOF.Weight.value) ;
		var Height= trimBlank(document.QOF.Height.value) ;
		var BmiValue=0;
		
		if (trimBlank(Height) =="")
			{ErrorText = ErrorText +'Height must be entered\n';}
		else if (IsNumeric(Height) == false)
			{ErrorText = ErrorText +'Height entered must be numeric\n';
			}
		
		if (trimBlank(Weight) =="")
			{ErrorText = ErrorText +'Weight must be entered\n';}
		else if (IsNumeric(Weight) == false)
			{ ErrorText = ErrorText +'Weight entered must be numeric\n';
			}
				
		if (ErrorText == '')
			{
				BmiValue=  Height * Height;
				BmiValue= Weight / BmiValue;
				document.QOF.CalBmi.value = BmiValue.toPrecision(2);
			}
		else
			{
				alert(ErrorText);
				return false;
			}	
	}

function CustomerQOFfrm()
	{
		var ErrorText="";
		if (trimBlank(document.QOF.Weight.value) =="")
			{	ErrorText = ErrorText +'Weight must be entered\n';	}
		else if (IsNumeric(document.QOF.Weight.value) == false)
			{ 	ErrorText = ErrorText +'Weight entered must be numeric\n';	}
		
		if (trimBlank(document.QOF.Height.value) =="")
			{	ErrorText = ErrorText +'Height must be entered\n';	}
		else if (IsNumeric(document.QOF.Height.value) == false)
			{	ErrorText = ErrorText +'Height entered must be numeric\n';		}
		
		if (trimBlank(document.QOF.CalBmi.value) =="")
			{	ErrorText = ErrorText + '\nPlease calculate you BMI';		}
		else if (IsNumeric(document.QOF.CalBmi.value) == false)
			{	ErrorText=ErrorText + 'BMI entered must be numeric\n';	}
			
		if (trimBlank(ErrorText) == "")
			{return true;}
		else
			{
				alert(ErrorText);
				return false;
			}	
	}

function CustomerLoginfrm()
	{
		var ErrorText="";
		if (trimBlank(document.LoginFrm.Login_Email.value) =="")
			{	ErrorText = ErrorText + '\nEmail Address';	}
		if (trimBlank(document.LoginFrm.Login_Password.value) =="")
			{	ErrorText = ErrorText + '\nPassword';	}
		
		if (trimBlank(ErrorText) == "")
			{return true;}
		else
			{
				alert('Please enter your:' + ErrorText);
				return false;
			}	
		
	}
	
	
function CustomerRequestPasswordfrm()
	{
		var ErrorText="";
		if (trimBlank(document.RequestPasswordForm.Request_Email.value) =="")
			{	ErrorText = ErrorText + 'Email Address\n';	}
		
		if (trimBlank(ErrorText) == "")
			{return true;}
		else
			{
				alert('The following fields are required:\n\n' + ErrorText);
				return false;
			}	
		
	}
function CustomerRegisterfrm ()
	{
		var ErrorText="";
		if (trimBlank(document.register.Fname.value) =="")
			{	ErrorText = ErrorText + 'First Name\n';	}
		
		if  (trimBlank(document.register.Lname.value) =="")
			{	ErrorText = ErrorText + 'Last name\n';	}

		//Passwords
		if  (trimBlank(document.register.Password.value) =="")
			{	ErrorText = ErrorText + 'Password\n';	}
		else if(trimBlank(document.register.ConfirmPassword.value) =="")
			{	ErrorText = ErrorText + 'Confirm Password\n';	}
		else if(trimBlank(document.register.ConfirmPassword.value) != trimBlank(document.register.Password.value))
			{	ErrorText = ErrorText + 'Password and Confirm Password must match\n';	}
		
		//Email Address email ConfirmEmailAddress
		if  (trimBlank(document.register.email.value) =="")
			{	ErrorText = ErrorText + 'Email Address\n';	}
		else if (!isEmailAddr(document.register.email.value))	
			{	ErrorText = ErrorText + 'Incorrect Email Address Format: name@yourdomain.com\n';	}
			
		else if(trimBlank(document.register.ConfirmEmailAddress.value) =="")
			{	ErrorText = ErrorText + 'Confirm Email Address\n';	}
				
		else if(trimBlank(document.register.ConfirmEmailAddress.value) != trimBlank(document.register.email.value))
			{	ErrorText = ErrorText + 'Email Address and Confirm Email Address must match\n';	}

		
		if  (trimBlank(document.register.telNumber.value) =="")
			{	ErrorText = ErrorText + 'Contact Number\n';	}
			
		if  (trimBlank(document.register.Address1.value) =="")
			{	ErrorText = ErrorText + 'Address\n';	}	

		if  (trimBlank(document.register.Postcode.value) =="")
			{	ErrorText = ErrorText + 'Postcode\n';	}

		if  (trimBlank(document.register.Surgery.value) == 0 || trimBlank(document.register.Surgery.value) == "")
			{	ErrorText = ErrorText + 'Surgery\n';	}

		if  (trimBlank(document.register.Doctor.value) == 0 || trimBlank(document.register.Doctor.value) =="")
			{	ErrorText = ErrorText + 'Doctor\n';	}	

		if  (trimBlank(document.register.Pharmacy.value) == 0)
			{	ErrorText = ErrorText + 'Pharmacy\n';	}	
		
		//Validate Date of Birth
		if (ValidDate(document.register.Month.value,document.register.Day.value,document.register.Year.value) == 'False')
			{	ErrorText = ErrorText + 'Invalid Date Of Birth\n';	}
		
		//Exemption Status
		if  (get_exempt_value() == 1)
			{//Yes has been checked. They must select option	
				if (trimBlank(document.getElementById('ExemptionStatusYes').value) == 0)
					{	ErrorText = ErrorText + 'Select The Exemption Status that describes you\n';	}
			}
		
		
		
		if (trimBlank(ErrorText) == "")
			{return true;}
		else
			{
				alert('The following fields are required:\n\n' + ErrorText);
				return false;
			}	
		
	

	}
	
//Validate date
function ValidDate(Month,Day,Year)

	{	var month = Month;
		var Day = Day;
		var Year = Year;
		var DateOk = 'True';
		
		var ThirtyOneMonths = new Array();
		ThirtyOneMonths[0] = 1;
		ThirtyOneMonths[1] = 3;
		ThirtyOneMonths[2] = 5;
		ThirtyOneMonths[3] = 7;
		ThirtyOneMonths[4] = 8;
		ThirtyOneMonths[5] = 10;
		ThirtyOneMonths[6] = 12;
		
		var ThirtyMonths = new Array();
		ThirtyMonths[0] =4;
		ThirtyMonths[1] =6;
		ThirtyMonths[2] =9;
		ThirtyMonths[3] =11;
		
		var TwentyEightMonths = new Array();
		TwentyEightMonths[0] = 2;
		
		
		if (Day == 31 )
			{
				//Check to make sure the month is valid, It can;t be in a 30 or 28 day month
				for (var i=0; i < ThirtyMonths.length; i++)
   					{
						if (Month == ThirtyMonths[i])	{	DateOk = 'False';	}
					}
   				for (var i=0; i < TwentyEightMonths.length; i++)
   					{
						if (Month == TwentyEightMonths[i])	{	DateOk = 'False';	}
					}
			}
		else if (Day == 30)
			{
				//Check to make sure the month is valid, It can't be in a 28 day month
				for (var i=0; i < TwentyEightMonths.length; i++)
   					{
						if (Month == TwentyEightMonths[i])	{	DateOk = 'False';	}
					}
   				
			}	
			
		return DateOk;
	}
	
function Loadform()
{	
	document.getElementById('ExemptStatus').style.display = 'none';
	document.getElementById('ExemptStatusLower').style.display = 'none';
	document.getElementById('ExemptStatusLowerNi').style.display = 'none';

	//Look to see if the Exemptstatus should be displayed
	if (get_exempt_value() == 1)
		{
			//show the requested div
			document.getElementById('ExemptStatus').style.display = 'block';
		}
	
	//Look to see if the ni and option should be displayed	
	ShowExemptionLower();
}

function LoadQOFForm()
{
	document.getElementById('smokeBox').style.display = 'none';
	document.getElementById('drinkBox').style.display = 'none';
}

function LoadPlaceOrderform()
{	
	document.getElementById('ExemptStatus').style.display = 'none';
	document.getElementById('ExemptStatusLower').style.display = 'none';
	document.getElementById('ExemptStatusLowerNi').style.display = 'none';
	//document.getElementById('smokeBox').style.display = 'none';
	//document.getElementById('drinkBox').style.display = 'none';
	
	//See if the Yes questions should be dislayed
	if (get_Drink_value() == 1)
		{
			//show the requested div
			document.getElementById('drinkBox').style.display = 'block';
		}
	if (get_Smoke_value() == 1)
		{
			//show the requested div
			document.getElementById('smokeBox').style.display = 'block';
		}
		
	//Look to see if the Exemptstatus should be displayed
	if (get_exempt_value() == 1)
		{
			//show the requested div
			document.getElementById('ExemptStatus').style.display = 'block';
		}
	
	//Look to see if the ni and option should be displayed	
	ShowExemptionLower();
}

function show_div() 
	{
		
		//hide all the divs
		document.getElementById('ExemptStatus').style.display = 'none';
		document.getElementById('ExemptionStatusYes').value=0;
		document.getElementById('ExemptStatusLower').style.display = 'none';
		document.getElementById('ExemptStatusLowerNi').style.display = 'none';
		

		if (get_exempt_value() == 1)
			{
				//show the requested div
				document.getElementById('ExemptStatus').style.display = 'block';
			}
	}

	
function get_exempt_value()
	{	// Get the value of the radio button Exempt
		for (var i=0; i < document.register.Exempt.length; i++)
   			{
   				if (document.register.Exempt[i].checked)
      				{	var rad_val = document.register.Exempt[i].value;     }
			}
		return rad_val;
	}	

function get_Drink_value()
	{	// Get the value of the radio button Drink
		for (var i=0; i < document.register.Drink.length; i++)
   			{
   				if (document.register.Drink[i].checked)
      				{	var rad_val = document.register.Drink[i].value;     }
			}
		return rad_val;
	}	
	
function get_Smoke_value()
	{	// Get the value of the radio button Smoke
		for (var i=0; i < document.register.Smoker.length; i++)
   			{
   				if (document.register.Smoker[i].checked)
      				{	var rad_val = document.register.Smoker[i].value;     }
			}
		return rad_val;
	}	
			
function ShowExemptionLower()
	{
		//Hide all both text boxes
		document.getElementById('ExemptStatusLower').style.display = 'none';
		document.getElementById('ExemptStatusLowerNi').style.display = 'none';

		//alert(document.getElementById('ExemptionStatusYes').value);
		//alert(OptionArray);
		//alert(NIArray);
		
		//only open those that should display
		for ( var i=OptionArray.length-1; i>=0; --i ){
 			if (OptionArray[i] == document.getElementById('ExemptionStatusYes').value){
				document.getElementById('ExemptStatusLower').style.display = 'block';
			}
		}
		for ( var i=NIArray.length-1; i>=0; --i ){
 			if (NIArray[i] == document.getElementById('ExemptionStatusYes').value){
				document.getElementById('ExemptStatusLowerNi').style.display = 'block';
			}
		}
	}
	
function CustomerUpdatefrm ()
	{
		var ErrorText="";
		if (trimBlank(document.register.Fname.value) =="")
			{	ErrorText = ErrorText + 'First Name\n';	}
		
		if  (trimBlank(document.register.Lname.value) =="")
			{	ErrorText = ErrorText + 'Last name\n';	}

		//Passwords
		if(trimBlank(document.register.ConfirmPassword.value) != trimBlank(document.register.Password.value))
			{	ErrorText = ErrorText + 'Password and Confirm Password must match\n';	}
		
		//Email Address email ConfirmEmailAddress
		if  (trimBlank(document.register.email.value) =="")
			{	ErrorText = ErrorText + 'Email Address\n';	}
		else if (!isEmailAddr(document.register.email.value))	
			{	ErrorText = ErrorText + 'Incorrect Email Address Format: name@yourdomain.com\n';	}
			
		else if(trimBlank(document.register.ConfirmEmailAddress.value) =="")
			{	ErrorText = ErrorText + 'Confirm Email Address\n';	}
				
		else if(trimBlank(document.register.ConfirmEmailAddress.value) != trimBlank(document.register.email.value))
			{	ErrorText = ErrorText + 'Email Address and Confirm Email Address must match\n';	}

		
		if  (trimBlank(document.register.telNumber.value) =="")
			{	ErrorText = ErrorText + 'Contact Number\n';	}
			
		if  (trimBlank(document.register.Address1.value) =="")
			{	ErrorText = ErrorText + 'Address\n';	}	

		if  (trimBlank(document.register.Postcode.value) =="")
			{	ErrorText = ErrorText + 'Postcode\n';	}

		if  (trimBlank(document.register.Surgery.value) == 0 || trimBlank(document.register.Surgery.value) == "")
			{	ErrorText = ErrorText + 'Surgery\n';	}

		if  (trimBlank(document.register.Doctor.value) == 0 || trimBlank(document.register.Doctor.value) == "")
			{	ErrorText = ErrorText + 'Doctor\n';	}	

		if  (trimBlank(document.register.Pharmacy.value) == 0)
			{	ErrorText = ErrorText + 'Pharmacy\n';	}	
		
		//Validate Date of Birth
		if (ValidDate(document.register.Month.value,document.register.Day.value,document.register.Year.value) == 'False')
			{	ErrorText = ErrorText + 'Invalid Date Of Birth\n';	}
		
		//Exemption Status
		if  (get_exempt_value() == 1)
			{//Yes has been checked. They must select option	
				if (trimBlank(document.getElementById('ExemptionStatusYes').value) == 0)
					{	ErrorText = ErrorText + 'Select The Exemption Status that describes you\n';	}
			}
		
		
		if (trimBlank(ErrorText) == "")
			{return true;}
		else
			{
				alert('The following fields are required:\n\n' + ErrorText);
				return false;
			}	
	}

function toggleBox(szDivID, iState) // 1 visible, 0 hidden
	{
	    if(document.layers)	   //NN4+
		    {       document.layers[szDivID].display = iState ? "block" : "none";    }
	    else if(document.getElementById)	  //gecko(NN6) + IE 5+
		    {
		        var obj = document.getElementById(szDivID);
		        obj.style.display = iState ? "block" : "none";
		    }
	    else if(document.all)	// IE 4
		    {     document.all[szDivID].style.display = iState ? "block" : "none";    }
	}

function PlaceOrderfrm()
	{
		var ErrorText="";
		if (trimBlank(document.register.Fname.value) =="")
			{	ErrorText = ErrorText + 'First Name\n';	}
		if  (trimBlank(document.register.Lname.value) =="")
			{	ErrorText = ErrorText + 'Last name\n';	}
		if  (trimBlank(document.register.telNumber.value) =="")
			{	ErrorText = ErrorText + 'Contact Number\n';	}
		if  (trimBlank(document.register.address1.value) =="")
			{	ErrorText = ErrorText + 'Address\n';	}	
		if  (trimBlank(document.register.Postcode.value) =="")
			{	ErrorText = ErrorText + 'Postcode\n';	}
		if  (trimBlank(document.register.Surgery.value) == 0 || trimBlank(document.register.Surgery.value) == "")
			{	ErrorText = ErrorText + 'Surgery\n';	}

		if  (trimBlank(document.register.Doctor.value) == 0 || trimBlank(document.register.Doctor.value) == "")
			{	ErrorText = ErrorText + 'Doctor\n';	}	
		
		//Exemption Status
		if  (get_exempt_value() == 1)
			{//Yes has been checked. They must select option	
				if (trimBlank(document.getElementById('ExemptionStatusYes').value) == 0)
					{	ErrorText = ErrorText + 'Select The Exemption Status that describes you\n';	}
			}
			
		if (trimBlank(document.register.ColFname.value) =="")
			{	ErrorText = ErrorText + 'Collected by: First Name\n';	}

		if  (trimBlank(document.register.ColLname.value) =="")
			{	ErrorText = ErrorText + 'Collected by: Last name\n';	}

				
		if  (trimBlank(document.register.ColtelNumber.value) =="")
			{	ErrorText = ErrorText + 'Collected by: Contact Number\n';	}

		if  (trimBlank(document.register.Pharmacy.value) == 0)
			{	ErrorText = ErrorText + 'Collection Pharmacy\n';	}	

		 if  (trimBlank(document.register.DrugPrescribed1.value) == "")
			{	ErrorText = ErrorText + 'Drug Prescribed\n';	}	
		
		if  (trimBlank(document.register.DrugStrength1.value) == "")
			{	ErrorText = ErrorText + 'Drug Strength\n';	}	
		/*
		for (var x = 1; x <= DrugCount; x++)
			{alert(eval('DrugPrescribed' + x));
				if  (trimBlank(document.register.eval('DrugPrescribed' + x).value) == "")
					{	ErrorText = ErrorText + 'Drug Prescribed\n';	}	
		
				if  (trimBlank(document.register.eval('DrugStrength' + x).value) == "")
					{	ErrorText = ErrorText + 'Drug Strength\n';	}	
					
			}
		*/
		
		 
		if (get_Smoke_value() == 1)
			{	
				if  (trimBlank(document.register.SmokeCount.value) == 0)
					{	ErrorText = ErrorText + 'Cigarettes per day\n';	}	
			}	
		if (get_Drink_value() == 1)
			{	
				if  (trimBlank(document.register.DrinkCount.value) == 0)
					{	ErrorText = ErrorText + 'Alcoholic drinks per day\n';	}	
			}
		if  (trimBlank(document.register.Pharmacy.value) == 0)
			{	ErrorText = ErrorText + 'Collection Pharmacy\n';	}	
				
		if (document.getElementById('Declaration').checked == 1)
			{}
		else	{	ErrorText = ErrorText + 'Must accept Declaration\n';	}	

		
		if (trimBlank(ErrorText) == "")
			{return true;}
		else
			{
				alert('The following fields are required:\n\n' + ErrorText);
				return false;
			}	
	}	
	
	
function SameAsAbove()
	{	
		if (document.getElementById('SameAsPrescriptionFor').checked == 1)
			{
				document.getElementById('Colfname').value=document.getElementById('fname').value;
				document.getElementById('Collname').value=document.getElementById('lname').value;
				document.getElementById('ColtelNumber').value=document.getElementById('telNumber').value;
			}

	}
	
function Add()
	{
		var DrugPrescribedField=document.createElement('input');
		var DrugPrescribed=document.getElementById('DrugPrescribed');
		DrugPrescribedField.type='text';
		DrugPrescribedField.size=45;
		DrugPrescribedField.name='DrugPrescribed' + DrugCount;
		DrugPrescribed.appendChild(DrugPrescribedField);
		DrugPrescribed.appendChild(document.createElement('br'));

		var DrugStrengthField=document.createElement('input');
		var DrugStrength=document.getElementById('DrugStrength');
		DrugStrengthField.type='text';
		DrugStrengthField.size=45;
		DrugStrengthField.name='DrugStrength'+DrugCount;
		DrugStrength.appendChild(DrugStrengthField);
		DrugStrength.appendChild(document.createElement('br'));

		document.getElementById('DrugCount').value=DrugCount
		DrugCount = DrugCount +1;
}
function AddQofToOrder()
{
	var AddQOFToOrder = document.getElementById('AddQOFToOrder')
	AddQOFToOrder.value=1;
	document.getElementById('SmokeDisplay').style.display = 'block';
	document.getElementById('SmokeDisplay1').style.display = 'block';
	document.getElementById('SmokeDisplay2').style.display = 'block';
	document.getElementById('SmokeDisplay3').style.display = 'block';
	document.getElementById('SmokeDisplay4').style.display = 'block';
	document.getElementById('SmokeDisplay5').style.display = 'block';
	document.getElementById('SmokeDisplay6').style.display = 'block';
	document.getElementById('SmokeDisplay7').style.display = 'block';
	document.getElementById('SmokeDisplay8').style.display = 'block';
	document.getElementById('removeQOFButton').style.display = 'block';
	document.getElementById('AddQOFButton').style.display = 'none';
}
 function RemoveQofToOrder()
 {
	 var AddQOFToOrder = document.getElementById('AddQOFToOrder')
	AddQOFToOrder.value=0;
	var ShareGP = document.getElementById('ShareGP')
	ShareGP.value=0;
	document.getElementById('SmokeDisplay').style.display = 'none';
	document.getElementById('SmokeDisplay1').style.display = 'none';
	document.getElementById('SmokeDisplay2').style.display = 'none';
	document.getElementById('SmokeDisplay3').style.display = 'none';
	document.getElementById('SmokeDisplay4').style.display = 'none';
	document.getElementById('SmokeDisplay5').style.display = 'none';
	document.getElementById('SmokeDisplay6').style.display = 'none';
	document.getElementById('SmokeDisplay7').style.display = 'none';
	document.getElementById('SmokeDisplay8').style.display = 'none';		
	document.getElementById('removeQOFButton').style.display = 'none';
	document.getElementById('AddQOFButton').style.display = 'block';
	AddQOFToOrder.value=0;
}	

function CheckSignUp()
{
	var ErrorText='';
	var fname= trimBlank(document.SignUp.fname.value) ;
	var lname= trimBlank(document.SignUp.lname.value) ;
	var email= trimBlank(document.SignUp.email.value) ;
		
	if (trimBlank(fname) =="")
		{ErrorText = ErrorText +'First name must be entered\n';}
		
	if (trimBlank(lname) =="")
		{ErrorText = ErrorText +'Last name must be entered\n';}
	if  (trimBlank(email) =="")
		{	ErrorText = ErrorText + 'Email address must be entered\n';	}
	else if (!isEmailAddr(email))	
		{	ErrorText = ErrorText + 'Incorrect email address format: name@yourdomain.com\n';	}
					
	if (ErrorText == '')
		{ return true;}
	else
		{
			alert(ErrorText);
			return false;
		}	
}
/*  These are commented our as this function is already defined in js.js
function trimBlank(checkString)
	{
   		var newString = "";
   		if (checkString.length > 0)
			{
      			for (i=0; i<checkString.length; i++)  
					{
				         if (checkString.charAt(i) != " ") newString += checkString.charAt(i)
				      }
			   }
		return newString
	}
	
function isEmailAddr(email)
	{
		var result = false;
  		var theStr = new String(email);
  		var index = theStr.indexOf("@");
  		if (index > 0) 
			{
    				var pindex = theStr.indexOf(".",index);
    				if ((pindex > index+1) && (theStr.length > pindex+1))
					result = true;
  			}
  		return result;
	}	
*/