﻿$(document).ready(
function() {
    $.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        if (regexp.constructor != RegExp)
            regexp = new RegExp(regexp);
        else if (regexp.global)
            regexp.lastIndex = 0;
        return this.optional(element) || regexp.test(value);
    },
    "Please check your input.");

    $.fn.delay = function(time, callback) {
        // Empty function:
        jQuery.fx.step.delay = function() { };
        // Return meaningless animation, (will be added to queue)
        return this.animate({ delay: 1 }, time, callback);
    }

    $("form").submit(function() {
        if ($(this).valid()) {
            $(":submit,:button", this).prop("disabled", "disabled");
        }
    });

    //Disable all controls if in View Mode
    IsViewMode();

    //Select button event
    $('#UpdateMyPage').unbind('click');
    $('#UpdateMyPage').click(function(e) {
        $('input,select,textarea,img').removeAttr("disabled");
        $('#buttonHolderTop, #buttonHolderBot, #buttonHolder').show();
        $('#UpdateMyPage').hide();
        $('input:button').show();
        //enable Click event for links
        $("#MainPageContent a:not(#UpdateMyPage)").die('click');
    });
    if ($('#SuccessMsg').length && $('#SuccessMsg').is(':visible')) {
        $('#SuccessMsg').delay(10000, function() {
            $(this).hide();
        });
    }

    if ($('div[id=SuccessMessage]').length && $('div[id=SuccessMessage]').is(':visible')) {
        $('div[id=SuccessMessage]').delay(10000, function() {
            $(this).hide();
        });
    }

    if ($('div[id=FailureMessage]').length && $('div[id=FailureMessage]').is(':visible')) {
        $('div[id=FailureMessage]').delay(10000, function() {
            $(this).hide();
        });
    }

    if ($('#showleft_side').length) {
        $("#hideleft_side").click(function() {
            $("#panelleft_side").animate({ marginRight: "-175px" }, 500);
            $(".left_side").animate({ width: "0px", opacity: 0 }, 400);
            $("#showleft_side").show("normal").animate({ width: "22px", float: "right", opacity: 1 }, 200);
            $(".right_side").animate({ marginRight: "32px" }, 500);
        });
        $("#showleft_side").click(function() {
            $(".right_side").animate({ marginRight: "217px" }, 200);
            $("#panelleft_side").animate({ marginRight: "0px" }, 400);
            $(".left_side").animate({ width: "200px", opacity: 1 }, 400);
            $("#showleft_side").animate({ width: "0px", opacity: 0 }, 600).hide("slow");
        });
    }
    if ($('#UpOccasionsPanelHeader').length) {
        $('#UpOccasionsPanelHeader').unbind('click');
        $('#UpOccasionsPanelHeader').click(function(e) {
            if ($('#UpOccasionsPanel').is(':hidden')) {
                $('#UpOccasionsPanel').show();
                $('#UpOccasionsPanelArrow').removeClass('collapsiblepanedownR');
                $('#UpOccasionsPanelArrow').addClass('collapsiblepaneupR');
            }
            else if ($('#UpOccasionsPanel').is(':visible')) {
                $('#UpOccasionsPanel').hide();
                $('#UpOccasionsPanelArrow').removeClass('collapsiblepaneupR');
                $('#UpOccasionsPanelArrow').addClass('collapsiblepanedownR');
            }
        });
    }

    String.prototype.escapeSingleQuotes = function() {
        if (this == null) return null;
        return this.replace(/'/g, "\\'");
    }
});

function IsViewMode() {
    var preview = getQuerystring('View');

    if (preview != null && preview != "" && preview.toLowerCase() == 'true') {
        $('input,select,textarea,img').prop('disabled', 'disabled');
        $('#buttonHolderTop, #buttonHolderBot, #buttonHolder').hide();
        $('input:button').hide();
        $('#UpdateMyPage').show();
        //disable Click event for links
        $("#MainPageContent a:not(#UpdateMyPage)").live('click', function(e) {
            e.preventDefault;
            return false;
        });
    }
}

function FormatAmount(obj) {
    var _fmtedAmt = CurrencyFormatted($(obj).val());
    if (_fmtedAmt.length <= $(obj).prop("maxlength")) {
        $(obj).val(_fmtedAmt);
    }
}

function RoundDecimal(number, digitsAfterDecimal) {
    var result = (Math.round((number * Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal - 1)) / Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal);
    return result;
}

function SetRoundDecimalToElement(obj, digitsAfterDecimal) {

    var result = 0;
    var number = $(obj).val();
    if (is_numeric(number)) {
        result = (Math.round((number * Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal - 1)) / Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal);
    }
    $(obj).val(result);
}
function SetRoundDecimalForInvoice(obj, digitsAfterDecimal) 
{
    var _id = $(obj).prop('id').replace($(obj).prop('name'), '');
    var result = 0;
    var number = $(obj).val();
    if (number > 6) 
    {
        if (number > 999999.99) 
        {
            number = number.substring(0, 6);
        }
    }
    if (is_numeric(number)) 
    {
        result = (Math.round((number * Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal - 1)) / Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal);
    }
    $(obj).val(result);
    return result;
}
function SetRoundDecimalNew(obj, digitsAfterDecimal)
 {

     var result = 0;
     if (obj.length > 6)
      {
          if (obj > 999999.99) 
         {
             obj.substring(0, 6);
         }
     }
    var number = obj;
    if (is_numeric(number)) {
        result = (Math.round((number * Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal - 1)) / Math.pow(10, digitsAfterDecimal)).toFixed(digitsAfterDecimal);
    }
    return result;
}


function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

//to validate the quantity and in hand
function ValidateQuantity(obj) {
    var _dynId = parseInt(obj.id.replace("POQuantity", ""));

    //validate quantity
    var _qty = parseInt($('#POQuantity' + _dynId).val());
    var _inHand = parseInt($('#QuantityInHand' + _dynId).val());
    if (_qty > _inHand) {
        $('#POQuantity' + _dynId).addClass('red');
        $('#POQuantity' + _dynId).prop("title", "This quantity exceeds quantity on hand.");
    }
    else {
        $('#POQuantity' + _dynId).removeClass('red');
        $('#POQuantity' + _dynId).prop("title", "*");
    }
}

function ValidateQuantityOnPopup(obj, inHand) {

    var _qty = parseInt($(obj).val());
    var _inHand = parseInt(inHand);
    if (_qty > _inHand) {
        $(obj).addClass('red');
        $(obj).prop("title", "This quantity exceeds quantity on hand.");
    }
    else {
        $(obj).removeClass('red');
        $(obj).prop("title", "");
    }
}
function padLeft(s, len, c) {
    c = c || '0';
    while (s.length < len) s = c + s;
    return s;
}
function GetCardTypeById(id) {
    if (id == 0) {
        return 'Visa';
    }
    else if (id == 1) {
        return 'MasterCard';
    }
    else if (id == 2) {
        return 'Discover';
    }
    else if (id == 3) {
        return 'Amex';
    }
    else if (id == 4) {
        return 'Switch';
    }
    else if (id == 5) {
        return 'Solo';
    }
    else if (id == 6) {
        return 'Diners Club';
    }
    else if (id == 7) {
        return 'JCB';
    }
}
function GetAppointmentPurposeById(id) {
    if (id == 0) {
        return 'FACIAL';
    }
    else if (id == 1) {
        return 'ON_THE_GO';
    }
    else if (id == 2) {
        return 'DOUBLE_FACIAL';
    }
    else if (id == 3) {
        return 'CLASS';
    }
    else if (id == 4) {
        return 'TEAM_BUILDING';
    }
    else if (id == 5) {
        return 'TRAINING';
    }
    else if (id == 6) {
        return 'SHOW';
    }
    else if (id == 7) {
        return 'MEETING';
    }
    else if (id == 8) {
        return 'FOLLOW_UP';
    }
    else if (id == 9) {
        return 'CUSTOMER_SERVICE';
    }
    else if (id == 10) {
        return 'TWO_DAY_FOLLOWUP';
    }
    else if (id == 11) {
        return 'TWO_WEEK_FOLLOWUP';
    }
    else if (id == 12) {
        return 'TWO_MONTH_FOLLOWUP';
    }
    else if (id == 13) {
        return 'OTHER';
    }
    else if (id == 14) {
        return 'COLOR_APPT';
    }
    else if (id == 15) {
        return 'FAMILY';
    }
    else if (id == 16) {
        return 'BOOKING';
    }
    else if (id == 17) {
        return 'INIT_APPT';
    }
    else if (id == 18) {
        return 'RESCHEDULE';
    }
    else if (id == 19) {
        return 'FULLCIRCLE';
    }
}

function GetAppointmentPurposeByText(id) {
    if (id == 0) {
        return 'Facial';
    }
    else if (id == 1) {
        return 'On The Go';
    }
    else if (id == 2) {
        return 'Double Facial';
    }
    else if (id == 3) {
        return 'Class';
    }
    else if (id == 4) {
        return 'Team Building';
    }
    else if (id == 5) {
        return 'Training';
    }
    else if (id == 6) {
        return 'Show';
    }
    else if (id == 7) {
        return 'Meeting';
    }
    else if (id == 8) {
        return 'Follow Up';
    }
    else if (id == 9) {
        return 'Customer Service';
    }
    else if (id == 10) {
        return 'Two Day Followup';
    }
    else if (id == 11) {
        return 'Two Week Followup';
    }
    else if (id == 12) {
        return 'Two Month Followup';
    }
    else if (id == 13) {
        return 'Other';
    }
    else if (id == 14) {
        return 'Color Appt';
    }
    else if (id == 15) {
        return 'Family';
    }
    else if (id == 16) {
        return 'Booking';
    }
    else if (id == 17) {
        return 'Init Appt';
    }
    else if (id == 18) {
        return 'Reshedule';
    }
    else if (id == 19) {
        return 'Full Circle';
    }
}

function GetAppointmentPurposeByEnum(id) {
    if (id == "FACIAL") {
        return 'Facial';
    }
    else if (id == "ONTHEGO") {
        return 'On The Go';
    }
    else if (id == "DOUBLEFACIAL") {
        return 'Double Facial';
    }
    else if (id == "CLASS") {
        return 'Class';
    }
    else if (id == "TEAMBUILDING") {
        return 'Team Building';
    }
    else if (id == "TRAINING") {
        return 'Training';
    }
    else if (id == "SHOW") {
        return 'Show';
    }
    else if (id == "MEETING") {
        return 'Meeting';
    }
    else if (id == "FOLLOWUP") {
        return 'Follow Up';
    }
    else if (id == "CUSTOMERSERVICE") {
        return 'Customer Service';
    }
    else if (id == "TWODAYFOLLOWUP") {
        return 'Two Day Followup';
    }
    else if (id == "TWOWEEKFOLLOWUP") {
        return 'Two Week Followup';
    }
    else if (id == "TWOMONTHFOLLOWUP") {
        return 'Two Month Followup';
    }
    else if (id == "OTHER") {
        return 'Other';
    }
    else if (id == "COLOR_APPT") {
        return 'Color Appointment';
    }
    else if (id == "FAMILY") {
        return 'Family';
    }
    else if (id == "BOOKING") {
        return 'Booking';
    }
    else if (id == "INITAPPT") {
        return 'Init Appt';
    }
    else if (id == "REORDER") {
        return 'Re-Order';
    }
    else if (id == "FULLCIRCLE") {
        return 'Full Circle';
    }
    else if (id == "MISCELLANEOUS") {
        return 'Miscellaneous';
    }
    else if (id == "SEASONAL") {
        return 'Seasonal';
    }
    else if (id == "ONLINE") {
        return 'Online / Personal Website';
    }
    else if (id == "PREFERREDCUSTOMERPROGRAM") {
        return 'Preferred Customer program';
    }
}

function GetAppointmentTypeByText(id) {
    if (id == 0) {
        return 'CALENDAR';
    }
    else if (id == 1) {
        return 'APPT';
    }
    else if (id == 2) {
        return 'CALL';
    }
    else if (id == 3) {
        return 'PARTY';
    }
    else if (id == 4) {
        return 'TASK';
    }
    else if (id == 5) {
        return 'FAMY';
    }
    else if (id == 6) {
        return 'NONE';
    }
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function CreateFullCKEditor(id) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.extraPlugins = 'MediaEmbed,fileicon,tableresize';
    CKEDITOR.config.height = 420;
    CKEDITOR.config.width = '98%';
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.replace(id,
    {
        uiColor: '',
        filebrowserImageBrowseUrl: '/ResourceLib/ImageBrowser',
        filebrowserBrowseUrl: '/ResourceLib/DocumentBrowser',
        filebrowserWindowWidth: '910',
        filebrowserWindowHeight: '520',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview', '-', 'Templates'],
            ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor'],
            ['Image', 'Flash', 'MediaEmbed', 'fileicon', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
            '/',
            ['Styles', 'Format', 'Font', 'FontSize'],
            ['TextColor', 'BGColor'],
            ['Maximize', 'ShowBlocks']
        ]
    });
}

function CreateGalleryCKEditor(id) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.extraPlugins = 'MediaEmbed,fileicon,GalleryEmbed,AudioEmbed';
    CKEDITOR.config.height = 420;
    CKEDITOR.config.width = '98%';
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.replace(id,
    {
        uiColor: '',
        filebrowserImageBrowseUrl: '/ResourceLib/ImageBrowser',
        filebrowserBrowseUrl: '/ResourceLib/DocumentBrowser',
        filebrowserWindowWidth: '910',
        filebrowserWindowHeight: '520',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview', '-', 'Templates'],
            ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor'],
            ['Image', 'Flash', 'MediaEmbed', 'fileicon', 'GalleryEmbed', 'AudioEmbed', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
            '/',
            ['Styles', 'Format', 'Font', 'FontSize'],
            ['TextColor', 'BGColor'],
            ['Maximize', 'ShowBlocks']
        ]
    });
}

function CreateBasicCKEditor(id) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview'],
            ['Cut', 'Copy', 'Paste', 'PasteText', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor'],
            ['Image', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
            '/',
            ['Styles', 'Format', 'Font', 'FontSize'],
            ['TextColor', 'BGColor']
        ]
    });
}

function CreateBasicCKEditorHtWidth(id, height, width) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.height = height;
    CKEDITOR.config.width = width;
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.config.startupFocus = true;
    CKEDITOR.config.baseFloatZIndex = 9999;
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview'],
            ['Cut', 'Copy', 'Paste', 'PasteText', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor'],
            ['Image', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
            '/',
            ['Styles', 'Format', 'Font', 'FontSize'],
            ['TextColor', 'BGColor']
        ]
    });
}


function CreateSimpleCKEditorHtWidth(id, height, width) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.height = height;
    CKEDITOR.config.width = width;
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview'],
            ['Cut', 'Copy', 'Paste', 'PasteText', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor']
        ]
    });
}

function CreateCKEditorWithoutForms(id) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.extraPlugins = 'MediaEmbed,fileicon,tableresize';
    CKEDITOR.config.height = 420;
    CKEDITOR.config.width = '98%';
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        filebrowserImageBrowseUrl: '/ResourceLib/ImageBrowser',
        filebrowserBrowseUrl: '/ResourceLib/DocumentBrowser',
        filebrowserWindowWidth: '910',
        filebrowserWindowHeight: '520',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview', '-', 'Templates'],
            ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor'],
            ['Image', 'Flash', 'MediaEmbed', 'fileicon', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
            '/',
            ['Styles', 'Format', 'Font', 'FontSize'],
            ['TextColor', 'BGColor'],
            ['Maximize', 'ShowBlocks']
        ]
    });
}

function CreateCKEditorWithoutFormsHtWidth(id, height, width) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.extraPlugins = 'MediaEmbed,fileicon,tableresize';
    CKEDITOR.config.height = height;
    CKEDITOR.config.width = width;
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        filebrowserImageBrowseUrl: '/ResourceLib/ImageBrowser',
        filebrowserBrowseUrl: '/ResourceLib/DocumentBrowser',
        filebrowserWindowWidth: '910',
        filebrowserWindowHeight: '520',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview', '-', 'Templates'],
            ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor'],
            ['Image', 'Flash', 'MediaEmbed', 'fileicon', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
            '/',
            ['Styles', 'Format', 'Font', 'FontSize'],
            ['TextColor', 'BGColor'],
            ['Maximize', 'ShowBlocks']
        ]
    });
}

function CreateBasicPlainCKEditorHtWidth(id, height, width) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.height = height;
    CKEDITOR.config.width = width;
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.config.scayt_autoStartup = false;
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        toolbar:
        [
          ['Source']
        ]
    });
}

function ImportFromIntouchEditor(id, height, width) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.height = height;
    CKEDITOR.config.width = width;
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.config.scayt_autoStartup = false;
    CKEDITOR.replace(id,
    {
        on:
		{
		    paste: function(ev) {
		        ev.data.html = $('<div>').append($(ev.data.html).find("#tblOrders").clone()).remove().html();
		    }
		},
        uiColor: '#fdd1ad',
        toolbar:
        [
          ['Source']
        ]
    });
}
function CreateCutPasteCKEditorHtWidth(id, height, width) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.height = height;
    CKEDITOR.config.width = width;
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.config.scayt_autoStartup = false;
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        toolbar:
        [
          ['Source'],
           ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat']
        ]
    });
}


function CreateSignatureCKEditorHtWidth(id, height, width) {
    if (CKEDITOR.instances[id]) {
        delete CKEDITOR.instances[id];
    }
    CKEDITOR.config.height = height;
    CKEDITOR.config.width = width;
    CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
    CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
    CKEDITOR.replace(id,
    {
        uiColor: '#fdd1ad',
        filebrowserImageBrowseUrl: '/ResourceLib/ImageBrowser',
        filebrowserBrowseUrl: '/ResourceLib/DocumentBrowser',
        filebrowserWindowWidth: '910',
        filebrowserWindowHeight: '520',
        toolbar:
        [
            ['Source', '-', 'NewPage', 'Preview', '-', 'Templates'],
            ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker'],
            ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],
            ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv'],
            ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
            ['Link', 'Unlink', 'Anchor'],
            ['Image', 'Flash', 'MediaEmbed', 'fileicon', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
             '/',
            ['Styles', 'Format', 'Font', 'FontSize'],
            ['TextColor', 'BGColor']

        ]
    });
}

function PositionSubMenu(subMenuId, mainMenuId) {
    var p = $("#" + mainMenuId);
    if (p.length) {
        var position = p.position();
        var posLeft = position.left;
        if (posLeft > ((screen.width / 2) - 100)) {
            posLeft = posLeft - 100;
        }
        $('#' + subMenuId).css({ 'padding-left': posLeft + "px" }).show();
    }
}

function SelectContact(id, text) {
    $('input:hidden[id^=hHostessValue]').val(text);
    $('input:text[id^=HostessValue]').val(text);

    $('input:hidden[id^=hHostessValueId]').val(id);
    $('input:hidden[id^=HostessValueId]').val(id);

    $.fancybox.close();
}

function SelectAppointment(id, text, purpose, cid, cName) {
    var $saleType;
    if ($('#InvoiceSaleType').length) {
        $saleType = $('#InvoiceSaleType');
    }
    else if ($('#SalesReceiptSaleType').length) {
        $saleType = $('#SalesReceiptSaleType');
    }

    if (cName != '') {
        //$('input:hidden[id^=hHostessValue]').val(cName);
        //$('input:text[id^=HostessValue]').val(cName);
        $('#hHostessValue').val(cName);
        $('#HostessValue').val(cName);
    }
    if (cid > 0) {
        //$('input:hidden[id^=hHostessValueId]').val(cid);
        //$('input:hidden[id^=HostessValueId]').val(cid);
        $('#hHostessValueId').val(cid);
        $('#HostessValueId').val(cid);
    }
    var _aid = $('#AppointmentValueId').val();
    //$('input:hidden[id^=hAppointmentValue]').val(text);
    //$('input:text[id^=AppointmentValue]').val(text);
    $('#hAppointmentValue').val(text);
    $('#AppointmentValue').val(text);
    //$('input:hidden[id^=hAppointmentValueId]').val(id);
    //$('input:hidden[id^=AppointmentValueId]').val(id);
    $('#hAppointmentValueId').val(id);
    $('#AppointmentValueId').val(id);

    if (purpose == 'FACIAL') {
        $saleType.val('FACIAL');
    }
    else if (purpose == 'ON_THE_GO') {
        $saleType.val('ONTHEGO');
    }
    else if (purpose == 'DOUBLE_FACIAL') {
        $saleType.val('DOUBLEFACIAL');
    }
    else if (purpose == 'COLOR_APPT') {
        $saleType.val('COLOR_APPT');
    }
    else if (purpose == 'CLASS') {
        $saleType.val('CLASS');
    }
    else if (purpose == 'SHOW') {
        $saleType.val('SHOW');
    }
    $.fancybox.close();
    
}


function ShowHideApptHostessFields(disableLookups) {
    var _saleType = '';
    if ($('#InvoiceSaleType').length) {
        _saleType = $('#InvoiceSaleType').val();
    }
    else if ($('#SalesReceiptSaleType').length) {
        _saleType = $('#SalesReceiptSaleType').val();
    }

    if (_saleType == 'ONTHEGO') {
        $('tr.trAppointment').show();
        $('tr.trHostessValue').show();
        $('#lAppointmentValue').show();
        $('#lAppointmentValue').html('Appointment:');
        $('#tdlAppointmentValue').show();
        $('#tdfAppointmentValue').show();
        $('#tdlHostessValue').hide();
        $('#tdfHostessValue').hide();
    }
    else if (_saleType == 'DOUBLEFACIAL') {
        $('tr.trAppointment').show();
        $('tr.trHostessValue').show();
        $('#lAppointmentValue').show();
        $('#lAppointmentValue').html('Appointment:');
        $('#tdlAppointmentValue').show();
        $('#tdfAppointmentValue').show();
        $('#tdlHostessValue').show();
        $('#tdfHostessValue').show();
    }
    else if (_saleType == 'CLASS' || _saleType == 'SHOW') {
        $('tr.trAppointment').show();
        $('tr.trHostessValue').show();
        $('#lAppointmentValue').show();
        $('#lAppointmentValue').html('Party:');
        $('#tdlAppointmentValue').show();
        $('#tdfAppointmentValue').show();
        $('#tdlHostessValue').show();
        $('#tdfHostessValue').show();
    }
    else {
        $('tr.trAppointment').hide();
        $('tr.trHostessValue').hide();
    }
    if (disableLookups == true) {
        $('#btn_app_sel').hide();
        $('#btn_app_clr').hide();
        $('#btn_host_sel').hide();
        $('#btn_host_clr').hide();
        $('#AppointmentValue').prop("disabled", "disabled");
        $('#HostessValue').prop("disabled", "disabled");
    }
}

function OpenLocation(loc) {
    top.location.href = loc;
}

function FormatCurrencyWithCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function isdefined(variable) {
    return (typeof (window[variable]) == "undefined") ? false : true;
}

function IsValidEmail(email) {
    email = email.toLowerCase();
    var filter = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/;
    return filter.test(email);
}

function CallFancyBox(width, height, title, href) {
    $.fancybox({
        'width': width,
        'height': height,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true,
        'overlayOpacity': '0.9',
        'autoScale': false,
        'autoDimensions': false,
        'title': title,
        'titleShow': true,
        'titlePosition': 'over',
        'hideOnOverlayClick': false,
        'centerOnScroll': false,
        'href': href,
        'onComplete': function() {
            $("#fancybox-wrap").css({ 'top': '5px', 'bottom': 'auto' });
            $("#fancybox-content").css({ 'padding-top': '35px' });
        }
    });
}
