// Ham su dung de chon lua tat ca cac check box
function checkAll(){
	for (var i=0;i<document.forms[0].elements.length;i++){
		var e = document.forms[0].elements[i];
		var selectedID = "";
		if (e.name != 'chkSelectAll'){
			e.checked = document.forms[0].chkSelectAll.checked;			
		}
	}	
}

// Ham su dung de inversion su chon lua
function invertSelection(){
	for (var i=0;i<document.forms[0].elements.length;i++){
		var e = document.forms[0].elements[i];
		if (e.name != 'chkSelectAll'){
			e.checked = !e.checked;
		}
	}		
}

// Ham lay cac thanh phan dang dc select
function getSelectedIDS(){
	var selectedIDS = '';
	for (var i=0;i<document.forms[0].elements.length;i++){
		var e = document.forms[0].elements[i];		
		if ((e.name != 'chkSelectAll') && (e.name == 'chkID') && e.checked){
			selectedIDS = selectedIDS + ',' + e.value;
		}
	}			
	return Right(selectedIDS,Len(selectedIDS)-1);
}

// Ham lay cac thanh phan dang network dc select
function getSelectedNets(){
	var selectedNets = '';
	for (var i=0;i<document.forms[0].elements.length;i++){
		var e = document.forms[0].elements[i];		
		if ((e.name != 'chkStoreID') && (e.name == 'chkNetID') && e.checked){
			selectedNets = selectedNets + ',' + e.value;
		}
	}			
	return Right(selectedNets,Len(selectedNets)-1);
}

// Ham lay cac thanh phan dang store dc select
function getSelectedStores(){
	var selectedStores = '';
	for (var i=0;i<document.forms[0].elements.length;i++){
		var e = document.forms[0].elements[i];		
		if ((e.name != 'chkNetID') && (e.name == 'chkStoreID') && e.checked){
			selectedStores = selectedStores + ',' + e.value;
		}
	}			
	return Right(selectedStores,Len(selectedStores)-1);
}

//Len(String) : Returns the number of characters in a string
//===========================================================

function Len(str)
/***
		IN: str - the string whose length we are interested in

		RETVAL: The number of characters in the string
***/
{  return String(str).length;  }

//Right(string, length): Returns a specified number of characters from the
//                       right side of a string
//========================================================================

function Right(str, n)
/***
		IN: str - the string we are RIGHTing
			n - the number of characters we want to return

		RETVAL: n characters from the right side of the string
***/
{
		if (n <= 0)     // Invalid bound, return blank string
		   return "";
		else if (n > String(str).length)   // Invalid bound, return
		   return str;                     // entire string
		else { // Valid bound, return appropriate substring
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
}

function Left(str, n)
/***
	IN: str - the string we are LEFTing
	n - the number of characters we want to return
	
	RETVAL: n characters from the left side of the string
***/
{
	if (n <= 0) // Invalid bound, return blank string
		return "";
	else if (n > String(str).length) // Invalid bound, return
		return str; // entire string
	else // Valid bound, return appropriate substring
	
	return String(str).substring(0,n);
}

//Trim(string) : Returns a copy of a string without leading or
//               trailing spaces
//=============================================================
function Trim(str)
/***
		PURPOSE: Remove trailing and leading blanks from our string.
		IN: str - the string we want to Trim

		RETVAL: A Trimmed string!
***/
{
		return RTrim(LTrim(str));
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
//==================================================================
function RTrim(str)
/***
		PURPOSE: Remove trailing blanks from our string.
		IN: str - the string we want to RTrim

		RETVAL: An RTrimmed string!
***/
{
		// We don't want to trip JUST spaces, but also tabs,
		// line feeds, etc.  Add anything else you want to
		// "trim" here in Whitespace
		var whitespace = new String(" \t\n\r");

		var s = new String(str);

		if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
			// We have a string with trailing blank(s)...

			var i = s.length - 1;       // Get length of string

			// Iterate from the far right of string until we
			// don't have any more whitespace...
			while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
				i--;


			// Get the substring from the front of the string to
			// where the last non-whitespace character is...
			s = s.substring(0, i+1);
		}

		return s;
}

//LTrim(string) : Returns a copy of a string without leading spaces.
//==================================================================
function LTrim(str)
/***
		PURPOSE: Remove leading blanks from our string.
		IN: str - the string we want to LTrim

		RETVAL: An LTrimmed string!
***/
{
		var whitespace = new String(" \t\n\r");

		var s = new String(str);

		if (whitespace.indexOf(s.charAt(0)) != -1) {
			// We have a string with leading blank(s)...

			var j=0, i = s.length;

			// Iterate from the far left of string until we
			// don't have any more whitespace...
			while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
				j++;


			// Get the substring from the first non-whitespace
			// character to the end of the string...
			s = s.substring(j, i);
		}

		return s;
}

//Mid(string, start, length): Returns a specified number of characters from a
//                            string
//============================================================================

function Mid(str, start, len)
/***
		IN: str - the string we are LEFTing
			start - our string's starting position (0 based!!)
			len - how many characters from start we want to get

		RETVAL: The substring from start to start+len
***/
{
		// Make sure start and len are within proper bounds
		if (start < 0 || len < 0) return "";

		var iEnd, iLen = String(str).length;
		if (start + len > iLen)
				iEnd = iLen;
		else
				iEnd = start + len;

		return String(str).substring(start,iEnd);
}


// Keep in mind that strings in JavaScript are zero-based, so if you ask
// for Mid("Hello",1,1), you will get "e", not "H".  To get "H", you would
// simply type in Mid("Hello",0,1)

// You can alter the above function so that the string is one-based.  Just
// check to make sure start is not <= 0, alter the iEnd = start + len to
// iEnd = (start - 1) + len, and in your final return statement, just
// return ...substring(start-1,iEnd)

//InStr(str, SearchForStr) : Returns the location a character (charSearchFor)
//                           was found in the string str
//========================================================================

// InStr function written by: Steve Bamelis - steve.bamelis@pandora.be

function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}

//FormatDateTime(datetime, FormatType) : Returns an expression formatted
//                                       as a date or time
//========================================================================

function FormatDateTime(datetime, FormatType)
/*
	 FomatType takes the following values
		1 - General Date = Friday, October 30, 1998
		2 - Typical Date = 10/30/98
		3 - Standard Time = 6:31 PM
		4 - Military Time = 18:31
*/
{
	var strDate = new String(datetime);

	if (strDate.toUpperCase() == "NOW") {
		var myDate = new Date();
		strDate = String(myDate);
	} else {
		var myDate = new Date(datetime);
		strDate = String(myDate);
	}


	// Get the date variable parts
	var Day = new String(strDate.substring(0,3));
	if (Day == "Sun") Day = "Sunday";
	if (Day == "Mon") Day = "Monday";
	if (Day == "Tue") Day = "Tuesday";
	if (Day == "Wed") Day = "Wednesday";
	if (Day == "Thu") Day = "Thursday";
	if (Day == "Fri") Day = "Friday";
	if (Day == "Sat") Day = "Saturday";	
	
	var Month = new String(strDate.substring(4,7)), MonthNumber = 0;
	if (Month == "Jan") { Month = "January"; MonthNumber = 1; }
	if (Month == "Feb") { Month = "February"; MonthNumber = 2; }
	if (Month == "Mar") { Month = "March"; MonthNumber = 3; }
	if (Month == "Apr") { Month = "April"; MonthNumber = 4; }
	if (Month == "May") { Month = "May"; MonthNumber = 5; }
	if (Month == "Jun") { Month = "June"; MonthNumber = 6; }
	if (Month == "Jul") { Month = "July"; MonthNumber = 7; }
	if (Month == "Aug") { Month = "August"; MonthNumber = 8; }
	if (Month == "Sep") { Month = "September"; MonthNumber = 9; }
	if (Month == "Oct") { Month = "October"; MonthNumber = 10; }
	if (Month == "Nov") { Month = "November"; MonthNumber = 11; }
	if (Month == "Dec") { Month = "December"; MonthNumber = 12; }
	
	var curPos = 11;
	var MonthDay = new String(strDate.substring(8,10));
	if (MonthDay.charAt(1) == " ") {
		MonthDay = "0" + MonthDay.charAt(0);
		curPos--;
	}	
	
	var MilitaryTime = new String(strDate.substring(curPos,curPos + 5));
	
	var Year = new String(strDate.substring(strDate.length - 4, strDate.length));	
	
	document.write(strDate + "");	

	// Format Type decision time!
	if (FormatType == 1)
		strDate = Day + ", " + Month + " " + MonthDay + ", " + Year;
	else if (FormatType == 2)
		strDate = MonthNumber + "/" + MonthDay + "/" + Year.substring(2,4);
	else if (FormatType == 3) {
		var AMPM = MilitaryTime.substring(0,2) >= 12 && MilitaryTime.substring(0,2) != "24" ? " PM" : " AM";
		if (MilitaryTime.substring(0,2) > 12)
			strDate = (MilitaryTime.substring(0,2) - 12) + ":" + MilitaryTime.substring(3,MilitaryTime.length) + AMPM;
		else {
			if (MilitaryTime.substring(0,2) < 10)
				strDate = MilitaryTime.substring(1,MilitaryTime.length) + AMPM;
			else
				strDate = MilitaryTime + AMPM;
		}
	}	
	else if (FormatType == 4)
		strDate = MilitaryTime;


	return strDate;
}

//Thuc hien gotoURL co xac nhan
function confirmGoToURL(sMsg,sURL,sCheck) { 
  //document.MM_returnValue = false;  
  if (sCheck!=''){
  	if (confirm(sMsg)){
  		parent.location.href=sURL;
  	}
  }else{
	alert('Chọn lựa các mục cần xử lý!');
  }
}

