/* ----------------------------------------------
*
* 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() {
    var allDivs = document.getElementsByTagName('div');
    var printDivs = new Array();
    var printDivClasses = new Array();
    
    var x = 0;
    for (i = 0; i < allDivs.length; i++) {
        if (allDivs[i].className == 'ixPrintLink') {
            printDivs[x++] = allDivs[i];
        }
    }

    for (i = 0; i < printDivs.length; i++) {
        printDivClasses[i] = printDivs[i].style.display;
        printDivs[i].style.display = 'none';
    }

    print();

    for (i = 0; i < printDivs.length; i++) {
        printDivs[i].style.display = printDivClasses[i];
    }
}

function insideOpenPrintable(linkUrl) {
    window.open(linkUrl, 'InsidePrint', 'width=900,height=600,scrollbars=yes,resizable=yes');
}
function insideOpenCompact(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenExtended(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductAccessories(linkUrl) {
    document.location.href = linkUrl;
}
function insideOpenProductAccessoryPage(linkUrl) {
    window.open(linkUrl + '&ixPrint=true', 'InsideAccessoryPage', 'width=900,height=600,scrollbars=yes,resizable=yes');
}
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 insideListOpenPrintable(linkUrl) {
    window.open(linkUrl, 'InsidePrint', 'width=900,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');
}

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=603&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 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 insideHideElement(elementId) {
        myElement = document.getElementById(elementId);
	if (myElement) {
	    myElement.style.display = 'none';
	}        
}
function insideShowElement(elementId) {
        myElement = document.getElementById(elementId);
	if (myElement) {
	        myElement.style.display = 'block';
	}
}
function insideHideClassElement(parentId, elementClass) {
        allDivs = document.getElementById(parentId).getElementsByTagName('div');
    	for (i = 0; i < allDivs.length; i++) {
        	if (allDivs[i].className.indexOf(elementClass) > -1) {
	        	allDivs[i].style.display = 'none';
        	}
    	}
}
function insideShowClassElement(parentId, elementClass) {
        allDivs = document.getElementById(parentId).getElementsByTagName('div');
    	for (i = 0; i < allDivs.length; i++) {
        	if (allDivs[i].className.indexOf(elementClass) > -1) {
	        	allDivs[i].style.display = 'block';
        	}
    	}
}

/******************************************
* 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;
    }

    var 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, 10000);
}


function checkFilter(elementId) {
	var myElement = document.getElementById(elementId + '_all');
	if (myElement) {
		myElement.checked = '';
	}
	var myElement = document.getElementById(elementId + '_selected');
	if (myElement) {
		myElement.checked = 'checked';
	}
}