/*********************************************************************************************************
	isValidDate
*********************************************************************************************************/
function isValidDate(fDay, fMth, fYr)
{
	if (fDay.value > 28 && fMth.value == 2 && ! isLeap(fYr.value)) {
		fDay.value = 28;
		alert("Invalid date entered. Date is being corrected.");
		return false;
	}
	if (fDay.value > 29 && fMth.value == 2 && isLeap(fYr.value)) {
		fDay.value = 29;
		alert("Invalid date entered. Date is being corrected.");
		return false;
	}
	if (fDay.value > 30 && isT(fMth.value)) {
		fDay.value = 30;
		alert("Invalid date entered. Date is being corrected.");
		return false;
	}
	return true;
}

/*********************************************************************************************************
	isLeap
*********************************************************************************************************/
function isLeap(Year)
{
	if (Year % 4 == 0) {
		if (Year % 100 == 0) {
			return (Year % 400 == 0);
		}
		return true;
	}
	return false;
}

/*********************************************************************************************************
	isT
*********************************************************************************************************/
function isT(Month)
{
	if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
		return true;
	}
	return false;
}

/*********************************************************************************************************
	isEmpty
*********************************************************************************************************/
function isEmpty(sField)
{
  for (var i=0; i < sField.length; i++)
  {
    if(sField.substring(i, i+1) != " ")
      return false;
  }
  
  return true;
}

/*********************************************************************************************************
	isAllDigit
*********************************************************************************************************/
function isAllDigit(sField)
{
  for (var i=0; i<sField.length; i++)
  {
    var cChar = sField.charAt(i);
    if (cChar < '0' || cChar > '9')
    {
      return false;
    }
  }
  return true;
}

/*********************************************************************************************************
	isValidDecimal
*********************************************************************************************************/
function isValidDecimal(sField)
{
  if (isAllDigit(sField))
  {
		return true;
  }

	var iDecCount = 0;
  for (var i=0; i<sField.length; i++)
  {
    var cChar = sField.charAt(i);
    if ((cChar < '0' || cChar > '9') && cChar != '.')
    {
      return false;
    }

		if (cChar == '.')
		{
			iDecCount++;
		}
  }

	if (iDecCount > 1)
	{
		return false;
	}
	else
	{
	  return true;
	}
}

/*********************************************************************************************************
	isValidEmail
*********************************************************************************************************/
function isValidEmail(sField) 
{
  // Declare invalid list of characters 
  var invalidCharsList = " /:,;~#";  
  var errorChar, iCount, i;
	iCount = 0;

	if (sField.length < 5)
		return false;
	// Must have at least 1 "@" and "."
	// Also "@" must not be the first character
  else if (sField.indexOf('@',0)==-1 || sField.indexOf('@',0)== 0 || sField.indexOf('.',0)==-1) 
    return false;
	// "." is not the last character       
	else if (sField.lastIndexOf('.') == (sField.length - 1))
		return false;		
 	// Has no more than 3 characters after last "."
  else if ((sField.length - sField.lastIndexOf('.')) > 4) 
		return false;
	// Has no "_" after the "@"
  else if ( (sField.indexOf('_',0) < 0) && (sField.lastIndexOf('_') > sField.lastIndexOf('@')) )
		return false;
	
	// Makes sure that only one "@" is inside
  for (i = 0; i < sField.length; i++) 
	{
		if (sField.charAt(i) == '@')
			iCount++;
	}
	
	if (iCount > 1)
		return false;
		
	// Checks for invalid chracters
  for (i = 0; i < invalidCharsList.length; i++) 
  {
	  errorChar = invalidCharsList.charAt(i);
		// If invalid character exists
    if (sField.indexOf(errorChar,0) != -1) 
	  {
    	return false;
    }
  }  
	return true;
}

/*********************************************************************************************************
	getFileExt
*********************************************************************************************************/
function getFileExt(strFile)
{
	var strExt = "";
	if (strFile.length <= 0)
		return "";
	else
	{
		var iPos = strFile.lastIndexOf(".");
		if (iPos >= 0)
			strExt = strFile.substr(iPos, strFile.length-iPos)
	}

	return strExt.toUpperCase();
}

/*********************************************************************************************************
	isDateOver
	Assumes that date parameters are valid
	Returns true if date1 is greater than date2
*********************************************************************************************************/
function isDateOver(day1, month1, year1,day2, month2, year2) 
{
	var sDate1 = year1 + month1 + day1;
	var sDate2 = year2 + month2 + day2;

	return sDate1 > sDate2;
}

/*********************************************************************************************************
	isDateTimeOver
	Assumes that date parameters are valid
	Returns true if date1 is greater than date2
*********************************************************************************************************/
function isDateTimeOver(day1, month1, year1,hr1,min1,sec1,day2, month2, year2,hr2,min2,sec2) 
{
	var sDate1 = year1 + month1 + day1 + hr1 + min1 + sec1;
	var sDate2 = year2 + month2 + day2 + hr2 + min2 + sec2;

	return sDate1 > sDate2;
}

/*********************************************************************************************************
	ValidateDate
*********************************************************************************************************/
function ValidateDate(day, month, year) {
        if (day.value > 28 && month.value == 2 && ! isLeap(year.value)) {
          day.value = 28;
          alert("Invalid date entered. Date is being corrected.");
          return false;
        }
        if (day.value > 29 && month.value == 2 && isLeap(year.value)) {
          day.value = 29;
          alert("Invalid date entered. Date is being corrected.");
          return false;
        }
        if (day.value > 30 && isT(month.value)) {
          day.value = 30;
          alert("Invalid date entered. Date is being corrected.");
          return false;
        }
      return true;
    }
    function isLeap(Year) {
      if (Year % 4 == 0) {
        if (Year % 100 == 0) {
          return (Year % 400 == 0);
        }
        return true;
      }
      return false;
    }
    function isT(Month) {
      if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
        return true;
      }
      return false;
    }

/*********************************************************************************************************
	SortList
*********************************************************************************************************/
function SortList(objList)  
{
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<objList.options.length; i++)  
	{
		temp_opts[i] = objList.options[i];
	}
	
	for(var x=0; x<temp_opts.length-1; x++)  
	{
		for(var y=(x+1); y<temp_opts.length; y++)  
		{
			if (temp_opts[x].text > temp_opts[y].text) 
			{
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
				temp = temp_opts[x].value;
				temp_opts[x].value = temp_opts[y].value;
				temp_opts[y].value = temp;
      }
   	}
	}

	for(var i=0; i<objList.options.length; i++)  
	{
		objList.options[i].value = temp_opts[i].value;
		objList.options[i].text = temp_opts[i].text;
  }
}	

function ClearListbox(optList)
{
	for (i=optList.options.length-1; i>=0; i--)
	{
		optList.options[i] = null;				
	}		
}

function AddListItem(optList, strText, strValue)
{
	var optionItem = new Option(strText, strValue);
	optList.options[optList.options.length] = optionItem;
}

function RemoveSelItem(optList)
{
	for (i=optList.options.length-1; i>=0; i--)
	{
		if (optList.options[i].selected)
			optList.options[i] = null;				
	}		
}

/*********************************************************************************************************
	MoveItems
*********************************************************************************************************/
function MoveItems(optFrom, optTo, SortDest, MoveAll)
{
	for (var i=optFrom.options.length-1; i>=0; i--)
	{
		if (MoveAll || optFrom.options[i].selected)
		{
			var optionItem = new Option(optFrom.options[i].text, optFrom.options[i].value);
			if (!ListhasItem(optTo, optFrom.options[i].value))
				optTo.options[optTo.options.length] = optionItem;
		//	optFrom.options[i] = null;				
		}
	}
	
	if (SortDest) 
		SortList(optTo);
}

function ListhasItem(optList, strItem)
{
	for (i=optList.options.length-1; i>=0; i--)
	{
		if (optList.options[i].value == strItem)
		{
			return true;
		}
	}

	return false;
}

/*********************************************************************************************************
	UpdateListVal
*********************************************************************************************************/
function UpdateListVal(HidField, objList)
{
	HidField.value = "";
	for (i=0; i<objList.options.length; i++)
	{
		if (HidField.value.length == 0)
		{			
			HidField.value = objList.options[i].value;
		}
		else
		{
			HidField.value = HidField.value + "#@SP2@#" + objList.options[i].value;
		}			
	}

//	alert(HidField.value);
}

/*********************************************************************************************************
	SelAllChk
*********************************************************************************************************/
function SelAllChk(chkItem, Val)
{
	if (chkItem.value)
	{
		chkItem.checked = Val;
	}
	else
	{
		for (var i=0;i<chkItem.length;i++)
		{
			chkItem[i].checked = Val;
		}
	}
}

/*********************************************************************************************************
	ToggleSelection
*********************************************************************************************************/
function ToggleSelection(chkSelectionList, chkToggle)
{
	SelAllChk(chkSelectionList,chkToggle.checked);
}

/*********************************************************************************************************
	chkHasSel
*********************************************************************************************************/
function chkHasSel(chkItem)
{
	RetVal = false;
	if (chkItem.value)
	{
		return chkItem.checked;
	}
	else
	{
		for (var i=0;i<chkItem.length;i++)
		{
			if (chkItem[i].checked)
				return true;
		}
	}	
}

/*********************************************************************************************************
	HasDup
*********************************************************************************************************/
function HasDup(strList)
{
	if (strList.indexOf(',') >=  0)
	{
		arrList = strList.split(',');
		for (i=0; i<arrList.length;i++)
		{
			for (j=0; j<arrList.length;j++)
			{
				if ( (RemSpace(arrList[i].toUpperCase()) == RemSpace(arrList[j].toUpperCase())) && i!=j)
				{
					return arrList[i];
				}
			}
		}
	}
	
	return "";
}

function IsCheckListSelected(chkItemList)
{
	var bSelected = false;

	if (chkItemList.value)
	{ 
		bSelected	= chkItemList.checked;
	}
	else
	{
		for (var i=0; i<chkItemList.length; i++)
		{
			if (chkItemList[i].checked) 
			{
				bSelected = true;
				break;
			}
		}
	}

	return bSelected;
}

function BuildCheckListSelection(chkItemList, strSeparator)
{
	var strReturn = "";

	if (chkItemList.value)
	{ 
		strReturn	= chkItemList.value;
	}
	else
	{
		for (var i=0; i<chkItemList.length; i++)
		{
			if (chkItemList[i].checked) 
			{
				if (strReturn.length <= 0)
				{
					strReturn = chkItemList[i].value;
				}
				else
				{
					strReturn  = strReturn + strSeparator + chkItemList[i].value;
				}
			}
		}
	}

	return strReturn;
}

function DeletionConfirmation()
{
	var str = "";
	if (confirm("Warning, restoration is not possible once selected item has been deleted. \nConfirm delete selected item(s) ?") == false)
      return false;
    else
    {
      var isSelected = false;
      if (document.searchform.chkItemList.value)
      { 
        isSelected	= document.searchform.chkItemList.checked;
		str			= document.searchform.chkItemList.value
      }
      else
      {
        for (var i=0; i<document.searchform.chkItemList.length; i++)
        {
          if (document.searchform.chkItemList[i].checked) {
            isSelected = true;
			str		= str + "," + document.searchform.chkItemList[i].value;
			}
        }
      }
      if (!isSelected)
        alert("Please select at least one item to be deleted.");
	document.myform.chkItemList.value = str;
      return isSelected;
    }
}

function PublishConfirmation()
{
	var str = "";
	if (confirm("Publish selected records?") == false)
		return false;
	else
	{
		var isSelected = false;
		if (document.searchform.chkItemList.value)
		{ 
			isSelected	= document.searchform.chkItemList.checked;
			str	= document.searchform.chkItemList.value
		}
		else
		{
			for (var i=0; i<document.searchform.chkItemList.length; i++)
			{
				if (document.searchform.chkItemList[i].checked) 
				{
					isSelected = true;
					if (str == "")
					{
						str	= document.searchform.chkItemList[i].value;
					}
					else
					{
						str	= str + "," + document.searchform.chkItemList[i].value;
					}
				}
			}
		}

		if (!isSelected)
		{
			alert("Please select at least one record.");
		}

		document.myform.chkItemList.value = str;
		return isSelected;
	}
}

function ShowPopup(URL, Width, Height)
{
	screenY = document.body.offsetHeight
	screenX = window.screen.availWidth
	leftvar = (screenX - Width) / 2
	rightvar = (screenY - Height) / 2
	properties = "scrollbars=no,height=" + Height;		
	properties = properties + ",width=" + Width;
	properties = properties + ",left=" + leftvar;
	properties = properties + ",top=" + rightvar;
	window.open(URL, "Location", properties);
}

function ShowPopupWithScrollbar(URL, Width, Height)
{
	screenY = document.body.offsetHeight
	screenX = window.screen.availWidth
	leftvar = (screenX - Width) / 2
	rightvar = (screenY - Height) / 2
	properties = "scrollbars=yes,height=" + Height;		
	properties = properties + ",width=" + Width;
	properties = properties + ",left=" + leftvar;
	properties = properties + ",top=" + rightvar;
	properties = properties + ",resizable=1";
	window.open(URL, "Location", properties);
}

function ShowModalPopup(strURL, strWindowName, iWidth, iHeight, bScrollbar)
{
	if (window.showModalDialog) 
	{
		// IE
		window.showModalDialog(strURL,strWindowName,"dialogWidth:" + iWidth + "px;dialogHeight:" + iHeight + "px");
	} 
	else 
	{
		// Mozilla and Netscape
		screenY = document.body.offsetHeight
		screenX = window.screen.availWidth
		leftvar = (screenX - iWidth) / 2
		rightvar = (screenY - iHeight) / 2

		// Properties
		var strProperties = "";

		if (bScrollbar == true)
		{
			strProperties += "scrollbars=yes";
		}
		else
		{
			strProperties += "scrollbars=no";
		}
		strProperties += ",toolbar=no";		
		strProperties += ",directories=no";		
		strProperties += ",status=no";		
		strProperties += ",resizable=no";		
		strProperties += ",modal=yes";		
		strProperties += ",height=" + iHeight;		
		strProperties += ",width=" + iWidth;
		strProperties += ",left=" + leftvar;
		strProperties += ",top=" + rightvar;

		window.open(strURL,strWindowName, strProperties);
	}
}
