function trim(stringToTrim) 
{
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function Replace(sString, sFind, sReplace) 
{
	while ((sString.indexOf(sFind))>0)
	{
		sString = sString.replace(sFind,sReplace);
	}
	
	return (sString);
} 

function textLimit(txtX, nMaxLen, lblError) 
{
	if (txtX.value.length > nMaxLen + 1)
	{
		lblError.style.visibility = "visible";
	}
	if (txtX.value.length > nMaxLen)
	{
		txtX.value = txtX.value.substring(0, nMaxLen);
	}
} 

function IsDate(dateStr) 
{
	//strip time part
	if (dateStr.indexOf(' ')>0)
	{
		dateStr = dateStr.split(" ")[0];
	}

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) 
	{
		return false;
	}

	day = matchArray[1]; // p@rse date into variables
	month = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) 
	{ // check month range
		return false;
	}

	if (day < 1 || day > 31) 
	{
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		return false;
	}

	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
		return false;
		}
	}
	return true; // date is valid
}

function DateDiff(sInterval,sDate1,sDate2)
{
  var nRes;
  var nYears;
  var nMonths;
  var sTemp;

  if (sInterval=='d')
  {
	// reformat into US format
	sDate1 = sDate1.split("/")[1] + '/' + sDate1.split("/")[0] + '/' + sDate1.split("/")[2];
	sDate2 = sDate2.split("/")[1] + '/' + sDate2.split("/")[0] + '/' + sDate2.split("/")[2];

	sDate1 = new Date(sDate1);
	sDate2 = new Date(sDate2);
	nRes = Math.round((sDate1-sDate2)/24*60*60*1000);
  }
  
  if (sInterval=='m')
  {

	// reformat into US format
	sDate1 = sDate1.split("/")[1] + '/' + sDate1.split("/")[0] + '/' + sDate1.split("/")[2];
	sDate2 = sDate2.split("/")[1] + '/' + sDate2.split("/")[0] + '/' + sDate2.split("/")[2];

	sDate1 = new Date(sDate1);
	sDate2 = new Date(sDate2);

    if ( sDate1 > sDate2 ) 
    {
      sTemp = sDate1;
      sDate1 = sDate2
      sDate2 = sTemp;
    }
    
    nYears  = ( sDate2.getFullYear() - sDate1.getFullYear() ) * 12;
    nMonths = ( sDate2.getMonth()    - sDate1.getMonth() );
 
	nRes = nYears + nMonths;

  }
  
  return nRes;
}

function SetElementVisibility(sID,bVisible)
{
    var oElement;

    oElement = document.getElementById(sID);

    if (oElement != null) 
    {
        if (!bVisible) 
        {
            oElement.style.display = "none";
        }
        else 
        {
            oElement.style.display = "";
        }
    }
}

function FlipElementVisibility(sID) 
{
    var oElement;
    var bVisible;
    
    oElement = document.getElementById(sID);

    if (oElement != null) 
    {
        if (oElement.style.display == "") 
        {
            oElement.style.display = "none";
            bVisible = false;
        }
        else 
        {
            oElement.style.display = "";
            bVisible = true;
        }
    }

    return (bVisible);
}

function GetQueryStringValue(sKey) 
{
    var nPair;
    var nPairs;
    var asPairs;
    var asPair;
    var sValue;
    var sPairKey;
    var sPairValue;
    var sRes;

    sList = document.location.href;
    sList = sList.split('?')[1];

    asPairs = sList.split("&");

    nPairs = asPairs.length;

    for (nPair = 0; nPair < nPairs; nPair++) {
        asPair = asPairs[nPair].split("=");
        sPairKey = asPair[0];
        sPairValue = asPair[1];
        if (sPairKey == sKey) {
            sRes = sPairValue;
            break;
        }
    }

    return (sRes);
}

function NumericTest(txtX, e, bAllowDecimal) 
{
    var key;
    var keychar;
    var bRes;

    if (window.event) 
    {
        key = window.event.keyCode;
    }
    else if (e) 
    {
        key = e.which;
    }
    else 
    {
        bRes = true;
    }

    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) 
    {
        bRes = true;
    }
    // numbers
    else if ((("0123456789").indexOf(keychar) > -1)) 
    {
        bRes = true;
    }
    // bAllowDecimalimal point jump
    else if ((bAllowDecimal) && (keychar == ".")) 
	{
		if (txtX.value.indexOf('.') == -1) 
		{
		    bRes = true;
		}
		else 
		{
		    bRes = false;
		}
    }
    else if ((bAllowDecimal) && (keychar == ",")) 
    {
        bRes = true;
    }
    else 
    {
        bRes = false;
    }
    event.returnValue = bRes;
    return (bRes);
}

