// JavaScript Document

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function CheckRef(id){
	if (document.getElementById && document.getElementById(id))	{
			return true; //document.getElementById(id);
	}
	else if (document.all && document.all[id]){
			return true; //document.all[id];
	}
	else if (document.layers && document.layers[id]){
		//document.layers[id].style=document.layers[id];
		return true;//document.layers[id];
	}
	else{
		return false;
	}
}

function getRef(id){
	if (document.getElementById && document.getElementById(id))	{
			return document.getElementById(id);
	}
	else if (document.all && document.all[id]){
			return document.all[id];
	}
	else if (document.layers && document.layers[id]){
		document.layers[id].style=document.layers[id];
		return document.layers[id];
	}
	else{
		return null;
	}
}

function setAction(sAction){
	if (CheckRef("comando")){
		document.getElementById("comando").value = sAction;
		document.form.submit();
	}
}
function confirmAction(sAction){
	if (confirm("Sei sicuro?")){
		setAction(sAction);
	}
}

function sendTo(form, url)
{
	if (url != '')
	{
		form.action = url;
		form.submit();
	}
}
function submitForm(form, aValues, url){
	setValues(aValues);
	if (url != "" && url != null) getRef(form).action = url;
	getRef(form).submit();
}
function setValues(arrValues){
	var x;
	var tmpArray = new Array();
	if (isArray(arrValues)){
		if(isArray(arrValues[0])){
			for (x = 0; x < arrValues.length; x++){
				tmpArray = arrValues[x];
				getRef(tmpArray[0]).value = tmpArray[1];
			}
		}else{
			getRef(arrValues[0]).value = arrValues[1];
		}
	}
}
function isArray(arrInput){
	if (typeof arrInput == 'object'){  
		var criterion =  arguments[0].constructor.toString().match(/array/i); 
 		return (criterion != null);  
	}
	return false;
}
function getFileName(oSource, oDestination){
	var sStringa = getRef(oSource).value;
	var arr = sStringa.split("\\");
	var sFile = arr[arr.length - 1];
	getRef(oDestination).value = sFile;
}

function selectItem(target, value){
	if (window.opener) {
  if (window.opener.document) {
    if (window.opener.document.getElementById) {
      var e = window.opener.document.getElementById(target);
      if (e) {
        window.opener.document.getElementById(target).value = value;
		window.close();
      }
      else alert(target +' doesn\'t exist');
    }
    else alert('window.opener.document.getElementById is null');
  }
  else alert('window.opener.document is null');
}
else alert('window.opener is null');
	
}
function OpenWindow(theURL, nome, w, h, l, t) {
	if (isNaN(l)){
		LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	} else {
		LeftPosition = l;
	}
	if (isNaN(t)){
		TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	} else {
		TopPosition = t;
	}
	
	if (! isNaN(w)){
		w = 100;
	}
	if (! isNaN(h)){
		h = 100;
	}
	window.open(theURL,nome,'status=no,scrollbars=false,resizable=no,width='+w+',height='+h+',left='+LeftPosition+',top='+TopPosition);
}

function ShowHide(fromID, toID){
	if (isArray(fromID)){
		for (x = 0; x < fromID.length; x++){
			if (CheckRef(fromID[x])) 			getRef(fromID[x]).style.display = "none";
			if (CheckRef("Tab_" + fromID[x]))	getRef("Tab_" + fromID[x]).className = "";
		} 
	}else{		
		if (CheckRef(fromID)) 			getRef(fromID).style.display = "none";
		if (CheckRef("Tab_" + fromID))	getRef("Tab_" + fromID).className = "";
	}
	if (isArray(toID)){
		for (x = 0; x < toID.length; x++){
			if (CheckRef(toID[x])) 			getRef(toID[x]).style.display = "block";
			if (CheckRef("Tab_" + toID[x]))	getRef("Tab_" + toID[x]).className = "active";
		} 
	}else{		
		if (CheckRef(toID)) 			getRef(toID).style.display = "block";
		if (CheckRef("Tab_" + toID))	getRef("Tab_" + toID).className = "active";
	}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function LoadSwappingImages(idBoxPreview, idBoxThumbs){
	firstnode = false;
	// Controllo che ci sia il box per la preview
	if (CheckRef(idBoxPreview)){
		// Controllo che nel box ci sia il tag IMG
		BoxPreview = getRef(idBoxPreview);
		if (BoxPreview.hasChildNodes){
			if(BoxPreview.firstChild.nodeName == "IMG"){
				// Imposto un ID all'IMMAGINE
				objImg = BoxPreview.firstChild;
				objImg.id = "JSDOM_PreviewImage";
				
				// Controllo che ci sia il box delle Thumbs
				if (CheckRef(idBoxThumbs)){
						// Imposto l'oggetto
						BoxThumbs = getRef(idBoxThumbs);
						// Ciclo i nodi figli per trovare il tag UL
						for (x=0;x<BoxThumbs.childNodes.length; x++){
							node = BoxThumbs.childNodes[x];
							// Controllo che all'interno del box ci sia il tag UL
							if (node.nodeName == "UL"){
									
									// Controllo che nell'UL ci siano i LI
									ULnode = BoxThumbs.childNodes[x];
									// ciciclo i nodi fino a trovare il LI
									for (i=0;i<ULnode.childNodes.length; i++){
										LInode = ULnode.childNodes[i];
										if (LInode.nodeName == "LI"){
											// Controllo che nell'LI ci sia un A con un IMG
											if (LInode.firstChild.nodeName == "A"){
												// Prendo il HREF del A e metto aggiuno la funzione onclic passandoglielo come parametro
												Anode = LInode.firstChild;
												
												if(Anode.firstChild.nodeName == "IMG"){
													IMGnode = Anode.firstChild;
													Anode.id = "JSDOM_A_" + i;
													ToImage = Anode.href;
													Anode.href = "javascript:SwapImage('"+Anode.id+"','"+objImg.id+"','"+ToImage+"');";
													Anode.target = "";
													Anode.className = "";													
													//Anode.onclick = function(){SwapImage(Anode.id,objImg.id,ToImage)};
													//Anode.onclick = "SwapImage('"+Anode.id+"','"+objImg.id+"','"+ToImage+"')";
													//Anode.setAttribute("onclick","SwapImage('"+Anode.id+"','"+objImg.id+"','"+ToImage+"')");
													//alert(Anode.onclick);
												}
												if (!firstnode){
													SwapImage(Anode.id,objImg.id,ToImage);
													firstnode = true;
												}
											}
										}
									}
							}
						}
				}
			}
		}
	}
}
var LastId = "";
function SwapImage(IdThumb,IdPreview,PathImage){
	//alert("IdThumb: " +IdThumb+ " IdPreview: " + IdPreview+ " PathImage: " + PathImage);
	getRef(IdThumb).className = "active";
	getRef(IdPreview).src = PathImage;
	getRef(IdPreview).alt = PathImage;
	(LastId != "") ? getRef(LastId).className = "" : LastId = IdThumb;
	LastId = IdThumb;
}

function SwapByIdObject(objectsName,idObject,countObjects){
		//alert("["+objectsName+"] ["+idObject+"] ["+countObjects+"]")
		for (x = 1; x <= countObjects; x++){
			//alert(objectsName+x)	
			if ( CheckRef(objectsName+x) )	getRef(objectsName+x).style.display = "none";
		} 
		if ( CheckRef(objectsName+idObject) )	getRef(objectsName+idObject).style.display = "block";
}

