function watch_demo()
{
	//alert('Playing');
	
	Lightview.show({
						href: '/franchiseprintdirect/video/promovideo.html',
						options: 
						{
							width: 840,
							height: 540
						}
				   });
}

function display_error(error)
{
	document.observe('lightview:loaded', function() {
		Lightview.show({
							href: '/error-msg.php?m=' + encodeURIComponent(error),
							options: 
							{
								width: 480,
								height: 150,
								closeButton: 'large',
								menubar: 'bottom'
							}
					   });	
	});
}

function checkAvail(quan, prodcode)
{
	var quan = quan;
	var prodcode = prodcode;
	
	if (quan == 0)
	{
		alert ("Item code: "+prodcode+". This item is not held in stock as it is a print to order item and is subject to our normal turn around times.");
	}
	
}


function getXmlHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
}

var xml_request = getXmlHttpRequestObject();

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
	HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode)
	{
		switch (where)
		{
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode, this);
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode, this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if (this.nextSibling)
				{
					this.parentNode.insertBefore(parsedNode, this.nextSibling);
				}
				else
				{
					this.parentNode.appendChild(parsedNode);
				}
				break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where, htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where, parsedHTML);
	}


	HTMLElement.prototype.insertAdjacentText = function(where, txtStr)
	{
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where, parsedText);
	}
}

function slash_single_quote(str)
{
	str = str.replace(/\'/g, "\\'");

	return str;
}

function get_element_position(element)
{
	var top = 0;
	var left = 0;

	if(element.offsetParent)
	{
		left = element.offsetLeft;
		top = element.offsetTop;

		while(element = element.offsetParent)
		{
			left += element.offsetLeft;
			top += element.offsetTop;
		}
	}

	return [left, top];
}

function get_cursor_position(event)
{
	event = event || window.event;

	var cursor = {x:0, y:0};

	if (event.pageX || event.pageY)
	{
		cursor.x = event.pageX;
		cursor.y = event.pageY;
	}
	else
	{
		cursor.x = event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
		cursor.y = event.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
	}

	return [cursor.x, cursor.y];
}

function cancel_bubble(event)
{
	if(navigator.userAgent.indexOf("MSIE") >= 0)
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	else
	{
		event.stopPropagation();
		event.preventDefault();
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function select_menu_item(id, value)
{
	var list = document.getElementById(id);
	
	for(var i = 0; i < list.options.length; i++)
	{
		if(list.options[i].value == value)
		{
			list.options[i].selected = true;
		}
		else
		{
			list.options[i].selected = false;
		}
	}
}

function get_selected_menu_item(id)
{
	var list = document.getElementById(id);
	var selected_item = list.selectedIndex;
	var value = list.options[selected_item].value;;

	return value;
}

var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;

ModalDialog.value = '';
ModalDialog.eventhandler = '';

function ModalDialogMaintainFocus()
{
	try
	{
		if (ModalDialogWindow.closed)
		{
			window.clearInterval(ModalDialogInterval);
			eval(ModalDialog.eventhandler);
			return;
		}

//		ModalDialogWindow.focus();
	}
	catch (everything)
	{

	}
}

function ModalDialogRemoveWatch()
{
	ModalDialog.value = '';
	ModalDialog.eventhandler = '';
}

function ModalDialogShow(id)
{
	ModalDialogRemoveWatch();
	
//	ModalDialog.eventhandler = "autosave_template()";

	var args='width=550,height=350,left=100,top=100,toolbar=0,';
	args += 'location=0,status=0,menubar=0,scrollbars=1,resizable=0';

	ModalDialogWindow=window.open("","",args);
	ModalDialogWindow.document.open('');
	ModalDialogWindow.document.location = "editor.php?t=" + id;
	ModalDialogWindow.document.close();
	ModalDialogWindow.focus();
	ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()", 5);
}

function find_address()
{
	// get the postcode
	var postcode = document.getElementById('postcode').value;

	if(valid_postcode(postcode, false, true) === false)
	{
		alert("Please enter a valid postcode to search for an address.");
		document.getElementById('search_postcode').focus();
	}
	else
	{
		get_postcode_addresses(postcode);
	}
}

function get_postcode_addresses(postcode)
{
	if (xml_request.readyState == 4 || xml_request.readyState == 0)
	{
		xml_request.open("GET", "find_address.php?postcode=" + escape(postcode), false);
		xml_request.send(null);

		// get the address list
		var address_list = document.getElementById('address_list');

		// get the xml document from the returned xml string
		var doc = xml_request.responseXML;

		var root_node = doc.documentElement;
		var child_nodes = root_node.childNodes;

		// check to see if any addresses have been found
		var found_addresses = false;

		for(var i = 0; i < child_nodes.length; i++)
		{
			var node = child_nodes[i];

			if(node.nodeType == 1)
			{
				found_addresses = true;
				break;
			}
		}

		if(found_addresses === false)
		{
			alert("The postcode you entered did not produce any results. Please fill in the address form manually or enter an alternate postcode.");
		}
		else
		{
			// empty the current list
			address_list.options.length = 0;
	
			// add a "please select address" option as default selected
			address_list.options[0] = new Option("Please select an address...", "");
	
			// loop through all the addresses
			var options_count = 1;
	
			for(var i = 0; i < child_nodes.length; i++)
			{
				var node = child_nodes[i];
	
				if(node.nodeType == 1)
				{
					var id = node.getElementsByTagName('id');
					var description = node.getElementsByTagName('description');
	
					// add an entry to the address list
					address_list.options[options_count] = new Option(description[0].firstChild.nodeValue, id[0].firstChild.nodeValue);
					options_count++;
				}
			}
			
			alert("Found "+(options_count - 1)+" addresses in the selected postcode. Please select one from the list to fill the address form.");
		}
	}
}

function select_address()
{
	// get the currently selected address
	var current_address = document.getElementById('address_list').selectedIndex;
	var address_id = document.getElementById('address_list').options[current_address].value;
	
	if(address_id != "")
	{	
		get_address_from_id(address_id);
	}
}

function clear_address_fields()
{
	var fields_to_clear = new Array("address_line1", "address_line2", "address_town", "address_county");

	for(var i = 0; i < fields_to_clear.length; i++)
	{
		var current_field = fields_to_clear[i];
		if(document.getElementById(current_field))
		{
			document.getElementById(current_field).value = "";
		}
	}
}

function get_address_from_id(address_id)
{
	if (xml_request.readyState == 4 || xml_request.readyState == 0)
	{
		xml_request.open("GET", "get_address.php?address_id=" + escape(address_id), false);
		xml_request.send(null);

		var doc = xml_request.responseXML;

		var root_node = doc.documentElement;
		var child_nodes = root_node.childNodes;

		// clear the current address fields
		clear_address_fields();

		for(var i = 0; i < child_nodes.length; i++)
		{
			var node = child_nodes[i];

			if(node.nodeType == 1)
			{
				if(document.getElementsByName(node.nodeName)[0])
				{
					document.getElementsByName(node.nodeName)[0].value = node.firstChild.nodeValue;
				}
			}
		}
		
		// check if the paon has been filled in...if not then fill with organisation name
		if(document.getElementById('building_name'))
		{
			if(document.getElementById('building_name').value == "")
			{
				document.getElementById('building_name').value = organisation_name;
			}
		}

		// check if town is London and the county is empty
		if(document.getElementById('post_town'))
		{
			if(document.getElementById('post_town').value == "London")
			{
				document.getElementById('county').value = "London";
			}
		}

		// check if town is Bristol
		if(document.getElementById('post_town'))
		{
			if(document.getElementById('post_town').value == "Bristol")
			{
				document.getElementById('county').value = "Bristol";
			}
		}
	}
}

function get_list_items(type, table, source_id, target_id, id_field, search_field, display_field)
{
	var source_field = document.getElementById(source_id);

	switch (source_field.tagName)
	{
		case "INPUT":
			var id = source_field.value;
			break;

		case "SELECT":
			var selected_option = source_field.selectedIndex;
			var id = source_field.options[selected_option].value;
			break;

		default:
			var id = "";
	}

	if(id == "")
	{
		// get the select list
		var list = document.getElementById(target_id);

		// empty the current list
		list.options.length = 0;

		// add a "please select a..." option as default selected
		list.options[0] = new Option("First select a " + type + "...", "");

		return false;
	}

	if ((xml_request.readyState == 4) || (xml_request.readyState == 0))
	{
		xml_request.open("POST", "get_list_items.php", false);
		xml_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xml_request.send("table=" + encodeURIComponent(table) + "&if=" + encodeURIComponent(id_field) + "&sf=" + encodeURIComponent(search_field) + "&df=" + encodeURIComponent(display_field) + "&id=" + encodeURIComponent(id));

		var doc = xml_request.responseXML;

		if(doc == null)
		{
			alert(xml_request.responseText);
		}
		else
		{
			// get the select list
			var list = document.getElementById(target_id);

			// empty the current list
			list.options.length = 0;

			// add a "please select a..." option as default selected
			list.options[0] = new Option("Please select an item...", "");

			var options_count = 1;

			// loop through all the items returned
			var root_node = doc.documentElement;
			var items = root_node.childNodes;

			if(items.length == 0)
			{
				list.options[options_count] = new Option("No items found!", "");
			}
			else
			{
				for(var i = 0; i < items.length; i++)
				{
					var item = items[i];
	
					if(item.nodeType == 1)
					{
						if(item.getElementsByTagName('value')[0].firstChild)
						{
							var value = item.getElementsByTagName('value')[0].firstChild.nodeValue;
						}
						else
						{
							var value = "";
						}
	
						if(item.getElementsByTagName('display')[0].firstChild)
						{
							var display = item.getElementsByTagName('display')[0].firstChild.nodeValue;
						}
						else
						{
							var display = "";
						}

						list.options[options_count] = new Option(display, value);
						options_count++;
					}
				}
			}
		}
	}
}

function confirm_action(text, uri)
{
	var confirmed = confirm(text);

	if (confirmed === true) 
	{
		document.location.href = uri;
	}
}

function show_element(element_id)
{
	if (document.getElementById(element_id)) 
	{
		var element = document.getElementById(element_id);

		element.style.display = "block";
	}
}

function hide_element(element_id)
{
	if (document.getElementById(element_id)) 
	{
		var element = document.getElementById(element_id);

		element.style.display = "none";
	}
}

// Copyright 2006-2007 javascript-array.com

var timeout	= 300;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 

function displayRow(id){
	var row = document.getElementById(id);
	if (row.style.display == '') row.style.display = 'none';
	else row.style.display = '';
}
function show_hide(id) {
    var element = document.getElementById(id);
    if (element.style.display == "none") {
        element.style.display = "block";
    } else if (element.style.display == "block") {
        element.style.display = "none";
    }
}
