<!--
// Various data verification routines
//
//		dates: checkDate - returns a date in a valid format or alerts if not parseable
//
//		integers: makeValidInteger
//

var isNav = document.layers;
var isIe = document.all;

// Return a string that is a "standard" date format
// from the argument, or NULL if the date isn't recognizable

function checkDate(theInput)
{
    var s = theInput.value;
	var stdDate = null;
	var dateElements = null;
	var day = null;
	var month = "";
    var monthNum = null;
	var year = null;

	if (isEmpty(s))
    {
        theInput.value = "";
        return;		
    }

    stdDate = s;

    // get out all spaces in our date string
    foundSep = 0;
	if (s.match(/ /) != null)
	{
	    stdDate = stdDate.replace(/ /g, 'X');
        foundSep = foundSep + 1
	}
	else if (s.match(/\//) != null)
	{
        stdDate = stdDate.replace(/\//g, 'X');
        foundSep = foundSep + 1
	}
	else if (s.match(/\./) != null)
	{
	    stdDate = stdDate.replace(/\./g, 'X');
        foundSep = foundSep + 1
	}
	else if (s.match(/,/) != null)
	{
	    stdDate = stdDate.replace(/,/g, 'X');
        foundSep = foundSep + 1
	}
	else if (s.match(/-/) != null)
	{
	    stdDate = stdDate.replace(/-/g, 'X');
        foundSep = foundSep + 1
	}
    // Didn't have separators
	if (foundSep == 0)
    {
    	alert("Invalid date format");
        theInput.value = "";
        theInput.focus();
        return;
    }
	dateElements = stdDate.split('X');

	//alert("dateElements=" + dateElements + ", length=" + dateElements.length);
	// we need at least 3 elements
	
	if (dateElements.length != 3)
    {
    	alert("Invalid date format");
       	theInput.value = "";
       	theInput.focus();
       	return;
    }
	// now check for reasonable month, day, and year    
   	month = dateElements[0];
   	monthNum = parseInt(month,10);
   	day = dateElements[1];
   	year = dateElements[2];
   	//window.alert("monthNum=" + monthNum + ", day=" + day + ", year=" + year);
    if ((parseInt(monthNum,10) < 1) || (parseInt(monthNum,10) > 12))
    {
 		alert("Invalid date format");
       	theInput.value = "";
       	theInput.focus();
        return;
    }
    // Check if year is a valid number
    num_year = (year * 10)/ 10;
	//window.alert("num_year=" + num_year + ", year=" + year);
    
    if (isNaN(num_year))
    {
       	alert("Invalid year format in date");
       	theInput.value = "";
       	theInput.focus();
       	return;
     }
	// check the year and adjust it to 1900 or 2000
	
	if ( ("" + year).length < 1)
    {
       	alert("Invalid year format in date");
       	theInput.value = "";
       	theInput.focus();
       	return;
    }
	
	if ((parseInt(num_year,10) < 10) || (("" + year).length == 1))
    {
	    // window.alert("length = 1");
		year = "200" + num_year;
    }
 	else if (parseInt(num_year,10) < 100 || (("" + year).length == 2))
	{
	    // window.alert("length = 2");
	    if (num_year > 25)
	    	year = "19" + num_year;
	    else 
           	year = "20" + num_year;
	}
	else if (parseInt(num_year,10) < 1000 || (("" + year).length == 3))
    {
       	alert("Invalid year format in date");
       	theInput.value = "";
       	theInput.focus();
       	return;
    }
	else if (("" + year).length > 4)
    {
       	alert("Invalid date format");
       	theInput.value = "";
       	theInput.focus();
       	return;
    }
	else if (parseInt(num_year,10) < 1900 )
    {
       	alert("Invalid date format");
       	theInput.value = "";
       	return;
    }

	// check for a reasonable day for the month
   	var leapYear=false;
   	/*****************************************
   	* Every fourth year is leap year unless evenly div by 4
   	* except when div by 400.
   	******************************************/
   	if(((year%4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
   	{ 
    	leapYear=true;
   	} 
   	var maxDays=31 //all the rest have 31
   	if (parseInt(monthNum,10)==2)    // Feb is the problem
   	{
      	if (leapYear)
      	{
         	maxDays=29;
      	}
      	else 
      	{
         	maxDays=28;
      	}
   	}
   	if (monthNum==4 || monthNum==6 || monthNum==9 || monthNum==11)// thirty days hath...
   	{
      	maxDays=30;
   	}
   	if ((parseInt(day,10) < 1) || (parseInt(day,10) > maxDays))
   	{
       	alert("Invalid number of days in the month");
       	theInput.value = "";
       	theInput.focus();
       	return;
   	}
	// add leading "0" if needed so we show dd
	if ((parseInt(day,10) < 10) && (day.length < 2))
		day = "0" + day;
    if (monthNum < 10 && monthNum.length < 2)
        month = "0" + monthNum;

    var newDay =  month + "/" + day + "/" + year;
    theInput.value=newDay;
}


function checkTMCNumber(theInput)
{
	var re = new RegExp("^\\d{6}$");		
	if(! theInput.value.match(re))
	{
		alert("Invalid  MCNumber format\nProper format is 999999");
		theInput.value="";
	}
}

function checkDPhone(theInput)
{
    var shortPhone;
    var phoneLong = theInput.value;
    if (phoneLong.length > 12) 
    {
		shortPhone = phoneLong.slice(0,12);
		var tempPhone = validPhone(shortPhone);
		if (tempPhone == "") 
		{
    	  	alert("Invalid phone number format\nProper format is 999-999-9999x999");
    	  	theInput.value = "";
    	  	theInput.focus();
    	  	return;
        }
        else 
        {
        	var strLen = phoneLong.length;
          	var extension = phoneLong.slice(12, strLen)
          	var phNum = tempPhone + extension
          	theInput.value = phNum;
          	theInput.focus();
          	return;
        }
    }
    else 
    {
    	var tempPhone = validPhone(phoneLong);
		if (tempPhone == "") 
		{
    	  	alert("Invalid phone number format\nProper format is 999-999-9999x999");
    	  	theInput.value = "";
    	  	theInput.focus();
    	  	return;
        }
        else 
        {
    	  	theInput.value = tempPhone;
	  		theInput.focus();
          	return;
    	}
    }
}

function checkPhone(theInput)
{
    var s = theInput.value;
    if (isEmpty(s))
    {
        theInput.value = "";
        return;		
    }
    else 
    {
		phoneNumber = validPhone(s);
		if (phoneNumber == "") 
		{
	  		alert("Invalid phone number format\nProper format is 999-999-9999");
	  		theInput.value = "";
	  		theInput.focus();
	  		return;
		}
		else 
		{
			theInput.value = phoneNumber;
	  		return;
        }
    }
}

function validPhone(theValue)
{
    var s = theValue;
	var stdPhone = null;
	var phoneElements = null;

    stdPhone = s;

    // get out all spaces in our date string
    foundSep = 0;
	if (s.match(/\(/) != null)
	{
		//alert("found paren");
        stdPhone = stdPhone.replace(/\(/g, '');
        stdPhone = stdPhone.replace(/\)/g, 'X');
        foundSep = foundSep + 1
	   	if (s.match(/ /) != null)
	   	{
			stdPhone = stdPhone.replace(/ /g, '');
	   	}
	   	if (s.match(/-/) != null)
	   	{
	  		stdPhone = stdPhone.replace(/-/g, 'X');
	   	}
        //alert(stdPhone);
	}
	else if (s.match(/ /) != null)
	{
	    stdPhone = stdPhone.replace(/ /g, 'X');
        foundSep = foundSep + 1
	}
	else if (s.match(/\./) != null)
	{
	    stdPhone = stdPhone.replace(/\./g, 'X');
        foundSep = foundSep + 1
	}
	else if (s.match(/,/) != null)
	{
	    stdPhone = stdPhone.replace(/,/g, 'X');
        foundSep = foundSep + 1
	}
	else if (s.match(/-/) != null)
	{
	    stdPhone = stdPhone.replace(/-/g, 'X');
        foundSep = foundSep + 1
	}
    // Didn't have separators
	if (foundSep == 0)
    {
		return "";
    }
	phoneElements = stdPhone.split('X');
	var areaCode = validateNumber(phoneElements[0]);
	var prefix = validateNumber(phoneElements[1]);
	var pNumber = validateNumber(phoneElements[2]);
	
	if (!areaCode || !prefix || !pNumber) 
	{
		return "";
	}
	// we need at least 3 elements
	if (phoneElements.length != 3 || phoneElements[0].length != 3 || phoneElements[1].length != 3 || phoneElements[2].length != 4)
	{
	    return "";
    }
    var newPhone =  phoneElements[0] + "-" + phoneElements[1] + "-" + phoneElements[2];
    return newPhone;
}

function makeValidInteger(keepFormat, s)
{
	var goodInteger = s;
	var integerElements = null;
	var hasDollar = false;
	var hasCommas = false;
	var hasCents = false;

	if ((s == null) || isEmpty(s))
		return ("");

	// First we'll get rid of anything that isn't a number.
	// Then we'll rebuild with extras if keepFormat is true
	// or just return the number as a string if keepFormat is false.

	// any dollar signs?  if so, get rid of them
	if (s.match(/\$/) != null)
		hasDollar = true;

	// any commas?
	if (s.match(/,/) != null)
		hasCommas = true;

	// any cents?
	if (s.match(/\./) != null)
	{
		hasCents = true;
		intElements = s.split('.');
		s = intElements[0];
	}

	// get rid of any letters
	goodInteger = s.replace(/\D/g, '');

	// need to put back formatting?
	if (keepFormat)
	{
		var formattedInt = "";
		// commas
		if (hasCommas)
		{
			i = goodInteger.length - 1;
			comCnt = 0;
			while (i >= 0)
			{
				formattedInt += goodInteger.charAt(i);
				comCnt++;
				if ((comCnt % 3 == 0) && (i > 0))
				{
					formattedInt += ',';
				}
				i--;
			}
			straight = "";
			for (i = formattedInt.length - 1; i >= 0; i--)
				straight += formattedInt.charAt(i);
			goodInteger = straight;
		}
		if (hasDollar)
			goodInteger = "$" + goodInteger;
	}

	return(goodInteger);
}

function makeValidDecimal(s, decimalIncr)
{
	var decimalPart = null;
	var nonDecimalPart = null;
	var numberParts = null;

	if ((s == null) || isEmpty(s))
		return ("");

	// split the string on decimal point(s)
	numberParts = s.split('.');

	// now get rid of anything in the 1st number part that isn't a number
	nonDecimalPart = numberParts[0].replace(/[^\d]/g, '');
	if (nonDecimalPart.length == 0)
		nonDecimalPart = "0";

	// do the same if we have a decimalPart
	if (numberParts.length > 1)
	{
		decimalPart = numberParts[1].replace(/[^\d]/g, '');
		if (decimalPart.length == 0)
			decimalPart = 0;
		else if (decimalPart.length == 1)
			decimalPart = decimalPart * 10;
		else decimalPart = decimalPart.substr(0, 2) - 0;
	}
	else decimalPart = 0;

	// now round the decimal part to the right increment
	decimalIncr *= 100;
	x = decimalPart - 0;
	x = Math.round(x / decimalIncr) * decimalIncr ;
	if (x == 100)
	{
		x = 0;
		nonDecimalPart = nonDecimalPart - 0 + 1;
	}

	// pad the decimal to 2 if it's not already
	if (x == 0)
		decimalPart = "00";
	else decimalPart = "" + x;
	return(nonDecimalPart + "." + decimalPart);
}

function isEmpty(s)
{
	if ((s == null) || (s == ""))
		return (true);
	else return(isBlank(s));
}

function isBlank(s)
{
    for (var i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t'))
            return false;
    }
    return true;
}

function isDigit(s)
{
    // This returns true if the string is a sigle digit.
    // match takes a regular expression and returns an array of matches if
    // there is a match and null if there is not. /d is a single digit (0-9)

    return s.match(/^\d$/) != null;
}

function isValidSQLName(string)
{
   	if(string.match(/[^a-zA-Z0-9_ ]/)) 
   	{ 
    	return false;
   	} 
   	else 
   	{
    	return true;
   	}
}
 //
 //	This is a copy of the "isNumber" function used on the search pages - <pg>

function checkNumber(number) 
{
  	if (isNaN(number.value))
  	{
    	alert("Only numbers or numbers with '.' allowed (ex: 1000 not 1,000)");
    	number.value="";
    	number.focus();
  	}
}

function validateNumber(number) 
{
  	if (isNaN(number))
  	{
		return false;
  	}
  	else return true;
}

function checkInteger(field, mustEnter)
{
	var fieldValue = field.value;
	var goodInteger = null;

	// good integer?
	goodInteger = makeValidInteger(true, fieldValue);
	if (isEmpty(goodInteger))
	{
		window.alert("Please re-enter a valid integer (ddd,ddd) or nothing.");
		field.value = "";
		if (mustEnter)
			field.focus();
	}
	else field.value = goodInteger;
}

function checkDecimal(field, floatIncr, mustEnter)
{
	var fieldValue = field.value;
	var goodDecimal = null;

	// good decimal?
	goodDecimal = makeValidDecimal(fieldValue, floatIncr);
	if (isEmpty(goodDecimal))
	{
		window.alert("Please re-enter a valid decimal (dd.dd) or nothing.");
		field.value = "";
		if (mustEnter)
			field.focus();
	}
	else field.value = goodDecimal;
}


function mustEnter(field)
{
	var fieldValue = field.value;

	if (isEmpty(fieldValue) || isBlank(fieldValue))
	{
		window.alert("Please enter data into field.");
		field.focus();
	}
}

function checkEmail(theEmail)
{
    var EmailCorrect = false
    if (theEmail.value == "") return;
	//just check for // and ., to many types/countries to allow a hard coded list
    for (var i = 0; i <= theEmail.value.length; i++)
    {
         if (theEmail.value.charAt(i) == "@") {EmailCorrect = true}
    }

	if (EmailCorrect == true)  // then we have the @ symbol
	{   
	    EmailCorrect = false;
	    for (var i = 0; i <= theEmail.value.length; i++)
	    {
	         if (theEmail.value.charAt(i) == ".") {EmailCorrect = true}  //period
	    }
	}

    /*
    var EMLength = theEmail.value.length
    if (theEmail.value.substring(EMLength - 3, EMLength) != "com" &&
        theEmail.value.substring(EMLength - 3, EMLength) != "net" &&
        theEmail.value.substring(EMLength - 3, EMLength) != "org") 
    {EmailCorrect = false}   
	*/

    if (EmailCorrect == false)
    {
        alert("Your e-mail address is invalid!"); 
        theEmail.value = "";
        theEmail.focus();
    }
}

function checkDPhone(theInput)
{
    var shortPhone;
    var phoneLong = theInput.value;
    if (phoneLong.length > 12) 
    {
		shortPhone = phoneLong.slice(0,12);
		var tempPhone = validPhone(shortPhone);
		if (tempPhone == "") 
		{
    	  	alert("Invalid phone number format\nProper format is 999-999-9999x999");
    	  	theInput.value = "";
    	  	theInput.focus();
    	  	return;
        }
        else 
        {
        	var strLen = phoneLong.length;
          	var extension = phoneLong.slice(12, strLen)
          	var phNum = tempPhone + extension
          	theInput.value = phNum;
          	theInput.focus();
          	return;
        }
    }
    else 
    {
    	var tempPhone = validPhone(phoneLong);
		if (tempPhone == "") 
		{
    	  	alert("Invalid phone number format\nProper format is 999-999-9999x999");
    	  	theInput.value = "";
    	  	theInput.focus();
    	  	return;
        }
        else 	
        {
    	  	theInput.value = tempPhone;
	  		theInput.focus();
          	return;
    	}
    }
}

/************************************
* Count the number of characters entered
* in a textarea box to limit the number of
* of characters to the db table column size.
************************************/
function textCounter(field,  maxlimit)
{
	if (field.value.length > maxlimit)
	{ // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		alert("You have reached the " + maxlimit + " character maximum for this entry");
	}
}

function SetTextFocus(thisone,fieldsize,nextone)
{
	if(thisone.value.length >= fieldsize)
	{
		nextone.focus();
	}
}

//  End hide from old browsers -->

