/*********************************************************************************************************
	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;
}


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;
}


/*********************************************************************************************************
	isValidMessage
*********************************************************************************************************/
function isValidMessage(strMsg)
{
	var AlphaList1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var AlphaList2 = "abcdefghijklmnopqrstuvwxyz";	
	var NumList = "0123456789";
	var charList = " .,'?!\"-()@/:_;+&%*=<>$[]{}\\~^#|"
//	var controlList = ch(13);

	var controlList = String.fromCharCode(13, 10);

	var validList = AlphaList1 + AlphaList2 + NumList + charList + controlList;
	if (strMsg.length <= 0)
		return false;
	
	var ch = "";
	for (var i=0; i<strMsg.length; i++)
	{
		ch = strMsg.charAt(i);
		if (validList.indexOf(ch) < 0)
			return false;
	}
	return true;
}
/*********************************************************************************************************
	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;
}

/*********************************************************************************************************
	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;
}

/*********************************************************************************************************
	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;
  }
}	

/*********************************************************************************************************
	MoveItems
*********************************************************************************************************/
function MoveItems(optFrom, optTo, SortDest, MoveAll)
{
	for (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);
			optTo.options[optTo.options.length] = optionItem;
			optFrom.options[i] = null;				
		}
	}
	
	if (SortDest) 
		SortList(optTo);
}

/*********************************************************************************************************
	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 + "," + objList.options[i].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;
		}
	}
}

/*********************************************************************************************************
	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 "";
}
