/* some global menu options */
menu_filter = '';
menu_position_mod_x = -4;
menu_position_mod_y = -8;
menu_fadeout = false; // no thanks
hideTime = 0;

function correctPNG( imageId, sizingMethod ) // correctly handle PNG transparency in Win IE 5.5 , 6 & 7.
{
	if ( typeof sizingMethod == 'undefined' ) {
		sizingMethod = 'scale'
	}
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
	{
		var img = document.getElementById( imageId );
		var imgName = img.src.toUpperCase()
		var imgID = (img.id) ? "id='" + img.id + "' " : "";
		var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		var imgStyle = "display:inline-block;" + img.style.cssText 
		if (img.align == "left") imgStyle = "float:left;" + imgStyle
		if (img.align == "right") imgStyle = "float:right;" + imgStyle
		if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		var strNewHTML = "<span " + imgID + imgClass + imgTitle
		+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		+ "(src=\'" + img.src + "\', sizingMethod='" + sizingMethod + "');\"></span>" 
		img.outerHTML = strNewHTML
	}    
}

function correctPNGBackground( divId, imgURL, sizingMethod ) // correctly handle PNG transparency in Win IE 5.5 , 6 & 7.
{
	if ( typeof sizingMethod == 'undefined' ) {
		sizingMethod = 'scale'
	}
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
	{
		var div = document.getElementById( divId );
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgURL+"', sizingMethod='" + sizingMethod + "')";
		div.style.background = '';
	}
}

function doMenu( title, status ) { 
	if( status == 'off' ) {
		// set the styles on the main item
		document.getElementById('menu_' + title ).className = '';
	} else {
		// set the styles on the main item
		document.getElementById('menu_' + title ).className = 'navOn';
	}
}

function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		document.getElementById( passwordField ).style.display = '';
		document.getElementById( passwordField+'_text' ).style.display = 'none';
		document.getElementById( passwordField ).focus();
	} else {
		if( input.value == defaultText ) {
				input.value = '';
		}
	}
}

function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			document.getElementById( passwordField ).style.display = 'none';
			document.getElementById( passwordField+'_text' ).style.display = '';
		} else {
			input.value = defaultText;
		}
	}
}

function checkform( fields, field_descriptions ) {
	for( var i = 0; i < fields.length; i++ ) {
		if( document.getElementById(fields[i]) && document.getElementById(fields[i]).value == 0 ) {
			alert( 'Please enter a value for ' + field_descriptions[i] );
			return false;
		}
	}
	
	return true;
}

function validateForm() {

	// holds an array of errors to output
	var fieldErrorArray = new Array();
	
	// private accessor methods
	this.displayBusinessErrors = displayBusinessErrors;
	this.checkRadio = checkRadio;
	this.checkChecked = checkChecked;
	this.checkText = checkText;
	this.checkTextMinLength = checkTextMinLength;
	this.checkNumeric = checkNumeric;
	this.checkPrice = checkPrice;
	this.checkListBoxHasItems = checkListBoxHasItems;
	this.checkSelect = checkSelect;
	this.validateEmailAddress = validateEmailAddress;
	this.validatePostCode = validatePostCode;
	this.displayErrors = displayErrors;
	this.numberOfErrors = numberOfErrors;
	this.addCustomError = addCustomError;
	
	// checks that one has been checked (may need altering)
	function checkRadio( element, length, output ) {
	
		var hasChecked = false;
		for (i = 0; i < length; i++){
			if (document.getElementById( element + '[' + i + ']').checked) {
		      hasChecked = true;
		   }
		}
		if(!hasChecked){
			fieldErrorArray.push(output);
		}
	}
	
	function checkChecked( element, output ) {
		if (!document.getElementById( element ).checked) {
			fieldErrorArray.push(output);
		}
	}
	
	// ensures text has been entered
	function checkText ( element, output ) {
		if (document.getElementById( element ).value == '') {
			fieldErrorArray.push(output);
		}
	}
	
	// ensures text of certain length has been entered
	function checkTextMinLength ( element, output, charLength ) {
		var checkString = document.getElementById( element ).value;
		if( checkString.length < charLength ){
			fieldErrorArray.push(output);
		}
	}
	
	function checkNumeric ( element, output ){
		var data = document.getElementById( element ).value;
		var ValidChars = "0123456789.";
		var IsNumber = true;
		var Char;
		if( data.length > 0 ) {
			for (i = 0; i < data.length && IsNumber == true; i++){ 
				Char = data.charAt(i); 
				if (ValidChars.indexOf(Char) == -1){
					IsNumber = false;
				}
			}
			if( !IsNumber ){
				fieldErrorArray.push( output + ' must be a numeric value' );
			}
		} else {
			// no value entered
			fieldErrorArray.push( output );
		}
	}
	
	function checkPrice ( element, output  ){
		var data = document.getElementById( element ).value;
		var ValidChars = "0123456789.,";
		var IsNumber = true;
		var Char;
		if( data.length > 0 ) {
			for (i = 0; i < data.length && IsNumber == true; i++){ 
				Char = data.charAt(i); 
				if (ValidChars.indexOf(Char) == -1){
					IsNumber = false;
				}
			}
			if( !IsNumber ){
				fieldErrorArray.push( output + ' must be a price' );
			}
		} else {
			// no value entered
			fieldErrorArray.push( output );
		}
	}
	
	
	// checks for items in the passed listbox
	function checkListBoxHasItems ( element, output ) {
		if( document.getElementById( element ).length <= 0) {
			fieldErrorArray.push(output);
		}
	}
	
	// checks the value chosen is not the passed default value
	function checkSelect ( element, nothingValue, output ){
		if( document.getElementById( element ).value == nothingValue ){
			fieldErrorArray.push(output);
		}
	}
	
	// checks the value chosen is not the passed default value
	function validateEmailAddress ( emailAddress, output ){
		if( document.getElementById( emailAddress ) )
			emailAddress = document.getElementById( emailAddress ).value;
		if( typeof( output ) == 'undefined' )
			output = 'Email address is invalid';
		var filter  = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if ( !filter.test( emailAddress )){
			fieldErrorArray.push( output );
		}
	}
	
	// checks the value chosen is not the passed default value
	function validatePostCode ( element, output ){
		var filter  = /^[A-Za-z]{1,2}[0-9]{1}[A-Za-z0-9]{0,1}[ ]?[0-9]{1}[A-Za-z]{2}$/;
		if ( !filter.test( document.getElementById( element ).value ) ){
			fieldErrorArray.push( output );
		}
	}
	
	
	// add a custom error to the array
	function addCustomError( errorMessage ){
		
		fieldErrorArray.push( errorMessage );
		
	}
	
	// returns the number of errors found
	function numberOfErrors(){
		
		return fieldErrorArray.length;
		
	}
	
	// alerts all current errors to the user	
	function displayErrors(){
		var output = '';
		for(i=0;i<fieldErrorArray.length;i++){
			output += ' - ' +fieldErrorArray[i] + '\n';
		}
		alert('The following fields have not been completed:\n\n' + output);
	}
	
	// alerts all current errors to the user	
	function displayBusinessErrors(){
		var output = '';
		for(i=0;i<fieldErrorArray.length;i++){
			output += ' - ' +fieldErrorArray[i] + '\n';
		}
		alert( 'You must complete the following Reporting Period information before calculating offsets:\n\n' + output);
	}
}

function isNumeric ( data ){
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	if( data.length > 0 ) {
		for (i = 0; i < data.length && IsNumber == true; i++){ 
			Char = data.charAt(i); 
			if (ValidChars.indexOf(Char) == -1){
				IsNumber = false;
			}
		}
		if( !IsNumber ){
			return false;
		}
	} else {
		// no value entered
		return false;
	}
	// must be numeric
	return true;
}

function round(number,X) {
	// rounds number to X decimal places, defaults to 2
    X = (!X ? 2 : X);
    var myNumber = Math.round(number*Math.pow(10,X))/Math.pow(10,X);
    return myNumber.toFixed(X);
}

String.prototype.toCapitalCase = function() {
	var re = /\s/;
	var words = this.split(re);
	re = /(\S)(\S+)/;
	for (i = words.length - 1; i >= 0; i--) {
		re.exec(words[i]);
		words[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
	}
	return words.join(' ');
} 

function doAlert( text ) {
	alert( text );
}

function strReplace( str, find, replace ) {
	while ( str.indexOf( find ) != -1 ) {
		str = str.replace( find, replace );
	}
	return str;
}
