/******************************************************************************************
* Name         : utilities.js
* Description : javascript utilities
* Author        : Shane Larkin 
* Date           : 05/06/2004
* Copyright    : Blue Hexagon Ltd 2004
* Modifications
* --------------
* Version    Date    Who
*
* Change
*
******************************************************************************************/ 
// determine browser type
var IE4=document.all?1:0;
var IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
var IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
// Netscape 6 - Mozilla 1.4.1
var NS6=document.getElementById&&!document.all ? true : false;
var NS4=document.layers? true : false;

/////////////////////////////////////////////////////////////////////////////////////////
// This function checks whether the input parameter is a numeric value.
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var numCheck=true;
   var Char;
 
   for (i = 0; i < sText.length; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         numCheck = false;
         }
      }
   return numCheck; 
}
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// This function checks whether the input parameter is a decimal value.
function IsDecimal(sText)
{
   var ValidChars = "0123456789.";
   var numCheck=true;
   var Char;
 
   for (i = 0; i < sText.length; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         numCheck = false;
         }
      }
   return numCheck; 
}
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// This function checks whether the input parameter is a currency value.
function IsMoney(thePrice)
{
   var ValidChars = ",0123456789";
   var monCheck=true;
   var Char;
 
   for (i = 0; i < thePrice.length; i++) 
      { 
      Char = thePrice.charAt(i);
      if (ValidChars.indexOf(Char) == -1) 
         {
         monCheck = false;
         }
      }
   return monCheck; 
}
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// This function checks whether the input parameter is a phone number.
function IsPhone(pNumber)
{
   var ValidChars = "+-()0123456789";
   var phoneCheck=true;
   var Char;
 
   for (i = 0; i < pNumber.length; i++) 
      { 
      Char = pNumber.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         phoneCheck = false;
         }
      }
   return phoneCheck; 
}
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// This function strips out characters.
function stringFilter(input) {
 s = input.value;
 filteredValues = ",";     // Characters to be stripped out
 var i;
 var returnString = "";
 for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
  var c = s.charAt(i);
  if (filteredValues.indexOf(c) == -1) {
   returnString += c;
  }
 }
 input.value = returnString;
}
////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////
// This function validates the date format.
function isDate(dtStr, dtTypeStr){

	 var dtCh1= "-";
 	 var dtCh2= "/";
 	 var minYear=2004;
 	 var maxYear=2069;
 	 var thisDate= new Date();

	var daysInMonth = DaysArray(12);

	// Declare a variable "pos1" to hold the first positional index of the character held in "dtCh1".
	var pos1=dtStr.indexOf(dtCh1);

	if (pos1==-1) // In other words if "dtCh1" was not found in the date string...
	{
	 // Let "pos1" hold the first positional index of the character held in "dtCh2".
	 pos1=dtStr.indexOf(dtCh2);
	}

	// Declare a variable "pos2" to hold the second positional index of the character held in "dtCh1".
	var pos2=dtStr.indexOf(dtCh1,pos1+1);
	if (pos2==-1) // In other words if "dtCh1" was not found in the date string...
	{
	 // Let "pos2" hold the second positional index of the character held in "dtCh2".
	 pos2=dtStr.indexOf(dtCh2,pos1+1);
	}

	// Confirm that the special characters were found at indices 2 and 5...
	if ((pos1 == 2) && (pos2 == 5))
	{
	 var dtCh = dtStr.charAt(pos1); // Determine which special character was used.
	}
	var strDay=dtStr.substring(0,pos1); // Grab the two day digits.
	var strMonth=dtStr.substring(pos1+1,pos2); // Grab the two months digits.
	var strYear=dtStr.substring(pos2+1); // Grab the four year digits.
	strYr=strYear;

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1)
		{
		 strYr=strYr.substring(1);
		}
	}
	month=parseInt(strMonth,10); // Convert the month string to an integer.
	day=parseInt(strDay,10); // Convert the day string to an integer.
	year=parseInt(strYr,10); // Convert the year string to an integer.

   // Check to see whether the allowed separators were found.
   if (pos1==-1 || pos2==-1){
		alert("The "+dtTypeStr+" format should be : dd-mm-yyyy");
		return false;
   }
	
	// Check to see whether the month is valid.
	if (strMonth.length<1 || month<1 || month>12){
		alert("You did not enter a valid month for the "+dtTypeStr);
		return false;
	}

	// Check to see whether the day is valid.
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("You did not enter a valid day for the "+dtTypeStr);
		return false;
	}
	
	// Check to see whether the year is valid.
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("You did not enter a valid 4 digit year between "+minYear+" and "+maxYear+" for the "+dtTypeStr);
		return false;
	}
	
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("You did not enter a valid "+dtTypeStr);
		return false;
	}
	return true;
}
// End of function isDate
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// This function calculates how many days there are in February for the given year.
function daysInFebruary(year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
// End of function daysInFebruary
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// This function calculates the maximum possible days in a particular month.
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11)
		{
		 this[i] = 30;
		}
		if (i==2)
		{
		 this[i] = 29;
		}
   } 
  return this;
}
// End of function DaysArray
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// This functions returns a string which contains only the digits from the date string.
// s = the date string; bag is the allowed separators.
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1)
		{
		 returnString += c;
		}
    }
    return returnString;
}
// End of function stripCharsInBag
//////////////////////////////////////////////////////////////////////////////////
