// email

function checkEmail (strng, name) {
var error="";
if (strng == "") {
   error = "You didn't enter a "+name+" email address.\r\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\r\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\r\n";
       }
    }
return error;    
}


function checkUrl(strng, name) {
var error="";
if (strng == "") {
   error = "You didn't enter a "+name+" website.\r\n";
}

    var urlFilter=/^((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?$/;
    if (!(urlFilter.test(strng))) { 
       error = "Please enter a valid website.\r\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The website contains illegal characters.\r\n";
       }
    }
return error;    
}

// phone number - strip out delimiters

function checkPhone (strng, name) {
var error = "";
if (strng == "") {
   error = "You didn't enter a "+name+" phone number.\r\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.\r\n";
  
    }
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng1, strng2) {
var error = "";
if (strng1 == "" || strng2 == "") {
   error = "You didn't enter a password.\r\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if (strng1 != strng2) {
       error = "The passwords entered do not match.\r\n";
    }    
    else if ((strng1.length < 6) || (strng1.length > 20)) {
      error = "The password must contain at least 6 characters and no more than 20.\r\n";
    }
    else if (illegalChars.test(strng1)) {
      error = "The password contains illegal characters.\r\n";
    }
    else if (!((strng1.search(/(a-z)+/)) && (strng1.search(/(A-Z)+/)) && (strng1.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\r\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a Username.\r\n";
}

    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 50)) {
       error = "The username must be at least 4 characters and no more than 50.\r\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The Username contains illegal characters.\r\n";
    } 
return error;
}       

// numeric Value

function checkValue (val, name, min, max) {
var error = "";

    if ((val < min) || (val > max)) {
       error = "The "+name+" must be between "+min+" and "+max+".\r\n";
    }
return error;
}

// length.

function checkLength (strng, name, min, max) {
var error = "";

    if ((strng.length < min) || (strng.length > max)) {
       error = "The "+name+" is the wrong length, must be between "+min+" and "+max+" characters.\r\n";
    }
return error;
}

// non-empty textbox

function isEmpty(strng, name) {
var error = "";
  if (strng == undefined || strng.length == 0) {
     error = "The "+name+" has not been filled in.\r\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\r\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\r\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice, name) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option for the "+name+" from the drop-down list.\r\n";
    }    
return error;
}

function checkSpaces(strng, name) {
var error = "";
    if (strng.indexOf(' ') > -1) {
      error = name+" cannot contain spaces.\r\n";
    }
return error;
}

function checkFilename(strng, name) {
var error = "";
var objRegExp  =  /(^[a-z_0-9A-Z]+$)/;
    if (!objRegExp.test(strng)) {
      error = name+" contains invalid characters, a-z A-Z 0-9 and _ allowed.\r\n";
    }
return error;
}

function extract(what) {
    if (what.indexOf('/') > -1)
        answer = what.substring(what.lastIndexOf('/')+1,what.length);
    else
        answer = what.substring(what.lastIndexOf('\\')+1,what.length);
    return answer;
}
