﻿// File JScript

function image_swap(ImageID, Image) 
{
	document.getElementById(ImageID).src = Image;
}


function text_swap(ObjID, ClsName) 
{
    //alert (document.getElementById(ObjID).className);
	document.getElementById(ObjID).className = ClsName;
}

function gotoUrl(Url)
{
    window.location.href = Url;
}


function ValidateInput(ObjIDS, RequiredAlert, RegExIDS, RegExs, RegExAlerts)
{
    // required fileds validation ********************************************
    var ObjID = ObjIDS.split(";");
    var DivID = "";
    for (i=0;i<=ObjID.length-1;i++)
    {
        if (document.getElementById(ObjID[i]).value == "")
        {
            DivID = "DV_" + ObjID[i].substring(3);
            //alert(DivID);
            if (document.getElementById(DivID)) 
                {Msg = RequiredAlert.replace("{0}",document.getElementById(DivID).innerHTML)}
            else
                {Msg = RequiredAlert.replace("{0}. ","")}
            alert(Msg);
            document.getElementById(ObjID[i]).focus();
            return false;
        }
    }
    
    
    // regex validation
    var RegExID = RegExIDS.split(";");
    var RegEx = RegExs.split(";");
    var RegExAlert = RegExAlerts.split(";");
    for (i=0;i<=RegExID.length-1;i++)
    {
        var re = new RegExp(RegEx[i]);
        alert (RegEx[i]);
        if (document.getElementById(RegExID[i]).value.match(re))
        {
            // ok
        } else {
            alert(RegExAlert[i]);
            document.getElementById(RegExID[i]).focus();
            return false;
        }
    }
}



function isRequired(ObjIDS, alertMsg)
{
    //alert (ObjIDS);
    var ObjID = ObjIDS.split(";");
    var DivID = "";
    for (i=0;i<=ObjID.length-1;i++)
    {
        if (document.getElementById(ObjID[i]).value == "")
        {
            DivID = "DV_" + ObjID[i].substring(3);
            //alert(DivID);
            if (document.getElementById(DivID)) 
                {Msg = alertMsg.replace("{0}",document.getElementById(DivID).innerHTML)}
            else
                {Msg = alertMsg.replace("{0}. ","")}
            alert(Msg);
            document.getElementById(ObjID[i]).focus();
            return false;
        }
    }
}

function showAlert(alertMsg)
{
    alert(alertMsg);
}

function StartMetaSearch(BaseUrl, Web, SearchMode, txtId, dropField, skipGrpQry){
	var el = document.getElementsByTagName('input');
	var Qry = '&Qry=';
	var Grp = '&Grp=';
	var id = '';
	var txtSearch = '';
	var dosrc = false;
	var dropFileds = ''
	if (skipGrpQry != null) {dosrc=true}
	for (i=0; i<el.length; i++){
	    if (el[i].type=='checkbox'){
		    if (el[i].id.indexOf('chkGrp_') != -1) {
			    if (el[i].checked) {Grp = Grp  + el[i].id.replace('chkGrp_', '')  + ',';};
	        }
		    if (el[i].id.indexOf('chkQry_') != -1) {
			    if (el[i].checked){Qry  = Qry + el[i].id.replace('chkQry_', '')  + ',';};
	        }
	    }
	}
	txtIDs = txtId.split(',');
	if (dropField != null) {dropFields = dropField.split('|');}
	for (i=0; i<txtIDs.length; i++){
	    ctl = document.getElementById(txtIDs[i])
	    //alert(txtIDs[i]);
	    if (ctl.value != '') {
		    if (SearchMode=='SIMPLE') {
		        txtSearch = '&txtSimple=' + ctl.value
	        } else {
	            if (dropField != null)  {
		            txtSearch += '&txtAdvanced' + (i+1) +'=' + ctl.value + '&dropField=' + dropFields[i]
		        } else {
		            txtSearch += '&txtAdvanced' + (i+1) +'=' + ctl.value
		        }
	        }
	    }
	}
	if (((Qry != '&Qry=') || (Grp != '&Grp=') || (dosrc)) && (txtSearch != '')) {
		    var w = window.open('','METASRC');
		    w.location = 'http://' + BaseUrl + '/DCsearch/MetaSearch.aspx?Web=' + Web + '&SEARCHMODE=' + SearchMode + txtSearch + Grp + Qry;
		    w.focus();
	} else {
	       if (txtSearch != '') {
		       alert('Nessun archivio selezionato');    
           } else {
                alert('Immettere il testo da cercare');
           }
     }
}


function StartOpacSearch(Version, Mode, CodWeb, CodBib, ServerUrl, ANYId, TITId, AUId, SOId)
{
	//CodWeb='DEMO';
	//ServerUrl = 'NT40';
	var CB = "";

	switch (Mode)
	{
	    case 'S':
	        ctlANY = document.getElementById(ANYId + CodWeb + CodBib)
	        if (ctlANY.value !='') {
		        var w = window.open('','OPAC');
		        if (Version == "5") {
		        	if (CodBib != "    ") {
	                    CB = "&OPAC=" + CodBib
	                }
		            if (ServerUrl.indexOf('/')>0) {
		                   w.location = 'http://' + ServerUrl + '/opac.aspx?web=' + CodWeb + CB + '&SRC=SSMP&TXT=' + ctlANY.value 
		            } else {
		                   w.location = 'http://' + ServerUrl + '/bw5net/opac.aspx?web=' + CodWeb + CB + '&SRC=SSMP&TXT=' + ctlANY.value
		            }
		        } else {
		            if (CodBib != "    ") {
	                    CB = "&CodBib=" + CodBib
	                }
		            w.location = 'http://' + ServerUrl + '/bwnet/default.asp?page=ListREC&ListMode=IDX&QRM=META&opac=' + CodWeb + CB + '&ANY=' + ctlANY.value
		        }
		        w.focus();
	        } else {
		        alert('Immettere il testo da cercare');
	        }
	    break;
	    
	    case 'A':
	        ctlTIT = document.getElementById(TITId + CodWeb + CodBib)
	        ctlAU = document.getElementById(AUId + CodWeb + CodBib)
	        ctlSO = document.getElementById(SOId + CodWeb + CodBib)
	        if (ctlTIT.value != '' || ctlAU.value != '' || ctlSO.value != '' ) 
	        {
		        var w = window.open('','OPAC');
		        if (Version == "5") {
		            if (CodBib != "    ") {
	                    CB = "&OPAC=" + CodBib
	                }
		            if (ServerUrl.indexOf('/')>0) {
		                w.location = 'http://' + ServerUrl + '/opac.aspx?web=' + CodWeb + CB + '&SRC=SBAS&TIT=' + ctlTIT.value + '&AU=' + ctlAU.value + '&SO=' + ctlSO.value
		            } else {
		                w.location = 'http://' + ServerUrl + '/bw5net/opac.aspx?web=' + CodWeb + CB + '&SRC=SBAS&TIT=' + ctlTIT.value + '&AU=' + ctlAU.value + '&SO=' + ctlSO.value
		            }
		            } else {
		                if (CodBib != "    ") {
	                        CB = "&CodBib=" + CodBib
	                    }
		                w.location = 'http://' + ServerUrl + '/bwnet/default.asp?page=ListREC&ListMode=IDX&QRM=META&opac=' + CodWeb + CB + '&TIT=' + ctlTIT.value + '&AU=' + ctlAU.value + '&SO=' + ctlSO.value
		            }
		        w.focus();
		    } else {
		        alert('Immettere il testo da cercare');
	        }
	    
	    break;
	}
}


function fnTrapKD(btnId, e){
    btn = document.getElementById(btnId);
	var key =  window.event ? window.event.keyCode : e.which;
	if (key == 13)
	{ 
		if (navigator.userAgent.indexOf("Firefox") == -1) {
			e.returnValue=false;
			e.cancel = true;
			btn.click();
		} else {
			correctSubmitHandler(e);
			btn.click();
		}
	} 
}

function correctSubmitHandler(e)
{
	if (e && e.preventDefault)
		e.preventDefault();
	return false;
}


function ResizeShowImage() {
	if (document.getElementById("imgMain").height >  window.screen.width){
		y = window.screen.height - 60;
	} else {
		y = document.getElementById("imgMain").height;
	}
	if (window.document.getElementById("imgMain").width > window.screen.width){
		x = window.screen.width  - 60;
	} else {
		x = document.getElementById("imgMain").width;
	}
	resizeTo(x+50,y+240);
	return;
}



function PageQuery(q) {
    if(q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if(q) {
    for(var i=0; i < this.q.split("&").length; i++) {
    this.keyValuePairs[i] = this.q.split("&")[i];
    }
}

this.getKeyValuePairs = function() { return this.keyValuePairs; }

this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
var keyval = this.keyValuePairs[j].split("=")[0];
if(keyval.toLowerCase() == s.toLowerCase())
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
var page = new PageQuery(window.location.search); 
return unescape(page.getValue(key)); 
}


function DoToggleLnk(that, ctlID, visTxt, HideTxt){
    var ctl = document.getElementById(ctlID)
    if (ctl.className.indexOf('HIDDEN') > 1) {
        ctl.className = ctl.className.replace(" HIDDEN", "");
        that.innerHTML = HideTxt;
    } else {
        ctl.className = ctl.className + " HIDDEN"
        that.innerHTML = visTxt
    }
    return false;
}

