/* ----------------------------------------------
*
* Utility functions
*
* ---------------------------------------------*/
function openWin(url, name, width, height, titlebar, status, scrollbars, resizable, x, y) {
    var parms;
    
    parms = 'top=' + y + ',left=' + x + ',height=' + height + ',width=' + width + ',dependent=yes,titlebar=' + titlebar + ',status=' + status + ',scrollbars=' + scrollbars + ',resizable=' + resizable
    popUp = window.open(url, name, parms);
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function elementInArray(myArray, myObject) {
    for(i=0; i<myArray.length; i++) {
        if (myArray[i] == myObject) {
            return true;
            break;
        }
    }
    return false;
}

function arrayIndexOf(myArray, myObject) {
    for(i=0; i<myArray.length; i++) {
        if (myArray[i] == myObject) {
            return i;
            break;
        }
    }
    return -1;
}

/* ----------------------------------------------
*
* Inside functions
*
* ---------------------------------------------*/
function insidePrintPage() {
    print();
}

function insideOpenPrintable(linkUrl) {
    window.open(linkUrl, 'InsidePrint', 'width=1000,height=600,scrollbars=yes,resizable=yes');
}
function insideOpenCompact(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenExtended(linkUrl) {
    window.open(linkUrl, '_self');
    return false;
}
function insideOpenProductAccessories(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductSuggestedAccessories(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductAlternatives(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductKit(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductSuggested(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductPromotions(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductAvailability(productId) {
    window.open('product_availability.jsp?ixPageId=2703&ixProductId='+productId, 'InsideProductAvailability', 'width=320,height=40,scrollbars=no,resizable=true');
}

function ixOpenBasketAddHolder(url) {
    openWin(url+'&ixPrint=true', 'BasketHolder',1000,600,'yes','yes','yes','no',0,0);
}
function ixOpenBasketAddDates(url) {
    openWin(url+'&ixPrint=true', 'BasketDates',1000,600,'yes','yes','yes','no',0,0);
}
function ixOpenBasketAddAttribute(url) {
    openWin(url+'&ixPrint=true', 'BasketAttribute',1000,600,'yes','yes','yes','no',0,0);
}
function ixOpenBasketAddDelivery(url) {
    openWin(url+'&ixPrint=true', 'BasketDelivery',1000,600,'yes','yes','yes','no',0,0);
}


function insideListOpenPrintable(linkUrl) {
    window.open(linkUrl, 'InsidePrint', 'width=800,height=600,scrollbars=yes,resizable=yes');
}
function insideListOpenCompact(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenExtended(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenProductAccessories(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenProductSuggestedAccessories(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenProductAlternatives(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenProductKit(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenProductSuggested(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenProductPromotions(linkUrl) {
    document.location.href = linkUrl;
}
function insideListOpenProductAvailability(productId) {
    window.open('product_availability.jsp?ixPageId=2703&ixProductId='+productId, 'InsideProductAvailability', 'width=320,height=40,scrollbars=no,resizable=true');
}

function insideOpenProductAlternativePage(linkUrl) {
	document.location.href = linkUrl;
}

prodCkVal = readCookie("ixProductComparison");
tmplCkVal = readCookie("ixProductComparisonTemplate");

function insideCheckProductComparison(checkControl, productId, templateId) {
    if (!checkControl.checked || tmplCkVal == null || (checkControl.checked && tmplCkVal != null && tmplCkVal == templateId)) {
        var selProducts = new Array();

        if (prodCkVal != null) {
            selProducts = prodCkVal.split('|');
        }

        var inArray = elementInArray(selProducts, productId);

        if (checkControl.checked && !inArray) {
            selProducts.push(productId);
        } else if (!checkControl.checked && inArray) {
            selProducts.splice(arrayIndexOf(selProducts, productId), 1);
        }

        prodCkVal = selProducts.join('|');
        tmplCkVal = templateId;

        createCookie("ixProductComparison", prodCkVal);
        createCookie("ixProductComparisonTemplate", tmplCkVal);
    } else {
        checkControl.checked = false;
        alert("Non è possibile confrontare prodotti di categorie diverse!");
    }
}

function insideOpenProductComparison() {
    window.open("index.jsp?ixPageId=2729&ixProductComparison=true", 'InsideProductComparison', 'width=800, height=600, scrollbars=yes, resizable=yes');
}

function insideClearProductComparison() {
    var myInputs = document.getElementsByTagName("input");
    
    for (i=0; i<myInputs.length; i++) {
        var myInput = myInputs[i];
        if (myInput.type == 'checkbox' && myInput.name.indexOf('ixCompareProductSelect_') > -1) {
            myInput.checked = false;
        }
    }

    prodCkVal = null;
    tmplCkVal = null;
    eraseCookie("ixProductComparison");
    eraseCookie("ixProductComparisonTemplate");
}

function checkFilter(elementId) {
	var myElement = document.getElementById(elementId + '_all');
	if (myElement) {
		myElement.checked = '';
	}
	var myElement = document.getElementById(elementId + '_selected');
	if (myElement) {
		myElement.checked = 'checked';
	}
}

/******************************************
* funzioni per calendari agenda
******************************************/
function ixAgendaCalStart(cal) {
    var ifDateEnd = cal.params.inputField.id.replace(/^date_start_/,"date_end_");
    document.getElementById(ifDateEnd).value = cal.params.inputField.value;
}
function ixAgendaCalEnd(cal) {
}
function ixAgendaCalReminder(cal) {
}

function ixActivityCalStart(cal) {
    document.getElementById("agenda_date_start").value = cal.params.inputField.value;
}
function ixActivityCalEnd(cal) {
}
function ixActivityCalReminder(cal) {
}

function ixActivityListCalStart(cal) {
}
function ixActivityListCalEnd(cal) {
}
function ixActivityListCalReminder(cal) {
}

/******************************************
* funzioni per menu a tendina
******************************************/
function mouseLeaves (element, evt) {
    if (typeof evt.toElement != 'undefined' && typeof element.contains != 'undefined') {
        return !element.contains(evt.toElement);
    } else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget) {
        return !contains(element, evt.relatedTarget);
    }
}

function contains (container, containee) {
    while (containee) {
        if (container == containee) {
            return true;
        }
        containee = containee.parentNode;
    }
    return false;
}

var myOldMenu = null;

function showMenu(menuName) {
    if (myOldMenu != null) {
        myOldMenu.style.display = 'none';
        myOldMenu = null;
    }

    myMenu = document.getElementById(menuName);
    if (myMenu != null) {
    
        if (navigator.userAgent.indexOf('MSIE') > -1) {
            myMenu.onmouseout = function() {
                if (mouseLeaves(myMenu, event)) {
                    hideMenu(menuName);
                }
            }
        } else {
            myMenu.onmouseout = function(event) {
                if (mouseLeaves(myMenu, event)) {
                    hideMenu(menuName);
                }
            }
        }
        myMenu.style.display = 'block';
        myMenuWidth = myMenu.offsetWidth;
        myMenuHeight = myMenu.offsetHeight;
        myMenu.style.clip = 'rect(0px, ' + myMenuWidth + 'px, 0px, 0px)';
    for (i=0; i<=myMenuHeight * 10; i++) {
            myMenu.style.clip = "rect(0px, " + myMenuWidth + "px, " + i/10 + "px, 0px)";
        }
        myOldMenu = myMenu;
        
    }
}

function hideMenu(menuName) {
    var myMenu = document.getElementById(menuName);
    if (myMenu != null) {
        myMenu.style.display = 'none';
    }
}
function doHideMenu(menuName) {
    // var myFunction = "hideMenu('" + menuName + "')";
    // setTimeout(myFunction, 5000);
}

// Submit forms (login, search, basket)
function ixSubmitForm(myField, e) {
var keycode;

    if (window.event) {
        keycode = window.event.keyCode;
    } else if (e) {
        keycode = e.which;
    } else {
        return true;
    }

    if (keycode == 13) {
       myField.form.submit();
       return false;
    } else {
        return true;
    }
}

function ixOpenShoppinCartItemLink(linkUrl) {
    window.open(linkUrl + '&ixPrint=true', 'Product', 'width=1000,height=600,scrollbars=yes,resizable=yes');
}

function ixOpenBasketItemLink(linkUrl) {
    window.open(linkUrl + '&ixPrint=true', 'Product', 'width=1000,height=600,scrollbars=yes,resizable=yes');
}

function insideCollapseCategory(categoryId) {
	var category = document.getElementById(categoryId);
	if (category) {
		var array = category.getElementsByTagName('div');
		for (i=0; i<array.length; i++) {
			var content = array[i];
			if (content.className.indexOf('ixContents') > -1) {
				if (content.style.display == 'none' || content.style.display == '') {
					content.style.display = 'block';
				} else {
					content.style.display = 'none';
				}
			}
		}
	}
}