// DL10Base.js
//
// Copyright © 2007 DL10 Design Limited. All Rights Reserved.
//
// 1 March 2007

//
// Global Variables
//

var currentRow = null;
var originalClass = null;
var currentID = null;

//
// Base Functions
//

// Browser Detect Functions

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// Printing Functions

function printPage( printFormatedPage ) {
	
	var display_settings = "toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=650"
	
	var docPrint = window.open( "", "_blank", display_settings );
	docPrint.location = printFormatedPage;
	docPrint.focus();
	
}

// Validation Functions

function TD_findObj( n, d )
{
	
	var x;
	
	if( !d )
	{
		
		d = document;
	
	}
	
	x = d.getElementById( n );
	
	return x;

}

function TD_validateForm() {
	
	var i, p, q, nm, test, num, min, max, errors = '', args = TD_validateForm.arguments;

	for (i=0; i<(args.length-2); i+=3) {
		
		test=args[i+2];
		val=TD_findObj( args[i] );
		
		if ( val ) {
			
			nm=val.name;
			
			if ( ( val=val.value ) != "" ) {
				
				if ( test.indexOf( 'isEmail' ) != -1 ) {
					
					p=val.indexOf( '@' );
					if ( p < 1 || p == ( val.length - 1 ) ) {
						
						errors += '- ' + nm + ' must contain an e-mail address.\n';
					
					}
				
				} else if ( test != 'R' ) {
					
					num = parseFloat( val );
					
					if ( isNaN( val ) ) {
						
						errors += '- ' + nm + ' must contain a number.\n';
					
					}
					if ( test.indexOf( 'inRange' ) != -1 ) {
						
						p = test.indexOf( ':' );
						min = test.substring( 8, p );
						max = test.substring( p + 1 );
						
						if ( num < min || max < num ) {
							
							errors += '- ' + nm + ' must contain a number between ' + min + ' and ' + max + '.\n';
							
						}
					
					}
					
				}
				
			} else if ( test.charAt( 0 ) == 'R' ) {
				
				errors += '- ' + nm + ' is required.\n';
			
			}
			
		}
		
	}
	
	if ( errors ) {
		
		alert( 'The following error(s) occurred:\n' + errors );
		
	}
	 
	 return ( errors == '' );
	
}

// List Functions

function selectRow( element, id )
{
	
	if ( currentRow != null )
	{
		
		changeCSSClassTo( currentRow, originalClass );
	
	}

	currentID = id;
	originalClass = element.className;
	currentRow = element;
	
	changeCSSClassTo( element, "nav_row_selected" );

}

// Layer Visibility

function showLayer( elementName )
{

	changeCSSVisibilityTo( document.getElementById( elementName ), "visible" );
	
}

function showLayerInherit( elementName )
{

	changeCSSVisibilityTo( document.getElementById( elementName ), "inherit" );
	
}

function hideLayer( elementName )
{

	changeCSSVisibilityTo( document.getElementById( elementName ), "hidden" );

}

// Layer Display

function displayLayer( elementName )
{

	changeCSSDisplayTo( document.getElementById( elementName ), "inline" );
	
}

function removeLayer( elementName )
{

	changeCSSDisplayTo( document.getElementById( elementName ), "none" );
	
}


// Page Loading

function loadPage( newURL )
{

	document.location = newURL;
	
}

function loadExternalPage( newURL, windowName )
{
	
	if ( window != "" )
	{
	
		var windowHandle = window.open( 'http://' + newURL,windowName,'' );
	
	}
	else
	{
	
		document.location = "http://" + newURL;
	
	}
	
}

//
// CSS Functions
//

function changeCSSClassTo( element, newClassName )
{

	if ( element != null )
	{

		element.className = newClassName;
	
	}

}

function changeCSSImageTo( element, newImageURL )
{

	if ( element != null )
	{
	
		element.style.backgroundImage = "url( '" + newImageURL + "' )";
	
	}

}

function changeCSSColourTo( element, newColour )
{

	if ( element != null )
	{
	
		element.style.color = newColour;
	
	}

}

function changeCSSVisibilityTo( element, newVisibility )
{

	if ( element != null )
	{
	
		if ( element.style.visibility != newVisibility )
		{
		
			element.style.visibility = newVisibility;
		
		}
	
	}

}

function changeCSSDisplayTo( element, newDisplay )
{

	if ( element != null )
	{
	
		if ( element.style.display != newDisplay )
		{
		
			element.style.display = newDisplay;
		
		}
	
	}

}

//
// Element Modification Functions
//

function changeTextTo( element, newText )
{

	if ( element != null )
	{
	
		element.innerHTML = newText;
	
	}

}

function changeImageTo( element, newImageURL )
{

	if ( element != null )
	{
	
		element.src = newImageURL;
	
	}

}
