function formFocus(theForm, theCurrent, theDesired) {
	if (theForm.value == theCurrent) {
		theForm.value = theDesired;
	}
}

function open_window(fileName, windowName, windowWidth, windowHeight, scrollbars, resizable) {
	window.open(fileName, windowName, 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=' + scrollbars + ', resizable=' + resizable + ', left=' + centre(windowWidth, screen.availWidth) + ', top=' + centre(windowHeight, screen.availHeight) + ', width=' + windowWidth + ', height=' + windowHeight);
}

function centre(size,area) {
	return (area/2)-(size/2);
}

function changeLocation(theRadio) {
	document.location.href = './get-a-quote.asp?size=' + theRadio.value;
}

function changeRoom(theSelect, theURL) {
	theURL = theURL + '?type=all';
	if (theSelect.options[theSelect.selectedIndex].value) {
		theURL = theURL + '&size=' + theSelect.options[theSelect.selectedIndex].value;
	}
	document.location.href = theURL;
}

function changeSort(theSelect) {
	var theURL = document.location.href.replace(/(\&sort=[0-9])/, '').replace(/(\?sort=[0-9])/, '').replace(/(\&offset=[0-9])/, '').replace(/(\?offset=[0-9])/, '');
	theURL = theURL.indexOf('?') > 1 ? theURL += '&' : theURL += '?';
	document.location.href = theURL + 'sort=' + theSelect.options[theSelect.selectedIndex].value;
}

function changeVan(theCheck) {
	if (theCheck.checked) {
		$("free_van1").value = 1;
		$("free_van2").value = 1;
	} else {
		$("free_van1").value = 0;
		$("free_van2").value = 0;
	}
}

function calculatePrice(theLength, thePrice) {
	arrLength = theLength.options[theLength.selectedIndex].value.split("|");
	theOffer = thePrice * arrLength[0] > 0 ? '&pound;' + (thePrice * arrLength[0]).toFixed(2) : "Free";
	$("offer-length").replace('<span id="offer-length">' + arrLength[1] + '</span>');
	$("offer-rate").replace('<dd id="offer-rate">' + theOffer + ' per week</dd>');
	$("you-save").replace('<dd id="you-save">&pound;' + ((thePrice - (thePrice * arrLength[0])) * arrLength[1]).toFixed(2) + '</dd>');
	$("length_stay1").value = theLength.options[theLength.selectedIndex].text;
	$("length_stay2").value = theLength.options[theLength.selectedIndex].text;
}

function showSpaces(ev) {
	var elt = Event.findElement(ev, 'a'); 
	var loc = elt.href.match(/#(\w+)/) ? RegExp.$1 : "tiny";
	var links = $$("ul.quote-space a");
	$$(".spaces tbody tr").each(function(el) {
		el.hide();
	});
	$$(".spaces tbody tr." + loc).each(function(el) {
		el.show();
	});
	elt.addClassName('selected');
	links.without(elt).each(function(el) {
		el.removeClassName('selected');
	});
	ev.stop();
}

function checkAvailibility(ev) {
	var insure = true; elt = Event.findElement(ev, 'a');
	var id = elt.id.replace('check', '')
	//if ($F('insurance' + id) == 33) {
	//	if (!confirm('When your belongings are stored with Easistore, as with all Storage Companies, you are required to have insurance.\n\nBy choosing \'None\' you will need to provide proof of alternative insurance when you arrive.')) {
	//		insure = false;
	//		return false;
	//	}
	//}
	if (insure) {
		var tempArray;
		for (var i = 0; i < spaceArray.length; i++) {
			tempArray = spaceArray[i].split('|');
			if (id == tempArray[0]) {
				$("space-price1").replace('<span id="space-price1">' + tempArray[5] + '</span>');
				$("space-price2").replace('<span id="space-price2">' + tempArray[5] + '</span>');
				$("space-location").replace('<dd id="space-location">' + tempArray[1] + '</dd>');
				$("space-size").replace('<dd id="space-size">' + tempArray[2] + '</dd>');
				$("space-date").replace('<dd id="space-date">' + $F('start_day') + ' ' + $('start_month').options[$('start_month').options.selectedIndex].text + ' ' + $F('start_year') + '</dd>');
				$("space-rrp").replace('<dd id="space-rrp">&pound;' + tempArray[3] + ' per week</dd>');
				//$("space-insurance").replace($('insurance' + id).options[$('insurance' + id).options.selectedIndex].text);
				//$("space-offers").replace(tempArray[4]);
				var offerArray = $F('offers' + id).split('|');
				$("space-offers").replace('<dd id="space-offers">' + $('offers' + id).options[$('offers' + id).options.selectedIndex].text + '</dd>');
				if ($('offers' + id).options[$('offers' + id).options.selectedIndex].text == 'Choose Your Special Offer...') {
					$("space-average").replace('<dd id="space-average" class="warning">&pound;' + tempArray[3] + ' per week</dd>');
					$("space-save").replace('<dd id="space-save" class="warning">0%</dd>');
				} else {
					$("space-average").replace('<dd id="space-average" class="warning">&pound;' + Math.round((tempArray[3]*offerArray[1])*Math.pow(10,2))/Math.pow(10,2) + ' per week</dd>');
					$("space-save").replace('<dd id="space-save" class="warning">' + Math.round((1-offerArray[1])*100) + '%</dd>');
				}
				$("image-holder").replace('<div id="image-holder"><img src="' + upload + 'images_products_large/' + id + '.jpg" alt="" /></div>');
				$("OPT_ID12").value = offerArray[0];
				$("V_ID").value = tempArray[6];
				$('section4').show();
				$('avail-anim').show();
				$('avail-yes').hide();
				$('section5').hide();
			
				new PeriodicalExecuter( function(pe) {
					$('avail-anim').hide();
					$('avail-yes').show();
					$('section5').show();
					pe.stop();
				}, 3); 
			}
		}
	}
}

var showHide = Class.create();
showHide.prototype = {
	initialize : function(links, holder, location) {
		var loc = document.location.href.match(/#(\w+)/) ? RegExp.$1 : location;
		this.links = links;
		this.holder = holder;
		this.holder.without(loc).each(this.hide.bind(this));
		this.show(loc);
		this.links.each(function(el) {
			var element = el.match(/#(\w.+)/) ? RegExp.$1 : "quote";
			if (loc == element) el.addClassName('selected');
		});
		this.links.each(this.setup.bind(this));
	},
	setup : function(elm) {
		Event.observe(elm, 'click', this.activate.bindAsEventListener(this), false)
	},
	activate : function(ev) {
		var elm = Event.findElement(ev, "a");
		this.holder.without(elm).each(this.hide.bind(this));
		this.show(this.hash(elm));
		elm.addClassName('selected');
		this.links.without(elm).each(function(el) {
			el.removeClassName('selected');
		});
		ev.stop();
	},
	hash : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	hide : function(elm) {
		$(elm).hide();
		//this.links.without(elm).each(removeClassName('selected'));
	},
	show : function(elm) {
		$(elm).show();
	}
}

function runShow(j) {
	Effect.Fade(theSlides[i], { duration:1, from:1.0, to:0.0 });
	theControls[i + 1].removeClassName('selected');
	isNaN(j) ? i++ : i = j
	if (i == theSlides.length) i = 0; 
	Effect.Appear(theSlides[i], { duration:1, from:0.0, to:1.0 });
	theControls[i + 1].addClassName('selected');
	theControls[8].innerHTML = theSlides[i].firstDescendant().alt;
}

function homeSlider(j) {
	Effect.Fade(theSlides[i], { duration:1, from:1.0, to:0.0 });
	theControls[i].down().down(1).hide();
	theControls[i].down().down(0).show();
	isNaN(j) ? i++ : i = j
	if (i == theSlides.length) i = 0; 
	Effect.Appear(theSlides[i], { duration:1, from:0.0, to:1.0 });
	theControls[i].down().down(0).hide();
	theControls[i].down().down(1).show();
}

function loadFlash(ev) {
	var elt = Event.findElement(ev, 'a');
	var theFlash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="750" height="400">';
	theFlash += '<param name="movie" value="' + elt.href + '">';
	theFlash += '<param name="quality" value="high" />';
	theFlash += '<param name="menu" value="false">';
	theFlash += '<param name="wmode" value="transparent">';
	theFlash += '<embed src="' + elt.href + '" width="750" height="400" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" menu="false" wmode="transparent"></embed>';
	theFlash += '</object>';
	
	$('flash-holder').replace('<div id="flash-holder" style="display: none;">' + theFlash + '</div>');
	new Lightbox.base('flash-holder', { closeOnOverlayClick : true, title : elt.title });
	Event.stop(ev);
}

var email_regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
function checkQuote(theForm) {
	var strAlert = "", bolAlert = false;

	if (theForm.first_name.value == "" || theForm.first_name.value == "First Name") {
		strAlert += "\nEnter a value for the \'First Name\' field.";
		bolAlert = true;
	}

	if (theForm.surname.value == "" || theForm.surname.value == "Surname") {
		strAlert += "\nEnter a value for the \'Surname\' field.";
		bolAlert = true;
	}

	if (email_regex.test(theForm.email_address.value) == false) {
		strAlert += "\nEnter a valid e-mail address for the \'E-mail Address\' field.";
		bolAlert = true;
	}

	if (theForm.telephone.value == "" || theForm.telephone.value == "Phone Number") {
		strAlert += "\nEnter a value for the \'Phone Number\' field.";
		bolAlert = true;
	}

	if (bolAlert == true) {
		alert("You have not fully completed the form, please correct the following:\n" + strAlert);
		return false;
	}
}
