/** 
 * Retrieve a reference to an html element by its id 
 * Returns null in case browser implementation not supported. 
 */ 
function $(id){ 
 if (document.getElementById!=null) 
  return document.getElementById(id); 
 if (document.all!=null) 
  return document.all[id]; 
 if (document.layers!=null) 
  return document.layers[id]; 
 return null; 
} 
/** 
* in: a html element 
* out: all decendant html elements, not nodes. 
*/ 
function getAllElems(root){ 
 var res=[]; 
 if (root){ 
  var chds=root.childNodes; 
  for(var i=0; i<chds.length; i++){ 
   if (chds[i].tagName) { 
    res[res.length]=chds[i]; 
    var tmp = getAllElems(chds[i]); 
    for(var j=0; j<tmp.length; j++) res[res.length]=tmp[j]; 
   } 
  } 
 } 
 return res; 
} 
/** 
* Sends a post or get based on some marked form elements 
* 
* in: 
* -identifier of html element containing the input fields 
* -action, the form action to be used 
* -method, the method to be used 
*/ 
function submitForm(fieldElemId,action,method){ 
var frm=document.forms["Form1"]; 
 var formDiv=$(fieldElemId); 
 var elems=getAllElems(formDiv); 
 if (method=="get"){ 
  var q=""; 
  for (i=0; i<elems.length; i++) { 
   var chd=elems[i]; 
   var name=chd.tagName; 
   if (name){ 
    name=name.toLowerCase(); 
    if (name == "input") { 
     if (chd.type == "text" || chd.type == "hidden" || chd.type == "password") { 
      q += chd.name + "=" + chd.value + "&"; 
     } 
     if (chd.type == "checkbox") { 
      if (chd.checked) { 
       q += chd.name + "=" + chd.value + "&"; 
      } else { 
       q += chd.name + "=&"; 
      } 
     } 
     if (chd.type == "radio") { 
      if (chd.checked) { 
       q += chd.name + "=" + chd.value + "&"; 
      } 
     } 
    } 
    if (name == "select") { 
     var sel = chd; 
      q += sel.name + "=" + sel.options[sel.selectedIndex].value + "&"; 
    }
	if ( name=="textarea" ){
		q+=chd.name+"="+chd.value+"&";
	}
   } 
  } 
  if (q.length>2) q=q.substring(0,q.length-1); 
  location.href=action+"?"+q; 
 } else 
 if (method=="post"){ 
  var els=frm.elements; 
  for (var i=0; i<els.length; i++){ 
   var j=elems.length-1; 
   while (j>=0 && elems[j].name!=els[i].name) j--; 
   if (j<0) els[i].value=""; 
  } 
  if (action) frm.action=action; 
  if (method) frm.method=method; 
  frm.submit(); 
 } 
} 

function filterEmailField( fieldToFilter, min, max )
{
	filterFieldValue( fieldToFilter );

	var email = fieldToFilter.value;
	var invalid = " \:\,\;\#$\%\&\(\)\+\=\/";

	if (email == '')
	{
		return true;
	}

	for(var i=0; i<invalid.length; i++)
	{
		var badChar = invalid.charAt(i);

		if (email.indexOf(badChar,0) != -1)
		{
			alert( 'The e-mail address you entered \ncontains one or more invalid characters.' );
			return false;
		}
	}

	if ( fieldToFilter.value.charAt(fieldToFilter.value.length -1) == "." )
	{
		alert( 'The e-mail address you entered \ncontains one or more invalid characters.' );
		return false;
	}

	if (-1 != 	email.indexOf(".@"))
	{
		alert( 'The e-mail address you entered \ncontains one or more invalid characters.' );
		return false;
	}

	var atSignPos = email.indexOf('@',1);

	if (atSignPos == -1)
	{
		alert( 'The e-mail address you entered \nis missing its @ sign.' );
		return false;
	}
	else if (email.indexOf('@',atSignPos+1) != -1)
	{
		alert( 'The e-mail address you entered \ncontains too many @ signs.' );
		return false;
	}

	var dotPos = email.indexOf('.',atSignPos+2);

	if (email.indexOf('..', 1) != -1)
	{
		alert( 'The e-mail address you entered \ncontains one or more invalid characters.' );
		return false;
	}

	if (dotPos == -1)
	{
		alert( 'The e-mail address you entered \nis missing its extension.' );
		return false;
	}
	else if (dotPos+3 > email.length)
	{
		alert( 'The e-mail address you entered appears\nto use an invalid extension.' );
		return false;
	}

	if ((min) && (max) && (fieldToFilter.value != ''))
	{
		if (( fieldToFilter.value.length < min ) || ( fieldToFilter.value.length > max ))
		{

			alert ('\nPlease enter a value \nthat is between ' + min + ' and ' + max + ' \ncharacters in length.\n');

			fieldToFilter.value     = "";
			filtered                = "";

			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
	}
	return true;
}   


function filterPasswordValue( fieldToFilter, min, max, fieldPW, fieldPWVer, message )
{
	
	var filtered        = '';
	var chr             = '';

	filterFieldValue( fieldToFilter, min, max );

	if ( ( fieldPW )
	  && ( fieldPWVer )
	  && ( fieldPW.value != '' )
	  && ( fieldPWVer.value != '' )
	  && ( fieldPW.value.toUpperCase() != fieldPWVer.value.toUpperCase() ))
	{

		alert( "\n" + message + "\n" );
		fieldPWVer.value = '';

		if (document.images)
		{
			fieldPWVer.focus();
		}
		return
	}
}	



function filterFieldValue(fieldToFilter, min, max, content)
{
	var filtered	= '';
	var chr			= '';
	var end			= false;
	var whiteSpace	= ' 	';
	var startPos	= 0;
	var tempString	= '';

	while ( (whiteSpace.indexOf(fieldToFilter.value.charAt(0)) >= 0)
				&& (fieldToFilter.value.length > 0) )
	{
		fieldToFilter.value = fieldToFilter.value.substring(1, fieldToFilter.value.length);
	}
	if ( fieldToFilter.value.length == 0 )
	{
		fieldToFilter.value = "";
		return false;
	}

	if ( (content) && (content == "name") )
	{
		if ( ( fieldToFilter.value.charAt(0) >= "0")
			&& ( fieldToFilter.value.charAt(0) <= "9") )
		{
				alert( 

									    '\nThis field contains invalid characters.\n\n'

									    +   'Please avoid the use of non-standard \n'

									    +   'ascii characters, such as:\n'

									    +   '     { } [ ] < > ! ^ | `\n'

									    +   '\nPlease enter corrected information.\n'

									 );
				end = true;
		}
	}

	for (var i=0; i < fieldToFilter.value.length; i++)
	{
		chr	= fieldToFilter.value.charAt(i);

		if ( (content) && (content == "int") )
		{
			if ( chr < '0' || chr > '9' )
			{
				alert("\nPlease enter a valid \nnumber in this field.\n");
				end = true;
			}
		}
		else if ( (content) && (content == "num") )
		{
			if ( (chr != ".") && ( chr < '0' || chr > '9' ) )
			{
				alert("\nPlease enter a valid \nnumber in this field.\n");
				end = true;
			}
		}
		else
		{
		var invalid	= '';
		if ( (content) && (content == "name") )
			{
				invalid	= '/&*()#$@%!{}[]^|`<>\"';
			}
			else
			{
				invalid	= '!{}[]^|`<>\"';
			}			if ( ( invalid.indexOf(chr) >= 0 )
				|| ( chr < '\x20' ) || ( chr > '\x7f') )
			{
				alert( 

									    '\nThis field contains invalid characters.\n\n'

									    +   'Please avoid the use of non-standard \n'

									    +   'ascii characters, such as:\n'

									    +   '     { } [ ] < > ! ^ | `\n'

									    +   '\nPlease enter corrected information.\n'

									 );
				end = true;
			}
		}

		if ( end )
		{
			fieldToFilter.value = "";
			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
	}

	if ( (content) && (content == "num") )
	{
		if ( fieldToFilter.value.indexOf(".") == -1 )
		{
			fieldToFilter.value = fieldToFilter.value + ".00";
		}
		else
		{
			dec = "00";
			fieldToFilter.value =
				fieldToFilter.value.substring( 0, fieldToFilter.value.indexOf(".") + 3  )
			  + dec.substring( fieldToFilter.value.length - 1 - fieldToFilter.value.indexOf(".") );
		}
	}

	if ((min) && (max) && (fieldToFilter.value != ''))
	{
		if ((min == max) && ( fieldToFilter.value.length != min ) )
		{
			alert ('\nPlease enter a value that is\n' + min + ' characters in length.\n');
			if ( fieldToFilter.value.length > min )
			{
				fieldToFilter.value	= "";
				filtered			= "";
			}

			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
		else if (( fieldToFilter.value.length < min ) || ( fieldToFilter.value.length > max ))
		{
			alert ('\nPlease enter a value \nthat is between ' + min + ' and ' + max + ' \ncharacters in length.\n');
			fieldToFilter.value	= "";
			filtered			= "";

			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
	}

	return true;

} 