/********************************************************************************************
	LIBRARY NAMING CONVENTION: 
		TO PREVENT NAME CONFLICT, ALL FUNCTION & VARIABLE NAMES
		HERE SHOULD BEGIN WITH js_UNDERSCORE.  FOR EXAMPLE, js_Is_Date().
	(Note: don't just start with underscore, like _Start_Function(), because 
		underscore-starting is not allowed in VB Script, and Javascript
		may be called from a VBS block!!!
********************************************************************************************/


/* -------------------------------------------------------------
Removes preceding and trailing blanks

Parameters:
	str: String
Returns:
	Original string with leading and trailing blanks removed.
---------------------------------------------------------------- */

/* ---------------------
	regular expression: 
		/,/   		- this will match 1st occurance of the comma
		/,/g   		- this will match all occurances of the comma
		/['",;]/   	- this will match 1st occurance of each char inside the []
		/['",;]/g   - this will match all occurances of each char inside the []
		/^\s+/  	- for 1st occurance of any beginning white space char (blank, tab, etc.)
		/\s+$/		- for 1st occurance of any trailing white space char
		/\s+/g   	- for any occurance of any white space char (blank, tab, etc.)
------------------------*/
function js_Trim_String(str) {	
   	var re_start = /^\s+/ ;  // regular expression for 1st occurance of any beginning white space char (blank, tab, etc.)
	var re_end = /\s+$/ ;	 // regular expression for 1st occurance of any trailing white space char
	
	if (str == null || str == "") return "";
	return str.replace(re_start, "").replace(re_end, "");		
}

/* -------------------------------------------------------------
Removes any blanks from a given string (leading, trailing, and anywhere in between)

Parameters:
	str: String
Returns:
	Original string with all blanks removed.
---------------------------------------------------------------- */
function js_Remove_Space(str) {
   	//var re_blk = /\s+/ ;  // regular expression for 1st occurance of any white space char (blank, tab, etc.)
   	var re_blk = /\s+/g ;  // regular expression for any occurance of any white space char (blank, tab, etc.)
	
	if (str == null || str == "") return str;		
	return str.replace(re_blk, "");		
}


/* -------------------------------------------------------------
Open a new browser window in the center of the screen

Parameters:
	mypage: URL of the web page to open
	myname: pop-up window's name
	w: the pop-up page's width, can be in pixel or percentage.  If bigger than actual screen, will be minimize to actual screen size.
	h: height, can be in pixel or percentage.  If bigger than actual screen, will be minimize to actual screen size.
	scrollable: "true / false" - whether scrollable (display scrollbar)
	resizable: "true / false" - whether user resizable
---------------------------------------------------------------- */		
function js_Open_New_Window(mypage, myname, w, h, scrollable, resizable) {
	var PercenPosistion, LeftPosition, TopPosition;
	var mysettings, win;
	var IsPercentW, IsPercentH;
	
	IsPercentW = IsPercentH = false;
	
	// whether percentage
	if (isNaN(w) && (PercenPosistion = w.indexOf("%")) != -1) {
		// w is given as percentage, need to convert it to actual pixel
		IsPercentW = true;
		w = w.substr(0, PercenPosistion);
	}	
	
	if (isNaN(h) && (PercenPosistion = h.indexOf("%")) != -1) {
		// h is given as percentage
		IsPercentH = true;
		h = h.substr(0, PercenPosistion);
	}
	
	// make sure they are integers
	w = parseInt(w);
	h = parseInt(h);
	
	if (isNaN(w)) {
		w = screen.availWidth;
	}
	else if (IsPercentW) {
		w = screen.availWidth * w / 100; 	// convert percentage to pixel
	}
	
	if (isNaN(h)) {
		h = screen.availHeight;
	}
	else if (IsPercentH) {
		h = screen.availHeight * h / 100;	// convert percentage to pixel
	}
	
	// make sure not bigger than the actual screen
	if (w > screen.availWidth) w = screen.availWidth;	// get 100%
	if (h > screen.availHeight) h = screen.availHeight;
	
	// make it center
	LeftPosition = (screen.availWidth) ? (screen.availWidth - w)/2 : 0;
	TopPosition = (screen.availHeight) ? (screen.availHeight - h)/2 : 0;
	
	mysettings = 'height=' + h + ',width=' + w 
				+ ',top=' + TopPosition + ',left=' + LeftPosition 
				+ ',scrollbars=' + scrollable + ',resizable=' + resizable;

	win = window.open(mypage, myname, mysettings);
	return win;
}

// This is just an enhanced version of the above js_Open_New_Window() by adding two more parameters
function js_Open_New_Window_2(mypage, myname, w, h, scrollable, resizable, menubar, toolbar) {
	var PercenPosistion, LeftPosition, TopPosition;
	var mysettings, win;
	var IsPercentW, IsPercentH;
	
	IsPercentW = IsPercentH = false;
	
	// whether percentage
	if (isNaN(w) && (PercenPosistion = w.indexOf("%")) != -1) {
		// w is given as percentage, need to convert it to actual pixel
		IsPercentW = true;
		w = w.substr(0, PercenPosistion);
	}	
	
	if (isNaN(h) && (PercenPosistion = h.indexOf("%")) != -1) {
		// h is given as percentage
		IsPercentH = true;
		h = h.substr(0, PercenPosistion);
	}
	
	// make sure they are integers
	w = parseInt(w);
	h = parseInt(h);
	
	if (isNaN(w)) {
		w = screen.availWidth;
	}
	else if (IsPercentW) {
		w = screen.availWidth * w / 100; 	// convert percentage to pixel
	}
	
	if (isNaN(h)) {
		h = screen.availHeight;
	}
	else if (IsPercentH) {
		h = screen.availHeight * h / 100;	// convert percentage to pixel
	}

	// make sure not bigger than the actual screen
	if (w > screen.availWidth) w = screen.availWidth;	// get 100%
	if (h > screen.availHeight) h = screen.availHeight;
	
	// make it center (since the height & width are the display area - not including menu bar - the height may not be cenetered.
	LeftPosition = (screen.availWidth) ? (screen.availWidth - w)/2 : 0;
	TopPosition = (screen.availHeight) ? (screen.availHeight - h)/2 : 0;
	
	mysettings = 'height=' + parseInt(h) + ',width=' + parseInt(w)
				+ ',top=' + parseInt(TopPosition) + ',left=' + parseInt(LeftPosition)
				+ ',scrollbars=' + scrollable + ',resizable=' + resizable
				+ ',menubar=' + menubar + ',toolbar=' + toolbar;
				
	//alert("mysettings = " + mysettings);
	win = window.open(mypage, myname, mysettings);
	return win;
}


// center the current browser window on the user's computer screen
function js_Center_Window() {
	var w, h, LeftPosition, TopPosition;
	
	w = document.body.clientWidth;
	h = document.body.clientHeight;
	// h = document.body.scrollHeight;	// with scroll bar

	// make sure not bigger than the actual screen
	if (w > screen.availWidth) w = screen.availWidth;	// get 100%
	if (h > screen.availHeight) h = screen.availHeight;
	
	// make it center
	LeftPosition = (screen.availWidth) ? (screen.availWidth - w)/2 : 0;
	TopPosition = (screen.availHeight) ? (screen.availHeight - h)/2 : 0;
	
	window.moveTo(LeftPosition, TopPosition);
}

// myString can be anything, but the correct format is one of the following (year should be after 1000):
//		yyyy-mm-dd 
//		yyyy-mm-dd-hh-mm	 	(hh is in 24 hour format)
//		yyyy-mm-dd-hh-mm-ss 	(hh is in 24 hour format)
function js_Is_Date(myString, hasTime, hasSecond, delimiter) {
	var myDate, myTime, year, month, day, MaxDays;
	var isDateValid = true;
	var EarliestYear = 1000;	// it should be a 4-digit year
	
	if (myString == null) {
		myString = "";
	}
	
	//myDate = myDate.replace(/-/g, ""); // myDate.replace("-", "")
	if (myString != "" && (delimiter != null || delimiter != "")) {
		myString = myString.replace(delimiter, "");
		myString = myString.replace(delimiter, "");
		
		// one more if there is time part
		if (hasTime) {
			myString = myString.replace(delimiter, "");
		}
	}
		
	if (!hasTime) {
		myDate = myString;
	}
	// has time part
	else if (myString.length <= 8) {
		myDate = myString;
		myTime = "";
	}
	else {
		myDate = myString.substring(0, 8);
		myTime = myString.substring(8);
	}

	if (myDate == "" || isNaN(myDate) || myDate.length != 8)	{
		isDateValid = false;
	}
	else {
		year = parseInt(myDate.substring(0, 4), 10)
		month = parseInt(myDate.substring(4, 6), 10)
		day = parseInt(myDate.substring(6, 8), 10)

		if (year < EarliestYear) {
			isDateValid = false;
		}
		else if (month < 1 || month > 12) {
			isDateValid = false;
		}
		else {
			switch ( month ) {
				case 4: case 6: case 9: case 11:
					MaxDays = 30;
					break;
				case 2:
					if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)) { 
						MaxDays = 29;
					}
					else {
						MaxDays = 28;
					}
					break;
				default:
					MaxDays = 31;
					break;
			}
			
			if (day < 1 || day > MaxDays) {
			  isDateValid = false;
			}
		}
	}
	
	if (isDateValid && hasTime) {
		isDateValid = js_Is_Time(myTime, hasSecond, delimiter);
	}
	
	return isDateValid;
}


// myTime can be anything, but the correct format is hh-mm-ss (hh is in 24 hour format)
function js_Is_Time(myTime, hasSecond, delimiter) {
	var myTime, hour, minute, second;
	var isTimeValid = true;
	
	if (myTime == null) {
		myTime = "";
	}
	// some may not have seconds built in
	else if (!hasSecond) {
		myTime += delimiter + "00";
	}
	
	//myDate = myDate.replace(/-/g, ""); // myDate.replace("-", "")
	if (myTime != "" && (delimiter != null || delimiter != "")) {
		myTime = myTime.replace(delimiter, "");
		myTime = myTime.replace(delimiter, "");
	}

	if (myTime == "" || isNaN(myTime) || myTime.length != 6)	{
		isTimeValid = false;
	}
	else {
		hour = parseInt(myTime.substring(0, 2), 10)
		minute = parseInt(myTime.substring(2, 4), 10)
		second = parseInt(myTime.substring(4, 6), 10)

		if (hour < 0 || hour > 23) {
			isTimeValid = false;
		}
		else if (minute < 0 || minute > 59) {
			isTimeValid = false;
		}
		else if (second < 0 || second > 59) {
			isTimeValid = false;
		}
	}
	
	return isTimeValid;
}

// whether the given string value is an integer (10-based)
// zeroOK: whether it is OK for the value to be 0
// negativeOK: whether it is OK for the value to be negative
function js_Is_Integer(Str, zeroOK, negativeOK) {
	var valid = true;
	
	try {
		Str = js_Trim_String(Str);
		var val = parseInt(Str, 10);
		if (Str != val || Str == "") {
			valid = false;
		}
		else if (val == 0 && !zeroOK) {
			valid = false;
		}
		else if (val < 0 && !negativeOK) {
			valid = false;
		}
	}
	catch (ex) {
		valid = false;
	}
	
	return valid;
}

// whether the given string value is a float
// zeroOK: whether it is OK for the value to be 0
// negativeOK: whether it is OK for the value to be negative
function js_Is_Float(Str, zeroOK, negativeOK) {
	var valid = true;
	
	try {
		Str = js_Trim_String(Str);
		var val = parseFloat(Str, 10);
		
		if (Str != val || Str == "") {
			valid = false;
		}
		else if (val == 0 && !zeroOK) {
			valid = false;
		}
		else if (val < 0 && !negativeOK) {
			valid = false;
		}
	}
	catch (ex) {
		valid = false;
	}
	
	return valid;
}

// whether the given Str is exactly Len characters long, and is a hex code.
function js_Is_HexCode(Str, Len) {
	var hex = "0123456789ABCDEF";
	var valid = true;
	
	try {
		Str = js_Trim_String(Str).toUpperCase();
		if (Str.length != Len) {
			valid = false;
		}
		else {
			for (var i = 0; i < Str.length; i++) {
				if (hex.indexOf(Str.charAt(i)) == -1) { 
					valid = false; 
					break;
				}
			}
		}
	}
	catch (ex) {
		valid = false;
	}

	return valid;
}


/* ------------------------------------------------------------------
	Use in many files to show a red progress bar (language neutral)
--------------------------------------------------------------------- */
var JS_Progress_Block_Root_Obj;
var JS_Progress_Msg_Obj;
var JS_Progress_Bar_Parent_Obj;
var JS_Progress_Bar_Name;
var JS_Progress_Bar_Count;
var JS_Progress_Bar_Color;	// set to progress bar color
var JS_Progress_Interval_MilliSecond;	// set to time between updates (milli-seconds)
var JS_Progress_Bar_Index = 0;
var JS_Progress_Handle;

function js_Progress_Initialize(ProgressBlockRootObj, ProgressMsgObj, ProgressBarParentObj, ProgressBarName, ProgressBarCount, ProgressBarColor, ProgressIntervalMilliSecond) {
	JS_Progress_Block_Root_Obj 	= ProgressBlockRootObj;
	JS_Progress_Msg_Obj 			= ProgressMsgObj;
	JS_Progress_Bar_Parent_Obj 	= ProgressBarParentObj;
	JS_Progress_Bar_Name 			= ProgressBarName;
	JS_Progress_Bar_Count 		= ProgressBarCount;
	JS_Progress_Bar_Color 		= ProgressBarColor;
	JS_Progress_Interval_MilliSecond 	= ProgressIntervalMilliSecond;
}

function js_Progress_Start() {
	JS_Progress_Block_Root_Obj.style.display = "block";
	JS_Progress_Block_Root_Obj.style.visibility = "visible";
	JS_Progress_Msg_Obj.style.visibility = "visible";
	JS_Progress_Bar_Parent_Obj.style.visibility = "visible";
	js_Progress_Update();		// start progress bar
}

// delay start the progress bar
function js_Progress_Start_Delay(delay_milliseconds) {
	window.setTimeout("js_Progress_Start()", delay_milliseconds);
}

function js_Progress_Clear(ClearAll) {
	if (ClearAll) {
		//JS_Progress_Block_Root_Obj.style.visibility = "hidden";	// hide progress bar (but reserve the layout space)
		//JS_Progress_Block_Root_Obj.style.display = "none";		// visibility = "hidden", or display = "none": if "none",  hide progress bar (also remove the layout space)
		JS_Progress_Block_Root_Obj.style.display = "none";		// visibility = "hidden", or display = "none": if "none",  hide progress bar (also remove the layout space)
		JS_Progress_Msg_Obj.style.visibility = "hidden";		// visibility = "hidden", or display = "none": if "none",  hide progress bar (also remove the layout space)
		JS_Progress_Bar_Parent_Obj.style.visibility = "hidden";		// visibility = "hidden", or display = "none": if "none",  hide progress bar (also remove the layout space)
	}
	/*  // this will hide it forever
	else {
		//JS_Progress_Bar_Parent_Obj.style.display = "none";		// visibility = "hide", or display = "none": if "none", not only hide progress bar (also remove the layout space)
		JS_Progress_Bar_Parent_Obj.style.visibility = "hidden";		// visibility = "hide", or display = "none": if "none", not only hide progress bar (also remove the layout space)
	}
	*/

	for (var i = 1; i <= JS_Progress_Bar_Count; i++) {
		document.getElementById(JS_Progress_Bar_Name + i).style.backgroundColor = 'transparent';
	}

	JS_Progress_Bar_Index = 0;
}

function js_Progress_Update() {
	JS_Progress_Bar_Index++;
		
	if (JS_Progress_Bar_Index > JS_Progress_Bar_Count) {
		js_Progress_Clear(false);
	}
	else {
		document.getElementById(JS_Progress_Bar_Name + JS_Progress_Bar_Index).style.backgroundColor = JS_Progress_Bar_Color;
	}

	JS_Progress_Handle = setTimeout('js_Progress_Update()', JS_Progress_Interval_MilliSecond);
}

function js_Progress_Stop() {
	clearTimeout(JS_Progress_Handle);
	js_Progress_Clear(true);
}

		
/* ------------------------------------------------------------------
	Use in several files to show a timer counter (English Only so far)
--------------------------------------------------------------------- */			
var JS_Tick_Handle;
var JS_Time_Counter = 0;
var JS_Timer_Text_Obj;
var JS_Timer_Text_Header;

function js_Start_Clock(textObj, textHeader) {
	JS_Timer_Text_Obj = textObj;	
	JS_Timer_Text_Header = textHeader;
	
	JS_Timer_Text_Obj.innerText = "";  // initialize
	JS_Tick_Handle = window.setInterval("js_Update_Clock()", 1000);
}

function js_Update_Clock() {
	var day, hour, minute, second;
	var displayMsg = JS_Timer_Text_Header;

	JS_Time_Counter++;
	day = Math.floor(JS_Time_Counter/216000);
	hour = (Math.floor(JS_Time_Counter/3600))%24;	// can't be greater than 23
	minute = (Math.floor(JS_Time_Counter/60))%60;	// can't be greater than 59
	second = JS_Time_Counter%60;
	
	// no display of day if it is 0
	if (day > 1) {
		displayMsg += day + " days ";
	}
	else if (day == 1) {
		displayMsg += day + " day ";
	}

	// no display of hour if it is 0
	if (hour > 1) {
		displayMsg += hour + " hours ";
	}
	else if (hour == 1) {
		displayMsg += hour + " hour ";
	}

	// no display of minute if it is 0
	if (minute > 1) {
		displayMsg += minute + " minutes ";
	}
	else if (minute == 1) {
		displayMsg += minute + " minute ";
	}

	// no display of second if it is 0
	if (second > 1) {
		displayMsg += second + " seconds";
	}
	else if (second == 1) {
		displayMsg += second + " second";
	}

	JS_Timer_Text_Obj.innerText = displayMsg;
}

function js_Stop_Clock() {
	clearTimeout(JS_Tick_Handle);
	JS_Timer_Text_Obj.innerText = "";
	JS_Timer_Text_Obj.style.display = "none";		// visibility = "hidden", or display = "none": if "none",  hide progress bar (also remove the layout space)
	JS_Timer_Text_Obj.style.visibility = "hidden";		// visibility = "hidden", or display = "none": if "none",  hide progress bar (also remove the layout space)
	JS_Timer_Text_Obj.style.visibility = "hidden"
}


