//
//
//	Name         : /lib/js/dataValidation.js
//	Author       : Danny Shaw
//	Created      : 03/13/2007
//	Last Updated : 
//
// this file contains a collection of general purpose javascript functions to be used 
// in various types of data validation. 


// this function will examine the received string parameter for dangerous characters and 
// if found remove them. the intent is to help prevent cross-site scripting. 
function stripBadChars(testStr)
{
	var tempStr = testStr.replace(/<|>|%|\|\|\=|&|\+/g,'');
	return tempStr;
}


// this function will test the received string parameter to see if it is valid email address format.
// this function "Does Not" actually validate the string as an actual email address. 
function validateEmailFormat(testStr) 
{
	var at   = "@";
	var dot  = ".";
	var lat  = testStr.indexOf(at);
	var lstr = testStr.length;
	var ldot = testStr.indexOf(dot);

	// make sure that "@" is present, but not first or last character.
	if (testStr.indexOf(at)==-1 || testStr.indexOf(at)==0 || testStr.indexOf(at)==lstr)
	{
	   return false;
	}

	// make sure that "." is present, but not first or last character. 
	if (testStr.indexOf(dot)==-1 || testStr.indexOf(dot)==0 || testStr.indexOf(dot)==lstr)
	{
	    return false;
	}

	// make sure that there are not multiple "@" characters. 
	if (testStr.indexOf(at,(lat+1))!=-1)
	{
	    return false;
	}

	// make sure that the string does not contain ".@" or "@.".
	if (testStr.substring(lat-1,lat)==dot || testStr.substring(lat+1,lat+2)==dot)
	{
	   return false;
	}

	// make sure that there is more that one character between "@" and ".".
	if (testStr.indexOf(dot,(lat+2))==-1)
	{
	   return false;
	}
		
	// make sure that there are no imbedded spaces. 
	if (testStr.indexOf(" ")!=-1)
	{
		return false;
	}

 	return true;
}

// this function will return true if the receieved parameters constitute a valid date. 
// expected format is "mm/dd/yyyy".
function validDate(dateStr)
{
	var yr;
	var mt; 
	var dy;
	var maxdays;
	var leapYr;
	
	// make sure that the date string is the correct length.
	if (dateStr.length != 10) return false;
	
	// parse apart the date components.
	var monthVal = dateStr.substr(0,2);
	var dayVal   = dateStr.substr(3,2);
	var yearVal  = dateStr.substr(6,4);
	var slash01  = dateStr.substr(2,1);
	var slash02  = dateStr.substr(5,1);
	
	// make sure that all the digits are digits. 
	if (isNaN(parseInt(monthVal.substr(0,1), 10)) || isNaN(parseInt(monthVal.substr(1,1), 10)) ||
		isNaN(parseInt(dayVal.substr(0,1), 10))   || isNaN(parseInt(dayVal.substr(1,1)), 10)   ||
		isNaN(parseInt(yearVal.substr(0,1), 10))  || isNaN(parseInt(yearVal.substr(1,1), 10))  || 
		isNaN(parseInt(yearVal.substr(2,1), 10))  || isNaN(parseInt(yearVal.substr(3,1), 10)) ) return false;
	
	// make sure that the appropriate seperators are used. 
	if (slash01 != "/" || slash02 != "/") return false;
			
	yr = parseInt(yearVal, 10);
	mt = parseInt(monthVal, 10);
	dy = parseInt(dayVal, 10);
			
	// make sure that the month, day and year values are valid (or at least reasonable).
	if (yr < 1900) return false;
			
	if (mt > 12 || mt < 1) return false;
			
	maxdays = 30;
	if ((mt == 1) || (mt == 3) || (mt == 5) || (mt == 7) || 
		(mt == 8) || (mt == 10) || (mt == 12)) 
	{
		maxdays = 31;
	}
			   
	if (mt == "2") 
	{
		maxdays = 28;
	}
	leapYr = yr % 4;
	if ((leapYr == 0) && (mt == "2")) 
	{
		maxdays = 29;
	}
			
	if (dy > maxdays) return false;
	
	return true;
}

