/* ----------------------------------------------
*
* 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=800,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 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 ixOpenEditPageLink(linkUrl) {
    document.location.href = linkUrl;
}
function ixCloseEditPageLink(linkUrl) {
    document.location.href = linkUrl;
}
function ixOpenPageEditUploadImageLink(linkUrl) {
    window.open(linkUrl, 'InsidePageEditUploadImage', 'width=320,height=40,scrollbars=no,resizable=true');
}
function ixOpenUserUploadFiles(linkUrl) {
    window.open(linkUrl, 'InsideUserUploadFiles', 'width=860,height=480,scrollbars=no,resizable=true');
}


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');
}

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");
}

// 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;
	}
}

// Check if user is really sure to delete page
function ixOpenDeletePopup(linkUrl, popupMessage) {
    var answer = confirm(popupMessage);
    if (answer) {
        document.location.href = linkUrl;
    }
}

// Set form operation for forms having multiple operations to do
function ixSetFormOperation(fieldId, operation) {
    var field = document.getElementById(fieldId);
    if (field) {
        field.value = operation;
    }
}

// Open/Close categories
function insideCollapseCategory(categoryId) {
    var categoryDiv = document.getElementById(categoryId);
    var allDivs = categoryDiv.getElementsByTagName('div');

    for (i = 0; i < allDivs.length; i++) {
        if (allDivs[i].className.indexOf('ixContents') > -1) {
	    if (allDivs[i].style.display != 'block')  {
		allDivs[i].style.display = 'block';
	    } else {
		allDivs[i].style.display = 'none';
	    }
        }
    }
}

// Check characteristic filter when selected
function checkFilter(characteristic_id) {
	var selectField = document.getElementById(characteristic_id);
	var radioSelectedField = document.getElementById(characteristic_id + '_selected');
	var radioAllField = document.getElementById(characteristic_id + '_all');
	if (selectField) {
		if (selectField.selectedIndex >= 0) {
			if (radioSelectedField) {
				radioSelectedField.checked = true;
			}
		} else {
			if (radioAllField) {
				radioAllField.checked = true;
			}
		}
	}
}

// Glossary
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);

var mouseX = 0;
var mouseY = 0;
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft;
    mouseY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX;
    mouseY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0;}
  if (mouseY < 0){mouseY = 0;}  
  return true
}

function showGlossary(wordId) {
	var glossaryDiv = document.getElementById('ixGlossary_' + wordId);

	document.onmousemove = getMouseXY;

	glossaryDiv.style.left = mouseX + 'px';
	glossaryDiv.style.top = mouseY + 'px';
	glossaryDiv.style.display = 'block';

}
function hideGlossary(wordId) {
    var glossaryDiv = document.getElementById('ixGlossary_' + wordId);
    glossaryDiv.style.display = 'none';
}

function insideHideElement(elementId) {
        myElement = document.getElementById(elementId);
        myElement.style.display='none';
}
function insideShowElement(elementId) {
        myElement = document.getElementById(elementId);
        myElement.style.display='block';
}

function checkAction(lang) {
	return confirm("Con questa azione l' utente verra' cancellato e non sara' piu' possibile fare login. Confermate?");
}

function checkActionDeleteFile(fieldId, fieldValue, formId, uploadFieldId) {
	if (confirm("Con questa operazione verra' cancellato il documento. Confermate?")) {
		 ixSetFormOperation(fieldId, fieldValue);
		 var myUploadFieldId = document.getElementById(uploadFieldId);
		 if (myUploadFieldId) {
		 	myUploadFieldId.value = '';
		 }
		 var myForm = document.getElementById(formId);
		 if (myForm) {
		 	myForm.submit();
		 }
	}
}

/* ----------------------------------------------
*
* data italiana
*
* ---------------------------------------------*/


var m_names = new Array("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre");

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var date_string = curr_date + " " + m_names[curr_month] + " " + curr_year;
var a = document.getElementById('id_data');
var b = document.createTextNode(date_string);

if (a && b) { a.appendChild(b);}
