

//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 Validate() {

report.reporttitle.style.background = '#ffffff';
report.reportdescr.style.background = '#ffffff';

var Message = "";
 if ((stipAll(report.reporttitle.value) == "") || (containsSomeSpecial(report.reporttitle.value))) {
  report.reporttitle.style.background = '#ffffcc';
  Message = "Alert Title is Blank or Contains Invalid Characters\n";
 };
 if ((report.reporttitle.value.length) > 100) {
  report.reporttitle.style.background = '#ffffcc';
  Message = "Alert Title Should Not Exceed 100 Characters\n";
 };
 
 if ((stipAll(report.reportdescr.value) == "") || (containsSomeSpecial(report.reportdescr.value))) {
  report.reportdescr.style.background = '#ffffcc';
  Message = Message + "Alert Description is Blank or Contains Invalid Characters\n";
 };
 if ((report.reportdescr.value.length) > 2300) {
  report.reportdescr.style.background = '#ffffcc';
  Message = "Alert Description Should Not Exceed 2300 Characters\n";
 };

 if (Message == ""){
  report.submit();
 } else { alert("Please make the following correction(s):\n\n" + Message);
 };
}

function ValidateModify() {

modifyrecord.reporttitle.style.background = '#ffffff';
modifyrecord.reportdescr.style.background = '#ffffff';

var Message = "";
 
 if ((stipAll(modifyrecord.reporttitle.value) == "") || (containsSomeSpecial(modifyrecord.reporttitle.value))) {
  modifyrecord.reporttitle.style.background = '#ffffcc';
  Message = Message + "Alert Title is Blank or Contains Invalid Characters\n";
 };
  if ((modifyrecord.reporttitle.value.length) > 100) {
  modifyrecord.reporttitle.style.background = '#ffffcc';
  Message = "Alert Title Should Not Exceed 100 Characters\n";
 };

 if ((stipAll(modifyrecord.reportdescr.value) == "") || (containsSomeSpecial(modifyrecord.reportdescr.value))) {
  modifyrecord.reportdescr.style.background = '#ffffcc';
  Message = Message + "Alert Description is Blank or Contains Invalid Characters\n";
 };
 if ((modifyrecord.reportdescr.value.length) > 2300) {
  modifyrecord.reportdescr.style.background = '#ffffcc';
  Message = "Alert Description Should Not Exceed 2300 Characters\n";
 };

 if (Message == ""){
  modifyrecord.submit();
 } else { alert("Please make the following correction(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 (((login.UserName.value.length) > 15) || ((login.UserName.value.length) < 5)){
  login.UserName.style.background = '#ffffcc';
  Message = "Username Should be Between 5 and 15 Characters\n";
 };
 if ((stipAll(login.Password.value) == "") || (containsSpecial(login.Password.value))){
  login.Password.style.background = '#ffffcc';
  Message = Message + "Invalid Password\n";
 };
 if (((login.Password.value.length) > 15) || ((login.Password.value.length) < 5)){
  login.Password.style.background = '#ffffcc';
  Message = "Password Should be Between 5 and 15 Characters\n";
 };
 
 if (Message == ""){
  login.submit();
 } else { alert("Please make the following correction(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 (((login.username.value.length) > 15) || ((login.username.value.length) < 5)){
  login.username.style.background = '#ffffcc';
  Message = "Username Should be Between 5 and 15 Characters\n";
 };
 if ((stipAll(login.password.value) == "") || (containsSpecial(login.password.value))){
  login.password.style.background = '#ffffcc';
  Message = Message + "Invalid Password\n";
 };
 if (((login.password.value.length) > 15) || ((login.password.value.length) < 5)){
  login.password.style.background = '#ffffcc';
  Message = "Password Should be Between 5 and 15 Characters\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("Please make the following correction(s):\n\n" + Message);
 };
}
