// The HTTP object
var http;


/*************************************************************************
AJAX RELATED
*************************************************************************/

/**
 * getHTTPObject() instantiates and returns an HTTP object
 *
 * @return a new HTTP object
*/
function getHTTPObject() {
	
	// Create Object
	var xmlhttp;
	
	// Try conditional statements
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	
	// If no good, try the old fashioned way
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	// Return
	return xmlhttp;
}

function doSceneCheck(code) {

	// Instantiate
	http = getHTTPObject();

	// Load form data
	http.open("GET", "../site_specific/check_for_order_updates.php?code=" + code, true);

	// Response
	http.onreadystatechange = doSceneCheckResponse;

	// Send
	http.send(null);
}

function doSceneCheckResponse() {
	// Check response type
	if (http.readyState == 4) {

		// If update happened
		if (http.responseText == "true") {
			window.location = window.location;
		}
	}
}


function bottomSearch() {

	// Search
	var search = document.getElementById("bs").value;
	document.getElementById("s").value = search;
	document.getElementById("browsesceneform").submit();

	return false;
}





/*************************************************************************
STYLESHEET RELATED
*************************************************************************/

/**
 * setActiveStyleSheet() sets an active stylesheet by name
*/
function setActiveStyleSheet(title) {

	var link_tag;

	// Get all <link> tags
	for (var i = 0; (link_tag = document.getElementsByTagName("link")[i]); i++) {

		// If its a style and it has a title
		if (link_tag.getAttribute("rel").indexOf("style") != -1 && link_tag.getAttribute("title")) {

			// If it's title matches
			if (link_tag.getAttribute("title") == title) {
				link_tag.disabled = false;
			} else {
				link_tag.disabled = true;
			}
		}
	}

	// Update selector
	writeStyleSelector();
}


/**
 * getActiveStyleSheet() gets the active stylesheet
*/
function getActiveStyleSheet() {

	// Get all <link> tags
	for (var i = 0; (link_tag = document.getElementsByTagName("link")[i]); i++) {

		// If its a style and it has a title
		if (link_tag.getAttribute("rel").indexOf("style") != -1 && link_tag.getAttribute("title")) {

			// If active, store it
			if (link_tag.disabled == false) {
				return link_tag.getAttribute("title");
			}
		}
	}
}


/**
 * writeStyleSelector() writes the style selector
*/
function writeStyleSelector() {

	// Get active style
	var active_style;

	// Get list of styles
	var style_list = new Array();

	// Get all <link> tags
	for (var i = 0; (link_tag = document.getElementsByTagName("link")[i]); i++) {

		// If its a style and it has a title
		if (link_tag.getAttribute("rel").indexOf("style") != -1 && link_tag.getAttribute("title")) {

			// Add to list
			style_list.push(link_tag.getAttribute("title"));

			// If active, store it
			if (link_tag.disabled == false) {
				active_style = link_tag.getAttribute("title");
			}
		}
	}

	// Write
	var output_element = document.getElementById("stylelist");
	var output = "";

	output = "Available Styles: ";

	// Create links for each style
	for (var i = 1; i < style_list.length; i++) {
		output += ((style_list[i] == active_style) ? "<strong>" : "") + "<a href=\"#\" onclick=\"setActiveStyleSheet('" + style_list[i] + "');\">" + style_list[i] + "</a>" + ((style_list[i] == active_style) ? "</strong>" : "");
		if (i < style_list.length - 1) {
			output += " | ";
		}
	}

	// Set output
	output_element.innerHTML = output;

	// Store cookie
	setCookie("ss-stylesheet", active_style, 100);
}


/**
 * setCookie() sets a cookie value
 *
 * @param name is the name of the cookie to set
 * @param value is the value to set for the cookies
 * @param days is the number of days before the cookie expires
*/
function setCookie(name, value, days) {

	// If days were passed
	if (days) {

		// Get date
		var date = new Date();

		// Set to number of passed days in future
		date.setTime(date.getTime() + (days * 24 * 60 * 60 *1000));

		// Determine expiry
		var expires = "; expires=" + date.toGMTString();

	} else {

		// Never expires
		var expires = "";
	}

	// Set the cookie
	document.cookie = name + "=" + value + expires + "; path=/";
}


// Original content of purchasing box for when displaying login in it
var purchasing_original_content = "";
var newsletters_original_content = "";


/**
 * displayLogin() displays the login dialog
 *
 * @param submission_page is the page to submit the form to
*/
function displayLogin(submission_page) {

	// Store original content if necessary
	if (purchasing_original_content == "") {
		purchasing_original_content = document.getElementById("logindialog").innerHTML;
	}

	// Replacement form
	var output = "<form name=\"usernameform\" id=\"usernameform\" action=\"" + submission_page + ".php\" method=\"post\">" +
		"<p><strong>username:</strong></p>" +
		"<p><input name=\"loginid\" id=\"loginid\" type=\"text\" value=\"\" /></p>" +
		"<p><strong>password:</strong></p>" +
		"<p><input name=\"loginpassword\" id=\"loginpassword\" type=\"password\" value=\"\" /> <input type=\"submit\" class=\"button\" value=\"Go\" /></p>" +
		"</form>";

	// Link back
	output += "<p><a href=\"#\" onclick=\"hideLogin();\">&laquo; Hide login</a></p>";

	document.getElementById("logindialog").innerHTML = output;

}


/**
 * hideLogin() hides the login dialog
*/
function hideLogin() {
	document.getElementById("logindialog").innerHTML = purchasing_original_content;
}


function displayNewsletterRegister(submission_page) {

	if (newsletters_original_content == "") {
		newsletters_original_content = document.getElementById("newsletterregister").innerHTML;
	}

	// Create output
	var output = "<form name=\"nlregform\" id=\"nlregform\" action=\"" + submission_page + ".php\" method=\"post\">" +
		"<p><strong>Username:</strong><br />" +
		"<input type=\"text\" name=\"nlusername\" value=\"\" /></p>" +
		"<p><strong>Password:</strong><br />" +
		"<input type=\"password\" name=\"nlpassword\" /></p>" +
		"<p><strong>First Name:</strong><br />" +
		"<input name=\"nlregfname\" id=\"nlregfname\" type=\"text\" value=\"\" /></p>" +
		"<p><strong>Last Name:</strong><br />" +
		"<input name=\"nlreglname\" id=\"nlreglname\" type=\"text\" value=\"\" /></p>" +
		"<p><strong>Email Address:</strong><br />" +
		"<input name=\"nlregemail\" id=\"nlregemail\" type=\"text\" value=\"\" /></p>" +
		"<p><input type=\"submit\" value=\"Register\" /></p>" +
		"</form>";

	// Link back
	output += "<p><a href=\"#\" onclick=\"hideNewsletterRegister();\">&laquo; Cancel Registration</a></p>";

	//
	document.getElementById("newsletterregister").innerHTML = output;
}

function hideNewsletterRegister() {
	document.getElementById("newsletterregister").innerHTML = newsletters_original_content;
}

function selectPage(page, mode, id) {

	// Get start
	var start = document.getElementById("start").options[document.getElementById("start").selectedIndex].value;

	// Redirect
	window.location = page + "?mode=" + mode + "&id=" + id + "&start=" + start;
}


function showAdvancedSearch() {
	document.getElementById("advsearch").value = "1";
	document.getElementById("advsearchshow").className = "hidden";
	document.getElementById("advsearchhide").className = "";
}

function hideAdvancedSearch() {
	document.getElementById("advsearch").value = "0";
	document.getElementById("advsearchshow").className = "";
	document.getElementById("advsearchhide").className = "hidden";
}

function openGlossary(page_name) {
	window.open(page_name + "?popup=true", "glosspopup", "width=750,screenx=10,screeny=10,height=560,location=0,menubar=0,personalbar=0,status=1,scrollbars=1,toolbar=0");
}