// Create the XML HTTP request object. We try to be
// more cross-browser as possible.
function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}


function trim(txt)
{
return txt.replace(/\s+$|^\s+/g,"");
}


// An handler that does nothing, used for AJAX requests that
// don't require a reply and are non-critical about error conditions.
function DummyHandler() {
    return true;
}

// Shortcut for creating a GET request and get the reply
// This few lines of code can make Ajax stuff much more trivial
// to write, and... to avoid patterns in programs is sane!
function ajaxGet(url,handler) {
    var a = new Array("placeholder");
    for (var j=2; j<arguments.length; j++) {
        a[a.length] = arguments[j];
    }
    var ajax_req = CreateXmlHttpReq(DummyHandler);
    var myhandler = function() {
        var content = ajaxOk(ajax_req);
        if (content !== false) {
            a[0] = content;
            try {
                return handler.apply(this, a);
            } catch(e) {
                return myDummyApply(handler, a);
            }
        }
    }
    ajax_req.onreadystatechange = myhandler;
    ajax_req.open("GET",url);
    ajax_req.send(null);
}

// IE 5.0 does not support the apply() method of the function object,
// we resort to this eval-based solution that sucks because it is not
// capable of preserving 'this' and is ugly as hell, but it works for us.
function myDummyApply(funcname,args) {
    var e = "funcname(";
    for (var i = 0; i < args.length; i++) {
        e += "args["+i+"]";
        if (i+1 != args.length) {
            e += ",";
        }
    }
    e += ");"
    return eval(e);
}

// Add a random parameter to the get request to avoid
// IE caching madness.
function ajaxGetRand(url,handler) {
    url += (url.indexOf("?") == -1) ? "?" : "&";
    url += "rand="+escape(Math.random());
    arguments[0] = url;
    try {
        return ajaxGet.apply(this,arguments);
    } catch(e) {
        return myDummyApply(ajaxGet,arguments);
    }
}

function ajaxOk(req) {
    if (req.readyState == 4 && req.status == 200) {
        return req.responseText;
    } else {
        return false;
    }
}


function hide(target) {
    var e = document.getElementById(target);
    if (e.style.visibility == 'hidden') {
        e.style.visibility = 'visible';
        e.style.display = 'block';
    } else {
        e.style.visibility = 'hidden';
        e.style.display = 'none';
    }
}

function $(id) {
    return document.getElementById(id);
}


function get_value(id)
{
return document.getElementById(id).value
}

function $html(id) {
    return document.getElementById(id).innerHTML;
	
}

function $sethtml(id,html) {
    document.getElementById(id).innerHTML = html;
}

function $apphtml(id,html) {
    document.getElementById(id).innerHTML += html;
}

//Funzione per modificare il contenuto di un elemento nella pagina, accetta i parametri target e text
function set_text(target, text) {
    var e = document.getElementById(target);
    e.innerHTML = text;
}

//Funzione per modificare il contenuto di un elemento nella pagina, accetta i parametri target e text
function timed_set_text(target, text, delay) {
var tim = setTimeout('set_text("'+target+'","'+text+'")', 1000);

}

//Funzione per prelevare il contenuto di un elemento
function get_text(target, text) {
    var e = document.getElementById(target);
   return e.innerHTML;
}

function $upd_div(content,elementid) {
    var e = document.getElementById(elementid);
    e.innerHTML = content;
}//function myHandler(content) {$sethtml('d1',content);}




function make_upd ()
{
var upd_div = function $upd_div(content,elementid) {var e = document.getElementById(elementid); e.innerHTML = content;};
return upd_div;
}

var htmlt = '<input type="submit" value="Ciao!">';





function getCookie(NameOfCookie)
{ if (document.cookie.length > 0)
{ begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function setCookie(NameOfCookie, value, expiremin)
{ var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiremin * 60 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) +
((expiremin == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}



function delCookie (NameOfCookie)
{ if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

}














// FUNZIONI PER CREARE I FORM NELL'AREA GESTIONE CLIENTI . . .
function make_form_clienti_nuova_scheda()
{
$sethtml('div_form','<b> Nuova scheda cliente: </b><br /><br /> <form name="nuovo_cliente"><table border="0"><tr><td> Nome: </td><td><input type="text" id="nome"> </td></tr><tr><td> Stato e Città: </td><td><input type="text" id="citta"> </td></tr><tr><td> Indirizzo: </td><td> <input type="text" id="indirizzo"> </td></tr><tr> <td> Telefono: </td><td><input type="text" id="numero_telefono"> </td></tr> <tr><td> P.Iva: </td><td><input type="text" id="piva"> </td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Crea scheda" onclick="crea_nuova_scheda_cliente();"></center></form> </td></tr></table> <br /><br />');
document.nuovo_cliente.nome.focus();
}


function salva_tipo_ricerca_componenti()
{
var tipologia = document.ricerca_componente.tipologia.value;
setCookie('default_tipo_di_ricerca_componenti', tipologia, 9000)
}

function salva_tipo_ricerca_oem()
{
var tipologia = document.ricerca_oem.tipologia.value;
setCookie('default_tipo_di_ricerca_oem', tipologia, 9000)
}

function salva_tipo_ricerca_progetti()
{
var tipologia = document.ricerca_progetto.tipologia.value;
setCookie('default_tipo_di_ricerca_progetti', tipologia, 9000)
}

function salva_tipo_ricerca_prodotti()
{
var tipologia = document.ricerca_prodotto.tipologia.value;
setCookie('default_tipo_di_ricerca_prodotti', tipologia, 9000)
}

function make_form_nuovo_item()
{
$sethtml('div_form','<b> Aggiungi un nuovo componente: </b><br /><br /> <form name="nuovo_componente"><table border="0"><tr><td> Nome: </td><td><input type="text" id="nome"> </td></tr><tr><td> Ubicazione: </td><td><input type="text" id="ubicazione"> </td></tr><tr><td> Quantità: </td><td><input type="text" id="quantita" value="0"> </td></tr> <tr><td> Valore indicativo: </td><td><input type="text" id="price" value="0"> € (Euro) </td></tr> <tr><td> Categoria: </td><td><input type="text" id="categoria" value="" readonly> <input type="button" name="choice" onClick="window.open(\'script/category_picker.php\',\'popuppage\',\'width=250,height=200,top=100,left=100\');" value=" ... "> </td></tr> <tr><td> Component: </td><td><input type="text" id="component" value=""> </td></tr> <tr><td> Footprint: </td><td><input type="text" id="footprint" value=""> </td></tr> <tr><td colspan="2"> Descrizione: <br> <textarea id="descrizione" cols="57" rows="20"></textarea> </td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Crea scheda" onclick="agg_nuovo_componente();"></center></form> </td></tr></table> <br /><br />');
document.nuovo_componente.nome.focus();
}


function make_ricerca_scheda_clienti()
{

$sethtml('div_form','<b> Ricerca di una scheda cliente </b><br /><br /> <form name="ricerca_cliente"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);"> </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia"><option value="nome">Nome</option> <option value="id">Id</option> <option value="citta">Città</option> <option value="indirizzo">Indirizzo</option> <option value="piva">Part. Iva</option> </select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_cliente()" id="btnEnter"></center> </td></tr></table> <br /><br />');
document.ricerca_cliente.chiave.focus();

}

function make_form_ricerca_ordini()
{
$sethtml('div_form','<b> Ricerca di un ordine </b><br /><br /> <form name="ricerca_ordine"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);"> </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia"><option value="nome">Nome</option> <option value="stato">Stato</option> <option value="data">Data</option> <option value="fornitore">Fornitore</option> <option value="price">Price</option></select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_ordine()" id="btnEnter"></center> </td></tr></table> <br /><br />');
document.ricerca_ordine.chiave.focus();
}

function make_modifica_scheda_clienti()
{
$sethtml('div_form','<b> Modifica di una scheda cliente: </b><br /><br /> <form name="modifica_cliente"><table border="0"><tr><td> Id della scheda cliente: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Modifica.." onclick="modifica_scheda_cliente();" id="btnEnter"> </td></tr></table> <br /><br />');
document.modifica_cliente.id.focus();
}
function make_elimina_scheda_cliente()
{
$sethtml('div_form','<b> Eliminazione di una scheda cliente </b><br /><br /> <form name="elimina_cliente"><table border="0"><tr><td> Id della scheda cliente: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Elimina.." onclick="elimina_scheda_cliente();"  id="btnEnter"> </td></tr></table> <br /><br />');
document.elimina_cliente.id.focus();
}

// FUNZIONI PER CREARE I FORM NELL'AREA GESTIONE CLIENTI . . .
function make_form_fornitori_nuova_scheda()
{
$sethtml('div_form','<b> Nuova scheda fornitore: </b><br /><br /> <form name="nuovo_fornitore"><table border="0"><tr><td> Nome: </td><td><input type="text" id="nome"> </td></tr><tr><td> Stato e Città: </td><td><input type="text" id="citta"> </td></tr><tr><td> Indirizzo: </td><td> <input type="text" id="indirizzo"> </td></tr><tr> <td> Numero: </td><td><input type="text" id="numero_telefono"> </td></tr><tr><td> Note:  </td><td> <input type="text" id="note"> </td></tr><tr><td> P.Iva: </td><td><input type="text" id="piva"> </td></tr><tr><td> Programming url: </td><td><input type="text" id="programming_url" value="#"> </td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Crea scheda" onclick="crea_nuova_scheda_fornitore();"></center></form> </td></tr></table> <br /><br />');
document.nuovo_fornitore.nome.focus();
}

function make_ricerca_scheda_fornitori()
{
$sethtml('div_form','<b> Ricerca di una scheda fornitore </b><br /><br /> <form name="ricerca_fornitore"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);"> </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia"><option value="nome">Nome</option> <option value="id">Id</option> <option value="citta">Città</option> <option value="indirizzo">Indirizzo</option> <option value="note">Note</option> <option value="piva">Part. Iva</option> </select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_fornitore()" id="btnEnter"></center> </td></tr></table> <br /><br />');
document.ricerca_fornitore.chiave.focus();
}

function make_modifica_scheda_fornitori()
{
$sethtml('div_form','<b> Modifica di una scheda fornitore <b/><br /> <form name="modifica_fornitore"><table border="0"><tr><td> Id della scheda fornitore: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Modifica.." onclick="modifica_scheda_fornitore();" id="btnEnter"> </td></tr></table> <br /><br />');
document.modifica_fornitore.id.focus();
}

function make_elimina_scheda_fornitori()
{
$sethtml('div_form','<b> Eliminazione di una scheda fornitore </b><br /><br /> <form name="elimina_fornitore"><table border="0"><tr><td> Id della scheda fornitore: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Elimina.." onclick="elimina_scheda_fornitore();" id="btnEnter"> </td></tr></table> <br /><br />');
document.elimina_fornitore.id.focus();
}

function make_form_modifica_item()
{
$sethtml('div_form','<b> Modifica di una scheda componente <b/><br /> <form name="modifica_componente"><table border="0"><tr><td> Id del del componente: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Modifica.." onclick="modifica_scheda_componente();" id="btnEnter"> </td></tr></table> <br /><br />');
document.modifica_componente.id.focus();
}

function make_form_elimina_item()
{
$sethtml('div_form','<b> Elimina una scheda componente <b/><br /> <form name="elimina_componente"><table border="0"><tr><td> Id del del componente: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Elimina.." onclick="elimina_scheda_componente();" id="btnEnter"> </td></tr></table> <br /><br />');
document.elimina_componente.id.focus();
}

function make_form_gestione_categorie()
{
$sethtml('div_form','<b> Aggiungi una nuova categoria: </b><br /><br /> <form name="agg_categoria"><table border="0"><tr><td> Nome categoria: </td><td><input type="text" id="nome"> <input type="button" name="choice" onClick="window.open(\'script/category_list.php\',\'popuppage\',\'width=250,height=200,top=100,left=100\');" value="Lista delle categorie"></td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Agg. Categoria" onclick="agg_nuova_categoria();"></center></form> </td></tr></table> <br /><br /> <b> Elimina una categoria: </b><br /><br /> <form name="elimina_categoria"><table border="0"><tr><td> Nome categoria: </td><td><input type="text" id="nome"> <input type="button" name="choice" onClick="window.open(\'script/category_picker_gestione.php\',\'popuppage\',\'width=250,height=200,top=100,left=100\');" value=" ... "></td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Rimuovi. Categoria" onclick="rimuovi_categoria();"></center></form> </td></tr></table> <br /><br />');
document.agg_categoria.nome.focus();
}

function make_form_ricerca_item()
{
$sethtml('div_form','<b> Ricerca di una scheda componente </b><br /><br /> <form name="ricerca_componente"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);" > </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia" onchange="salva_tipo_ricerca_componenti();"><option value="nome">Nome</option> <option value="id">Id</option> <option value="descrizione">Descrizione</option> <option value="ubicazione">Ubicazione</option> <option value="footprint">Footprint</option> <option value="component">Component</option> <option value="categoria">Categoria</option> </select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_componente()" id="btnEnter"></center> </td></tr></table> <br /><br />');
document.ricerca_componente.chiave.focus();
var tipo = getCookie('default_tipo_di_ricerca_componenti');

if ( tipo == 'id' )
document.ricerca_componente.tipologia.selectedIndex = 1;

if ( tipo == 'descrizione' )
document.ricerca_componente.tipologia.selectedIndex = 2;

if ( tipo == 'ubicazione' )
document.ricerca_componente.tipologia.selectedIndex = 3;

if ( tipo == 'footprint' )
document.ricerca_componente.tipologia.selectedIndex = 4;

if ( tipo == 'component' )
document.ricerca_componente.tipologia.selectedIndex = 5;

if ( tipo == 'categoria' )
document.ricerca_componente.tipologia.selectedIndex = 6;
}

function make_form_nuovo_oem()
{

$sethtml('div_form','<b> Aggiungi un nuovo oem: </b><br /><br /> <form name="nuovo_oem"><table border="0"><tr><td> Nome: </td><td><input type="text" id="nome"> </td></tr><tr><td> Ubicazione: </td><td><input type="text" id="ubicazione"> </td></tr><tr><td> Quantità: </td><td><input type="text" id="quantita" value="0"> </td></tr> <tr><td> Valore indicativo: </td><td><input type="text" id="price" value="0"> € (Euro) </td></tr> <tr><td> Categoria: </td><td><input type="text" id="categoria" value="" readonly> <input type="button" name="choice" onClick="window.open(\'script/category_picker_oem.php\',\'popuppage\',\'width=250,height=200,top=100,left=100\');" value=" ... "> </td></tr> <tr><td colspan="2"> Descrizione: <br> <textarea id="descrizione" cols="57" rows="20"></textarea> </td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Crea scheda" onclick="agg_nuovo_oem();"></center></form> </td></tr></table> <br /><br />');

document.nuovo_oem.nome.focus();

}

function make_form_ricerca_oem()
{
$sethtml('div_form','<b> Ricerca di una scheda oem </b><br /><br /> <form name="ricerca_oem"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);" > </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia" onclick="salva_tipo_ricerca_oem();"><option value="nome">Nome</option> <option value="id">Id</option> <option value="descrizione">Descrizione</option> <option value="ubicazione">Ubicazione</option> <option value="categoria">Categoria</option> </select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_oem()" id="btnEnter"></center> </td></tr></table> <br /><br />');

document.ricerca_oem.chiave.focus();

var tipo = getCookie('default_tipo_di_ricerca_oem');

if ( tipo == 'id' )
document.ricerca_oem.tipologia.selectedIndex = 1;

if ( tipo == 'descrizione' )
document.ricerca_oem.tipologia.selectedIndex = 2;

if ( tipo == 'ubicazione' )
document.ricerca_oem.tipologia.selectedIndex = 3;

if ( tipo == 'categoria' )
document.ricerca_oem.tipologia.selectedIndex = 4;

}


function make_form_ricerca_prodotto()
{
$sethtml('div_form','<b> Ricerca di una scheda prodotto </b><br /><br /> <form name="ricerca_prodotto"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);" > </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia" onclick="salva_tipo_ricerca_prodotti();"><option value="nome">Nome</option> <option value="id">Id</option> <option value="descrizione">Descrizione</option> <option value="ubicazione">Ubicazione</option> <option value="categoria">Categoria</option> </select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_prodotto()" id="btnEnter"></center> </td></tr></table> <br /><br />');
document.ricerca_prodotto.chiave.focus();

var tipo = getCookie('default_tipo_di_ricerca_prodotti');

if ( tipo == 'id' )
document.ricerca_prodotto.tipologia.selectedIndex = 1;

if ( tipo == 'descrizione' )
document.ricerca_prodotto.tipologia.selectedIndex = 2;

if ( tipo == 'ubicazione' )
document.ricerca_prodotto.tipologia.selectedIndex = 3;

if ( tipo == 'categoria' )
document.ricerca_prodotto.tipologia.selectedIndex = 4;
}


function make_form_modifica_oem()
{
$sethtml('div_form','<b> Modifica di una scheda oem <b/><br /> <form name="modifica_oem"><table border="0"><tr><td> Id del oem: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Modifica.." onclick="modifica_scheda_oem();" id="btnEnter"> </td></tr></table> <br /><br />');
document.modifica_oem.id.focus();
}
function make_form_elimina_oem()
{
$sethtml('div_form','<b> Elimina una scheda oem <b/><br /> <form name="elimina_oem"><table border="0"><tr><td> Id del del componente: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Elimina.." onclick="elimina_scheda_oem();" id="btnEnter"> </td></tr></table> <br /><br />');
document.elimina_oem.id.focus();
}


function make_form_nuovo_prodotto()
{
$sethtml('div_form','<b> Aggiungi un nuovo prodotto: </b><br /><br /> <form name="nuovo_prodotto"><table border="0"><tr><td> Nome: </td><td><input type="text" id="nome"> </td></tr><tr><td> Ubicazione: </td><td><input type="text" id="ubicazione"> </td></tr><tr><td> Quantità: </td><td><input type="text" id="quantita" value="0"> </td></tr> <tr><td> Valore indicativo: </td><td><input type="text" id="price" value="0"> € (Euro) </td></tr> <tr><td> Categoria: </td><td><input type="text" id="categoria" value="" readonly> <input type="button" name="choice" onClick="window.open(\'script/category_picker_prodotti.php\',\'popuppage\',\'width=250,height=200,top=100,left=100\');" value=" ... "> </td></tr> <tr><td colspan="2"> Descrizione: <br> <textarea id="descrizione" cols="57" rows="20"></textarea> </td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Crea scheda" onclick="agg_nuovo_prodotto();"></center></form> </td></tr></table> <br /><br />');
document.nuovo_prodotto.nome.focus();
}

function make_form_modifica_prodotto()
{
$sethtml('div_form','<b> Modifica di una scheda prodotto <b/><br /> <form name="modifica_prodotto"><table border="0"><tr><td> Id del prodotto: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Modifica.." onclick="modifica_scheda_prodotto();" id="btnEnter"> </td></tr></table> <br /><br />');
document.modifica_prodotto.id.focus();
}
function make_form_elimina_prodotto()
{
$sethtml('div_form','<b> Elimina una scheda prodotto <b/><br /> <form name="elimina_prodotto"><table border="0"><tr><td> Id del del prodotto: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Elimina.." onclick="elimina_scheda_prodotto();" id="btnEnter"> </td></tr></table> <br /><br />');
document.elimina_prodotto.id.focus();
}





function make_form_nuovo_progetto()
{
$sethtml('div_form','<b> Crea un nuovo progetto: </b><br /><br /> <form name="nuovo_progetto"><table border="0"><tr><td> Nome: </td><td><input type="text" id="nome"> </td></tr><tr><td> Stato: </td><td><input type="text" id="stato" value="In Progettazione" readonly> </td></tr> <tr><td> Valore indicativo: </td><td><input type="text" id="price" value="0"> € (Euro) </td></tr>  <tr><td colspan="2"> Descrizione: <br> <textarea id="descrizione" cols="57" rows="20"></textarea> </td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Crea scheda" onclick="agg_nuovo_progetto();"></center></form> </td></tr></table> <br /><br />');
document.nuovo_progetto.nome.focus();
}
function make_form_ricerca_progetto()
{
$sethtml('div_form','<b> Ricerca di un progetto: </b><br /><br /> <form name="ricerca_progetto"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);" > </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia" onclick="salva_tipo_ricerca_progetti();"><option value="nome">Nome</option><option value="descrizione">Descrizione</option> <option value="id">Id</option> <option value="stato">Stato</option></select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_progetto()" id="btnEnter"></center> </td></tr></table> <br /><br />');
document.ricerca_progetto.chiave.focus();


var tipo = getCookie('default_tipo_di_ricerca_progetti');

if ( tipo == 'descrizione' )
document.ricerca_progetto.tipologia.selectedIndex = 1;

if ( tipo == 'id' )
document.ricerca_progetto.tipologia.selectedIndex = 2;

if ( tipo == 'stato' )
document.ricerca_progetto.tipologia.selectedIndex = 3;

}
function make_form_modifica_progetto()
{
$sethtml('div_form','<b> Modifica di una scheda progetto: <b/><br /> <form name="modifica_progetto"><table border="0"><tr><td> Id del progetto: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Modifica.." onclick="modifica_scheda_progetto();" id="btnEnter"> </td></tr></table> <br /><br />');
document.modifica_progetto.id.focus();
}
function make_form_elimina_progetto()
{
$sethtml('div_form','<b> Elimina una scheda progetto: <b/><br /> <form name="elimina_progetto"><table border="0"><tr><td> Id del del progetto: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Elimina.." onclick="elimina_scheda_progetto();" id="btnEnter"> </td></tr></table> <br /><br />');
document.elimina_progetto.id.focus();
}





function make_form_nuova_commessa()
{
$sethtml('div_form','<b> Aggiungi una nuova commessa/attivita\': </b><br /><br /> <form name="nuova_commessa"><table border="0"><tr><td> Nome: </td><td><input type="text" id="nome"> </td></tr><tr><td> Numero commessa/attivita\: </td><td><input type="text" id="numero_commessa"> </td></tr><tr><td> Data: </td><td><input type="text" id="data" value="oggi" readonly> </td></tr> <tr><td> Stato: </td><td><input type="text" id="stato" value="da trattare" readonly> </td></tr>  <tr><td> Prezzo vendita indicativo: </td><td><input type="text" id="price" value="0"> € (Euro) </td></tr> <tr><td colspan="2"> Descrizione: <br> <textarea id="descrizione" cols="57" rows="20"></textarea> </td></tr><tr><td colspan="2"> <br /> <center><input type="button" value="Crea scheda" onclick="agg_nuova_commessa();"></center></form> </td></tr></table> <br /><br />');
document.nuova_commessa.nome.focus();
}

function make_form_ricerca_commessa()
{
$sethtml('div_form','<b> Ricerca di una commessa/attivita\ </b><br /><br /> <form name="ricerca_commessa"><table border="0"><tr><td> Chiave di ricerca: </td><td><input type="text" id="chiave" onkeypress="return onEnter(event,this.form);"> </td></tr><tr><td>  Ricerca per: </td><td> <select name="tipologia"><option value="nome">Nome</option> <option value="id">Id</option>  <option value="numero_commessa">Numero Commessa/attivita\</option> <option value="descrizione">Descrizione</option>   </select></td></tr><tr><td colspan="2"><br /><br /> <center><input type="button" value="Ricerca.." onclick="ricerca_scheda_commessa()" id="btnEnter"></center> </td></tr></table> <br /><br />');
document.ricerca_commessa.chiave.focus();
}

function make_form_elimina_commessa()
{
$sethtml('div_form','<b> Elimina una commessa/attivita\ <b/><br /> <form name="elimina_commessa"><table border="0"><tr><td> Id dela commessa/attivita\: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Elimina.." onclick="elimina_scheda_commessa();" id="btnEnter"> </td></tr></table> <br /><br />');
document.elimina_commessa.id.focus();
}

function make_form_modifica_commessa()
{
$sethtml('div_form','<b> Modifica una commessa/attivita\ <b/><br /> <form name="modifica_commessa"><table border="0"><tr><td> Id della commessa/attivita\: </td><td><input type="text" id="id" onkeypress="return onEnter(event,this.form);"> <input type="button" value="Modifica.." onclick="modifica_scheda_commessa();" id="btnEnter"> </td></tr></table> <br /><br />');
document.modifica_commessa.id.focus();
}

function agg_nuova_categoria() {
    var nome = document.agg_categoria.nome.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/agg_nuova_categoria.php?nome="+escape(nome)+"&r="+escape(r));
    myRequest.send(null);
}

function rimuovi_categoria() {
    var nome = document.elimina_categoria.nome.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/elimina_categoria.php?nome="+escape(nome)+"&r="+escape(r));
    myRequest.send(null);
}


function modifica_componente_ok() {
    var id = document.modifica_componente.id.value;
    var nome = document.modifica_componente.nome.value;
	var ubicazione = document.modifica_componente.ubicazione.value;
	var descrizione = document.modifica_componente.descrizione.value;
	var quantita = document.modifica_componente.quantita.value;
	var categoria = document.modifica_componente.categoria.value;
	var component = document.modifica_componente.component.value;
	var footprint = document.modifica_componente.footprint.value;   
	var price = document.modifica_componente.price.value;   
	var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_componente_ok.php?nome="+escape(nome)+"&id="+escape(id)+"&ubicazione="+escape(ubicazione)+"&quantita="+escape(quantita)+"&descrizione="+escape(descrizione)+"&footprint="+escape(footprint)+"&categoria="+escape(categoria)+"&component="+escape(component)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_commessa_ok() {
    var id = document.modifica_commessa.id.value;
	var nome = document.modifica_commessa.nome.value;
	var descrizione = document.modifica_commessa.descrizione.value;
	var numero_commessa = document.modifica_commessa.numero_commessa.value;
	var stato = document.modifica_commessa.stato.value;
	var price = document.modifica_commessa.price.value;   
	var storico = document.modifica_commessa.storico.value;
	
	var commessa_flag = 0;
	
	if (document.modifica_commessa.commessa_flag.checked == true)
	commessa_flag = 1;
	
	//alert(storico);
	
	var r = Math.random();
    
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
	myRequest.open("GET","script/modifica_scheda_commessa_ok.php?nome="+escape(nome)+"&id="+escape(id)+"&storico="+escape(storico)+"&numero_commessa="+escape(numero_commessa)+"&stato="+escape(stato)+"&commessa_flag="+escape(commessa_flag)+"&descrizione="+escape(descrizione)+"&price="+escape(price)+"&r="+escape(r));
	myRequest.send(null);
}

function add_a_note_commessa(dt){

	var storico = document.modifica_commessa.storico.value;
	document.modifica_commessa.storico.value = dt + '\n\n\n' + '======================================\n\n\n\n' + document.modifica_commessa.storico.value;
}

function add_a_note_progetto(dt){

	var storico = document.modifica_progetto.storico.value;
	document.modifica_progetto.storico.value = dt + '\n\n\n'+ '======================================\n\n\n\n' + document.modifica_progetto.storico.value;
}

function modifica_progetto_ok() {
    var id = document.modifica_progetto.id.value;
	var nome = document.modifica_progetto.nome.value;
	var descrizione = document.modifica_progetto.descrizione.value;
	var storico = document.modifica_progetto.storico.value;
	var stato = document.modifica_progetto.stato.value;
	var price = document.modifica_progetto.price.value;   
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
	myRequest.open("GET","script/modifica_scheda_progetto_ok.php?nome="+escape(nome)+"&id="+escape(id)+"&storico="+escape(storico)+"&stato="+escape(stato)+"&descrizione="+escape(descrizione)+"&price="+escape(price)+"&r="+escape(r));
	myRequest.send(null);
}



function modifica_oem_ok() {
    var id = document.modifica_oem.id.value;
    var nome = document.modifica_oem.nome.value;
	var ubicazione = document.modifica_oem.ubicazione.value;
	var descrizione = document.modifica_oem.descrizione.value;
	var quantita = document.modifica_oem.quantita.value;
	var categoria = document.modifica_oem.categoria.value;
	var price = document.modifica_oem.price.value;   
	var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_oem_ok.php?nome="+escape(nome)+"&id="+escape(id)+"&ubicazione="+escape(ubicazione)+"&quantita="+escape(quantita)+"&descrizione="+escape(descrizione)+"&categoria="+escape(categoria)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_prodotto_ok() {
    var id = document.modifica_oem.id.value;
    var nome = document.modifica_oem.nome.value;
	var ubicazione = document.modifica_oem.ubicazione.value;
	var descrizione = document.modifica_oem.descrizione.value;
	var quantita = document.modifica_oem.quantita.value;
	var categoria = document.modifica_oem.categoria.value;
	var price = document.modifica_oem.price.value;   
	var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_prodotto_ok.php?nome="+escape(nome)+"&id="+escape(id)+"&ubicazione="+escape(ubicazione)+"&quantita="+escape(quantita)+"&descrizione="+escape(descrizione)+"&categoria="+escape(categoria)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
}

function agg_nuova_commessa() {
    var nome = document.nuova_commessa.nome.value;
	var numero_commessa = document.nuova_commessa.numero_commessa.value;
	var descrizione = document.nuova_commessa.descrizione.value;
	var price = document.nuova_commessa.price.value;
	var stato = document.nuova_commessa.stato.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/agg_nuova_commessa.php?nome="+escape(nome)+"&descrizione="+escape(descrizione)+"&stato="+escape(stato)+"&price="+escape(price)+"&numero_commessa="+escape(numero_commessa)+"&r="+escape(r));
    myRequest.send(null);
}

function agg_nuovo_progetto() {
    var nome = document.nuovo_progetto.nome.value;
	var stato = document.nuovo_progetto.stato.value;
	var descrizione = document.nuovo_progetto.descrizione.value;
	var price = document.nuovo_progetto.price.value;   
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/agg_nuovo_progetto.php?nome="+escape(nome)+"&stato="+escape(stato)+"&descrizione="+escape(descrizione)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
}

function agg_nuovo_oem() {
    var nome = document.nuovo_oem.nome.value;
	var ubicazione = document.nuovo_oem.ubicazione.value;
	var descrizione = document.nuovo_oem.descrizione.value;
	var quantita = document.nuovo_oem.quantita.value;
	var categoria = document.nuovo_oem.categoria.value;
	var price = document.nuovo_oem.price.value;   
	var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/agg_nuovo_oem.php?nome="+escape(nome)+"&ubicazione="+escape(ubicazione)+"&quantita="+escape(quantita)+"&descrizione="+escape(descrizione)+"&categoria="+escape(categoria)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
}

function pass2md5()
{
//document.getElementById('password').value = hex_md5(document.getElementById('password').value);
//alert (document.getElementById('password').value);
return true;
}

function agg_nuovo_prodotto() {
    var nome = document.nuovo_prodotto.nome.value;
	var ubicazione = document.nuovo_prodotto.ubicazione.value;
	var descrizione = document.nuovo_prodotto.descrizione.value;
	var quantita = document.nuovo_prodotto.quantita.value;
	var categoria = document.nuovo_prodotto.categoria.value;
	var price = document.nuovo_prodotto.price.value;   
	var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/agg_nuovo_prodotto.php?nome="+escape(nome)+"&ubicazione="+escape(ubicazione)+"&quantita="+escape(quantita)+"&descrizione="+escape(descrizione)+"&categoria="+escape(categoria)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
}

function agg_nuovo_componente() {
    var nome = document.nuovo_componente.nome.value;
	var ubicazione = document.nuovo_componente.ubicazione.value;
	var descrizione = document.nuovo_componente.descrizione.value;
	var quantita = document.nuovo_componente.quantita.value;
	var categoria = document.nuovo_componente.categoria.value;
	var component = document.nuovo_componente.component.value;
	var footprint = document.nuovo_componente.footprint.value;   
	var price = document.nuovo_componente.price.value;   
	var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/agg_nuovo_componente.php?nome="+escape(nome)+"&ubicazione="+escape(ubicazione)+"&quantita="+escape(quantita)+"&descrizione="+escape(descrizione)+"&footprint="+escape(footprint)+"&categoria="+escape(categoria)+"&component="+escape(component)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
}
function elimina_scheda_commessa_confermato(id)
{
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la scheda?")
if (x)
elimina_scheda_commessa_confirm_rq(id);
}

function elimina_scheda_cliente_confermato(id)
{
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la scheda?")
if (x)
elimina_scheda_cliente_confirm_rq(id);
}

function elimina_scheda_componente_confermato(id)
{
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la scheda del componente?")
if (x)
elimina_scheda_componente_confirm_rq(id);
}

function elimina_scheda_oem_confermato(id)
{
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la scheda del oem?")
if (x)
elimina_scheda_oem_confirm_rq(id);
}

function elimina_scheda_prodotto_confermato(id)
{
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la scheda del prodotto?")
if (x)
elimina_scheda_prodotto_confirm_rq(id);
}

function elimina_categoria_confermato(id)
{
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la categoria?")
if (x)
elimina_categoria_confermato_rq(id);

}


function elimina_scheda_fornitore_confermato(id)
{
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la scheda?")
if (x)
elimina_scheda_fornitore_confirm_rq(id);

}



function crea_nuova_scheda_fornitore() {
    var nome = document.nuovo_fornitore.nome.value;
	var indirizzo = document.nuovo_fornitore.indirizzo.value;
	var citta = document.nuovo_fornitore.citta.value;
	var note = document.nuovo_fornitore.note.value;
	var numero_telefono = document.nuovo_fornitore.numero_telefono.value;
	var piva = document.nuovo_fornitore.piva.value;
    var r = Math.random();
	var programming_url = document.nuovo_fornitore.programming_url.value;
	
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/crea_nuova_scheda_fornitore.php?nome="+escape(nome)+"&indirizzo="+escape(indirizzo)+"&citta="+escape(citta)+"&note="+escape(note)+"&numero_telefono="+escape(numero_telefono)+"&piva="+escape(piva)+"&programming_url="+escape(programming_url)+"&r="+escape(r));
    myRequest.send(null);
}


function crea_nuova_scheda_cliente() {
    var nome = document.nuovo_cliente.nome.value;
	var indirizzo = document.nuovo_cliente.indirizzo.value;
	var citta = document.nuovo_cliente.citta.value;
	var numero_telefono = document.nuovo_cliente.numero_telefono.value;
	var piva = document.nuovo_cliente.piva.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/crea_nuova_scheda_clienti.php?nome="+escape(nome)+"&indirizzo="+escape(indirizzo)+"&citta="+escape(citta)+"&numero_telefono="+escape(numero_telefono)+"&piva="+escape(piva)+"&r="+escape(r));
    myRequest.send(null);
}

function myHandler_nuova_scheda_cliente() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        set_text('div_form', myRequest.responseText);
    }
}

function myHandler_timed() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        timed_set_text('div_form', myRequest.responseText, 3000);
    }
}


function projectload() {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_timed);
    myRequest.open("GET","script/ultimi_progetti.php?r="+escape(r));
    myRequest.send(null);
}


function modifica_scheda_oem() {
    var id = document.modifica_oem.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_oem.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_progetto() {
    var id = document.modifica_progetto.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_progetto.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_progetto_id(id) {
   // var id = document.modifica_progetto.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_progetto.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_commessa_id(id) {
   // var id = document.modifica_commessa.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_commessa.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_commessa() {
    var id = document.modifica_commessa.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_commessa.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_oem_id(id) {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_oem.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_prodotto() {
    var id = document.modifica_prodotto.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_prodotto.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}
function modifica_scheda_prodotto_id(id) {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_prodotto.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_componente() {
    var id = document.modifica_componente.id.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_componente.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_componente_id(id) {
   // var id = document.modifica_componente.id.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_componente.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_cliente_id(id) {
   // var id = document.modifica_cliente.id.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_clienti.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_cliente() {
    var id = document.modifica_cliente.id.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_clienti.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}


function modifica_scheda_fornitore() {
    var id = document.modifica_fornitore.id.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_fornitori.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_cliente_ok() {
    var id = document.modifica_dati_clienti.id.value;
    var nome = document.modifica_dati_clienti.nome.value;
	var indirizzo = document.modifica_dati_clienti.indirizzo.value;
	var citta = document.modifica_dati_clienti.citta.value;
	//var riferimenti = document.modifica_dati_clienti.riferimenti.value;
	var numero_telefono = document.modifica_dati_clienti.numero_telefono.value;
	var piva = document.modifica_dati_clienti.piva.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_clienti_ok.php?nome="+escape(nome)+"&id="+escape(id)+"&indirizzo="+escape(indirizzo)+"&citta="+escape(citta)+"&numero_telefono="+escape(numero_telefono)+"&piva="+escape(piva)+"&r="+escape(r));
    myRequest.send(null);
}

function modifica_scheda_fornitore_ok() {
    var id = document.modifica_dati_fornitori.id.value;
    var nome = document.modifica_dati_fornitori.nome.value;
	var indirizzo = document.modifica_dati_fornitori.indirizzo.value;
	var citta = document.modifica_dati_fornitori.citta.value;
	var riferimenti = document.modifica_dati_fornitori.riferimenti.value;
	var numero_telefono = document.modifica_dati_fornitori.numero_telefono.value;
	var piva = document.modifica_dati_fornitori.piva.value;
	var programming_url = document.modifica_dati_fornitori.programming_url.value;
    var r = Math.random();
    //alert(piva);
	myRequest = CreateXmlHttpReq(myHandler_nuova_scheda_cliente);
    myRequest.open("GET","script/modifica_scheda_fornitori_ok.php?nome="+escape(nome)+"&id="+escape(id)+"&indirizzo="+escape(indirizzo)+"&citta="+escape(citta)+"&riferimenti="+escape(riferimenti)+"&numero_telefono="+escape(numero_telefono)+"&piva="+escape(piva)+"&programming_url="+escape(programming_url)+"&r="+escape(r));
    myRequest.send(null);
}


function myHandler_modifica_scheda_cliente() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        set_text('div_form', myRequest.responseText);
    }
}

function ricerca_scheda_progetto()
{
    var chiave = document.ricerca_progetto.chiave.value;
	var tipologia = document.ricerca_progetto.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_progetto.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}

function ricerca_scheda_commessa() {
    var chiave = document.ricerca_commessa.chiave.value;
	var tipologia = document.ricerca_commessa.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_commessa.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}


function ricerca_scheda_ordine() {
    var chiave = document.ricerca_ordine.chiave.value;
	var tipologia = document.ricerca_ordine.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_ordine.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}


function ricerca_scheda_componente() {
    var chiave = document.ricerca_componente.chiave.value;
	var tipologia = document.ricerca_componente.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_componente.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}

function ricerca_scheda_oem() {
    var chiave = document.ricerca_oem.chiave.value;
	var tipologia = document.ricerca_oem.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_oem.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}
function ricerca_scheda_prodotto() {
    var chiave = document.ricerca_prodotto.chiave.value;
	var tipologia = document.ricerca_prodotto.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_prodotto.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}

function ricerca_scheda_cliente() {
    var chiave = document.ricerca_cliente.chiave.value;
	var tipologia = document.ricerca_cliente.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_cliente.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}

function ricerca_scheda_fornitore() {
    var chiave = document.ricerca_fornitore.chiave.value;
	var tipologia = document.ricerca_fornitore.tipologia.value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/ricerca_scheda_fornitore.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
    myRequest.send(null);
}

function myHandler_ricerca_scheda_cliente() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        set_text('div_form', myRequest.responseText);
    }
}
function elimina_scheda_progetto() {
    var id = document.elimina_progetto.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_progetto.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_commessa() {
    var id = document.elimina_commessa.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_commessa.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_componente() {
    var id = document.elimina_componente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_componente.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_oem() {
    var id = document.elimina_oem.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_oem.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_prodotto() {
    var id = document.elimina_prodotto.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_prodotto.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_cliente() {
    var id = document.elimina_cliente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_cliente.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_fornitore() {
    var id = document.elimina_fornitore.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_fornitore.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function myHandler_elimina_scheda_cliente() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        set_text('div_form', myRequest.responseText);
    }
}

function elimina_categoria_confermato_rq(id) {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_categoria_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_commessa_confirm_rq(id) {
  //  var id = document.elimina_cliente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_commessa_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_progetto_confermato(id) {
var x=window.confirm("Sei sicuro di voler eliminare definitivamente la scheda?")
if (x)
elimina_scheda_progetto_confirm_rq(id);
}

function elimina_scheda_progetto_confirm_rq(id) {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_progetto_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_cliente_confirm_rq(id) {
  //  var id = document.elimina_cliente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_cliente_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_componente_confirm_rq(id) {
  //  var id = document.elimina_cliente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_componente_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}


function elimina_scheda_oem_confirm_rq(id) {
  //  var id = document.elimina_cliente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_oem_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_prodotto_confirm_rq(id) {
  //  var id = document.elimina_cliente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_prodotto_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function elimina_scheda_fornitore_confirm_rq(id) {
  //  var id = document.elimina_cliente.id.value;
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/elimina_scheda_fornitore_cnf.php?id="+escape(id)+"&r="+escape(r));
    myRequest.send(null);
}

function myHandler_elimina_scheda_cliente_confirm_rq() {
  if (myRequest.readyState == 4 && myRequest.status == 200) {
  set_text('div_form', myRequest.responseText);
 }
}

function timertest()
{
setTimeout('alert("ciao!")', 3000); //l'oggetto window può essere sottointeso
}

function 
bgImage(id,image)
{
document.getElementById(id).style.backgroundImage = 'url('+image+')'; 
}
	
	
	
//GESTIONE DELL'EVENTO ENTER NEI CAMPI DELLE VARIE FORM........
function onEnter( evt, frm ) {
var keyCode = null;

if( evt.which ) {
keyCode = evt.which;
} else if( evt.keyCode ) {
keyCode = evt.keyCode;
}
if( 13 == keyCode ) {
frm.btnEnter.click();
return false;
}
return true;
}

function myHandler_gest_search(){
  if (myRequest.readyState == 4 && myRequest.status == 200) {
   set_text('risultati_search', myRequest.responseText);
   }
}

function componenti_gest_search() 
{
var chiave = document.searchform.chiave.value;
var tipologia = document.searchform.tipologia.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_gest_search);
myRequest.open("GET","ricerca_scheda_componente_gest.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
myRequest.send(null);
}

function componenti_gest_search_sn() 
{
var chiave = document.searchform.chiave.value;
var tipologia = document.searchform.tipologia.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_gest_search);
myRequest.open("GET","ricerca_scheda_componente_gest_sn.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
myRequest.send(null);
}

function progetti_gest_search_sn() 
{
var chiave = document.searchform.chiave.value;
var tipologia = document.searchform.tipologia.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_gest_search);
myRequest.open("GET","ricerca_scheda_progetto_gest.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
myRequest.send(null);
}

function oem_gest_search() 
{
var chiave = document.searchform.chiave.value;
var tipologia = document.searchform.tipologia.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_gest_search);
myRequest.open("GET","ricerca_scheda_oem_gest.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
myRequest.send(null);
}

function oem_gest_search_sn() 
{
var chiave = document.searchform.chiave.value;
var tipologia = document.searchform.tipologia.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_gest_search);
myRequest.open("GET","ricerca_scheda_oem_gest_sn.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
myRequest.send(null);
}


function prodotto_gest_search_sn() 
{
var chiave = document.searchform.chiave.value;
var tipologia = document.searchform.tipologia.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_gest_search);
myRequest.open("GET","ricerca_scheda_prodotto_gest_sn.php?chiave="+escape(chiave)+"&tipologia="+escape(tipologia)+"&r="+escape(r));
myRequest.send(null);
}

function associa_progetto(id, nome, price)
{
document.searchform.id_progetto.value = id;
document.searchform.nome.value = nome;
document.searchform.prezzo.value = price;
}

function associa_componente_oem(id, nome)
{
document.searchform.id_componente.value = id;
document.searchform.nome_componente.value = nome;
document.searchform.quantita.focus();
}

function associa_oem_prodotto(id, nome)
{
document.searchform.id_oem.value = id;
document.searchform.nome_oem.value = nome;
document.searchform.quantita.focus();
}
function associa_oem_prodotto_sn(id, nome)
{
document.searchform.id_oem.value = id;
document.searchform.nome_oem.value = nome;
document.searchform.sn.focus();
}
function associa_prodotto_commessa_sn(id, nome)
{
document.searchform.id_prodotto.value = id;
document.searchform.nome_prodotto.value = nome;
document.searchform.sn.focus();
}
function associa_componente_oem_sn(id, nome)
{
document.searchform.id_componente.value = id;
document.searchform.nome_componente.value = nome;
document.searchform.sn.focus();
}

function myHandler_serial_search(){
  if (myRequest.readyState == 4 && myRequest.status == 200) {
   set_text('hidden_div', myRequest.responseText);
document.ss.id_entita.value = document.sh.id_entita.value;
document.ss.tipo_entita.value = document.sh.tipologia.value;
document.ss.nome_ogetto.value = document.sh.nome.value;
   
   
   }
   
}


function serial_search() 
{
var serial = document.ss.serial.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_serial_search);
myRequest.open("GET","ricerca_serial.php?serial="+escape(serial)+"&r="+escape(r));
myRequest.send(null);
}

function product_search() 
{
var nome = document.ss.nome.value;
var r = Math.random();
myRequest = CreateXmlHttpReq(myHandler_serial_search);
myRequest.open("GET","ricerca_prodotto.php?nome="+escape(nome)+"&r="+escape(r));
myRequest.send(null);
}


function myHandler_check_componente() {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
    }
}

function check_componente(id,stato) {
    var r = Math.random();
	var statos;

	if ( stato == true )
	statos = '1';
	else
	statos = "0";
	
	myRequest = CreateXmlHttpReq(myHandler_check_componente);
    myRequest.open("GET","script/check_componente.php?id="+escape(id)+"&statos="+escape(statos)+"&r="+escape(r));
    myRequest.send(null);
}

function mqty_componente(id,qty) {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_check_componente);
    myRequest.open("GET","script/mod_qty_componente.php?id="+escape(id)+"&qty="+escape(qty)+"&r="+escape(r));
    myRequest.send(null);
}

function mqty_arrivata_componente(id,qty) {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_check_componente);
    myRequest.open("GET","script/mod_qty_arrivata_componente.php?id="+escape(id)+"&qty="+escape(qty)+"&r="+escape(r));
    myRequest.send(null);
}

function convalida_ordine(id,nome_ordine, price) {
    var r = Math.random();
	if ( nome_ordine != "" && price != "" )
	{
	myRequest = CreateXmlHttpReq(myHandler_ricerca_scheda_cliente);
    myRequest.open("GET","script/convalida_ordine.php?id="+escape(id)+"&nome_ordine="+escape(nome_ordine)+"&price="+escape(price)+"&r="+escape(r));
    myRequest.send(null);
	}
	else
	{
	alert('Inserire un nome ordine e la spesa totale per proseguire..');
	}
}

function modifica_stato_ordine(id,stato) {
    var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler_check_componente);
    myRequest.open("GET","script/mod_stato_ordine.php?id="+escape(id)+"&stato="+escape(stato)+"&r="+escape(r));
    myRequest.send(null);
}

function getCookie(NameOfCookie)
{ if (document.cookie.length > 0)
{ begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}


function setCookie(NameOfCookie, value, expiredays)
{ var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}



function delCookie (NameOfCookie)
{ if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

}


var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */


function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }


function md5_vm_test()
{
  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);

}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Calculate the HMAC-MD5, of a key and some data
 */
function core_hmac_md5(key, data)
{
  var bkey = str2binl(key);
  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  return core_md5(opad.concat(hash), 512 + 128);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
    str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  return str;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of little-endian words to a base-64 string
 */
function binl2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}

