var subjects=new Array("Archaeology","Art history","Atmospheric science","Earth observation","Earth science","Faunal studies","Forensic science","Marine science","Other","Polar science","Science based archaeology","Terrestrial and freshwater science");
var materials=new Array("Antler","Bone","Carbonate","Cellulose","Charcoal","Charred bone","Charred seeds","CO2","Cremated bone","Hair","Insect remains","Ivory","Linen","Other","Peat","Pigment","Plant remains","Plaster","Pottery","Sediment","Seeds","Shell","Soil","Textile","Tissue","Tooth","Water DIC","Water DOC","Wood");
var funding=new Array("NERC grant","NERC training grant","AHRC grant","Leverhulme grant", "Commercial","Other");

var rclemail="m.hastings@nercrcl.gla.ac.uk,s.moreton@suerc.gla.ac.uk";
var orauemail="orau@rlaha.ox.ac.uk,thomas.higham@rlaha.ox.ac.uk";
var nercemail="roen@nerc.ac.uk";

function subjectGetArea(subject)
{
 switch(subject)
 {
 case "Archaeology":
 case "Art history":
 case "Faunal studies":
 case "Science based archaeology":
  return "A";
 case "Atmospheric science":
 case "Earth science":
 case "Earth observation":
 case "Marine science":
 case "Polar science":
 case "Terrestrial and freshwater science":
  return "E";
 case "Other":
 case "Forensic science":
  return "B";
 };
 return false;
};

function materialGetArea(material)
{
 switch(material)
 {
 case "Antler":
 case "Bone":
 case "Charred bone":
 case "Cremated bone":
 case "Hair":
 case "Ivory":
 case "Linen":
 case "Pigment":
 case "Plaster":
 case "Pottery":
 case "Textile":
 case "Tissue":
 case "Tooth":
  return "A";
 case "Peat":
 case "Sediment":
 case "Soil":
 case "Water DIC":
 case "Water DOC":
  return "E";
 case "CO2":
 case "Other":
  return "B";
 case "Carbonate":
 case "Cellulose":
 case "Charcoal":
 case "Charred seeds":
 case "Insect remains":
 case "Plant remains":
 case "Seeds":
 case "Shell":
 case "Wood":
  return "-";
 };
 return false;
};

function makeProjString(no,d)
{
 var t="New";
 if(no)
 {
  t=no.toString();
  if(d)
  {
   m=d.getMonth()+1;
   if(m<10){m="0"+m;};
   t+="."+m+d.getFullYear().toString().slice(2);
  };
 };
 return t;
};

function showProj(spec)
{
 var e,s,m;
 var t;
 var no=spec.object;
 t=makeProjString(no,spec.parent.object.dsub);
 spec.specialTeX=t;
 e=document.createTextNode(t);
 if(no && database.fields.proj_no.linkPage)
 {
  s=document.createElement("SPAN");
  s.className="link";
  s.onclick=doAction;
  s.spec=spec;
  s.appendChild(e);
  e=s;
 };
 spec.container.appendChild(e);
};

function getCouncil(subject,issues)
{
 switch(subject)
 {
 case "Science based archaeology":
  if(issues.culture){return "B";};
  return "E";
 case "Archaeology":
 case "Other":
  if(issues.climate || issues.hazard || issues.gmcrops || issues.biodiversity ||
   issues.evolution || issues.dispersal )
  {
   if(issues.culture){return "B";};
   return "E";
  };
  if(issues.culture || (subject=="Archaeology")){return "A";};
 case "Art history":
  return "A";
 };
 return "E";
};

function getLabs(subject,material)
{
    var subj,mat,orau,rcl,cc;
    orau=false;rcl=false;cc=false;
    subj=subjectGetArea(subject);
    mat=materialGetArea(material);
    switch(subj)
    {
    case "A":
     switch(mat)
     {
      case "-":
      case "A":
       orau=true;
       break;
      case "B":
       orau=true;
       cc=true;
       break;
      case "E":
       rcl=true;
       cc=true;
       break;
     };
     break;
    case "B":
     switch(mat)
     {
      case "A":
       orau=true;
       cc=true
       break;
      case "-":
      case "B":
       orau=true;
       rcl=true;
       break;
      case "E":
       rcl=true;
       cc=true;
       break;
     };
     break;
    case "E":
     switch(mat)
     {
      case "A":
       orau=true;
       cc=true;
       break;
      case "B":
       rcl=true;
       cc=true;
       break;
      case "-":
      case "E":
       rcl=true;
       break;
     };
     break;
    };
    if(rcl)
    {
     if(orau){return "BOTH";};
     if(cc){return "RCLccORAU";};
     return "RCL";
    };
    if(orau) 
    {
     if(cc){return "ORAUccRCL";};
     return "ORAU";
    };
    return false;
};

function getLabsFromForm(o)
{
  var labs,i;
  if(!o.analyses[0]){return false;};
  labs=getLabs(o.context.subject,o.analyses[0].material);
  for(i=1;i<o.analyses.length;i++)
  {
   switch(getLabs(o.context.subject,o.analyses[i].material))
   {
   case "RCL":
    switch(labs)
    {
    case "RCL":
     break;
    case "ORAU":
     labs="BOTH";
     break;
    default:
     labs="RCLccORAU";
     break;
    };
    break;
   case "ORAU":
    switch(labs)
    {
    case "ORAU":
     break;
    case "RCL":
     labs="BOTH";
     break;
    default:
     labs="RCLccORAU";
     break;
    };
    break;
   case "ORAUccRCL":
   case "RCLccORAU":
   case "BOTH":
    break;
   };
  };
  return labs;
};

function getContacts(labs)
{
 switch(labs)
 {
 case "RCL":
  return rclemail;
 case "ORAU":
  return orauemail;
 case "BOTH":
 case "RCLccORAU":
  return rclemail+","+orauemail;
 case "ORAUccRCL":
  return orauemail+","+rclemail;
 };
 return false;
};

function nercFormChecker(spec)
{
 var wrds;
 if(submitter)
 {
  spec.object.pi.name=submitter.prefix+" "+submitter.initials+" "+submitter.surname;
  spec.object.pi.address=submitter.address;
  spec.object.pi.tel=submitter.tel;
  spec.object.pi.fax=submitter.fax;
  spec.object.pi.email=submitter.email;
 };
 spec.readonly=false;
 if(spec.object)
 {
  if(spec.object.header)
  {
   if(spec.object.issues && spec.object.context)
   {
    switch(getCouncil(spec.object.context.subject,spec.object.issues))
    {
    case "A": spec.object.header.council="AHRC";break;
    case "B": spec.object.header.council="Both";break;
    case "E": spec.object.header.council="NERC";break;
    };
   };
   switch(getLabsFromForm(spec.object))
   {
   case "ORAUccRCL":
   case "ORAU":
    spec.object.header.sub=1;
    break;
   default:
    spec.object.header.sub=2;
    break;
   };
   if(spec.parent.object.grade)
   {
    spec.object.header.grade=spec.parent.object.grade;
   };
   if(spec.parent.object.sub)
   {
    spec.object.header.submitted=true;
    spec.readonly=true;
   }
   else
   {
    spec.object.header.subdate=new Date();
    spec.object.header.submitted=false;
   };
  };
  if(spec.object.summary && spec.object.summary.summary)
  {
   wrds=spec.object.summary.summary.split("%20");
   if(wrds.length > 150)
   {
    alert("Summary too long ("+wrds.length+" words)- reducing to ~150 words");
    wrds.length=150;
    spec.object.summary.summary=wrds.join("%20");
   };
  };
 };
 if(!spec.readonly)
 {
  if(!spec.object.required)
  {
   spec.edit=true;
  };
 };
};

function calcApplication(spec)
{
 var o=spec.parent.parent.parent.object;
 if(o.proj_no)
 {
  spec.object=makeProjString(o.proj_no,o.dsub);
 };
};

function calcGrade(spec)
{
 var o=spec.parent.parent.parent.object;
 if(o.grade)
 {
  spec.object=o.grade;
 };
};

function calcNo(spec)
{
 var o=spec.parent.parent.object.analyses;
 var i;
 spec.object=0;
 if(typeof(o)=='undefined'){return;};
 for(i=0;i<o.length;i++)
 {
  spec.object+=o[i].no;
 };
};

function changeStudents(spc)
{
 spc.parent.parent.resTypeSpec.emptyContainer();
 spc.parent.parent.resTypeSpec.changed=true;
 if(spc.object.length)
 {
  spc.parent.parent.resTypeSpec.object="Student research";
 }
 else
 {
  spc.parent.parent.resTypeSpec.object="Academic research";
 };
 spc.parent.parent.resTypeSpec.fillContainer();
};

function nercForm()
{
 var s,ss,sss,ssss;
 spec=new itemSpec("data","Project application form","Object",true);
 spec.checker="nercFormChecker(this)";
 spec.outline=true;
 if(args.page=="nercNew")
 {
  spec.expand=false;
 };

 //header
 s=spec.appendChild("header","","Object");
 s.inline=true;
 ss=s.appendChild("makeEdit","Edit","Button");
 ss.action="editForm(this.parent.parent)";
 ss.readonly=true;
 ss.width=8;
 ss=s.appendChild("save","Save","Button");
 ss.action="databaseAction('Save')";
 ss.readonly=true;
 ss.width=8;
 ss=s.appendChild("print","Print","Button");
 ss.action="printForm(this.parent.parent)";
 ss.readonly=true;
 ss.width=8;
 ss=s.appendChild("submit","Submit","Button");
 ss.action="submitForm(this.parent.parent)";
 ss.readonly=true;
 ss.width=8;
 ss=s.appendChild("council","Council","Text");
 ss.readonly=true;
 ss.width=8;
 ss=s.appendChild("sub","Sub-committee","Number");
 ss.options=new Array("","Archaeology","Environment");
 ss.readonly=true;
 ss.width=16;
 ss=s.appendChild("subdate","Date","Date");
 ss.readonly=true;
 ss.width=12;
 ss=s.appendChild("submitted","Submitted","Boolean");
 ss.readonly=true;
 ss.width=10;
 ss=s.appendChild("requested","No","Number");
 ss.readonly=true;
 ss.width=5;
 ss.calculator="calcNo(this)";
 ss=s.appendChild("application","Application No.","Text");
 ss.readonly=true;
 ss.width=18;
 ss.calculator="calcApplication(this)";
 ss=s.appendChild("grade","Grade","Text");
 ss.readonly=true;
 ss.width=12;
 ss.calculator="calcGrade(this)";

 //project type
 s=spec.appendChild("type","Details of proposal","Object",true);
 ss=s.appendChild("proposal","Nature of proposal","Text");
 ss.options=new Array("New","Resubmission","Follow-up");
 ss=s.appendChild("previous","Previous application No.","Text");
 ss.hint="Application number of previous version of this application - if resubmission or followup";
 ss=s.appendChild("application","Nature of application","Text");
 ss.options=new Array("Full","In principle","Rangefinder");
 ss=s.appendChild("collaborative","Collaborative with lab(s)","Boolean");
 ss.hint="Is this collaborative with one of the radiocarbon labs?";
 ss=s.appendChild("both","Both labs required","Boolean");
 ss.hint="Would this benefit from collaboration with both labs?";
 ss=s.appendChild("contact","Lab contact (if any)","Text");

 //summary
 s=spec.appendChild("summary","Project title and summary","Object",true);
 ss=s.appendChild("projtitle","Title","Text",true);
 ss=s.appendChild("summary","Summary of aims of project and hypotheses being tested (c. 100 words)","TextArea",true);

 //PI
 s=spec.appendChild("pi","Principal investigator","Object",true);
 ss=s.appendChild("name","Name","Text",true);
 ss.readonly=true;ss.hint="Taken from 'Your details'";
 ss=s.appendChild("jes","Je-S User ID","Text",true);
 ss.hint="PI must be eligible to hold NERC/AHRC grant - if no Je-S number write none and explain on CV";
 ss=s.appendChild("gender","Gender","Text",true);
 ss.options=new Array("","F","M");
 ss=s.appendChild("institution","Institution","Text",true);
 ss.options=database.fields.institution.options;
 ss.hint="Name of university or academic institution";
 ss=s.appendChild("status","Current status","Text",true);
 ss.hint="Position within that institution, eg University Lecturer";
 ss=s.appendChild("started","Date current post started","Date",true);
 ss.hint="Date on which you took up this position";
 ss=s.appendChild("address","Address","Pre",true);
 ss.readonly=true;ss.hint="Taken from 'Your details'";
 ss=s.appendChild("tel","Telephone","Text",true);
 ss.readonly=true;ss.hint="Taken from 'Your details'";
 ss=s.appendChild("fax","Fax","Text",false);
 ss.readonly=true;ss.hint="Taken from 'Your details'";
 ss=s.appendChild("email","Email","Text",true);
 ss.readonly=true;ss.hint="Taken from 'Your details'";


 //Personnel
 s=spec.appendChild("personnel","Personnel - Other investigators","Object",true);
 s.outline=true;
 ss=s.appendChild("ci","Co-investigators, lab staff etc.","Array",false);
 sss=ss.appendChild("name","Name","Text",true);
 sss.width=20;
 sss=ss.appendChild("position","Position","Text",true);
 sss.width=20;
 sss=ss.appendChild("institution","Institution","Text",true);
 sss.width=20;
 sss=ss.appendChild("capacity","Capacity in project","Text",true);
 sss.width=30;
 ss=s.appendChild("students","Doctoral student(s)","Array",false);
 ss.changer="changeStudents(this)";
 sss=ss.appendChild("name","Name","Text",true);
 sss.width=22;
 sss=ss.appendChild("funding","Funding","Text",true);
 sss.hint="NERC, NERC Tied, AHRC etc.";
 sss.width=20;
 sss=ss.appendChild("start","Start date","Date",true);
 sss.width=15;
 sss=ss.appendChild("end","End date","Date",true);
 sss.width=15;
 sss=ss.appendChild("visit","Will student visit lab","Boolean");
 sss.width=15;
 ss=s.appendChild("thesis","Significance to student thesis (written by student)","Object",true);
 sss=ss.appendChild("summary","Summary (c. 200-300 words)","TextArea",false);
 sss.hint="Summary, written by the student, of the significance of this proposal to their thesis";
 ss=s.appendChild("contact","Main contact (if not PI)","Object");
 ss.hint="Contact details if the PI is not the main contact for this application";
 ss.inline=true;
 sss=ss.appendChild("name","Name","Text");
 sss.width=18;
 sss=ss.appendChild("tel","Telephone","Text");
 sss.width=18;
 sss=ss.appendChild("fax","Fax","Text");
 sss.width=18;
 sss=ss.appendChild("email","Email","Text"); 
 sss.width=36;
 
 // Funding
 s=spec.appendChild("funding","Sources of funding for overall project","Array",true);
 ss=s.appendChild("type","Funding type","Text");
 ss.width=20;
 ss.options=funding;
 ss=s.appendChild("ref","Grant number","Text",true); 
 ss.width=20;
 ss=s.appendChild("title","Grant title","Text",true); 
 ss.width=50;

 //Timing
 s=spec.appendChild("timing","Timing","Object",true);
 ss=s.appendChild("start","Project start","Date",true);
 ss.hint="Give date in format 24/10/2007 or 2007-10-24";
 ss=s.appendChild("sub","Expected sample submission","Date",true);
 ss.hint="Give date in format 24/10/2007 or 2007-10-24";
 ss=s.appendChild("expected","Results expected by","Date",true);
 ss.hint="Give date in format 24/10/2007 or 2007-10-24";
 ss=s.appendChild("end","Project end","Date",true);
 ss.hint="Give date in format 24/10/2007 or 2007-10-24";
 ss=s.appendChild("explain","If timing critical please explain","TextArea"); 

 //Analyses requested
 s=spec.appendChild("analyses","Analyses requested","Array",true);
 ss=s.appendChild("material","Type","Text",true);
 ss.options=materials;
 ss.width=30;
 ss=s.appendChild("no","No. requested","Number",true);
 ss.width=20;
 ss=s.appendChild("further","Possible extra","Number",false);
 ss.width=20;
 ss.hint="If project successful: possible further numbers";
 ss=s.appendChild("extra","Extra standards","Text");
 ss.width=20;
 ss.hint="Special standards required (for lab use only)";
 ss.readonly=true;

 //Requirements
 s=spec.appendChild("special","Special requirements","Object");
 s.hint="Always contact lab staff if ticking any of  these";
 ss=s.appendChild("fast","Fast turnaround","Boolean");
 ss.hint="This must be justified above";
 ss=s.appendChild("small","Small samples (< 500ugC)","Boolean");
 ss=s.appendChild("hazard","Health hazard","Boolean");
 ss.hint="Any bio-hazard, toxic materials etc";
 ss=s.appendChild("contact","Lab personnel contacted","Text");
 ss.hint="Required if any of the above are ticked";

 //Research context
 s=spec.appendChild("context","Research context","Object",true);
 ss=s.appendChild("type","Type of research","Text",true);
 ss.options=new Array("Academic research","Student research","NERC core programme","NERC  commissioned research");
 spec.resTypeSpec=ss;
 ss=s.appendChild("subject","Research area","Text",true);
 ss.options=subjects;

 //Issues
 s=spec.appendChild("issues","Research themes in this project","Object")
 s.hint="Tick as many as might be relevant and at least one";
 ss=s.appendChild("biodiversity","Biodiversity","Boolean");
 ss=s.appendChild("climate","Climate system","Boolean");
 ss=s.appendChild("earth_system","Earth system science","Boolean");
 ss=s.appendChild("pollution","Environment, pollution & human health","Boolean");
 ss=s.appendChild("culture","Human culture","Boolean");
 ss=s.appendChild("evolution","Human evolution","Boolean");
 ss=s.appendChild("dispersal","Human migration","Boolean");
 ss=s.appendChild("hazard","Natural hazards","Boolean");
 ss=s.appendChild("resources","Sustainable use of natural resources","Boolean");
 return spec;
};


