﻿/* add watermarl script */
function addWatermarkScript(obj, defaultValue, style, watermarkstyle) {
    try {
        if ($(obj).val() == "") $(obj).val(defaultValue).removeClass(style).addClass(watermarkstyle);
        
        /* Define what happens when the textbox comes under focus, remove the watermark class and clear the box */
        jQuery(obj).focus(function () {
            jQuery(this).filter(function () {
                /* We only want this to apply if there's not something actually entered */
                return jQuery(this).val() == "" || jQuery(this).val() == defaultValue
            }).removeClass(watermarkstyle).addClass(style).val("");
        });
        /* Define what happens when the textbox loses focus, add the watermark class and default text */
        jQuery(obj).blur(function () {
            jQuery(this).filter(function () {
                /* We only want this to apply if there's not something actually entered */
                return jQuery(this).val() == "" || jQuery(this).val() == defaultValue
            }).removeClass(style).addClass(watermarkstyle).val(defaultValue);
        });
    }
    catch (e) { alert(e); }
}

/* checbox exclusive, check only one of the checkbox in the group */
function CheckboxExclusive(obj, cbarray) {
    $("#" + obj).click(function () {
        for (var i = 0; i < cbarray.length; i++) {
            if (obj != cbarray[i]) document.getElementById(cbarray[i]).checked = false;
        }
    });
}

/**--------------------------
//* Validate Date Field script- By JavaScriptKit.com
//* For this script and 100s more, visit http://www.javascriptkit.com
//* This notice must stay intact for usage
---------------------------**/
function checkdate(date, dateType) {
    var validformat = /^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity

    if (!validformat.test(date)) {
        alert("Invalid Date Format. Please correct " + dateType + ".");
        return false;
    }
    else { //Detailed check for valid date ranges
        var monthfield = date.split("/")[0]
        var dayfield = date.split("/")[1]
        var yearfield = date.split("/")[2]
        var dayobj = new Date(yearfield, monthfield - 1, dayfield)

        if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield)) {
            alert("Invalid Day, Month, or Year range detected. Please correct " + dateType + ".");
            return false;
        }
        else
            return true;
    }
}

/* make sure email is in a valid format */
function CheckEmail(email) {
    var objRegExp = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
    var result = objRegExp.test(email);

    if (result == false) {
        alert('The e-mail address provided is invalid. Please check the value and try again.');
        return false;
    }
    else
        return true;
}


/* determine browser the user is using */
function GetBrowser() {
    var browser = "";
    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
        browser = "firefox";
    }
    else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
        browser = "ie";
    }
    else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
        browser = "opera";
    }
    else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
        browser = "chrome";
    }
    else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
        browser = "safari";
    }

    return browser;
}
