/**
 * Sets user home page in IE 5.0 and later.
 * <br/>
 * In other browsers this function simply does nothing.
 * <br/>
 * Example:
 * <code>
 * <a href="#" onclick="set_home_page('http://www.starchat.ee', this)">set home page</a>
 * </code>
 *
 * @param {String} url new home page url
 * @param {Element} element element used by this function
 *
 */
function set_home_page(url, element) {

    if (document.all) { // works with IE >= 5.0
        element.style.behavior = "url('#default#homePage')";
        element.addBehavior("#default#homePage");
        element.setHomePage(url);
    }
}

/**
 * Adds specified URL to favorites.
 * <br/>
 * This works only in IE.
 * <br/>
 * Example:
 * <code>
 * <a href="#" onclick="add_to_favorites('http://www.starchat.ee', 'Starchat');">add to favorites</a>
 * <a href="#" onclick="add_to_favorites('http://www.starchat.ee');">add to favorites</a>
 * <a href="#" onclick="add_to_favorites();">add to favorites</a>
 * </code>
 *
 * @param {String} url url to add to favorites
 * @param {String} title title of the favorites link
 */
function add_to_favorites() {
    var url = location.href;
    var title = document.title;

    if (arguments.length == 1) {
        url = arguments[0];
    } else if (arguments.length == 2) {
        url = arguments[0];
        title = arguments[1];
    }

    if (document.all) { // IE
        external.AddFavorite(url, title);
    }
}


function checkBoxState(state, element){
    var arrInput = document.getElementsByTagName(element);

    for(i=0; i<arrInput.length; i++){
        if(arrInput[i].type == 'checkbox'){
            arrInput[i].checked = state;
        }
    }
 }

function checkBoxStateByName(elementName, state){
    var collection = document.getElementsByName(elementName);

    if(collection.length > 0){
        for(var i = 0; i <= collection.length; i++){
            if(collection[i].type == "checkbox"){
                collection[i].checked = state;
            }
        }            
    }
}

function loadChat(room){

    var url = "index.html?action=goto_starchat"

    url += "&userId=" + document.getElementById("userId").value;

    if(typeof(room) != 'undefined'){
        url += "&roomId=" + room;    
    }
    
    window.open(url, "Starchat", "toolbar=no, status=0, width=720,height=600,resizable=no");
}

function popup(url, name){
    window.open(url, name, "toolbar=no");
}

var NS = (navigator.appName=="Netscape")?true:false;

function fitPage() {

    var table = document.getElementById("table");

    iWidth = (NS)?window.innerWidth:document.body.clientWidth;
    iHeight = (NS)?window.innerHeight:document.body.clientHeight;
    iWidth = table.offsetWidth - iWidth;
    iHeight = table.offsetHeight - iHeight;

    window.resizeBy(iWidth, iHeight);
    self.focus();
};

function deleteUser(nickName) {
    document.getElementById("nameToDel").value=nickName;
}

function selectAll(form, group) {
    var nodeList = document.forms[form].elements[group];
    if(nodeList.length){
        for(i = 0; i < nodeList.length; i++) {
            nodeList[i].checked = true;
	    }
    }else{
        nodeList.checked = true;
    }
}

function addChecked(form, group, destination){

    var array = new Array();
    var nodeList = document.forms[form].elements[group];

    if(nodeList.length){
        for(i=0; i < nodeList.length; i++){
            if(nodeList[i].checked){
                array[i] = nodeList[i].value;
            }
        }
    }else{
        if(nodeList.checked){
            array[0] = nodeList.value;
        }
    }

    document.forms[form].elements[destination].value = array;
}

function changeValue(form, field, value){
    var obj = document.forms[form].elements[field];
    if(obj){
        obj.value = value;
    }
}

function setChecked(form, group, id){

    var nodeList = document.forms[form].elements[group];
    if(nodeList.length){
        for(i=0; i < nodeList.length; i++){
            if(nodeList[i].value == id){
                nodeList[i].checked = true;
            }
        }
    }else{
        if(nodeList.value == id){
            nodeList.checked = true;
        }
    }

}

function makeTwoChars(inp) {
        return String(inp).length < 2 ? "0" + inp : inp;
}

function initialiseDatePicker() {
        // Attempt to grab the datePicker objects
        var datebox = datePickerController.datePickers["birthdate"];

        // For Internet Explorer: If they are not created then call this function 500 milliseconds later
        if(!datebox) {
                setTimeout("initialiseDatePicker()", 200);
                return;
        }

        // Reset the low ranges to be today for both the datePickers
        var today = new Date();
        today = String(today.getFullYear()) + makeTwoChars(today.getMonth()+1) + makeTwoChars(today.getDate());

        datebox.setRangeHigh( today );

        // Clear any old values from the inputs (that might be cached by the browser after a page reload)
        document.getElementById("birthdate").value = "";

        // Add the onchange event handler to the start date input
        //document.getElementById(birthdate).onchange = setReservationDates;
}

function loginFocus(){

    var form = document.forms['login'];
    if(form != null){
        form.login.focus();
    }
}

function loadImage(slotName, containerName, loadingSlot, id, host){

    var imageSlot = $(slotName);
    var imageDiv = $(containerName);
    var loadingImageSlot = $(loadingSlot);

    var newImageSrc = "http://" + host + "/images/image.png?id=" + id;

    if(id != 0){
        loadingImageSlot.src = "http://" + host + "/static/img/loading.gif";

        var loadedImage = new Image();
        loadedImage.onload = function(){

            imageDiv.hide();
            imageSlot.src = newImageSrc;
            imageSlot.src = newImageSrc;
            imageDiv.show();

            loadingImageSlot.src = "http://" + host + "/static/img/loading_done.png";
        }

        loadedImage.src = newImageSrc;
    }else{
        imageSlot.src = "";
    }
}

function clientBannerInit(){
    setTimeout("clientBannerRefresh()", 60000);
}

function clientBannerRefresh(){
    source = document.getElementById('bannerFrame').src;
    
    var elem = document.getElementById('bannerFrame');

    if (elem) {
    	elem.src = source;
    }

    clientBannerInit();
}

function submitComment(){

    var news = $F('id');
    if(news == null)
        return;

    $('comment_add').request({
        onSuccess: function (transport){
            $('addComment').replace(transport.responseText);
        }
    });
}

function deleteComment(id, msg){

    var news = $F('id');
    if(news == null)
        return;

    if(confirm(msg)){
        new Ajax.Request( 'comment.html', {
            method: 'POST',
            parameters: {page: $F('page'), action: 'deleteComment', commentId: id, id: news},
            onSuccess: function (transport){
                $('addComment').replace(transport.responseText);
            }
        });
    }
}

function toggleStats(type, country){
    $(type + '_EE').hide();
    $(type + '_LV').hide();
    $(type + '_LT').hide();

    $('BUTTON_' + type + '_EE_HILITE').hide();
    $('BUTTON_' + type + '_LV_HILITE').hide();
    $('BUTTON_' + type + '_LT_HILITE').hide();

    $('BUTTON_' + type + '_EE').show();
    $('BUTTON_' + type + '_LV').show();
    $('BUTTON_' + type + '_LT').show();
    
    $('BUTTON_' + type + '_' + country).hide();
    $('BUTTON_' + type + '_' + country + '_HILITE').show();
    $(type + '_' + country).show();
}