// JavaScript validation Functions for Nelix Projects
// 11/11/03 BTE & GM

function numbersOnly(e,otherchars)
{// Embedded Javascript validation for numbers, with decimal<br>
//Calling: return numbersOnly(event);
var key=0;
var validChars = "0123456789";

if (window.event || e.which==false) // IE
	{key = e.keyCode; }	
else if (e.which) // netscape
	{key = e.which; }
	
if (key==0) return true;

if (otherchars !=null) {validChars = validChars + otherchars;}

var cVal = (validChars.indexOf(String.fromCharCode(key)) > -1 );
if (key > 47 & key < 58) cVal=true;
//8 = backspace, 46=delete, 37=left arrow, 39 =right arrow
if ( key==8 | key==46  | key==37 | key==39 | cVal)
	{ return true;  }
else
	{return false;}	
} //end function

String.prototype.trim = function () {
    this.replace(/^\s+/, "").replace(/\s+$/, "");
} 

function autoTab (element,typeLength)
{
// Created 10/9/03 BTE for NetsToYou.com
// autotab function will shift focus to next field for phone number info.
// This is broken into areacode or last 7 digits of core phone number, the allowedit property allows for edits to the value

//calling:   onFocus="this.select(); allowEdit=false;" onKeyDown="allowEdit=true;" onKeyUp=" if (allowEdit) {autoTab(this,7);}" 

if (element.value.length >= typeLength) 
{
var nexti=0; 
for (x=0; x<document.forms[0].length;x++) { 
	if (element.name==document.forms[0].elements[x].name) 
		{nexti = x + 1; break;} //end if
};//end for
document.forms[0].elements[nexti].focus();
}//end outer if
else
{//for phone number only auto inserts the '-'
 if (typeLength==8 & element.value.length==3)
element.value=element.value + '-';
}//end else

}//end function

function superTrim(theForm)
{
// BTE 7.30.03  Used to trim out the spaces and returns that are useless
for (var x = 0; x < theForm.elements.length; x++)
{   var newString="";
	var bolnormal=false;	
	if (theForm.elements[x].type=='textarea' | theForm.elements[x].type=='text')
	{ newString="";
	//alert(theForm.elements[x].value.length);
	for (i = theForm.elements[x].value.length; i >=0; i--)
	{ var tchar = theForm.elements[x].value.charCodeAt(i);
		if (tchar > 32)
			{start=0;
			if (theForm.elements[x].value.charCodeAt(0) < 33)
			{start=2;};
			newString = theForm.elements[x].value.substr(start,i+1);
			theForm.elements[x].value = newString;
			//alert("|" + newString + '|');
			break;}//end if
		else {bolnormal=true;}
	}//end inner for
	if (bolnormal & i==0) { theForm.elements[x].value='';}
	} //end if
} //end for
}//end function


function formatValue(input,symbol,location)
{
// This will autoformat the value in an input based on parameters
//added 10/15/03 for NetsToYou.com

//calling:  onBlur="formatValue(this,'$','prefix');"
if (input.value.indexOf(symbol) == -1)
{
if (location=='prefix')
{ input.value = symbol + input.value;}
if (location=='suffix')
{ input.value =   input.value + symbol;}
}//end if

}//end function

function validateDate(obj)  {
//validates for a date entered as text in MM/DD/YYYY format

//calling:  onBlur="validateDate(this);"
var slashes = 0;
var notnumber = false;
var today = new Date();
	var dateStr=obj.value;
   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

   if (matchArray == null) {
        alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
			obj.focus();
			return false;    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
			obj.focus();
			return false;    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
			obj.focus();
			return false;    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn't have 31 days!")
			obj.focus();
			return false;    }

    if (month == 2) { // check for february 29th
        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!");
			obj.focus();
			return false;        }
    }

/*

//for (i = 1; i < obj.elements.length; i++) {
//	if (obj.elements[i].name.substr(1,6) == "birth") {
		slashes = 0;
		//dateVal = obj.elements[i].value;
		dateVal = obj.value;
		for (position=1; position < dateVal.length; position++) {
			letter = dateVal.charAt(position);
			if (letter == "/") {slashes++; continue;}
			if (!(parseInt(letter)) & letter !="0") {notnumber=true;} 
		}//end for
		if (slashes != 2) {notnumber= true;}
		if (notnumber == false) {
			datenum = dateVal.split('/');
			if (datenum[0] > 12) {notnumber = true;}
			if (datenum[1] > 31) {notnumber = true;}
			if (datenum[2] > today.getYear()) {notnumber = true;}
		}
		if (notnumber == true) {
			alert("The date is not in corrent MM/DD/YYYY format \n or is not a valid date, please correct it.");
			//obj.elements[i].focus();
			obj.focus();
			return false;
		}
	//}
//} // end for
*/
return true;

}//end validate


//dynamic property assignment functions
//calling:  findObj('image1')
function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function showSection(name)
{
var x = findObj(name);
if (x!=null)
	{ x.style.display='inline';}
return true;
}

function hideSection(name)
{
var x = findObj(name);
if  (x!=null)
	{ x.style.display='none';}
return true;
}
