function validateDate(value, useFirstDay) {
    if (useFirstDay) {
        var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    } else {
        var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    }


    if ((value.match(RegExPattern)) && (value!='')) {
	return true;
    } else {
	return false;
    }
}

function isInteger(s)
{   
    var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isFloat(strValue)
{
    var fValue = parseFloat(strValue);

    if (isNaN(fValue)) {
        return false;
    } else {
	if (fValue == strValue) {
            return true;
        } else {
            return false;
        }
    }
}

function isPositiveFloat(strValue) {
    var fValue = parseFloat(strValue);

    if (isNaN(fValue)) {
        return false;
    } else {
	if (fValue == strValue) {
            if (fValue >=0) {
                return true;
            }  else {
                return false;
            }
        } else {
            return false;
        }
    }
}

function validateEmail( strValue) {
    var objRegExp  = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    //check for valid email
    return objRegExp.test(strValue);
}

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY);
}

function ecalIsLeapYear(year) {	
	return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}

function DaysPerMonth(year, month) 
{
	switch (month) 
	{
		case 1: 
		case 3: 
		case 5: 
		case 7: 
		case 8: 
		case 10: 
		case 12: return 31;
		case 4: 
		case 6: 
		case 9: 
		case 11: return 30;
		case 2: return ecalIsLeapYear(year) ? 29 : 28;
		default: return 0;
	}
}

/*
Clears all form's controls.
*/
function FormClear() {
    if (document.forms[0] != undefined) {
        document.forms[0].reset();
    }
}

/*
Opens a new window and load the given url for printing.
*/
function PrintForm(url) {
    window.open(url, "PrintWindow");
}

/*
Replaces all occurrences within the string.
*/
function replaceAll(text, strA, strB) 
{
    while ( text.indexOf(strA) != -1)
    {
        text = text.replace(strA,strB);
    }
    return text;
}

/*
Returns the selected date for the given DatePicker control
*/
function GetDatePickerDate(ctrlName) {
    var oMonth = document.getElementById(ctrlName + "_ddlMonth");
    var oDay = document.getElementById(ctrlName + "_ddlDay");
    var oYear = document.getElementById(ctrlName + "_ddlYear");
    var selMonth = oMonth.options[oMonth.selectedIndex].value - 1;
    var selDay = oDay.options[oDay.selectedIndex].value;
    var selYear = oYear.options[oYear.selectedIndex].value;

    var selDate = new Date(selYear, selMonth, selDay);
    return selDate;
}

/*
Returns the value of the selected item in the give select control.
*/
function GetSelectedItemValue(oCtl) {
    for (var i=0; i<oCtl.options.length; i++) 
    {
	    if (oCtl.options[i].selected) {
	        if (oCtl.options[i].value != "" || i+1 == oCtl.options.length) {
                return oCtl.options[i].value;
            } else {
                return oCtl.options[i+1].value;
            }
	    }
    }
   
    return oCtl.options[oCtl.options.length - 1].value;
}

/*
Returns the text of the selected item in the give select control.
*/
function GetSelectedItemText(oCtl) {
    for (var i=0; i<oCtl.options.length; i++) 
    {
	    if (oCtl.options[i].selected) {
	        if (oCtl.options[i].text != "" || i+1 == oCtl.options.length) {
                return oCtl.options[i].text;
            } else {
                return oCtl.options[i+1].text;
            }
	    }
    }
   
    return oCtl.options[oCtl.options.length - 1].value;
}

/*
Trims the given string.
*/
function StringTrim(str) {
    var trimmed = str.replace(/^\s+|\s+$/g, '');
    return trimmed;
}
