var clicks = 0;
var invalidChars = "\"<>|";
var invalidFileChars = "/\\: *?" + invalidChars;
var reWhitespace = /^\s+$/;
var digits = "0123456789";
var reDigit = /^\d/;
var reInteger = /^\d+$/;
var digitsInUSPhoneNumber = 10;
var phoneNumberDelimiters = "()-. ";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var reEmail = /^.+\@.+\..+$/;
var defaultEmptyOK = false;
var errStack = "";
var focusSet = 0;
var daysInMonth = new Array(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function isEmpty(s)
{   
    return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{
    return (isEmpty(s) || reWhitespace.test(s));
}

function isAlpha (s)
{
	return isIntegerInRange(s.toLowerCase().charCodeAt(0), 97, 122);
}

function isFloat (s)
{
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
	for(i = 0; i < s.length; i++)
		if(s.charAt(i) != "." && !isInteger (s.charAt(i)))
			return false;
	return true;
}

function isInteger (s)
{   var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    return reInteger.test(s)
}

function isSignedInteger (s)
{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);
    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;
        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;
    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);
    if (!isInteger(s, false)) return false;
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 containsBlanks(s){
    for (var i = 0; i < s.length; i++)
        if(isWhitespace(s.charAt(i))) return true;
   return false;
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    else if (containsBlanks(s)) return false;
    else {
       return reEmail.test(s);
    }
}

function hasInvalidChars (c)
{
	var return_val = false;
	for(var i = 0; i < c.length; i++)
		for(var j = 0; j < invalidChars.length; j++)
			if(c.charAt(i)==invalidChars.charAt(j))
				return_val = true;
	return return_val;
}

function isValidFile (c)
{
	for(var i = 0; i < invalidFileChars.length; i++)
  if(c.indexOf(invalidFileChars.charAt(i)) != -1) return false;
	return true;
}

function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function reformatUSPhone (USPhone)
{ return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}

function checkUSPhone (theField, emptyOK)
{  if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;	
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
       if (!isUSPhoneNumber(normalizedPhone, false)) 
          return false;
       else 
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          theField.value = reformatUSPhone(normalizedPhone)
          return true;
       }
    }
}

function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}

function daysInFebruary (year)
{
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day)
{
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);
    if (intDay > daysInMonth[intMonth]) return false; 
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
    return true;
}

function isValidDate(s) {
	var d, m, y, pos1, pos2;
	pos1 = s.indexOf("/");
	m = s.substr(0,pos1);
	if(m.charAt(0) == "0") m = m.charAt(1);
	if(s.charAt(pos1 + 3) == "/") pos2 = pos1 + 3;
	else pos2 = pos1 + 2;
	d = s.substring(pos1 + 1, pos2);
	if(d.charAt(0) == "0") d = d.charAt(1);
	y = s.substring(pos2 + 1, s.length);
	return isDate(y,m,d);
}

function moveUp(f)
{
    var temp;
    for(var i = 1; i < f.options.length; i++)
    {
        if(f.options[i].selected == true)
        {
           temp = f.options[i-1];
           f.options[i-1] = new Option(f.options[i].text,f.options[i].value);
           f.options[i] = new Option(temp.text,temp.value);
           f.options[i-1].selected = true;
        }
    }
}

function moveDown(f)
{
    var temp;
    for(var i = f.options.length - 2; i >= 0; i--)
    {
        if(f.options[i].selected == true)
        {
           temp = f.options[i+1];
           f.options[i+1] = new Option(f.options[i].text,f.options[i].value);
           f.options[i] = new Option(temp.text,temp.value);
           f.options[i+1].selected = true;
        }
    }
}

function move(fromField,toField)
{
    for(var i = 0; i < fromField.options.length; i++)
        if(fromField.options[i].selected)
            toField.options[toField.options.length] = new Option(fromField.options[i].text, fromField.options[i].value);
    for(var i = fromField.options.length; i > 0; i--)
        if(fromField.options[i-1].selected)
            fromField.options[i-1] = null;
}

function selAll(f) { for(var i = 0; i < f.options.length; i++) f.options[i].selected = true; }


function setFocus(f)
{
    if(!focusSet)
    {
        f.select();
        f.focus();
		focusSet = 1;
    }
}

function displayError (s,c)
{
	alert("This form contains the following invalid data:\n" + s + "\n\nPlease correct the listed items before continuing.");
    errStack = "";
    focusSet = 0;
}

function disableButtons(f)
{
	for(var i = 0; i < f.elements.length; i++)
		if(f.elements[i].type == "button")
			f.elements[i].disabled = true;
}

function findFocus(f)
{
	for (var i = 0; i < f.elements.length; i++)
	{
		try
		{
			f.elements[i].focus();
			break;
		}
		catch(er) { }
	}
}