function com_submit()
{
    if(document.getElementById('com_text').value.length == 0)
    {
        alert('Skriv inn en kommentar først.')
        return false;
    }
    return true;
}

function alb_passwd_popup(alb_id)
{
    var alb_password_popup_qwe = window.open("alb_passwd_login_id" + alb_id + ".html", "pixbox_alb_passwd_popup", "toolbar=no,top=250,left=70,status=no,menubar=no,scrollbars=no,z-index=10,width=480,height=270,resizable=no");
    alb_password_popup_qwe.focus();
}

var gb_reply_popup_windows=new Array();
function gb_reply_popup(owner, parent)
{
    if(!gb_reply_popup_windows[owner] || gb_reply_popup_windows[owner].closed)
        gb_reply_popup_windows[owner] = window.open('/gb_reply_popup.php?parent='+parent+'&owner='+owner, 'pixbox_gb_reply_usr'+owner, 'toolbar=no,top=250,left=70,status=no,menubar=no,scrollbars=no,z-index=10,width=480,height=270,resizable=yes');
    gb_reply_popup_windows[owner].focus();
}

function gb_new_submit()
{
    if(document.getElementById('gb_message').value.length < 2)
    {
        alert('Skriv et lengre innlegg');
        return false;
    }

    button_disable('gb_new_submit_button');
    return true;
}

function gb_delete_confirm()
{
    return confirm('Slette gjestebokinnlegget?');
}

function site_choose_popup()
{
    var site_choose_popup_wp = window.open("/site_choose_popup.php", "choose_site_popup_window", "toolbar=no,top=250,left=70,status=no,menubar=no,scrollbars=no,z-index=10,width=320,height=350,resizable=yes");
    site_choose_popup_wp.focus();
}

function site_choose_popup_change_lang(lang)
{
    window.location = '/ses/ses_lang_change.php?popup=true&lang='+lang;
}

function site_choose_popup_change_site(domain)
{
    window.opener.location =  window.opener.location.protocol + '//www.' + domain + window.opener.location.pathname + window.opener.location.search + window.opener.location.hash;
    window.close();
}

function alb_create_validate()
{
    if(document.getElementById('alb_title').value.length < 3)
    {
        alert('Skriv inn en kommentar først. Minst 3 tegn.');
        return false;
    }
    else
    {
        document.getElementById('alb_create_submit').value='Vent...';
        document.getElementById('alb_create_submit').disabled='disabled';
        return true;
    }
}

function alb_edit_validate()
{
    if(document.getElementById('alb_title').value.length < 3)
    {
        alert('Skriv inn en kommentar først. Minst 3 tegn.');
        return false;
    }
    else
    {
        document.getElementById('alb_edit_submit').value='Vent...';
        document.getElementById('alb_edit_submit').disabled='disabled';
        return true;
    }
}

function popup(page, name, size)
{
    if(size == 'small')
    {
        width = 200;
        height = 150;
    }
    else
    {
        width = 410;
        height = 230;
    }

    /* Obs! page innehåller GET-parametrar i vissa fall */
    var pixbox_popup = window.open('popup.php?page=' + page, "pixbox_popup_"+name, "toolbar=no,top=300,left=320,status=no,menubar=no,scrollbars=no,z-index=10,width=" + width + ",height=" + height + ",resizable=no");
    pixbox_popup.focus();
}

function verify_email(email) {
    if(email.indexOf("@")==-1 || email.indexOf(" ")!=-1)
        return false;
    else
        return true;
}

function usr_reg_submit() {
    try {
        document.getElementById('usr_email').value = trim(document.getElementById('usr_email').value);
        if(!verify_email(document.getElementById('usr_email').value)) {
            alert('Skriv inn e-postadressen din. Du må bruke e-posten din for å aktivere brukerkontoen din senere.');
            return false;
        }

        if(document.getElementById('usr_firstname').value.length == 0) {
            alert('Skriv inn fornavnet ditt.');
            return false;
        }

        if(document.getElementById('usr_lastname').value.length == 0) {
            alert('Skriv inn etternavnet ditt.');
            return false;
        }

        if(document.getElementById('usr_kom_id').value == 0) {
            alert('Velg ditt bosted.');
            return false;
        }

        if(!document.getElementById('usr_sex_f').checked && !document.getElementById('usr_sex_m').checked) {
            alert('Velg kjønn.');
            return false;
        }


        if(document.getElementById('usr_birth_year').value == 0) {
            alert('Angi fødselsdatoen din eller iallfall fødselsåret ditt');
            return false;
        }


        if(document.getElementById('usr_nickname').value.length < 3 || document.getElementById('usr_nickname').value.length > 16) {
            alert('Skriv inn et brukernavn på mellom 3 og 16 tegn.')
            return false;
        }


        if(document.getElementById('usr_password').value.length < 6) {
            alert('Skriv inn et passord på minst 6 tegn.');
            return false;
        }

        if(document.getElementById('usr_password').value != document.getElementById('usr_password_validate').value) {
            alert('Feil bekreftelse av passordet.');
            return false;
        }

        if(!document.getElementById('usr_agreement').checked) {
            alert('Du må akseptere medlemsavtalen for å kunne registrere deg.');
            return false;
        }

        document.getElementById('usr_submit_button').value='Vent...';
        document.getElementById('usr_submit_button').disabled='disabled';
        return true;
    }
    catch (e) {
        return true;
    }
}

function usr_edit_submit()
{
    if(document.getElementById('usr_firstname').value.length == 0)
    {
        alert('Skriv inn fornavnet ditt.');
        return false;
    }

    if(document.getElementById('usr_lastname').value.length == 0)
    {
        alert('Skriv inn etternavnet ditt.');
        return false;
    }

    document.getElementById('usr_email').value = trim(document.getElementById('usr_email').value);
    if(!verify_email(document.getElementById('usr_email').value))
    {
        alert('Skriv inn en gyldig e-postadresse.');
        return false;
    }

    if(document.getElementById('usr_password_new').value.length > 0)
    {
        if(document.getElementById('usr_password_new').value.length < 6)
        {
            alert('Skriv inn et passord på minst %d tegn.');
            return false;
        }

        if(document.getElementById('usr_password_new').value != document.getElementById('usr_password_new_verify').value)
        {
            alert('Feil bekreftelse av nytt passord.');
            return false;
        }
    }

    if(document.getElementById('usr_sex').value == 0)
    {
        alert('Velg kjønn.');
        return false;
    }

    if(document.getElementById('usr_birth_year').value == 0)
    {
        alert('Velg fødselsår.');
        return false;
    }

    if(document.getElementById('usr_kom_id').value == 0)
    {
        alert('Velg hvor du bor.');
        return false;
    }

    if(!document.getElementById('usr_reg_agreement').checked)
    {
        alert('Du må akseptere brukeravtalen.');
        return false;
    }

    if(document.getElementById('usr_password').value.length == 0)
    {
        alert('Skriv inn ditt nåværende passord nederst.');
        return false;
    }

    if(document.getElementById('usr_email').value != document.getElementById('usr_email_original').value)
    {
        if(!confirm('Ettersom du valgte å endre e-postadressen din, blir brukerkontoen din deaktivert inntil du svarer på e-posten som kommer til å bli sendt til den nye e-postadressen din. Fortsette?'))
            return false;
    }
    return true;
}

function usr_search_submit()
{
    var usr_age_min = parseInt(document.getElementById('srch_age_min').value);
    var usr_age_max = parseInt(document.getElementById('srch_age_max').value);

    if(usr_age_min > usr_age_max)
    {
        alert('Du kan ikke søke på en minimumsalder som er høyere enn maksimumsalderen.');
        return false;
    }

    return true;
}

function usr_passwd_change_submit()
{
    if(document.getElementById('usr_password').value.length < 6)
        alert('For kort passord. Minst 6 tegn.');
    else if(document.getElementById('usr_password').value != document.getElementById('usr_password_verify').value)
        alert('Feil bekreftelse av nytt passord.');
    else
        return true;

    return false;
}


function lan_update(lan_id_selected, kom_id_selected)
{
    ctry_id = document.getElementById('usr_ctry_id').value;

    document.getElementById('usr_kom_id').options.length = 0;
    document.getElementById('usr_kom_id').options[0] = new Option('Sted/kommune',0);
    document.getElementById('usr_kom_id').options[1] = new Option('Velg len først',0);

    if(ctry_id == 0)
    {
        document.getElementById('usr_lan_id').options.length = 0;
        document.getElementById('usr_lan_id').options[0] = new Option('Len',0);
        document.getElementById('usr_lan_id').options[1] = new Option('Velg len først',0);
    }
    else
    {
        document.getElementById('usr_lan_id').options.length = 0;
        document.getElementById('usr_lan_id').options[0] = new Option('Len',0);

        for(i=1 ; i<kom_array[ctry_id].length ; i++)
        {
            document.getElementById('usr_lan_id').options[i] = new Option(kom_array[ctry_id][i]['name'], kom_array[ctry_id][i]['id']);
        }
    }

    if(lan_id_selected != 0)
    {
        for(i=1 ; i<document.getElementById('usr_lan_id').options.length ; i++)
        {
            if(document.getElementById('usr_lan_id').options[i].value == lan_id_selected)
            {
                document.getElementById('usr_lan_id').options[i].selected = 'selected';
                kom_update(kom_id_selected)
                break;
            }
        }
    }
}

function kom_update(kom_id_selected)
{
    ctry_id = document.getElementById('usr_ctry_id').value;
    lan_id = document.getElementById('usr_lan_id').value;
    lan_i_id = 0;

    if(lan_id != 0)
    {
        //alert('Kör! ctry_id=' + ctry_id + ', lan_id=' + lan_id);
        for(i=1 ; i<=kom_array[ctry_id].length ; i++)
        {
            if(lan_id == kom_array[ctry_id][i]['id'])
            {
                lan_i_id = i;
                //alert('hittat');
                break;
            }
        }
    }

    document.getElementById('usr_kom_id').options.length = 0;
    if(lan_i_id == 0)
    {
        document.getElementById('usr_kom_id').options.length = 0;
        document.getElementById('usr_kom_id').options[0] = new Option('Sted/kommune',0);
        document.getElementById('usr_kom_id').options[1] = new Option('Velg len først',0);
    }
    else
    {
        document.getElementById('usr_kom_id').options[0] = new Option('Sted/kommune',0);

        for(i=1 ; i<kom_array[ctry_id][lan_i_id]['kom'].length ; i++)
        {
            document.getElementById('usr_kom_id').options[i] = new Option(kom_array[ctry_id][lan_i_id]['kom'][i]['name'], kom_array[ctry_id][lan_i_id]['kom'][i]['id']);
        }
    }


    if(kom_id_selected != 0)
    {
        for(i=1 ; i<document.getElementById('usr_kom_id').options.length ; i++)
        {
            if(kom_array[ctry_id][lan_i_id]['kom'][i]['id'] == kom_id_selected)
            {
                document.getElementById('usr_kom_id').options[i].selected = 'selected';
                break;
            }
        }
    }
}

function alb_upload_plugin_submit()
{
    action = "http://"+document.getElementById('upload_hostname').value+"/alb/alb_upload_plugin_submit.php?alb_id="+iu_alb_id+"&pic_censur=";

    if(document.getElementById('pic_censur_0').checked)
    {
        action = action + "0";
    }
    else if(document.getElementById('pic_censur_1').checked)
    {
        action = action + "1";
    }
    else if(document.getElementById('pic_censur_2').checked)
    {
        action = action + "2";
    }
    else
    {
        alert('Velg rettighetsnivå.');
        return false;
    }

    getImageUploader("ImageUploader").setAction(action);
    //getImageUploader('ImageUploader').Send();
    return true;
}

function alb_delete_confirm(alb_id)
{
    if(confirm('Er du sikker på at du vil slette albumet samt tilhørende bilder?'))
        window.location = "alb/alb_sub.php?action=delete&alb_id=" + alb_id;
    return false;
}

function pm_send_submit()
{
    if(document.getElementById('pm_subject').value.length == 0)
    {
        alert('Skriv inn tittel.');
        return false;
    }

    button_disable('pm_send_submit_button');
    return true;
}

function usr_del()
{
    if(confirm('Er du sikker på at du vil slettes?'))
    {
        document.getElementById('usr_del_submit').value = 'Vent...';
        document.getElementById('usr_del_submit').disabled='disabled';
        return true;
    }
    else
    {
        return false;
    }
}

function hme_upload_choose_alb_id()
{
    alb_id = document.getElementById('hme_upload_alb_id').value;
    if(alb_id == 0)
    {
        alert('Velg album å laste opp til');
    }
    else
    {
        window.location = '/?page=alb_upload&alb_id=' + alb_id;
    }
}

function acc_level_owner_change(usr_id, location)
{
    location_tmp = 'acc/acc_submit.php?acc_usr_id=' + usr_id + '&returnUrl=' + location;

    if(document.getElementById('acc_friend_' + usr_id).checked)
        location_tmp += '&acc_friend=1';

    window.location = location_tmp;
}

function pm_list_line_mark(o)
{
    o.className = 'line_marked';
}

function pm_list_line_unmark(o)
{
    o.className = 'line_unmarked';
}

function button_disable(but_id)
{
    document.getElementById(but_id).value = 'Vent...';
    document.getElementById(but_id).disabled = 'disabled';
}

/*
function tip_preview_update()
{
    s = new String(document.getElementById('tip_preview_raw').innerHTML);
    s = s.replace('[tip_name]', document.getElementById('tip_name').value);
    s = s.replace('[tip_email]', document.getElementById('tip_email').value);
    s = s.replace('[tip_text]', document.getElementById('tip_text').value);
    s = s.replace(/\r|\n|\r\n/g, "<br />");

    document.getElementById('tip_preview').innerHTML = s;
}
*/

var pic_preload_loading = false;
var pic_preload_goto = false;
var pic_preload_goto_url = null;
function pic_preload_click()
{
    if(!pic_preload_loading)
        return true;

    pic_preload_goto = true;
    return false;
}

function pic_preload_loaded()
{
    /*alert('laddad');*/
    pic_preload_loading = false;

    document.getElementById('pic_preload_text').style.visibility = 'hidden';
    if(pic_preload_goto && pic_preload_goto_url!=null)
    {
        window.location = pic_preload_goto_url;
    }
}

function pic_slideshow_toggle()
{
    if(pic_slideshow_running)
        pic_slideshow_stop();
    else
        pic_slideshow_start();
}

function pic_slideshow_start()
{
    setCookie('pic_slideshow_interval', 4);
    pic_slideshow_running = true;
    document.getElementById('pic_slideshow_startstop').innerHTML = 'Stopp bildefremvisning';
    pic_slideshow_slide();
}

function pic_slideshow_stop()
{
    setCookie('pic_slideshow_interval', 0);
    pic_slideshow_running = false;
    document.getElementById('pic_slideshow_startstop').innerHTML = 'Start bildefremvisning';
}

function pic_slideshow_slide()
{
    if(pic_slideshow_running)
    {
        if(pic_preload_loading)
            setTimeout("pic_slideshow_slide();", 100);
        else
            window.location = pic_slideshow_next_url;
    }
}

function pic_show_options_submit()
{
    if(document.getElementById('pic_show_options').value.substring(0, 6) == 'alert ')
    {
        alert(document.getElementById('pic_show_options').value.substring(6));
        return;
    }

    window.location = document.getElementById('pic_show_options').value;
}

function manageCheckboxes(formblock, name_suffix, whatToDo, checkboxController)
{
    if(whatToDo == null)
    {
        if(checkboxController.checked)
            whatToDo = 'checkAll';
        else
            whatToDo = 'uncheckAll';
    }


    forminputs = formblock.getElementsByTagName('input');

    for(i=0; i<forminputs.length; i++)
    {
        if(forminputs[i].type == 'checkbox')
        {
            if(forminputs[i].name.slice(0, name_suffix.length) == name_suffix)
            {
                switch(whatToDo)
                {
                    case 'checkAll':
                        forminputs[i].checked = true;
                        break;

                    case 'uncheckAll':
                        forminputs[i].checked = false;
                        break;

                    case 'inverseSelection':
                        if(forminputs[i].checked)
                            forminputs[i].checked = false;
                        else
                            forminputs[i].checked = true;
                        break;

                    default:
                        break;
                }
            }
        }
    }
}

function acc_list_checkAllSwitch(upperLower)
{
    if(upperLower == 'upper')
    {
        o = document.getElementById('acc_list_checkAll_upper');
        if(document.getElementById('acc_list_checkAll_lower'))
            document.getElementById('acc_list_checkAll_lower').checked = o.checked;
    }
    else
    {
        o = document.getElementById('acc_list_checkAll_lower');
        if(document.getElementById('acc_list_checkAll_upper'))
            document.getElementById('acc_list_checkAll_upper').checked = o.checked;
    }

    if(o.checked)
    {
        whatToDo = 'checkAll';
    }
    else
    {
        whatToDo = 'uncheckAll';
    }

    manageCheckboxes(document.getElementById('acc_list_form'), 'acc_list_usr_id_', whatToDo, null);
}

function pic_move_submit()
{
    if(document.getElementById('pic_move_to_alb_id').value == 0)
    {
        alert('Velg album du vil flytte bildene til.');
        return false;
    }

    button_disable('pic_move_submit_button');

    return true;
}

function abuse_new_submit(ses_usr_id)
{
    if(document.getElementById('abuse_descr').value.length == 0)
    {
        alert('Skriv inn en beskrivelse av problemet');
        return false;
    }

    if(ses_usr_id == 1)
    {
        if(document.getElementById('abuse_reporter_name').value.length == 0)
        {
            alert('Skriv inn navnet ditt');
            return false;
        }

        if(document.getElementById('abuse_reporter_email').value.length == 0)
        {
            alert('Skriv inn e-postadressen din');
            return false;
        }
    }

    if(document.getElementById('abuse_reporter_phone').value.length == 0)
    {
        alert('Skriv inn telefonnummeret ditt');
        return false;
    }

    return true;
}

function recruit_submit()
{
    emails_in_form = 6;
    emails_not_empty = 0;


    for(i=0 ; i<emails_in_form ; i++)
    {
        if(document.getElementById('recruit_email_' + i).value.length > 0)
            emails_not_empty++;
    }

    if(emails_not_empty == 0)
    {
        alert('Skriv iallfall inn en e-postadresse.');
        return false;
    }

    if(document.getElementById('recruit_text').value.length == 0)
    {
        alert('Skriv en melding du ønsker å sende med.');
        return false;
    }

    return true;
}

function alb_edit_pic_censur_submit()
{
    if(document.getElementById('alb_edit_pic_censur').value == -1)
    {
        alert('Velg ønsket rettighet for bildene først.');
        return false;
    }

    button_disabled('alb_edit_pic_censur_submit_button');
    return true;
}

function pic_edit_submit()
{
    var year  = parseInt(document.getElementById('pic_time_year').value);
    var month = parseInt(document.getElementById('pic_time_month').value);
    var day   = parseInt(document.getElementById('pic_time_day').value);
    var hour  = parseInt(document.getElementById('pic_time_hour').value);
    var min   = parseInt(document.getElementById('pic_time_min').value);
    var sec   = parseInt(document.getElementById('pic_time_sec').value);

    if(year!=0 || month!=0 || day!=0)
    {
        if(year >= 2000)
            year = year - 2000;
        else
            year = year - 1900;

        var myDate = new Date(year, month-1, day);

        if(myDate.getYear() != year ||
           myDate.getMonth() != month-1 ||
           myDate.getDate() != day)
        {
            alert('Feil dato');
            return false;
        }


        if(hour<0 || hour>23 || min<0 || min>59 || sec<0 || sec>59)
        {
            alert('Feil angitt tidspunkt.');
            return false;
        }
    }

    return true;
}

// Functions for manage cookies

// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function setCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "/") +
    ((domain) ? "; domain=" + domain : '.pixbox.no') +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function deleteCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//
//  Examples - Unremark if you want to see how this code works
/*
var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now
SetCookie ("ccpath", "http://www.hidaho.com/colorcenter/", expdate);
SetCookie ("ccname", "hIdaho Design ColorCenter", expdate);
SetCookie ("tempvar", "This is a temporary cookie.");
SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
SetCookie ("goner", "This cookie must die!");
document.write (document.cookie + "<br>");
DeleteCookie ("goner");
document.write (document.cookie + "<br>");
document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
document.write ("ccname = " + GetCookie("ccname") + "<br>");
document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
*/



function pic_rate_stars_highlight(color, rate)
{
    var img_src, fillness;

    for(i=1 ; i<=5 ; i++)
    {
        img_src = "/images/pic_rate_star_";
        fillness = rate-i+1;
        if(fillness < 0.25)
            img_src += "grey";
        else if(fillness < 0.5)
            img_src += color+"_1_4";
        else if(fillness < 0.75)
            img_src += color+"_1_2";
        else if(fillness < 1)
            img_src += color+"_3_4";
        else
            img_src += color+"_1";
        img_src += ".gif";

        document.getElementById('pic_rate_star_'+i).src = img_src;
    }
}

function poca_email_submit()
{
    if(document.getElementById('poca_email_validate_comp_form'))
    {
        if(!document.getElementById('poca_comp_participate_yes').checked && !document.getElementById('poca_comp_participate_no').checked)
        {
            alert('Velg om du skal være med i konkurransen eller ikke.');
            return false;
        }

        if(document.getElementById('poca_comp_participate_yes').checked)
        {
            id_i = 0;
            while(true)
            {
                o = document.getElementById('poca_comp_'+id_i);
                if(o)
                {
                    if(o.checked)
                        break;
                }
                else
                {
                    alert('Velg kategori å konkurrere i.');
                    return false;
                }
                id_i++;
            }
        }
    }

    return true;
}

function forum_topic_new_submit()
{
    if(document.getElementById('forum_topic_new_forum').value == 0)
    {
        alert('Velg kategori først.');
        return false;
    }
    if(document.getElementById('forum_topic_new_subject').value.length < 5)
    {
        alert('Skriv inn en lengre rubrikk.');
        return false;
    }
    if(document.getElementById('forum_topic_new_subject').value.length > 50)
    {
        alert('Rubrikken er for lang. Maks 50 tegn.');
        return false;
    }
    if(document.getElementById('forum_topic_new_body').value.length < 4)
    {
        alert('Skriv inn et innhold.');
        return false;
    }

    return true;
}

function forum_reply_submit()
{
    if(document.getElementById('forum_reply_body').value.length < 4)
    {
        alert('Skriv inn et svar.');
        return false;
    }

    return true;
}

function forum_post_edit_submit(is_topic)
{
    if(is_topic)
    {
        if(document.getElementById('forum_post_edit_subject').value.length < 5)
        {
            alert('Skriv inn en lengre rubrikk.');
            return false;
        }
        if(document.getElementById('forum_post_edit_subject').value.length > 50)
        {
            alert('Rubrikken er for lang. Maks 50 tegn.');
            return false;
        }
    }

    if(document.getElementById('forum_post_edit_body').value.length < 4)
    {
        alert('Skriv inn et svar.');
        return false;
    }

    return true;
}

function gb_show_pic_extra(gb, pic)
{
    document.getElementById('gb_pic_extra_img').src = '/images/blank.gif';
    document.getElementById('gb_pic_extra_img').src = document.getElementById('gb_pic_img_url_'+pic).innerHTML;
    document.getElementById('gb_pic_extra_title').innerHTML = document.getElementById('gb_pic_title_'+pic).innerHTML;
    document.getElementById('gb_pic_extra_comments').innerHTML = document.getElementById('gb_pic_comments_'+pic).innerHTML;
    document.getElementById('gb_pic_extra_link').href = '/pic_show_id'+pic+'.html';

    ref_offset = get_pos_offset( document.getElementById('gb_'+gb+'_pic'), document.getElementById('gb_pic_extra') );

    document.getElementById('gb_pic_extra').style.left = (ref_offset[0] - 192/2 - 120/2 + 60)+'px';
    document.getElementById('gb_pic_extra').style.top = (ref_offset[1]-35)+'px';
    //document.getElementById('gb_pic_extra').style.display = 'block';
    document.getElementById('gb_pic_extra').style.visibility = 'visible';
}

function gb_hide_pic_extra()
{
    document.getElementById('gb_pic_extra').style.visibility = 'hidden';
}

function get_pos_offset(ref, match)
{
    ref_offset = [ref.offsetLeft, ref.offsetTop];

    ref_parent_pos = findParentPos(ref);
    match_parent_pos = findParentPos(match);

    parent_offset = [ref_parent_pos[0]-match_parent_pos[0], ref_parent_pos[1]-match_parent_pos[1]];

    return [ (parent_offset[0]+ref_offset[0]), (parent_offset[1]+ref_offset[1]) ];
}


function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function findParentPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function pic_show_add_tag()
{
    document.getElementById('tags_add_input').style.display = 'block';
    document.getElementById('tag_list').style.display = 'block';
    document.getElementById('tags_add_heading').style.display = 'none';
    document.getElementById('tags_input_field').focus();
    document.getElementById('tags_input_field').select();
}

function utf8_encode( s )
{
    alert('utf8_encode: '+s);
    return unescape( encodeURIComponent( s ) );
}

function utf8_decode( s )
{
    alert('utf8_decode: '+s);
    return decodeURIComponent( escape( s ) );
}

function com_new_toggle_form()
{
    if(document.getElementById('com_input').style.display == 'none')
    {
        document.getElementById('com_input').style.display = 'block';
        document.getElementById('com_text').focus();
        document.getElementById('com_text').select();
    }
    else
    {
        document.getElementById('com_input').style.display = 'none';
    }
}

function truncate(text, lenMax)
{
    if(text.length <= lenMax)
        return text;
    else
        return text.substring(0, lenMax-2)+'...';
}

function isArray(obj) {
    if(obj.constructor.toString().indexOf("Array") == -1)
        return false;
    else
        return true;
}

function isFunction(obj) {
    if(typeof obj == 'function')
        return true;
    else
        return false;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/**
 * Cleans the content of the DOM node
 */
function cleanNode(node) {
    while(node.firstChild) {
        node.removeChild( node.firstChild );
    }
}

var namespaceSuffix = new Array();
/**
 * Generates an uniqe name prefix for javascript identifiers
 */
function getNamespacePrefix() {
    while(true) {
        var prefix = '';
        for(i=0 ; i<5 ; i++)
            prefix += String.fromCharCode(getRand(97, 122));

        var ok = true;
        for(i=0 ; i < namespaceSuffix.length ; i++) {
            if(prefix == namespaceSuffix[i]) {
                ok = false;
                break;
            }
        }
        if(ok)
            break;
    }
    namespaceSuffix.push(prefix);
    return prefix;
}

function getRand(min, max) {
    var span = (max - min) + 1;
    var randVal =  Math.floor(Math.random()*span);
    return min + randVal;
}

function normalizeDegrees(d) {
    while(d >= 360)
        d = d - 360;
    while(d < 0)
        d = d + 360;
    return d;
}