/**
 * displays the appropriate logo for the corresponding paper
 *
 * @param paperPrefix the prefix of the paper
 * @hostPath the host of the path
 */
function displayPaperLogo(paperPrefix, hostPath){
    var img;
    var path;
    //alert(hostPath);
    if(paperPrefix == 'ek'){
        img = 'ek-logo.jpg';
        path = hostPath;
    }else if(paperPrefix == 'ekn'){
        img = 'ek-logo.jpg';
        path = hostPath+'index.php?lang=np';
    }else if(paperPrefix == 'tkp'){
        path = hostPath+'tkp';
        img = 'logo-ktm-post.gif';
    }else if(paperPrefix == 'kan'){
        path = hostPath+'kantipur';
        img = 'logo-kantipur.gif';
    }else if(paperPrefix == 'kq'){
        path = hostPath+'qatar';
        img = 'logo-kantipur-qatar.gif';
    }else if (paperPrefix == 'nep'){
        path = hostPath+'nepal';
        img = 'logo-nepal.gif';
    }else if (paperPrefix == 'sap'){
        path = hostPath+'saptahik';
        img = 'logo-saptahik.gif';
    }else if (paperPrefix == 'nar'){
        path = hostPath+'saptahik';
        img = 'logo-naari.gif';
    }
    
    $('#paper').html('<a href="'+path+'"><img src ="../../images/'+img+'" /></a>');

    return false;
}

function getTodayDate(){
    var d=new Date();
    var today = d.getFullYear() + '-'+ (d.getMonth() + 1) + '-' +d .getDate();
    return today;
}

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

/**
 * Blueprint for select/combo that requies chaining reload
 *
 * @param <string> src the id of the parent select/combo
 * @targer <string> the id of the child/target select/combo
 * @task <string> the task that is to be handled in the server side
 * @isLeaf <boolean> the value that specifies whether the parent needs to
 *                          trigger the reload of child
 * @child <Combo> the child Combo object
 * @name <string> [optional] the name that needs to be prepended
 */
function Combo(src, target, task, isLeaf, child, name){
    this.src = src;
    this.target = target;
    this.task = task;
    this.isLeaf = isLeaf;

    this.toString = function(){
        return name;
    }

    /**
     * return the curretn selected value of source select/combo
     */
    this.getValue = function(){
        return $('#'+src).val();
    }

    /**
	 * the function that triggers reload
     *
	 * @param url the URL from where the JSON is to be fetched
	 * @param prependSelect if '---SELECT XXX ---' is to be prepended to the options
	 */
    this.reloadChild = function(url, prependSelect){        
        // the url which queries the database
        var _url = url;

        // the params to be sent as POST varaiables the above URL
        var params ={
            // the task to be performed
            task: this.task,
            // id of the parent select/combo
            parent_id: this.getValue()
        };

        // make the AJAX call the above URL
        $.ajax({
            url: _url,
            type: 'POST',
            data: params,
            dataType: 'json',
            success: function(itemList){
                $('#'+target).empty();
                if(itemList.length > 0){
                    for(var n = 0; n < itemList.length; n++) {
                        $('#'+target).get(0).add(
                            new Option(itemList[n].name,itemList[n].id),
                            document.all ? 0 : null);
                    }
                }
                // if --Select XXX-- needs to prepended to the child select/combo
                if(prependSelect != null || prependSelect == true){
                    //$('#'+target).prepend('<option selected value="">--Select '+child.toString()+'--</select>')
                    $('<option value="">--- Select '+child.toString()+'--</option>').prependTo($('#'+target+':first-child'));
                    $('#'+target+' option[value=""]').attr('selected', 'selected');
                }
            },
            complete:function(){
                // the recursive handler which checks if there is any child element to reload
                if(!child.isLeaf){
                    child.reloadChild(url, prependSelect);
                }
            }
        });
        return false;
    }
}