﻿
//
// Misc functions
//
//

function disable(cont) {
    $(cont).attr("disabled", "disabled");
};

function enable(cont) {
    $(cont).removeAttr("disabled");
};

function submitTo(formname,url) {
    var form = $("#" + formname);
    form.attr("action", url);
    form.submit();
};

function clearForm(formname) {
    $("#" + formname + " input").each(function () {
        var t = this.type;
        if ("text" == t)
            this.value = "";
        if ("checkbox" == t || "radio" == t)
            this.checked = false;
    });

    $("#" + formname + " select").each(function () {
        //$(this).find("option").attr("selected", "");
        $(this).attr("selectedIndex", -1);
    });

    $("#" + formname + " textarea").attr("value", "");
};

//
// Menu items
//
//

var menuitem = function(title, header, body) {
    this.title = title;
    this.header = header;
    this.body = body;
}

var menulist =
{
    'items': new Array(),
    'additem': function (it) {
        this.items.push(it);
    },
    'showlist': function (container) {
        var list = new String();
        for (i = 0; i < this.items.length; i++) {
            list += '<a id="' + this.items[i].header + '" class="menuheaderitem';
            if (0 == i) list += ' menuselectedheaderitem';
            list += '" onclick="switchItems(\'' + this.items[i].header + '\',\'' + this.items[i].body + '\'); return false;">' +
                this.items[i].title + '</a>'
        }
        $('#' + container).html(list);
    },
    'switchbytitle': function (title) {
        for (i = 0; i < this.items.length; i++) {
            if (title == this.items[i].title) {
                switchItems(this.items[i].header, this.items[i].body);
                break;
            }
        }
    }
}

function switchItems(menuitem, menubody) {
    menuitem = '#' + menuitem;
    menubody = '#' + menubody;

    // remove selected class
    $('.menuselectedheaderitem').removeClass('menuselectedheaderitem');

    // add selected class
    $(menuitem).addClass('menuselectedheaderitem');
    

    // remove selected class
    $('.menuselectedbodyitem').removeClass('menuselectedbodyitem');

    // add selected class
    $(menubody).addClass('menuselectedbodyitem');

}


//
// Re-Stringify - change /r/n to carriage return an linefeed, /t to tab
//
//
function restringify(s) {
    s = new String(s);
    s = s.replace(/\\r\\n/g, "<br />")
        .replace(/\\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    
    return s;
}


//
// Figure out correct URL for both dev and production
//
//
function getURL(targeturl) {
    // first split out target and current urls...
    tlist = targeturl.toString().split("/");

}


//
// Display Notes popup dialog
//
//

$(document).ready(function() {
var $dialog = $('#displaynote')
	.dialog({
	    buttons: { "Ok": function() { $(this).dialog("close"); } },
	    autoOpen: false,
	    title: 'Notes',
	    height: 400,
	    width: 700,
	    modal: true
	});

    $('#opener').click(function() {
        $dialog.dialog('open');
    });

    $(document).ajaxError(function (e, xhr, settings, exception) {
        alert('error in: ' + settings.url + ' \\n' + 'error:\\n' + exception);
    });
});

function getNotes(turl, id, t) {
    var title = t;
    $.get(turl, function (data) {
        data = restringify(data);
        $dialog = $("#displaynote")
        $dialog.html("<label>" + data + "</label>")
        $dialog.dialog('option', 'title', "Notes: " + t);
        $dialog.dialog('open');
    });
}


function getPermitPeople(turl, id, t) {
    var title = t;
    $.get(turl, function (data) {
        // build table
        var htm = "<table>";
        for (i = 0; i < data.length; i++) {
            htm += "<tr>";
            htm += "<td>";
            htm += data[i]['csp_name'];
            htm += "</td>";
            htm += "<td>";
            htm += data[i]['role_description'];
            htm += "</td>";
            htm += "<td>";
            htm += data[i]['csp_address'];
            htm += "</td>";
            htm += "<td>";
            if (data[i]['cpc_comm_detail'])
                htm += data[i]['cpc_comm_detail'];
            htm += "</td>";
            htm += "</tr>";
        }
        htm += "</table>";

        // display table
        $dialog = $("#displaynote")
        $dialog.html(htm)
        $dialog.dialog('option', 'title', t);
        $dialog.dialog('open');
    });
}

function getParcelPeople(turl, id, t) {
    var title = t;
    $.get(turl, function (data) {
        // build table
        var htm = "<table>";
        for (i = 0; i < data.length; i++) {
            htm += "<tr>";
            htm += "<td>";
            htm += data[i]['lastname'];
            htm += "</td>";
            htm += "<td>";
            htm += data[i]['role_type'];
            htm += "</td>";
            htm += "<td>";
            htm += data[i]['address1'];
            if (data[i]['address2'])
                htm += '<br />' + data[i]['address2'];
            if (data[i]['city'])
                htm += '<br />' + data[i]['city'];
            if (data[i]['state'])
                htm += '<br />' + data[i]['state'];
            if (data[i]['zip'])
                htm += '<br />' + data[i]['zip'];
            htm += "</td>";
            htm += "</tr>";
        }
        htm += "</table>";

        // display table
        $dialog = $("#displaynote")
        $dialog.html(htm)
        $dialog.dialog('option', 'title', t);
        $dialog.dialog('open');
    });
}

function getObject(turl, id, t) {
    var title = t;
    $.get(turl, function (data) {
        // build table
        var htm = "<table>";
        for (i = 0; i < data.length; i++) {
            htm += "<tr>";
            for (o in data[i]) {
                htm += "<td>";
                htm += data[i][o];
                htm += "</td>";
            }
            htm += "</tr>";
        }
        htm += "</table>";

        // display table
        $dialog = $("#displaynote")
        $dialog.html(htm)
        $dialog.dialog('option', 'title', t);
        $dialog.dialog('open');
    });
}

