/*==============================================================================
 Global JS
 
 @author caphun <caphun at yelotofu dot com>
==============================================================================*/

// IE6 background flicker fix
// See: http://www.mister-pixel.com/#Content__state=is_that_simple

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


jQuery.noConflict();

// on Dom ready
jQuery(function($){
	
	if ($.browser.msie){
		// 
		if ($.browser.version < 7) {
			$('.globalnav ul li a').each(function(){
				// insert the curve image (avoid BG flicker IE6 use images)
				$(this).children('span')[0].innerHTML = '<img src="/images/tabs/left.gif">';
				$(this).children('span')[2].innerHTML = '<img src="/images/tabs/right.gif">';
				// turn off BG images
				$(this).children('span').addClass('ie');
			});
		}
	}
				
		
	$('.subnav ul ul li a').each(function(){
		if (this.href == location.href) {
			// $(this).parents('ul:first').slideDown('fast').prev().addClass('selected');
			$(this).parents('li').addClass('selected');
		}
	});

	$('.subnav ul ul ul li a').each(function(){
		if (this.href == location.href) {
			// $(this).parents('ul:first').slideDown('fast').prev().addClass('selected');
			$(this).parents('ul li').addClass('selected');
			$(this).addClass('selected');
		}
	});
	

});

function region( s_region ) {
	// handle on and off mouse states & default image state.
	if ( s_region == '' ) {
		// restore original default image (looking at location string)
		var l = document.location.href.split('/');
		var n = l.length;
		var x = l[n-2];
		switch(x)
		{
		case 'north_east': s_region = 'ne'; break;
		case 'central': s_region = 'c'; break;
		case 'east': s_region = 'e'; break;
		case 'east_midlands': s_region = 'em'; break;
		case 'north_west': s_region = 'nw'; break;
		case 'north_east': s_region = 'ne'; break;
		case 'south_east': s_region = 'se'; break;
		case 'south_west': s_region = 'sw'; break;
		case 'wales': s_region = 'wc'; break;
		default: s_region = 'uk'; break;
		}
	}
	// replace the image with the rollover or default.
//	document.uk.src = '/images/map/'+ s_region +'.gif';
}


function StaffLogin() {
	// alert('staff login');
	document.login_form.submit();
}


var history_name = 0;
function Clean( frmelem ) {
	var fe = frmelem.value;
	if ( history_name == 0 ) frmelem.value = '';
	history_name ++;
}

var history_pwd = 0;
function Password( frmelem ) {
	// change form type to password.
	try {
		frmelem.type = 'password'; // IE does not like this :(
	} catch(err) {}

	var fe = frmelem.value;
	if ( history_pwd == 0 ) {
		frmelem.value = '';
		
	}
	history_pwd ++;
	
}



// element name, rule, is valid
/*
var notNulls = new Array (
	['name', 'text', false],
	['company', 'text', false],
	['email', 'email', false],
	['telephone', 'text', false]
);
*/

function validate( obj ) {
	var formready = checkFormFileds( obj );
	var isdoc = true;

	// the application form requires a file type DOC
	if (obj.name == 'application') {
		var filename = document.application.file.value.toLowerCase().split('.');
		var i = filename.length;
		var isdoc = ( filename[ i-1 ]=='doc' || filename[ i-1 ]=='docx');
	
		if (!isdoc) getElem( 'illegal' ).style.display = 'block';
	}

	if ( isdoc && formready ) obj.submit();
	return false;

}

function checkFormFileds( obj ) {
/*
var frmVal = obj.value; 
	var name = obj.name;
*/	
	var bConfirmed = false;
	var err = 0;


	for(var r=0; r<notNulls.length;r++) {
		var name = notNulls[r][0];


		if (obj.elements[ name ]) {
			// alert(obj.elements[ frmelename ].type);
			var frmVal = obj.elements[ name ].value;

			var FieldType = notNulls[r][1] 
			switch (FieldType) {
					case "text" : 
						bConfirmed = (frmVal.length>0);
						break; 

					case "email" :
						// this does not take into account CCed emails i.e. contains ";"
						var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						bConfirmed = filter.test(frmVal);
						break;
			}
			
			if (!bConfirmed) { 
				getElem( name ).style.color = "#cc0000";
				notNulls[r][2]=false;
				err ++;
			} else {
				getElem( name ).style.color = "#666666";
			}
		}
	}
	if (err >0) return false;
	
	
	// form valid ...
	return true;
	// obj.submit();
}






function getElem( strLayerID ) {
	// returns the style element
	var whichLayer = strLayerID;
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	return elem;
}


