// lastmod 25 marzo 2011 (fix charset)
// lastmod 16 ottobre 2008 (fix per IE)
// lastmod 14 ottobre 2008
// lastmod  9 ottobre 2008
// lastmod 11 settembre 2008
// lastmod 9 settembre 2008
// lastmod 8 settembre 2008
// lastmod 5 settembre 2008
// lastmod 15 febbraio 2008
// lastmod 14 febbraio 2008
// lastmod 9 febbraio 2008
// javascript di sebastiano.midolo@comune.torino.it

global_nascondi_maschera = 'RICERCA AVANZATA NEL CATALOGO (chiudi)';
global_mostra_maschera = 'RICERCA AVANZATA NEL CATALOGO';

function invia_richiesta_librinlinea() {
    var campi = ['AUTORE','TITOLO','SOGGETTO','NUMERO_STANDARD','CODICE_BIBLIO','TIPO_NUMERO_STANDARD','GENERE','LINGUA'],
	form = ottieni_form_per_invio(),
	inputform = document.getElementById('maschera_ricerca');

    for (var n=0; n < campi.length; n++) {
	form[campi[n]].value=inputform[campi[n]].value;
    }
    ordina_per(form);
    imposta_genere_e_tipo_documento(inputform,form,inputform.TIPO_RICERCA.value);
    document.getElementById('user_message').innerHTML='Richiesta inviata';
    form.submit();
}

function ottieni_form_per_invio() {
    var form = document.getElementById('form_per_invio_richiesta');
    if(form) {return form;}

    var div_per_form = document.getElementById('div_per_form');

    form = document.createElement('form');
    form.setAttribute('id','form_per_invio_richiesta');
    // form.setAttribute('action','http://biblio.comune.torino.it');
    form.setAttribute('action','http://sbnweb.csi.it:8092/BASIS/OPACX/UDMOPAC/esimplex/SDF');
	form.setAttribute('accept-charset', 'ISO-8859-1');
    form.setAttribute('method','get');
    form.setAttribute('target','_blank');
    standard_fields(form);
    div_per_form.appendChild(form);
    return form;
}

function standard_fields(form) {
    var input,
	vars = {
	    'CODICE_BIBLIO_O': 'uguale',
	    'CODICE_BIBLIO': '',
	    'BASE_DATI_O': 'uguale',
	    'BASE_DATI': '',
	    'AUTORE_O': 'AND',
	    'AUTORE': '',
	    'TITOLO_O': 'AND',
	    'TITOLO': '',
	    'SOGGETTO_O': 'AND',
	    'SOGGETTO': '',
	    'ABSTRACT2_O': 'AND',
	    'ABSTRACT2': '',
	    'COLLANA_O': 'AND',
	    'COLLANA': '',
	    'DESCRIZIONE_CLASSIFICAZIONE_O': 'AND',
	    'DESCRIZIONE_CLASSIFICAZIONE': '',
	    'TIPO_DOCUMENTO_O': 'uguale',
	    'TIPO_DOCUMENTO': '',
	    'MATERIALE_O': 'uguale',
	    'MATERIALE': '',
	    'GENERE_O': 'uguale',
	    'GENERE': 'video',
	    'DATA_PUBBLICAZIONE_O': 'uguale',
	    'DATA_PUBBLICAZIONE': '',
	    'LUOGO_EDIZIONE_O': 'AND',
	    'LUOGO_EDIZIONE': '',
	    'EDITORE_O': 'AND',
	    'EDITORE': '',
	    'LINGUA_O': 'AND',
	    'LINGUA': '',
	    'PAESE_O': 'AND',
	    'PAESE': '',
	    'NOTE_ISBD_O': 'AND',
	    'NOTE_ISBD': '',
	    'BID_O': 'uguale',
	    'BID': '',
	    'VID_O': 'uguale',
	    'VID': '',
	    'IMPRONTA_O': 'AND',
	    'IMPRONTA': '',
	    'POSSESSORE_O': 'AND',
	    'POSSESSORE': '',
	    'MARCHE_O': 'AND',
	    'MARCHE': '',
	    'NOTE_ESEMP_O': 'AND',
	    'NOTE_ESEMP': '',
	    'DATA_INSERIMENTO_O': 'uguale',
	    'DATA_INSERIMENTO': '',
	    'FORM_C': 'AND',
	    'FORMFL_OB': '',
	    'FORM_SO': 'Ascend',
	    'NUMERO_STANDARD': '',
	    'TIPO_NUMERO_STANDARD': ''
    };

    // http://www.thunderguy.com/semicolon/2005/05/23/setting-the-name-attribute-in-internet-explorer/
    if (Prototype.Browser.IE) {
	for (var v in vars) {
	    input = document.createElement('<input name="'+v+'">');
	    form.appendChild(input);
	    input.setAttribute('value',vars[v]);
	}
    } else {
	for (var v in vars) {
	    input = document.createElement('input');
	    form.appendChild(input);
	    input.setAttribute('name',v);
	    input.setAttribute('value',vars[v]);
	}
    }
    // alert(form.innerHTML);
}

function scelta_tipo_ricerca() {
    var form = ottieni_form_per_invio(),
	inputform = document.getElementById('maschera_ricerca');
    imposta_genere_e_tipo_documento(inputform,form,inputform.TIPO_RICERCA.value);
}

function imposta_genere_e_tipo_documento(inputform,form,tipo_ricerca) {
    for_msg = document.getElementById('user_message');

    for_msg.innerHTML='';
    document.getElementById('numero_standard_id').firstChild.nodeValue='ISBN';

    if (inputform.NUMERO_STANDARD.value!='') {
	if (tipo_ricerca == 'PERIODICO') {
	    inputform.TIPO_NUMERO_STANDARD.value=form.TIPO_NUMERO_STANDARD.value='ISSN';
	} else {
	    inputform.TIPO_NUMERO_STANDARD.value=form.TIPO_NUMERO_STANDARD.value='ISBN';
	}
    } else {
	inputform.TIPO_NUMERO_STANDARD.value=form.TIPO_NUMERO_STANDARD.value='';
    }

    switch (tipo_ricerca)
    {
    case 'PERIODICO':
	form.TIPO_DOCUMENTO.value=tipo_ricerca;
	form.GENERE.value='';
	inputform.CODICE_BIBLIO.value=form.CODICE_BIBLIO.value='TO0 01';
	for_msg.innerHTML='solo per la Biblioteca Civica Centrale';
	// form.NUMERO_STANDARD.value=form.NUMERO_STANDARD.value.substr(0,8);
	document.getElementById('numero_standard_id').firstChild.nodeValue='ISSN';
	break;
    case 'musica a stampa':
	form.GENERE.value=tipo_ricerca;
	form.TIPO_DOCUMENTO.value='';
	inputform.CODICE_BIBLIO.value=form.CODICE_BIBLIO.value='TO0 75'
	for_msg.innerHTML='solo per la Biblioteca Musicale';
	break;
    case 'video':
	form.GENERE.value=tipo_ricerca;
	form.TIPO_DOCUMENTO.value='';
	break;
    case 'letteratura per ragazzi':
	form.GENERE.value=tipo_ricerca;
	form.TIPO_DOCUMENTO.value='';
	break;
    default:
	form.TIPO_DOCUMENTO.value='';
	form.GENERE.value='';
    }
    
    // show_debug_message("Scelta tipo ricerca: " + tipo_ricerca + " - genere: " + form.GENERE.value + "; tipo_documento: " + form.TIPO_DOCUMENTO.value);
}

function verifica_isbn(form,isbn,for_msg) {
    var newval='';
    if(isbn=='') {
	for_msg.innerHTML='';
    } else {
	newval = isbn.replace(/-| /g,'');

	var morethen,lessthen;
	if (form.TIPO_RICERCA.value=='PERIODICO') {morethen=4; lessthen=8;} else {morethen=5; lessthen=10;}

	if(newval.length>morethen && newval.length<lessthen && newval.charAt(newval.length-1)!='*') newval+='*';
	if (newval!=isbn) {
	    form.NUMERO_STANDARD.value = newval;
	    // for_msg.innerHTML=' (il numero va inserito senza spazi o trattini)';
	    for_msg.innerHTML='';
	} else {
	    for_msg.innerHTML='';
	}
    }
}

function ordina_per(form) {
    var ord = document.getElementById('ordina_per_autore');
    if (ord.checked) {
	form.FORM_SO.value='Ascend';
	form.FORMFL_OB.value='Stringa';
    } else {
	form.FORM_SO.value='Descend';
	form.FORMFL_OB.value='Anno pubblicazione';
    }
}

function inizializza_form() {
    var form = document.getElementById('maschera_ricerca');
    form[0].focus();
}

function x_suggerimento_per(oggetto) {
    var form = document.getElementById('maschera_ricerca'),
	for_msg = document.getElementById('user_message');
    
    switch(oggetto.name) {
    case 'NUMERO_STANDARD':
	if (oggetto.value!='') verifica_isbn(form,oggetto.value,for_msg);
	break;
    }
}

function form_toggle(oggetto) {
    var form = document.getElementById('contenitore_maschera');

    if (form.style.display == 'none') {
	form.style.display = '';
	oggetto.firstChild.nodeValue = global_nascondi_maschera;
	inizializza_form();
    } else {
	form.style.display = 'none';
	oggetto.firstChild.nodeValue = global_mostra_maschera;
    }
}

function ripristina_stato_iniziale() {
    document.getElementById('numero_standard_id').firstChild.nodeValue='ISBN';
    inizializza_form();
}

function nascondi_maschera() {
    // var oggetto = document.getElementById('controller_maschera');
    form_toggle(document.getElementById('controller_maschera'));
}

function invio() {
    document.getElementById("maschera_ricerca").action = "http://sbnweb.csi.it:8092/CIC/BASIS/OPACX/UDMOPAC/esimplex/SDF";
	document.getElementById("maschera_ricerca").target = "_blank";
    document.getElementById("maschera_ricerca").submit();
}

function show_debug_message(msg) {
    document.getElementById('debug_window').innerHTML=msg;
}

