function delCookie(cookieName) {
    setCookie(cookieName, "", new Date(0));
}

function setCookie(cookieName, cookieValue, expires) {
    var today = new Date();
    var expire = new Date();
    nDays=1;
    if (expires == null ) {
        expire.setTime(today.getTime() + 3600000*24*nDays);
    } else {
        expire = expires;
    }
    document.cookie = cookieName+"="+escape(cookieValue)
        + ";expires="+expire.toGMTString()
        + ";path=/";
}

function portlet_collapse(event, cookieName) {
    var target = event.target ? event.target : event.srcElement;
    //console.log(target);
    imas = target.parentNode.childNodes;
    var regtest = /menu_down/;
    var new_mode = 'none';
    for ( i=0; i<imas.length; i++ ) 
    {
        if ( imas[i].nodeName == 'IMG' ) 
        {
            if ( regtest.test(imas[i].src) )
            { // img src contains menu_down so, it's open
                //console.log(imas[i]);
                new_mode = 'none';
                imas[i].src = 'menu_up.gif';
                setCookie(cookieName, "1");
            } else {
                new_mode = 'block';
                imas[i].src = 'menu_down.gif';
                delCookie(cookieName);
            }
        }
    }
    preparent = target.parentNode.parentNode.parentNode;
    childs = preparent.childNodes;
    for ( i=0; i<childs.length; i++ )
    {
        if ( childs[i].className == 'portletHeader' )
            continue;
        if ( childs[i].nodeType != 1 )
            continue;
        childs[i].style.display = new_mode;
    }
}

function collapse(event)
{
    var target = event.target ? event.target : event.srcElement;
    parentnode = target.parentNode;
    nodes = parentnode.childNodes;
    var new_mode = 'none';
    for (i=0; i<nodes.length; i++)
    {
        if (nodes[i].nodeName == 'DIV')
        {
            if (nodes[i].style.display == 'none') 
            {
                new_mode = 'block';
            }
            nodes[i].style.display = new_mode;
        }
    }
    if ( new_mode == 'none' ) {
        target.src = 'add_icon.gif';
    } else {
        target.src = 'unadd_icon.gif';
    }
}

// browser check
var isNav4, isNav6, isIE4;
if (navigator.appVersion.charAt(0) == "4"){
    if (navigator.appName.indexOf("Explorer") >= 0){
        isIE4 = true;
    }
    else{
        isNav4 = true;
    }
}
else if (navigator.appVersion.charAt(0) > "4"){
    isNav6 = true;
}

function setIdProperty( id, property, value ){
    if (isNav6){
        var styleObject = document.getElementById( id );
        if (styleObject != null){
            styleObject = styleObject.style;
            styleObject[ property ] = value;
        }
    }
    else if (isNav4){
        document[id][property] = value;
    }
    else if (isIE4){
        if(document.all[id]!=null){
        document.all[id].style[property] = value;
        }
    }
} 

function getIdProperty( id, property )
{
    if (isNav6)
    {
        var styleObject = document.getElementById( id );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            if (styleObject[property])
            {
                return styleObject[ property ];
            }
        }
        styleObject = getStyleBySelector( "#" + id );
        return (styleObject != null) ?
            styleObject[property] :
            null;
    }
    else if (isNav4)
    {
        return document[id][property];
    }
    else
    {
        return document.all[id].style[property];
    }
} 

function getStyleBySelector( selector )
{
    if (!isNav6)
    {
        return null;
    }
    var sheetList = document.styleSheets;
    var ruleList;
    var i, j;

    /* look through stylesheets in reverse order that
       they appear in the document */
    for (i=sheetList.length-1; i >= 0; i--)
    {
        ruleList = sheetList[i].cssRules;
        for (j=0; j<ruleList.length; j++)
        {
            if (ruleList[j].type == CSSRule.STYLE_RULE &&
                    ruleList[j].selectorText == selector)
            {
                return ruleList[j].style;
            }
        }
    }
    return null;
}

// xmlrpc ------
function createXMLHTTP() {    var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
    for (var arS=0; arS< arrSignatures.length; arS++)
    {
        try {
            xmlObj = new ActiveXObject(arrSignatures[arS]);
            return xmlObj;
        } catch (oError) {
            // ignore
        }
    }
    throw new Error("MSXML is not installed on your system.");
}

function call_remote(receiving_end, data, next_action, attr1, attr2){
    var xmlObj = null;
    if ( window.XMLHttpRequest ) {
        xmlObj = new XMLHttpRequest();
    } else if ( window.ActiveXObject ) {
        xmlObj = createXMLHTTP();
    } else {
        return;
    }

    xmlObj.onreadystatechange = function()
    {
        if(xmlObj.readyState == 4)
        {
            if (next_action) { next_action(xmlObj.responseText, attr1, attr2); }
        }
    }

    try {
        xmlObj.open('POST', receiving_end, true);
    } catch (oError) {
        alert('paha:'+oError);
    }
    xmlObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlObj.send(data);
}

// ------- xmlrpc

function deleteThisItem(id){
if(confirm("Delete "+id)){
      var url = "deleteObject?id="+id;
      location.href=url;
    } else{

    }
    
}

//function showHelpInfo(id){
//    p = getIdProperty(id, "display");
//    if(p == 'none'){
//        setIdProperty(id, "display", "block");
//    }
//    if(p == 'block'){
//        setIdProperty(id, "display", "none");
//    }
//}

function showInfo(id){
    setIdProperty(id, "display", "block");
}
function hideInfo(id){
    setIdProperty(id, "display", "none");
}

function replaceElement(oldElement, tag, text){
    var oldElementChild = oldElement.getElementsByTagName(tag)[0];
    var newElementChild = document.createElement(tag);
    if (text!=""){
        appendTextElement(newElementChild, text);
    }
    oldElement.replaceChild(newElementChild,oldElementChild);
}

function appendElement(oldElement, tag, text){
    var newElementChild = document.createElement(tag);
    if (text!=""){
        appendTextElement(newElementChild, text);
    }
    oldElement.appendChild(newElementChild);
}

function replaceTextElement(oldElement, text){
    var newTextElement = document.createTextNode(text);
    var oldTextElement = oldElement.childNodes[0];
    oldElement.replaceChild(newTextElement, oldTextElement);
}

function appendTextElement(oldElement, text){
    var newTextElement = document.createTextNode(text);
    oldElement.appendChild(newTextElement);
}

//restrict page
function showGroupMembersBox(){
    showInfo('groupmembers-box')
    hideInfo('groups-box')
}

function showGroupsBox(){
    showInfo('groups-box')
    hideInfo('groupmembers-box')
}

function hideGroupBoxes(){
    hideInfo('groups-box')
    hideInfo('groupmembers-box')
}

//dashboard page
function showGroupsBoxEdit(){
    showInfo('dash-groups-edit');
    hideInfo('dash-groups-box');
}

function hideGroupsBoxEdit(){
    showInfo('dash-groups-box');
    hideInfo('dash-groups-edit');
}

//group references
function showGroupRefEdit(){
    showInfo('box-ref-edit');
    hideInfo('box-references');
    hideInfo('box-ref-history')
    document.getElementById('show_refshistory_icon').style.display='';
    hideInfo('hide_refshistory_icon');
}

function hideGroupRefEdit(){
    hideInfo('box-ref-edit');
    showInfo('box-references');
}

function showGroupRefHistory(){
    showInfo('box-ref-history');
    hideInfo('box-references');
    hideInfo('box-ref-edit');
    document.getElementById('hide_refshistory_icon').style.display='';
    hideInfo('show_refshistory_icon');
}

function hideGroupRefHistory(){
    hideInfo('box-ref-history');
    showInfo('box-references');
    document.getElementById('show_refshistory_icon').style.display='';
    hideInfo('hide_refshistory_icon');
}

//group photo fn-s
function showGroupImgSearch(){
    showInfo('box-img-search');
    hideInfo('box-img-view');
    showInfo('box-buttons');
    chosen = document.getElementById('ps_service').value;
    if(chosen == 'nagi'){
        showInfo('box-nagi-username');
    }else{
        hideInfo('box-nagi-username');
    }
    hideInfo('box-photos-history');
    document.getElementById('show_photoshistory_icon').style.display='';
    hideInfo('hide_photoshistory_icon');
}

function showGroupImgView(){
    hideInfo('box-img-search');
    showInfo('box-img-view');
    hideInfo('box-buttons');
    hideInfo('box-nagi-username')
}

function showorhideusername(){
    chosen = document.getElementById('ps_service').value;
    if(chosen == 'nagi'){
        showInfo('box-nagi-username');
    }else{
        hideInfo('box-nagi-username');
    }
}

function showGroupPhotosHistory(){
    showInfo('box-photos-history');
    document.getElementById('hide_photoshistory_icon').style.display='';
    hideInfo('show_photoshistory_icon');
    hideInfo('box-img-view');
    hideInfo('box-img-search');
}

function hideGroupPhotosHistory(){
    hideInfo('box-photos-history');
    hideInfo('hide_photoshistory_icon')
    document.getElementById('show_photoshistory_icon').style.display=''
    showInfo('box-img-view');
}

//photoalbum fn-s
function showFlickrOrNagi(){
    chosen = document.getElementById('photo_service').value;
    if(chosen == 'nagi'){
        showInfo('nagi_options');
        hideInfo('flickr_options')
    }else{
        showInfo('flickr_options')
        hideInfo('nagi_options');
    }
}

function delReply(reply_id, deltext){
    question_text = document.getElementById(deltext).innerHTML
    if(confirm(question_text)){
        var url = "delObjectById?id="+reply_id;
        location.href=url;
    }        
}

function delTopic(topic_id, deltext, question_text){
    t = document.getElementById(deltext).innerHTML;
    question_text = t + question_text;
    if(confirm(question_text)){
        var url = "delObjectById?id="+topic_id;
        location.href=url;
    }
}

// begin of functions related with group notes

var visibleGroupNoteNr = 0;

function showGroupNoteView(){
    hideInfo('box-note-edit');
    showInfo('box-note-view');
    hideInfo('box-note-delete');
}

function showGroupNoteDelete(){
    hideInfo('box-note-edit');
    hideInfo('box-note-view');
    showInfo('box-note-delete');
}

function showGroupNoteAdd(){
    showInfo('box-note-edit');
    hideInfo('box-note-view');
    hideInfo('box-note-delete');
    document.getElementById('note_id').value="none";
    document.getElementById('note_title').value="";
    document.getElementById('note_note').value="";
}

function showGroupNoteEdit(){
    var nest = document.getElementById("box-note-view");
    var activeGroupNote = nest.getElementsByTagName("div")[visibleGroupNoteNr];
    document.getElementById('note_id').value = activeGroupNote.id;
    document.getElementById('note_title').value = activeGroupNote.getElementsByTagName("h4")[0].childNodes[0].nodeValue;
    document.getElementById('note_note').value = activeGroupNote.getElementsByTagName("p")[0].childNodes[0].nodeValue;
    showInfo('box-note-edit');
    hideInfo('box-note-view');
    hideInfo('box-note-delete');
}

function switchGroupNote(cnt){
    var nnr = visibleGroupNoteNr + cnt;
    var vnr = visibleGroupNoteNr;
    var nest = document.getElementById("box-note-view");
    var allGroupNotes = nest.getElementsByTagName("div");
    if (nnr>=0 && nnr<allGroupNotes.length){
        var nextGroupNote = allGroupNotes[nnr].id;
        for (var i=0;i<allGroupNotes.length;i++){
            var gnmid = allGroupNotes[i].getAttribute("id");
            hideInfo(gnmid);
            hideInfo("del_message_"+gnmid);
        }
        showInfo(nextGroupNote);
        showInfo("del_message_"+nextGroupNote);
        visibleGroupNoteNr = nnr;
        switchGroupNoteDisplay((nnr+1),allGroupNotes.length);
    }
}

function switchGroupNoteDisplay(current,total){
    var display = document.getElementById("notes-display");
    replaceTextElement(display, "("+current+"/"+total+")");
}

// end of functions related with group notes

function deleteItemById(id, deltext, question_text){
    t = document.getElementById(deltext).innerHTML;
    question_text = t + question_text;
    if(confirm(question_text)){
        var url = "delObjectById?id="+id;
        location.href=url;
    }
}

function openUrl(url){
    location.href=url;
}

function deleteTrackBackById(abs_url,id,deltext, question_text){
    t = document.getElementById(deltext).innerHTML;
    question_text = t + question_text;
    if(confirm(question_text)){
        var url = abs_url+"/delTrackBackById?id="+id;
        location.href=url;
    }
}

function addTarget(fieldName){
var targetGroupId = document.getElementById('targetGroupId_'+fieldName).value;
var destinationBox = document.getElementById(fieldName);
var sourceGroupBox = document.getElementById('groups_'+fieldName);
var sourceUserBox = document.getElementById('users_'+targetGroupId+'_'+fieldName);
var source_users = sourceUserBox.value;
var sourceBox = sourceGroupBox;
    if (source_users){
        sourceBox = document.getElementById('users_'+targetGroupId+'_'+fieldName);
    }
    addOptions(sourceBox,sourceGroupBox,destinationBox,fieldName);
    selectAllOptions(destinationBox);
    createEmptyOption(destinationBox);
}          

function remTarget(fieldName){
    var targetGroupId = document.getElementById('targetGroupId_'+fieldName).value;
    var sourceBox = document.getElementById(fieldName);
    var destinationUserBox = document.getElementById('users_'+targetGroupId+'_'+fieldName);
    var destinationGroupBox = document.getElementById('groups_'+fieldName);
    remOptions(sourceBox,destinationGroupBox,fieldName);
    selectAllOptions(sourceBox);
    createEmptyOption(sourceBox);
}

function addOptions(sourceBox,sourceGroupBox,destinationBox,fieldName){
    for (i=0;i<sourceBox.options.length;i++){
        if(sourceBox.options[i].selected){
            sourceBox.options[i].selected="";
            selectedOption = sourceBox.options[i];
            optVal = sourceBox.options[i].value;
            if (sourceBox.options[i].getAttribute('type')=='User'){
                for (j=0;j<sourceGroupBox.options.length;j++){
                    sourceUserBox = document.getElementById('users_'+sourceGroupBox.options[j].value+'_'+fieldName);
                    searchAndTurnOff(sourceUserBox, optVal);
                }
            }else{
                searchAndTurnOff(sourceBox, optVal);
            }
            copyOption(selectedOption,destinationBox);
        }
    }
}

function remOptions(sourceBox,destinationGroupBox,fieldName){
    for (i=0;i<sourceBox.options.length;i++){
        if(sourceBox.options[i].selected){
            selectedOption = sourceBox.options[i];
            optVal = selectedOption.value;
            sourceBox.options[i] = null;
            if (selectedOption.getAttribute('type')=='Group'){
                searchAndTurnOn(destinationGroupBox,optVal);
            }else{
                for (j=0;j<destinationGroupBox.options.length;j++){
                    destinationUserBox = document.getElementById('users_'+destinationGroupBox.options[j].value+'_'+fieldName);
                    searchAndTurnOn(destinationUserBox,optVal);
                }
            }
        }
    }
}

function selectAllOptions(destinationBox){
    for (i=0;i<destinationBox.options.length;i++){
        if (destinationBox.options[i].value){
            destinationBox.options[i].selected='selected';
        }else{
            destinationBox.options[i] = null;
        }
    }
}

function createEmptyOption(sourceBox){
    if (sourceBox.options.length==0){
        var newOption = document.createElement('OPTION');
        newOption.selected='selected';
        sourceBox.appendChild(newOption);
    }
}

function copyOption(selectedOption,destinationBox){
    var newOption = document.createElement('OPTION');
    newOption.value = selectedOption.value;
    newOption.text = selectedOption.text;
    newOption.setAttribute('type',selectedOption.getAttribute('type'));
    destinationBox.appendChild(newOption);
}

function searchAndTurnOn(destinationBox,optionValue){
    for (i=0;i<destinationBox.options.length;i++){
        if (destinationBox.options[i].value==optionValue){
            destinationBox.options[i].removeAttribute('disabled');
        }
    }
}
function searchAndTurnOff(sourceBox, optionValue){
    for (i=0;i<sourceBox.options.length;i++){
        if (sourceBox.options[i].value==optionValue){
            sourceBox.options[i].setAttribute('disabled','disabled');
        }
    }
}


function switchGroupUsersBox(fieldName){
    element = document.getElementById('groups_'+fieldName);
    groupId = element.options[element.selectedIndex].value;
    var currentGroupId = "div_users_"+document.getElementById('targetGroupId_'+fieldName).value+"_"+fieldName;
    var nextGroupId = "div_users_"+groupId+"_"+fieldName;
    setIdProperty(currentGroupId, "display", "none");
    setIdProperty(nextGroupId, "display", "block");
    document.getElementById('targetGroupId_'+fieldName).value = groupId;
}

