// A collection of functions to validate form fields

// ***************************************************************************
// * Description                                                  *   Date   *
// ***************************************************************************
// * New                                                          | 27/04/05 *
// *--------------------------------------------------------------+----------*
// * Added validation of movie thumbnail upload                   | 29/04/05 *
// *--------------------------------------------------------------+----------*
// * Added validation of media rename option                      | 17/05/05 *
// *--------------------------------------------------------------+----------*
// * Added validation of file actions                             | 18/05/05 *
// *--------------------------------------------------------------+----------*
// * Added validation of search field                             | 20/05/05 *
// *--------------------------------------------------------------+----------*
// * Added illegal char checking for file/dir renames, dir creatn | 23/05/05 *
// *--------------------------------------------------------------+----------*
// * Added illegal char checking for search function - don't allow| 27/05/05 *
// * '<' or '>' to prevent someone embedding html code in the srch|          *
// *--------------------------------------------------------------+----------*
// * Added illegal char checking for login screen                 | 31/05/05 *
// *--------------------------------------------------------------+----------*
// * Added "&" and "'" to illegal char list for names - its just  | 01/06/05 *
// * far too much hassle and they're not needed thanks to captions|          *
// *--------------------------------------------------------------+----------*
// * Minor grammer changes only.                                  | 23/06/05 *
// *--------------------------------------------------------------+----------*
// * Added validation of large version upload form.               | 04/08/05 *
// *--------------------------------------------------------------+----------*
// * Deleted validate_edit_description_action() - no longer needed| 28/09/05 *
// *--------------------------------------------------------------+----------*
// * User can no longer try to rename a dir to " " - still needs  | 12/10/05 *
// * further improvement - i.e. "  " is currently allowed (fails).|          *
// *--------------------------------------------------------------+----------*
// * Added "+" to illegal char list for names.                    | 11/11/05 *
// * Added illegal char checking when renaming files.             |          *
// *--------------------------------------------------------------+----------*
// * Added validation of reduced points gpx upload form.          | 07/08/06 *
// *--------------------------------------------------------------+----------*
// * Fixed Most warning produced by JSLint.                       | 23/08/06 *
// *--------------------------------------------------------------+----------*
// * Added validation that new gpx file name is entered when      | 20070304 *
// *       set_gpx_name option is chosen.                         |          *
// *--------------------------------------------------------------+----------*
// * Added prevent_chars() function to prevent illegal chars in   | 2008100? *
// *       text input boxes                                       |          *
// *--------------------------------------------------------------+----------*
// ***************************************************************************


// to do:

// validate the user login screen
function validate_login() {
  if (isEmpty(document.login.input_username.value)) {
    alert("Please enter Username");
    return false;
  } else {  
    if (isEmpty(document.login.input_password.value)) {
      alert("Please enter Password");
      return false;
    }
  }
  
  // check they don't contain illegal characters
  var illegalChars = /\<|\>/;
  if (illegalChars.test(document.login.input_username.value) == true) {
    alert("Username cannot contain any of the following characters:\n                                      < >");
    return false;
  } else {
    if (illegalChars.test(document.login.input_password.value) == true) {
      alert("Password cannot contain any of the following characters:\n                                      < >");
      return false;
    }
  }
  return true;
}


// validate the directory actions
function validate_directory_action(dir_in) {

  // an action must be selected from the dropdown box
  if (isEmpty(document.getElementById(dir_in).action.value)) {
  //~ if (isEmpty(document.getElementById(dir_in).value)) {                // firefox - but breaks the checking of the dropdown list even when something is selected...
    alert("Choose an action for this directory from the dropdown list");
    return false;
  }
  
  // if we're renaming a dir check we have been given the new name
  if (document.getElementById(dir_in).action.value=="rename_dir_and_file") {
  //~ if (document.getElementById(dir_in).value=="rename_dir_and_file") {
    if (isEmpty(document.getElementById(dir_in).new_name.value)) {
      alert("Please enter the new directory name in the box");
      return false;
    }
        
    // check the new name doesn't contain illegal characters
    var illegalChars = /\\|\/|\:|\*|\?|\"|\<|\>|\||#|&|'|\+/;
    if (illegalChars.test(document.getElementById(dir_in).new_name.value) == true) {
      alert("A directory name cannot contain any of the following characters:\n                          \\ / : * ? \" < > | # & ' +");
      return false;
    }
    
    // check the new name isn't just a space - this really needs to be a call to a function
    // that checks that the new name isn't comprised of just spaces - however many
    if (document.getElementById(dir_in).new_name.value==" ") {
      alert("Name cannot be just a space");
      return false;
    }
  }
  return true;
}

// validate the file actions
function validate_file_action(file_in) {

  // an action must be selected from the dropdown box
  if (isEmpty(document.getElementById(file_in).action.value)) {
    alert("Choose an action for this file from the dropdown list");
    return false;
  }
  
  // if we're renaming a file check we have been given the new name
  if ((document.getElementById(file_in).action.value=="rename_dir_and_file") || (document.getElementById(file_in).action.value=="set_gpx_name")) {
    if (isEmpty(document.getElementById(file_in).new_name.value)) {
      alert("Please enter the new name in the box");
      return false;
    }
    
    // check the new name doesn't contain illegal characters
    var illegalChars = /\\|\/|\:|\*|\?|\"|\<|\>|\||#|&|'|\+/;
    if (illegalChars.test(document.getElementById(file_in).new_name.value) == true) {
      alert("A file name cannot contain any of the following characters:\n                     \\ / : * ? \" < > | # & ' +");
      return false;
    }
    
    // check the new name isn't just a space - this really needs to be a call to a function
    // that checks that the new name isn't comprised of just spaces - however many
    if (document.getElementById(file_in).new_name.value==" ") {
      alert("Name cannot be just a space");
      return false;
    }
  }
  return true;
}

// validate the image actions
function validate_image_action() {

  // an action must be selected from the dropdown box
  if (isEmpty(document.image_action.action.value)) {
    alert("Choose an option from the dropdown list");
    return false;
  }
  
  // if we're renaming a media file check we have been given the new name
  if (document.image_action.action.value=="rename_media") {
    if (isEmpty(document.image_action.new_media_name.value)) {
      alert("Please enter the new media name in the box");
      return false;
    }
    
    // check the new name doesn't contain illegal characters
    var illegalChars = /\\|\/|\:|\*|\?|\"|\<|\>|\||#|&|'|\+/;
    if (illegalChars.test(document.image_action.new_media_name.value) == true) {
      alert("A filename cannot contain any of the following characters:\n                     \\ / : * ? \" < > | # & ' +");
      return false;
    }
  }
  return true;
}

// validate the create directory button
function validate_create_directory_button() {

  // check we know what to call the new directory
  if (isEmpty(document.create_directory_button.dir_to_create.value)) {
      alert("Please enter the new directory name in the box");
      return false;
  }
  
   // check the new name doesn't contain illegal characters
  var illegalChars = /\\|\/|\:|\*|\?|\"|\<|\>|\||#|&|'|\+/;
  if (illegalChars.test(document.create_directory_button.dir_to_create.value) == true) {
    alert("A directory name cannot contain any of the following characters:\n                          \\ / : * ? \" < > | # & ' +");
    return false;
  }
  return true;
}

// validate the search button
function validate_search_button() {

  // check we know what to seacrh for
  if (isEmpty(document.search_button.search.value)) {
      alert("Please enter search criteria in the box");
      return false;
  }
  
   // check the search criteria doesn't contain illegal characters
  var illegalChars = /\<|\>/;
  if (illegalChars.test(document.search_button.search.value) == true) {
    alert("A search cannot contain any of the following characters:\n                                      < >");
    return false;
  }
  
  return true;
}

// validate the 'send this file' button
function validate_send_this_file_button() {

  // check a file name has been entered
  if (isEmpty(document.send_this_file_button.userfile.value)) {
      alert("Please enter file name to upload");
      return false;
  }
  return true;
}


// validate the 'send this movie thumbnail' button
function validate_send_this_movie_thumbnail_button() {

  // check a file name has been entered
  if (isEmpty(document.send_this_movie_thumbnail_button.movie_thumb.value)) {
      alert("Please enter file name to upload");
      return false;
  }
  return true;
}

// validate the 'upload large version' button
function validate_send_this_large_version_button() {

  // check a file name has been entered
  if (isEmpty(document.send_this_large_version_button.large_version.value)) {
      alert("Please enter file name to upload");
      return false;
  }
  return true;
}

// validate the 'upload reduced gpx' button
function validate_send_this_reduced_version_button(file_in) {
//~ alert(file_in);
  // check a file name has been entered
  if (isEmpty(document.getElementById(file_in).reduced_version.value)) {
  //~ if (isEmpty(document.send_this_reduced_version_button.reduced_version.value)) {
      alert("Please enter file name to upload");
      return false;
  }
  return true;
}

// check whether a field is empty
function isEmpty(s) { 
  return ((s == null) || (s.length == 0));
}

// check for illegal chars in string
function validate_char(field) { 
 var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 var ok = "yes"; 
 var temp; 
 for (var i=0; i<field.value.length; i++) { 
   temp = "" + field.value.substring(i, i+1); 
   if (valid.indexOf(temp) == "-1") ok = "no"; 
 } 
 if (ok == "no") { 
   alert("Invalid entry!  Only characters Aa-Zz are accepted!"); 
   field.focus(); 
   field.select(); 
 } 
} 

function prevent_chars() {

  str = document.FormDeleteTCX.TCXName.value
  //~ alert("Checking char " + str);

   // check the new name doesn't contain illegal characters
  var illegalChars = /\\|\/|\:|\*|\?|\"|\<|\>|\||#|&|'|\+/;
  if (illegalChars.test(document.FormDeleteTCX.TCXName.value) == true) {
    //~ alert("The name cannot contain any of the following characters:\n                     \\ / : * ? \" < > | # & ' +");
    document.FormDeleteTCX.TCXName.value = document.FormDeleteTCX.TCXName.value.replace(/\\|\/|\:|\*|\?|\"|\<|\>|\||#|&|'|\+/g,'')
  }
  //~ return true;
  
  //~ var field = document.forms[0].field1
  //~ var valo = new String();
  //~ var numere = "0123456789.";
  //~ var chars = field.value.split(""); 
  //~ for (i = 0; i < chars.length; i++) {
  //~ if (numere.indexOf(chars[i]) != -1) valo += chars[i];
  //~ else{alert("No non-numeric allowed!");}
  //~ }
  //~ if (field.value != valo) field.value = valo; 
}