// constants for message dispatching (same constants are defined in config.inc.php)
var ERROR   = 0;
var INFO    = 1;
var WARNING = 2;

 function formatMessage(jsonMsg, msgDivId){
   
    msgDivId = (msgDivId == null) ? '#message' : '#'+msgDivId;

    var level = jsonMsg.level;
    var message = jsonMsg.msg;

    jQuery(msgDivId).addClass('ui-widget');
    var msgDiv = '';

    if(level == ERROR){ //|| level == INFO || level == WARNING){
        msgDiv += '<div class="ui-state-error ui-corner-all"><p>';
        msgDiv += '<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"/>';
        msgDiv += '<b>Alert: </b>'+message;
        msgDiv += '</p></div>';
    }else if(level == WARNING){
        msgDiv += '<div class="ui-state-warning ui-corner-all"><p>';
        msgDiv += '<span class="ui-icon ui-icon-notice" style="float: left; margin-right: 0.3em;"/>';
        msgDiv += '<b>Warning: </b>'+message;
        msgDiv += '</p></div>';
    }else if(level == INFO){
        msgDiv += '<div class="ui-state-highlight ui-corner-all"><p>';
        msgDiv += '<span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"/>';
        msgDiv += '<b>Info: </b>'+message;
        msgDiv += '</p></div>';
    }
    //alert('sdfsdf'+msgDivId);
    jQuery(msgDivId).html(msgDiv);
}





function Cloner(cloneLimit, msgDivId){
    this.cloneLimit = cloneLimit;
    this.msgDiv = '#'+msgDivId;

    /**
     * clones and formats the node(s).
     * Also displays message on clone limt exceeded.
     *
     * @param <string> clonedSelector the selector that has been cloned
     * @param <string> cloneSrc the source for cloning
     * @param <string> cloneTarget the target before which the cloned elements are to be placed
     */
    this.clone = function(clonedSelector, cloneSrc, cloneTarget){
        var cloneCount = $(clonedSelector).length;

        if(cloneCount <= this.cloneLimit){
            var cloneContent = $('#'+cloneSrc).html();
            var outputClone = $.format(cloneContent, cloneCount);

            $(outputClone).insertBefore('#'+cloneTarget);
        }else{
            $(this.msgDiv).html('You cannot add more than '+cloneLimit+' items');
            $(this.msgDiv).fadeIn(400).fadeOut(4000);
        }
    }

    /**
     *
     * clones and formats the node(s)
     * Also displays message if minimum clone limit is reached.
     *
     * @param <string> clonedSelector the selector that has been cloned
     */
    this.remove = function(clonedSelector){
        var cloneCount = $(clonedSelector).size();

        if(cloneCount > 1){
            $(clonedSelector+':last').remove();
        }else{
            $(this.msgDiv).html('You must add at least 1 item.');
            $(this.msgDiv).fadeIn(400).fadeOut(4000);
        }
    }
}

function redirect(url){
    window.location = url;
}



function back(){
    window.history.back();
}

function postAjax(url, data, successHandler){
    $.ajax({
        url: url,
        data: data,
        type: 'post',
        dataType: 'json',
        timeout: 5000,
        error: function(){
            $('message').val('Cannot connect to server at the moment!!!');
        //alert('error');
        },
        success: function(response){
            successHandler(response);
        }
    });
}

function alterFontSize(target, factor){
    var oldFontSize = parseInt(target.css('font-size'));
    alert(oldFontSize);
    if(factor < 0 && oldFontSize > 14 && oldFontSize <= 20){ //decrease font
        target.css('font-size', (oldFontSize + factor)+'px');
    }else if(factor > 0 && oldFontSize >= 14 && oldFontSize <= 18){ // inccrease font
        target.css('font-size', (oldFontSize + factor)+'px');
    }
}

function alterFontSize1(target, factor){
    /*alert(target.css('font-size'));*/
    var oldFontSize = parseInt(target.css('font-size'));
    if(factor < 0 && oldFontSize > 12 && oldFontSize <= 24){ //decrease font
        target.css('font-size', (oldFontSize + factor)+'px');
    }else if(factor > 0 && oldFontSize >= 12 && oldFontSize <= 22){ // inccrease font
        target.css('font-size', (oldFontSize + factor)+'px');
    }
}


// Font size change
function textSize(fontClass)
{
	var theContents = document.getElementById('ctrlContents');
	theContents.className = fontClass;
}
