/*
 WebSweet JavaScript.
 (c) Caters-Hardy Design Studio

*/
// 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) {
    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
   		});
    }
}

// Because of Ajax, I have to execute functions to make tooltips appear or other functionality enabled.
// It is  created by jQuery library
function ws_system_post_ajax() {
    // This is a proxy function to /js/jquery.js:ws_install_jquery_handlers function.
    ws_install_jquery_handlers();
    return;
}

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/array/diff [rev. #1]
// Compares two arrays and returns the distinct values.
ws_diff = function(v, c, m) {
    var d = [], e = -1, h, i, j, k;
    for(i = c.length, k = v.length; i--;){
        for(j = k; j && (h = c[i] !== v[--j]););
        h && (d[++e] = m ? i : c[i]);
    }
    return d;
};

// 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;
}