﻿/*
Dialog to confirm or deny a question.
Requires HTML:
<div style="position:fixed; width:100%; height:100%;">
    <div id="ConfirmDiv" class="ConfirmDiv DialogGeneral" title="TITLE">
        MESSAGE
    </div>
</div>    
*/
function ConfirmDialog(strTitle, strMessage, funcYes, funcNo) {
 
    if(strTitle == '') {
        strTitle = "Are you sure?";
    }
    if(strMessage == '') {
        strMessage = "";
    }
    if (funcNo == null) {
        funcNo = CloseConfirmDialog;
    }
    if (typeof(funcYes) != "function") {
        funcYes = CloseConfirmDialog;
    }
    if(typeof(funcNo) != "function") {
        funcNo = CloseConfirmDialog;
    }

    $(".ConfirmDiv").html(strMessage);
    
    $(".ConfirmDiv").dialog('destroy');
    $(".ConfirmDiv").dialog({
        title: strTitle,
        position: 'center',
        autoOpen: true,
        bgiframe: true,
        buttons: { "Yes": funcYes, "No": CloseConfirmDialog },
        hide: 'slide',
        show: 'clip',
        modal: true
    });
}
function CloseConfirmDialog() {
    $(".ConfirmDiv").dialog('close');
}

//// ERROR DIALOG //////////////////////////////////////////////////////////////////
/*
                <div id="ErrorDiv" class="ErrorDiv DialogGeneral ui-state-error" title="Error">
                    MESSAGE
                </div>
*/
function ErrorDialog(strTitle, strMessage) {

    if (strTitle == '') {
        strTitle = "Error";
    }
    if (strMessage == '') {
        strMessage = "Unknown Error";
    }

    $(".ErrorDialogDiv").html(strMessage);

    $(".ErrorDialogDiv").dialog('destroy');
    $(".ErrorDialogDiv").dialog({
        title: strTitle,
        position: 'center',
        autoOpen: true,
        buttons: { "Ok": function() {
        $(".ErrorDialogDiv").dialog('close');
        } 
        },
        bgiframe: true,
        hide: 'slide',
        show: 'clip',
        modal: true
    });
}


/// VALIDATIONS - EMAIL /////////////////////////////////////////
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
function AddEmailValidator(selector, cbValid, cbInvalid) {
    $(selector).keyup(function() {
        var email = $(selector).val();
        if (email != "" && email != 0) {
            if (isValidEmailAddress(email)) {
                cbValid();
            } else {
                cbInvalid();
            }
        } else {
            cbInvalid();
        }
    });
    $(selector).blur(function() {
        var email = $(selector).val();
        if (email != "" && email != 0) {
            if (isValidEmailAddress(email)) {
                cbValid();
            } else {
                cbInvalid();
            }
        } else {
            cbInvalid();
        }
    });
}

// NUMERICs only (PHONE NUMBER)
function FilterOnlyNumeric(e) {
    var keyVal = e.keyCode ? e.keyCode : e.charCode;
    
    if (((keyVal < 48) || (keyVal > 58))) {
        return false;
    }
}
function FilterOnlyMoney(e) {
    var keyVal = e.keyCode ? e.keyCode : e.charCode;
    
    if ( ((keyVal < 48) || (keyVal > 58)) && (keyVal != 46) ) {
        return false;
    }
}