function saveForm(id, script, iddiv){
	var formCat = document.getElementById(id);
	var str='';
	for (var i=0; i<formCat.length; i++)
	{
		if (formCat[i].type=="text") {
			str+="&"+formCat[i].name+"="+clearGetString(formCat[i].value);
		}
		if (formCat[i].type=="textarea") {
			str+="&"+formCat[i].name+"="+clearGetString(formCat[i].value);
		}
		if (formCat[i].type=="select-one") {
			str+="&"+formCat[i].name+"="+clearGetString(formCat[i].value);
		}
		if (formCat[i].type=="checkbox") {
			var value = (formCat[i].checked)?1:0;
			str+="&"+formCat[i].name+"="+value;
		}
		if (formCat[i].type=="radio") {
			if (formCat[i].checked) str+="&"+formCat[i].name+"="+clearGetString(formCat[i].value);
		}
		if (formCat[i].type=="select") {
			str+="&"+formCat[i].name+"="+clearGetString(formCat[i].value);
		}
		if (formCat[i].type=="hidden") {
			str+="&"+formCat[i].name+"="+clearGetString(formCat[i].value);
		}
	}
	new ajax(script,document.getElementById(iddiv),str,null,'post');
	setTimeout('window.scrollBy(0,300)', 600);
}

function fillDiv(id, script)
{
	new ajax(script,document.getElementById(id),null,null,null);
}

function emptyDiv(id)
{
	div = document.getElementById(id);
	div.innerHTML = '';
}

function initAnag(){
	var valori = this.request.responseText;
	form = document.getElementById('form_customer');
	arrval = valori.split(';');
	//se cambi i campi da compilare autoamticamente controllare la condizione dell'if!
	if(arrval.length>=7)
	{
		form.cliente.value = arrval[0];
		form.sede_di.value = arrval[1];
		form.referente.value = arrval[2];
		form.telefono.value = arrval[3];
		form.fax.value = arrval[4];
		form.email.value = arrval[5];
		form.cliente.readOnly=true;
		form.sede_di.readOnly=true;
		form.referente.readOnly=true;
		form.telefono.readOnly=true;
		form.fax.readOnly=true;
		form.email.readOnly=true;
		document.getElementById('clientemod').style.display = 'inline';
		document.getElementById('sededimod').style.display = 'inline';
		document.getElementById('referentemod').style.display = 'inline';
		document.getElementById('telefonomod').style.display = 'inline';
		document.getElementById('faxmod').style.display = 'inline';
		document.getElementById('emailmod').style.display = 'inline';
		window.top.frameAllegati.window.location.replace(arrval[6]);
		document.getElementById('frameAllegati').style.display = 'block';
		form.ddt_numero.focus();
	}
	else
	{
		alert("Attenzione! al codice inserito non corrisponde alcuna anagrafica.\nSe avevi gia' effettuato segnalazioni controlla di avere inserito il codice corretto.\nSe invece e' la prima segnalazione prosegui.");
		form.cliente.readOnly=false;
		form.sede_di.readOnly=false;
		form.referente.readOnly=false;
		form.telefono.readOnly=false;
		form.fax.readOnly=false;
		form.email.readOnly=false;
		form.cliente.value = "";
		form.sede_di.value = "";
		form.referente.value = "";
		form.telefono.value = "";
		form.fax.value = "";
		form.email.value = "";
		document.getElementById('clientemod').style.display = 'none';
		document.getElementById('sededimod').style.display = 'none';
		document.getElementById('referentemod').style.display = 'none';
		document.getElementById('telefonomod').style.display = 'none';
		document.getElementById('faxmod').style.display = 'none';
		document.getElementById('emailmod').style.display = 'none';
		//document.getElementById('frameAllegati').src = ;
		//alert(arrval[0]);
		window.top.frameAllegati.window.location.replace(arrval[0]);
		document.getElementById('frameAllegati').style.display = 'block';
		form.cliente.focus();
	}
}

function loadAnagrafica(codice, script, idform)
{
	new ajax(script,initAnag,null,null,null);
}

function sblocca(id)
{
	element = document.getElementById(id);
	element.readOnly = false;
	element.focus();
}

function swap_up(element)
{
	string = new String(element.src);
	element.src = string.replace('_off.','_on.');
}

function swap_down(element)
{
	string = new String(element.src);
	element.src = string.replace('_on.','_off.');
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

var openeddiv = null;
function opendiv(id)
{
	if (openeddiv!=null) document.getElementById(openeddiv).style.display = 'none';
	openeddiv = id;
	document.getElementById(id).style.display = 'block';
}

function openClosediv(id1, id2)
{
	if (id2!=null) 
	{
		document.getElementById(id2).style.display = 'none';
	}
	if (id1!=null) 
	{
		document.getElementById(id1).style.display = 'block';
		var frame = 'frameArticoli'+id1;
		if(window.top[frame]!=undefined)
			window.top[frame].window.location.reload();
	}
}

function vedidiv(id)
{
	if (document.getElementById(id).style.display=='none')
		document.getElementById(id).style.display = 'block';
	else
		document.getElementById(id).style.display = 'none';
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return new Array(myWidth,myHeight);
}

/*
 * forza il submit se premo invio
 * Es:
 * <FORM ACTION="../cgi-bin/mycgi.pl">
 *   name:     <INPUT NAME=realname SIZE=15><BR>
 *   password: <INPUT NAME=password TYPE=PASSWORD SIZE=10
 *          onKeyPress="return submitenter(this,event)"><BR>
 *   <INPUT TYPE=SUBMIT VALUE="Log In">
 *   </FORM>
 */

function submitenter(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 indirizzo Mail
 *
 */

function checkMail(string)
{
    var espressione = /^([A-z._\-])+@([A-z._\-])+\.([A-z])/;
    return espressione.test(string);
}


/*
 * Setto la select passata al valore dato
 */

function setSelect(select,value)
{
    for (j=0; j<select.options.length;j++)
	if (select.options[j].value==value)
	{
	    select.selectedIndex=j;
	    return;
	}
}


/*
 * Pulisco una stringa per passarla in get via js
 */
function clearGetString(string){
	string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

		if (c==43)
		{
			utftext += '&#43;';
		}
		else if(c==38)
		{
			utftext += '&#38;';
		}
        else if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += '&#'+c+';';
			//utftext += String.fromCharCode((c >> 6) | 192);
            //utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
			utftext += '&#'+c+';';
            //utftext += String.fromCharCode((c >> 12) | 224);
            //utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            //utftext += String.fromCharCode((c & 63) | 128);
        }

    }
    return escape(utftext);
}





function centra(url, width, height)
   {
   if (document.all)
      {
      var x = window.screenLeft;
      var y = window.screenTop;
      var w = window.document.body.offsetWidth;
      var h = window.document.body.offsetHeight;
      }
  else
   {
   var x = window.screenX;
   var y = window.screenY;
   var w = window.outerWidth;
   var h = window.outerHeight;
   }
  var cntx = x + Math.round((w - width) / 2);
  var cnty = y + Math.round((h - height) / 2);
 winContent = window.open (url, null, 'left=' + cntx + ',top=' + cnty + ',width=' + width + ',height=' + height  + ',resizable=yes')

winContent.focus()

}


/*
 * Moostra il div con l'id specificato
 */
function showDiv(id)
{
    var myDiv = document.getElementById(id);
    myDiv.style.display='inline';
}


/*
 * Apre un popup
 **/
function popup(url) {
	if (url!="") {
		newwindow=window.open(url,"","width=490,height=490,align=center,scrollbars=yes,resizable=yes");
		newwindow.creator=self;
	}
}


/*
 * Esegue il submit di una form in una nuova finestra
 * 
 **/
function doSubmit(nomeForm,nomepagina) {
    var submitForm = document.getElementById(nomeForm);
    displayWindow = window.open('', nomepagina, "width=490,height=490,align=center,scrollbars=yes,resizable=yes");
    submitForm.submit();
}

/*
 * Apre il link nel div specificato (ajax)
 * 
 **/
function openInDiv(nomeDiv,nomeScript,param)
{
    setTarget(document.getElementById(nomeDiv));
    update(nomeScript,param);
    
}
