﻿var ReturnPath = ""
//Move to external file
$(function() {
    //setup modal window
    $('#widgetTempDiv').dialog({ autoOpen: false, modal: true, width: 400 });

    //setup event
    if (TargetElement == '') {
        if ((typeof (DelayForm) != 'undefined') || (DelayForm != "")) {
            $.cookie("VisitCount", null);
            setTimeout(DisplayForm, DelayForm);
        }
        else {
            DisplayForm();
        }

    } else {
        if ((typeof (iFrameLoad) == 'undefined') || (iFrameLoad == "0")) {
            $(TargetElement).click(function() {
                return DisplayForm();
            });
        } else {
            $(TargetElement).load(function() {
                return DisplayForm();
            });
        }

    }


    function DisplayForm() {

        if ((typeof (VisitCount) != 'undefined') && (!$.cookie("isKnown"))) {
            var vc = 0;
            if ($.cookie("VisitCount")) { vc = $.cookie("VisitCount"); }

            if (vc >= VisitCount) {
                //set return path 
                ReturnPath = "";
                if (useReturnPath == "1") {
                    ReturnPath = $(this).attr("href");
                }
                $.get(FormToLoad, function(data) {
                    $("#widgetTempDiv").html("");
                    $("#widgetTempDiv").append(data);
                });
                $('#widgetTempDiv').dialog('open');
                return false;

            } else {
                //Set visit count cookie
                $.cookie('VisitCount', +vc + 1, { path: '/', expires: 1000 });
                return true;
            }

        } else {
            //open modal window
            if (!$.cookie("isKnown")) {

                //set return path 
                ReturnPath = "";
                if (useReturnPath == "1") {
                    ReturnPath = $(this).attr("href");
                }
                $.get(FormToLoad, function(data) {
                    $("#widgetTempDiv").html("");
                    $("#widgetTempDiv").append(data);
                });
                $('#widgetTempDiv').dialog('open');
                return false;
            }
        }

    }
});