<!--
//common.js
//author: EH
//purpose: common client side functions
//last updated 4/19/04
//
function isMaskChar(maskchar)
{
	if (maskchar=="#" || maskchar=="?" || maskchar=="!" || maskchar=="*")
	{
		return(true)
	}
	else
	{
		return(false)
	}
}
function updMask(txtobj,strmask)
{
	var retval=""
	var badchar=false
	var keyval=event.keyCode
	var strval=txtobj.value
	var maskchar=""
	var m,s
	
	//don't proceed if directional keys were pressed arrows backspace tab, etc.
	if ((keyval>=37 && keyval<=40)||keyval==9||(keyval==16&&keyval<=18)||(keyval>=33&&keyval<=36)||keyval==42||keyval==45||keyval==46||keyval==8)
	{
		return(true)
	}
	
	if (isMaskChar(strmask.charAt(0))==false && strval.charAt(0)!=strmask.charAt(0))
	{
		strval=strmask.charAt(0) + strval
	}
	for(var i=0; i<strmask.length; i++)
	{
		
		if (strval.length>i)
		{
			maskchar=strmask.charAt(i)
			strchar=strval.charAt(i)
			if (isMaskChar(maskchar)==false && strchar!="")
			{
				retval=retval+maskchar
			}
			else if (maskchar=='#') {
				if(!isNumberChar(strchar))
				{	
					badchar=true
				}
			}
			else if (maskchar=='?') {
				if(!isAlphabeticChar(strchar))
				{
					badchar=true
				}
			}
			else if (maskchar=='!') {
				if(!isNumOrChar(strchar))
				{
					badchar=true
				}
			}
			else if (maskchar=='*') {
			}

			if (badchar==false && isMaskChar(maskchar)==true)
			{
				retval=retval+strchar
			}
		}
	}

	i=strval.length
	maskchar=strmask.charAt(i)
	if (badchar==false && strval.length!=0 && strval.length<strmask.length && isMaskChar(strmask.charAt(i))==false)
	{
		retval=retval + strmask.charAt(i)
	}

	txtobj.value=retval
}
function isAlphabeticChar (InString)  {
	if(InString.length!=1) 
		return (false);
	InString=InString.toLowerCase();
	RefString="abcdefghijklmnopqrstuvwxyz";
	if (RefString.indexOf (InString.toLowerCase(), 0)==-1) 
		return (false);
	return (true);
}

function isNumberChar (InString)  {
	if(InString.length!=1) 
		return (false);
	RefString="1234567890";
	if (RefString.indexOf (InString, 0)==-1) 
		return (false);
	return (true);
}

function isNumOrChar (InString)  {
	if(InString.length!=1) 
		return (false);
	InString=InString.toLowerCase();
	RefString="1234567890abcdefghijklmnopqrstuvwxyz";
	if (RefString.indexOf (InString, 0)==-1)  
		return (false);
	return (true);
}

function checkAll(field)
{
	if (field==undefined)
	{ 
	return(false);}
	else if (field.length==undefined || field.length==null)
		{
			field.checked=true;}
	else
	{
		for (i = 0; i < field.length; i++)
		{	field[i].checked = true ; }
	}
}

function checkAllCheckBoxList(fieldname)
{
	flag=false;
	i=0;	//check box list field indexes
	while(flag==false)
	{
		tempfld = fieldname + '_' + i;
		if (eval(tempfld)==undefined)
		{ flag=true }	//end once an undefined field is found
		else
		{ eval(tempfld).checked = true ;	}	//check it
		i += 1;		//increment index to next field
	}
}

function uncheckAll(field)
{
	if (field==undefined)
	{ return(false);}
	if (field.length==undefined || field.length==null)
		{field.checked=false;}
	else
	{
		for (i = 0; i < field.length; i++)
			field[i].checked = false ;
	}
}

function uncheckAllCheckBoxList(fieldname)
{
	flag=false;
	i=0;	//check box list field indexes
	while(flag==false)
	{
		tempfld = fieldname + '_' + i;
		if (eval(tempfld)==undefined)
		{ flag=true }	//end once an undefined field is found
		else
		{ eval(tempfld).checked = false ;	}	//uncheck it
		i += 1;		//increment index to next field
	}
}
function disableAllFields()
{
	for (i=0;i<document.forms.length;i++)
	{
		for (j=0;j<document.forms[i].elements.length;j++)
		{
			document.forms[i].elements[j].disabled=true;
		}
	}
}

function enableAllFields()
{
	for (i=0;i<document.forms.length;i++)
	{
		for (j=0;j<document.forms[i].elements.length;j++)
		{
			document.forms[i].elements[j].disabled=false;
		}
	}
}

function addToList(listField, newText, newValue) {
   if ( ( newValue == "" ) || ( newText == "" ) ) {
      alert("You cannot add blank values!");
   } else {
      var len = listField.length++; // Increase the size of list and return the size
      listField.options[len].value = newValue;
      listField.options[len].text = newText;
      listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
   } // Ends the check to see if the value entered on the form is empty
}
function removeFromList(listField) 
{
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}

// InStr function written by: Steve Bamelis - steve.bamelis@pandora.be

function InStr(strSearch, strSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (strSearchFor == Mid(strSearch, i, Len(strSearchFor)))
	    {
			return i;
	    }
	}
	return -1;
}

function InStrRev(strSearch, strSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the last location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=Len(strSearch)-Len(strSearchFor)+1; i >= 0; i--)
	{
	    if (strSearchFor == Mid(strSearch, i, Len(strSearchFor)))
	    {
			return i;
	    }
	}
	return -1;
}

//Mid(string, start, length): Returns a specified number of characters from a string
function Mid(str, start, len)
        /***
                IN: str - the string we are LEFTing
                    start - our string's starting position (0 based!!)
                    len - how many characters from start we want to get

                RETVAL: The substring from start to start+len
        ***/
        {
                // Make sure start and len are within proper bounds
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                else
                        iEnd = start + len;

                return String(str).substring(start,iEnd);
        }

//Len(String) : Returns the number of characters in a string
        function Len(str)
        /***
                IN: str - the string whose length we are interested in

                RETVAL: The number of characters in the string
        ***/
        {  return String(str).length;  }

function dateAdd( start, interval, number ) {
	
    // Create 3 error messages, 1 for each argument. 
    var startMsg = "Sorry the start parameter of the dateAdd function\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var numberMsg = "Sorry the number parameter of the dateAdd function\n"
        numberMsg += "must be numeric.\n\n"
        numberMsg += "Please try again." ;
		
    // get the milliseconds for this Date object. 
    var buffer = Date.parse( start ) ;
	
    // check that the start parameter is a valid Date. 
    if ( isNaN (buffer) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }

    // check that the number parameter is numeric. 
    if ( isNaN ( number ) )	{
        alert( numberMsg ) ;
        return null ;
    }

    // so far, so good...
    //
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            number *= 24 ; // days to hours
            // fall through! 
        case 'h': case 'H':
            number *= 60 ; // hours to minutes
            // fall through! 
        case 'm': case 'M':
            number *= 60 ; // minutes to seconds
            // fall through! 
        case 's': case 'S':
            number *= 1000 ; // seconds to milliseconds
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    return new Date( buffer + number ) ;
}
function addtome(myfldval,addval)
{
	var tmpmyfldval

	tmpmyfldval = parseInt(myfldval);
	if (isNaN(tmpmyfldval)) { tmpmyfldval=0; }
	
	if (!isNaN(parseInt(addval)))
	{
		tmpmyfldval += parseInt(addval);
	}
	
	return tmpmyfldval;
}
function isnumbetween ( s, min, max )
{
	return ( ( s >= ( min != null ? min : Number.MIN_VALUE ) )
		&&  ( s <= ( max != null ? max : Number.MAX_VALUE ) ) );
}

function islenbetween ( s, min, max )
{
	return ( ( s.length >= ( min != null ? min : 0 ) )
		&&  ( s.length <= ( max != null ? max : Number.MAX_VALUE ) ) );
}
function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}

function compareFloats(num1,num2)
{
	
	if (num1=="" || num2=="") { return(true); }
	
	var tmpnum1 = parseFloat(num1);
	var tmpnum2 = parseFloat(num2);
	
	if (tmpnum1<tmpnum2) { return(-1); }
	if (tmpnum1==tmpnum2) { return(0); }
	if (tmpnum1>tmpnum2) { return(1); }

}
function sumFields(myformname,myfldlist,mytotalfld)
{
	var myfldarr = myfldlist.split(",")
	var i,tmptotal
	tmptotal = 0;

	for (i=0;i<myfldarr.length;i++)
	{
		//debug...alert(myfldarr[i] + "=" + eval("document."+myformname+"."+myfldarr[i]).value);
		tmptotal = addtome(tmptotal,eval("document."+myformname+"."+myfldarr[i]).value);
	}
	mytotalfld.value = tmptotal;
}
function sum(num1,num2)
{
	var tmpnum1,tmpnum2
	
	if (isNaN(parseFloat(num1)))
	{ tmpnum1=0; }
	else
	{ tmpnum1=parseFloat(num1); }
	if (isNaN(parseFloat(num2)))
	{tmpnum2=0}
	else
	{tmpnum2=parseFloat(num2)}	

	return tmpnum1+tmpnum2;
}
function subtract(num1,num2)
{
	var tmpnum1,tmpnum2
	
	if (isNaN(parseFloat(num1)))
	{ tmpnum1=0; }
	else
	{ tmpnum1=parseFloat(num1); }
	if (isNaN(parseFloat(num2)))
	{tmpnum2=0}
	else
	{tmpnum2=parseFloat(num2)}	

	return tmpnum1-tmpnum2;
}
function divide(num1,num2)
{
	var tmpnum1,tmpnum2
	
	if (isNaN(parseFloat(num1)))
	{ return 0; }
	else
	{ tmpnum1=parseFloat(num1); }
	if (isNaN(parseFloat(num2)))
	{return 0; }
	else
	{tmpnum2=parseFloat(num2)}	

	return tmpnum1/tmpnum2;
}
 function DoCal(elTarget) {
  if (showModalDialog) {
    var sRtn;
    sRtn = showModalDialog("include/Calendar.asp","","center=yes;dialogWidth=150pt;dialogHeight=200pt");
    if (sRtn!="")
      elTarget.value = sRtn;
  } else
    alert("Internet Explorer 4.0 or later is required.")
 }
 
function getRadioValue(myfld)
{
	var retval="";
	
	for (var i=0; i < myfld.length; i++)
    {
   		if (myfld[i].checked)
       {
      		retval = myfld[i].value;
       }
    }
	
	return(retval);
}
//
//drop down functions were found at
//http://home.att.net/~codeLibrary/HTML/dropdown.htm?make=&state=&model=Select+make&city=&color=Select+make
//
//update and create dynamic drop down lists
function update_dropdown(e, dd)
{
	for (j=1; j < dd.length; j++)
	{
		dd[j][0] = true;
	}

	for (j=1; j < dd[0].length; j++)
	{
		for (i=1; i < dd.length; i++)
		{
			current = dd[i][j].split("|");
			value = current[0];
			choice = current[0];
			if (current.length == 2) choice = current[1];
			if (value != document[dd[0][0]][dd[0][j]][document[dd[0][0]][dd[0][j]].selectedIndex].value) dd[i][0] = false;
		}
		if (e == document[dd[0][0]][dd[0][j]])
		{
			dropdown(j+1,dd);
			for (k=j+2; k < dd[0].length; k++)
			{
				document[dd[0][0]][dd[0][k]].length = 0;
			}
			break;
		}
	}
}

function dropdown(item,dd)
{
	var pre1 = "";
	var j = 1;
	document[dd[0][0]][dd[0][item]].options.length = 0;
	document[dd[0][0]][dd[0][item]].options[0] = new Option('Select ' + dd[0][item], '');
	document[dd[0][0]][dd[0][item]].options[0].selected = true;
	for (i=1; i < dd.length; i++)
	{
		if (dd[i][0] || item == 1)
		{
			current = dd[i][item].split("|");
			value = current[0];
			choice = current[0];
			if (current.length == 2) choice = current[1];
			if (value != pre1)
			{
				var op = new Option(choice, value);
				document[dd[0][0]][dd[0][item]].options[j] = op;
				j++;
				pre1 = value;
			}
		}
	}
}

//-->

