//*****************************************************
//* Function name: is_valid_date                      *
//* Function desc: returns true if valid date,        *
//*                false otherwise                    *
//*****************************************************
function is_valid_date(day, month, year)
{
	// check for valid year
	if (year < 1900) {
		return false;
	}

	// check for valid month
	if (month < 1 || month > 12) { // check month range
		return false;
	}

	// check for valid days
	if (day < 1 || day > 31) {
		return false;
	}


	// check for valid days for given month
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
		return false;
	}

	// check for leap year
	if (month == 2) {
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day == 29 && !isleap)) {
			return false;
	  	}
	}

	return true;
}


//*****************************************************
//* Function name: relative_time                      *
//* Function desc: returns number of seconds from     *
//*                current date and time to specified *
//*                date                               *
//*****************************************************
function relative_time(day, month, year)
{
	var date = new Date(year, month-1, day, 0, 0, 0);
	var nowTime = new Date();
        var nowDate = new Date(nowTime.getYear(), nowTime.getMonth(), nowTime.getDate(), 0, 0, 0);
	return (date.getTime() - nowDate.getTime());
}
//*****************************************************
//* Function name: trim_lowercase_text_value          *
//* Function desc: trims all leading and trailing     *
//*                spaces from text value and convert *
//*                to lowercase.                      *
//*****************************************************
function trim_lowercase_text_value(text_component)
{
	text_component.value = trim(text_component.value).toLowerCase();
}

//*****************************************************
//* Function name: trim_uppercase_text_value          *
//* Function desc: trims all leading and trailing     *
//*                spaces from text value and convert *
//*                to uppercase.                      *
//*****************************************************
function trim_uppercase_text_value(text_component)
{
	text_component.value = trim(text_component.value).toUpperCase();
}

//*****************************************************
//* Function name: trim_titlecase_text_value          *
//* Function desc: trims all leading and trailing     *
//*                spaces from text value and convert *
//*                to titlecase.                      *
//*****************************************************
function trim_titlecase_text_value(text_component)
{
	text_component.value = toTitleCase(trim(text_component.value));
}

//*****************************************************
//* Function name: trim_text_value                    *
//* Function desc: trims all leading and trailing     *
//*                spaces from text value.            *
//*****************************************************
function trim_text_value(text_component)
{
	text_component.value = trim(text_component.value);
}


//*****************************************************
//* Function name: ltrim                              *
//* Function desc: trims all leading spaces from      *
//*                string.                            *
//*****************************************************
function ltrim(string)
{
	while(string.charAt(0)==' ')
	{
		string = string.substring(1, string.length);
	}
	return string;
}

//*****************************************************
//* Function name: rtrim                              *
//* Function desc: trims all trailing spaces from     *
//*                string.                            *
//*****************************************************
function rtrim(string)
{
	while(string.charAt(string.length - 1)==' ')
	{
		string = string.substring(0, string.length - 1);
	}
	return string;
}


//*****************************************************
//* Function name: trim                               *
//* Function desc: trims all leading and trailing     *
//*                spaces from string.                *
//*****************************************************
function trim(string)
{
	return rtrim(ltrim(string));
}


//*****************************************************
//* Function name: textIsNumeric                      *
//* Function desc: returns true if text value is      *
//*                numeric.                           *
//*                Otherwise, an error message alert  *
//*                is displayed, focus is set on text *
//*                component, and false is returned.  *
//*****************************************************
function textIsNumeric(text_component, error_msg)
{
	if (!isNumeric(get_text_value(text_component)))
	{
		alert(error_msg);
		text_component.focus();
		return false;
	}
	return true;
}


//*****************************************************
//* Function name: textIsAlphabetic                   *
//* Function desc: returns true if text value is      *
//*                alphabetic.                        *
//*                Otherwise, an error message alert  *
//*                is displayed, focus is set on text *
//*                component, and false is returned.  *
//*****************************************************
function textIsAlphabetic(text_component, error_msg)
{
	if (!isAlphabetic(get_text_value(text_component)))
	{
		alert(error_msg);
		text_component.focus();
		return false;
	}
	return true;
}


//*****************************************************
//* Function name: textIsAlphaNumeric                 *
//* Function desc: returns true if text value is      *
//*                alphanumeric.                      *
//*                Otherwise, an error message alert  *
//*                is displayed, focus is set on text *
//*                component, and false is returned.  *
//*****************************************************
function textIsAlphaNumeric(text_component, error_msg)
{
	if (!isAlphaNumeric(get_text_value(text_component)) || 
	     get_text_value(text_component).indexOf(' ') >= 0)
	{
		alert(error_msg);
		text_component.focus();
		return false;
	}
	return true;
}


//*****************************************************
//* Function name: textIsValidText                    *
//* Function desc: returns true if text value is      *
//*                valid text.                        *
//*                Otherwise, an error message alert  *
//*                is displayed, focus is set on text *
//*                component, and false is returned.  *
//*****************************************************
function textIsValidText(text_component, error_msg)
{
	if (!isValidText(get_text_value(text_component)))
	{
		alert(error_msg);
		text_component.focus();
		return false;
	}
	return true;
}

function textIsValidCode(text_component, error_msg)
{
	if (!isValidCode(get_text_value(text_component)))
	{
		alert(error_msg);
		text_component.focus();
		return false;
	}
	return true;
}



//*****************************************************
//* Function name: textIsCurrency                     *
//* Function desc: returns true if text value is a    *
//*                valid currency value.              *
//*                Otherwise, an error message alert  *
//*                is displayed, focus is set on text *
//*                component, and false is returned.  *
//*****************************************************
function textIsCurrency(text_component, error_msg)
{
	if (IsitFloat(get_text_value(text_component))) {
	     if (get_text_value(text_component) < 0)
	     {
		  alert(error_msg);
	        text_component.focus();
		  return false;
	     }
  	     return true;
	}
      else {
         alert(error_msg);
	   text_component.focus();
	   return false;
	}
}



//*****************************************************
//* Function name: isDecimal                          *
//* Function desc: returns true if valid decimal,     *
//*                false otherwise                    *
//*****************************************************
function isDecimal(str)
{
	var floatValue = parseFloat(str);

	if (isNaN(floatValue))
	 {
//		alert("nanumber");
  		return false;
	 }
      else {
//      alert("is number");
	    return true;
	}
}

function IsitFloat(num)
{
var parsednum;
var pat;
var res = new Array();
pat = /(\.)/g;
res = num.match(pat);
if(res!=null)
    if(res.length>1)
        return false;

pat=/(\D)/g;
res=num.match(pat);
if(res!=null)
    if(res.length>2)
         return false;
    else
         if(res.length==1)
	  if(res[0]=='-' || res[0]=='.')
	        return true;
	  else
	        return false;
          else //res.length is 2
  	   if(res[0]=='-' && res[1]=='.')
	   {
	       parsednum=parseFloat(num);
	       if(parsednum==num)
	           return true;
	       else
 	           return false;
                }	
	    else
	       return false;
else
    return true;
}
//*****************************************************
//* Function name: isNumeric                          *
//* Function desc: returns true if valid integer,     *
//*                false otherwise                    *
//*****************************************************
function isNumeric(str)
{
	var ch;
	var counter=0;
	for(var i=0; i < str.length; i++){
		ch = str.charAt(i);
		if (!char_isDigit(ch)) {
			return false;
		}
	}
	return true;
}


//*****************************************************
//* Function name: isAlphabetic                       *
//* Function desc: returns true if alphabetic,        *
//*                false otherwise                    *
//*****************************************************
function isAlphabetic(str){
	var ch;
	var counter=0;
	for(var i=0; i < str.length; i++){
		ch = str.charAt(i);
		if (!char_isAlphabet(ch)) {
			return false;
		}
	}
	return true;
}


//*****************************************************
//* Function name: isAlphaNumeric                     *
//* Function desc: returns true if alphanumeric,      *
//*                false otherwise                    *
//*****************************************************
function isAlphaNumeric(str)
{
	var ch;
	var counter=0;
	for(var i=0; i < str.length; i++){
		ch = str.charAt(i);
		if (!(char_isAlphabet(ch) || char_isDigit(ch))) {
			return false;
		}
	}
	return true;
}

//*****************************************************
//* Function name: isBothAlphaNumeric                     *
//* Function desc: returns true if alphanumeric,      *
//*                false otherwise                    *
//*****************************************************
function isBothAlphaNumeric(str)
{
	var ch;
	var hasAlpha = false;
	var hasNumeric = false;
	var counter=0;
	for(var i=0; i < str.length; i++){
		ch = str.charAt(i);
		if (char_isAlphabet(ch)) {
		  hasAlpha = true;
		}
		else {
		  if (char_isDigit(ch)) {
		    hasNumeric = true;
		  }
		  else {
			return false;
		  }
		}
	}
	if (hasNumeric && hasAlpha) {
  	  return true;
  	}
  	else {
  	  return false;
  	}
}

//*****************************************************
//* Function name: isValidText                        *
//* Function desc: returns true if valid text,        *
//*                false otherwise                    *
//*****************************************************
function isValidText(str)
{
	var ch;
	var counter=0;
        var validSymbols="`~!@#$%^&*()-+_=|/\\[]{}:;.,?<>'"

	for(var i=0; i < str.length; i++){
		ch = str.charAt(i);
		if (!(char_isAlphabet(ch) || char_isDigit(ch) ||
                     (validSymbols.indexOf(ch) >= 0) || 
                     (escape(ch) == "%0A") || (escape(ch) == "%0D") )) {
			return false;
		}
	}
	return true;
}


//*****************************************************
//* Function name: isValidCode                        *
//* Function desc: returns true if valid text,        *
//*                false otherwise                    *
//*****************************************************
function isValidCode(str)
{
	var ch;
	var counter=0;
        var validSymbols="`~!@#$%^&*()-+_=|/\\[]{}:;.,?<>'"

	for(var i=0; i < str.length; i++){
		ch = str.charAt(i);
		if (!(char_isAlphabet(ch) ||
                     (validSymbols.indexOf(ch) >= 0) || 
                     (escape(ch) == "%0A") || (escape(ch) == "%0D") )) {
			return false;
		}
	}
	return true;
}


//*****************************************************
//* Function name: containsSpace                      *
//* Function desc: returns true if contains space(s), *
//*                false otherwise                    *
//*****************************************************
function containsSpace(str)
{
	var ch;
	var counter=0;
	for(var i=0; i < str.length; i++){
		ch = str.charAt(i);
		if (ch == ' ') {
			return true;
		}
	}
	return false;
}


//*****************************************************
//* Function name: char_isAlphabet                    *
//* Function desc: returns true if char is alphabet,  *
//*                false otherwise                    *
//*****************************************************
function char_isAlphabet(ch)
{
	if ( ((ch >= 'a') && (ch <= 'z')) ||
	     ((ch >= 'A') && (ch <= 'Z')) ||
	      (ch == " ") ){
		return true;
	} else {
		return false;
	}
}


//*****************************************************
//* Function name: char_isDigit                       *
//* Function desc: returns true if char is digit,     *
//*                false otherwise                    *
//*****************************************************
function char_isDigit(ch)
{
	if ((ch >= '0') && (ch <= '9')) {
		return true;
	} else {
		return false;
	}
}

//*****************************************************
//* Function name: toTitleCase                        *
//* Function desc: returns string in title case       *
//*****************************************************
function toTitleCase(s) {
	var ch;
	var sout = "";
	var ucasenext = 1;

	for (var i = 0; i < s.length; i++) {
		// walk through the string and capitalise first letter of sentences
		ch = s.charAt(i);
		if (ch == ".") {
			ucasenext = 1;
		} else {
			if (ucasenext) {
				if (char_isDigit(ch)) {
					ucasenext = 0;
				} else if (char_isAlphabet(ch) && (ch != " ")) {
					ch = ch.toUpperCase();
					ucasenext = 0;
				}
			}
		}
		
		sout += ch;
	}

	return sout;
}

//*****************************************************
//* Function name: isEmail                            *
//* Function desc: returns true is email is valid     *
//*****************************************************
function isEmail (s)
{   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
//*****************************************************
//* Function name: isEmpty                            *
//* Function desc: returns true if value is empty,    *
//*                false otherwise                    *
//*****************************************************
function isEmpty(value)
{
	if ((value == "") || (value == null)) {
		return true;
	} else {
		return false;
	}
}

//*****************************************************
//* Function name: isWhitespace                       *
//* Function desc: returns true if s is empty,        *
//*                or whitespace characters only      *
//*****************************************************
function isWhitespace (s)
{   
	var i;
	var whitespace = " \t\n\r";
	
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


//*****************************************************
//* Function name: isChecked                          *
//* Function desc: returns true if checkbox is        *
//*                checked, false otherwise           *
//*****************************************************
function isChecked(checkbox_component)
{
	return checkbox_component.checked;
}

//*****************************************************
//* Function name: get_select_value                   *
//* Function desc: returns option value of specified  *
//*                select component                   *
//*****************************************************
function get_select_value(select_component)
{
	if (select_component.selectedIndex < 0) {
		return "";
	} else {
		return select_component.options[select_component.selectedIndex].value;
	}
}


//*****************************************************
//* Function name: get_radiogroup_value               *
//* Function desc: returns selected value of specified*
//*                radio button component             *
//*****************************************************
function get_radiogroup_value(radiogroup_component)
{
	for (i=0; i<radiogroup_component.length; i++)
	{
		if (radiogroup_component[i].checked)
		{
			return radiogroup_component[i].value;
		}
	}
	return "";
}


//*****************************************************
//* Function name: get_text_value                     *
//* Function desc: returns value of specified text    *
//*                component                          *
//*****************************************************
function get_text_value(text_component)
{
	return text_component.value;
}

//*****************************************************
//* Function name: exit                               *
//* Function desc: prompts user for confirmation      *
//*                before exiting to specified url    *
//*****************************************************
function exit(url)
{
	var replyOK = confirm("Ok to cancel?");
	if (replyOK)
	{
		open(url, "_self");
	}
}

//*****************************************************
//* Function name: new_window                         *
//* Function desc: creates a new window for           *
//*                specified url                      *
//*****************************************************
function new_window(url, type)
{
	open(url, type);
}

//*****************************************************
//* Function name: exit_without_prompt                *
//* Function desc: exits to specified url without     *
//*                prompting user for confirmation    *
//*****************************************************
function exit_without_prompt(url)
{
	open(url, "_self");
}

//******************************************************
//* Function name: isNRIC			       *
//* Function desc: Check Singapore NRIC / FIN format   *
//******************************************************
function isNRIC(tmpNRIC) {
  var tmpSum = 0;
  var tmpRem = 0;
  tmpNRIC = trim(tmpNRIC.toUpperCase());
  var digitArr = new Array (2, 7, 6, 5, 4, 3, 2);
  var lastCodeArr = new Array ("", "A", "B", "C", "D", "E", "F", "G", "H", "I", "Z", "J");
  var lastCodeFINArr = new Array ("", "K", "L", "M", "N", "P", "Q", "R", "T", "U", "W", "X");
  if (tmpNRIC.length != 9) {
    return false;
    //return true;
  }
  if ((tmpNRIC.charAt(0) == "T") || (tmpNRIC.charAt(0) == "G")) {
    tmpSum = 4;
  }
  else {
    if ((tmpNRIC.charAt(0) != "S") || (tmpNRIC.charAt(0) == "F")) {
    return false;
    //return true;
    }
  }
  for (var i = 1; i < 8; i++) {
    if ((tmpNRIC.charAt(i) >= '0') && (tmpNRIC.charAt(i) <= '9')) {
      tmpSum = tmpSum + (tmpNRIC.charAt(i) * digitArr[i-1]);
    }
    else {
    return false;
    //return true;
    }
  }
  tmpRem = 11 - (tmpSum % 11);
//  if ((tmpNRIC.charAt(i) == "S")  || (tmpNRIC.charAt(i) == "T")) {
  if ((tmpNRIC.charAt(0) == "S")  || (tmpNRIC.charAt(0) == "T")) {
    if (lastCodeArr[tmpRem] != tmpNRIC.charAt(8)) {
    return false;
    //return true;
    }
    else {
      return true;
    }
  }
  else {
    if (lastCodeFINArr[tmpRem] != tmpNRIC.charAt(8)) {
    return false;
    //return true;
    }
    else {
      return true;
    }
  }
}

//*****************************************************
//* Function name:                                    *
//* Function desc: expand/collapse div                *
//*****************************************************
function showhide(i) {
    var obj = document.getElementById('id_' + i);
    if (obj){
        if (obj.style.display == 'none')
            obj.style.display = 'block';
        else
            obj.style.display = 'none';
    }
}
function imgPlusMinus(i) {
    var objP = document.getElementById('imgPlus_' + i);
    var objM = document.getElementById('imgMinus_' + i);
    if (objP){
        if (objP.style.display == 'block')
            objP.style.display = 'none';
        else
            objP.style.display = 'block';
    }
    if (objM){
        if (objM.style.display == 'none')
            objM.style.display = 'block';
        else
            objM.style.display = 'none';
    }
}