/**
 * jQuery Form Builder Plugin
 * Copyright (c) 2009 Mike Botsko, Botsko.net LLC (http://www.botsko.net)
 * http://www.botsko.net/blog/2009/04/jquery-form-builder-plugin/
 * Originally designed for AspenMSM, a CMS product from Trellis Development
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 */
(function ($) {
    $.fn.formbuilder = function (options) {

        // Extend the configuration options with user-provided
        var defaults =
        {
            definitionId: "",
            save_url: false,
            load_url: false,
            list_url: false,
            cancel_url: false,
            fieldNames: [],
            ListNames: []
        };
        var opts = $.extend(defaults, options);
        opts.fieldNames.splice(0, 0, { "text": "&lt;Custom Field&gt;", "value": "custom" });

        if (opts.ListNames == undefined)
            opts.ListNames = [];

        opts.ListNames.splice(0, 0, { "text": "&lt;Custom List&gt;", "value": "-1" });

        for (var f = 0; f < opts.fieldNames.length; f++)
            opts.fieldNames[f].used = false;

        return this.each(function () {

            // Create form control select box and add into the editor
            var controls = "";

            controls += "<span class=\"buttonsSection\"><a href=\"#\" rel=\"section\">Section</a></span>";
            controls += "<span class=\"buttonsText\"><a href=\"#\" rel=\"text\">Text</a></span>";
            controls += "<span class=\"buttonsParagraph\"><a href=\"#\" rel=\"div\">Paragraph</a></span>";
            controls += "<span class=\"buttonsMultiline\"><a href=\"#\" rel=\"textarea\">Multi-Line Text</a></span>";
            controls += "<span class=\"buttonsCheckbox\"><a href=\"#\" rel=\"checkbox\">Checkbox</a></span>";
            controls += "<span class=\"buttonsRadio\"><a href=\"#\" rel=\"radio\">Radio</a></span>";
            controls += "<span class=\"buttonsDropdown\"><a href=\"#\" rel=\"select\">DropDown</a></span>";
            controls += "<span class=\"buttonsDate\"><a href=\"#\" rel=\"date\">Date</a></span>";

            $('#FormTopControls').append(controls);

            controls = '<div class="FieldControls">' + controls + '</div>';

            $('#FormBottomControls').append(controls);

            controls = '';

            //controls += "<br />";
            controls += '<div class="buttonsSave">';
            controls += "<a id=\"save-form\"><span>Save</span></a>";
            controls += '</div>';
            controls += '<div class="buttonsCancel">';
            controls += "<a id=\"cancel-form\"><span>Cancel</span></a>";
            controls += '</div>';

            
            $('#FormBottomControls').append(controls);

            //$(this).before('<div class="field_control">' + controls + '</div>');

            $(".FieldControls a").click(function () {
                appendNewField($(this).attr('rel'));
                $.scrollTo($('#form-' + (last_id - 1) + '-item'), 800);
                return false;
            });

            // set the form save action
            $("#save-form").click(function () {
                save();
                return false;
            });

            // set the form cancel action
            $("#cancel-form").click(function () {
                window.location = opts.cancel_url;
                return false;
            });

            // Wrapper for adding a new field
            var appendNewField = function (type, values, options, required, name) {
                field = '';
                field_type = type;
                field_name = name;

                if (typeof (values) == 'undefined') { values = ''; }

                switch (type) {
                    case 'text':
                        appendTextInput(values, options, required);
                        break;
                    case 'textarea':
                        appendTextarea(values, options, required);
                        break;
                    case 'checkbox':
                        appendCheckboxGroup(values, options, required);
                        break;
                    case 'radio':
                        appendRadioGroup(values, options, required);
                        break;
                    case 'select':
                        appendSelectList(values, options, required);
                        break;
                    case 'section':
                        appendSectionDivider(values);
                        break;
                    case 'date':
                        appendDateInput(values, options, required);
                        break;
                    case 'div':
                        appendDiv(values, options, required);
                }
            }

            // Section Control
            var appendSectionDivider = function (values) {

                field += '<label>Label:</label>';
                field += '<input class="fld-title" id="title-' + last_id + '" type="text" value="' + values + '" />';
                help = '';

                appendFieldLi('Section Divider', field, 'false', help);

            }

            // Date Control
            var appendDateInput = function (values, options, required) {
                if (options == undefined) {
                    options = new Array();
                    options[0] = 'Outside';
                }
                field += '<label>Label:</label>';
                field += '<input class="fld-title" id="title-' + last_id + '" type="text" value="' + values + '" />';

                field += '<select class="lblPos" id="lblPos-' + last_id + '" name="lblPos-' + last_id + '">';
                field += '<option ' + (options[0] == 'Outside' ? " selected='selected'" : "") + ' value="Outside">Outside</option>';
                field += '<option ' + (options[0] == 'Inside' ? " selected='selected'" : "") + ' value="Inside">Inside</option>';
                field += '<option ' + (options[0] == 'Both' ? " selected='selected'" : "") + ' value="Both">Both</option>';
                field += '</select>';

                help = '';

                appendFieldLi('Date Picker', field, required, help);

            }

            // single line input type="text"
            var appendTextInput = function (values, options, required) {

                if (options == undefined) {
                    options = new Array();
                    options[0] = 'Outside';
                }
                field += '<label>Label:</label>';
                field += '<input class="fld-title" id="title-' + last_id + '" type="text" value="' + values + '" />';

                field += '<select class="lblPos" id="lblPos-' + last_id + '" name="lblPos-' + last_id + '">';
                field += '<option ' + (options[0] == 'Outside' ? " selected='selected'" : "") + ' value="Outside">Outside</option>';
                field += '<option ' + (options[0] == 'Inside' ? " selected='selected'" : "") + ' value="Inside">Inside</option>';
                field += '<option ' + (options[0] == 'Both' ? " selected='selected'" : "") + ' value="Both">Both</option>';
                field += '</select>';

                help = '';

                appendFieldLi('Text Field', field, required, help);

            }

            var appendDiv = function (values, options, required) {

                field += '<textarea class="fld-title" id="title-' + last_id + '" rows="6" cols="60">' + values + '</textarea>';

                help = '';

                appendFieldLi('Paragraph', field, required, help);
            }

            // multi-line textarea
            var appendTextarea = function (values, options, required) {

                if (options == undefined) {
                    options = new Array();
                    options[0] = 'Outside';
                    options[1] = 5;
                }

                field += '<div class="form-fld">';
                field += '<label>Label:</label>';
                field += '<input type="text" value="' + values + '" />';

                field += '<select class="lblPos" id="lblPos-' + last_id + '" name="lblPos-' + last_id + '">';
                field += '<option ' + (options[0] == 'Outside' ? " selected='selected'" : "") + ' value="Outside">Outside</option>';
                field += '<option ' + (options[0] == 'Inside' ? " selected='selected'" : "") + ' value="Inside">Inside</option>';
                field += '<option ' + (options[0] == 'Both' ? " selected='selected'" : "") + ' value="Both">Both</option>';
                field += '</select>';

                field += '</div>';

                field += '<div class="form-fld">';
                field += '<label>Rows:</label>';

                field += '<select class="height" id="height-' + last_id + '" name="height-' + last_id + '">';

                for (var i = 3; i <= 10; i++) {
                    field += '<option ' + (options[1] == i ? " selected='selected'" : "") + ' value="' + i + '">' + i + '</option>';
                }

                field += '</select>';
                field += '</div>';

                help = '';

                appendFieldLi('Multiline Text Field', field, required, help);

            }

            // adds a checkbox element
            var appendCheckboxGroup = function (values, options, required) {

                var title = '';
                if (typeof (options) == 'object') {
                    title = options[0];
                }

                field += '<div class="chk_group">';
                field += '<div class="form-fld"><label>Label:</label>';
                field += '<input type="text" name="title" value="' + title + '" /></div>';
                field += '<div class="false-label">Select Options</div>';
                field += '<div class="fields">';

                if (typeof (values) == 'object') {
                    $.each(values, function (index, c) {
                        field += checkboxFieldHtml(c);
                    });
                } else {
                    field += checkboxFieldHtml('');
                }

                field += '<div class="add-area"><a href="#" class="add add_ck"><span>Add</span></a></div>';
                field += '</div>';
                field += '</div>';

                help = '';
                appendFieldLi('Checkbox Group', field, required, help);

                $('.add_ck').live('click', function () {
                    $(this).parent().before(checkboxFieldHtml());
                    return false;
                });
            }

            // Checkbox field html, since there may be multiple
            var checkboxFieldHtml = function (values) {

                var checked = false;

                if (typeof (values) == 'object') {
                    var value = values[0];
                    checked = values[1] == false ? false : true;
                } else {
                    var value = '';
                }

                field = '';
                field += '<div>';
                field += '<input type="checkbox"' + (checked ? ' checked="checked"' : '') + ' /><input type="text" value="' + value + '" />';
                field += '<a href="#" class="remove" title="Are you sure you want to remove this checkbox?"><span>Remove</span></a>';
                field += '</div>';

                return field;

            }

            // adds a radio element
            var appendRadioGroup = function (values, options, required) {

                var title = '';
                if (typeof (options) == 'object') { title = options[0]; }

                field += '<div class="rd_group">';
                field += '<div class="form-fld"><label>Label:</label>';
                field += '<input type="text" name="title" value="' + title + '" /></div>';
                field += '<div class="false-label">Select Options</div>';
                field += '<div class="fields">';

                if (typeof (values) == 'object') {
                    $.each(values, function (index, c) {
                        field += radioFieldHtml(c, 'form-' + last_id + '-fld');
                    });

                } else {
                    field += radioFieldHtml('', 'form-' + last_id + '-fld');
                }

                field += '<div class="add-area"><a href="#" class="add add_rd"><span>Add</span></a></div>';
                field += '</div>';
                field += '</div>';
                help = '';

                appendFieldLi('Radio Group', field, required, help);

                $('.add_rd').live('click', function () {
                    $(this).parent().before(radioFieldHtml(false, $(this).parents('.form-holder').attr('id')));
                    return false;
                });
            }

            // Radio field html, since there may be multiple
            var radioFieldHtml = function (values, name) {

                var checked = false;

                if (typeof (values) == 'object') {
                    var value = values[0];
                    checked = values[1] == false ? false : true;
                } else {
                    var value = '';
                }

                field = '';
                field += '<div>';
                field += '<input type="radio"' + (checked ? ' checked="checked"' : '') + ' name="radio_' + name + '" /><input type="text" value="' + value + '" />';
                field += '<a href="#" class="remove" title="Are you sure you want to remove this radio button option?"><span>Remove</span></a>';
                field += '</div>';

                return field;

            }

            // adds a select/option element
            var appendSelectList = function (values, options, required) {

                var multiple = false;
                var listid = -1;
                var title = '';
                if (typeof (options) == 'object') {
                    title = options[0];
                    multiple = options[1] == true ? true : false;
                    listid = options[2];
                }

                field += '<div class="opt_group">';
                field += '<div class="form-fld"><label>Label:</label>';
                field += '<input type="text" name="title" value="' + title + '" /> </div>';

                field += '<br />';
                field += '<div class="form-fld"><label>Allow Multiple:</label>';
                field += '<input type="checkbox" name="multiple"' + (multiple ? 'checked="checked"' : '') + '></div>';
                field += '<br />';

                field += '<div class="form-fld"><label>Options:</label>';
                field += '<select class="listname">';

                for (var f = 0; f < opts.ListNames.length; f++) {
                    field += '<option value="' + opts.ListNames[f].value + '"' + (listid == opts.ListNames[f].value ? 'selected=selected' : '') + '>' + opts.ListNames[f].text + '</option>';
                }

                field += '</select></div>';

                field += '<div class="form-fld" ' + (listid > -1 ? 'style="display:none"' : '') + '><label>&nbsp;</label>';
                field += '<div class="fields">';

                field += '<div>';

                if (typeof (values) == 'object') {
                    $.each(values, function (index, c) {
                        field += selectFieldHtml(c, multiple);
                    });

                } else {
                    field += selectFieldHtml('', multiple);
                }

                field += '<div class="add-area"><a href="#" class="add add_opt"><span>Add</span></a></div>';
                field += '</div>';
                field += '</div>';

                field += '</div>';
                field += '</div>';
                help = '';

                appendFieldLi('Dropdown List', field, required, help);

                $('.add_opt').live('click', function () {
                    $(this).parent().before(selectFieldHtml('', multiple));
                    return false;
                });
            }

            // Select field html, since there may be multiple
            var selectFieldHtml = function (values, multiple) {
                if (multiple) {
                    return checkboxFieldHtml(values);
                } else {
                    return radioFieldHtml(values);
                }
            }


            // Appends the new field markup to the editor
            var appendFieldLi = function (title, field_html, required, help) {

                if (required) { required = required == true ? true : false; }

                var li = '';
                li += '<li id="form-' + last_id + '-item" class="' + field_type + '">';
                li += '<div class="legend"><strong id="txt-title-' + last_id + '">' + title + '</strong></div>';
                li += '<div id="form-' + last_id + '-fld" class="form-holder">';
                li += '<div class="form-elements">';

                if (field_type != 'section' && field_type != 'div') {

                    li += '<div class="form-fld">';
                    li += '<label for="fieldname-' + last_id + '">Field:</label> <select class="fieldname" id="fieldname-' + last_id + '" name="fieldname-' + last_id + '">';
                    for (var f = 0; f < opts.fieldNames.length; f++) {
                        if (!opts.fieldNames[f].used) {
                            li += '<option value="' + opts.fieldNames[f].value + '" ' + (field_name == opts.fieldNames[f].value ? 'selected="selected"' : '') + '>' + opts.fieldNames[f].text + '</option>';

                            if (opts.fieldNames[f].value != "custom" && field_name == opts.fieldNames[f].value)
                                opts.fieldNames[f].used = true;
                        }
                    }
                    li += '</select>';
                    li += '&nbsp;&nbsp;&nbsp;<input class="required" type="checkbox" value="1" name="required-' + last_id + '" id="required-' + last_id + '"' + (required ? ' checked="checked"' : '') + ' /> Required';
                    li += '</div>';

                }

                li += field;
                li += '</div>';
                li += '<div class="buttonsDeleteField"><a id="del_' + last_id + '" class="del-button delete-confirm" href="#"  title="Are you sure you want to delete this form section?"><span>Delete</span></a></div>';
                li += '</div>';
                li += '</li>';

                $(ul_obj).append(li);

                last_id++;
            }

            $('.listname').live('change', function (e) {

                if (e.currentTarget.value == -1)
                    $(this).parent().next().show('fast');
                else
                    $(this).parent().next().hide('fast');
            });

            $('.fieldname').live('change', function () {

                UpdateFieldNames();

            });

            var UpdateFieldNames = function () {

                // Reset used state for all options
                for (var f = 0; f < opts.fieldNames.length; f++) {
                    opts.fieldNames[f].used = false;
                }

                // Enumerate all dropdowns and detect used state
                $('.fieldname').each(function () {
                    for (var f = 0; f < opts.fieldNames.length; f++) {
                        if (opts.fieldNames[f].value == "custom" || opts.fieldNames[f].value == "Customer.Comments") continue;
                        if (opts.fieldNames[f].value == $(this).val()) {
                            opts.fieldNames[f].used = true;

                            //Required Field Names
                            //                            if (opts.fieldNames[f].value == "Customer.FirstName" || opts.fieldNames[f].value == "Customer.LastName" || opts.fieldNames[f].value == "Customer.Email") {
                            //                                $(this).attr("disabled", "disabled").next().attr("checked", "checked").attr("disabled", "disabled");
                            //                                $('#' + $(this).attr("id").replace("fieldname-", "del_") + ' span').remove();
                            //                            }
                            break;
                        }
                    }
                });

                // Repopulate dropdowns
                $('.fieldname').each(function () {
                    var currVal = $(this).val();
                    $(this).children().remove();
                    for (var f = 0; f < opts.fieldNames.length; f++) {
                        if (currVal == opts.fieldNames[f].value)
                            $(this).append("<option value='" + opts.fieldNames[f].value + "' selected='selected'>" + opts.fieldNames[f].text + "</option>");
                        else {
                            if (!opts.fieldNames[f].used)
                                $(this).append("<option value='" + opts.fieldNames[f].value + "'>" + opts.fieldNames[f].text + "</option>");
                        }
                    }
                });

            }
            // handle field delete links
            $('.remove').live('click', function () {
                $(this).parent('div').animate({ opacity: 'hide', height: 'hide', marginBottom: '0px' }, 'fast', function () {
                    $(this).remove();
                });
                return false;
            });


            // handle delete confirmation
            $('.delete-confirm').live('click', function () {
                var delete_id = $(this).attr("id").replace(/del_/, '');
                if (confirm($(this).attr('title'))) {
                    $('#fieldname-' + delete_id).val("custom");
                    $('#fieldname-' + delete_id).trigger("change");
                    $('#form-' + delete_id + '-item').animate({ opacity: 'hide', height: 'hide', marginBottom: '0px' }, 'slow', function () {
                        $(this).remove();
                    });
                }
                return false;
            });

            // saves the serialized data to the server 
            var save = function () {

                //Validate
                var IsValid = true;
                $(ul_obj).children().each(function () {

                    if ($('#' + $(this).attr('id') + ' input[type=text]').val() == '') {
                        alert('Labels are required for all fields');
                        $('#' + $(this).attr('id') + ' input[type=text]').focus();
                        IsValid = false;
                        return false;
                    }

                    if ($('#' + $(this).attr('id') + ' input[type=text]').val() != '') {
                        var pattern = new RegExp(/<[^<]+?>|&|%|<|>/);
                        if (pattern.test($('#' + $(this).attr('id') + ' input[type=text]').val())) {
                            alert('Labels cannot contain HTML Tags or any other special characters such as <, >, &, and %');
                            $('#' + $(this).attr('id') + ' input[type=text]').focus();
                            IsValid = false;
                            return false;
                        }
                    }

                    if ($('#' + $(this).attr('id') + ' textarea').val() != '') {
                        var pattern = new RegExp(/<[^<]+?>|&|%|<|>/);
                        if (pattern.test($('#' + $(this).attr('id') + ' textarea').val())) {
                            alert('Labels cannot contain HTML Tags or any other special characters such as <, >, &, and %');
                            $('#' + $(this).attr('id') + ' textarea').focus();
                            IsValid = false;
                            return false;
                        }
                    }
                });

                if (IsValid) {
                    if (opts.save_url) {
                        $.ajax({
                            type: "POST",
                            url: opts.save_url,
                            data: $(ul_obj).jsonifyFormList(),
                            success: function (xml) {
                                $('#FormMessage').text('Form saved successfully!');
                            }
                        });
                    }
                }
                else
                    $('#FormMessage').text('');
            }


            $(this).append("<ul class=\"form-builder\"></ul>");
            var ul_obj = $(".form-builder");
            var field = '';
            var field_type = '';
            var last_id = 1;


            // Load definition from a URL
            if (opts.load_url) {
                $.getJSON(opts.load_url, function (data) {
                    if (data != null) {

                        var values = '';
                        var options = false;
                        var requried = false; // = data[0].class;

                        $.each(data, function (index, field) {

                            if ((field.datatype == 'checkbox') || (field.datatype == 'radio')) {
                                options = new Array;
                                options[0] = field.label;
                                values = new Array;
                                for (var a = 0; a < field.options.length; a++) {
                                    var option = field.options[a];
                                    values[a] = new Array(2);
                                    values[a][0] = option.value;
                                    values[a][1] = option.checked;
                                };
                            }

                            // select type
                            else if (field.datatype == 'select') {
                                options = new Array;
                                options[0] = field.label;
                                options[1] = field.multiple;
                                options[2] = field.listid;

                                values = new Array;
                                for (var a = 0; a < field.options.length; a++) {
                                    var option = field.options[a];
                                    values[a] = new Array(2);
                                    values[a][0] = option.value;
                                    values[a][1] = option.checked;
                                };
                            }
                            else if (field.datatype == 'textarea') {
                                values = field.label;
                                options = new Array();
                                options[0] = field.labelPos;
                                options[1] = field.rows;
                            }
                            else if (field.datatype == 'text' || field.datatype == 'date') {
                                values = field.label;
                                options = new Array();
                                options[0] = field.labelPos;
                            }
                            else if (field.datatype == 'div')
                                values = field.innerText;
                            else
                                values = field.label;

                            appendNewField(field.datatype, values, options, field.required, field.name);

                        });

                        UpdateFieldNames();

                    }
                });
            }
        });
    };
})(jQuery);



(function($){
	$.fn.jsonifyFormList = function(options) {
		// Extend the configuration options with user-provided
		var defaults = {
			attributes: ['class']
		};
		var opts = $.extend(defaults, options);
        var fields = "";
        var fieldId = 1;
		var sep = "";
		
		// Begin the core plugin
		this.each(function() {
			//var ul_obj = this;

            var field = {};
			$(this).children().each(function(){
				
                for(att in opts.attributes){
					// append the form field values
					if(opts.attributes[att] == 'class'){
						field['datatype'] = $(this).attr(opts.attributes[att]);
						field["required"] = ($('#'+$(this).attr('id')+' input.required').attr('checked') == 'checked') ? true : false;
                        field["name"] =  $('#'+$(this).attr('id')+' select.fieldname').val();
				
						switch($(this).attr(opts.attributes[att])){
                          case 'date':
                                field["label"] = $('#'+$(this).attr('id')+' input[type=text]').val();
                                field["labelPos"] =  $('#'+$(this).attr('id')+' select.lblPos').val();
								break;
                            case 'section':
                                field["label"] = $('#'+$(this).attr('id')+' input[type=text]').val();
								break;
                            case 'div':
                                field["innerText"] = $('#'+$(this).attr('id')+' textarea').val();
								break;
							case 'text':
								field["label"] = $('#'+$(this).attr('id')+' input[type=text]').val();
                                field["labelPos"] =  $('#'+$(this).attr('id')+' select.lblPos').val();
								break;
							case 'textarea':
								field["label"] = $('#'+$(this).attr('id')+' input[type=text]').val();
                                field["rows"] =  $('#'+$(this).attr('id')+' select.height').val();
                                field["labelPos"] =  $('#'+$(this).attr('id')+' select.lblPos').val();
								break;
							case 'checkbox':
							case 'radio':
                                field["options"] = [];
								$('#'+$(this).attr('id')+' input[type=text]').each(function(){
    								if( $(this).attr("name") == "title") 
                                       field["label"]= $(this).val();
									else 
                                        field.options.push( { "value" : $(this).val(), "checked" : $(this).prev().attr('checked')});
								});
								break;
							case 'select':
                                field["options"] = [];
								field["multiple"]=$('#'+$(this).attr('id')+' input[name=multiple]').attr('checked');
                                
                                field["listid"] = $('#'+$(this).attr('id')+' .listname').val();
                                								
								$('#'+$(this).attr('id')+' input[type=text]').each(function(){
									
									if($(this).attr('name') == 'title')
										field["label"]=$(this).val();
									 else 
                                        field.options.push( { "value" : $(this).val(), "checked" : $(this).prev().attr('checked')});
								});
							break;
						}
					}
				}

                field['id'] = 'fmf' + fieldId;
                fieldId++;

                fields += sep + JSON.stringify(field);
                if (sep == "") sep = ", ";

                field = {};
			});

		});
		return("[" + fields + "]");
	};
})(jQuery);


(function($){
	$.fn.formrenderer = function(options) {
		// Extend the configuration options with user-provided
        var defaults =
        {
            definitionId: "",
            list_url: false
        };
		var opts = $.extend(defaults, options);
		
		// Begin the core plugin
		this.each(function() {
            $(this).append('<fieldset class="gmvBodyText"></fieldset>');
            var container = $('fieldset',$(this));
            
            var counter = 0;
            var data = null;

            if (opts.load_url){
            // Load definition from a URL
                $.getJSON(opts.load_url, function (d) { data = d; });
            }
            else if (opts.definitionId){
            // Load definition from an existing element

            // NOTE: Running "eval" on untrusted code is dangerous
            // This assumes that the field referenced by opts.definitionId
            // is populated from a trusted source
                data = eval($("#" + opts.definitionId).val());
            }

            // load existing form data
            if (data != null)
            {
                 
                
                for(var f in data)
                {
                    var field = data[f];
                    if (!field.datatype) continue;

                    var fieldName = field.name;
                    if (field.name == "custom")
                        fieldName = field.label;
  
                
                    var fieldHtml = "";
                    switch(field.datatype) {
                        case 'date':
                            fieldHtml = "<input id='"+field.id+"' type='text' name='" + fieldName + "' rel='date' class='fwfield" + (field.required == true ? ' required' : '') + "' />";
                            break;
                        case 'div':
                            field.innerText = field.innerText.replace(/\u000a/g,"<br />");
                            fieldHtml += "<div class='paragraph gmvAltText'>"+field.innerText+"</div>";
                            break;
                        case 'section':
                            fieldHtml += "<div class='formheader gmvHeader'><span class='gmvHeaderTitle'>"+field.label+"</span></div>";
                            break;
					    case 'text':
                            fieldHtml = "<input id='"+field.id+"' type='text' name='" + fieldName + "' class='fwfield" + (field.required == true ? ' required' : '') + (field.labelPos === "Inside"? ' insideLabel': '') + "' label='" + field.label + "' />";
						    break;
					    case 'textarea':
                            fieldHtml = "<textarea id='"+field.id+"' name='" + fieldName + "' rows='" + field.rows + "' class='fwfield" + (field.required == true ? ' required' : '') + (field.labelPos === "Inside"? ' insideLabel': '') + "' label='" + field.label + "'></textarea>";
						    break;
					    case 'checkbox':
                        case 'radio':
                            fieldHtml += "<ul class='fwfield" + (field.required == true ? ' required' : '') + "'>";
                            for(var o in field.options)
                            {
                                var option = field.options[o];
                                if (option.value) {
                                    fieldHtml += "<li><input type='" + field.datatype + "' name='" + fieldName + "'" + (option.checked ? " checked='checked'" : "") + " value='" + option.value + "'></input><label class='wmformRadioLabel'> "+option.value+"</label></li>";
								}
                            }
                            fieldHtml += "</ul>";
                            break;
					    case 'select':
                            fieldHtml += "<select name='" + fieldName + "'" + (field.multiple ? " multiple='multiple'" : "") + " class='fwfield" + (field.required == true ? ' required' : '') + "'>";

                                if (!field.multiple){
                                    fieldHtml += "<option value='Select'>&lt;Select&gt;</option>";
                                }

                            if (field.listid > -1 && opts.list_url) {
                            
                              $.ajax({
                                dataType: 'json',
                                url: opts.list_url + "&ListId=" + field.listid,
                                async:false,
                                jsonp:'jsonpCallback',
                                success: function (listdata) 
                                    {
                                        $.each(listdata, function(index, value) { 
                                            fieldHtml += "<option value='" + value.Value + "'> " + value.Text + "</option>";
                                        });
                                    }
                              });
                                  
                            }
                            else
                            {
                                for(var o in field.options)
                                {
                                    var option = field.options[o];
                                    if (option.value)   
                                        fieldHtml += "<option " + (option.checked ? " selected='selected'" : "") + " value='" + option.value + "'> " + option.value + "</option>";
                                }
                            }
                            fieldHtml += "</select>";
    					    break;
                    }
                    if (fieldHtml != "")
                    {                        
                        var fieldLabel = '';
                        //if (field.labelPos == undefined || field.labelPos == 'Outside' || field.labelPos == 'Both')
                        
                        if (field.datatype != 'section' && field.datatype != 'div')
                        {
                            fieldLabel = "<label class='formLabel' for='" + fieldName + "'>" + ((field.labelPos == 'Inside') ? '' : field.label + ":") + "</label> ";
                            fieldHtml = "<div class='formElement'>" + fieldLabel + fieldHtml + "</div>";
                        }                      
                        container.append(fieldHtml);
                    }
                }

                if(options.RequiresPerson){
                    var realPerson = '<hr /><div class="realperson-div"><input type="text" id="txtRealVerify" label="Enter Code" class="realperson-challenge insideLabel" />';
                    container.append(realPerson);
                }

                var frmButtons = '<div class="formBtn">';
                frmButtons += '<button class="gmvFormBtn" id="SubmitForm" type="submit">Submit</button>';
                
                //Removed Clear Button Per Specs 06/02/2011
                //frmButtons += '<button id="ResetForm" type="submit">Clear</button>';
                
                frmButtons += '</div>';
                container.append(frmButtons);
                container.append('<div class="ThankYou" style="display:none"></div>');
                
            }

            

		});
	};
})(jQuery);
