/*
 * 
 * Funzione javascript per invertire la visualizzazione di un div.
 * 
 * @copyright ? 1995-2006 Intesys S.r.l. Verona (I). All rights reserved. 
 * @version 24/10/2006 - 1.0
 * 
 * 
 */



function invertDisplay(divName)
{
	var div = document.getElementById(divName)
	if (div) {
		if (typeof(div.memoDisplay) == 'undefined') {
			div.memoDisplay = (typeof(div.style.display)!='undefined' && div.style.display!='none' ? div.style.display : 'block')
		}	
		div.style.display = (div.style.display != 'none' ? 'none' : div.memoDisplay)
	}
	return false
}


/*
 * onkeyup su tasto TAB
 */


/*function showUp(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    if (evt) {
        //document.getElementById("upKey").innerHTML = evt.keyCode;
        if (evt.keyCode == "9") {
			//alert('TAB');
        }
    }
}
document.onkeyup = showUp;*/


/*
 * Altre funzioni
 */

function trova(n, d) { //v4.01
	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=trova(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n);
	return x;
}

function dentro(nome, valore, defaultVal) {
	var nome, valore, defaultVal;
	var obj = document.getElementById(nome);
	if(valore==defaultVal){
		obj.value='';
	};
	obj.style.backgroundColor='#ffffff';
	obj.style.backgroundImage='url(http://www.entigo.it/widgets/ico-pen.gif)';
	obj.style.backgroundRepeat='no-repeat';
	obj.style.backgroundPosition='top right';
	obj.style.border='solid 1px #ccc';
	obj.style.color='#000000';
	obj.style.fontWeight='bold';
	obj.style.fontStyle='normal';
}

function fuori(nome, valore, defaultVal) {
	var nome, valore, defaultVal;
	var obj = document.getElementById(nome);
	if(valore==''){
		obj.value=defaultVal;
		obj.style.color='#565656';
		obj.style.padding='2px';
		obj.style.fontWeight='normal';
		obj.style.fontStyle='italic';
		obj.style.backgroundImage='none';
	} else if(valore==defaultVal){
		obj.style.color='#565656';
		obj.style.fontWeight='normal';
		obj.style.fontStyle='italic';
		obj.style.backgroundImage='none';
	} else {
		obj.style.backgroundColor='#ffffff';
		obj.style.backgroundImage='url(http://www.entigo.it/widgets/ico-ok.gif)';
		obj.style.backgroundRepeat='no-repeat';
		obj.style.backgroundPosition='top right';
		obj.style.border='solid 1px #ccc';
		obj.style.color='#008000';
		obj.style.fontWeight='bold';
		obj.style.fontStyle='normal';
	}
}

function validaForm() { //v4.0
	var i,p,q,nm,nv,df,test,num,min,max,errors='',args=validaForm.arguments;	
	for (i=0; i<(args.length-2); i+=3) {
		test=args[i+2];
		val=trova(args[i]);
		if (val) {
			nm=val.name;
			nv=val.value;
			df=val.defaultValue;
			
			alert('\n nm '+nm+'\n nv '+nv+'\n df '+df+'\n valdefaul '+val.defaultValue);
			
			if (nv == df) {
				if(nm=='nome')		nm='Nome';
				if(nm=='cognome')	nm='Cognome';
				if(nm=='azienda')	nm='Azienda';
				if(nm=='tel')		nm='Telefono';
				if(nm=='email')		nm='E-mail';
				errors += 'il campo '+nm+' è obbligatorio.\n';
				}
			if ((val=val.value)!="") {
				if (test.indexOf('isEmail')!=-1) {
					p=val.indexOf('@');
					if (p<1 || p==(val.length-1)) {						
						if(nm=='email') nm='E-mail';
						errors +='il campo '+nm+' ['+nv+'] deve essere un indirizzo valido.\n';
					}
				}
			} else if (test.charAt(0) == 'R') {
				if(nm=='nome')		nm='Nome';
				if(nm=='cognome')	nm='Cognome';
				if(nm=='azienda')	nm='Azienda';
				if(nm=='tel')		nm='Telefono';
				if(nm=='email')		nm='E-mail';
				errors += 'il campo '+nm+' è obbligatorio.\n';
			}
		}
	}
	if (errors) alert('Errore:\n'+errors);
	document.valore = (errors == '');
}

function openPopup(url, windowParam, windowName) {
	var now = new Date();
	windowName = (windowName ? windowName : 't'+now.getTime()+'r'+Math.floor(Math.random()*1000)) 
	var popUpWindow = window.open(url, windowName, windowParam)
	if (popUpWindow && popUpWindow.focus) {
		popUpWindow.focus()
	}
}

function popup(url, kind, width, height, windowName, leftPos, topPos) {
	var toolbarFlag         = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //   1
	var locationFlag        = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //   2
	var directoriesFlag     = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //   4
	var statusFlag          = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //   8
	var menubarFlag         = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //  16
	var scrollbarsFlag      = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //  32
	var resizableFlag       = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   //  64
	var centerFlag          = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   // 128
	var openOnlyOneTimeFlag = (kind % 2 ? 1 : 0);  kind = parseInt(kind / 2, 10);   // 256
	if (centerFlag) {
		leftPos = window.screen.width/2  - width/2  - 10
		topPos = window.screen.height/2 - height/2 - 50
	}
	var param = 'toolbar='+toolbarFlag
			  + ',location='+locationFlag
			  + ',directories='+directoriesFlag
			  + ',status='+statusFlag
			  + ',menubar='+menubarFlag
			  + ',scrollbars='+scrollbarsFlag
			  + ',resizable='+resizableFlag
			  + (typeof(width)   != 'undefined' ? ',width=' +width  : '')
			  + (typeof(height)  != 'undefined' ? ',height='+height : '')
			  + (typeof(leftPos) != 'undefined' ? ",left=" + leftPos + ",screenX=" + leftPos : '')
			  + (typeof(topPos)  != 'undefined' ? ",top="  + topPos  + ",screenY=" + topPos  : '')

    if (!openOnlyOneTimeFlag || !alreadyOpen(windowName)) {
		openPopup(url, param, windowName)
    }
}