// Check whether the String is null or empty
function isEmpty(s) {
	s = s.replace(/^[ \t\r\n]*/g, "");
	s = s.replace(/[ \t\r\n]*$/g, "");
	return ( (s == null) || (s.length == 0) );
}

function StrTrim(value)
{
	var str = value;

	str = str.replace(/\s+$/g, "");
	str = str.replace(/^\s+/g, "");

	return str;
}

function rtrim(element)
{
	var instr = element.value;

	instr = instr.replace(/[ \t\n\r]*$/g, "");

	return instr;
}

function ltrim(element)
{
	var instr = element.value;

	instr = instr.replace(/^[ \t\n\r]*/g, "");

	return instr;
}

function trim(element)
{
	var instr = element.value;

	instr = instr.replace(/^[ \t\n\r]*/g, "");
	instr = instr.replace(/[ \t\n\r]*$/g, "");

	return instr;
}




// Check whether the input year is Leap Year
function isLeapYear(x) {
    if ( x/400 == parseInt(x/400) )
        return (true);
    else if ( x/100 == parseInt(x/100) )
        return (false);
    else if ( x/4 == parseInt(x/4) )
        return (true);
    else
        return (false);
}

// Check whether the input date is in valid.
// Note that we will NOT check whether the month and day is number
// since month and day are used select option.
// There should not be such error.
//
// There is some special for this date checking method.
// It is used to handle the case that
// some people forget the day and month of born.
// They can only remember the year of born.
// In this case, strictCheck should be set to "false".
// Since "loosely" checking is applied.
// Otherwise, strictCheck should be set to "true".

function isValidDate ( strYear, strMonth, strDay, strictCheck ) {
    var isValid = false;

    if ( !isNaN(strYear) ) {
        var year = parseInt(strYear, 10);
        var month = parseInt(strMonth, 10);
        var day = parseInt(strDay, 10);

        // Assume year would not be small than 1800
        // It can lower the chance of human error
        if ( year >= 1800 ) {

            if ( month >= 1 && month <= 12 ) {  // "Normal" Valid Month
                if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) {   // "Big" Month
                    if ( day >= 1 && day <= 31 ) {

                        isValid = true;
                    }
                } else if ( month == 2 ) {  // Feburary
                    if ( isLeapYear(year) ) {   // Lead Year
                        if ( day >= 1 && day <= 29 ) {
                          isValid = true;
                        }
                    } else {    // NOT a Leap Year
                        if ( day >= 1 && day <= 28 ) {
                           isValid = true;
                        }
                    }
                } else {    // "Small" Month
                    if ( day >= 1 && day <= 30 ) {
                        isValid = true;
                    }
                }
            } else if ( month == 0 && !strictCheck ) {
                if ( day == 0 ) {
                    isValid = true;
                }
            }
        }
    }
    return isValid;
}


//leading zero for a target string within a specific length(numSpace)
function leadZero(target, numSpace) {

  var lead = "";
  var targetlength = target.length;
  for( i=targetlength; i < numSpace; i++) {
    lead += "0";

  }

  return lead + target;
}



// ****************************************************************
// Check whether Date is Before or After Another Date
// ****************************************************************

function before(date1, date2, isStrictBefore) {
    if ( isStrictBefore ) {
        if ( date1.getTime() < date2.getTime() )
            return true;
        else
            return false;
    } else {
        if ( date1.getTime() <= date2.getTime() )
            return true;
        else
            return false;
    }
}

function after(date1, date2, isStrictAfter) {
    if ( isStrictAfter ) {
        if ( date1.getTime() > date2.getTime() )
            return true;
        else
            return false;
    } else {
        if ( date1.getTime() >= date2.getTime() )
            return true;
        else
            return false;
    }
}

function getSelect(theSelect) {
	for (var i=0 ; i < theSelect.length ; i++) {
		if (theSelect.options[i].selected == true) {
//			return theSelect.options[i].text;
			return theSelect.options[i].value;
		}
	}
	return ""
}


function printErrorMessage(field, message) {
	    document.getElementById(field).innerHTML = message;
}

function hideErrorMessage(field) {
	    document.getElementById(field).innerHTML = '';
}

function changeFontColor(a, colorNum) {
	//colorNum = "#ff6600"
	a.color=colorNum;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function disableControl() {
	var sItems = document.getElementsByTagName('input');
	for (i=0; i< sItems.length; i++) {
		if (sItems[i].type != 'button' && sItems[i].type != 'hidden') {
			if (sItems[i].type == 'text')
				sItems[i].readOnly = true;
			else
				sItems[i].disabled = true;
		}
	}

	sItems = document.getElementsByTagName('select');
	for (i=0; i < sItems.length; i++) {
		sItems[i].disabled = true;
	}
}

function hideDocument() {

	disableControl();

	var pageWidth,pageHeight;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) { // all but Explorer Mac
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}
	else { // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
		pageWidth = document.body.offsetWidth;
		pageHeight = document.body.offsetHeight;
	}

	var loadingDiv = document.getElementById('loadingDiv');
	loadingDiv.style.width = '100%';
	loadingDiv.style.height = pageHeight;
	loadingDiv.style.display = "inline";
}

var hasFormSubmitted = false;

function submitForm(formName, action) {
	if ( !hasFormSubmitted ) {
		document.forms[formName].actionFwd.value=action;
		document.forms[formName].submit();
		hasFormSubmitted = true;

		setTimeout("hideDocument()", 500);

		var anchors = document.getElementsByTagName("a");
		for (i=0; i< anchors.length; i++) {
			if ( anchors[i].href.indexOf("javascript") != -1 ) {
				anchors[i].href = '#';
			}
		}
	}

}

function doBlink() {
  // Blink, Blink, Blink...
  var blink = document.getElementsByTagName("BLINK")
  for (var i=0; i < blink.length; i++)
    blink[i].style.visibility = blink[i].style.visibility == "" ? "hidden" : ""
}

function startBlink() {
  // Make sure it is IE4
  if (document.all)
    setInterval("doBlink()",1000)
}

function fixLink(linkToFix) {
	var link1 = linkToFix;
	var link2 = '';
	var idx = link1.indexOf('&amp;');
	while (idx != -1 ) {
		link2 = link1.substr(0, idx+1) + link1.substr(idx+5);
		link1 = link2;
		idx = link1.indexOf('&amp;');
	}
	return link1;
}

function setLoginJustCompleted() {
	if (typeof document.forms[mainForm].loginJustCompleted != 'undefined')
		document.forms[mainForm].loginJustCompleted.value='true';
}

function doSwitchLang(reqLang, currAction) {
	document.forms[mainForm].lang.value=reqLang;
	document.forms[mainForm].actionType.value=5;
	if ( currAction != '' ) {
		document.forms[mainForm].actionFwd.value=currAction;
	}
	document.forms[mainForm].submit();
}

function showVeil() {
	var pageWidth,pageHeight;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) { // all but Explorer Mac
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}
	else { // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
		pageWidth = document.body.offsetWidth;
		pageHeight = document.body.offsetHeight;
	}

	var loadingDiv = document.getElementById('loadingDiv');
	loadingDiv.style.width = '100%';
	loadingDiv.style.height = pageHeight;
	loadingDiv.style.display = "inline";
}

function hideVeil() {
	var loadingDiv = document.getElementById('loadingDiv');
	loadingDiv.style.display = "none";
}
