var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var SSNDelimiters = "- ";
var reWhitespace = /^\s+$/
var digitsInSocialSecurityNumber = 9;
var reInteger = /^\d+$/
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var ZIPCodeDelimiters = "-";


function centerScreen(page, winname, prefs)
{
var w = 480, h = 340;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

var popW = 300, popH = 200;

var leftPos = (w-popW)/2, topPos = (h-popH)/2;

OpenWin = this.open(page, winname, prefs+',top='+topPos+',left='+leftPos);
}


function centerBrowser(page, winname, prefs, popW, popH)
{
var w = 480, h = 340, h=0, t=0;
var wo = 0, ho = 0;

	if (document.all) {
	   /* the following is only available after onLoad */
	   w = document.body.clientWidth;
	   h = document.body.clientHeight;
	   t = document.body.clientTop;
	   wo = window.screenLeft;
	   ho = window.screenTop;
	   ho = ho - 70;
	   w = (w)/2 + wo;
	   h = (h)/2 + ho;
	}
	else if (document.layers) {
	   w = window.innerWidth;
	   h = window.innerHeight;
	   wo = window.screenX;
	   ho = window.screenY;
	   w = (w)/2 + wo;
	   h = (h)/2 + ho;
	}
	popW = popW/2;
	popH = popH/2;
	var leftPos = (w-popW), topPos = (h-popH);	

	OpenWin = this.open(page, winname, prefs+',top='+topPos+',left='+leftPos);
}



function newWin(page, winname, prefs) 
//Entered Matt Brass 2/1/2002
//Launch a new window with prefs and control name
//Usage onclick="newWin("login.asp","CtrlWindow", "status=no,height=300,width=300");"
{
	OpenWin = this.open(page, winname, prefs);
}


function numCheck(field1, fieldName)
//Entered Matt Brass 2/1/2002
//Ensure a field contains only numbers
//Usage onBlur="numCheck(this);"
{
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	for (var i=0; i<field1.value.length; i++) 
		{
		temp = "" + field1.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
		}
	if (ok == "no")
		{
		alert("Please enter only digit characters in the " + fieldName + " field.");
		field1.focus();
		field1.select();
		return (false);
		}
	return(true);
}


function autoTab(input,len, e) 
//Entered Matt Brass 2/1/2002
//AutoTabs to next field in TabOrder when a len(field) = X
//Usage onkeyup="return autoTab(this, 5, event);"
{
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) 
	{
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	
	function containsElement(arr, ele) 
	{
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
			found = true;
			else
			index++;
		return found;
	}

	function getIndex(input) 
	{
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	return true;
}


function emailValidate(field1)
//Entered Matt Brass 2/1/2002
//Ensures valid email address entered
//Usage onblur="emailValidate(this.form);"
{

if(field1.value.length > 1 )
	{
	var checkEmail = "@.";
	var checkStr = field1.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
		{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkEmail.length;  j++)
			{
			if (ch == checkEmail.charAt(j) && ch == "@")
				EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
				EmailPeriod = true;
			if (EmailAt && EmailPeriod)
				break;
			if (j == checkEmail.length)
				break;
			}
		// if both the @ and . were in the string
		if (EmailAt && EmailPeriod)
				{
			EmailValid = true
			//break;
			}
		}
	if (!EmailValid)
		{
			 alert("The \"email\" field is invalid. It must contain an \"@\" and a \".\".");
			field1.focus();
			field1.select();
			return (false);
		}
	else
		{
		return(true);
		}
	}
	else
	{
	return(true);
	}
}


function emailValidateHandle(field1)
//Entered Matt Brass 2/1/2002
//Ensures valid email address handle entered
//	but no domain.
//Usage onblur="emailValidateHandle(this.form);"
{

if(field1.value.length > 1 )
	{
	var checkEmail = "@.";
	var checkStr = field1.value;
	var EmailValid = true;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
		{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkEmail.length;  j++)
			{
			if (ch == checkEmail.charAt(j) && ch == "@")
				EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
				EmailPeriod = true;
			if (EmailAt || EmailPeriod)
				break;
			if (j == checkEmail.length)
				break;
			}
		// if both the @ and . were in the string
		if (EmailAt)
				{
			EmailValid = false
			//break;
			}
		}
	if (!EmailValid)
		{
			 alert("The \"email\" field is invalid. It cannot contain an \"@\" .");
			field1.focus();
			field1.select();
			return (false);
		}
	else
		{
		return(true);
		}
	}
	else
	{
	return(true);
	}
}



function textCounter(field, countfield, maxlimit)
//Entered Matt Brass 2/1/2002
//Creates a limit to the ammount of data that can be put into a textarea
//This is useful to prevent errors from oversided fields in an SP
//Usage onKeyDown="textCounter(this.form.notes,this.form.remLen,1000);" 
//      onKeyUp="textCounter(this.form.notes,this.form.remLen,1000);"
//		Plus create a readonly <input type=text name="remLen"
{
	if (field.value.length > maxlimit) 
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else 
	{
		countfield.value = maxlimit - field.value.length;
	}
}


function DeleteCheck(form) //function is called when the submit button is clicked 
//Entered Matt Brass 2/1/2002
//JS popup to determine if user wants to proceed with deleting X.
//Usage onclick="DeleteCheck(form);"
{
	if (confirm("Are you sure you want to Delete this Item from the system?  It CANNOT be undone!!!"))
		{
			return (true);
		}
		else
		{
			form.DeleteButton.value="Delete...";
			return (false);
		}
}


function checkAll()
//Entered Matt Brass 2/1/2002
//Sets all CheckBoxes in a group to On or Off
//Usage onClick="return checkAll();"
{
	for (var i=0;i<document.inbox.elements.length;i++)
	{
		var e = document.inbox.elements[i];
		if (e.name != 'selectAll')
		e.checked = !e.checked;
	}
}


function AllFilledIn(form)
//Entered Matt Brass 2/1/2002
//Ensures that all fields in a form are filled out
//Usage onClick="return checkAll();"
{
	//var form = document.forms[0]
	for (i=0;i<form.elements.length;i++) 
	{
		if ((form.elements[i].type == "text" || form.elements[i].type == "password") && form.elements[i].value == "") 
		{
			alert("Please fill out all fields.");
			form.elements[i].focus();
			form.elements[i].select();
			return (false);
			break
		}
	}
	return(true);
}


function AtLeastOneCheckedRadio(form) 
//Entered Matt Brass 2/1/2002
//Ensures that at least one checkbox is filled in
//Usage onClick="return checkAll();"
{
	//var form = document.forms[0]
	var checker = false
	for (i=0;i<form.elements.length;i++) 
	{
		if (form.elements[i].type == "radio" && form.elements[i].status == true) 
		{
			checker = true;
			break
		}
	}
	
	if (checker != true)
	{
		alert("Please click a radio button.");
		return (false);
	}
	else
	{
		return(true);
	}
}



function AtLeastOneCheckedAnswer(form) 
//Entered Matt Brass 2/1/2002
//Ensures that at least one checkbox is filled in
//Usage onClick="return checkAll();"
{
	//var form = document.forms[0]
	var checker = false
	for (i=0;i<form.elements.length;i++) 
	{
		if (form.elements[i].type == "radio" && form.elements[i].status == true) 
		{
			checker = true;
			break
		}
	}
	
	if (checker != true)
	{
		alert("Please select an Answer.");
		return (false);
	}
	else
	{
		return(true);
	}
}

function PassCheck(field1, field2)
//Entered Matt Brass 2/1/2002
//Ensures two password fields are correct, and ensures password is long enough
//Usage onBlur="PassCheck(form.password1, form.password2);"
{
	if (field1.value != field2.value)
		{
		alert("The two passwords are not the same.");
		field1.focus();
		field1.select();
		return (false);
		}
	else
		{
		return(true);
		}
    if(field1.value.length > 10 )
        {
        alert("You Cannot have a password longer than 10 characters.");
        field1.focus();
        return (false);
        }
	else
		{
		return(true);
		}
    if(form.field1.value.length < 5 )
        {
        alert("You must Enter a Password at least 5 Chars long.");
        field1.focus();
        return (false);
        }
	else
		{
		return(true);
		}
}

function valTextLen(field1, fieldName)
//Entered Matt Brass 2/1/2002
//Used to validate that a field has data
//Usage onBlur="valTextLen(this, 'First Name');" 
{
    if(field1.value.length == 0)
        {
        alert(fieldName + " cannot be blank.");
        if (field1.type == "select-one") 
			{
			field1.focus();
			}
		else
			{
			field1.focus();
			field1.select();
			return (false);
			}
        }
	else
		{
		return(true);
		}
}



function valTextLenHidden(field1, fieldName)
//Entered Matt Brass 2/1/2002
//Used to validate that a field has data
//Usage onBlur="valTextLen(this, 'First Name');" 
{
    if(field1.value.length == 0)
        {
        alert(fieldName + " cannot be blank.");
        if (field1.type == "hidden") 
			{
			
			}
		else
			{
			field1.focus();
			field1.select();
			return (false);
			}
        }
	else
		{
		return(true);
		}
}


function validateCC(field1, fieldName) 
{
	if(field1.value.length > 1 )
	{
		var valid = "0123456789"
		var ok = "yes";
		var temp;
		
		if(field1.value.length > 16 || field1.value.length < 16)
		{
			ok = "no";		
		}
		
		for (var i=0; i<field1.value.length; i++) 
		{
			temp = "" + field1.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") 
		{
			alert(fieldName + " must be 16 digits with no other characters or spaces.");
			field1.focus();
			field1.select();
			return(false);
		}
		else
		{
			return(true);
		}
	}
	else
	{
		return(true);
	}
}

function validatePhone(field1, fieldName) 
{
	if(field1.value.length > 1 )
	{
		var valid = "()0123456789-"
		var ok = "yes";
		var temp;
		
		if(field1.value.length > 14)
		{
			ok = "no";		
		}
		
		for (var i=0; i<field1.value.length; i++) 
		{
			temp = "" + field1.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") 
		{
			alert(fieldName + " can have numbers, dash, or Parentheses.");
			field1.focus();
			field1.select();
			return(false);
		}
		else
		{
			return(true);
		}
	}
	else
	{
		return(true);
	}
}

function checkSSN (theField, fieldName, emptyOK)
{
   if (checkSSN.arguments.length == 2) emptyOK = false;
    if ((emptyOK == true) && (theField.value.length == 0) && (fieldName.length==0)) return true;
    else
    {  var normalizedSSN = stripCharsInBag(theField.value, SSNDelimiters)
       if (!isSSN(normalizedSSN, false)) {
			alert(fieldName + " is an incorrect SSN.");
			theField.focus();
			theField.select();
			return (false);
		}
       else 
       {  // if you don't want to reformats as 123-456-7890, comment next line out
          theField.value = reformatSSN(normalizedSSN)
          return (true);
       }
    }
}

function isSSN (s)
//Used to validate Social Security Numbers
//Usage onBlur="isSSN(this);"
{ 	if (isEmpty(s)) 
       if (isSSN.arguments.length == 1) return false;
       else return (isSSN.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInSocialSecurityNumber)
}


function isEmpty(s)
// Check whether string s is empty.
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}

function stripCharsInBag (s, bag)
// Removes all characters which appear in string bag from string s.
{   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 reformatSSN (SSN)
// takes SSN, a string of 9 digits
// and reformats as 123-45-6789
{   return (reformat (SSN, "", 3, "-", 2, "-", 4))
}

function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function isInteger (s)
{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}

function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}

function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 4));
}

function checkZIPCode (theField, fieldName, emptyOK)
{   if (checkZIPCode.arguments.length == 2) emptyOK = false;
    if ((emptyOK == true) && (theField.value.length == 0) && (fieldName.length==0)) return true;
    else
    { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
      if (!isZIPCode(normalizedZIP, false)) {
			alert(fieldName + " is an incorrect Zip Code.");
			theField.focus();
			theField.select();
			return (false);
			}
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    }
}


function check(field, extVal) 
//Entered Matt Brass 4/18/2002
//Ensures that uploaded files are only of value passed in
//Usage onClick="return checkAll();"
{
	if(field.value.length != 0)
	{
		var ext = field.value;
		ext = ext.substring(ext.length-3,ext.length);
		ext = ext.toLowerCase();
		if(ext != extVal) 
		{
				alert('You selected a .'+ext+
			' file; please select an .'+extVal+' file instead!');
			return(false); 
		}
		else
		{
			return(true); 
		}
	}
	else
	{
		return(true);
	}
}

function dateCheck(dateVal, dateField, fieldName) 
{
	var b = /(\d{1,2})-(\d{1,2})-(\d{1,})/;
	var c = /[^\d-]/;
	var d = dateVal.match(b);
	if ((c.test(dateVal)) || (d == null)) 
	{
		//alert("The form field does not contain a valid date, or the date is not in the format \"dd-mm-yyyy\".");
		return(false);
	}
	if (d[1].length == 1) 
	{
		dateVal = "0" + dateVal;
	}
	var e = /29-0?2-(190[48]|19[2468][048]|19[13579][26]|20[02468][048]|20[13579][26])/;
	var f = /31-(0?[13578]|10|12)-(\d{1,})/;
	var g = /([012][1-9]|10|20|30)-((0?[13456789])|10|11|12)-(\d{1,})/;
	var h = /([012][0-8]|09|19)-(0?2)-(\d{1,})/;
	if (e.test(dateVal) || f.test(dateVal) || g.test(dateVal) || h.test(dateVal)) 
	{
		//alert("This is a valid date.");
		return(true);
	}
	//dateField.focus();
	alert(fieldName + " is not a valid Date.");
	return(false);
}
