var numberOfItems=1; //do not touch this unless you know what you are doing!!
var maxItems=20; //do not touch this unless you know what you are doing!!

var pricePerItem=7.40;
var exemptMessage="exempt from prescription charges";
var redirectLocation='http://www.sgcourt.co.uk/index.php';

function handleMessage(message){
	alert(message);
}

function addLine(){
	if(numberOfItems==maxItems){
		alert("The maximum number of items you can order at a time is "+maxItems+".");
		return;
	} else {
		var numDrugs=document.getElementById("numDrugs");
		numDrugs.selectedIndex=numberOfItems;
		changeTotal();
	}
}

function setPrice(price){
	var totalCost=document.getElementById("totalCost");
	totalCost.innerHTML=price;
}

function updatePrice(){
	var box = document.getElementById("payment1");
	if(box.checked==true){
		setPrice(exemptMessage);
	}else{
		var totalCost=pricePerItem*numberOfItems;
		setPrice("&pound;"+totalCost.toFixed(2));
	}
}

function yesExempt(clickedBox){
	clickBox(clickedBox,'payment',2);
	updatePrice();
}

function noExempt(clickedBox){
	clickBox(clickedBox,'payment',2);
	updatePrice();
}

function changeTotal(){
	var dropDown=document.getElementById("numDrugs");
	numberOfItems = dropDown.value;
	updatePrice();
	var i=1;
	while(i<=numberOfItems){
		var drugLines=document.getElementById("drugLines"+i);
		drugLines.style.display="block";
		i++;
	}
	while(i<=maxItems){
		var drugLines=document.getElementById("drugLines"+i);
		drugLines.style.display="none";
		i++;
	}
}

function clickBox(clickedBox,boxGroup,groupMax){
	var i=1;
	while(i<=groupMax){
		var checkbox=document.getElementById(boxGroup+""+i);
		checkbox.checked=false;
		i++;
	}
	clickedBox.checked=true;
}

function checkExempt(){
	var box = document.getElementById("payment1");
	if(box.checked==false){
		return true;
	}else{
		var option = document.getElementById("exempt");
		if (option.value=="if exempt you must select an option that applies:"){
			alert("If exempt, you must select an option that applies.");
			return false;
		}
		return true;		
	}
}

function checkDrugs(){
	var whiteSpace = /^[\s]+$/;
	for(var i=1; i<=numberOfItems;i++){
		var drugName = document.getElementById('drugName'+i);
		var drugStrength = document.getElementById('drugStrength'+i);
		if ( drugName.value == '' || whiteSpace.test(drugName.value) ) {
			handleMessage("Please enter the drug name.");
			return false;
		} else if ( drugStrength.value == '' || whiteSpace.test(drugStrength.value) ) {
			handleMessage("Please enter the drug strength.");
			return false;
		}
	}
	return true;
}


function validateFields() {
	// grab the fields in the form and the form itself
	var fullName = document.getElementById('Full_Name');
	var doctorName = document.getElementById('doctorName');
	var doctorSurgery = document.getElementById('doctorSurgery');
	var address1 = document.getElementById('Address_1');
	var postCode = document.getElementById('Zip');
	var email = document.getElementById('Email_address');
	var telephone = document.getElementById('Phone_number');
	var exempt = document.getElementById('exempt');

	var whiteSpace = /^[\s]+$/;
	var okToSend = true;
	
	// Check to see if a required field is blank or null. If so, set an error message.
	if ( fullName.value == '' || whiteSpace.test(fullName.value) ) {
		handleMessage("Please enter your name.");
		okToSend = false;
	} else if ( doctorName.value == '' || whiteSpace.test(doctorName.value) ) {
		handleMessage("Please enter your doctor's name.");
		okToSend = false;
	} else if ( doctorSurgery.value == '' || whiteSpace.test(doctorSurgery.value) ) {
		handleMessage("Please enter your doctor's surgery.");
		okToSend = false;
	} else if ( address1.value == '' || whiteSpace.test(address1.value) ) {
		handleMessage("Please enter your house name/number.");
		okToSend = false;
	} else if ( postCode.value == '' || whiteSpace.test(postCode.value) ) {
		handleMessage("Please enter your post code.");
		okToSend = false;
	} else if(!emailCheck(email.value)){
		okToSend = false;
	} else if ( telephone.value == '' || whiteSpace.test(telephone.value) ) {
		handleMessage("Please enter your telephone.");
		okToSend = false;
	} else if(!checkExempt()){
		okToSend = false;	
	} else if(!checkDrugs()){
		okToSend = false;	
	}

	// If any of the checks for required information failed, 	
	if(okToSend == true){
		sendEmail();
	}
}



/**
 * Sends the actual data in the form to the server via an AJAX request.
 * Change this method to extract whatever data you need to be taken from
 * the form and uploaded to the server.
**/
function sendEmail () {
	// grab the fields in the form and the form itself
	var pfullName = document.getElementById('Full_Name');
	var pdoctorName = document.getElementById('doctorName');
	var pdoctorSurgery = document.getElementById('doctorSurgery');
	var paddress1 = document.getElementById('Address_1');
	var paddress2 = document.getElementById('Address_2');
	var ptown = document.getElementById('City');
	var pcounty = document.getElementById('State');
	var ppostCode = document.getElementById('Zip');
	var pemail = document.getElementById('Email_address');
	var ptelephone = document.getElementById('Phone_number');
	var pexempt = document.getElementById('exempt');
	var pbranchId = document.getElementById('branchId');
	var pspecialRequest = document.getElementById('specialRequest');

	// the page on the server that sends the email
	var page = "./orders/sendMail.php?contact=true&xml=true";
	showContactTimer(); // quickly begin the load bar
	
	// convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.
	var fullName = cleanString(pfullName.value);
	var doctorName = cleanString(pdoctorName.value);
	var doctorSurgery = cleanString(pdoctorSurgery.value);
	var address1 = cleanString(paddress1.value);
	var address2 = cleanString(paddress2.value);
	var town = cleanString(ptown.value);
	var county = cleanString(pcounty.value);
	var postCode = cleanString(ppostCode.value);
	var email = cleanString(pemail.value);
	var telephone = cleanString(ptelephone.value);
	var exempt = cleanString(pexempt.value);
	var branchId = cleanString(pbranchId.value);
	var specialRequest = cleanString(pspecialRequest.value);


	// create the data that is to be sent
	var data = 	"&branchId="+branchId+
				"&fullName="+fullName+
				"&doctorName="+doctorName+
				"&doctorSurgery="+doctorSurgery+
				"&address1="+address1+
				"&address2="+address2+
				"&town="+town+
				"&county="+county+
				"&postCode="+postCode+
				"&email="+email+
				"&telephone="+telephone;

	var delivery = document.getElementById('delivery1');
	if(delivery.checked){
		data+="&delivery=yes";
	}else{
		data+="&delivery=no";
	}

	var payment = document.getElementById('payment1');
	if(payment.checked){
		data+="&exempt=yes";
		data+="&reason="+exempt;
	}else{
		data+="&exempt=no";
		data+="&reason=not applicable";
	}

	var ptotalCost = document.getElementById('totalCost');
	var totalCost = cleanString(ptotalCost.innerHTML);
	data+="&totalCost="+totalCost;
	data+="&numberOfItems="+numberOfItems;

	for(var i=1; i<=numberOfItems;i++){
		var pdrugName = document.getElementById('drugName'+i);
		var pdrugStrength = document.getElementById('drugStrength'+i);
		var drugName=cleanString(pdrugName.value);
		var drugStrength=cleanString(pdrugStrength.value);
		data+="&drugName"+i+"="+drugName;
		data+="&drugStrength"+i+"="+drugStrength;
	}
	
	data+="&specialRequest="+specialRequest;
	
	// put the data into the request to be sent to the server
	loadXMLPosDoc(page,data);
}

/**
 * Cleans a string so that it can go in a string
**/
function cleanString(str){
	str = str.replace(/&/g,"**am**");
	str = str.replace(/=/g,"**eq**");
	str = str.replace(/\+/g,"**pl**");
	str = str.replace(/ /g,"_");
	return str;
}

function showContactTimer () {
	handleMessage("Please click okay and wait for confirmation that your prescription request has been sent");
	sentTimer = setTimeout("hideContactTimer()",6000);
}

function hideContactTimer () {
	handleMessage(grabPosXML("confirmation"));
	window.location = redirectLocation;
}

function ajaxContact() {
}
addEventForForms(window, 'load',ajaxContact, false);
