
/**
 * Author: Nathan Stanford
 * 
 * Description:
 * This is a Javascript Library for Form Validation.  
 * If there are exceptions that will need to be used 
 * then they need to be added here so they will be consistent
 * accross the application.
 * 
 */

// email
function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "\n\t- You didn't enter an email address.";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
       error = "\n\t- Please enter a valid email address.";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "\n\t- The email address contains illegal characters.";
       }
    }
return error;
}


// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "\n\t- You didn't enter a phone number.";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "\n\t- The phone number contains illegal characters.";

    }
    if (!(stripped.length == 10)) {
	error = "\n\t- The phone number is the wrong length. Make sure you included an area code.";
    }
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral
function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "\n\t- You didn't enter a password.";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers

    if ((strng.length < 6) || (strng.length > 8)) {
       error = "\n\t- The password is the wrong length. (6-8 Characters)";
    }
    else if (illegalChars.test(strng)) {
      error = "\n\t- The password contains illegal characters.";
    }
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "\n\t- The password must contain at least one uppercase letter, one lowercase letter, and one numeral.";
    }
return error;
}
 
// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "\n\t- You didn't enter a username.";
}
    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "\n\t- The username is the wrong length.";
    }
    else if (illegalChars.test(strng)) {
    error = "\n\t- The username contains illegal characters.";
    }
return error;
}

// non-empty textbox
function isEmpty(strng,fieldName) {
var error = "";
  if (strng.length == 0) {
     error = "\n\t- The " + fieldName + " has not been filled in.";
  }
return error;
}

// non-empty textbox
function isEmptyTwo(strng1,fieldName1,strng2,fieldName2) {
var error = "";
  if ((strng1 == "") && (strng2 == ""))  {
     error = "\n\t- You must fill in " + fieldName1 + " or " + fieldName2 + ".";
  }
return error;
}

// was textbox altered
function isDifferent(strng) {
var error = "";
  if (strng != "Can\'t touch this!") {
     error = "\n\t- You altered the inviolate text area.";
  }
return error;
}


// exactly one radio button is chosen
function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "\n\t- Please check a radio button.";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "\n\t- You didn't choose an option from the drop-down list.";
    }
return error;
}

// two fields compare same
function isSame(strng1,fieldName1,strng2,fieldName2) {
var error = "";
  if (strng1 != strng2) {
     error = "\n\t- The "+fieldName1+" and "+fieldName2+" are not the same.";
  }
return error;
}




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;
}


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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}


// isDate(formFieldValue,formFieldName,minYear,maxYear)
function isDate(dtStr,fieldName,minYear,MaxYear){
	// Declaring valid date character
	var dtCh= "/";
	var minYear=minYear;
	var maxYear=MaxYear;
	var error=""
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		error += "\n\t- The "+fieldName+" has not been filled in correctly.";
	}
	
	if (strMonth.length<1 || month<1 || month>12){
		error +="\n\t- Enter a valid month.";
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error += "\n\t- Enter a valid day.";
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		error += "\n\t- Enter a valid 4 digit year between "+minYear+" and "+maxYear+".";
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		error += "\n\t- Enter a valid date.";
}
if (error != '') {
		return error;
	}
	else
	{
		return true;
	}

}


