/*
(c) ModoCMS
(c) Caters-Hardy Design Studio 2007-2009

Requires: jQuery and Scriptaculous to be loaded
*/
j = jQuery.noConflict();

// this function used for operations that take some time to complete.
function ws_please_wait(user_form, user_container) {
    if (user_form) {
        $('contact_form').hide();
    }

    if (user_container) {
        $(user_container).innerHTML = $('ws_loading').innerHTML;
    } else {
        document.body.innerHTML='<span id="please_wait"></span>' + document.body.innerHTML;
    }
}

/*
Input Param1: object that whose value property is being checked.
Input Param2: message that will be displayed if the object has an empty value
Return: bool
Example Usage:
<form ..... onsubmit="return ws_check_empty(this.label, 'The label field cannot be left blank.');">....</form>
<input type="text" name="label" id="label" size="" maxlength="" value="" />
OR
<input type="submit" name="edit" id="edit" value="Save Page" class="submit" onclick="return ws_check_empty(this.form.label, 'The label field cannot be left blank.');" />
*/
function ws_check_empty(anobj, message) {
    if (anobj.value == '') {
        alert(message);
        return false;
    }
    return true;
}

/*
Deletes contents of a container depending on the object type.
Supported types: DIV, SPAN, Input
Requires: Prototype function: "$"
Input elements are focused after it has been cleared
*/
function ws_clear_container(container_name) {
    cobj = $(container_name);

    if (cobj.tagName == 'DIV' || cobj.tagName == 'SPAN') {
        cobj.innerHTML = '';
    } else if (cobj.tagName == 'INPUT') {
        cobj.value = '';
        cobj.focus();
    } else {
        return false;
    }

    return true;
}

// Makes some effects e.g. for deletion the item pulsates and changes its colour to red to
// make itself noticed.
function ws_effect(object_id, status, colour) {
    // There are JS errors with the effects even under IE 8
    if (jQuery.browser.msie) {
        return;
    }

    status = status || 'blink';
    if (status == 'delete') {
        colour = colour || 'transparent'; // Red
    } else if (status == 'blink') {
        colour = colour || 'transparent'; // Silver
    }

    new Effect.Highlight($(object_id), {
        startcolor   : colour,
        duration     : 1
    });

    new Effect.Pulsate($(object_id), {
        pulses: 1,
        from: 0.3,
        to: 1.0,
        transition: Effect.Transitions.linear,
        duration: 1.0
    });

    // If delete remove item visually, if the colour is a different one that means the developer wants just to trigger user's attention.
    if (status == 'delete') {
        //                new Effect.SwitchOff($(object_id));
        //                new Effect.Puff($(object_id));

        new Effect.BlindUp($(object_id), {
            duration     : 1.0
        });
        new Effect.Fade($(object_id), {
            duration     : 1.0,
            from: 1.0,
            to: 0.0
        });
    }
}

// Ignores the enter key to prevent accidental form submission.
// Usage: <input class="" type="text" id="filter" name="filter" value="" onKeyPress="return ws_ignore_enter(event);" />
// src: http://jennifermadden.com/javascript/stringEnterKeyDetector.html
// src: http://www.w3schools.com/jsref/jsref_onkeypress.asp
function ws_ignore_enter(e) {
    var characterCode;

    if (window.event) { // IE
        characterCode = e.keyCode;
    } else if(e.which) { // Netscape/Firefox/Opera
        characterCode = e.which;
    }

    if (characterCode == 13) {
        return false;
    }

    return true;
}

