/*
Author:  Stan Merrill
Date:  Feb 28, 2009
*/
function trimAll(sString)
/* *******************************************
Trims all spaces from the string and returns ths string
******************************************* */
{
    if (typeof(sString) == 'undefined' )
    {
        return "";
    } 

    if (sString.length  == 0)
    {
        return "";
    } 

    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
return sString;
}

function checkNumeric(input, fieldName)
{ 
    //Issues an alert if the field is not numeric.
 var number_format = " .0123456789";
 var number_format_2 = "0123456789";
 var check_char;
 var decimal = false;
 var trailing_blank = false;
 var digits = false;
 for (var i = 0; i < input.value.length; i++)
 {
  check_char = number_format.indexOf(input.value.charAt(i))
  if (check_char < 0)
  {
   alert(fieldName + " is not in numeric format")
   return false;
  }
  if (check_char == 1)
  {
   if (decimal)
   {  // Second decimal.
    alert(fieldName + " is not in numeric format.    It can not have two decimals")
    return false;
   }
   else
   {
    decimal = true;
   }
  }
  if (check_char == 0)
  {
   if (decimal || digits) 
    trailing_blank = true;
          // ignore leading blanks
  }
     if (trailing_blank)
  {
   alert(fieldName + " is not in numeric format.    It can not have a trailing blank")
   return false;
  }
  else
  {
   digits = true;
  }
 }
 return true; 
}

function checkInteger(input, fieldName)
{ 
 // Issues an alert if the field is not integer.
 var number_format = "0123456789";
 var check_char;
 var decimal = false;
 var digits = false;
 for (var i = 0; i < input.value.length; i++)
 {
  check_char = number_format.indexOf(input.value.charAt(i))
  if (check_char < 0)
  {
   alert(fieldName + " is not in integer format")
   return false;
  }
 }
 return true; 
}

function dateEdit(input,fieldName){
 // The field has been updated and the update should be marked markUpdate();
    //Returns true if value is a date in the mm/dd/yyyy format
 if (_checkdate(input))
 {
  return true;
 }
 else
 {
  errorReturn(input,fieldName);
  return false;
 }
}
function errorReturn(input, fieldName)
    {
  alert("Invalid date format for " + fieldName);
  input.focus();
  return false;
 }
function _checkdate(input)
{
 var ObjectValue=input.value;
 today = new Date();
 ObjectValue=strip(ObjectValue);
    if (ObjectValue.length == 0)
        return true;
    //Returns true if value is a date in the mm/dd/yyyy format
 FirstSplit = ObjectValue.indexOf('/');

 if (FirstSplit == -1 )
 {
  // No Seperator
  switch (ObjectValue.length)
  {
   case 1 :
    sDay=ObjectValue;
    sMonth=today.getMonth()+1;
    sYear=today.getFullYear();
    break ; 
   case 2 :
    sDay=ObjectValue;
    sMonth=today.getMonth()+1;
    sYear=today.getFullYear();
    break ;
   case 4 :
    sMonth=ObjectValue.substring(0,2); 
    sDay=ObjectValue.substring(2); 
    sYear=today.getFullYear();
    break ;
   case 6 :
    sMonth=ObjectValue.substring(0,2); 
    sDay=ObjectValue.substring(2,4); 
    sYear=ObjectValue.substring(4);
    break ;
   case 8 :
    sMonth=ObjectValue.substring(0,2); 
    sDay=ObjectValue.substring(2,4); 
    sYear=ObjectValue.substring(4);
    break ; 
   default :
    return false; 
  }
 }
 else 
 {
  if  (FirstSplit == ObjectValue.length)
  {
  // Seperator is at the end of the field
   return false ;
  }
  else
  {
   // Seperator is not at the end of the field 
   SecondSplit=ObjectValue.indexOf('/', FirstSplit + 1);
   //isplitMonth = ObjectValue.indexOf('/', isplitMonth + 1); 
   if (SecondSplit== -1 )
   {
    //Only one seperator
    sMonth=ObjectValue.substring(0, FirstSplit);
    sDay=ObjectValue.substring(FirstSplit+1);
    sYear=today.getFullYear();
   }
   else
   {
    // Two Seperators
    sMonth=ObjectValue.substring(0, FirstSplit);
    sDay=ObjectValue.substring(FirstSplit+1,SecondSplit);
    sYear=ObjectValue.substring(SecondSplit+1);
   }
  
  }
 }
 if (sMonth.length == 0 || sDay.length == 0 || sYear.length ==0)
        return false;
 if (sMonth.length == 1)
 {
  sMonth="0" +sMonth;
 }
 if (sDay.length == 1)
 {
  sDay="0" +sDay;
 }
 
 if (sYear.length == 1)
 {
  sYear = "200" + sYear;
 }
 // 04/22/2004 AM added this code to do a length check of the year
 //AM 08/02/04 AM added month and day length check too
 if (sYear.length == 3 || sDay.length == 3 || sMonth.length ==3)
 {
  return false;
 }
 else
 {
  if (sYear.length == 2)
  {
   if (sYear > 50)
   {
    sYear = "19" + sYear;
   }
   else
   {
    sYear = "20" + sYear;
   }
  }
 }
  //if (!JS_EDIT_checkinteger(sMonth))check month
 // return false;
 //else
  
 ObjectValue= sMonth + "/" +sDay +"/" +sYear; 
 input.value=ObjectValue;
 
 // 02/12/2004 AM added this code to do a length check of the individual day, month and year
 /*if (sDay.length > 2)
 {
  //alert('day false');
  return false;
 }
 
 if (sMonth.length > 2)
 {
  //alert('month false');
  return false;
 }
 
 if (sYear.length != 4)
 {
 alert('year false');
  return false;
 }*/
 // End AM change 02/12/2004
 
 if (!_JS_EDIT_RandD_checkrange(sMonth, 1, 12)) //check month
  return false;
 else
 if (!JS_EDIT_checkinteger(sYear)) //check year
  return false;
 else
 if (!_JS_EDIT_RandD_checkrange(sYear, 0, 9999)) //check year
  return false;
 else
 if (!JS_EDIT_checkinteger(sDay)) //check day
  return false;
 else
 if (!_JS_EDIT_RandD_checkday(sYear, sMonth, sDay)) // check day
  return false;
 else
  return true;
}
function _JS_EDIT_RandD_checkday(checkYear, checkMonth, checkDay)
    {
 maxDay = 31;
 if (checkMonth == 4 || checkMonth == 6 ||
   checkMonth == 9 || checkMonth == 11)
  maxDay = 30;
 else
 if (checkMonth == 2)
 {
  if (checkYear % 4 > 0)
   maxDay =28;
  else
  if (checkYear % 100 == 0 && checkYear % 400 > 0)
   maxDay = 28;
  else
   maxDay = 29;
 }
 return _JS_EDIT_RandD_checkrange(checkDay, 1, maxDay); //check day
    }
function _JS_EDIT_RandD_checkrange(ObjectValue, min_value, max_value)
    {
    //if value is in range then return true else return false
    if (ObjectValue.length == 0)
        return true;

    if (!JS_EDIT_checkinteger(ObjectValue))
 {
 return false;
 }
    else
 {
 return (_JS_EDIT_RandD_numberrange((eval(ObjectValue)), min_value, max_value));
 }
 
    //All tests passed, so...
    return true;
    }
function _JS_EDIT_RandD_numberrange(ObjectValue, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
 {
        if (ObjectValue < min_value)
  return false;
 }
    // check maximum
    if (max_value != null)
 {
 if (ObjectValue > max_value)
  return false;
 }
 
    //All tests passed, so...
    return true;
    }
function JS_EDIT_checkinteger(ObjectValue)
{
 if (ObjectValue.length == 0)
        return true;
 var number_format = "0123456789";
 
 for (var i = 0; i < ObjectValue.length; i++)
 {
 
  if (number_format.indexOf(ObjectValue.charAt(i)) == -1)
  {
   return false;
  } 
 }
 return true;
}
function JS_EDIT_checkfloat(object_value)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false 
    if (object_value.length == 0)
        return true;
    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
 var start_format = " .+-0123456789";
 var number_format = " .0123456789";
 var check_char;
 var decimal = false;
 var trailing_blank = false;
 var digits = false;
    //The first character can be + - .  blank or a digit.
 check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
 if (check_char == 1)
     decimal = true;
 else if (check_char < 1)
  return false;
        
 //Remaining characters can be only . or a digit, but only one decimal.
 for (var i = 1; i < object_value.length; i++)
 {
  check_char = number_format.indexOf(object_value.charAt(i))
  if (check_char < 0)
   return false;
  else if (check_char == 1)
  {
   if (decimal)  // Second decimal.
    return false;
   else
    decimal = true;
  }
  else if (check_char == 0)
  {
   if (decimal || digits) 
    trailing_blank = true;
        // ignore leading blanks
  }
         else if (trailing_blank)
   return false;
  else
   digits = true;
 } 
    //All tests passed, so...
    return true
}
function strip(myString)
{
// alert ('XYZ DEBUG typeof(myString)'+ typeof(myString));
 if (typeof(myString) != "string")
 {
  return;
 }
 var StringLen = myString.length;
 for (var I=0 ; I<StringLen ; I++)
 {
   myString = myString.replace(' ','');
 }
 return myString;
}

//var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) 
{
 //var keyCode = (isNN) ? e.which : e.keyCode; 
 var keyCode = e.keyCode; 
 //var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
 var filter = [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);
  // for a change only batch ALLC - the next fields are hidden and can not
  //   take a focus.  The Donor button is a type submit
  for (var n = 1; n < 10; n++)
  {
   if (input.form[(getIndex(input)+n) % input.form.length].type == 'text' || input.form[(getIndex(input)+n) % input.form.length].type == 'submit')
   {
    input.form[(getIndex(input)+n) % input.form.length].focus();
    break;
   }
  }
 }
}
//this function returns the Width centered pixle location based on the screen resolution.
function getWindowCenterLeft(WindowWidth)
{
 if (screen.width > WindowWidth)
 {
  return ((screen.width-WindowWidth)/2);
 }
 else
 {
  return 0;
 }
}
  
//this function returns the height centered pixle location based on the screen resolution.
function getWindowCenterTop(WindowHeight)
{
 if (screen.height > WindowHeight)
 {
  return ((screen.height-WindowHeight)/2);
 }
 else
 {
  return 0;
 }

}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}
function switchid(id) {
	hideallids();
	showdiv(id);
}



