function LTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1) {
	var j=0, i = s.length;

	while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
	j++;

	s = s.substring(j, i);
	}
	return s;
}

function RTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
	var i = s.length - 1;       // Get length of string

	while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
	i--;

	s = s.substring(0, i+1);
	}
	return s;
}

function Trim(str)
{
	return RTrim(LTrim(str));
}
  
function validateEmail(str)
{
	var emailExp = new RegExp("[A-Za-z0-9\-\_\+\.][A-Za-z0-9]+@[A-Za-z0-9\-\_][A-Za-z0-9]+\.[A-Za-z][A-Za-z\-\.]*$");
	if ( (Trim(str) == "") || (!emailExp.test(str)) || (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Trim(str)) ) )  
	{  return false; }        
	else
	{ return true; }
}  

function validateNRIC(str)
{
    var bValid = true;
	
    if (Trim(str) != "")
    {
	    // these variables used for NRIC checks
	    weight = new Array("2","7","6","5","4","3","2");
	    u_reftb = new Array("A","B","C","D","E","F","G","H","I","Z","J");
	    f_reftb = new Array("K","L","M","N","P","Q","R","T","U","W","X");
	    var i=0, prod=0, sum=0, rem = 0 ;
	    var pfx, chkdgt;

        if (str.length!=9)
        { bValid = false; }
        
        if (bValid)
        {
	        pfx    = Trim(str).charAt(0).toUpperCase();
	        chkdgt = Trim(str).charAt(8).toUpperCase();

	        if (pfx != 'I' && pfx != 'S' && pfx != 'T' &&
	        pfx != 'F' && pfx != 'G')
	        { bValid = false; }
	        for(i=0; i<7; i++)
	        {
		        prod = weight[i] * Trim(str).charAt(i+1);
		        sum +=prod;
	        }
	        if(pfx == 'T' || pfx == 'G')
	        {
		        sum +=4;
	        }
	        rem = sum % 11;
	        if (pfx == 'I' || pfx == 'S' || pfx == 'T')
	        {
		        if (chkdgt == u_reftb[(11-rem) -1])
		        { bValid = true; }
		        else
		        { bValid = false; }
	        }
	        else
	        {
		        if (chkdgt == f_reftb[(11-rem) -1])
		        { bValid = true; }
		        else
		        { bValid = false;	}
	        }
	   }
    }
    return bValid
}

function isAtLeastOneChecked(form, type, containName) {
    var atLeastOneChecked = false;

    for (var i = 0; i < form.length; i++) {
        if (form.elements[i].type == type && form.elements[i].id.indexOf(containName) != -1) {
            if (form.elements[i].checked == true) {
                atLeastOneChecked = true;
                break;
            }
        }
    }

    return atLeastOneChecked;
}

function jsHTMLEncode (str)
{
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
}

function converttodate(datefield, separator)
{
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;

	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";

	strDate = datefield

	strDateArray = strDate.split(separator);

	strDay = strDateArray[0];
	strMonth = strDateArray[1];
	strYear = strDateArray[2];

	intday = parseInt(strDay, 10);
	intMonth = parseInt(strMonth, 10);
	intYear = parseInt(strYear, 10);

	datefield = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	return datefield;
}


function fPopUpWindow(url, hWind, nWidth, nHeight, nScroll, nResize) {
    var popupwin, cToolBar;
    cToolBar = 'toolbar=0,location=0,directories=0,status=' + nResize + ',menubar=0,scrollbars=' + nScroll + ',resizable=' + nResize + ',width=' + nWidth + ',height=' + nHeight;
    popupwin = window.open(url, hWind, cToolBar);
    return popupwin;
}
function fCloseWindow(hWind) {
    //try { if (hWind) { window.open("javascript:close()",hWind) } } catch(errC) {}	
    var closewin;
    try { if (hWind) { closewin = window.open("", hWind); closewin.close(); } } catch (errC) { }

    return true;
}

// Horizontal Menu
var menu = function() {
    var t = 15, z = 50, s = 6, a;
    function dd(n) { this.n = n; this.h = []; this.c = [] }
    dd.prototype.init = function(p, c) {
        a = c; var w = document.getElementById(p), s = w.getElementsByTagName('ul'), l = s.length, i = 0;
        for (i; i < l; i++) {
            var h = s[i].parentNode; this.h[i] = h; this.c[i] = s[i];
            h.onmouseover = new Function(this.n + '.st(' + i + ',true)');
            h.onmouseout = new Function(this.n + '.st(' + i + ')');
        }
    }
    dd.prototype.st = function(x, f) {
        var c = this.c[x], h = this.h[x], p = h.getElementsByTagName('a')[0];
        clearInterval(c.t); c.style.overflow = 'hidden';
        if (f) {
            p.className += ' ' + a;
            if (!c.mh) { c.style.display = 'block'; c.style.height = ''; c.mh = c.offsetHeight; c.style.height = 0 }
            if (c.mh == c.offsetHeight) { c.style.overflow = 'visible' }
            else { c.style.zIndex = z; z++; c.t = setInterval(function() { sl(c, 1) }, t) }
        } else { p.className = p.className.replace(a, ''); c.t = setInterval(function() { sl(c, -1) }, t) }
    }
    function sl(c, f) {
        var h = c.offsetHeight;
        if ((h <= 0 && f != 1) || (h >= c.mh && f == 1)) {
            if (f == 1) { c.style.filter = ''; c.style.opacity = 1; c.style.overflow = 'visible' }
            clearInterval(c.t); return
        }
        var d = (f == 1) ? Math.ceil((c.mh - h) / s) : Math.ceil(h / s), o = h / c.mh;
        c.style.opacity = o; c.style.filter = 'alpha(opacity=' + (o * 100) + ')';
        c.style.height = h + (d * f) + 'px'
    }
    return { dd: dd }
} ();
