//
// File        : general.js
// Description : general JAVASCRIPT functions 
//
//
//
//


/*
Omdat de standaard Date.getYear() de jaren tussen 1900 en 1999 teruggeeft als getal tussen 0 en 99
wordt deze hieronder overload.
*/
Date.prototype.oldGetYear = Date.prototype.getYear;
function DateGetYear() { var year = this.oldGetYear();	return ((year>=0) && (year<=99)) ?  year + 1900 : year;}
Date.prototype.getYear = DateGetYear;

/*
Omdat de standaard escape functie de + niet vervangt door %2B worden de escape
en de unescape functies hieronder overloaded. 
*/

var old_escape = escape
var escape = CADAN_BUGFIX_escape
var old_unescape = unescape
var unescape = CADAN_BUGFIX_unescape

function CADAN_BUGFIX_escape(inputtekstje) 
{
  // escape de inputtekst op de oude manier en vervang vervolgens + door %2B
  // na escapen op de oude manier zullen geen +-jes voorkomen als deze correct
  // worden vervangen. 
  return old_escape(inputtekstje).replace(/\+/g, '%2B');
}

function CADAN_BUGFIX_unescape(inputtekstje) 
{
  // vervang %2B door + en unescape vervolgens op de oude manier
  return old_unescape(inputtekstje.replace(/\%2B/g, '+'));
}


/*
' Function		: String2Int
' Description	: Convert a string to an integer.
'               As parseInt also converts octal and hex notation that function should not be used
'               e.g. parseInt('08') = 0 en parseInt('010') = 8  
' Arguments		: integer or NaN when not a number
' Returns		: string
*/
function String2Int(s)
{
   while ((s.length > 1) && (s.charAt(0) == '0'))
     s = s.substring(1);
   return parseInt(s); 
}

/*
' Function		: AddLeadingCharacters
' Description	: Add additional chars to beginning of string until ilength is reached
' Arguments		: in: string, string, integer
' Returns		: string
*/
function AddLeadingCharacters (sOriginalString, sLeadingCharacter, iLength)
{
	var s = sOriginalString
	while (s.length < iLength)
		s = sLeadingCharacter + s
	return s
}


/*
' Function		: Trim
' Description	: Removes spaces from start and end of string
' Arguments		: in: string
' Returns		: string
*/
function Trim (s)
{
	var i = 0;
	while (s.charAt(i)==" ") i++;
	var j = s.length - 1;
	while (s.charAt(j)==" ") j--;
	return (j < i) ? "" : s.substring(i,j+1);
}


/*
' Function		: ReplaceSubStringInString
' Description	: Replace a string in another string
' Arguments		: in: string, substring, replacement
' Returns		: string
' Bug			: ES 040506 kan \n niet vervangen met deze functie
*/
function ReplaceSubStringInString(s, sSubString, sReplaceByString)
{
	var i = s.indexOf(sSubString);
	while (i >= 0)
	{
		s = s.substring(0, i) + sReplaceByString + s.substring(i + sSubString.length, s.length);
	    i = s.indexOf(sSubString, i + sReplaceByString.length);
	}
	return s;
}


/*
' Function		: StringsContainDate
' Description	: Checks for a valid date
' Arguments		: in: strings: year, month, day
' Returns		: true || false
*/
function StringsContainDate(y, m, d)
{
	if (!y) return false;
	if ((!StringContainsInt(y)) || (!StringContainsInt(m)) || (!StringContainsInt(d))) return false;
	y = String2Int(y);
  m = String2Int(m);
  d = String2Int(d);
	var date = new Date(y,m-1,d);
	return ((date.getDate() == d) && (date.getMonth()+1 == m) && (date.getYear(date) == y))
}

/*
' Function		: StringsContainTime
' Description	: Checks for a valid time
' Arguments		: in: strings: hour, minute,second
' Returns		: true || false
*/
function StringsContainTime(h,m,s)
{
	if ((!StringContainsInt(h)) || (!StringContainsInt(m)) || (!StringContainsInt(s))) return false;
  h = String2Int(h);
  m = String2Int(m);
  s = String2Int(s);
  return ((h >= 0) && (h <= 23) && (m >= 0) && (m <= 59) && (s >= 0) && (s <= 59));
}


/*
' Function		: StringContainsInt
' Description	: Checks for a valid integer
' Arguments		: in: string (no implicit cast performed!!! e.q. "3.4" -> false)
' Returns		: true || false
*/
function StringContainsInt(s)
{
	var i = 0;
	while ((i < s.length) && (s.charAt(i) >= "0") && (s.charAt(i) <= "9"))
		i++;
	return (i == s.length) && (i > 0)  // empty string is also not numeric
}


/*
' Function		: GetRadioControlValue
' Description	: Returns value of selected radiocontrol
' Arguments		: in: radiocontrol (object!)
' Returns		: selected value (string) || null (non selected)
*/
function GetRadioControlValue(radRadioControl)
{
	if (radRadioControl.length == null)
  {
  	if (radRadioControl.checked == true)
			return radRadioControl.value;
    else
    	return null;
  }
  else
  {
		for (var i=0; i < radRadioControl.length; i++)
			if (radRadioControl[i].checked == true)
				return radRadioControl[i].value;
		return null;
  }
}

/*
' Function		: GetRadioControlSelectedIndex
' Description	: Returns index of selected radiocontrol
' Arguments		: in: radiocontrol (object!)
' Returns		: selected index (int) first=0 || null (non selected)
*/
function GetRadioControlSelectedIndex(radRadioControl)
{
	if (radRadioControl.length == null)
  {
  	if (radRadioControl.checked == true)
			return 0;
    else
    	return null;
  }
  else
  {
		for (var i=0; i < radRadioControl.length; i++)
			if (radRadioControl[i].checked == true)
				return i;
		return null;
  }
}

/*
' Function		: GetCheckboxControlValues
' Description	: Returns value of selected radiocontrol
' Arguments		: in: radiocontrol (object!)
' Returns		: selected value (string) || null (non selected)
*/
function GetCheckboxControlValues(radRadioControl)
{
	if (radRadioControl.length == null)
  {
  	if (radRadioControl.checked == true)
			return radRadioControl.value;
    else
    	return '';
  }
  else
  {
		var sValues = '';
		for (var i=0; i < radRadioControl.length; i++)
    {
			if (radRadioControl[i].checked == true)
      {
      	if (sValues > '')
        	sValues = sValues + ','; 
       	sValues = sValues + radRadioControl[i].value;
      }  
    }    
		return sValues;
  }
}


/*
' Function		: GetFirstSelectedOptionIndex
' Description	: Returns index of FIRST selected selectionlist
' Arguments		: in: optionlist (object!)
' Returns		: FIRST selected value (index)
*/
function GetFirstSelectedOptionIndex(lstOptionList)
{
	for (var i=0; i < lstOptionList.length; i++)
		if (lstOptionList.options[i].selected)
			return i
	return -1;
}

/*
' Function		: GetFirstSelectedOptionValue
' Description	: Returns value of FIRST selected selectionlist
' Arguments		: in: optionlist (object!)
' Returns		: FIRST selected value (string)
*/
function GetFirstSelectedOptionValue(lstOptionList)
{
	for (var i=0; i < lstOptionList.length; i++)
		if (lstOptionList.options[i].selected)
			return lstOptionList.options[i].value
	return null;
}

/*
' Function		: GetFirstSelectedOptionText
' Description	: Returns text of FIRST selected selectionlist
' Arguments		: in: optionlist (object!)
' Returns		: FIRST selected value (string)
*/
function GetFirstSelectedOptionText(lstOptionList)
{
	for (var i=0; i < lstOptionList.length; i++)
		if (lstOptionList.options[i].selected)
			return lstOptionList.options[i].text
	return -1;
}
/*
' Function		: AddOptionToOptionList
' Description	: Add an option to an optionlist
' Arguments		: in: optionlist (object!)
'				  in: sDescription - description of the new element
'				  in: sValue - value of the new element
'				  in: bSelected - optional - should the option been selected
' Remark		: the new element is added at the end of the list
*/
function AddOptionToOptionList(lstOptionList, sDescription, sValue, bSelected)
{
	var optOption = new Option(sDescription, sValue, false, bSelected);
	lstOptionList.options[lstOptionList.options.length] = optOption;
}


/*
' Function		: Date2String
' Description	: Returns 'dutch' notation of date e.q. dd-MM-yyyy
' Arguments		: in: date
' Returns		: string with date: 11-07-1966
*/
function Date2String(tDate)
{
	return  AddLeadingCharacters (tDate.getDate().toString(), '0', 2) + "-" +
			AddLeadingCharacters ((tDate.getMonth() + 1).toString(), '0', 2) + "-" +
			tDate.getYear().toString();
}

/*
' Function		: Time2String
' Description	: Returns time in HH:MM, and adds leading zeros if necessary
' Arguments		: in: date
' Returns		: string with time: 09:05
' Remark    : added by rja on 050330
*/
function Time2String(tDate)
{
	return  AddLeadingCharacters (tDate.getHours().toString(), '0', 2) + ":" +
			AddLeadingCharacters (tDate.getMinutes().toString(), '0', 2);
}

/*
' Function		: FirstCharacterUppercase
' Description	: returns the input string only the first character of it will be converted to uppercase
' Arguments		:
' Returns		:
*/
function FirstCharacterUppercase(s)
{
	if (typeof(s) == "string")
		if (s != "")
			s = (s.charAt(0)).toUpperCase() + s.substring(1);
	return s;
}


//
// return the text that has been selected in one of the text-fields (MSIE or Netscape)
//
function getSelectedText()
{ 
  return (document.all) ? document.selection.createRange().text : document.getSelection();
}

