//@@FORMSWORKFLOW auto-generated script

/**************************************************************
AdobePatentID="B643"
**************************************************************/
var AdobePatentID = "AdobePatentID=\"B643\"";

// Show a dialog to ask the user's name and email during submit.
askUserIdentity = function(doc)
{
    // string constants
    var dlgStrings =
    {
        sDlgTitle:          "%STR_SUBMIT_DLG_TITLE%",
        sEmailLabel:        "%STR_EMAIL_LABEL%",
        sNameLabel:         "%STR_NAME_LABEL%",
        sSubmitLabel:       "%STR_SUBMIT%",
        sToLabel:           "%STR_TO_LABEL%",
        sSubjectLabel:      "%STR_SUBJECT_LABEL%",
        sSubjectContent:    "%STR_SUBJECT_CONTENT%",
        sAttachmentLabel:   "%STR_ATTACHMENT_LABEL%",
        sFromLabel:         "%STR_FROM_LABEL%",
        sErrorEmailRequired:"%STR_ERROR_EMAIL_REQUIRED%",
        sErrorNameRequired: "%STR_ERROR_NAME_REQUIRED%"
    };
    
    var dlgElems = [];
    dlgElems[dlgElems.length] = {
        type: "view",
        align_children: "align_left",
        elements:
        [
            {
                type: "view",
                align_children: "align_row",
                elements:
                [
                    {
                        type: "static_text",
                        name: dlgStrings.sToLabel,
                        width: 80,
                        bold: true,
                        font: "dialog"
                    },
                    {
                        type: "static_text",
                        item_id: "sdto",
                        width: 270,
                        font: "dialog"
                    }
                ]
            },
            {
                type: "view",
                align_children: "align_row",
                elements:
                [
                    {
                        type: "static_text",
                        name: dlgStrings.sSubjectLabel,
                        bold: true,
                        width: 80,
                        font: "dialog"
                    },
                    {
                        type: "static_text",
                        name: dlgStrings.sSubjectContent,
                        width: 270,
                        font: "dialog"
                    }
                ]
            },
            {
                type: "view",
                align_children: "align_row",
                elements:
                [
                    {
                        type: "static_text",
                        name: dlgStrings.sAttachmentLabel,
                        bold: true,
                        width: 80,
                        font: "dialog"
                    },
                    {
                        type: "static_text",
                        item_id: "atch",
                        width: 270,
                        font: "dialog"
                    }
                ]
            },
            {
                type: "gap",
                height: 5,
                width: 350
            },
            {
                type: "static_text",
                name: dlgStrings.sFromLabel,
                bold: true,
                font: "dialog"
            },
            {
                type: "static_text",
                name: dlgStrings.sEmailLabel,
                bold: true,
                font: "dialog"
            },
            {
                type: "edit_text",
                item_id: "emal",
                width: 250
            },
            {
                type: "static_text",
                name: dlgStrings.sNameLabel,
                bold: true,
                font: "dialog"
            },
            {
                type: "edit_text",
                item_id: "name",
                width: 250
            },
            {
                type: "gap",
                height: 5,
                width: 350
            },
            {
                type: "ok_cancel",
                ok_name: dlgStrings.sSubmitLabel
            }
        ]
    }
    
    var desc = {
        resourceContext: "AcroForm",
        description:  {
            name: dlgStrings.sDlgTitle,
            elements: [
                {
                    type: "view",
                    align_children: "align_left",
                    elements: dlgElems
                }
            ]
        }
    }

    desc.initialize = function(dialog)
    {
        var METADATA_ANNOT_NAME = "adhocFormState";
        var PROP_INITIATOR = "initiator";
        
        var initiator;
        var annot = doc.getAnnot(0, METADATA_ANNOT_NAME);
        if (annot != null) {
            var arrProps = new Array();
            arrProps = annot.contents.split(";");
            initiator = getProperty(arrProps, PROP_INITIATOR);
        }
        
        var decodedFileName = unescape(doc.documentFileName);
        var initVars =
        {
            sdto: initiator,
            atch: decodedFileName
        }
        dialog.load(initVars);
    }

    desc.validate = function(dialog)
    {
        var values = dialog.store();
        if (values.emal == "") {
            app.alert(dlgStrings.sErrorEmailRequired);
            return false;
        }
        if (values.name == "") {
            app.alert(dlgStrings.sErrorNameRequired);
            return false;
        }
        return true;
    };
        
    desc.commit = function(dialog)
    {
        this.result = new Object;
        var values = dialog.store();
        this.result.name = values.name;
        this.result.email = values.emal;
    };

    var ret = app.execDialog(desc);
    if (ret == "ok")
        return desc.result;
    else
        return null;
};

askEmailClient = function(doc)
{
// string constants
    var dlgStrings =
    {
        sDlgTitle:      "%STR_CLIENT_DLG_TITLE%",
        sDlgText:       "%STR_CLIENT_DLG_TEXT%",
        sDesktop:       "%STR_DESKTOP%",
        sDesktopDesc:   "%STR_DESKTOP_DESC%",
        sInternet:      "%STR_INTERNET%",
        sInternetDesc:  "%STR_INTERNET_DESC%"
    };
    
    var dlgElems = [];
    dlgElems[dlgElems.length] = {
        type: "view",
        align_children: "align_left",
        alignment: "align_fill",
        elements:
        [
            {
                type: "static_text",
                name: dlgStrings.sDlgText,
                width: 350,                
                font: "dialog"
            },
            {
                type: "radio",
                name: dlgStrings.sDesktop,
                item_id: "rad1",
                group_id: "rgrp",
                bold: true,
                font: "dialog"
            },
            {
                type: "view",
                align_children: "align_top",
                elements:
                [
                    {
                        type: "gap",
                        width: 20
                    },
                    {
                        type: "static_text",
                        name: dlgStrings.sDesktopDesc,
                        wrap_name: true,
                        width: 342,
                        font: "dialog"
                    }
                ]
            },            
            {
                type: "radio",
                name: dlgStrings.sInternet,
                item_id: "rad2",
                group_id: "rgrp",
                bold: true,
                font: "dialog"

            },
            {
                type: "view",
                align_children: "align_top",
                elements:
                [
                    {
                        type: "gap",
                        width: 20
                    },
                    {
                        type: "static_text",
                        name: dlgStrings.sInternetDesc,
                        wrap_name: true,
                        width: 342,
                        font: "dialog"
                    }
                ]
            },
            {
                type: "gap",
                height: 5,
                width: 350
            },
            {
                type: "ok_cancel",
            }
        ]
    }
    
    var desc = {
        resourceContext: "AcroForm",
        description:  {
            name: dlgStrings.sDlgTitle,
            width: 250,
            elements: [
                {
                    type: "view",
                    align_children: "align_left",
                    elements: dlgElems
                }
            ]
        }
    }
    
    desc.commit = function(dialog)
    {
        this.result = new Object;
        var values = dialog.store();
        this.result.send = values.rad1;
        this.result.save = values.rad2;
    };

    var ret = app.execDialog(desc);
    if (ret == "ok")
        return desc.result;
    else
        return null;            
};

/**************************************************************
AdobePatentID="B643"
**************************************************************/
// set a new property value to a property in the specified array.
setProperty = function(arrProps, propName, propValue)
{
    var i;
    var bSet = false;
    for (i = 0; i < arrProps.length; i++) {
        var propParts = new Array();
        propParts = arrProps[i].split(":");
        if (propParts[0] == propName) {
            propParts[1] = propValue;
            arrProps[i] = propParts.join(":");
            bSet = true;
            break;
        }
    }
    
    if (!bSet)
        arrProps[arrProps.length] = propName + ":" + propValue;
};

/**************************************************************
AdobePatentID="B643"
**************************************************************/
// get the value for a property in the specified array.
getProperty = function(arrProps, propName)
{
    var i;
    for (i = 0; i < arrProps.length; i++) {
        var propParts = new Array();
        propParts = arrProps[i].split(":");
        if (propParts[0] == propName) {
            return propParts[1];
        }
    }
    return null;
};

/**************************************************************
AdobePatentID="B643"
**************************************************************/
showAutoGenSubmitBtn = function(bShow)
{
    var autoGenSubmitName = "%AUTO_GEN_SUBMIT_NAME%";
    var fld = this.getField(autoGenSubmitName);
    if (fld != null)
    {
        var b= this.dirty;
        if (bShow)
            fld.display = display.noPrint;
        else
            fld.display = display.hidden;
        this.dirty= b;
    }
};

// Hide the auto-generated submit button for Acrobat/Reader >= 9.0
if (app.viewerVersion >= 9)
    showAutoGenSubmitBtn(false);
else
    showAutoGenSubmitBtn(true);
