﻿
function clearText(thefield) {
    if (thefield.defaultValue == thefield.value)
        thefield.value = ""
}

(function($) {
    // plugin to allow form.clearForm() to be called which will clear all the fields 
    $.fn.clearForm = function() {
        return this.each(function() {
            var type = this.type, tag = this.tagName.toLowerCase();
            if (tag == 'form')
                return $(':input', this).clearForm();
            if (type == 'text' || type == 'password' || tag == 'textarea')
                this.value = '';
            else if (type == 'checkbox' || type == 'radio')
                this.checked = false;
            else if (tag == 'select')
                this.selectedIndex = 0; // original was -1 which sets it to nothing
        });
    };
})(jQuery);

function confirmDelete() {
    return confirm("You are about to delete this record. This is permananent. Are ABSOLUTELY sure?");
}

function ajaxJSON(json) {
    var obj = (typeof (json) == 'object') ? json : eval('(' + json + ')');
    eval(obj.script);
    if (obj.confirm) {
        obj.success = confirm(obj.confirm);
    }
    return obj;
}

jQuery.fn.labelOver = function(overClass) {
    return this.each(function() {
        var label = jQuery(this);
        var f = label.attr('for');
        if (f) {
            var input = jQuery('#' + f);

            this.hide = function() {
                label.css({ textIndent: -10000 })
            }

            this.show = function() {
                if (input.val() == '') label.css({ textIndent: 0 })
            }

            // handlers
            input.focus(this.hide);
            input.blur(this.show);
            input.change(function() { if ($(input).val() != "") this.hide; });
            label.addClass(overClass).click(function() { input.focus(); });
            setTimeout(function() { if (input.val() != '') $(this).hide(); }, 1000);
            if (input.val() != '') this.hide();
        }
    })
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}