/*==============================================================================
 Global JS
 
 @author caphun <caphun at yelotofu dot com>
==============================================================================*/

// IE6 background flicker fix
// See: http://www.mister-pixel.com/#Content__state=is_that_simple
var ishomepage = false;

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

// initialize jQuery
jQuery.noConflict();


// on Dom ready
jQuery(function($){

	var s = 0; // hightinghted section booleen
	var gn = jQuery.makeArray( $('.globalnav ul li a') );
	var n = gn.length;
	
	

	// examine the location & highlight respective section
	$('.globalnav ul li a').each(function() {
		var section = location.href.toString();
		var ahref = this.href.split('/');
		//alert(section + '\n' + ahref )
		var i = ahref.length;
		var x = ahref[i-1]; // NB!! global nav a href must NOT end with "/"
		
		// check all pages EXCEPT homepage
		if ( section.indexOf(x)>0) {
			$(this).parents('li').addClass('selected');
			s++;
		}
		n--;

		// ... and finally nothing was "selected" so it must be homepage section.
		if (n==0 && s==0) $('.globalnav ul li a').parents('li:first').addClass('selected');		
	});

	$('.subnav ul li a:path').each(function() {
		$(this).parents('li').addClass('selected');
	});

	// change the downloads box class
	var current = $('.globalnav ul li.selected a span').html().toLowerCase();
	var section = current.replace(' ', '-').toString();
	
	
	// infinate folley of aqueduct requires these exceptions in the services section
	if (section = 'services') {
		var iam = script_name().toLowerCase(); //jQuery(this).href;

		switch(iam)
		{
			case 'stress-training.html': var section = 'our-team'; break;    
			case 'financial-help.html': var section = 'about-us'; break;
			case 'you-count.html' : var section = 'pink'; break;
		
		}
	}
	// update the active section colour
	$('.right .downloads').addClass( section.toString() );
	
	// random page header ...
	var rand_no = Math.ceil(4*Math.random())
	var root = "/";
	var dloc = document.location.toString().toLowerCase();
	var dhost = 'http://' + document.location.host.toLowerCase();
	if(dloc.indexOf('stage.aquepreview.com') >0) root += 'caba/';
	ishomepage = (dhost += root) == (dloc);
	if (ishomepage) { rand_no = 1; }
	$('.header').css('background-image', 'url('+root+'images/headers/header-'+ rand_no +'.jpg)');
});


function script_name() {
	// just the script name!!!!
	var l = document.location.href.split('/');
	var i = l.length;
	return l[i-1];
	
}


function validate( 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 ...
	obj.submit();
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}



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;
}

