/*
Standard JavaScript Library
for MIA Web Portal
Modified by : STWONG
Modified date : 03/11/2004
*/

//-----------------------------------------------------------------------------------------------
/* A routine to set cookies information	*/
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var thisCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = thisCookie;
}
//-----------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------
/* A routine to get entire Cookies information */ 
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
//-----------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------
/* A simple routine to delete Cookies information */
// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
//----------------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------------
// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
//----------------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------------
/* A simple date validation routine	*/
// take the date in the form of d/m/y
function validateDate( sDate ){
	
	if ( sDate == '' ) return false;
	if ( sDate.length > 10  ) return false;
	
	var i, j;
	var dy, mo, yr;

	// get dy
	i = sDate.indexOf('/');
	j = sDate.lastIndexOf('/');
	
	if (i == 0) return false;
	if (j == 0) return false;

	mo = sDate.slice(i+1,j);
	dy = sDate.slice(0,i);	
	yr = sDate.slice(j+1, sDate.length);


	if (isNaN(mo)) return false;
	if (isNaN(dy)) return false;
	if (isNaN(yr)) return false;
	
	if (dy.charAt(0) == '0') { dy = dy.charAt(1) };
	if (mo.charAt(0) == '0') { mo = mo.charAt(1) };
	
	// verify if this is an acceptable date
	dy = parseInt(dy);
	mo = parseInt(mo);

	if ( isNaN(dy) ) return false;
	if ( isNaN(mo) ) return false;
	if ( isNaN(parseInt(yr)) || yr.length != 4) return false;

	switch (mo) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (!(dy >= 1 && dy <= 31 ) ) return false
			break;
		case 2:
			if ( yr % 4 == 0 ) {
				// leap year
				if (!(dy >= 1 && dy <= 29 ) ) return false
				break;
			} else {
				// non leap year
				if (!(dy >= 1 && dy <= 28 ) ) return false
				break;
			}			
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if (!(dy >= 1 && dy <= 30 ) ) return false
			break;
		default:
			return false;
	};
	return true;
};

// this is a backup copy
// A simple date validation routine
// take the date in the form of m/d/y
function validateDate2( sDate ){
	
	var i, j;
	var dy, mo, yr;

	// get dy
	i = sDate.indexOf('/');
	j = sDate.lastIndexOf('/');
	
	if (i == 0) return false;
	if (j == 0) return false;

	dy = sDate.slice(i+1,j);
	mo = sDate.slice(0,i);	
	yr = sDate.slice(j+1, sDate.length);
	
	// verify if this is an acceptable date
	dy = parseInt(dy);
	mo = parseInt(mo);

	if ( isNaN(dy) ) return false;
	if ( isNaN(mo) ) return false;
	if ( isNaN(parseInt(yr)) || yr.length != 4) return false;
	
	switch (mo) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (!(dy >= 1 && dy <= 31 ) ) return false
			break;
		case 2:
			if ( yr % 4 == 0 ) {
				// leap year
				if (!(dy >= 1 && dy <= 29 ) ) return false
				break;
			} else {
				// non leap year
				if (!(dy >= 1 && dy <= 28 ) ) return false
				break;
			}			
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if (!(dy >= 1 && dy <= 30 ) ) return false
			break;
		default:
			return false;
	};
	return true;
};
//----------------------------------------------------------------------------------------------


//-------------------------------------------------------------
/* Some routines for trimming the input */
function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
};

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
};

function trim ( s )
{
	return rtrim(ltrim(s));
};
//--------------------------------------------------------------


/* A simple function to validate if the input value available */
function validateInput( sInput ){
	if (trim(sInput)=='') return false;
	return true;
};

function validPostcode( sPostcode ) {
   var sPostcode = trim(sPostcode);
   var iPostcode= /^\d{5}$/;
   
   /* eslee - 15/02/2005 - do not restrict length to 5 numbers only
   if (sPostcode.match(iPostcode)){
   		return true;
	}
	else{
		return false;
	}
	*/
	if (isNaN(sPostcode)){
		return false;
	}
	else {
		return true;
	}
};

/* A simple function to validate NRIC value */
function validIC(sIC) {
   var icno= /^\d{6}-\d{2}-\d{4}$/;
   if (sIC.match(icno)){
   		return true;
	}
	else{
		return false;
	}
};


/* A simple function to validate Email value */
function validEmail( sEmail ) {
   //var email=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; //--edited by alizah due to MN6451 prob
   //var email=/^\w+([\.-][-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; //MN6451 s.h.-lim@streamyx.com
   var email="^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$"

   if (sEmail.match(email)){
   		return true;
	}
	else{
		return false;
	}
};


// A simple function to print out entire web page
function PrintFriendly(){
	document.all("top_banner").style.visibility='hidden';
	popwin = window.open ('', 'Print', 'location=no,menubar=no,resizable=yes,titlebar=no,width=600,height=400');
	popwin.document.write( '<html>' ) ;
	popwin.document.write( '\n<' + 'script language="JavaScript" type="text/JavaScript">' ) ;
	popwin.document.write( '\nfunction PrintFriendly(){' ) ;
	popwin.document.write( '\n  window.print();' ) ;
	popwin.document.write( '\n  window.close();' ) ;
	popwin.document.write( '\n};' ) ;
	popwin.document.write( '\n<' + '/script>' ) ;
	popwin.document.write( '\n<' + 'link rel="stylesheet" href="/mia/css/ocean.css" type="text/css">');		
	popwin.document.write( '\n<body onLoad="PrintFriendly();">' ) ;
	popwin.document.write( '\n' + PrintContentBody.innerHTML ) ;
	popwin.document.write( '\n</body>' ) ;
	popwin.document.write( '\n</html>' ) ;
	popwin.document.close();
	document.all("top_banner").style.visibility='visible';
}

