// JavaScript Document
var boxcols = ['#376A6D','#427B7E','#396867','#376772','#3C7588','#37666C','#396867','#497B83','#487F81']; // array of all colours used for each section 0-8 = 1-9
var pages = ['/','/info/2/Location.html','/info/3/Room-Layouts.html','/info/4/Serviced-Offices.html','/info/5/Virtual-Office.html','/info/6/Catering.html','/info/7/Business-Club.html','/info/8/Contact.html','/info/9/Book-Now.html']; // array of all pages 0-8 = 1-9
var error_handler = '/error/test/1';
var updater = 0;
var debug = 0;

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function changepage(evt){
	var child = Event.element(evt); // get event element
	var pgstr = child.id.replace('homebox_', ''); 
	var pgint = parseInt(pgstr) - 1;
	document.location = pages[pgint]; // get relevant page from pages array based on pageid minus 1
}

function myFormat(y,m,d){
	var d = new Date(y, m, d);
	return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate();
}

function resetBoxes(element, index, array) { // resets all navigation items and boxes back to their original colours
	var id = index + 1; // ids start from 1 not 0
	if(id != currentpage){ // only reset if not currently selected item
		if($('homebox_' + id) != null){
			$('homebox_' + id).style.backgroundColor = element; // set background back to original 
			$('homemenu_' + id).style.color = '#77979A'; // make sure not currently selected menu item, leave that one alone.
		}
	}
}

function icoAlert(msg,title){
	if(!title) var title = 'There was a problem';
	showPopWin(msg, title, 200, 200, function(){});
	//alert(msg);
}

function resetAllBoxes(){boxcols.each(resetBoxes);}

function mainmenu(evt){	
	var child = Event.element(evt);
	var matchingbox, matchingtext;
	boxcols.each(resetBoxes); // loop through boxes and reset them
	if(child.id.indexOf('menu') > -1){ // text link clicked
		child.style.color = '#ffffff'; // set active text nav item
		matchingbox = child.id.replace('homemenu', 'homebox');
		$(matchingbox).style.backgroundColor = '#7EB365'; // set active box
	}else{
		matchingtext = child.id.replace('homebox', 'homemenu');
		$(matchingtext).style.color = '#ffffff'; // set active text nav item
		child.style.backgroundColor = '#7EB365'; // set active box
	}
}

function altRows() {
	var col1 = '#FFFFFF'; var col2 = '#E3EDCA'; 
	var rowcol = col1;
	$$('li.swaprow').each(
		function(row){
			rowcol = rowcol == col1 ? col2 : col1;
			row.style.backgroundColor = rowcol;
		}
	);
}

function multiCols() {
	$$('ul.multi li').each(function(li){li.style.color = boxcols[Math.floor(Math.random() * 8 + 1)];});
}

function startUpdater(elid, url, freq){
	if(updater != 0) updater.stop();
		updater = new Ajax.PeriodicalUpdater($(elid),
			url, {
			asynchronous:true,
			frequency: 10,
			onSuccess: function() {
				Effect.Appear(elid, { duration: 4.0 });
				timeout = setTimeout("Effect.Fade('" + elid + "', { duration: 4.0 });", 6000);
			}
		}
	);
}

function getRadioValue(idOrName) {
        var value = null;
        var element = document.getElementById(idOrName);
        var radioGroupName = null;  
        
        // if null, then the id must be the radio group name
        if (element == null) {
                radioGroupName = idOrName;
        } else {
                radioGroupName = element.name;     
        }
        if (radioGroupName == null) {
                return null;
        }
        var radios = document.getElementsByTagName('input');
        for (var i=0; i<radios.length; i++) {
                var input = radios[ i ];    
                if (input.type == 'radio' && input.name == radioGroupName && input.checked) {                          
                        value = input.value;
                        break;
                }
        }
        return value;
}

function delconfirm(evt){
	var del = Event.element(evt);
	var str = del.rel ? '\'' + del.rel + '\'' : 'this item'
	if(confirm('Are you sure you want to delete ' + str + '?')){
		document.location = del.href;
	}else{
		evt.stop();	
	}
}

// Set up events....
Event.observe(window, 'load', page_loaded, false);

function page_loaded(evt) {
	altRows();
	multiCols();
	//if($('form1')) Event.observe('form1', 'submit', function(e){new icoValidate($('form1')).validate(); Event.stop(e);}, false);
	// Add navigation text links to event handler.	
	var eventitems = document._getElementsByClassName('navtext').each(function(navtext){Event.observe(navtext.id, 'mouseout', resetAllBoxes, false); Event.observe(navtext.id, 'mouseover', mainmenu, false);});
	// Add coloured boxes to event handler.
	var smallboxes = document._getElementsByClassName('smallbox').each(function(box){if(currentpage != box.id.replace('homebox_', '')){Event.observe(box.id, 'mouseout', resetAllBoxes, false); Event.observe(box.id, 'mouseover', mainmenu, false); Event.observe(box.id, 'click', changepage, false);}});
	// Add coloured boxes to event handler, these are the boxes on the left, they have different class (smallbox1) due to extra left margin.
	var smallboxes1 = document._getElementsByClassName('smallbox1').each(function(box){if(currentpage != box.id.replace('homebox_', '')){Event.observe(box.id, 'mouseout', resetAllBoxes, false); Event.observe(box.id, 'mouseover', mainmenu, false); Event.observe(box.id, 'click', changepage, false);}});
	document._getElementsByClassName('delete').each(
		function(e){
			try{
				Event.observe(e.id, 'click', delconfirm, false);
			}catch(e){}
		}
	);
	
	document._getElementsByClassName('popup').each(
		function(el){
			Event.observe(el.id, 'click', function(e){
				window.open(el.href, 'pop', 'scrollbars=0,width=400,height=400');
				e.stop();
			}, false);
		},false
	);
	
	resetAllBoxes();
	if($('homebox_' + currentpage)) $('homebox_' + currentpage).style.backgroundColor = '#7EB365'; // set active box
	
	try{
		if($('contact_form')){
			Event.observe('contact_form', 'submit', 
				function(e){
					var $msg = '';
					var $filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,7})+$/;
					if(!$('email') || $('email').value == '' || !$filter.test($('email').value)) $msg+= 'Please enter a valid email address\n';
					if(!$('email') || $('email').value == '') $msg+= 'Please enter your name\n';
					if($msg != ''){
						e.stop();
						//alert($msg);
						showPopWin($msg, 'There was a problem', 200, 200, null, null);
						return false;
					}else{
						return true;	
					}
				}
				,false
			);
		}
	}catch(e){}
	
	if($('confirm_booking')){
		Event.observe('confirm_booking', 'submit', 
			function(e){
				var $msg = '';
				if(!$('ptype_cc').checked && !$('ptype_ac').checked) $msg+= 'Please select a payment method<br />';
				if($msg != ''){
					e.stop();
					//alert($msg);
					showPopWin($msg, 'There was a problem', 200, 200, null, null);
					return false;
				}else{
					return true;	
				}
			}
			,false
		);
	}
}