
// ========================================================================
//  IMCPL js
// ========================================================================
window.onload = function(){init()};
//window.onload = function(){}
function getABLFrame(){
 for (var i = 0; i < window.top.frames.length; i++) {
        var curFrame = window.top.frames[i];
		var nameCheck = (curFrame.name);
        if(nameCheck == "ablframe"){
			var ABLFrame = curFrame;
		}
 }

 return window;
}

function checkEnter(e,bibno){
	var characterCode;  
	 if(e && e.which){
	 characterCode = e.which;
	 }
	 else{
//	 e = event;
	 characterCode = e.keyCode;
	 }
	 if(characterCode == 13){ 
	 
	 loginto(bibno);
	 return false ;
	 }
	 else{
	 return true ;
	 }

}

function init(){
	
        var curFrame = getABLFrame();
	var mbox = curFrame.document.getElementsByTagName("body")[0];
	var maskDiv = curFrame.document.createElement('div');
	maskDiv.id = "mask";
	maskDiv.onclick = function(){hideModal();}
	mbox.appendChild(maskDiv);

	var winDiv = curFrame.document.createElement('div');
	winDiv.id = "window";
	mbox.appendChild(winDiv);

	var closeImg = curFrame.document.createElement('div');
	closeImg.id = "closeModalButton";
	closeImg.innerHTML = "<img src='http://www.imcpl.org//borrowerServices/img/x.png' onclick='hideModal()'>";
	closeImg.style.display = "none";
	mbox.appendChild(closeImg);
}


function ajacks(url,parameters) {
        curFrame = getABLFrame();
	
	document.getElementById("window").innerHTML = "<div style='text-align:center;width:100%;' ><img src='/borrowerServices/img/loader.gif' style='margin-top:70px;margin-left:auto;margin-right:auto'/></div>";
	http_request = false;
	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.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);
  http_request.onreadystatechange = writeContents;
}

function writeContents(){
  curFrame = getABLFrame();
  var target = document.getElementById("window");
  target.innerHTML = "<div style='text-align:center;width:100%;' ><img src='borrowerServices/img/loader.gif' style='margin-top:70px;margin-left:auto;margin-right:auto'/></div>";
  //var closeButton = "<a style=\"cursor:pointer\" onclick=\"window.top.hideModal()\" ><img  style=\"float:right;\" src='borrowerServices/img/x.png'/></a>";	
   var closeButton = "";
   if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		result = closeButton + http_request.responseText;	
		target.innerHTML = result;
		try {
			setCaretPosition(curFrame.document.getElementById('barcode'),5);
		} catch (error) {
		}
	 } else {
		target.innerHTML = closeButton + "Sorry, your request cannot be completed at this time.<br/>Request Status = " + http_request.readyState;

	 }
  }
}

function showModal(windowStyle){
	
	curFrame = getABLFrame();

	var maskHeight = windowSize("height");
	var maskWidth = windowSize("width");
	
	var mask = curFrame.document.getElementById("mask");
	
	
	mask.style.width = "700px";
	mask.style.height = "100%";
	
	mask.style.top = 0;
	mask.style.left = 0;
	//mask.style.position = "absolute";
	mask.style.display = "block";
	
	var closeButton = document.getElementById("closeModalButton");
	closeButton.style.position = "absolute";
	closeButton.style.top = 50;
	closeButton.style.left = 450;
	
	closeButton.style.display = "block";
	closeButton.style.zIndex = 10001;
	

	var box = curFrame.document.getElementById('window');
			
	if(windowStyle == "popup"){
		box.style.height = "400px";
		box.style.width = "640px";
		box.style.overflow = "auto";
		box.style.backgroundColor = "#FFFFFF";
		box.style.borderColor = "#AAAAAA";
		box.style.color = "#333333";
		box.style.left = "192px";
	}
	else
	{
		box.style.height = "200px";
		box.style.width = "440px";
		box.style.overflow = "hidden";
		box.style.backgroundColor = "#333333";
		box.style.borderColor = "#AAAAAA";
		box.style.color = "#FFFFFF";
		box.style.left = "130px";		
	}
	
	
	box.style.top = "80px";
	box.style.display = "block";
	

}

function loginto(bibno)
{
	curFrame = getABLFrame();
	var barcode = curFrame.document.getElementById("barcode").value;
	var pin = curFrame.document.getElementById("pin").value;
	var requestPage = "/borrowerServices/placeRequest.php";
	var args="mode=2&p="+pin+"&b="+barcode+"&bibno="+bibno;
	ajacks(requestPage, args);	
}

function logout(bibno){
	var requestPage = "/borrowerServices/placeRequest.php";
	var args="mode=3&bibno="+bibno;
	ajacks(requestPage, args);
}

function request(args){
	var bibno = args.split("|");
	showModal("req");
	var requestPage = "/borrowerServices/placeRequest.php";
	var data = "mode=1&bibno="+bibno[2];
	ajacks(requestPage, data);
}

function popup(url){
	var args =  url.split("?");
	showModal("popup");
	ajacks(args[0], args[1]); 
}

function hideModal(){
	curFrame = getABLFrame();
	curFrame.document.getElementById("mask").style.display = "none";
	curFrame.document.getElementById("window").style.display = "none";
	curFrame.document.getElementById("closeModalButton").style.display = "none";
}

function windowSize(d) {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}
	if(d=="width")
	return myWidth;
	else 
	return myHeight

}


function placeRequest(bibno, barcode){
	curFrame = getABLFrame();
	var selected = curFrame.document.getElementById("pickuplocation").selectedIndex;
	var pickup = curFrame.document.getElementById("pickuplocation").options[selected].value;
	var requestPage = "/borrowerServices/placeRequest.php";
	var args="mode=4&bibno="+bibno+"&barcode="+barcode+"&pickup="+pickup;
	ajacks(requestPage, args);
	
}


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();
	}
}

