

//Global Validations

//This is the function for verifiying a phone number
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function stipAll(s) {
	//stips all extra white space and renames characters
	var thisS = "";
	thisS = s;
	thisS = thisS.replace( /^\s+/g, "" );
	thisS = thisS.replace( /\s+$/g, "" );
	s = thisS.replace(/<!/g, "");
	thisS = s;
	s = thisS.replace(/"/g, '\'');
	return s;
} //end of stipAll

function containsSomeSpecial(s) {
	//checks the input string for special characters
	//var iChars = "$%^*+=[]\\\'{}|\"?";
        if (s.indexOf("<script") != -1) {
			return true;
		} //end of if
		if (s.indexOf("%3C%73%63%72%69%70%74%3E") != -1) {
			return true;
		} //end of if
		//for (var i = 0; i < s.length; i++) {
        //	if (iChars.indexOf(s.charAt(i)) != -1) {
          //  	return true;
		//	} end if
		//} end for

  //sqlInjection sequence formula as described above
   var E_sequence=/((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;))/i;

  //check the pattern of the current with the the sequence formula
  if (E_sequence.test(s)) {
   return true;
  } //end test pattern
 
} //end containsSpecial

function containsSpecial(s) {
	//checks the input string for special characters
	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
        if (s.indexOf("<script") != -1) {
			return true;
		} //end of if
		if (s.indexOf("%3C%73%63%72%69%70%74%3E") != -1) {
			return true;
		} //end of if
		for (var i = 0; i < s.length; i++) {
        	if (iChars.indexOf(s.charAt(i)) != -1) {
            	return true;
			} //end if
		} //end for
} //end containsSpecial

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
} //end isInteger

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
} //end stripCharsInBag

//Page Specific Validation
function ValidateSearch() {

search_text.style.background = '#ffffff';

var Message = "";
 if (containsSpecial(search_text.value)){
  search_text.style.background = '#ffffcc';
  Message = Message + "Invalid Search Name\n";
 };
 
 if (Message == ""){
  this.location = "view_list.asp?search=" + search_text.value;
 } else { alert("We did not get all the information we need from you.\n\nPlease fill in the following field(s):\n\n" + Message);
 };
}

function ValidateLogin() {

login.UserName.style.background = '#ffffff';
login.Password.style.background = '#ffffff';

var Message = "";
 if ((stipAll(login.UserName.value) == "") || (containsSpecial(login.UserName.value))){
  login.UserName.style.background = '#ffffcc';
  Message = Message + "Invalid User Name\n";
 };
 if ((stipAll(login.Password.value) == "") || (containsSpecial(login.Password.value))){
  login.Password.style.background = '#ffffcc';
  Message = Message + "Invalid Password\n";
 };
 
 if (Message == ""){
  login.submit();
 } else { alert("We did not get all the information we need from you.\n\nPlease fill in the following field(s):\n\n" + Message);
 };
}

function ValidateUser() {

login.username.style.background = '#ffffff';
login.password.style.background = '#ffffff';
login.firstname.style.background = '#ffffff';
login.lastname.style.background = '#ffffff';

var Message = "";
 if ((stipAll(login.username.value) == "") || (containsSpecial(login.username.value))){
  login.username.style.background = '#ffffcc';
  Message = Message + "Invalid User Name\n";
 };
 if ((stipAll(login.password.value) == "") || (containsSpecial(login.password.value))){
  login.password.style.background = '#ffffcc';
  Message = Message + "Invalid Password\n";
 };
 if ((stipAll(login.firstname.value) == "") || (containsSpecial(login.firstname.value))){
  login.firstname.style.background = '#ffffcc';
  Message = Message + "Invalid First Name\n";
 };
 if ((stipAll(login.lastname.value) == "") || (containsSpecial(login.lastname.value))){
  login.lastname.style.background = '#ffffcc';
  Message = Message + "Invalid Last Name\n";
 };
 
 if (Message == ""){
  login.submit();
 } else { alert("We did not get all the information we need from you.\n\nPlease fill in the following field(s):\n\n" + Message);
 };
}
