// Function to return XMLHttpRequest to use for AJAX calls
function getXMLHTTPRequest() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
		return xmlHttp;
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			return xmlHttp;
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				return xmlHttp;
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}
// allow firefox to handle a click - code possibly by Jason Karl Davis
if (typeof HTMLElement != 'undefined' && !HTMLElement.prototype.click)
	HTMLElement.prototype.click = function() {
		var evt = this.ownerDocument.createEvent('MouseEvents');
		evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView,
				1, 0, 0, 0, 0, false, false, false, false, 0, null);
		this.dispatchEvent(evt);
	}
function toggleTabVisible(tabId) {
	// Hide all tabs
	var tabs = document.getElementById("tabs");
	var children = tabs.childNodes;
	for ( var i = 0; i < children.length; i = i + 1) {
		if (children[i].style) {
			children[i].style.display = "none";
		}
	}
	// Change all the play buttons to off
	var tabList = document.getElementById("tabList");
	children = tabList.childNodes;
	for (i = 0; i < children.length; i = i + 1) {
		if (children[i].style) {
			children[i].style.background = "url(/images/play_off.gif) no-repeat center left";
		}
	}
	var tabDiv = document.getElementById(tabId + 'Div');
	tabDiv.style.display = "block";
	var tabLi = document.getElementById(tabId + 'Li');
	tabLi.style.background = "url(/images/play.gif) no-repeat center left";
}

// Function to mark a page
function markPage(entityId) {
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var str = xmlHttp.responseText;
			var el = document.getElementById("numMarkedPagesSpan");
			if (el) {
				var num = xmlHttp.responseText;
				el.innerHTML = num;
				if (num > 0) {
					var el2 = document.getElementById("viewMarkedPagesSpan");
					el2.style.display = "inline";
				}
			}
		}
	};
	xmlHttp.open("GET", "/admin/markpage.action?id=" + entityId, true);
	xmlHttp.send(null);
	return false;
}
// Function to remove a bookmarked page
function removeMarkedPage(id) {
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var liName = id + "MarkedPage";
			var liEl = document.getElementById(liName);
			if (liEl) {
				var listEl = liEl.parentNode;
				listEl.removeChild(liEl);
			}
			var str = xmlHttp.responseText;
			var el = document.getElementById("numMarkedPagesSpan");
			if (el) {
				var num = xmlHttp.responseText;
				el.innerHTML = num;
				if (num > 0) {
					var el2 = document.getElementById("viewMarkedPagesSpan");
					el2.style.display = "inline";
				}
			}
		}
	};
	xmlHttp.open("GET", "/admin/removeMarkpage.action?id=" + id, true);
	xmlHttp.send(null);
	return false;
}
//Function to remove all bookmarked pages
function removeAllMarkedPages() {
	confirm('Do you want to remove all book marked pages from the list?');
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var str = xmlHttp.responseText;
			var el = document.getElementById("numMarkedPagesSpan");
			if (el) {
				var num = xmlHttp.responseText;
				el.innerHTML = num;
				var el2 = document.getElementById("viewMarkedPagesSpan");
				el2.style.display = "none";
			}
			var olEl = document.getElementById("bookmarkedList");
			if (olEl) {
				olEl.parentNode.removeChild(olEl);
			}
		}
	};
	xmlHttp.open("GET", "/admin/removeAllMarkedPagespage.action", true);
	xmlHttp.send(null);
	return false;
}
// Function to display popup for emailing
function openContactForm() {
	var contactWindow = window.open("/contact.action", "_blank", "width=600, height=410, toolbar=no, scrollbars=yes");
	return false;
}

function getSkuPage(windowNumber, skuId) {
    //Look up the node we'll stick the text under.
	var divName = "sideSkuPopupDiv" + windowNumber.toString();
    var targetNode = dojo.byId(divName);
    var targetUrl = "/skuDisplay.action?skuId=" + skuId.toString();
    
    //The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
    var xhrArgs = {
        url: targetUrl,
        handleAs: "text",
        load: function(data) {
            targetNode.innerHTML = data;
        },
        error: function(error) {
            targetNode.innerHTML = "An unexpected error occurred: " + error;
        }
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrGet(xhrArgs);
}



// Function to display sku popup with additional information.
function openSkuPopup(windowNumber, skuId)
{
	closeAllSkuPopups();
	/*
	var divName = "#sideSkuPopupDiv" + windowNumber.toString();
	
	//First load information
	$(divName).load("/skuDisplay.action?skuId=" + skuId.toString());*/
	getSkuPage(windowNumber, skuId);
	
	//Now make division visible
	var divNameJs = "sideSkuPopupDiv" + windowNumber.toString();
	var popupDiv = document.getElementById(divNameJs);
	popupDiv.style.display = "block";
	
	
	return false;
}


// Function to close all sku popups if another sku popup is selected.
function closeAllSkuPopups()
{
	var sidePopupDivs = document.getElementsByName("sideSkuPopupDiv");
	
	for (i = 0; i < sidePopupDivs.length; i++)
	{
		sidePopupDivs[i].style.display = "none";
	}
}

function closeSkuPopup(skuId)
{
	var elementId = "skuId" + skuId.toString();
	var hiddenSkuId = document.getElementById(elementId);
	
	hiddenSkuId.parentNode.style.display = "none";
	return false;
}

//Calculates and sets the final price of the quote
function setFinalPrice()
{
    var quotePrice = 0;
    
    var productPrice = +(jQuery('#productPrice').val());
    quotePrice += productPrice;
    //Determine which supplements are selected and add their price.
    var supplementSelectedArray = jQuery('[supplementNumber]');
    
    for (var i=0; i<supplementSelectedArray.length; i++)
    {
        var supplementCheckbox = supplementSelectedArray[i];
        if (supplementCheckbox.checked)
        {
			var supplementNumber = supplementCheckbox.attributes['supplementNumber'].value;
            var supplementPriceInput = jQuery('#supplement'+supplementNumber+'Price')[0];
            var supplementPrice = +(supplementPriceInput.value)
            quotePrice += supplementPrice;
        }
    }
    
    var formattedPrice = "R " + addSeparators(quotePrice.toString(), " ");
    jQuery('#quotePrice').html(formattedPrice);
    var discount = parseFloat(jQuery('#quoteDiscount').val());
    
    if (discount>0) quotePrice -= discount;
    
    var formattedFinalPrice = "R " + addSeparators(quotePrice.toString(), " ");
    jQuery('#finalQuotePrice').html(formattedFinalPrice);
}


function testValidResponse(replyTextSelector)
{
    var replyTextElement = jQuery(replyTextSelector)[0];
    
    var replyText = replyTextElement.value;
    
    if (replyText.length > 0) return true;
    else 
    {
    	alert("Your response is empty. Please enter your reply.");
    	return false;
    }
}
