// A series of validation functions for Javascript

// a function to trim whitespace from the start and end of a string:
function trim(s) 
{
	return s.replace(/^\s+|\s+$/g,"");
}
// the above regular expression reads like this:
//   /........./  match the stuff between these slashes
//   ^  starts with 
//    \s   a whitespace character
//   +   or more than one
//  |  or
//  \s  a whitespace character
//  +  or more than one
//  $ is at the very end
//  g  global... replace them ALL



// isEmpty returns true if the string is empty 
//  or if it only has whitespace characters
function isEmpty(s) 
{	
	s = trim(s);  // get rid of whitespace characters
	return (s.length == 0);   // return the result of this condition
}


// Numeric validator makes sure the string sent is all numerals.
// Whitespace characters are ignored.
// If the variable sent to this function is empty, or if it
// contains any non-numeric (0-9) character, false is returned.
// NOTE: This function will return false if there are commas,
// decimal points, or minus signs (,.-)!!!
function isNumeric2(s)
{
   var c, i;
   s = trim(s); // ignore whitespace
   
   // if blank, it's not a number
   if (s.length == 0) return false;

   // check each character
   for (i=0; i<s.length; i++)
   {
      c = s.charAt(i); // One Character at a time
      if (c < "0" || c > "9") return false;
   }
   
   // it IS all numerals
   return true;
}


// regular expression version of the above...
function isNumeric(s)
{
	s = trim(s);
	// if length is zero or if it contains a non-numeral, it's not all numeric:
	return (! (s.length == 0 || /^0-9/.test(s)));
}

// trims whitespace from the start and end, then checks if the remainder has at least one
// character and all characters are alphanumeric (0-9, A-Z, _)  
function isAlphanumeric(s)
{	
	s = trim(s);
	// return false if length is zero or if it contains a non-alphanumeric character (0-9, A-Z, _)
	return (! (s.length == 0 || /\W\s/.test(s)));
}


// isInteger returns true if the value can be parsed as an integer and is only an integer.
// White space at the beginning and end is ignored, but any non-numeric character will cause
// this function to return false, even if "parseInt" would work. 
// For example, these will return false: "12abc" and "12.34" , even though parseInt would return
// the value of 12 for each.
function isInteger(s)
{
	s = trim(s);
	var v = parseInt(s);
	if (isNaN(v)) return false;
	if (v != s) return false;
	return true;
}


// isFloat returns true if the value can be parsed as a float and there are no extraneous 
// characters besides whitespace at the start or end. 
function isFloat(s)
{
	s = trim(s);
	var v = parseFloat(s);
	if (isNaN(v)) return false;
	if (v != s) return false;
	return true;
}


// Return true if Radio set contains checked item, false if nothing checked
// Parameter should be sent as: document.formname.radioname
function isRadioNameChecked(radioSet)
{
	for (var i=0; i < radioSet.length; i++)
		if (radioSet[i].checked) return true;
	return false;
}


// Return true if Radio set contains checked item, false if nothing checked
// Radio button Ids should be numbered starting at 1, as in radio1, radio2, radio3, radio4
// The first parameter should be the Id minus the number, as in "radio" above.
// The second parameter should be the number of radio buttons in the set, as in 4 above.
function isRadioIdChecked(radioId, count)
{
	for (var i=1; i <= count; i++)
		if (document.getElementById(radioId + i).checked) return true;
	return false;
}


// Is an item from a selection list chosen, when first item in list is blank (or an instruction)
// Note: this one is easy to write as an instruction instead of a function call!
// The parameter is the id of the selection list.
function isSelectionChosen(selectionId)
{
	return ( document.getElementById(selectionId).selectedIndex != 0 )
}


// Is a checkbox checked?
// Note: this one is pretty lame as a function. It's an easy "if" statement by itself.
// The parameter is the id of the checkbox.
function isCheckboxChecked(checkboxId)
{
	return ( document.getElementById(checkboxId).checked  );
}


// This function returns true if the email address appears okay,
// and false if email address is invalid.
function validEmail(email)
{
	email = trim(email);  // Added after writing JAR
	// blank is bad
	if (email == "") return false;

	// need one @, and it can't be in first spot (0)
	var a = email.indexOf("@");
	if (a < 1) return false;

	// can't have 2 @'s
	if (email.indexOf("@",a+1) != -1) return false;

	// need a period, and at least 2 characters after
	var p = email.indexOf(".", a+2);
	if (p == -1 || p > email.length-3) return false;

	// these characters can't be anywhere in an address
	var bad = " /,;:!";
	for (a = 0; a < bad.length; a++)  // re-use "a" as counter
	{
	  p = bad.charAt(a); // re-use "p" for one bad character
	  if (email.indexOf(p) > -1) return false;
	}

	// if we get here, it appears okay... 
	return true;
}

function isYouTube(s)
{
	s = trim(s);
	
	var YouTubeExp = /http:\/\/www\.youtube\.com\/watch\?v=/;
	var resultYT = s.match(YouTubeExp);
	
//	alert("resultYT: " + resultYT);
	if (!resultYT)
		return false;

	 // if we get here, it appears okay... 
	return true;	
}


function isTeacherTube(s)
{
	s = trim(s);
	
	var ttLen = s.length;
	var TeacherTubeExp = /viewVideo\.php\?video_id/;
	var resultTT = s.match(TeacherTubeExp);

//	alert("TT URL: " + s);
//	alert("TT Length: " + ttLen);
//	alert("resultTT: " + resultTT);

//	if (ttLen < 700) return false;
	
	if (!resultTT) return false;

	 // if we get here, it appears okay... 
	return true;	
}
// SEE: regular expression samples for drivers license and phone number validation expressions

// TIP:  google this: javascript credit card validator

