var agent_opera = navigator.userAgent.indexOf("Opera") > -1 ? true : false;
var agent_ie = navigator.userAgent.indexOf("MSIE") > -1 ? true : false;
var agent_konqueror = navigator.userAgent.indexOf("Konqueror") > -1 ? true : false;

function CL()
{
    l = document.Login.language.value;
    u = document.Login.nome.value;
    a = document.Login.password.value;

    if(u=="")
    {
        if(l=="it")
            m="Inserire il proprio nome utente";
        else
            m="Enter your user name";

        alert(m);

        document.Login.nome.focus();

        return false;
    }

    if(a=="")
    {
        if(l=="it")
            m="Inserire la propria password";
        else
            m="Enter your password";

        alert(m);

        document.Login.password.focus();

        return false;
    }

    return true;
}

function ChkUsr()
{
    l = document.UserForm.language.value;
    cmd = document.UserForm.cmd.value;

    if(cmd == "nu" || cmd == "admu")
    {
        if(document.UserForm.nome.value == "")
        {
            if(l=="it")
                m="Inserire il nome utente";
            else
                m="Please enter user name";

            alert(m);

            document.UserForm.nome.focus();

            return;
        }
    }

    if(cmd == "nu")
    {
        if(document.UserForm.password.value == "")
        {
            if(l=="it")
                m="Inserire la password";
            else
                m="Please enter a password";

            alert(m);

            document.UserForm.password.focus();

            return;
        }
    }

    if(cmd == "nu")
    {
        if(document.UserForm.rptpassword.value == "")
        {
            if(l=="it")
                m="Ripetere la password";
            else
                m="Please repeat password";

            alert(m);

            document.UserForm.rptpassword.focus();

            return;
        }
    }

    if(document.UserForm.email.value == "")
    {
        if(l=="it")
            m="Inserire l'indirizzo E-Mail";
        else
            m="Enter E-Mail address";

        alert(m);

        document.UserForm.email.focus();

        return;
    }

    if(chkem(document.UserForm.email.value) == false)
    {
        if(l=="it")
            m="Inserire un indirizzo E-Mail valido";
        else
            m="Enter a valid E-Mail address";

        alert(m);

        document.UserForm.email.focus();

        return;
    }

    if(cmd == "nu")
    {
        if(document.UserForm.password.value != document.UserForm.rptpassword.value)
        {
            if(l=="it")
                m="Le password non coincidono";
            else
                m="Passwords do not match";

            alert(m);

            document.UserForm.password.focus();

            return;
        }
    }

    if(cmd == "nu")
    {
        if(document.UserForm.accetta[0].checked == false)
        {
            if(l=="it")
                m="Per registrarsi a EnoForum è necessario accettare le condizioni.";
            else
                m="In order to register to EnoForum it is required to accept conditions.";

            alert(m);

            return;
        }
    }

    if(cmd == "nu")
    {
        if(l=="it")
            m="Si conferma l'invio di questi dati di registrazione?";
        else
            m="Send this registration data?";
    }

    if(cmd == "mu" || cmd == "admu")
    {
        if(l=="it")
            m="Aggiornare il profilo utente?";
        else
            m="Update user profile?";
    }

    if(confirm(m))
        document.UserForm.submit();
    else
        return;
}

function ChkForum()
{
    l = document.ForumForm.language.value;
    cmd = document.ForumForm.cmd.value;

    if(document.ForumForm.nome.value == "")
    {
        if(l=="it")
            m="Inserire il nome del forum";
        else
            m="Please enter forum name";

        alert(m);

        document.ForumForm.nome.focus();

        return;
    }

    if(cmd == "adfa")
    {
        if(l=="it")
            m="Si conferma la creazione di questo forum?";
        else
            m="Create this forum?";
    }

    if(cmd == "adfu")
    {
        if(l=="it")
            m="Si conferma l'aggiornamento di questo forum?";
        else
            m="Update this forum?";
    }

    if(confirm(m))
        document.ForumForm.submit();
    else
        return;
}

function ChkRank()
{
    l = document.RankForm.language.value;
    cmd = document.RankForm.cmd.value;

    if(document.RankForm.nomeit.value == "")
    {
        if(l=="it")
            m="Inserire il nome Italiano del livello";
        else
            m="Enter the Italian name for the rank";

        alert(m);

        document.RankForm.nomeit.focus();

        return;
    }

    if(document.RankForm.nomeen.value == "")
    {
        if(l=="it")
            m="Inserire il nome Inglese del livello";
        else
            m="Enter the English name for the rank";

        alert(m);

        document.RankForm.nomeen.focus();

        return;
    }

    if(cmd == "adra")
    {
        if(l=="it")
            m="Si conferma la creazione di questo livello?";
        else
            m="Create this rank?";
    }

    if(cmd == "adru")
    {
        if(l=="it")
            m="Si conferma l'aggiornamento di questo livello?";
        else
            m="Update this rank?";
    }

    if(confirm(m))
        document.RankForm.submit();
    else
        return;
}

function ChkGroup()
{
    l = document.GroupForm.language.value;
    cmd = document.GroupForm.cmd.value;

    if(document.GroupForm.nomeit.value == "")
    {
        if(l=="it")
            m="Inserire il nome Italiano del gruppo";
        else
            m="Enter the Italian name for the group";

        alert(m);

        document.GroupForm.nomeit.focus();

        return;
    }

    if(document.GroupForm.nomeen.value == "")
    {
        if(l=="it")
            m="Inserire il nome Inglese del gruppo";
        else
            m="Enter the English name for the group";

        alert(m);

        document.GroupForm.nomeen.focus();

        return;
    }

    if(cmd == "adga")
    {
        if(l=="it")
            m="Si conferma la creazione di questo gruppo?";
        else
            m="Create this group?";
    }

    if(cmd == "adgu")
    {
        if(l=="it")
            m="Si conferma l'aggiornamento di questo gruppo?";
        else
            m="Update this group?";
    }

    if(confirm(m))
        document.GroupForm.submit();
    else
        return;
}

function ChkPost()
{
    l = document.MsgForm.language.value;
    cmd = document.MsgForm.cmd.value;

    if(cmd == "at")
    {
        if(document.MsgForm.nome.value == "")
        {
            if(l=="it")
                m="Inserire l'argomento";
            else
                m="Enter Topic";

            alert(m);

            document.MsgForm.nome.focus();

            return;
        }
    }

    if(document.MsgForm.messaggio.value.length <= 0)
    {
        if(l=="it")
            m="Inserire il testo del messaggio";
        else
            m="Enter message text";

        alert(m);

        document.MsgForm.messaggio.focus();

        return;
    }

    if(cmd == "at" || cmd == "ap")
    {
        if(l=="it")
            m="Si conferma l'invio di questo messaggio?";
        else
            m="Post this message?";
    }

    if(cmd == "up")
    {
        if(l=="it")
            m="Si conferma la modifica di questo messaggio?";
        else
            m="Update this message?";
    }

    if(confirm(m))
    {
        document.MsgForm.v.value = "0";
        document.MsgForm.submit();
    }
    else
        return;
}

function ChkQuickPost()
{
    l = document.QuickMsgForm.language.value;

    if(document.QuickMsgForm.messaggio.value.length <= 0)
    {
        if(l=="it")
            m="Inserire il testo del messaggio";
        else
            m="Enter message text";

        alert(m);

        document.QuickMsgForm.messaggio.focus();

        return;
    }

    if(l=="it")
        m="Si conferma l'invio di questo messaggio?";
    else
        m="Post this message?";

    if(confirm(m))
        document.QuickMsgForm.submit();
    else
        return;
}

function PvwPost()
{
    document.MsgForm.v.value = "1";
    document.MsgForm.submit();
}

function ChkCfg()
{
    l = document.CfgForm.language.value;

    if(l=="it")
        m="Conferma aggiornamento?";
    else
        m="Confirm update?";

    if(confirm(m))
        document.CfgForm.submit();
    else
        return;
}

function SaveCaret()
{
    if(agent_ie == true && agent_opera == false && agent_konqueror == false)
        document.MsgForm.messaggio.caretPos = document.selection.createRange().duplicate();
}

function SetEditStyle(style, value)
{
    txt = document.MsgForm.messaggio;
    agent = navigator.userAgent;

    if(agent_ie == true && agent_opera == false && agent_konqueror == false)
        insmode=1;
    else if(txt.selectionEnd && (txt.selectionEnd - txt.selectionStart > 0))
        insmode=2;
    else
        insmode=3;

    switch(style)
    {
        case 1:
        {
            btag="[b]";
            etag="[/b]";
        }
        break;

        case 2:
        {
            btag="[i]";
            etag="[/i]";
        }
        break;

        case 3:
        {
            btag="[u]";
            etag="[/u]";
        }
        break;

        case 4:
        {
            btag="[size=" + value +"]";
            etag="[/size]";
        }
        break;

        case 5:
        {
            btag="[color=" + value +"]";
            etag="[/color]";
        }
        break;

        case 6:
        {
            btag="[quote]";
            etag="[/quote]";
        }
        break;

        case 7:
        {
            btag="[url]";
            etag="[/url]";
        }
        break;

        case 8:
        {
            btag="[align=" + value +"]";
            etag="[/align]";
        }
        break;

        case 9:
        {
            btag="[font=" + value +"]";
            etag="[/font]";
        }
        break;

        case 10:
        {
            btag="[list]\n";
            etag="[/list]\n";
        }
        break;

        case 11:
        {
            btag="[nlist]\n";
            etag="[/nlist]\n";
        }
        break;

        case 12:
        {
            btag="[alist]\n";
            etag="[/alist]\n";
        }
        break;

        case 13:
        {
            btag="[*]";
            etag="";
        }
        break;

        case 50:
        {
            btag=" :-) ";
            etag="";
        }
        break;

        case 51:
        {
            btag=" ;-) ";
            etag="";
        }
        break;

        case 52:
        {
            btag=" :-D ";
            etag="";
        }
        break;

        case 53:
        {
            btag=" :-P ";
            etag="";
        }
        break;

        case 54:
        {
            btag=" 8-) ";
            etag="";
        }
        break;

        case 55:
        {
            btag=" :-o ";
            etag="";
        }
        break;

        case 56:
        {
            btag=" :-(";
            etag="";
        }
        break;

        case 57:
        {
            btag=" :weep: ";
            etag="";
        }
        break;

        case 58:
        {
            btag=" :-| ";
            etag="";
        }
        break;

        case 59:
        {
            btag=" :-? ";
            etag="";
        }
        break;

        case 60:
        {
            btag=" 8-O ";
            etag="";
        }
        break;

        case 61:
        {
            btag=" :-@ ";
            etag="";
        }
        break;

        default:
        {
            btag="";
            etag="";
        }
        break;
    }

    switch(insmode)
    {
        case 1:
        {
	    range=document.selection.createRange();
	
            if(document.selection.type == 'Text' && (txt.value.indexOf(range.text) != -1))
            {
		range.text = btag + range.text + etag;

		if(txt.value.indexOf(document.selection.createRange().text) == -1)
			document.selection.createRange().text = range.text;
	    }
	    else
            {
	        if(txt.createTextRange && txt.caretPos)
	        {
                    if(txt.caretPos.text.charAt(txt.caretPos.text.length - 1) == ' ')
		        txt.caretPos.text = btag + etag + ' ';
                    else
		        txt.caretPos.text = btag + etag;
	        }
	        else
		    txt.value  =  txt.value + btag + etag;
            }
        }
        break;

        case 2:
        {
            begtxt = txt.value.substring(0, txt.selectionStart);
            endtxt = txt.value.substring(txt.selectionEnd, txt.value.length);

	    htxt = txt.value.substring(txt.selectionStart, txt.selectionEnd);

            txt.value = begtxt + btag + htxt + etag + endtxt;
        }
        break;

        default:
        {
            txt.value = txt.value + btag + etag;
        }
        break;
    }
}

function PListMode()
{
    v = document.ListMode.view.options[document.ListMode.view.selectedIndex].value;
    l = document.ListMode.list.options[document.ListMode.list.selectedIndex].value;
    o = document.ListMode.sort.options[document.ListMode.sort.selectedIndex].value;

    document.ListMode.action += "&v=" + v + "&l=" + l + "&o=" + o;

    document.ListMode.submit();
}

function TListMode()
{
    l = document.ListMode.list.options[document.ListMode.list.selectedIndex].value;
    o = document.ListMode.sort.options[document.ListMode.sort.selectedIndex].value;

    document.ListMode.action += "&l=" + l + "&o=" + o;

    document.ListMode.submit();
}

function ChkTopic()
{
    l = document.EditTopic.language.value;

    if(document.EditTopic.nome.value=="")
    {
        if(l=="it")
            m="Inserire il nome dell'argomento";
        else
            m="Enter Topic Name";

        alert(m);

        document.RenameTopic.nome.focus();

        return;
    }

    if(l=="it")
        m="Si conferma l'aggiornamento di questo Argomento?";
    else
        m="Update this Topic?";

    if(confirm(m))
        document.EditTopic.submit();
    else
        return;
}

function ChkPwdReq()
{
    l = document.PwdReq.language.value;

    if(document.PwdReq.email.value == "")
    {
        if(l=="it")
            m="Inserire l'indirizzo E-Mail";
        else
            m="Enter E-Mail address";

        alert(m);

        document.PwdReq.email.focus();

        return;
    }

    if(chkem(document.PwdReq.email.value) == false)
    {
        if(l=="it")
            m="Inserire un indirizzo E-Mail valido";
        else
            m="Enter a valid E-Mail address";

        alert(m);

        document.PwdReq.email.focus();

        return;
    }

    document.PwdReq.submit();
}

function ChkNewPwd()
{
    l = document.NewPwd.language.value;

    if(document.NewPwd.password.value == "")
    {
        if(l=="it")
            m="Inserire la password";
        else
            m="Please enter a password";

        alert(m);

        document.NewPwd.password.focus();

        return;
    }

    if(document.NewPwd.rptpassword.value == "")
    {
        if(l=="it")
            m="Ripetere la password";
        else
            m="Please repeat password";

        alert(m);

        document.NewPwd.rptpassword.focus();

        return;
    }

    if(document.NewPwd.password.value != document.NewPwd.rptpassword.value)
    {
        if(l=="it")
            m="Le password non coincidono";
        else
            m="Passwords do not match";

        alert(m);

        document.NewPwd.password.focus();

        return;
    }

    if(l=="it")
        m="Conferma cambio password?";
    else
        m="Change your password?";

    if(confirm(m))
        document.NewPwd.submit();
    else
        return;
}

function ChkWord()
{
    l = document.WordForm.language.value;
    cmd = document.WordForm.cmd.value;

    if(document.WordForm.word.value == "")
    {
        if(l=="it")
            m="Inserire il termine";
        else
            m="Enter word";

        alert(m);

        document.WordForm.word.focus();

        return;
    }

    if(cmd == "adwa")
    {
        if(l=="it")
            m="Si conferma la creazione di questo termine?";
        else
            m="Create this word?";
    }

    if(cmd == "adwu")
    {
        if(l=="it")
            m="Si conferma l'aggiornamento di questo termine?";
        else
            m="Update this word?";
    }

    if(confirm(m))
        document.WordForm.submit();
    else
        return;
}

function ChkAvatar()
{
    l = document.AvatarForm.language.value;
    cmd = document.AvatarForm.cmd.value;

    if(document.AvatarForm.nome.value == "")
    {
        if(l=="it")
            m="Inserire la descrizione";
        else
            m="Enter description";

        alert(m);

        document.AvatarForm.nome.focus();

        return;
    }

    if(document.AvatarForm.file.value == "")
    {
        if(l=="it")
            m="Inserire il nome del file";
        else
            m="Enter file name";

        alert(m);

        document.AvatarForm.file.focus();

        return;
    }

    if(cmd == "adaa")
    {
        if(l=="it")
            m="Si conferma la creazione di questo avatar?";
        else
            m="Create this avatar?";
    }

    if(cmd == "adau")
    {
        if(l=="it")
            m="Si conferma l'aggiornamento di questo avatar?";
        else
            m="Update this avatar?";
    }

    if(confirm(m))
        document.AvatarForm.submit();
    else
        return;
}

function SelectAvatar()
{
    avpath="/images/enoforum/avatars/";
    avpath += document.UserForm.avatarname.options[document.UserForm.avatarname.selectedIndex].value;

    document.images.avatarimg.src=avpath;
}

function ShowAvatarList(language)
{
    url = '/actions/AvatarList.php?l=' + language;

    window.open(url, '_pickavatar', 'height=300,width=500,resizable=yes,scrollbars=yes');
}

function PickAvatar(file)
{
    avsel = opener.document.forms['UserForm'].avatarname;

    for(i=0; i<avsel.length; i++)
    {
        if(avsel.options[i].value == file)
            avsel.options[i].selected = true;
    }

    avpath="/images/enoforum/avatars/" + file;

    opener.document.images['avatarimg'].src = avpath;

    this.close();
}
