function isEmailAddr(email) {
  var result = false;
  var theStr = email.value;
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField) {
    var result = true;
	
    try {
        formField.value = stripInitialWhitespace(formField.value);
     } catch(e) {
        //this doesn't work on the file object
     }

    if (formField.value == "") {
        result = false;
    }	
    return result;
}

function allDigits(str) {
	return inValidCharSet(str,"0123456789");
}

function isDigit (c) {   
    return ((c >= "0") && (c <= "9"))
}

function allLetter(str)	//check char is letter
{   return inValidCharSet(str,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
}

function allLetterDigit(str)	//check char is letter
{   return inValidCharSet(str,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}

function stripInitialWhitespace (s)

{   var i = 0;
	var whitespace = " \t\n\r";

    //while ((i < s.length) && charInString (s.charAt(i), whitespace))
		while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
       i++;
    
    return s.substring (i, s.length);
}

function isAlphanumeric(str) {   
    if (!validRequired(str)) return false;	
    if (!allLetterDigit(str)) return false; 
    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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isValidStr(str)
{   
    // Not Empty
	if (!validRequired(str)) return false;
	
    return true;
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = true;
			break;
		}
	
	return result;
}

function validEmail(formField)
{
	var result = true;
	
	if (!validRequired(formField))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField)) )
	{
		//alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		//formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField)
{
	var result = true;

	if (!validRequired(formField))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField)
{
	var result = true;

	if (!validRequired(formField))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
		if (!(elems.length == 3)) {
 			result = false; // should be three components
		} 
 		
 		if (result)
 		{
 			var day = parseInt(elems[0],10);
  			var month = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[1]) && (month > 0) && (month < 13) &&
					 allDigits(elems[0]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
			result = false;
		}
	} 
	
	return result;
}


	function randomImage() {
		// JavaScript to interpolate random images into a page.
		var ic = 5;     // Number of alternative images
		var xoxo = new Array(ic);  // Array to hold filenames
			
		xoxo[0] = "<a href='dyn/Lifetime.jsp' style='text-decoration: none'><IMG SRC='/images/studio2.jpg' width='381' height='187' border='0'></a>";
		xoxo[1] = "<a href='dyn/Lifetime.jsp' style='text-decoration: none'><IMG SRC='/images/studio3.jpg' width='381' height='187' border='0'></a>";
		xoxo[2] = "<a href='dyn/Lifetime.jsp' style='text-decoration: none'><IMG SRC='/images/studio4.jpg' width='381' height='187' border='0'></a>";
		xoxo[3] = "<a href='dyn/Lifetime.jsp' style='text-decoration: none'><IMG SRC='/images/studio5.jpg' width='381' height='187' border='0'></a>";
		xoxo[4] = "<a href='dyn/Lifetime.jsp' style='text-decoration: none'><IMG SRC='/images/studio6.jpg' width='381' height='187' border='0'></a>";
				
		// Write out an IMG tag, using a randomly-chosen image name.
		var choice = pickRandom(ic);
		return xoxo[choice];
	}
	
	function pickRandom(range) {
		if (Math.random)
			return Math.round(Math.random() * (range-1));
		else {
			var now = new Date();
			return (now.getTime() / 1000) % range;
		}
	}


	function opensmlpopup(pagePopup){
		winpops=window.open(pagePopup,"popwin","width=400,height=200,")
	}
	
	function openminipopup(pagePopup){
		winpops=window.open(pagePopup,"popwin","width=100,height=50,")
	}

	function openhelppopup(pagePopup){
		winpops=window.open(pagePopup,"popwin","width=400,height=500,scrollbars=yes")
	}

	function opentcpopup(pagePopup){
		winpops=window.open(pagePopup,"popwin","width=700,height=500,,scrollbars=yes")
	}

//
// Set the image for rollovers
//
function setImage(element, image) {
    element.src = image;
}
