/**
 * Questa funzione imposta il path alle componenti fondamentali
 * Viene richiama solo dai link sul menu orizzontale
 */
function selectMenuItem(isHomePage, myForm, 
                        homePageUrl, homePageLabel, homePagePath, 
                        newUrl, newLabel, newPath) {
    myForm.sMenuCurrSelItem.value = newLabel;

    setRootNavPath(homePageUrl, 
                   homePageLabel, 
                   homePagePath,
                   myForm.menu_labels, 
                   myForm.menu_urls, 
                   myForm.menu_path);
    if (isHomePage == "F") {
        appendNavPath(newUrl, 
                      newLabel, 
                      newPath,
                      myForm.menu_labels, 
                      myForm.menu_urls, 
                      myForm.menu_path);
    }
    sendRequest(myForm, newUrl, "", "");
}

function callMenuItem(myForm, newUrl, newLabel, newPath, resetHome) {
    var formAction = newUrl;

    appendNavPath(newUrl, newLabel, newPath, myForm.menu_labels, myForm.menu_urls, myForm.menu_path);
    
	sendRequest(myForm, formAction, "", "");
}

/**
 * Aggiunge al nav path corrente url e label.
 * Imposta i campi currFormLabels e currFormUrls che rappresentato
 * gli input type della form eseguita
 */
function appendNavPath(newUrl, newLabel, newPath, currFormLabels, currFormUrls, currFormPath) {
    privateAppenderNavPath(newUrl, newLabel, newPath, currFormLabels, currFormUrls, currFormPath, true);
}

/**
 * Imposta il nav path ai valori passati, vengono rimossi eventuali path
 * presenti
 */
function setRootNavPath(newUrl, newLabel, newPath, currFormLabels, currFormUrls, currFormPath) {
    privateAppenderNavPath(newUrl, newLabel, newPath, currFormLabels, currFormUrls, currFormPath, false);
}

/**
 * Aggiunge al nav path corrente url e label.
 * Imposta i campi currFormLabels e currFormUrls che rappresentato
 * gli input type della form eseguita
 */
function privateAppenderNavPath(newUrl, newLabel, newPath, currFormLabels, currFormUrls, currFormPath, append) {
    if (document.NavigationForm) {
        var l = "";
        var u = "";
        var p = "";
        
        if (append) {
            var sep = "|";
            var lArr = document.NavigationForm.menu_labels.value.split(sep);
            var uArr = document.NavigationForm.menu_urls.value.split(sep);
            var pArr = document.NavigationForm.menu_path.value.split(sep);
            
            if (lArr.length != uArr.length || lArr.length != pArr.length) {
                return;
            }
            var len = pArr.length;
            var insertPos = 0;
            for (; insertPos < len; insertPos++) {
                if (pArr[insertPos] == newPath) {
                    break;
                }
            }
            if (insertPos == len) { // new element
                lArr.push(newLabel);
                uArr.push(newUrl);
                pArr.push(newPath);
            } else {
                lArr[insertPos] = newLabel;
                uArr[insertPos] = newUrl;
            }
            ++insertPos;

            for (var i = 0; i < insertPos; i++) {
                l += lArr[i];
                u += uArr[i];
                p += pArr[i];
                
                if ((i + 1) < insertPos) {
                    l += sep;
                    u += sep;
                    p += sep;
                }
            }
        } else {
            l = newLabel;
            u = newUrl;
            p = newPath;
        }
        currFormLabels.value = l;
        document.NavigationForm.menu_labels.value = l;
        
        currFormUrls.value = u;
        document.NavigationForm.menu_urls.value = u;

        currFormPath.value = p;
        document.NavigationForm.menu_path.value = p;
    }
}

function showPath(navForm) {
    var l = navForm.menu_labels.value.split("|");
    var u = navForm.menu_urls.value.split("|");

    var minLen = Math.min(l.length, u.length) - 1;
    
    for (var i = 0; i < minLen; i++) {
        document.write("<a href=\"javascript:execAction(" + i + ")\"\">" + l[i] + "</a>");
        document.write(" - ");
    }
    // l'ultimo elemento non e' cliccabile
    document.write(l[minLen]);
}

function execAction(idx) {
    if (document.NavigationForm) {
        var sep = "|";

        var lArr = document.NavigationForm.menu_labels.value.split(sep);
        var uArr = document.NavigationForm.menu_urls.value.split(sep);
        var pArr = document.NavigationForm.menu_path.value.split(sep);
        
        var l = "";
        var u = "";
        var p = "";

        if (idx < lArr.length) {
            for (var i = 0; i <= idx; i++) {
                l += lArr[i];
                u += uArr[i];
                p += pArr[i];
                
                if (i < idx) {
                    l += sep;
                    u += sep;
                    p += sep;
                }
            }
            document.NavigationForm.menu_labels.value = l;
            document.NavigationForm.menu_urls.value = u;
            document.NavigationForm.menu_path.value = p;
    	    
    	    sendRequest(document.NavigationForm, uArr[idx], "", "");
        }
    }
}
