// Copyright © 2003 by Viggie, All Rights Reserved.
//
// This validatation file is built on Apple Sample Code provided by 
// Apple Inc. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. This code was
// modified a lot by Viggie. The original had lots of bugs and too generic.


// email

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "Please enter an email address.\n";
   return error;
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 8-13 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "Please enter a phone number.\n";
   return error;
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if ((stripped.length < 8) || (stripped.length > 13)) {
	error = "The phone number is in wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// Mobile number - strip out delimiters and check for 8-13 digits (same as Phone above)

function checkMobile (strng) {
var error = "";
if (strng == "") {
   error = "Please enter a mobile number.\n";
   return error;
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The mobile number contains illegal characters.";
  
    }
    if ((stripped.length < 8) || (stripped.length > 13)) {
	error = "The mobile number is in wrong length. Make sure you entered it correctly.\n";
    } 
return error;
}


// password - between 4-20 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 4) || (strng.length > 20)) {
       error = "The password should have minimum 4 characters.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-15 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 15)) {
       error = "The username should be minimum 4 and maximum 15 characters.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng, name) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your "+name+".\n"
  } else if (strng.length < 3) {
     error = "Please enter a proper "+name+", for clarity.\n"
  }

return error;
}

// non-empty textbox

function isTxtOnly(strng, name) {
var error = "";
var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
var illegalChars = /[\W_9876543210]/; // allow only letters

  if (strng.length == 0) {
     error = "Please enter your "+name+".\n"
  } else if (strng.length < 3) {
     error = "Please enter a proper "+name+", for clarity.\n"
  } else if (illegalChars.test(stripped)) {
      error = "The "+name+" contains illegal characters.\n";
  }


return error;
}


// non-empty textarea box

function isTareaEmpty(strng, name) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your "+name+".\n"
  } else if (strng.length < 10) {
     error = "Please enter a proper "+name+", for clarity.\n"
  }

return error;
}


// was textbox altered

function isDifferent(strng) {
var error = "";
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue, name) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button in "+name+".\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list.\n";
    }    
return error;
}    

// Checkbox ticked for terms agreement

function checkBox(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please select the I agree check box!!\n";
    }
return error;
}

