var is_ie = (document.all)
var opacWin;

function openOpac(pUrl) {
	opacWin = window.open(pUrl,"Opac","HEIGHT=600, WIDTH=800");
	opacWin.focus();
}
function frmOpacwebSub() {
	if (document.frmOpacweb.search.value!="") openOpac("/opac.php?search=" + document.frmOpacweb.search.value)
	//return false;
}
function closeOpacWin() {
	opacWin.close()
}
function updateNSP(pPort,pUser) {
	var zURL = "http://LOCALHOST:"+pPort+"/GETLOGIN?AB=" + pUser
	var loaded = false
	var xml = new ActiveXObject("Msxml.DOMDocument");
	xml.async = false
	try {
		xml.setProperty("ServerHTTPRequest", true)
		xml.load(zURL);
		loaded = true
		if (xml.parseError.errorCode!=0) {			
		//	alert("Erreur chargement config " + zURL + " : " + xml.parseError.errorCode + " - " + xml.parseError.reason)
		}
	} catch (e) {
	//	alert(zURL + " : " + e.number + " : " + e.description)
	}		
}
	
function frameSize() {
	var iHeaderFooterHeight = document.getElementById("outerColumnContainer").offsetTop + document.getElementById("opacwebFrame").offsetTop + document.getElementById("footer").offsetHeight 
	var newHeight = document.body.clientHeight-iHeaderFooterHeight 
	if (!is_ie) newHeight -=17;
	return Math.max(newHeight,300);
}

function ShowAccount(LogCode,FullName,DataBase) {
	var opacwebDLL = opacwebURL + "/bin/opacweb.dll/"
	// Make it the same height and width of calling window
	var win = parent
	if (NSP) win = self
	var obj = getWindowDetails(win)
	
	if (is_ie) {
		popupIE(opacwebDLL,self,'AccountAb?XFile=Account&LoginCAb='+LogCode+'&ConsultCAb='+LogCode+'&DataBaseNames='+DataBase,'Consultation du compte',obj.height,obj.width,'r','F',obj.top,obj.left);
	} else {
		popupFF(opacwebDLL,self,'AccountAb?XFile=Account&LoginCAb='+LogCode+'&ConsultCAb='+LogCode+'&DataBaseNames='+DataBase,'Consultation du compte',obj.height,obj.width,'r','F',obj.top,obj.left);
	}
}

function goLogin() {
	var opacwebDLL = opacwebURL + "/bin/opacweb.dll/"
	var zUserId = 'Identification abonn&eacute;'
	var zLink = 'GlobalInfo?XFile=LoginAb&Init=T&Action=ShowAccount&ChooseBase=T'
	if (is_ie) {
		popupIE(opacwebDLL,self,zLink,encodeUtf8(zUserId),'310','500','r','T');
	} else {
		popupFF(opacwebDLL,self,zLink,encodeUtf8(zUserId),'310','500','r','T');
	}
}

function getWindowDetails(pWindow) {
	var win = pWindow
	var obj = new Object()
	
	obj.left	= 0
	obj.top		= 0
	obj.height	= 500
	obj.width	= 750

//	if (document.all) {
	if (is_ie) {
		try {obj.left	= win.screenLeft} catch (e) {}
		try {obj.top	= win.screenTop} catch (e) {}
		try {obj.height	= win.document.body.clientHeight} catch (e) {}
		try {obj.width	= win.document.body.clientWidth} catch (e) {}
	} else {
		try {obj.left	= win.screenX+(win.outerWidth-win.innerWidth)-4;} catch (e) {}
		try {obj.top	= win.screenY+(win.outerHeight-24-win.innerHeight)+2;} catch (e) {}
		try {obj.height	= win.innerHeight-0} catch (e) {}
		try {obj.width	= win.innerWidth-0} catch (e) {}
	}
	return obj
}
function encodeUtf8(pText) {
	var zRes   = ''	
	var zInput = pText

	for (var i=0; i < zInput.length;i++) {
		var zChar = zInput.substr(i,1)
		var c = zChar.charCodeAt(0)
		if (c <= 0x7F) {
			zRes += zChar
		} else {
			if (c <= 0x7FF) {
				// Had used escape(String.fromCharCode()), but escape wouldn't translate ABC to % equiv
				zRes += translateToHex(0xC0 | (c >> 6))
				zRes += translateToHex(0x80 | (c & 0x3F))
			} else {
				zRes += translateToHex(0xE0 | (c >> 12))
				zRes += translateToHex(0x80 | ((c >> 6) & 0x3F))
				zRes += translateToHex(0x80 | (c & 0x3F))
			}
		}			
	}
	return zRes;
}
function translateToHex(pInt) {
	var nos = new Array(pInt >> 4,pInt & 15)	// Left and right bits
	for (var i=0;i<nos.length;i++) if (nos[i] >= 10) nos[i] = String.fromCharCode(nos[i]+55);
	return "%" + nos[0] + nos[1]
}



function encode64(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function popupFF(path,receptorName,it,t,h,w,r,i,top,left)
{	
	winTop = (top) ? top : (screen.availHeight - h)/2;
	winLeft = (left) ? left : (screen.availWidth - w)/2;

	if (r)
		/* reziable window */
		var newWind=window.open(path+it , null, "scrollbars,modal=1,width="+w+",height="+h+",ScreenY="+winTop+",ScreenX="+winLeft+",Resizable=Yes");
	else
		/* no reziable window */
		var newWind=window.open(path+it , null, "scrollbars,modal=1,width="+w+",height="+h+",ScreenY="+winTop+",ScreenX="+winLeft+",Center=Yes");
	// +'&receptor='+receptorName
	// pour Mozilla, NN ...
	// if (newWind.opener == null) newWind.opener = self;  	
}
function popupIE(p,o,u,t,h,w,r,i,top,left){
	 var param = new Object()
	 param.Url = u
	 param.Opener = o
	 // var param = new Param(o,u);
	 zTop  = (top) ? ' dialogTop:'+top+'px;' : ''
	 zLeft = (left) ? ' dialogLeft:'+left+'px;' : ''
	 
	 var zPrefix = (String(u).substr(0,4)=='Free') ? 'Free' : ''

	 if (r)
	 	/* reziable window */
	 	var returnValue=window.showModalDialog(p + zPrefix+'GlobalInfo?XFile=Modal&WinTitle='+t+'&INIT='+i,param,"dialogHeight: " + h + "px;dialogWidth: " + w + "px;scroll: No;"+zTop+zLeft+" center: Yes; help: No; resizable: Yes; status: No;");
	 else
	 	/* no reziable window */
	 	var returnValue=window.showModalDialog(p + zPrefix+'GlobalInfo?XFile=Modal&WinTitle='+t+'&INIT='+i,param,"dialogHeight: " + h + "px;dialogWidth: " + w + "px;scroll: No;"+zTop+zLeft+" center: Yes; help: No; resizable: No; status: No;");
	if (returnValue) eval(returnValue)
}


