function loadExtraImages()
{
	if (document.images)
	{
		image1 = new Image();
		image1.src = "../images/home_on.gif";
		image2 = new Image();
		image2.src = "../images/contact_on.gif";
	}
	//eventLoader();
}

function swapImages(object,image)
{
	object.src = image;
}

// Self Quote functions
// calculate the new value for one specific row
function calcValue(obj)
{
	var baseName = new String();
	var defValue = new Number();
	var newValue = new Number();
	
	baseName = obj.name.substring(0,obj.name.indexOf("_"));
	defValue = Number(xGetElementById(baseName + "_def_value").value);
	newValue = obj.value * defValue;
	
	xGetElementById(baseName + "_price").value = currency(newValue);
	
	calcTotals();
}

// function to calculate the subtotal, VAT and Grand Total
function calcTotals()
{	
	var subtotal = new Number();
	var vat = new Number();
	var total = new Number();
	var doc = new Object();
	var name = new Array();
	
	doc = xGetElementById("enquiry_form");
			
	for(i=0; i<doc.length; i++)
	{
		name = doc[i].name.split("_");
		if(name[1] == "qty")
		{
			subtotal += doc[i].value * Number(xGetElementById(name[0]+"_def_value").value);
			i+=3;
		}
	}
	vat = subtotal * 0.175;
	total = subtotal + vat;
	
	xGetElementById("subtotal").value = currency(subtotal);
	xGetElementById("vat").value = currency(vat);
	xGetElementById("total").value = currency(total);
}

// apply the currency format (£1.234,56)
function currency(value)
{
	var result = new String();
	var delim = new String(".");
	var defNum = new String();
	var first = new String();
	var last = new String();
	var pos = new Number();
	var aux = new Array();
	
	defNum = value.toFixed(2);
	pos = defNum.indexOf(".");
	
	if(pos === false)
	{
		first = defNum;
		last = "00";
	} else {
		first = defNum.substring(0,pos);
		last = defNum.substring(Number(pos + 1));
	}
	
	while(first.length > 3)
	{
		var nn = first.substr(first.length-3);
		aux.unshift(nn);
		first = first.substr(0,first.length-3);
	}
	if(first.length > 0) { aux.unshift(first); }
	first = aux.join(delim);
	
	result = "£" + first + "," + last;
	
	return result;
}

// function validate the enquiry datum
function validateEnquiry()
{
	var doc = new Object();
	var name = new Array();
	
	doc = xGetElementById("enquiry_form");
	
	// check mandatory fields
	if(doc["name"].value == "")
	{
		alert("Your name is mandatory !");
		doc["name"].focus();
		return false;
	}
	if(doc["telephone"].value == "")
	{
		alert("Your telephone is mandatory !");
		doc["telephone"].focus();
		return false;
	}
	if(doc["email"].value == "")
	{
		alert("Your email is mandatory !");
		doc["email"].focus();
		return false;
	}
	if(!isEmailAddr(doc["email"].value))
	{
		alert("Your email address is not valid !");
		doc["email"].focus();
		return false;
	}
	if(doc["postalcode"].value == "")
	{
		alert("Your postalcode is mandatory !");
		doc["postalcode"].focus();
		return false;
	}
	
	// check for optional products choosen: colour
	for(i=0; i<doc.length; i++)
	{
		name = doc[i].name.split("_");
		if(name[1] == "qty" && doc[i].value > 0 && xGetElementById(name[0]+"_color") && xGetElementById(name[0]+"_color").value == "none")
		{
			alert("You didn't choose the colour for all optional elements !");
			xGetElementById(name[0]+"_color").focus();
			return false;
		}
	}
	
	return true;
}

function sendEnquiry()
{
	if(validateEnquiry())
		xGetElementById("enquiry_form").submit();
}

// function to create print preview
function printEnquiry()
{
	if(validateEnquiry())
	{
		var doc = new Object();
		doc = xGetElementById("enquiry_form");
		
		popup = window.open('','',"menubar=no,width=650,height=450,toolbar=no,status=no,location=no");
		popup.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		popup.document.write("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" /><title>BistroCentre.com - Print Self Quote</title></head><body>");
		popup.document.write("<form name=\"print\" method=\"post\" action=\"print.php\">");
		
		for(i=0; i<doc.length; i++)
		{
			popup.document.write("<input type=\"hidden\" name=\""+doc[i].name+"\" value=\""+doc[i].value+"\" />");
		}
		
		popup.document.write("</form>");
		popup.document.write("</body></html>");
		popup.document.forms.print.submit();
		popup.focus();
		popup.print();
	}
}

// cross-browser for getElementById
function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

// Function which validates an email address
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}



// Made by geeeet@ghtml.com
// Keep these two lines and you're free to use this code

var upH = 18; // Height of up-arrow
var upW = 12; // Width of up-arrow
var downH = 18; // Height of down-arrow
var downW = 12; // Width of down-arrow
var dragH = 26; // Height of scrollbar
var dragW = 17; // Width of scrollbar
var scrollH = 161; // Height of scrollbar
var speed = 4; // Scroll speed

// Browser detection
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;

var mouseY; // Mouse Y position onclick
var mouseX; // Mouse X position onclick

var clickUp = false; // If click on up-arrow
var clickDown = false; // If click on down-arrow
var clickDrag = false; // If click on scrollbar
var clickAbove = false; // If click above scrollbar
var clickBelow = false; // If click below scrollbar

var timer = setTimeout("",500); // Repeat variable
var upL; // Up-arrow X
var upT; // Up-arrow Y
var downL; // Down-arrow X
var downT; // Down-arrow Y
var dragL; // Scrollbar X
var dragT; // Scrollbar Y
var rulerL; // Ruler X
var rulerT; // Ruler Y
var contentT; // Content layer Y;
var contentH; // Content height
var contentClipH; // Content clip height
var scrollLength; // Number of pixels scrollbar should move
var startY; // Keeps track of offset between mouse and span

// Mousedown
function down(e){
	if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
	getMouse(e);
	startY = (mouseY - dragT);
	
	// If click on up-arrow
	if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
		clickUp = true;
		return scrollUp();
	}	
	// Else if click on down-arrow
	else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
		clickDown = true;
		return scrollDown();
	}
	// Else if click on scrollbar
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
		clickDrag = true;
		return false;
	}
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
		// If click above drag
		if(mouseY < dragT){
			clickAbove = true;
			clickUp = true;
			return scrollUp();
		}
		// Else click below drag
		else{
			clickBelow = true;
			clickDown = true;
			return scrollDown();
		}
	}
	// If no scrolling is to take place
	else{
		return true;
	}
}

// Drag function
function move(e){
	if(clickDrag && contentH > contentClipH){
		getMouse(e);
		dragT = (mouseY - startY);
		
		if(dragT < (rulerT))
			dragT = rulerT;		
		if(dragT > (rulerT + scrollH - dragH))
			dragT = (rulerT + scrollH - dragH);
		
		contentT = ((dragT - rulerT)*(1/scrollLength));
		contentT = eval('-' + contentT);

		moveTo();
		
		// So ie-pc doesn't select gifs
		if(ie4)
			return false;
	}
}

function up(){
	clearTimeout(timer);
	// Resetting variables
	clickUp = false;
	clickDown = false;
	clickDrag = false;
	clickAbove = false;
	clickBelow = false;
	return true;
}

// Reads content layer top
function getT(){
	if(ie4)
		contentT = document.all.content.style.pixelTop;
	else if(nn4)
		contentT = document.contentClip.document.content.top;
	else if(dom)
		contentT = parseInt(document.getElementById("content").style.top);
}

// Reads mouse X and Y coordinates
function getMouse(e){
	if(ie4){
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}
	else if(nn4 || dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
}

// Moves the layer
function moveTo(){
	if(ie4){
		document.all.content.style.top = contentT;
		document.all.ruler.style.top = dragT;
		document.all.drag.style.top = dragT;
	}
	else if(nn4){
		document.contentClip.document.content.top = contentT;
		document.ruler.top = dragT;
		document.drag.top = dragT;
	}
	else if(dom){
		document.getElementById("content").style.top = contentT + "px";
		document.getElementById("drag").style.top = dragT + "px";
		document.getElementById("ruler").style.top = dragT + "px";
	}
}

// Scrolls up
function scrollUp(){
	getT();
	
	if(clickAbove){
		if(dragT <= (mouseY-(dragH/2)))
			return up();
	}
	
	if(clickUp){
		if(contentT < 0){		
			dragT = dragT - (speed*scrollLength);
			
			if(dragT < (rulerT))
				dragT = rulerT;
				
			contentT = contentT + speed;
			if(contentT > 0)
				contentT = 0;
			
			moveTo();
			timer = setTimeout("scrollUp()",25);
		}
	}
	return false;
}

// Scrolls down
function scrollDown(){
	getT();
	
	if(clickBelow){
		if(dragT >= (mouseY-(dragH/2)))
			return up();
	}

	if(clickDown){
		if(contentT > -(contentH - contentClipH)){			
			dragT = dragT + (speed*scrollLength);
			if(dragT > (rulerT + scrollH - dragH))
				dragT = (rulerT + scrollH - dragH);
			
			contentT = contentT - speed;
			if(contentT < -(contentH - contentClipH))
				contentT = -(contentH - contentClipH);
			
			moveTo();
			timer = setTimeout("scrollDown()",25);
		}
	}
	return false;
}

// reloads page to position the layers again
function reloadPage(){
	location.reload();
}

// Preload
function eventLoader(){
	if(ie4){
		// Up-arrow X and Y variables
		upL = document.all.up.style.pixelLeft;
		upT = document.all.up.style.pixelTop;		
		// Down-arrow X and Y variables
		downL = document.all.down.style.pixelLeft;
		downT = document.all.down.style.pixelTop;
		// Scrollbar X and Y variables
		dragL = document.all.drag.style.pixelLeft;
		dragT = document.all.drag.style.pixelTop;		
		// Ruler Y variable
		rulerT = document.all.ruler.style.pixelTop;		
		// Height of content layer and clip layer
		contentH = parseInt(document.all.content.scrollHeight);
		contentClipH = parseInt(document.all.contentClip.style.height);
	}
	else if(nn4){
		// Up-arrow X and Y variables
		upL = document.up.left;
		upT = document.up.top;		
		// Down-arrow X and Y variables
		downL = document.down.left;
		downT = document.down.top;		
		// Scrollbar X and Y variables
		dragL = document.drag.left;
		dragT = document.drag.top;		
		// Ruler Y variable
		rulerT = document.ruler.top;
		// Height of content layer and clip layer
		contentH = document.contentClip.document.content.clip.bottom;
		contentClipH = document.contentClip.clip.bottom;
	}
	else if(dom){
		// Up-arrow X and Y variables
		upL = parseInt(document.getElementById("up").style.left);
		upT = parseInt(document.getElementById("up").style.top);
		// Down-arrow X and Y variables
		downL = parseInt(document.getElementById("down").style.left);
		downT = parseInt(document.getElementById("down").style.top);
		// Scrollbar X and Y variables
		dragL = parseInt(document.getElementById("drag").style.left);
		dragT = parseInt(document.getElementById("drag").style.top);
		// Ruler Y variable
		rulerT = parseInt(document.getElementById("ruler").style.top);
		// Height of content layer and clip layer
		contentH = parseInt(document.getElementById("content").offsetHeight);
		contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
		document.getElementById("content").style.top = 0 + "px";
		
	}
	// Number of pixels scrollbar should move
	scrollLength = ((scrollH-dragH)/(contentH-contentClipH));
	// Initializes event capturing
	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
}

