//JAVASCRIPT FILE COPY OF WHAT USED TO BE IN ALL PERL SCRIPTS//

//for adding and removing html fields

  function addField()
  {
var mainOb = document.getElementById('mainDiv');
var valOb = document.getElementById('val');
var num = (document.getElementById("val").value -1)+ 2;
var num2 = (document.getElementById("val").value -1)+ 2;
valOb.value = num;
var newDivName = "my"+num+"Div";

var newBoxName = "another"+num;
var newDiv = document.createElement('div');
newDiv.setAttribute("id",newDivName);
newDiv.innerHTML = "<table width='100%' border='0' cellpadding='3' cellspacing='0' name='"+newBoxName+"' + i><tr><td><strong>Collaborator Name:</strong><br />&nbsp;</td><td><input type='text' id='poc' maxlength='50 name='"+newDivName+"'></td>&nbsp;<td><strong>Institution</strong></td><td><input type='text' id='institution' maxlength='250' name='"+newDivName+"'></td></tr>&nbsp;<tr><td><strong>Postal Address</strong><br />&nbsp;</td><td><textarea id='address' maxlength='2000' rows='3' align='left' wrap'soft' name='"+newDivName+"'></textarea></td>&nbsp;<td><strong>Country</strong></td><td><input id='country'' maxlength='500' type='text' name='"+newDivName+"'></td></tr>&nbsp;<tr><td><strong>Post Code/Zip Code</strong><br />&nbsp;</td><td><input id='postcode' maxlength='25' type='text' name='"+newDivName+"'></td>&nbsp;<td><strong>Email</strong></td><td><input id='email' id='email' maxlength='75' type='text' name='"+newDivName+"'></td></tr>&nbsp;<tr><td><strong>Website</strong></td><td><input id='website' maxlength='100' type='text' value='http://' name='"+newDivName+"'></td></tr>&nbsp;<tr><td td width='50%'><strong>Is this person a point of contact for this project?</strong><br />&nbsp;</td><td width='10%'><input type='radio' id='poc' value='1'  name='"+newDivName+"'>Yes<br></td><td><input type='radio' id='poc' value='0' checked  name='"+newDivName+"'> No<br></td></tr></table>&nbsp;<a href='#' onclick='removeField(\""+newDivName+"\");'>Remove Field</a><hr />";
mainOb.appendChild(newDiv);

}
  
  
  function removeField(divNam)
  {
  var o = document.getElementById('mainDiv');
  var oldDiv = document.getElementById(divNam);
  o.removeChild(oldDiv);
  }


function edit_pass(a, b, c)
	{
	var request=GXmlHttp.create();
	var url = '/tephra/update_user.pl?username='+a+'&password1='+b+'&password2='+c;
	request.open("GET",url,false);
	request.send(null); 
	}

function sup_sub(a)
	{
	var request=GXmlHttp.create();
	var url = '/tephra/add_project2.pl?sup_sub='+a;
	request.open("GET",url,false);
	request.send(null); 
	}

function search (a)
{
var request=GXmlHttp.create();
var url = '/real/explore.pl?keyword='+a;
request.open("GET",url,false);
request.send(null); 
}

function fieldSearch (a, b)
{
var request=GXmlHttp.create();
var url = '/real/explore.pl?field='+a+'&field2='+b;
request.open("GET",url,false);
request.send(null); 
}

function region (a)
{
var request=GXmlHttp.create();
var url = '/real/region_search.pl?parish='+a;
request.open("GET",url,false);
request.send(null); 
}


function timeSearch (a, b, c)
{
var request=GXmlHttp.create();
var url = '/real/explore.pl?start_end='+a+'&maths='+b+'&year='+c;
request.open("GET",url,false);
request.send(null); 
}


function addComment(a, b, c)
{
document.getElementById("postComments").src="/cgi_bin/test/comments.pl?comments="+a.value+'&name='+b.value+'&ID='+c.value;
}


function submitProject (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)
{
var request=GXmlHttp.create();
var url = '/real/input.pl?northlimit='+a+'&eastlimit='+b+'&southlimit='+c+'&westlimit='+d+'&title='+e+'&abstract='+f+'&categ='+g+'&kwords='+h+'&sporg='+i+'&cov_area='+j+'&loc_auth='+k+'&other='+l+'&StartYear='+m+'&EndYear='+n+'&poc='+o+'&phone='+p+'&addr='+q+'&wsite='+r+'&pcode='+s+'&email='+t+'&code='+u+'&photo='+v;
request.open("GET",url,false);
request.send(null); 
}


function editProject (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)
{
var request=GXmlHttp.create();
var url = '/real/update.pl?northlimit='+a+'&eastlimit='+b+'&southlimit='+c+'&westlimit='+d+'&title='+e+'&abstract='+f+'&categ='+g+'&kwords='+h+'&sporg='+i+'&cov_area='+j+'&loc_auth='+k+'&other='+l+'&StartYear='+m+'&EndYear='+n+'&poc='+o+'&phone='+p+'&addr='+q+'&wsite='+r+'&pcode='+s+'&email='+t+'&code='+u+'&id='+v;
request.open("GET",url,false);
request.send(null); 
}


document.write('<style type="text/css">');
document.write('div.domtab div{display:none;}<');
document.write('/s'+'tyle>');


function update_user(a, b, c, d, e, f, g, h, i, j, k)
{
var request=GXmlHttp.create();
var url = '/real/update_user.pl?FirstName='+a+'&LastName='+b+'&org='+c+'&email='+d+'&phone='+e+'&PostAddress='+f+'&PostCode='+g+'&username='+h+'&password='+i;
request.open("GET",url,false);
request.send(null); 
}

// Validation for registration

function validateFormOnSubmit(theForm) {
var reason = "";

reason += validateName(theForm.name);
reason += validateInst(theForm.inst);
reason += validateEmail(theForm.email);
reason += validatePhone(theForm.phone);
reason += validateAddr(theForm.address);
reason += validatePcode(theForm.postcode);
reason += validateCountry(theForm.country);
reason += validateWsite(theForm.website);
reason += validateUsername(theForm.username);
reason += validatePassword(theForm.password);
reason += validateConfpassword(theForm.confpassword);
reason += validatecheckPasses(theForm.password, theForm.confpassword);




if (reason != "") 
	{
	alert("Please correct the following fields:\n\n" + reason);
	return false;
	}

else
	{

	}
}



// if a field is left blank but shouldn't be
function validateEmpty(fld) {
var error = "";

if (fld.value.length == 0) {
  fld.style.background = 'Yellow'; 
  error = "The required field has not been completed.\n"
} else {
  fld.style.background = 'White';
}
return error;  
}

// Validation for the user name
// cannot be blank
//cannot contain illegal chars
function validateName(fld) {
var error = "";
var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/; // allow letters, numbers, and underscores

if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter your name.\n";
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "Your name contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

// validation for the user org
// can be blank
// if not blank, cannot contain illegal chars
function validateInst(fld) {
var error = "";
var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/; // allow letters, numbers, and underscores

if (fld.value == "") {
  fld.style.background = 'White';
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "Your organisation name contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

// trims whitespace and illegal chars
function trim(s)
{
return s.replace(/^\s+|\s+$/, '');
}

// validation of email add
// cannot be blank
//  must contain @ symbol etc
// cannot contain illegal chars
function validateEmail(fld) {
var error="";
var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

if (fld.value == "") {
  fld.style.background = 'Yellow';
  error = "Please enter an email address.\n";
} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
  fld.style.background = 'Yellow';
  error = "Please enter a valid email address.\n";
} else if (fld.value.match(illegalChars)) {
  fld.style.background = 'Yellow';
  error = "Your email address contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

// validation of phone num
// can be blank
// if not blank, cannot contain illegal chars
// if not blank, cannot be wrong length

function validatePhone(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

if (fld.value == "") {
fld.style.background = 'White';
} else if (isNaN(parseInt(stripped))) {
  error = "The phone number contains illegal characters, please user numbers only.\n";
  fld.style.background = 'Yellow';
} else if (!(stripped.length >= 11)) {
  error = "The phone number is too short. Make sure you included an area code.\n";
  fld.style.background = 'Yellow';
} else {
  fld.style.background = 'White';
}
return error;
}

// validation of user address
// cannot be blank
function validateAddr(fld) {
var error = "";

if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter an address.\n"
} else {
  fld.style.background = 'White';
}
return error;
}


// validation of postcode
// can be blank
// if not blank, cannot contain illegal chars
function validatePcode(fld) {
var error = "";
var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/; // allow letters, numbers, and underscores

if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter a postcode.\n"
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "Your postcode contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

// validation of user country
// cannot be blank
function validateCountry(fld) {
var error = "";

if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter a country.\n"
} else {
  fld.style.background = 'White';
}
return error;
}

// validation of user website
// can be blank
// cannot contain illegal chars
function validateWsite(fld) {
var error = "";

if (fld.value == "") {
  fld.style.background = 'White'; 
} 
return error;
}



function validateUsername(fld) {
var error = "";
var illegalChars = /\W/; // allow letters, numbers, and underscores

if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter a username.\n";
} else if ((fld.value.length < 3) || (fld.value.length > 25)) {
  fld.style.background = 'Yellow'; 
  error = "Please enter a username between 4 and 25 characters in length.\n";
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "The username contains illegal characters.  Please user letters, numbers or underscores only.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

function validatecheckPasses(fld, fld2) {
var error = "";

if (fld.value == "") {
  fld.style.background = 'Yellow';
} else if (fld.value != fld2.value) {
  fld.style.background = 'Yellow';
  error = "Your passwords do not match.\n";
} else {
  fld.style.background = 'White';
}
return error;
} 


function validatePassword(fld) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers 

if (fld.value == "") {
  fld.style.background = 'Yellow';
  error = "Please enter a password.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
  error = "Your password should be between 5 and 15 characters long. \n";
  fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
  error = "Your password contains illegal characters. Please user letters, numbers or underscores only. \n";
  fld.style.background = 'Yellow';
} else {
  fld.style.background = 'White';
}
return error;
}  



function validateConfpassword(fld) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers 

if (fld.value == "") {
  fld.style.background = 'Yellow';
  error = "Please confirm your password.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
  error = "Your password should be between 5 and 15 characters long. \n";
  fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
  error = "Your password contains illegal characters. Please user letters, numbers or underscores only. \n";
  fld.style.background = 'Yellow';
} else {
  fld.style.background = 'White';
}
return error;
}  

//Validation for projects which are neither super nor subordinate

function validateFormOnSubmit2(theForm) {
var reason = "";

reason += validateTitle(theForm.title);
reason += validateCoverage(theForm.coverage);
reason += validateStart(theForm.start);
reason += validateEnd(theForm.end);


if (reason != "") 
	{
	alert("Please correct the following fields:\n\n" + reason);
	return false;
	}

else
	{

	}
}


// if a field is left blank but shouldn't be
function validateEmpty(fld) {
var error = "";

if (fld.value.length == 0) {
    fld.style.background = 'Yellow'; 
    error = "i. The required field has not been filled in.\n"
} else {
    fld.style.background = 'White';
}
return error;  
}

// Validation for the project title
// cannot be blank
// cannot contain illegal chars, chars are stripped out
function validateTitle(fld) {
var error = "";

if (fld.value == "") {
    fld.style.background = 'Yellow'; 
    error = "Please enter a project title.\n";
} else {
    fld.style.background = 'White';
}
return error;
}


// if a field is left blank but shouldn't be
function validateCoverage(fld) {
var error = "";

if (fld.value.length == 0) {
    fld.style.background = 'Yellow'; 
    error = "Please select the coverage area of the project.\n"
} else {
    fld.style.background = 'White';
}
return error;  
}


// if a field is left blank but shouldn't be
function validateStart(fld) {
  var error = "";
<!-- Get current year so the year is always correct -->
var theDate = new Date();
var theYear = theDate.getFullYear();
  if (fld.value.length == 0) {


  fld.style.background = 'Yellow'; 
      error = "Please enter the project start date.\n"
	} else if (fld.value.length <= 3) {
      fld.style.background = 'Yellow'; 
      error = "Please enter a valid date (e.g. "+theYear+").\n"
  } else if (fld.value.length >= 5) {
      fld.style.background = 'Yellow'; 
      error = "Please enter a valid date (e.g. "+theYear+").\n"
  } else {
      fld.style.background = 'White';
  }
  return error;  
}


// trims whitespace and illegal chars
function trim(stringToTrim)
{
return stringToTrim.replace(" ", "");
}


// if a field is left blank but shouldn't be
function validateEnd(fld) {
  var error = "";
<!-- Get current year so the year is always correct -->
  var theDate = new Date();
  var theYear = theDate.getFullYear();

if (fld.value.match(/ongoing/ig)) {


fld.style.background = 'White';
  } else if (isNaN(fld.value)) {
      fld.style.background = 'Yellow'; 
      error = "Please enter a valid date (e.g. "+theYear+" or 'Ongoing').\n"
  } else if (fld.value.length == 0) {
      fld.style.background = 'Yellow'; 
      error = "Please enter the project end date.\n"
  } else if (fld.value.length <= 3) {  
      fld.style.background = 'Yellow'; 
      error = "Please enter a valid date (e.g. "+theYear+" or 'Ongoing').\n"
  } else if (fld.value.length >= 5) {
      fld.style.background = 'Yellow'; 
      error = "Please enter a valid date (e.g. "+theYear+" or 'Ongoing').\n"
  } else {
      fld.style.background = 'White';
  }
  return error;  
}

//Validation for password change

function validateFormOnSubmit3(theForm) {
var reason = "";

reason += validatepassword1(theForm.password1);
reason += validatepassword2(theForm.password2);
reason += validatepassword3(theForm.password3);
reason += validatecheckPasses(theForm.password2, theForm.password3);

if (reason != "") 
	{
	alert("Please correct the following fields:\n\n" + reason);
	return false;
	}

else
	{

	}
}

function validatecheckPasses(fld, fld2) {
var error = "";

if (fld.value == "") {
  fld.style.background = 'Yellow';
} else if (fld.value != fld2.value) {
  fld.style.background = 'Yellow';
  error = "Your passwords do not match.\n";
} else {
  fld.style.background = 'White';
}
return error;
} 


function validatepassword1(fld) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers 

if (fld.value == "") {
  fld.style.background = 'Yellow';
  error = "Please enter your password.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
  error = "Your password should be between 5 and 15 characters long. \n";
  fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
  error = "Your password contains illegal characters. Please user letters, numbers or underscores only. \n";
  fld.style.background = 'Yellow';
} else {
  fld.style.background = 'White';
}
return error;
}  


function validatepassword2(fld) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers 

if (fld.value == "") {
  fld.style.background = 'Yellow';
  error = "Please enter a new password.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
  error = "Your password should be between 5 and 15 characters long. \n";
  fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
  error = "Your password contains illegal characters. Please user letters, numbers or underscores only. \n";
  fld.style.background = 'Yellow';
} else {
  fld.style.background = 'White';
}
return error;
}  

function validatepassword3(fld) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers 

if (fld.value == "") {
  fld.style.background = 'Yellow';
  error = "Please confirm your new password.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
  error = "Your password should be between 5 and 15 characters long. \n";
  fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
  error = "Your password contains illegal characters. Please user letters, numbers or underscores only. \n";
  fld.style.background = 'Yellow';
} else {
  fld.style.background = 'White';
}
return error;
} 


//Validation for supername


function validateFormOnSubmit4(theForm) {
var reason = "";

reason += validateSupername(theForm.super_name);


if (reason != "") 
	{
	alert("Please correct the following fields:\n\n" + reason);
	return false;
	}

else
	{

	}
}
// Validation for the super project name
// cannot be blank
//cannot contain illegal chars
function validateSupername(fld) {
var error = "";
var illegalChars = /[\(\)\<\>\\\/\"\[\]]/; // allow letters, numbers, and underscores

if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter your project name.\n";
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "Your project name contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}


//Validation for addition of new collaborators

function validateCollaborator(theForm) {
var reason = "";

reason += validatename(theForm.name);
reason += validateInst(theForm.institution);
reason += validateCountry(theForm.country);
reason += validatePcode(theForm.postcode);
reason += validatePhone(theForm.phone);
reason += validateEmail(theForm.email);


if (reason != "") 
	{
	alert("Please correct the following fields:\n\n" + reason);
	return false;
	}

else
	{

	}
}
// Validation for collaborator name
// cannot be blank
//cannot contain illegal chars
function validatename(fld) {
var error = "";
var illegalChars = /[\(\)\<\>\\\/\"\[\]]/; // allow letters, numbers, and underscores

if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter the collaborator's name.\n";
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "The collaborator's name contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

// validation for the user institution
// can be blank
// if not blank, cannot contain illegal chars
function validateInst(fld) {
var error = "";
var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/; // allow letters, numbers, and underscores

if (fld.value == "") {
  fld.style.background = 'White';
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "Your organisation name contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}


// validation of email add
// can be blank
//  must contain @ symbol etc if not blank
// cannot contain illegal chars
function validateEmail(fld) {
var error="";
var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

if (fld.value == "") {
  fld.style.background = 'White';
} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
  fld.style.background = 'Yellow';
  error = "Please enter a valid email address.\n";
} else if (fld.value.match(illegalChars)) {
  fld.style.background = 'Yellow';
  error = "Your email address contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

// validation of phone num
// can be blank
// if not blank, cannot contain illegal chars
// if not blank, cannot be wrong length

function validatePhone(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

if (fld.value == "") {
fld.style.background = 'White';
} else if (isNaN(parseInt(stripped))) {
  error = "The phone number contains illegal characters, please user numbers only.\n";
  fld.style.background = 'Yellow';
} else if (!(stripped.length >= 11)) {
  error = "The phone number is too short. Make sure you included an area code.\n";
  fld.style.background = 'Yellow';
} else {
  fld.style.background = 'White';
}
return error;
}

// validation of postcode
// can be blank
// if not blank, cannot contain illegal chars
function validatePcode(fld) {
var error = "";
var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/; // allow letters, numbers, and underscores

if (fld.value == "") {
fld.style.background = 'White';
} else if (illegalChars.test(fld.value)) {
  fld.style.background = 'Yellow'; 
  error = "Your postcode contains illegal characters.\n";
} else {
  fld.style.background = 'White';
}
return error;
}

// validation of user country
// can be blank
function validateCountry(fld) {
var error = "";

if (fld.value == "") {
fld.style.background = 'White';
} else {
  fld.style.background = 'White';
}
return error;
}

//Validation for addition of content


function validateContent(theForm) {
var reason = "";

reason += validateAddContent(theForm.photo, theForm.url);
reason += validateDescription(theForm.description);


if (reason != "") 
	{
	alert("Please correct the following fields:\n\n" + reason);
	return false;
	}

else
	{

	}
}
// Validation for the description
// cannot be blank
function validateDescription(fld) {
var error = "";


if (fld.value == "") {
  fld.style.background = 'Yellow'; 
  error = "Please enter a description of the content.\n";
} else {
  fld.style.background = 'White';
}
return error;
}


function validateAddContent(fld, fld2) {
var error = "";

if (fld.value == "" && fld2.value == "") {
  fld.style.background = 'Yellow';
  fld2.style.background = 'Yellow';
  error = "Please enter some content.\n";
} else if (fld.value != "" && fld2.value != "") {
   fld.style.background = 'Yellow';
   fld2.style.background = 'Yellow';
  error = "Please upload a file OR add a URL.\n";
} else {
  fld.style.background = 'White';
}
return error;
} 

//text field counter

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) {
  field.value = field.value.substring(0, maxlimit);
 }
else {
  countfield.value = maxlimit - field.value.length;
 }
}

