﻿// MacR2go 
//
// Allows the adding of pages to a library (help in a session variable)
// whose pages can then be built into a PDF file for download to the user.
// 
// Maximum number of pages set to 30
//
// David Turfery
// September 2008

// updateMacR2go
//
// Updates the MacR2go_cart DIV to contain correct number of pages and relevant icons for library
// If number of pages > 0 The icons change from an info one to a create and a view one
// 
// Variables:
//
// noOfItems : The number of pages in the library
//

// GLOBAL VARIABLES
var MacR2GoAJAXPage = "/MacR2Go/MacR2go.aspx";
var MacR2GoCreatePage = "/MacR2Go/MacR2go_Create.aspx";
var MacR2GoViewPage = "/content/content_472.aspx";

function updateMacR2go(noOfItems)
{
	var s = "You have <span id='cart'><strong>";
	s = s + noOfItems;
	s = s + "</strong></span> page(s) in your <img src='/images/macr2go/MacR2go_logo_large.jpg' />";
	if(noOfItems > 0)
	{
		s = s + "<span id='MacR2go_options'><a href='" + MacR2GoCreatePage + "' target='_blank'>";
		s = s + "<img src='/images/MacR2go/button_createit1.jpg' alt='Create your MacR2go to go!' /></a>";
		s = s + "<a href='" + MacR2GoViewPage + "'><img src='/images/MacR2go/button_view.jpg' alt='View the current content of your MacR2go'  /></a>";
		s = s + "</span>";
	}
	//else
	//{
		//s = s + "<span id='MacR2go_options'><a href='" + MacR2GoViewPage + "'><img src='images/MacR2go/button_whatsthis.gif' alt='Read more about MacR2go' class='whatsthis' /></a></span>";
	//}
	
	var myDiv = document.getElementById("MacR2go_cart");
	if(myDiv)
	{
		myDiv.innerHTML = s;
	}
	
	AJAX_AlreadyAddedToLibrary();
}

// updateMacR2goAdd
//
// Updates the MacR2go_add DIV to contain correct icon depending on whether the page has been added or not
// 
// Variables:
//
// isAdded :	true	when the page has already been added
//				false	when thje page has not been added
//
function updateMacR2goAdd(isAdded)
{
	var s = ""
	var myDiv = document.getElementById("MacR2go_add");
	if(myDiv)
	{
		if(isAdded == "true")
	    {
		    s = s + "<p class='mtg_button_added'>Already added to MacR2go.</p>"
	    }
	    else
	    {
		    s = s + "<a href='Javascript: AJAX_AddToCart(); ' class='mtg_button' title='Click to add this page to your MacR2go'>Click to add this page to your MacR2go</a>";
	    }
    	
	    s = s + "<a href='" + MacR2GoViewPage + "'><img src='/images/MacR2go/button_whatsthis.gif' alt='What is MacR2Go?' title='What is MacR2Go?' class='whatsthis' /></a>";
    		
	    myDiv.innerHTML = s;
	}
	var myDiv = document.getElementById("MacR2go_added");
	if(myDiv)
	{
		s = s + "<img src='/images/macr2go/MacR2go_logo_large.jpg' alt='macR2go' title='macR2go' id='macR2Go_logo' />";
	    
    	
	    s = s + "<a href='" + MacR2GoViewPage + "'><img src='/images/MacR2go/button_whatsthis.gif' alt='What is MacR2Go?' title='What is MacR2Go?' class='whatsthis' /></a>";
    		
	    myDiv.innerHTML = s;
	}
}

// AJAX_AddToCart
//
// Adds the page (by href) to the session variables via an AJAX call to the MacR2go.aspx page
// 
//
function AJAX_AddToCart()
{
	var xmlHttp;
	
	try
	{  // Firefox, Opera 8.0+, Safari  
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{  // Internet Explorer  
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{    
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}
		}
	}
	
	xmlHttp.onreadystatechange = function()
    {
		if(xmlHttp.readyState==4)
		{
			// check that it was a success
			if(xmlHttp.responseText == "true")
			{
				AJAX_GetNoOfItems();
			}
			else
			{
				alert(xmlHttp.responseText);
			}
		}
    }
    
    //alert(document.URL);//window.location.href);
    
    var pagename = window.document.title;
    var pageid = document.URL;//.replace(/&/,"&amp;");
	xmlHttp.open("GET",MacR2GoAJAXPage + "?action=add&id=" + pageid + "::" + pagename, true);
	xmlHttp.send(null);
}

// AJAX_AddToCart
//
// Gets the number of pages in the library via an AJAX call to the MacR2go.aspx page
// 
//
function AJAX_GetNoOfItems()
{
	var xmlHttp;
	
	try
	{  // Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{  // Internet Explorer  
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{    
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}
		}
	}
	
	xmlHttp.onreadystatechange = function()
    {
		if(xmlHttp.readyState==4)
		{
			updateMacR2go(xmlHttp.responseText);
		}
    }
    
	xmlHttp.open("GET",MacR2GoAJAXPage + "?action=count",true);
	xmlHttp.send(null);
}

// AJAX_AddToCart
//
// Gets whether a page is in the library via an AJAX call to the MacR2go.aspx page
// 
//
function AJAX_AlreadyAddedToLibrary()
{
	var xmlHttp;
	
	try
	{  // Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{  // Internet Explorer  
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{    
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}
		}
	}
	
	xmlHttp.onreadystatechange = function()
    {
		if(xmlHttp.readyState==4)
		{
			updateMacR2goAdd(xmlHttp.responseText);
		}
    }
    
    var pagename = window.document.title;
    var pageid = window.location.href;
    
	xmlHttp.open("GET",MacR2GoAJAXPage + "?action=check&id=" + pageid + "::" + pagename,true);
	xmlHttp.send(null);
}
