
function deleteObject(id, name, type, ref) {
    if(!confirm("Är du säker på att du vill ta bort '" + name + "' ur systemet?")) {
        return false;
    }

    window.location="delete.php?type=" + type + "&id=" + id + "&ref=" + ref;
    return true;
}

function showTab(showId) {
    var i;

    var panels=$$('.tabPanel');
    for(i=0;i<panels.length;i++) {
        panels[i].className='tabPanel';
    }

    var tabs=$$('.tab');
    for(i=0;i<tabs.length;i++) {
        tabs[i].className='tab';
    }

    var tabId="tab_"+showId;
    var panelId="tabPanel_"+showId;

    $(tabId).className='tab active';
    $(tabId).blur();
    $(panelId).className='tabPanel active';
}

function numbersOnly(event) {
    var key;
    var keychar;

    if (window.event) {
        key = window.event.keyCode;
    } else if (event) {
        key = event.which;
    } else {
        return true;
    }

    // control keys
    if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) {
        return true;
    }

    keychar = String.fromCharCode(key);

    // numbers
    if ((("0123456789").indexOf(keychar) > -1)) {
        return true;
    }

    return false;
}

function setPageTitle(base, title) {
    $('pagetitle').innerHTML = title;
    document.title = base + ' - ' + title;
}

function setPageSubtitle(title) {
    $('pagesubtitle').innerHTML = title;
    $('pagesubtitle').style.display = (title != '') ? 'inherit' : 'none';
}

function toggleElement(elementId, linkId, hiddenLinkText, visibleLinkText) {
    var link = $(linkId);
    var element = $(elementId);

    if(element.style.display == 'none') {
        element.style.display = 'block';
        if(link) {
            link.innerHTML = visibleLinkText;
        }
    } else {
        element.style.display = 'none';
        if(link) {
            link.innerHTML = hiddenLinkText;
        }
    }

    return false;
}

function focusSearchField(id, defaultText, focused) {
    if (focused && $F(id) == defaultText) {
        $(id).value = '';
    }
	
    if(!focused && $F(id) == '') {
        $(id).value = defaultText;
    }
}


function updateGrade(event) {
    if(!event) {
        event = window.event;
    }
	
    var control = (window.event) ? event.srcElement : event.target;
	
    if(control.tagName == "IMG") {
        control = control.parentNode;
    }
	
    var elementLeft = getElementPosition(control)[0];
    var x = event.clientX - elementLeft;
    //window.status = x + "/" + control.offsetWidth + " " + elementLeft;
    var percent = x / control.offsetWidth;
    var value = 5 * percent;
    value = Math.round(value * 2)/2;
    value = Math.max(value, 0.5);
    setTempGrade(control, value);
    refreshGrade(control);
}

function getElementPosition(control) {
    var x=0, y=0;
    var obj = (typeof control == "string") ? document.getElementById(control) : control;
	
    if (obj) {
        x = obj.offsetLeft;
        y = obj.offsetTop;
        var body = document.getElementsByTagName('body')[0];
        while (obj.offsetParent && obj!=body){
            x += obj.offsetParent.offsetLeft;
            y += obj.offsetParent.offsetTop;
            obj = obj.offsetParent;
        }
    }
	
    return new Array(x, y);
}

function refreshGrade(control) {
    var name = control.id.substring(6);
    var value = $F('temp_'+name);
	
    var children = control.getElementsByTagName("img");
    for(var i = 0; i < children.length; i++) {
        var child = children.item(i);
		
        if(i < value-0.5) {
            img = "star_full.gif";
        } else if(i < value) {
            img = "star_half.gif";
        } else {
            img = "star_empty.gif";
        }
		
        child.src = "/public/style/" + img;
        child.alt = value;
        child.title = value;
    }
}

function setTempGrade(control, value) {
    var id = control.id;
    var name = "temp_" + id.substring(6);
    if(!$(name)) {
        alert("No " + name + " " + control);
    }
    $(name).value = value;
}

function setGrade(id) {
    var name = id.substring(6);
    var tempid = 'temp_' + name;
    $(name).value = $F(tempid);
    updateGradeSubmit(name);
}

function resetGrade(event) {
    if (!event) {
        event = window.event;
    }
	
    var source = (window.event) ? event.srcElement : event.target;
    var target = (event.relatedTarget) ? event.relatedTarget : event.toElement;
	
    if(target.tagName == "SPAN" || target.tagName == "IMG") {
        return;
    }
	
    var control = source;
    if(control.tagName == "IMG") {
        control = control.parentNode;
    }
	
    var name = control.id.substring(6);
    $('temp_'+name).value = $F(name);
    refreshGrade(control);
}

function updateGradeSubmit(name) {
    var split = name.lastIndexOf('_');
    var prefix = '';

    if (split > -1) {
        prefix = name.substring(0, split+1);
    }

    var complete = $F(prefix+'taste') > 0 && $F(prefix+'container') > 0 && $F(prefix+'appearance') > 0;
    $('savegrade').disabled = !complete;
    $('savegrade').className = complete ? '' : 'disabled';
}

function startValidating(input, code) {
    $(input).onkeyup = function(e) {
        var ok = ($F(input) == code);
        $(input).className = ok ? 'form_ok' : 'form_invalid';
    };
    return true;
}

function stopValidating(input) {
    $(input).onkeyup = null;
    return true;
}

function submitTo(formname, action) {
    document.forms[formname].action = action;
    document.forms[formname].submit();
}

function navigateTo(url) {
    document.location = url;
}

function confirmDelete(object) {
    return confirm('Är du säker på att du vill ta bort ' + object + '?');
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
} 
