/**
 * @package Freizeit-Treff
 * @category Gemeinsames Javascript
 * @author Göran Bruns <goeran@brunsware.de>
 * @copyright 2010
 * @version 1.0.2
 */
var contactEntryCount=0;
var guestEntryCount=0;


function PrintMsg(msgId)
{
    printMsg = window.open("msg_print.php?action=print&uid="+msgId,"PrintMsg","width=800,height=600,dependent=yes,location=no");    
}
function SelectAllEntries(formname,fieldname)
{
    
    var theFieldArray = document.forms[formname].elements[fieldname+"[]"];
    if(theFieldArray)
    {
        if(theFieldArray.length)
        {
            for(var i = 0; i < theFieldArray.length; i++)
            {
                theFieldArray[i].checked = true;
            }
        }
        else
        {
            theFieldArray.checked = true;
        }
    }
}
function SignOff(action,id,msg)
{
    if(confirm(msg))
    {
        document.forms[1].elements.uid.value=id;
        document.forms[1].elements.action.value=action;
        document.forms[1].submit();
    }
}

function SubmitForm(action,id)
{
    document.forms[1].elements.uid.value=id;
    document.forms[1].elements.action.value=action;
    document.forms[1].submit();    
}
function SubmitFormEx(action,id,formName,target)
{
    if(target && target != "")
    {
        var oldTarget = document.forms[formName].target;
        document.forms[formName].target=target;
    }   
    document.forms[formName].elements.uid.value=id;
    document.forms[formName].elements.action.value=action;
    document.forms[formName].submit();
    if(target && target != "")
    {
        document.forms[formName].target=oldTarget;
    }
}

function SubmitMailboxForm(folder,id)
{
    document.forms.mailbox.elements.uid.value=id;
    document.forms.mailbox.elements.folder.value=folder;
    if(folder == 'delete')
    {
        if(!confirm("Wirklich löschen ?"))
        {
            return;
        }
    }
    document.forms[1].submit();    
}

function SubmitMailboxActionForm(folder,id,action)
{
    if(action == 'delete')
    {
        if(!confirm("Wirklich löschen ?"))
        {
            return;
        }
    }
    document.forms.mailbox.elements.uid.value=id;
    document.forms.mailbox.elements.folder.value=folder;
    document.forms.mailbox.elements.action.value=action;
    document.forms.mailbox.submit();    
}

function MailboxCheck() {
    if(document.forms.mailbox.elements.receiver.value.length == 0
        || document.forms.mailbox.elements.receiverKey.value == 0) {
        alert("Empfänger auswählen !");
        return false;
    }
    if(document.forms.mailbox.elements.title.value.length == 0) {
        document.forms.mailbox.elements.title.focus();
        alert("Betreff eingeben !");
        return false;
    }
    if(document.forms.mailbox.elements.text.value.length == 0) {
        document.forms.mailbox.elements.text.focus();
        alert("Nachricht eingeben !");
        return false;
    }
    return true;
}

function Delete(id)
{
    if(confirm("Wirklick löschen ?"))
    {
        document.forms[1].elements.uid.value=id;
        document.forms[1].elements.action.value='delete';
        document.forms[1].submit();
    }
}

function AddContactEntries()
{
    var contactAddButton = document.getElementById("contactAddButton");
    for(var i=0;i<5;i++)
    {
        contactAddButton.parentNode.insertBefore(CreateContactEntry("","","","",""),contactAddButton);
    }  
}

function AddContactEntry(forename,name,email,cellular,group)
{
    var contactAddButton = document.getElementById("contactAddButton");
    contactAddButton.parentNode.insertBefore(CreateContactEntry(forename,name,email,cellular,group),contactAddButton);
}

function CreateGroupSelect(group)
{
    var GroupSelect = document.createElement("select");
    GroupSelect.setAttribute("name","group[]");
    for(var i=0;i<groups.length;i++)
    {
        var GroupOption = document.createElement("option");
        GroupOption.setAttribute("value",groups[i][0]);
        GroupOption.appendChild(document.createTextNode(groups[i][2]));
        GroupSelect.appendChild(GroupOption);        
    }
    GroupSelect.selectedIndex=group;
    var ContactEntryData = document.createElement("td");
    ContactEntryData.setAttribute("class","contact entry");
    ContactEntryData.appendChild(GroupSelect);
    return ContactEntryData;
}

function CreateContactEntry(forename,name,email,cellular,group)
{
    var ContactId = "contactEntryRow_"+ contactEntryCount;
    var ContactEntryRow = document.createElement("tr");
    ContactEntryRow.setAttribute("id",ContactId);
    var ContactEntryData = document.createElement("td");
    ContactEntryData.setAttribute("class","contact entry");
        
    var DataInput = document.createElement("input");
    DataInput.setAttribute("type","text");
    DataInput.setAttribute("maxlength","100");
    DataInput.setAttribute("size","20");
/*
    var DelInput = document.createElement("input");
    DelInput.setAttribute("type","button");
    DelInput.setAttribute("value","X");
    DelInput.setAttribute("class","button x");
    DelInput.setAttribute("onClick","DeleteEntry('"+ContactId+"')");
*/

    ContactEntryData.appendChild(DataInput);
    
    var ForenameData = ContactEntryData.cloneNode(true);
    ForenameData.firstChild.setAttribute("name","forename[]");
    ForenameData.firstChild.setAttribute("value",forename);
    ForenameData.firstChild.setAttribute("size","15");
    ContactEntryRow.appendChild(ForenameData);

    var NameData = ContactEntryData.cloneNode(true);
    NameData.firstChild.setAttribute("name","name[]");
    NameData.firstChild.setAttribute("value",name);
    NameData.firstChild.setAttribute("size","15");
    ContactEntryRow.appendChild(NameData);

    var EmailData = ContactEntryData.cloneNode(true);
    EmailData.firstChild.setAttribute("name","email[]");
    EmailData.firstChild.setAttribute("value",email);
    ContactEntryRow.appendChild(EmailData);

    var CellularData = ContactEntryData.cloneNode(true);
    CellularData.firstChild.setAttribute("name","cellular[]");
    CellularData.firstChild.setAttribute("value",cellular);
    CellularData.firstChild.setAttribute("maxlength","100");
    CellularData.firstChild.setAttribute("size","11");
    ContactEntryRow.appendChild(CellularData);
/*    
    var BirthdayData = ContactEntryData.cloneNode(true);
    BirthdayData.firstChild.setAttribute("name","birthday[]");
    BirthdayData.firstChild.setAttribute("value",birthday);
    BirthdayData.firstChild.setAttribute("maxlength","10");
    BirthdayData.firstChild.setAttribute("size","11");
    ContactEntryRow.appendChild(BirthdayData);
*/
    ContactEntryRow.appendChild(CreateGroupSelect(group));

    var DelData = ContactEntryData.cloneNode(false);
//    DelData.appendChild(DelInput);
    ContactEntryRow.appendChild(DelData);

    contactEntryCount++;
    return ContactEntryRow; 
}

function DeleteEntry(entryId)
{
    var entry = document.getElementById(entryId);
    entry.parentNode.removeChild(entry);
}

function DeleteContact(contactId)
{
    if(confirm("Wirklick löschen ?"))
    {
        document.forms[1].elements.deleteContact.value=contactId;
        document.forms[1].submit();
    }
}

function UpdateContact(id)
{
    document.forms[1].elements.uid.value=id;
    document.forms[1].elements.action.value="update";
    document.forms[1].submit();
}

function Redirect(url)
{
    window.location.href = url;
}

function LoadGroups()
{
    var groupList1 = document.getElementById("group1");
    var groupList2 = document.getElementById("group2");
    var listIndex1 = groupList1.selectedIndex;
    var listIndex2 = groupList2.selectedIndex;

    while(groupList1.hasChildNodes())
    {
        groupList1.removeChild(groupList1.firstChild);
    }
    while(groupList2.hasChildNodes())
    {
        groupList2.removeChild(groupList2.firstChild);
    }

    for(var i = 0; i < groups.length; i++)
    {
        var optionEntry = document.createElement("option");
        optionEntry.setAttribute("value",groups[i][0]);
        var optionEntryName = document.createTextNode(groups[i][2]);
        optionEntry.appendChild(optionEntryName);
        groupList1.appendChild(optionEntry);
        groupList2.appendChild(optionEntry.cloneNode(true));        
    }
    if(listIndex1 != -1)
    {
        groupList1.selectedIndex = listIndex1;
    }
    else
    {
        groupList1.selectedIndex = 0;
    }
    if(listIndex2 != -1)
    {
        groupList2.selectedIndex = listIndex2;
    }
    else
    {
        groupList2.selectedIndex = 0;
    }

    LoadList("list1","group1");
    LoadList("list2","group2");
}


/*
function LoadList(list,group)
{
    var contactList = document.getElementById(list);
    var groupList = document.getElementById(group);
    var groupId = groupList.value;
    var contactsToAdd = new Array();
    // clear list
    while(contactList.hasChildNodes())
    {
        contactList.removeChild(contactList.firstChild);
    }
    var j=0;
    for( var i=0; i < contacts.length; i++)
    {
        if(contacts[i][1] == groupId || groupId == -1)
        {
            contactsToAdd[j] = contacts[i];
            j++;
        }
    }
    contactsToAdd.sort(ContactsSort);
    for( var i=0; i < contactsToAdd.length; i++)
    {
        var optionEntry = document.createElement("option");
        optionEntry.setAttribute("value",contactsToAdd[i][0]);
        var optionEntryName = document.createTextNode(contactsToAdd[i][2]);
        optionEntry.appendChild(optionEntryName);
        contactList.appendChild(optionEntry);
    }    
}
*/
function LoadList(list,group)
{
    var contactList = document.getElementById(list);
    var groupList = document.getElementById(group);
    var groupId = groupList.value;
    // clear list
    while(contactList.hasChildNodes())
    {
        contactList.removeChild(contactList.firstChild);
    }
    for( var i=0; i < contacts.length; i++)
    {
        if(contacts[i][1] == groupId || groupId == -1)
        {
        var optionEntry = document.createElement("option");
        optionEntry.setAttribute("value",contacts[i][0]);
        var optionEntryName = document.createTextNode(contacts[i][2]);
        optionEntry.appendChild(optionEntryName);
        contactList.appendChild(optionEntry);
        }
    }
}


function ContactsSort(contact1,contact2)
{
    return contact1[2] == contact2[2];   
}

function DeleteGroup(group)
{
    var groupList = document.getElementById(group);
    var groupId = groupList.value;
    for(var i = 0; i < groups.length; i++)
    {
        if(groupId == groups[i][0])
        {
            if(groups[i][1] == 0)
            {
                alert("Dies ist eine Standard-Gruppe und kann nicht gelöscht werden");
                return;
            }
            else
            {
                var confirmed = false;
                for( var j = 0; j < contacts.length; j++)
                {
                    if(contacts[j][1] == groupId)
                    {
                        if(!confirmed)
                        {
                            if(!confirm("Dieser Gruppe sind noch Kontakte zugeordnet. Möchten Sie die Gruppe trotzdem löschen ?"))
                            {
                                return;
                            }
                            else
                            {
                                confirmed = true;
                            }
                            
                        }
                        contacts[j][1] = 0;
                    }
                }
                groups.splice(i,1);
                groupList.selectedIndex = -1;
                LoadGroups();
                
            }
        }
    }
}

function InsertGroup(groupId,groupOwner,groupName)
{
    for(var i=0; i < groups.length; i++)
    {
        if(groups[i][3] == groupName)
        {
            return;
        }
    }
    group = new Array();
    group[0] = groupId;
    group[1] = groupOwner;
    group[2] = groupName;
    group[3] = 1;
    groups[groups.length] = group;
}

function Move1to2()
{
    MoveContact("list1","list2","group2","group1");
}

function Move2to1()
{
    MoveContact("list2","list1","group1","group2");
}

function MoveContact(list1,list2,group1,group2)
{
    var contactList1 = document.getElementById(list1);
    var contactList2 = document.getElementById(list2);
    var groupList1 = document.getElementById(group1);
    var groupList2 = document.getElementById(group2);
    
    var groupId1 = groupList1.value;
    var groupId2 = groupList2.value;
    if(groupId1 != groupId2)
    {
        for(var i = 0; i < contactList1.options.length; i++)
        {
            if(contactList1.options[i].selected == true)
            {
                var contact = contactList1.removeChild(contactList1.childNodes[i]);
                var idx = contactList2.selectedIndex;
                contactList2.appendChild(contact);
                contactList2.selectedIndex = idx;
                var contactId = contact.getAttribute("value");
                for( var j=0; j < contacts.length; j++)
                {
                    if(contacts[j][0] == contactId)
                    {
                        contacts[j][1] = groupId1;
                        break;
                    }
                }
                i--;
            }
        }
    }    
}
/*
function DeleteContact(list)
{
    var contactList = document.getElementById(list);
    var deleted = false;
    for(var i = 0; i < contactList.options.length; i++)
    {
        if(contactList.options[i].selected == true)
        {
            if(confirm("Wollen Sie den Kontakt \""+ contactList.options[i].text+"\" wirklich löschen ?"))
            {
                for(var j = 0; j < contacts.length; j++)
                {
                    if(contacts[j][0] == contactList.options[i].value)
                    {
                        contacts.splice(j,1);
                        break;   
                    }
                }
                deleted = true;
            }
        }
    }
    if(deleted == true)
    {
        LoadGroups();
    }
}
*/

function NewGroup()
{
    newGroup = window.open("groups.php?group&newGroup=","NewGroup","width=300,height=100,left=400,top=300,dependent=yes,location=no");
    newGroup.focus();
}

function DelGroup(groupId)
{
    for(var i=0;i< groups.length;i++)
    {
        if(groups[i][0] == groupId && groups[i][1] != 0)
        {
            if(confirm("Dieser Gruppe sind schon Kontakte zu geordnet.\nTrotzdem löschen ?"))
            {
                break;
            }
            else
            {
                return;
            }
        }
    }
    document.forms[1].elements.delGroup.value=groupId;
    document.forms[1].submit();
}
/*
function NewGroup()
{
    var groupName = prompt("Welchen Namen soll die neue Gruppe haben ?","");
    var groupId = 0;
    for (var i = 0; i < groups.length; i++)
    {
        if(groups[i][0] > groupId)
        {
            groupId = groups[i][0];
        }
        if(groups[i][2] == groupName)
        {
            alert("Eine Gruppe mit diesem Namen existiert schon."+groups[i][2]+ " "+groupName);
            return;
        }
    }
    InsertGroup(groupId,ownerId,groupName);
    LoadGroups();
    var groupList = document.getElementById("group2");
    groupList.selectedIndex = groupList.options.length - 1;
    LoadList("list2","group2");
}
*/
function SubmitContacts()
{
    var contactForm = document.forms.contacts;
    for(var i=0; i < groups.length; i++)
    {
        if(groups[i][1] != 0)
        {
            contactForm.appendChild(CreateHiddenField("group_id[]",groups[i][0]));
            contactForm.appendChild(CreateHiddenField("group_name[]",groups[i][2]));
            contactForm.appendChild(CreateHiddenField("group_new[]",groups[i][3]));
        }
    }
    for(var i=0; i < contacts.length; i++)
    {
        contactForm.appendChild(CreateHiddenField("contact_id[]",contacts[i][0]));
        contactForm.appendChild(CreateHiddenField("contact_group[]",contacts[i][1]));
    }
    contactForm.appendChild(CreateHiddenField("do_edit",1));
    contactForm.submit();
}

function CreateHiddenField(_name,_value)
{
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type","hidden");
    hiddenField.setAttribute("name",_name);
    hiddenField.setAttribute("value",_value);
    return hiddenField;
}

function AddGroup2Entries()
{
    var groupList = document.getElementById("group1");
    var entryList = document.getElementById("entries");
    var groupId = groupList.value;
    for( var i=0; i < contacts.length; i++)
    {
        if(contacts[i][1] == groupId || groupId == -1)
        {
            if(!CheckForContact(contacts[i][0],entryList))
            {
                var optionEntry = document.createElement("option");
                optionEntry.setAttribute("value",contacts[i][0]);
                var optionEntryName = document.createTextNode(contacts[i][2]);
                optionEntry.appendChild(optionEntryName);
                entryList.appendChild(optionEntry);
            }
        }
    }    
}

function LoadGroup()
{
    var groupList1 = document.getElementById("group1");
    var listIndex1 = groupList1.selectedIndex;

    while(groupList1.hasChildNodes())
    {
        groupList1.removeChild(groupList1.firstChild);
    }

    for(var i = 0; i < groups.length; i++)
    {
        var optionEntry = document.createElement("option");
        optionEntry.setAttribute("value",groups[i][0]);
/*        optionEntry.setAttribute("onDblClick","AddContact2Entries()");*/
        var optionEntryName = document.createTextNode(groups[i][2]);
        optionEntry.appendChild(optionEntryName);
        groupList1.appendChild(optionEntry);
    }
    if(listIndex1 != -1)
    {
        groupList1.selectedIndex = listIndex1;
    }
    else
    {
        groupList1.selectedIndex = 0;
    }
    LoadList("list1","group1");
}


function CheckForContact(contactId,entryList)
{
    for(var i=0; i < entryList.options.length; i++)
    {
        if(entryList.options[i].value == contactId)
        {
            return true;
        }
    }    
    return false;
}

function AddContact2Entries()
{
    var contactList = document.getElementById("list1");
    var entryList = document.getElementById("entries");
    for(var i = 0; i < contactList.options.length; i++)
    {
        if(contactList.options[i].selected == true)
        {
            if(!CheckForContact(contactList.options[i].value,entryList))
            {
                entryList.appendChild(contactList.options[i].cloneNode(true));
            }
            else
            {
                alert("Kontakt \""+ contactList.options[i].text +"\" ist schon in der Liste enthalten.");
            }
        }
    }
}

function RemoveContactFromEntries()
{
    var entryList = document.getElementById("entries");
    for(var i = 0; i < entryList.options.length; i++)
    {
        if(entryList.options[i].selected == true)
        {
            entryList.removeChild(entryList.childNodes[i]);
            i--;
        }
    }
}

function SubmitAction()
{
    var entryList = document.getElementById("entries");
    if(entryList.options.length == 0)
    {
        alert("Bitte wählen Sie Gäste aus.");
        return;
    }
    for(var i = 0; i < entryList.options.length; i++)
    {
        entryList.options[i].selected = true;
    }
    document.forms.newaction.submit();
}

function SubmitGuests(thisButton)
{
    var entryList = document.getElementById("entries");
    if(entryList.options.length == 0)
    {
        alert("Bitte wählen Sie Gäste aus.");
        return;
    }
    for(var i = 0; i < entryList.options.length; i++)
    {
        entryList.options[i].selected = true;
    }
    thisButton.form.submit();
}


function ViewInvitation(Id)
{
    document.forms.invitation.actionId.value = Id;
    document.forms.invitation.submit();
    return false;
}
function ViewAction(Id)
{
    document.forms.action.actionId.value = Id;
    document.forms.action.submit();
    return false;
}

function OpenContacts()
{
    contactsWin = window.open("mailbox.php?folder=contacts","Kontakte","width=600,height=450,left=400,top=300,dependent=yes,location=no");
    contactsWin.focus();
}

function OpenPassword()
{
    passwordWin = window.open("/password.php","PasswortVergessen","width=500,height=150,left=400,top=300,location=no");
    passwordWin.focus();
}


function AddContact(contactId)
{
    document.forms.invitation.action.value = "addcontact";
    document.forms.invitation.contact.value = contactId;
    document.forms.invitation.submit();
}

function SwitchGroupSelect(enable)
{
    if(enable)
    {
        document.forms[1].elements.contacts_group.disabled = false;
    }
    else
    {        
        document.forms[1].elements.contacts_group.disabled = true;
    }
}

function AddSpamCheck(theForm,theField)
{
	var hiddenField = document.createElement('input');
	hiddenField.type = "hidden";
	hiddenField.name = "spamcheck";
    hiddenField.value = hex_md5(theForm.elements[theField].value);
	//now add the input to the DOM.
	theForm.appendChild(hiddenField);
}

function CreateGuestEntry(id,guest,notes)
{
    var guestId = "guestEntryRow_"+ guestEntryCount;
    var guestEntryRow = document.createElement("tr");
    guestEntryRow.setAttribute("id",guestId);
    var guestEntryText = document.createElement("td");
    guestEntryText.setAttribute("class","normal");
    guestEntryText.appendChild(document.createTextNode("Gast"));
    guestEntryRow.appendChild(guestEntryText);

    var guestIdInput = document.createElement("input");
    guestIdInput.setAttribute("type","hidden");
    guestIdInput.setAttribute("name","g_id[]");
    guestIdInput.setAttribute("value",id);
    guestEntryRow.appendChild(guestIdInput);

    var guestEntryData = document.createElement("td");            

    var DataInput = document.createElement("input");
    DataInput.setAttribute("type","text");
    DataInput.setAttribute("size","20");

    guestEntryData.appendChild(DataInput);
    
    var guestTextDataHeader = document.createTextNode("Name des Gastes");
    var guestNoteDataHeader = document.createTextNode("Bemerkungen");
    
    var guestTextData = guestEntryData.cloneNode(true);
    guestTextData.firstChild.setAttribute("name","guest[]");
    guestTextData.firstChild.setAttribute("value",guest);
    guestTextData.firstChild.setAttribute("maxlength","50");
    guestTextData.insertBefore(document.createElement("br"),guestTextData.firstChild);
    guestTextData.insertBefore(guestTextDataHeader,guestTextData.firstChild);
    guestEntryRow.appendChild(guestTextData);

    var guestNoteData = guestEntryData.cloneNode(true);
    guestNoteData.firstChild.setAttribute("name","g_notes[]");
    guestNoteData.firstChild.setAttribute("value",notes);
    guestNoteData.firstChild.setAttribute("maxlength","255");
    guestNoteData.insertBefore(document.createElement("br"),guestNoteData.firstChild);
    guestNoteData.insertBefore(guestNoteDataHeader,guestNoteData.firstChild);
    guestEntryRow.appendChild(guestNoteData);

    guestEntryCount++;
    return guestEntryRow; 
}

function AddGuestEntry(id,guest,notes)
{
    if(guestEntryCount == 10)
    {
        return alert("Mehr als 10 Gäste können nicht mitgenommen werden !");
    }
    var guestAddButton = document.getElementById("guestAddButton");
    guestAddButton.parentNode.insertBefore(CreateGuestEntry(id,guest,notes),guestAddButton);
}

function GeneratePw(len,values) {
    var pw = '';
    if(!len) {
        len = 5;        
    }
    if(!values) {
        values="0123456789";
    }
    for(var i=0;i < len;i++) {
        var idx = Math.round(Math.random() * (values.length - 1));
        pw += values.charAt(idx);
    }
    return pw;
}