/*
pdfBinder

Javascript library for adding pages to a session variable and subsequently
creating a PDF containing all of those pages.

The web.config file in the pdfBinder folder controls things like the maximum number
of pages.
*/

//	These should be overridden by the page that includes this JS file
//	absoulte path to the folder pdfBinder resides in
var PDFBINDER_Path = "";
//	the current page ID.
var PDFBINDER_Url = "";
//	the id of the pdfBinderView control on the page
var PDFBINDER_ViewControlId = "";
//	flag indicating whether to hide the add button on the page
var PDFBINDER_HideButtons = "1";

//	the main functionality is implemented in this page
var PDFBINDER_ControlPage = "control.aspx";

//	the PDF creation is implemented in this page
var PDFBINDER_GeneratePage = "generate.aspx";

//	the PDF creation is implemented in this page
var PDFBINDER_ViewPage = "/document-creator-contents.aspx";

//	the page elements we react to and/or manipulate
var PDFBINDER_elemAdd = null;
var PDFBINDER_elemNoAdd = null;
var PDFBINDER_elemRemove = null;
var PDFBINDER_elemView = null;
var PDFBINDER_elemGenerate = null;
var PDFBINDER_elemCount = null;
var PDFBINDER_elemCountHolder = null;

//	current states
var PDFBINDER_PageAlreadyAdded = false;
var PDFBINDER_PageCount = 0;
var PDFBINDER_MaxPageCount = 0;

//	locate all the possible page elements we are interested in
function PDFBINDER_FindPageElements()
{

	PDFBINDER_elemAdd = document.getElementById("PDFBINDER_elemAdd");
	PDFBINDER_elemRemove = document.getElementById("PDFBINDER_elemRemove");
	PDFBINDER_elemNoAdd = document.getElementById("PDFBINDER_elemNoAdd");
	PDFBINDER_elemView = document.getElementById("PDFBINDER_elemView");
	PDFBINDER_elemGenerate = document.getElementById("PDFBINDER_elemGenerate");
	PDFBINDER_elemCount = document.getElementById("PDFBINDER_elemCount");
	PDFBINDER_elemCountHolder = document.getElementById("PDFBINDER_elemCountHolder");
}

function PDFBINDER_UpdatePageElement(element, classNameBase, condition)
{

	if (element != null) {
		if (condition == true) {
			element.className = classNameBase + "Visible";
		} else {
			element.className = classNameBase;
		} //if
	} //if

}

function PDFBINDER_UpdatePageElements()
{


	PDFBINDER_FindPageElements();

	if (PDFBINDER_MaxPageCount > 0) {
//	we've had a valid initial AJAX response
		if (PDFBINDER_HideButtons == 0) {
			PDFBINDER_UpdatePageElement(PDFBINDER_elemAdd, "PDFBINDER_elemAdd", ((PDFBINDER_PageAlreadyAdded == false) && (PDFBINDER_PageCount < PDFBINDER_MaxPageCount)));
			PDFBINDER_UpdatePageElement(PDFBINDER_elemNoAdd, "PDFBINDER_elemNoAdd", (!((PDFBINDER_PageAlreadyAdded == false) && (PDFBINDER_PageCount < PDFBINDER_MaxPageCount))));
			PDFBINDER_UpdatePageElement(PDFBINDER_elemRemove, "PDFBINDER_elemRemove", (PDFBINDER_PageAlreadyAdded == true));
			PDFBINDER_UpdatePageElement(PDFBINDER_elemCountHolder, "PDFBINDER_elemCountHolder", true);
		} //if
		PDFBINDER_UpdatePageElement(PDFBINDER_elemView, "PDFBINDER_elemView", (PDFBINDER_PageCount > 0));
		PDFBINDER_UpdatePageElement(PDFBINDER_elemGenerate, "PDFBINDER_elemGenerate", (PDFBINDER_PageCount > 0));
		
		if (PDFBINDER_elemCount != null) {
			switch (PDFBINDER_PageCount) {
				case 1:
					PDFBINDER_elemCount.innerHTML = PDFBINDER_PageCount + " ITEM";
					break;
				default:
					PDFBINDER_elemCount.innerHTML = PDFBINDER_PageCount + " ITEMS";
					break;
			} //switch
		} //if
	} //if
}

function PDFBINDER_AjaxResponderView(response)
{

	PDFBINDER_FindPageElements();
	if (PDFBINDER_elemView != null) {
		PDFBINDER_elemView.innerHTML = response;
	} //if

}

function PDFBINDER_AjaxResponder(response)
{
//	expected value is a,b,c where
//		a is 0 if this page is NOT already in the binder, 1 if it is
//		b is the number of pages currently in the binder
//		c is the maximum number of pages that can be saved in the binder

	try {
		splits = response.toString().split(",");
		if (splits.length == 3) {
			PDFBINDER_PageAlreadyAdded = (splits[0] == 1);
			PDFBINDER_PageCount = parseInt(splits[1]);
			PDFBINDER_MaxPageCount = parseInt(splits[2]);
		} //if
	} catch (e) { }
	
//	update the page elements to reflect the new state
	PDFBINDER_UpdatePageElements();
}

function PDFBINDER_Initialise()
{

	PDFBINDER_AjaxCall(PDFBINDER_AjaxResponder, "init");
	PDFBINDER_AjaxCall(PDFBINDER_AjaxResponderView, "updateView");
	
}

function PDFBINDER_AddToBinder()
{

	PDFBINDER_AjaxCall(PDFBINDER_AjaxResponder, "add");
	PDFBINDER_AjaxCall(PDFBINDER_AjaxResponderView, "updateView");
}

function PDFBINDER_RemoveFromBinder()
{

	PDFBINDER_AjaxCall(PDFBINDER_AjaxResponder, "remove");
	PDFBINDER_AjaxCall(PDFBINDER_AjaxResponderView, "updateView");
}

function PDFBINDER_Generate()
{

	location.href = PDFBINDER_Path + "/" + PDFBINDER_GeneratePage;
}

function PDFBINDER_ViewContents()
{

	location.href = PDFBINDER_ViewPage;
}

function PDFBINDER_AjaxCall(targetFunction, action)
{
var xmlHttp = null;

	if (xmlHttp == null) {
//	Firefox, Opera 8.0+, Safari  	
		try { xmlHttp=new XMLHttpRequest(); } catch (e) { }
	} //if
	
	if (xmlHttp == null) {
//	Internet Explorer 5
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
	} //if

	if (xmlHttp == null) {
//	Internet Explorer 6+
		try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
	} //if
	
	if (xmlHttp != null) {
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				targetFunction(xmlHttp.responseText);
			} //if
		} //if

		now = new Date().toString();
		
		url = PDFBINDER_Path + "/" + PDFBINDER_ControlPage + "?action=" + action + "&page=" + window.location.href + "&title=" + document.title + "&r=" + now;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);

	} //if
	
}

function PDFBINDER_SetPage(pdfBinderPath, url, pdfBinderViewControlId, hideButtons)
{
	PDFBINDER_Path = pdfBinderPath;
	PDFBINDER_Url = url;
	PDFBINDER_ViewControlId = pdfBinderViewControlId;
	PDFBINDER_HideButtons = hideButtons;
	
	PDFBINDER_Initialise();

}

