
function checkOther() {//convert the dropdown to freetype
v = document.getElementById("profession").selectedIndex;
tl = document.getElementById("profession").options.length -1;

if (v == tl) {//if it's the last element in the list - always "OTHER" then do some DOM fiddling and change it to a text field
var nel = document.createElement("input");
nel.setAttribute('id','profession');
nel.setAttribute('name','profession');
nel.setAttribute('value','Please specify...');
var oldField = document.getElementById('profession');
var pNode = oldField.parentNode;
var replaced = pNode.replaceChild(nel,oldField);
nel.focus();
}
}

function checkOtherInst() {//convert the dropdown to freetype
v = document.getElementById("institution").selectedIndex;
tl = document.getElementById("institution").options.length -1;

if (v == tl) {//if it's the last element in the list - always "OTHER" then do some DOM fiddling and change it to a text field
var nel = document.createElement("input");
nel.setAttribute('id','institution');
nel.setAttribute('name','institution');
nel.setAttribute('value','Please specify...');
var oldField = document.getElementById('institution');
var pNode = oldField.parentNode;
var replaced = pNode.replaceChild(nel,oldField);
nel.focus();
}
}



function addInst() {
if (!document.getElementById('institution')) {
var pd = document.getElementById("inject");
//firstly create the dropdown
var nf = document.createElement("select");
nf.setAttribute('id','institution');
nf.setAttribute('name','institution');
nf.setAttribute('onchange','checkOtherInst()');
nf.onchange = new Function('checkOtherInst()'); //dirty Microsoft IE 6 & 7 hack - not strictly DOM, but it works. Sort it out, Microsoft!
nf.options[nf.options.length] = new Option("--Select One--","null",false,false);
nf.options[nf.options.length] = new Option("Keele","Keele",false,false);
nf.options[nf.options.length] = new Option("Southampton","Southampton",false,false);
nf.options[nf.options.length] = new Option("Coventry","Coventry",false,false);
nf.options[nf.options.length] = new Option("Other","Other",false,false);
var nlabel = document.createElement("label");
nlabel.setAttribute('for','institution');
nlabel.setAttribute('id','instlabel');
ntext = document.createTextNode("At which institution did you earn your diploma/certificate?");
nlabel.appendChild(ntext);
//now create the textbox for the date
var dt = document.createElement("input");
	dt.setAttribute('type','text');
	dt.setAttribute('id','qualification_date');
	dt.setAttribute('name','qualification_date');
	dt.setAttribute('value','dd/mm/yyyy');
//and the label
var dl = document.createElement("label");
	dl.setAttribute('id','datelabel');
	dl.setAttribute('for','qualification_date');
	dtext = document.createTextNode("Date of qualification");
	dl.appendChild(dtext);
pd.appendChild(nlabel);
pd.appendChild(nf);
pd.appendChild(dl);
pd.appendChild(dt);
nf.focus();
}

else {
var pd = document.getElementById("inject");
pd.removeChild(document.getElementById('institution'));
pd.removeChild(document.getElementById('instlabel'));
pd.removeChild(document.getElementById('datelabel'));
pd.removeChild(document.getElementById('qualification_date'));
}
}//end function


function isPhysio() {
checkBoxes();
if (!document.getElementById('hcp_number')) {
var pNode = document.getElementById('physioradio').parentNode;
var ul = document.createElement('ul');
var li = document.createElement('li');
var li2= document.createElement('li2');
ul.setAttribute('id','isphysioul');

var csp = document.createElement('input');
csp.setAttribute('type','text');
csp.setAttribute('id','csp_number');
csp.setAttribute('name','csp_number');
var hcp = document.createElement('input');
hcp.setAttribute('type','text');
hcp.setAttribute('id','hcp_number');
hcp.setAttribute('name','hcp_number');

var csplabel = document.createElement('label');
csplabel.setAttribute('for','csp_number');
csplabel.setAttribute('id','csplabel');
csText = document.createTextNode('CSP Number');
csplabel.appendChild(csText);

var hcplabel = document.createElement('label');
hcplabel.setAttribute('for','hcp_number');
hcplabel.setAttribute('id','hcplabel');
hcText = document.createTextNode('HCP Number');
hcplabel.appendChild(hcText);

if (document.getElementById('notphysioul')) {
var pNode = document.getElementById('notphysioul').parentNode; 
pNode.removeChild(document.getElementById('notphysioul'));


}
ul.appendChild(li);


li.appendChild(csplabel);
li.appendChild(csp);
li2.appendChild(hcplabel);
li2.appendChild(hcp);

ul.appendChild(li);
ul.appendChild(li2);

pNode.insertBefore(ul,document.getElementById('inject'));


}
}

function isNotPhysio() {
checkBoxes();
if (!document.getElementById('notphysioul')) {
var pNode = document.getElementById('physioradio').parentNode; 
var ul = document.createElement('ul');
var li = document.createElement('li');
ul.setAttribute('id','notphysioul');
var pf = document.createElement("select");
pf.setAttribute('id','profession');
pf.setAttribute('name','profession');
pf.setAttribute('onchange','checkOther()');
pf.onchange = new Function('checkOther()'); //dirty Microsoft IE 6 & 7 hack - not strictly DOM, but it works. Sort it out, Microsoft!
pf.options[pf.options.length] = new Option("--Select One--","null",false,false);
pf.options[pf.options.length] = new Option("Doctor","Doctor",false,false);
pf.options[pf.options.length] = new Option("Consultant","Consultant",false,false);
pf.options[pf.options.length] = new Option("Sports Therapist","Sports Therapist",false,false);
pf.options[pf.options.length] = new Option("Massage Therapist","Massage Therapist",false,false);
pf.options[pf.options.length] = new Option("Graduate Sports Rehabilitator","Graduate Sports Rehabilitator",false,false);
pf.options[pf.options.length] = new Option("Osteopath","Osteopath",false,false);
pf.options[pf.options.length] = new Option("Chiropractor","Chiropractor",false,false);
pf.options[pf.options.length] = new Option("Podiatrist","Podiatrist",false,false);
pf.options[pf.options.length] = new Option("Nurse","Nurse",false,false);
pf.options[pf.options.length] = new Option("Other","Other",false,false);
var plabel = document.createElement("label");
plabel.setAttribute('for','profession');
ptext = document.createTextNode("What is your profession?");
plabel.appendChild(ptext);

li.appendChild(plabel);
li.appendChild(pf);
ul.appendChild(li);

if (document.getElementById('isphysioul')) {
var pNode = document.getElementById('physioradio').parentNode; 
pNode.removeChild(document.getElementById('isphysioul'));
}
pNode.insertBefore(ul,document.getElementById('inject'));

}
}

function checkBoxes() {
if (!document.getElementById('inject')) {
var pNode = document.getElementById('physioradio').parentNode;
var d = document.createElement('div');
d.setAttribute('id','inject');
var omsd = document.createElement('input');
var somd = document.createElement('input');
var omsc = document.createElement('input');
var somc = document.createElement('input');
var hei = document.createElement('input');
var lomsd = document.createElement('label');
var lsomd = document.createElement('label');
var lomsc = document.createElement('label');
var lsomc = document.createElement('label');
var lhei = document.createElement('label');

omsd.setAttribute('type','checkbox');
omsd.setAttribute('id','chkomsd');
omsd.setAttribute('name','chkomsd');
omsd.setAttribute('value','1');
lomsd.setAttribute('for','chkomsd');
omsd.setAttribute('onclick','checkOptIn()');
omsd.onclick = new Function('checkOptIn()');
omsdtext = document.createTextNode("OMS Diploma Injection Therapy");
lomsd.appendChild(omsdtext);
d.appendChild(lomsd);
d.appendChild(omsd);

somd.setAttribute('type','checkbox');
somd.setAttribute('id','chksomd');
somd.setAttribute('name','chksomd');
somd.setAttribute('value','1');
lsomd.setAttribute('for','chksomd');
somd.setAttribute('onclick','checkOptIn()');
somd.onclick = new Function('checkOptIn()');
somdtext = document.createTextNode("SOM Diploma Injection Therapy");
lsomd.appendChild(somdtext);
d.appendChild(lsomd);
d.appendChild(somd);

omsc.setAttribute('type','checkbox');
omsc.setAttribute('id','chkomsc');
omsc.setAttribute('name','chkomsc');
omsc.setAttribute('value','1');
omsctext = document.createTextNode("OMS Certificate Orthopaedic Medicine");
lomsc.appendChild(omsctext);
d.appendChild(lomsc);
d.appendChild(omsc);

somc.setAttribute('type','checkbox');
somc.setAttribute('id','chksomc');
somc.setAttribute('name','chksomc');
somc.setAttribute('value','1');
somctext = document.createTextNode("SOM Certificate Orthopaedic Medicine");
lsomc.appendChild(somctext);
d.appendChild(lsomc);
d.appendChild(somc);

hei.setAttribute('type','checkbox');
hei.setAttribute('id','hei');
hei.setAttribute('name','hei');
hei.setAttribute('value','1');
hei.setAttribute('onclick','addInst()');
hei.onclick = new Function('addInst()');//another (unnecessary) IE hack. People - if you can read this then PLEASE switch to Firefox - it's much better AND compliant.
heitext = document.createTextNode("Higher Eductation Institute");
lhei.appendChild(heitext);
d.appendChild(lhei);
d.appendChild(hei);

pNode.appendChild(d);
}//end if
}//end function

/*Form checking and validation routines below*/

var xmlHttp;
var requestType;

function createXMLHttpRequest() {
	if(window.ActiveXObject) {
	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
	xmlHttp = new XMLHttpRequest();
	}
}//end function


function checkFields(u,c) {//this performs an asynchronous request to the server to check if certain details are already on it
	    createXMLHttpRequest();
	var url = "checkInfo.php?u="+u+"&c="+c;
	    xmlHttp.open("GET",url,false);
	    xmlHttp.send(null);
	var rep =xmlHttp.responseText;
if (rep == "false") {
return false;
}
else {
return true;
}
}//end function


function checkValidDate(d) {
    createXMLHttpRequest();
    var url = "checkDate.php?d="+d;
	    xmlHttp.open("GET",url,false);
	    xmlHttp.send(null);
	var dt =xmlHttp.responseText;
if (dt == "false") {
return false;
}
else {
return true;
}
}//end function

function checkForm() {
var isError = false;
var reqArr = new Array("first_name","surname","email","address","city","county","postcode","username","password","confirm_password");//setup static requirements
var errArr = new Array();
//first stage - we'll setup the required fields based on the form structure

if (document.getElementById('isphysioul')) {//if the "is physio" is selected
reqArr.push("csp_number","hcp_number");
}
if (document.getElementById('notphysioul')) {
reqArr.push("profession");
}
if (document.getElementById('institution')) {
reqArr.push("institution","qualification_date");
}
//now we'll iterate the "reqArr" array and check for values - we'll use the field type as a guide
var notAllowed = "Please specify...";

for (c=0;c < reqArr.length; c++) {
if ((document.getElementById(reqArr[c]).type == "text" || document.getElementById(reqArr[c]).type == "password") && (!document.getElementById(reqArr[c]).value || document.getElementById(reqArr[c]).value == notAllowed)) {
var str = "The field \""+reqArr[c]+"\" requires a value";
errArr.push(str);
isError = true;
}
if (document.getElementById(reqArr[c]).type == "select-one" && document.getElementById(reqArr[c]).selectedIndex=="0") {
var str = "You must make a selection for "+reqArr[c];
errArr.push(str);
isError = true;
}
}//end iteration

if (document.getElementById('qualification_date')) {
var d = document.getElementById('qualification_date').value;
if (!checkValidDate(d)) {//we'll call a function that uses asynchronous requests to a PHP script
var str = "The qualification date is not valid.";
errArr.push(str);
isError = true;

}//end inner if
}//end outer if

if (document.getElementById('password').value != document.getElementById('confirm_password').value) {
var str = "The password and confirm password fields don't match";
isError = true;
errArr.push(str)
}

//now we'll check whether login, csp or hcp values already exist. Again we'll use an asynchronous call to PHP

if (checkFields(document.getElementById('username').value,'uname')) {
isError = true;
errArr.push("Your chosen username already exists. Please select another");
}

if (document.getElementById('csp_number') && document.getElementById('csp_number').value && checkFields(document.getElementById('csp_number').value,'csp')) {
isError = true;
errArr.push("The CSP number you have entered already exists on our system. Have you already registered?");
}

if (document.getElementById('hcp_number') && document.getElementById('hcp_number').value && checkFields(document.getElementById('hcp_number').value,'hcp')) {
isError = true;
errArr.push("The HCP number you have entered already exists on our system. Have you already registered?");
}
if (!isError) {
return true;
}

else {
//use the DOM to create a UL list of errors to insert before the form
var pNode = document.getElementById('form1').parentNode;
if (document.getElementById('errorlist')) {
pNode.removeChild(document.getElementById('errorlist').previousSibling);
pNode.removeChild(document.getElementById('errorlist'));
}//end if
var phead = document.createElement('p');
phead.setAttribute('class','errorMsg');
pt = document.createTextNode("There seems to be a problem with the information you have provided. Please see the list below for more information");
phead.appendChild(pt);
var elist = document.createElement('ul');
elist.setAttribute('id','errorlist');
for (a=0;a < errArr.length;a++) {
eout = errArr[a].replace(/_/," ");
el = document.createElement('li');
elt = document.createTextNode(eout);
el.appendChild(elt);
elist.appendChild(el);
}//end for loop

//now we'll insert the list into the dom
pNode.insertBefore(elist,document.getElementById('form1'));
pNode.insertBefore(phead,elist);
self.scrollTo(0,0);
return false;
}
}//end form checking function

/*Study Day form routines*/

function clearSiblings() {
var pn = document.getElementById('regApplyForm');
while (pn.childNodes.length > 3) {
pn.removeChild(pn.lastChild);
}
}

function createHiddenFields() {
var pn = document.getElementById('regApplyForm');
var itemname = document.createElement('input');
var itemnumber = document.createElement('input');
var amount = document.createElement('input');
var reg = document.createElement('input');
var formType = document.createElement('input');

itemname.setAttribute('type','hidden');
itemname.type = "hidden";//dirty IE hack
itemname.setAttribute('name','item_name');
itemname.setAttribute('id','item_name');

itemnumber.setAttribute('type','hidden');
itemnumber.type = "hidden";//dirty IE hack
itemnumber.setAttribute('name','item_number');
itemnumber.setAttribute('id','item_number');

amount.setAttribute('type','hidden');
amount.type = "hidden";//dirty IE hack
amount.setAttribute('name','amount');
amount.setAttribute('id','amount');

reg.setAttribute('type','hidden');
reg.type = "hidden";//dirty IE hack
reg.setAttribute('name','reg');
reg.setAttribute('id','reg');

formType.setAttribute('type','hidden');
formType.type = "hidden";//dirty IE hack
formType.setAttribute('name','formType');
formType.setAttribute('id','formType');
formType.setAttribute('value','studyday');

pn.appendChild(itemname);
pn.appendChild(itemnumber);
pn.appendChild(amount);
pn.appendChild(reg);
pn.appendChild(formType);
}

/*BEGIN Adverse Reactions Form*/
function addDrug(d) {
var d=parseInt(d);//makes sure we treat it as an integer
var pe = document.getElementById('injectionDl');
//firstly remove the <a> element from the end of the list. We'll replace it later so the user can add more fields
//iterate the parent element (pe) and remove any <a> from it. This is re-applied at the end of the next batch of fields.
//pe.removeChild(pe.childNodes[pe.childNodes.length-1]);
pe.removeChild(document.getElementById('adddrug'));
//now create the dynamic form fields for data entry. They will copy the static fields exactly and add a <dd id="adddrug"> to the end with a d+1 increment.
var dndt = document.createElement('dt');
var dnlabel = document.createElement('label');
dnlabel.setAttribute('for','suspected_drug_'+d);
var dntext = document.createTextNode('Suspected Drug #'+d);
dnlabel.appendChild(dntext);
dndt.appendChild(dnlabel);

var dndd = document.createElement('dd');
var dnddfield = document.createElement('input');
dnddfield.setAttribute('type','text');
dnddfield.setAttribute('id','suspected_drug_'+d);
dnddfield.setAttribute('name','suspected_drug_'+d);
dndd.appendChild(dnddfield);



var dosedt = document.createElement('dt');
var doselabel = document.createElement('label');
doselabel.setAttribute('for','dose_'+d);
var dntext = document.createTextNode('Dose / Volume #'+d);
doselabel.appendChild(dntext);
dosedt.appendChild(doselabel);


var dosedd = document.createElement('dd');
var dosefield = document.createElement('input');
dosefield.setAttribute('type','text');
dosefield.setAttribute('id','dose_'+d);
dosefield.setAttribute('name','dose_'+d);
dosedd.appendChild(dosefield);


//finally create the "add drug" link so more fields can be added by the user

var adlinkdd = document.createElement('dt');
adlinkdd.setAttribute('id','adddrug');
var adlink = document.createElement('a');
var linktext = document.createTextNode('+ Add Drug');
adlink.setAttribute('href','javascript:void(0)');
adlink.setAttribute('onmouseup','addDrug(\''+(d+1)+'\')');
adlink.onmouseup = new Function('addDrug(\''+(d+1)+'\')');//nasty IE hack again
adlink.appendChild(linktext);
adlinkdd.appendChild(adlink);


//append all the elements to the form in the correct order
pe.appendChild(dndt);
pe.appendChild(dndd);
pe.appendChild(dosedt);
pe.appendChild(dosedd);
pe.appendChild(adlinkdd);
}

/*END Adverse Reactions Form*/


function clearDOB() {
var f= document.getElementById('patient_dob');
var dateFormat = /\d{2}\/\d{2}\/\d{4}/;
if (dateFormat.test(f.value)) {
f.select();
}
else {
f.value = "";
}

}

function checkDOB() {
//formats the D.O.B. string correctly - checks it whilst it's being typed
var f= document.getElementById('patient_dob');
var first = /^\d{2}/;
var second=/^\d{2}\/\d{2}/;
var third=/^\d{2}\/\d{2}\/\d{4}$/;
if (f.value.length == "2" && first.test(f.value)) {
f.value = f.value+"/";
moveCur(f);
}

if (f.value.length == "5" && second.test(f.value)) {
f.value = f.value+"/";
moveCur(f);
}

if (third.test(f.value)) {
document.getElementById('patient_condition').focus();
}

}

function moveCur(t) {
len=t.value.length;
if(t.setSelectionRange){
t.setSelectionRange(len,len)
t.focus()
}else if(t.createTextRange){
var rn=t.createTextRange();
rn.moveStart('character',len)
rn.select()
}
}


function checkOptIn() {
//create and "opt in" checkbox on the main form if someone is SOM or OMS IT qualified
//first we'll create the elements before inserting them before the HEI box
if (!document.getElementById('optin')) {
var pNode = document.getElementById('inject');
var opt = document.createElement('input');
var lopt = document.createElement('label');

opt.setAttribute('type','checkbox');
opt.setAttribute('id','optin');
opt.setAttribute('name','optin');
opt.setAttribute('value','1');
lopt.setAttribute('for','chkomsd');
opttext = document.createTextNode("Make your details searchable?");
lopt.appendChild(opttext);
}

//insert the checkbox AFTER the SOMIT field

var lastField = document.getElementById('chksomd');
if (document.getElementById('chksomd').checked == true || document.getElementById('chkomsd').checked == true) {
var pNode = document.getElementById('inject');
pNode.insertBefore(opt,lastField.nextSibling);
pNode.insertBefore(lopt,document.getElementById('optin'));
}

else if (!document.getElementById('chksomd').checked && !document.getElementById('chkomsd').checked && document.getElementById('optin')) {
var pNode = document.getElementById('inject');
pNode.removeChild(document.getElementById('optin').previousSibling);
pNode.removeChild(document.getElementById('optin'));
}
}


function checkPwd() {
var pass = document.getElementById('password').value;
var conf = document.getElementById('confirm_password').value;

if (pass == conf) {
return true;

}

else {
alert("Password and Confirm Password do not match");
return false;
}
}

function checkContents() {
var dt = document.getElementById('document_title').value;
var f = document.getElementById('ufile').value;

if (!dt || !f) {
alert("You must provide both the Document Title and a file to upload");
return false;
}
else {
return true;
}
}//end function

function checkUpdateContents() {
var dt = document.getElementById('document_title').value;

if (!dt) {
alert("You must provide a Document Title");
return false;
}
else {
return true;
}
}//end function

function checkMembAddData() {
var n = document.getElementById('fullname').value;
var f = document.getElementById('template').value;

if (!n && !f) {
alert("You must provide either an individual name or a preformatted file to upload.");
return false;

} else {
return true;
}


}//end function






