//===============================================
// BROWSER DETECTION FUNCTIONS

function browserVersion()
// Returns an array [BrowserName, Version]
/*
 * Currently, we only care whether the browser is IE or not
 * and, if IE, whether it is IE8+ or not, so the function
 * only distinguishes between IE and Other 
 * and only returns version for IE.	
 */

{
	var v;
	
	// declare and initialise variables
	var browser = "Other";
	var version = 0;
	
	// check for IE
	v = getIEVersion();
	if(v != -1) {
		browser = "IE";
		version = v;
	}
	
	return [browser, version];		
}

function getIEVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
// source: http://msdn.microsoft.com/en-us/library/ms537509(VS.85).aspx
{
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
    	var ua = navigator.userAgent;
    	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    	if (re.exec(ua) != null)
      		rv = parseFloat( RegExp.$1 );
  	}
  	return rv;
}


//===============================================
// GENERIC COPYING FUNCTIONS


function getCopyElement(elementName, elementType)
{	var elem = document.getElementById(elementName);
	if(elem == undefined) {
		elem = document.createElement(elementType);
		try {
			elem.style.width = 0;
			elem.style.height = 0;
		}
		catch(e) {}
		elem.setAttribute("style", "width:0; height:0;");
		elem.setAttribute("id", elementName);
		document.body.appendChild(elem);
	}
	return elem;
}

// This is currently only supported by IE browsers.
function copyElementToClipboard(element)
{
	var ctrlRange = document.body.createControlRange();
	ctrlRange.add(element);
	ctrlRange.execCommand('Copy');
}


//===============================================
// IMAGE GENERATION FUNCTIONS

function getCopyImageElement()
{
	return getCopyElement("copyImageContainer", "img");
}

// For browsers that support clipboard copy but do not support data URLs (IE7-), load image from server
// The imageID is the id for the image generated by the server
function loadCopyImageFromServer(imageID) {
	setCopyImageURL("https://comparator.ricewarner.com/secure-content/clients/displayimage/loadbase64.php?id=" + imageID); 
}	

// For browsers that support clipboard copy and data URLs (IE8+), generate image from data url
// The imageID is the id for the image generated by the server
function loadCopyImageFromData(imageData) { 
	setCopyImageURL("data:image/png;base64," + imageData); 
}

// For browsers that support data URLs but do not support clipboard copy (non-IE), display
// image in a new window with instruction to right-click and copy
function displayCopyImageFromData(imageData) {
	displayCopyImageURL("data:image/png;base64," + imageData);
}

// For browsers that support neither data URLs nor clipboard copy (old browsers!), display
// server-created image in a new window with instruction to right-click and copy
function displayCopyImageFromServer(imageID) {
	displayCopyImageURL("https://comparator.ricewarner.com/secure-content/clients/displayimage/loadbase64.php?id=" + imageID);
}

// Generic function to set URL of image container
function setCopyImageURL(url) { 
	var imageContainer = getCopyImageElement()
	imageContainer.onload = copyImageContainerToClipboard;	// attempt to copy image to clipboard after loading
	imageContainer.setAttribute('src', url);
}	

// Generic function to display image for manual copying based on URL
function displayCopyImageURL(url) { 
	var win = window.open("", "_blank");
	win.document.writeln("<html><head>");
	win.document.writeln("<style>p { font-size: 10pt; font-family: Arial, Helvetica, Sans-Serif; }</style>");
	win.document.writeln("</head><body>");
	win.document.writeln("<p>Please right-click on the image and select \"Copy\" to copy the image to clipboard.</p>");
	win.document.writeln("<p><input type=\"button\" value=\"Close Window\" onClick=\"javascript:self.close();\"/></p>");
	win.document.writeln("<hr/><div>");
	win.document.writeln("<img src=\"" + url + "\" />");
	win.document.writeln("</div>");
	win.document.writeln("</body></html>");
	win.document.close();
}	


//===============================================
// IMAGE COPY FUNCTIONS

// This is currently only supported by IE browsers.
function copyImageContainerToClipboard()
{
	var imageElem = getCopyImageElement();
	copyElementToClipboard(imageElem);
}

//===============================================
// HTML GENERATION/COPY FUNCTIONS

function getCopyHTMLElement()
{
	return getCopyElement("copyHTMLContainer", "div");
}

function loadCopyHTMLFromData(htmlString)
{
	var copyElem = getCopyHTMLElement();
	copyElem.innerHTML = htmlString;
}

// This is currently only supported by IE browsers.
// This does not currently support copying formatting.
function copyHTMLToClipboard()
{
	var txtArea = getCopyElement("copyHTMLTextArea", "textarea");
	var copyElem = getCopyHTMLElement();
	txtArea.innerText = copyElem.innerText;
	var txtRange = txtArea.createTextRange();
	txtRange.execCommand('RemoveFormat');
	txtRange.execCommand('Copy');
}

// Generic function to display HTML for manual copying
function displayCopyHTMLFromData(htmlString) { 
	var win = window.open("", "_blank");
	win.document.writeln("<html><head>");
	win.document.writeln("<style>");
	win.document.writeln("body { font-size: 10pt; font-family: Arial, Helvetica, Sans-Serif; }");
	win.document.writeln("table { border-collapse: collapse; } ");
	win.document.writeln("td, th { font-size: 10pt; border: 1px solid #cccccc; padding: 2px 5px 2px 5px; margin: 0px 0px 0px 0px; } ");
	win.document.writeln("</style>");
	win.document.writeln("</head><body>");
	win.document.writeln("<p>Please select the text to copy, right-click and select \"Copy\".</p>");
	win.document.writeln("<p><input type=\"button\" value=\"Close Window\" onClick=\"javascript:self.close();\"/></p>");
	win.document.writeln("<hr/><div>");
	win.document.writeln(htmlString);
	win.document.writeln("</div>");
	win.document.writeln("</body></html>");
	win.document.close();
}	

