// JavaScript Document
var http_request = false;
var divno;

function makePOSTRequest(url, parameters, bibno) {
	//showLoader();
	http_request = false;
	divno = "myspan"+bibno;
	showLoader();
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents(){
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		//alert(http_request.responseText);
		//result = http_request.responseText;
		
		//specify width if ie6 or less
		result = ie_six_width(http_request.responseText);
		document.getElementById(divno).innerHTML = result;
		try {
			setCaretPosition(document.getElementById('bbarcode'),5);
		} catch (error) {
		}
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}

function startRequest(obj) {
  var poststr = "bbarcode=" + encodeURI( document.getElementById("bbarcode").value) +
		"&pin=" + encodeURI( document.getElementById("pin").value) +
		"&bibno=" + encodeURI( document.getElementById("bibno").value );
  makePOSTRequest('request.pl', poststr, document.getElementById("bibno").value);
}

function finishRequest(obj) {
  var poststr = "loc=" + encodeURI( document.getElementById("loc").value) +
		"&placeaction=" + encodeURI( document.getElementById("placeaction").value) +
		"&reqtitle=" + encodeURI( document.getElementById("reqtitle").value) +
		"&reqauthor=" + encodeURI( document.getElementById("reqauthor").value) +
		"&noticeby=" + encodeURI( document.getElementById("noticeby").value) +		
		"&bibno=" + encodeURI( document.getElementById("bibno").value );
  makePOSTRequest('request.pl', poststr, document.getElementById("bibno").value);
}

function makePreRequest(bibno){
  hideWins("wrap");
  var divToDisp = "wrap"+bibno;
  document.getElementById(divToDisp).style.display = "inline";	
  var poststr = "bibno=" + encodeURI(bibno);
  makePOSTRequest('request.pl', poststr, bibno);
}

function cancelReq(obj){
  var poststr = "bibno=" + encodeURI( document.getElementById("bibno").value) +
		"&logout=" + encodeURI( document.getElementById("logout").value);
  makePOSTRequest('request.pl', poststr, document.getElementById("bibno").value);
}

function closeScreen(bibno){
  var divToClose = "wrap"+bibno;	
  document.getElementById(divToClose).style.display = "none";
}
function hideWins(divName){
    var DivsToHide = getElementsByClassName(document, "div", divName);
    for(var i=0;i<=DivsToHide.length-1;i++)
    {
	DivsToHide[i].style.display = "none";
    }
    //alert(DivsToHide);
}
function getElementsByClassName(oElm, strTagName, oClassNames){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
	for(var i=0; i<oClassNames.length; i++){
	    arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
	}
    }
    else{
	arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
	oElement = arrElements[j];
	bMatchesAll = true;
	for(var k=0; k<arrRegExpClassNames.length; k++){
	    if(!arrRegExpClassNames[k].test(oElement.className)){
			bMatchesAll = false;
			break;
	    }
	}
	if(bMatchesAll){
		arrReturnElements.push(oElement);
	}
    }
    return (arrReturnElements)
}
function showLoader()
{
   loaderText = "<div style='width: 100%; text-align: center; min-height: 170px;'><h3>Loading...</h3><img src='skin.ashx?u=img/req_loading.gif' align='center' style='margin-top:30px'></div>";
   document.getElementById(divno).innerHTML = loaderText; 
}


function ie_six_width(requestCopy)
	{
		var BrowserDetect = {
			init: function () {
				this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
				this.version = this.searchVersion(navigator.userAgent)
					|| this.searchVersion(navigator.appVersion)
					|| "an unknown version";
				this.OS = this.searchString(this.dataOS) || "an unknown OS";
			},
			searchString: function (data) {
				for (var i=0;i<data.length;i++)	{
					var dataString = data[i].string;
					var dataProp = data[i].prop;
					this.versionSearchString = data[i].versionSearch || data[i].identity;
					if (dataString) {
						if (dataString.indexOf(data[i].subString) != -1)
							return data[i].identity;
					}
					else if (dataProp)
						return data[i].identity;
				}
			},
			searchVersion: function (dataString) {
				var index = dataString.indexOf(this.versionSearchString);
				if (index == -1) return;
				return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
			},
			dataBrowser: [
				{ 	string: navigator.userAgent,
					subString: "OmniWeb",
					versionSearch: "OmniWeb/",
					identity: "OmniWeb"
				},
				{
					string: navigator.vendor,
					subString: "Apple",
					identity: "Safari"
				},
				{
					prop: window.opera,
					identity: "Opera"
				},
				{
					string: navigator.vendor,
					subString: "iCab",
					identity: "iCab"
				},
				{
					string: navigator.vendor,
					subString: "KDE",
					identity: "Konqueror"
				},
				{
					string: navigator.userAgent,
					subString: "Firefox",
					identity: "Firefox"
				},
				{
					string: navigator.vendor,
					subString: "Camino",
					identity: "Camino"
				},
				{		// for newer Netscapes (6+)
					string: navigator.userAgent,
					subString: "Netscape",
					identity: "Netscape"
				},
				{
					string: navigator.userAgent,
					subString: "MSIE",
					identity: "Explorer",
					versionSearch: "MSIE"
				},
				{
					string: navigator.userAgent,
					subString: "Gecko",
					identity: "Mozilla",
					versionSearch: "rv"
				},
				{ 		// for older Netscapes (4-)
					string: navigator.userAgent,
					subString: "Mozilla",
					identity: "Netscape",
					versionSearch: "Mozilla"
				}
			],
			dataOS : [
				{
					string: navigator.platform,
					subString: "Win",
					identity: "Windows"
				},
				{
					string: navigator.platform,
					subString: "Mac",
					identity: "Mac"
				},
				{
					string: navigator.platform,
					subString: "Linux",
					identity: "Linux"
				}
			]
		
		};
	BrowserDetect.init();
	if((BrowserDetect.browser == "Explorer") && (BrowserDetect.version <= 6))
		return "<!--Forcing width for IE6--><table width=\"400px\"><tr><td>"+requestCopy+"</td></tr></table>";
	else			
		return requestCopy;
	
}
function setCaretPosition(ctrl, pos) {
	if(ctrl.setSelectionRange) {
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	} else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}