<!--

// -----------------------------------------------------------------
// Function    : IsEmailValid
// Language    : JavaScript
// Description : Checks if given email address is of valid syntax
// Copyright   : (c) 1998 Shawn Dorman
// http://www.goodnet.com/~sdorman/web/IsEmailValid.html
// ----------------------------------------------------------------
// Ver    Date    Description of modification
// --- ---------- --------------------------------------------------
// 1.0 09/04/1996 Original write
// 1.1 09/30/1998 CHG: Use standard header format
// -----------------------------------------------------------------
// Source: Webmonkey Code Library
// (http://www.hotwired.com/webmonkey/javascript/code_library/)
// -----------------------------------------------------------------

function IsEmailValidString( Temp )
{
    Temp = trim(Temp);

    var EmailOk  = true;
    var AtSym    = Temp.indexOf('@');
    var Period   = Temp.lastIndexOf('.');
    var Space    = Temp.indexOf(' ');
    var Length   = Temp.length - 1;   // Array is from 0 to length-1

    // test for special chars, 2001/08/03 - lp
    var iChars = "*|\":<>[]{}`\;()$#%";
    for (var i = 0; i < Temp.length; i++) {
       if (iChars.indexOf(Temp.charAt(i)) != -1)  EmailOk =  false;
    }

    if ((AtSym < 1) ||                 // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space != -1))                     // No empty spaces permitted
    {
        EmailOk = false;
    }

    return EmailOk;
}

function IsEmailValid(FormName,ElemName)
{
	var foo = document.forms[FormName].elements[ElemName].value;
	return IsEmailValidString( foo );
}


// Checks Multiple email addresses (Separated by Commas or Semi-Colons)
// NOTE! You must run the semi-colon replace on the template as well! (see below)
// SemiAccept, 1 = Yes Accept Semi-Colons
function IsEmailValidMultiple(FormName,ElemName,SemiAccept)
{
	var Test = document.forms[FormName].elements[ElemName].value;

	if( Test.length < 1)
	{
		return false;
	}

	if (Test.indexOf(';') != -1 && SemiAccept == 1) Test = replace(Test,';',','); // replaces semi-colons with commas
	var junk = Test;
        var comma = junk.indexOf(',');

	while ( comma != -1 ) //used to have "junk != '' && junk.length > 0"
	{
      		comma = junk.indexOf(',');
      		if( comma == -1 ) { break; } 
		var junkCheck = junk.substring(0,comma);
		junkCheck = trim(junkCheck);
		if( IsEmailValidString( junkCheck ) )
		{
			junk = junk.substring(comma+1);
		}	
		else
		{
			return false;
		}
	}

	if ( junk == '' )
	{
		return true;
	}
	if (IsEmailValidString(junk))
	{
		return true;
	}
	else
	{
		return false;
	}
	document.forms[FormName].elements[ElemName].value = Test;
}


function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


// Function to trim whitespace 2001/07/26 - lp

function trim(txt) {
    while (txt.substring(0,1) == ' ') {
	txt = txt.substring(1, txt.length);
	}
    while (txt.substring(txt.length-1,txt.length) == ' ') {
	txt = txt.substring(0, txt.length-1);
	}
    return txt;
} 


// "" is NOT a valid URL
// (in other words, only call this function if you've already tested for blanks, or if you want this field to be required)
function isValidURL(url_field) {
    var urlStr = url_field.value;

    if (urlStr.length < 7 || (urlStr.toLowerCase().substring(0,7) != "http://" && urlStr.toLowerCase().substring(0,8) != "https://"))
    {
	return false;
    }
    else if (urlStr.toLowerCase() == "http://" || urlStr.toLowerCase() == "https://")
    {
	url_field.value = "";
	return true;
    }

   return true;
}

function isChecked( radio_element )
{
	for (var ii=0; ii < radio_element.length; ii++)
	{
		if (radio_element[ii].checked == true)
			return true;
	}
	
	return false;
}

function selectionChosen( select_element )
{
  return select_element.options[select_element.selectedIndex].value != 0;
}

function selectionChosenMulti( multi_select_element )
{
  var selected = getSelected( multi_select_element.options );
  if (selected.length == 1)  {
    // user might have chosen just the "select one" option; that's not a real choice
    if (selected[0].value == 0)
      return false;
    else
      return true;
  } else if (selected.length > 1) {
    return true;
  } else {
    return false;
  }
}

function getSelected( opt )
{
  var selected = new Array();
  var index = 0;

  for (var ii=0; ii < opt.length; ii++) {
    if (opt[ii].selected) {
      index = selected.length;
      selected[index] = new Object;
      selected[index].value = opt[ii].value;
      selected[index].index = ii;
    }
  }

  return selected;
}

// unselects the "select one" option at the top of a multi-select.  call this prior to
// submitting a form with a multi-select
function fixMultiSelect( multi_select )
{
  var opt = multi_select.options;
  for (var ii=0; ii < opt.length; ii++) {
    if (opt[ii].selected && opt[ii].value == 0) {
      opt[ii].selected = false;
    }
  }
}

// Date Validator 2001/08/06 - lp
// Usage: add onBlur="isValidDate(this)"; at the end of the date input field

function isValidDate(date_field) {
    var dateStr = date_field.value;

    if (dateStr.length < 10 && dateStr.length != "") {
            alert("Please provide date in appropriate format (mm/dd/yyyy)");
	    date_field.focus();
	    return false;
    } 

    if (dateStr.length != "") {
        var strErr=0;
        var datename = new Date();
        var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{2}|\d{4})$/;
        var matchArray = dateStr.match(datePat);
        if (matchArray == null) {
//            alert("Date is not in a valid format.")
            strErr=1;
            date_field.focus();
            return false;
        }
        month = matchArray[1];
        day = matchArray[3];
        year = matchArray[4];
        if (month < 1 || month > 12) {
            alert("Month must be between 1 and 12.");
            strErr=1;
            date_field.focus();
            return false;
        }
        if (day < 1 || day > 31) {
            alert("Day must be between 1 and 31.");
            strErr=1;
            date_field.focus();
            return false;
        }
        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
            alert("Month "+month+" doesn't have 31 days!")
            strErr=1;
            date_field.focus();
            return false;
        }
        if (month == 2) {
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day==29 && !isleap)) {
                alert("February " + year + " doesn't have " + day + " days!");
                strErr=1;
                date_field.focus();
                return false;
            }
        }
    }
    return true;
}



//Launches a window with News URL//
function displayNews(url) {
	var Win = window.open(url,'Special','width=700,height=500,resizable=1,scrollbars=yes,menubar=yes,toolbar=yes,locations=no' );
}

//Opens either News Link or News Text//
//openNews( News.news_id, News.url, Look&Feel, ##addcmds_link##)//

function openNews(newsid, url, lf, addcmds) {

   if ( url != '' ) {
      displayNews( url );
   } else { 
      window.location = '/jsp/Front/News.jsp?lf=' + lf + '&' + addcmds + '&cmd=newsdetails&News.id=' + newsid;
   }

}

////////////////////////////////////////////////////////////////////////////////
//    function: addYearsToDropDown
//      params: the_select_element - select form element for adding options
//              startYear - four-digit year to start with
// description: for adding option elements to a drop down, starting at 
//              startYear and continuing to the current year.
//
//   usage: addYearsToDropDown(document.forms[0].elements['selectname'], 1998);

function addYearsToDropDownAdvanced(the_select_element, startYear, isAscending)
{
  var now = new Date();
  var nowYear = now.getFullYear();
  var idx = the_select_element.options.length;

  if (isAscending) {
  	for(i=startYear; i<= nowYear; i++)
  	{ 
    	the_select_element.options[idx] = new Option(i, i);
    	idx++;
  	}
  } else {
  		// desc
	  	for(i=nowYear; i>= startYear; i--)
  	{ 
    	the_select_element.options[idx] = new Option(i, i);
    	idx++;
  	}	
		
  }
}

function addYearsToDropDown(the_select_element, startYear)
{
	addYearsToDropDownAdvanced(the_select_element, startYear, true);
}


// filterByFirstChar filters a pulldown by removing any values that begin with start_string
function filterByFirstChar( start_string, obj ) {
    with ( obj ) {
      var x = 0;
      while ( x < options.length ) {
        var c = options[x].text;
        if ( c.substring( 0, start_string.length ) == start_string ) {
          options[x] = null;  // this moves all higher ones down
        } else {
          x++;
        }
      }
    }
}

// Show and Hide the given element
function showElement(id) {document.getElementById(id).style.display = "block";}
function hideElement(id) {document.getElementById(id).style.display = "none";}

// Toggle an element's visibility
// Must set the original display to block or none Ex. <div id="blurb" style="display:block;">
function displayToggle(id){
	var the = document.getElementById(id).style;
	if (the.display == "none") { the.display = "block"; }
	else if (the.display == "block") { the.display = "none";}
}

function checkAll(field)
{
    for (i = 0; i < field.length; i++)
        field[i].checked = true ;
}   

function uncheckAll(field)
{
    for (i = 0; i < field.length; i++)
        field[i].checked = false ;
}
//-->

