// Validation
function validate() {
	var name = document.getElementById( "name" );
	var phone = document.getElementById( "phone" );
	var email = document.getElementById( "email" );
	var postcode = document.getElementById( "postcode" );
	
	var sError = "";
	if ( name.value.length == 0 ) {
		sError += "Please enter your contact name.\n";
	}

	// Validate Phone
	if ( phone.value.length == 0 ) {
		sError += "Please enter your telephone number.\n";
	} else {

		var str = phone.value;
		var newphone = str.replace(/(\s|\(|\)|-)/gi, "");
		var patt1 = new RegExp("[0-9]{10}");
		var result = patt1.test(newphone);
		if ( !result ) {
			sError += "Please include the phone number area code.\n";
			phone.select();
		}
	}

	// Validate Email
	if ( email.value.length == 0 ) {
		sError += "Please enter your email address.\n";
	} else {

		var str = email.value;
		var patt1 = new RegExp("^[a-z][a-zA-Z0-9_.-]*@[a-zA-Z0-9_.-]+\.[a-z]{2,4}$");
		var result = patt1.test(str);
		if ( !result ) {
			sError += "The email is not valid.\n";
		}
	}

	// Validate Postcode
	if ( postcode.value.length == 0 ) {
		sError += "Please provide your postcode\n";
	} else {

		var str = postcode.value;
		var newphone = str.replace(/(\s|\(|\)|-)/gi, "");
		var patt1 = new RegExp("[0-9]{4,}");
		var result = patt1.test(newphone);
		if ( !result ) {
			sError += "The postcode is not valid.\n";
		}
	}

	if ( sError.length == 0 ) {
		return true;
	}
	else {
		alert( sError );
		return false;
	}
}

function validate_book() {
	var name = document.getElementById( "name_1" );
	var phone = document.getElementById( "phone_1" );
	var email = document.getElementById( "email_1" );
	var postcode = document.getElementById( "postcode_1" );
	
	var sError = "";
	if ( name.value.length == 0 ) {
		sError += "Please enter your contact name.\n";
	}

	// Validate Phone
	if ( phone.value.length == 0 ) {
		sError += "Please enter your telephone number.\n";
	} else {

		var str = phone.value;
		var newphone = str.replace(/(\s|\(|\)|-)/gi, "");
		var patt1 = new RegExp("[0-9]{10}");
		var result = patt1.test(newphone);
		if ( !result ) {
			sError += "Please include the phone number area code.\n";
			phone.select();
		}
	}

	// Validate Email
	if ( email.value.length == 0 ) {
		sError += "Please enter your email address.\n";
	} else {

		var str = email.value;
		var patt1 = new RegExp("^[a-z][a-zA-Z0-9_.-]*@[a-zA-Z0-9_.-]+\.[a-z]{2,4}$");
		var result = patt1.test(str);
		if ( !result ) {
			sError += "The email is not valid.\n";
		}
	}

	// Validate Postcode
	if ( postcode.value.length == 0 ) {
		sError += "Please provide your postcode\n";
	} else {

		var str = postcode.value;
		var newphone = str.replace(/(\s|\(|\)|-)/gi, "");
		var patt1 = new RegExp("[0-9]{4,}");
		var result = patt1.test(newphone);
		if ( !result ) {
			sError += "The postcode is not valid.\n";
		}
	}

	if ( sError.length == 0 ) {
		return true;
	}
	else {
		alert( sError );
		return false;
	}
}

// Resize Menus
function size_menu() {
	// top menu numbers
	top_num = $$("div.hor_menu li").size();
	top_w = Math.floor(582/top_num)-1;
	top_last = 1 + top_w + 582%top_num;

	// botom menu numbers
	bottom_num = $$("div.bottom li").size();
	bottom_w = Math.floor(560/bottom_num)-1;
	bottom_last = bottom_w + 560%bottom_num;

	// top menu
	$$("div.hor_menu li").each(function(n) {
		n.setStyle({
		  width: top_w+'px',
		  textAlign: 'center'
		});

	});

	// bottom menu
	$$("div.bottom li").each(function(n) {
		n.setStyle({
		  width: bottom_w+'px',
		  textAlign: 'center'
		});

	});

	$$("div.hor_menu li a, div.bottom li a").each(function(n) {
		n.setStyle({
		  width: '100%'
		});

	});

	// top
	$$("div.hor_menu li").last().setStyle({	  width: top_last+'px', border: 'none'	});
	// bottom
	$$("div.bottom li").last().setStyle({	  width: bottom_last+'px', border: 'none'	});

}

function load_slider() {

	if($('slider')) {

		$('slider').innerHTML='\
\
		<div class="scroller">\
			<div class="gcontent">\
				<div class="section" id="bed-linen">\
					<a href="bed-linen"><img src="images/slider/bed-linen.jpg" alt="Bed Linen"></a>\
				</div>\
				<div class="section" id="quilts">\
					<a href="quilts"><img src="images/slider/quilts.jpg" alt="Quilts"></a>\
				</div>\
				<div class="section" id="throws">\
					<a href="blankets-throws"><img src="images/slider/throws.jpg" alt="Blankets &amp; Throws"></a>\
				</div>\
				<div class="section" id="cushions">\
					<a href="cushions"><img src="images/slider/cushions.jpg" alt="Cushions"></a>\
				</div>\
				<div class="section" id="table-linen">\
					<a href="table-linen"><img src="images/slider/table-linen.jpg" alt="Table Linen"></a>\
				</div>\
				<div class="section" id="hand-towels">\
					<a href="guest-kitchen-towels"><img src="images/slider/hand-towels.jpg" alt="Hand &amp; Kitchen Towels"></a>\
				</div>\
				<div class="section" id="cool-galah">\
					<a href="cool-galah-towels"><img src="images/slider/cool-galah.jpg" alt="Cool Galah Towels"></a>\
				</div>\
			</div>\
		</div>\
		<div class="controls">\
			<a href="#bed-linen" class="active">1</a>\
			<a href="#quilts">2</a>\
			<a href="#throws">3</a>\
			<a href="#cushions">4</a>\
			<a href="#table-linen">5</a>\
			<a href="#hand-towels">6</a>\
			<a href="#cool-galah">7</a>\
		</div>\
		';
	}
}

/* Google Map Functions */
function reset_box() {
	boxtext = '<img id="close" src="images/lightbox/mf_close.gif" onClick="MfLightbox.hideBox()" alt="Close" title="Close this window" />';
	boxtext += '<div id="boxContents"></div>';
	$('box').update(boxtext);
}

function gmap() {
  if (GBrowserIsCompatible()) {

	var body = document.getElementById('box');
	var map = new GMap2(body);
	map.addControl(new GMapTypeControl());
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(-37.7950, 145.0000), 12);

	// Create our "tiny" marker icon
	var icon = new GIcon();
	icon.image = "http://www.google.com/mapfiles/ms/icons/red-pushpin.png";
	icon.shadow = "http://www.google.com/mapfiles/ms/icons/pushpin_shadow.png";
	icon.iconSize = new GSize(32, 32);
	icon.shadowSize = new GSize(59, 32);
	icon.iconAnchor = new GPoint(10, 32);
	icon.infoWindowAnchor = new GPoint(16, 1);


	// Creates a marker whose info window displays the given info
	function createMarker(point, info) {
	  // Create a lettered icon for this point using our icon class

	  var marker = new GMarker(point, icon);

	  GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(info);
	  });
	  return marker;
	}

	// St Kilda Road Towers
	map.addOverlay(createMarker(new GLatLng(-37.835920,144.973729), "<div align=\"left\"><B>St Kilda Road Towers</B><BR>1 Queens Road, <BR>Melbourne</div>"));

	// Collins Street Tower
	map.addOverlay(createMarker(new GLatLng(-37.817717,144.958549), "<div align=\"left\"><B>Collins Street Tower</B><BR>480 Collins Street, <BR>Melbourne</div>"));

	// Exchange Tower
	map.addOverlay(createMarker(new GLatLng(-37.816929,144.957154), "<div align=\"left\"><B></B>Exchange Tower<BR>530 Lt Collins Street, <BR>Melbourne</div>"));

	// The Jewel Business Centre
	map.addOverlay(createMarker(new GLatLng(-37.846918,144.978933), "<div align=\"left\"><B>The Jewel Business Centre</B><BR>566 St Kilda Road, <BR>Melbourne</div>"));

	// Toorak Business Centre
	map.addOverlay(createMarker(new GLatLng(-37.846198,145.038800), "<div align=\"left\"><B>Toorak Business Centre</B><BR>23 Milton Parade, <BR>Malvern</div>"));

	// 89 Business Centre
	map.addOverlay(createMarker(new GLatLng(-37.807927,145.028221), "<div align=\"left\"><B>Kew Junction Business Centre</B><BR>Level 1, 89 High Street, <BR>Kew</div>"));

	// 250 Bell Street
	map.addOverlay(createMarker(new GLatLng(-37.746015,145.01232099999999), "<div align=\"left\"><B>Bell City</B><BR>215 Bell Street, <BR>Preston</div>"));

  }

	var x='<img id="close" src="images/lightbox/mf_close.gif" onClick="MfLightbox.hideBox(); reset_box();" alt="Close" title="Close this window" style="position: absolute; right:0; top:0; z-index: 10000000;"/>';	
	new Insertion.Top($('box'), x);

}

/* ---------- */


Event.observe(window, 'load', function() {
	//size_menu();
	// slider
	if($('slider')) {
		//load_slider();
		my_glider = new Glider('slider', {duration:0.5, autoGlide:true, frequency: 6});
	}

	// initialise mf_lightbox
	try {
		MfLightbox.init();
	} catch (e) {
		Event.stop(e);
		// zzz
	}

	// tab boxes
	if($('tnav')) {
		boxes = $$('div.main div.tab');
		boxes.invoke('hide');
		boxes.first().show();
		$$('#tnav a').first().toggleClassName('active');

		$$('#tnav a').each(function(n, index) {

			Event.observe(n, 'click', function(event) {
				n.blur();
				Event.stop(event);

				// change active 
				$$('#tnav a').invoke('removeClassName', 'active');
				n.toggleClassName('active');

				//show corresponding box
				boxes.invoke('hide');
				boxes[index].show();
			});
		});
	}

	// Enquire Form
	$$('#enquire_options input').each(function(n, index) {
		Event.observe(n, 'click', function(event) {

			// if Serviced Offices or Boardroom Hire is selected
			if( n.checked && (n.value=="Serviced Offices" || n.value=="Boardroom Hire") ) {
				$('options').update('\
							<table border="0" cellspacing="0" cellpadding="0">\
							  <tr>\
								<td colspan="2">Location:</td>\
							  </tr>\
\
							  <tr>\
								<td colspan="2">\
									<select name="location">\
										<option value="Collins Street Tower">Collins Street Tower</option>\
										<option value="Exchange Tower">Exchange Tower</option>\
										<option value="St Kilda Road Towers">St Kilda Road Towers</option>\
										<option value="The Jewel">The Jewel</option>\
										<option value="Toorak Business Centre">Toorak Business Centre</option>\
										<option value="Kew Business Centre">Kew Business Centre</option>\
										<option value="Bell City">Bell City</option>\
									</select>\
								</td>\
							  </tr>\
\
							  <tr>\
								<td width="90px">\
									No. of People:\
								</td>\
								<td>\
									<select name="people">\
										<option value="1-5">1-5</option>\
										<option value="6-10">6-10</option>\
										<option value="10-20">10-20</option>\
										<option value="20+">20+</option>\
									</select>\
								</td>\
							  </tr>\
\
							  <tr>\
								<td colspan="2">\
										<input type="image" src="images/submit_button.gif" value="Submit" title="Submit" />\
										<input name="mode" type="hidden" value="submit" />\
										<input name="service" type="hidden" id="service" value="" />\
								</td>\
							  </tr>\
							</table>\
				');
			}

			// if Virtual Offices is selected
			if( n.checked && (n.value=="Virtual Offices") ) {
				$('options').update('\
							<table border="0" cellspacing="0" cellpadding="0">\
							  <tr>\
								<td colspan="2">\
									<BR>\
									Preferred Business Address:\
								</td>\
							  </tr>\
\
							  <tr>\
								<td colspan="2">\
									<select name="location">\
										<option value="480 Collins Street Melbourne" selected>480 Collins Street Melbourne</option>\
										<option value="530 Lt Collins Street Melbourne">530 Lt Collins Street Melbourne</option>\
										<option value="1 Queens Road Melbourne">1 Queens Road Melbourne</option>\
										<option value="566 St Kilda Road Melbourne">566 St Kilda Road Melbourne</option>\
										<option value="215 Bell Street, Preston">215 Bell Street, Preston</option>\
										<option value="23 Milton Parade Malvern">23 Milton Parade Malvern</option>\
										<option value="89 High Street Kew">89 High Street Kew</option>\
									</select>\
								</td>\
							  </tr>\
\
							  <tr>\
								<td colspan="2">\
										<input type="image" src="images/submit_button.gif" value="Submit" title="Submit" />\
										<input name="mode" type="hidden" value="submit" />\
										<input name="service" type="hidden" id="service" value="" />\
								</td>\
							  </tr>\
							</table>\
				');
			}

			$('service').value=n.value;

		});
	});
	// ------------------------------------------------------------------------


	// Emails
	$$('span.email_enq').each(function(n){
		n.update('<a href="mailto:julia.ci'+''+'pullo@apbc.com.au?subject='+n.readAttribute('title')+'">Email Enquiry</a>');
	});


});