var win=null;
function EditorOpenPopup(mypage,myname,TopPosition,LeftPosition,w,h,scroll,menubar,toolbar,status,pos,resizable)
{
    var win
    if(pos=="random")
    {
        LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
        TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
    }
    if(pos=="center")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    if(pos=="topleft")
    {
        LeftPosition=0;
        TopPosition=0;
    }
    if(pos=="topcenter")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=0;
    }
    if(pos=="topright")
    {
        LeftPosition=(screen.width)?(screen.width-w):100;
        TopPosition=0;
    }   
    if(pos=="midleft")
    {
        LeftPosition=0;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    if(pos=="midcenter")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    if(pos=="midright")
    {
        LeftPosition=(screen.width)?(screen.width-w):100;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    if(pos=="bottomleft")
    {
        LeftPosition=0;
        TopPosition=(screen.height)?(screen.height-h):100;
    }
    if(pos=="bottomcenter")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=(screen.height)?(screen.height-h):100;
    }
    if(pos=="bottomright")
    {
        LeftPosition=(screen.width)?(screen.width-w):100;
        TopPosition=(screen.height)?(screen.height-h):100;
    }
    
    if(resizable!=1){
       resizable = 0;
    }

    //settings='width='+w+', height='+h+', top='+TopPosition+', left='+LeftPosition+', scrollbars='+scroll+', location=no, directories=no, status='+status+', menubar='+menubar+', toolbar='+toolbar+', resizable='+resizable;
    settings='width='+w+', height='+h+', scrollbars='+scroll+', location=no, directories=no, status='+status+', menubar='+menubar+', toolbar='+toolbar+', resizable='+resizable;
    e=window.open(mypage,myname,settings);
    e.moveTo(LeftPosition,TopPosition);
}

function PopupSelPosition(position)
{
	document.getElementById("img1").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img2").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img3").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img4").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img5").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img6").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img7").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img8").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img9").src="../image/repository_pop_vuoto.gif";

	if(position==1)
	{		
		document.getElementById("img1").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="topleft";
	} else if(position==2)
	{
		document.getElementById("img2").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="topcenter";
	} else if(position==3)
	{
		document.getElementById("img3").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="topright";
	} else if(position==4)
	{
		document.getElementById("img4").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="midleft";
	} else if(position==5)
	{
		document.getElementById("img5").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="midcenter";
	} else if(position==6)
	{
		document.getElementById("img6").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="midright";
	} else if(position==7)
	{
		document.getElementById("img7").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="bottomleft";
	} else if(position==8)
	{
		document.getElementById("img8").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="bottomcenter";
	} else if(position==9)
	{
		document.getElementById("img9").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="bottomright";
	}
}

function PopupStartPosition(position)
{
	document.getElementById("img1").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img2").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img3").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img4").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img5").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img6").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img7").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img8").src="../image/repository_pop_vuoto.gif";
	document.getElementById("img9").src="../image/repository_pop_vuoto.gif";

	if(position=='topleft')
	{		
		document.getElementById("img1").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="topleft";
	} else if(position=='topcenter')
	{
		document.getElementById("img2").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="topcenter";
	} else if(position=='topright')
	{
		document.getElementById("img3").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="topright";
	} else if(position=='midleft')
	{
		document.getElementById("img4").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="midleft";
	} else if(position=='midcenter')
	{
		document.getElementById("img5").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="midcenter";
	} else if(position=='midright')
	{
		document.getElementById("img6").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="midright";
	} else if(position=='bottomleft')
	{
		document.getElementById("img7").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="bottomleft";
	} else if(position=='bottomcenter')
	{
		document.getElementById("img8").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="bottomcenter";
	} else if(position=='bottomright')
	{
		document.getElementById("img9").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="bottomright";
	} else if(position=='')
	{
		document.getElementById("img5").src="../image/repository_pop_selez.gif";
		document.forms.APPLICATION_FORM.pop_position.value="midcenter";
	}
}

function PopupChangeDiv(seldiv)
{
	if(seldiv==1)
	{
		document.getElementById("repository_pop_fixed").style.display = 'none';
		document.getElementById("repository_pop_desktop").style.display = 'block';
	}
	if(seldiv==2)
	{
		document.getElementById("repository_pop_desktop").style.display = 'none';
		document.getElementById("repository_pop_fixed").style.display = 'block';		
	}
}
function PopupTargetSel(obj)
{    
	if(obj.options[obj.selectedIndex].value=="custom")
	{
		document.getElementById("show_link_target").style.display = 'block';	
	} else {
		document.getElementById("show_link_target").style.display = 'none';
	}
	
}




function OpenNewWindow(mypage,myname,w,h,scroll,pos,resizable)
{
    var win
    if(pos=="random")
    {
        LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
        TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
    }
    if(pos=="center")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    else if((pos!="center" && pos!="random") || pos==null)
    {
        LeftPosition=0;
        TopPosition=20
    }

    if(resizable!=1){
       resizable = 0;
    }

    settings='width='+w+', height='+h+', top='+TopPosition+', left='+LeftPosition+', scrollbars='+scroll+', location=no, directories=no, status=no, menubar=no, toolbar=no, resizable='+resizable;
    var win=window.open(mypage,myname,settings);

    if(win.focus)
    {
        win.focus();
    }
}

function CloseNewWindow()
{
    if(win!=null && win.open)
        win.close()
}

function DeleteRecord(url){
    if(strtodisplay==""){
        strtodisplay = "E' sicuro di voler cancellare il record specificato?";
    }
    if(confirm(strtodisplay)){
        location = url;
    }
    return true;
}

function CheckAll()
{
    for (var i=0;i<document.forms.APP_INDEX_FORM.elements.length;i++)
    {
        var e = document.APP_INDEX_FORM.elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox'))
        e.checked = document.APP_INDEX_FORM.allbox.checked;
    }
}
function DoEmpty()
    {
    var ok;
    for (var i=0;i<document.forms.APP_INDEX_FORM.elements.length;i++)
    {
        var e = document.forms.APP_INDEX_FORM.elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox')){
        if (e.checked) ok=e.checked;
        }
    }

    if (ok){
    if (confirm("Si vuole veramente procedere?"))
        document.forms.APP_INDEX_FORM.submit();
    }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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 MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function GotoRelation(valore, url){
    if(valore != ""){
        location.href = url+valore ;
    }else{
        alert("Nessun elemento correlato");
    }

}

function butRemove_onclick(deststr, sorgstr)
{
    var dest = document.forms["APPLICATION_FORM"].elements[deststr];
    var sorg = document.forms["APPLICATION_FORM"].elements[sorgstr];
    selection= new Array;

    for (index = 0 ; index < dest.length ; index++)
    {
         //alert("ciclo "+index);
         if(dest.options[index].selected==true)
         {
             //alert ('trovato la selezione a '+index+' sorg ? lungo '+sorg.length);
             selection[selection.length]=dest.options[index].value;
         }
    }

    for (fooIndex = 0; fooIndex < selection.length; fooIndex++)
    {
        for (fooIndex2 = 0; fooIndex2 < dest.length; fooIndex2++)
        {
            if (dest.options[fooIndex2].value == selection[fooIndex])
            {
                 //alert(selection[fooIndex]);
                dest.options[fooIndex2]=null;
            }
        }
    }

    current_predef = -1;
    for (index = 0 ; index < dest.length ; index++)
    {
        //alert("ciclo "+index);
        if(dest.options[index].selected==true)
        {
            selection[selection.length]=index;
        }
        a = dest.options[index].value.split("|");
        if(a[1]=="p"){
            current_predef = index;
            //alert(current_predef)
        }
    }

    if(dest.length==1 || current_predef==-1 && dest.options[0]){
        dest.options[0].selected = true;
        setPredef(dest.name);
        dest.options[0].selected = false;
    }
}
var current_predef;
function setPredef(deststr)
{
    var dest = document.forms["APPLICATION_FORM"].elements[deststr];
    selection= new Array;
    current_predef = -1;
    for (index = 0 ; index < dest.length ; index++)
    {
        //alert("ciclo "+index);
        if(dest.options[index].selected==true)
        {
            selection[selection.length]=index;
        }
        a = dest.options[index].value.split("|");
        if(a[1]=="p"){
            current_predef = index;
            //alert(current_predef)
        }
    }

    if(selection.length == 1){
        if(current_predef!=-1){
            //alert(dest.options[current_predef].value.substr(0,dest.options[current_predef].value.length-2))
            dest.options[current_predef].text = dest.options[current_predef].text.substr(0,dest.options[current_predef].text.length-14);
            dest.options[current_predef].value = dest.options[current_predef].value.substr(0,dest.options[current_predef].value.length-2);

        }

        dest.options[selection[0]].text = dest.options[selection[0]].text + " (Predefinito)";
        dest.options[selection[0]].value = dest.options[selection[0]].value + "|p";

    }else if(selection.length > 1){
        alert("E' possibile impostare solo un predefinito!")
    }else if(selection.length <= 0){
        alert("E' necessario selezionare un elemento!")
    }
}
function butAdd_onclick(deststr, sorgstr, alert_message)
{
    var dest = document.forms["APPLICATION_FORM"].elements[deststr];
    var sorg = document.forms["APPLICATION_FORM"].elements[sorgstr];

    selection= new Array;

    old_dest_len = dest.length;

    for (index = 0 ; index < sorg.length ; index++)
    {
        //alert("ciclo "+index);
        if(sorg.options[index].selected==true)
        {
            //alert ('trovato la selezione a '+index+' sorg ? lungo '+sorg.length);
            selection[selection.length]=index;
            //alert ('selezione ? lungo '+selection.length);
         }
    }
    //alert (selection.length+" ? la misura dei selezionati");
    var aaa = new Array()
    for (fooIndex = 0; fooIndex < selection.length; fooIndex++)
    {
        //  These are the elements to be added
        mytext  = sorg.options[selection[fooIndex]].text;
        myvalue = sorg.options[selection[fooIndex]].value;
        //alert (myvalue);
        //  First of all we check if the option selected is already present in the destination
        found=false;

        if (dest.length>0)
        {
            for (indexCounter = 0; indexCounter < dest.length; indexCounter++)
            {
                //alert (indexCounter+" dest="+dest.length);
                aaa = dest.options[indexCounter].value.split("|");
                //alert("a0 vale; " + aaa[0]);
                if (myvalue == aaa[0])
                {
                    found=true;
                    break;
                    //alert ('add: trovato');
                }
                for(j=0; j< aaa.length; j++)
                {
                    aaa.shift()
                }

                if (dest.options[indexCounter].value == myvalue)
                {
                    found=true;
                    break;
                    //alert ('add: trovato');
                }
            }
        }

        if (found == false)
        {
            inserted=false;
            //alert (dest.length+" ? la misura");
            if (dest.length>=0)
            {
                for (indexCounter = 0;indexCounter <= dest.length -1; indexCounter++)
                {
                    if (dest.options[indexCounter].text.toLowerCase() > mytext.toLowerCase())
                    {
                        var lastoption = new Option();
                         dest.options[dest.length] = lastoption;
                        break;
                        //alert ('trovato');
                    }
                }
                for (indexCounter2 = dest.length -1 ; indexCounter2 > indexCounter ; indexCounter2--)
                {
                    //alert("indexCounter= "+indexCounter+" indexcounter2= " + indexCounter2);
                    dest.options[indexCounter2].text = dest.options[indexCounter2 - 1].text;
                    dest.options[indexCounter2].value = dest.options[indexCounter2 - 1].value;
                }
                var option = new Option(mytext,myvalue);
                dest.options[indexCounter] = option;
                //dest.options[indexCounter].selected = true;
            }

        }
        else
        {
            //alert('The element specified is already present!');
            if ("" != alert_message)
                alert(mytext + " " + " e' gia' presente!");
        }
    }
    if(old_dest_len==0){
        dest.options[0].selected = true;
        setPredef(dest.name);
        dest.options[0].selected = false;
    }
}
function scrolldown(str) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        window.location.href = "#" + str;
    }
}

function ReloadLeft(){
        window.parent.window.frames.left.ERW_LEFT_Reload();
}

function InputOnChange(){
    if(typeof current_action == 'string'){
	    if(current_action=="modify"){
	        theelm = document.getElementById("InputModified");
	        theelm.style.display = 'block'
	    }
    }
}

function setInputAtToday(the_id){
	var today = new Date()
	mymonth = today.getMonth()+1
	myday   = today.getDate()
    myyear  = today.getFullYear() 
    
    myhour  = today.getHours()
    mymin   = today.getMinutes()
    mysec   = today.getSeconds()
    
	
	if(the_day   = document.getElementById(the_id+'_dd')){
		for(i=0;i<the_day.options.length;i++){
			if(the_day.options[i].value == myday){
				the_day.options.selectedIndex = i;
			}
		}
	}
	
	if(the_month   = document.getElementById(the_id+'_mm')){
		for(i=0;i<the_month.options.length;i++){
			if(the_month.options[i].value == mymonth){
				the_month.options.selectedIndex = i;
			}
		}
	}	
	if(the_year   = document.getElementById(the_id+'_yyyy')){
		for(i=0;i<the_year.options.length;i++){
			if(the_year.options[i].value == myyear){
				the_year.options.selectedIndex = i;
			}
		}
	}	
	if(the_hour   = document.getElementById(the_id+'_hh')){
		for(i=0;i<the_hour.options.length;i++){
			if(the_hour.options[i].value == myhour){
				the_hour.options.selectedIndex = i;
			}
		}
	}	
	if(the_min   = document.getElementById(the_id+'_mi')){
		for(i=0;i<the_min.options.length;i++){
			if(the_min.options[i].value == mymin){
				the_min.options.selectedIndex = i;
			}
		}
	}	
	if(the_sec   = document.getElementById(the_id+'_ss')){
		for(i=0;i<the_sec.options.length;i++){
			if(the_sec.options[i].value == mysec){
				the_sec.options.selectedIndex = i;
			}
		}
	}	
}

// This next little bit of code tests whether the user accepts cookies.
var ER_acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'ER_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('ER_acceptsCookies=yes') != -1) {
        ER_acceptsCookies = true;
    }// If it succeeds, set variable
} else { // there was already a cookie
  ER_acceptsCookies = true;
}


function ER_setCookie (name, value, hours, path, domain, secure) {
    if (ER_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
        var not_NN2 = (navigator && navigator.appName
                       && (navigator.appName == 'Netscape')
                       && navigator.appVersion
                       && (parseInt(navigator.appVersion) == 2))?false:true;

        if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
            if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
                var numHours = hours;
            } else if (typeof(hours) == 'number') { // calculate Date from number of hours
                var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
            }
        }
        document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} // ER_setCookie


