// alert('master_lib');

/*	$Id: master_lib.js,v 1.5 2004/09/20 20:10:18 smarty Exp $	*/


/* The following is (c) 2003 by Yellowspace SMART Solutions, smart@yellowspace.net */


/*******************************************************
 *******************************************************
 
	BROWSER ABSTRACTION
 
 *******************************************************
 */ function ____BrowserAbstraction________() {} /*
 *******************************************************/	

bAgent = window.navigator.userAgent;
bAppName = window.navigator.appName;
bVerIdx = 1; Browser = '';
// alert(bAgent);

if (bAppName.indexOf("Netscape") >= 0) {
	Browser="Netscape";
	bVerIdx = bAgent.indexOf("Mozilla/") + 8;
}
if (bAppName.indexOf("Explorer") >= 0) {
	Browser="Explorer";
	bVerIdx = bAgent.indexOf("MSIE")+4;
}
if (bAgent.indexOf("Opera") >= 0) {
	Browser="Opera";
	bVerIdx = bAgent.indexOf("Opera")+6;
	
}
if (bAgent.indexOf("Konqueror") >= 0) {
	Browser="Konqueror";
	bVerIdx = bAgent.indexOf("Konqueror/")+10;
}
if (bAgent.indexOf("Safari") >= 0) {
	Browser="Safari";
	bVerIdx = bAgent.indexOf("Safari/") + 7;
}

if (bAgent.indexOf("Mac") >= 0)
	Platform="Mac";
if (bAgent.indexOf("Win") >= 0)
	Platform="Win";

//------------ edit by paolo: changed "bVerIdx+3" into "bVerIdx+4" sun 18 nov 2001 21:25
//------------ reason: was not recognizing explorer 5.5

BrVersion=parseFloat(bAgent.substring(bVerIdx, bVerIdx+4));
// alert(bVerIdx + "z" + bAgent + "x" + bAppName +  "y" + bAgent.substring(bVerIdx, bVerIdx+4));
// alert("Browser="+Browser+" Platform="+Platform+" BrVersion="+BrVersion);

if (BrVersion < 5.0)
	Version="4";
if ((BrVersion >= 5.0) && (BrVersion < 5.5))
	Version="5";
if ((BrVersion >= 5.5) && (BrVersion < 6.0))
	Version="5.5";
if ((BrVersion >= 6.0))
	Version="6gt";
if ((BrVersion >= 51))
	Version="51+";
// alert("Version="+Version);


switch (Browser){
	case "Netscape":
		SmartAgent="Netscape";
		switch (Version){
			case "4":
				SmartAgent="Netscape4";
				break;
			case "5":
			case "5.5":
			case "6gt":
				SmartAgent="Netscape6";
				break;
			default:
		}
	break;
	case "Explorer":
		switch (Version){
			case "4":
			case "5":
				SmartAgent="MSIE5";
				break;
			case "5.5":
			case "6gt":
				SmartAgent="MSIE5.5";
				break;
			default:
		}
	break;
	case "Opera":
		switch (Version){
			case "4":
			case "5":
			case "5.5":
			case "6gt":
			default:
				SmartAgent="Opera5";
		}
		break;
	case "Konqueror":
		switch (Version){
			case "3":
			default:
				SmartAgent="Netscape6";
		}
		break;
	case "Safari":
		switch (Version){
			case "51+":
			default:
				SmartAgent="Khtml";
		}
		break;
	default:
		//alert("unsupported browser type: "+bAgent);
		SmartAgent='';
}

// alert("SmartAgent="+SmartAgent);


/*-------------------------------------------------------------------------------------------
copy this master to make new functions!

function Master() {

		switch (SmartAgent){
			case "Netscape4":
			case "Netscape6":
			case "Khtml":
				break;
			case "MSIE5":
				break;
			case "MSIE5.5":
			case "Opera5":
				break;
			default:
		}
-------------------------------------------------------------------------------------------*/




function SwapImage(ImgName,Url) {

	if(document.images && document.images[ImgName]) {
		document.images[ImgName].src=Url;
	} else if(document.all && document.all[ImgName]) {
		document.all[ImgName].src=Url;
	} else if(document.getElementById && document.getElementById(ImgName)) {
		document.getElementById(ImgName).src=Url;
	}
	
	
	/*
		switch (SmartAgent){
			case "Opera5":	
			case "Netscape4":
				if(document.images[ImgName]) document.images[ImgName].src=Url;
				break;
			case "MSIE5":
				if(document.all[ImgName]) document.all[ImgName].src=Url;
				break;
			default:
				if(document.getElementById(ImgName)) { 
					document.getElementById(ImgName).src=Url;
				}
		}
		*/
}

/*******************************************************
 *******************************************************
 
	IMAGES
 
 *******************************************************
 */ function ____Images________() {} /*
 *******************************************************/	

function PreLoad(imgUrl) {
	PreloadedImage = new Image();
	PreloadedImage.src = imgUrl;
	return PreloadedImage;
}

function SwapImageInLayer(LayerName,ImgName,Url) {

		switch (SmartAgent){
			case "Netscape4":
				if(document.layers[LayerName].document[ImgName]) 
				document.layers[LayerName].document[ImgName].src=Url;
				break;
			default:
				SwapImage(ImgName,Url);
		}

}

function ShowHideLayers(LayerName,On) {
	ManageLayer(LayerName,On);
}

/*******************************************************
 *******************************************************
 
	LAYERS / DIVS
 
 *******************************************************
 */ function ____LayersAndDivs________() {} /*
 *******************************************************/	


function ManageLayer(LayerName,On,Left,Top,Width,Height,ZIndex) {



/*

Notes:
if arguments are not given or false, they are not used, but they have to be in the right order. 
If you want to change only arguments at the end, like width and height, 
you can do it by setting On, Left and Top to false.

*/

		switch (SmartAgent){
				case "Netscape4":
				document.layers[LayerName].visibility = (On) ? "show" : "hide";
				TheOperation = (Left) ? document.layers[LayerName].left = Left : "nogo";
				TheOperation = (Top) ? document.layers[LayerName].top = Top : "nogo";
				TheOperation = (Width) ? document.layers[LayerName].width = Width : "nogo";
				TheOperation = (Height) ? document.layers[LayerName].height = Height : "nogo";
				TheOperation = (ZIndex) ? document.layers[LayerName].zIndex = ZIndex : "nogo";
				break;
				
			case "MSIE5":
			//  explorer 4.5 braucht diesen case
				// alert("MSIE5");
				document.all[LayerName].style.visibility = (On) ? "visible" : "hidden";
				TheOperation = (Left) ? document.all[LayerName].style.pixelLeft = Left : "nogo";
				TheOperation = (Top) ? document.all[LayerName].style.pixelTop = Top : "nogo";
				TheOperation = (Width) ? document.all[LayerName].style.pixelWidth = Width : "nogo";
				TheOperation = (Height) ? document.all[LayerName].style.pixelHeight = Height : "nogo";
				TheOperation = (ZIndex) ? document.all[LayerName].style.zIndex = ZIndex : "nogo";
				break;
				
			case "Netscape6":
			case "Khtml":
			case "MSIE5.5":
			case "Opera5":
			default:
				document.getElementById(LayerName).style.visibility = (On) ? "visible" : "hidden";
				TheOperation = (Left) ? document.getElementById(LayerName).style.left = Left : "nogo";
				TheOperation = (Top) ? document.getElementById(LayerName).style.top = Top : "nogo";
				TheOperation = (Width) ? document.getElementById(LayerName).style.width = Width : "nogo";
				TheOperation = (Height) ? document.getElementById(LayerName).style.height = Height : "nogo";
				TheOperation = (ZIndex) ? document.getElementById(LayerName).style.zIndex = ZIndex : "nogo";
				break;
		}

}


function WriteToLayer(LayerName,Content) {


		switch (SmartAgent){
			case "Netscape4":
				document.layers[LayerName].document.write(Content);
				break;
			case "MSIE5":
				document.all[LayerName].innerHTML=Content;
				break;
			case "Khtml":
			case "Netscape6":
			case "MSIE5.5":
			case "Opera5":
			default:
				document.getElementById(LayerName).innerHTML=Content;
				break;
			
		}

}


function hidden(scroll) { 

/*
don«t show any scrollbar, works for MSIE, and that is where you need it
scroll can be 'hidden', 'auto', 'visible'
Internet Explorer can not use overflow:visible.
*/

	switch (SmartAgent){
		case "Netscape4":
		case "Netscape6":
		case "Khtml":
			break;
		case "MSIE5":
		case "MSIE5.5":
		case "Opera5":
			document.body.style.overflow=scroll; 
			break;
		}
}

/*******************************************************
 *******************************************************
 
	Window and Screen
 
 *******************************************************
 */ function ____WindowAndScreen________() {} /*
 *******************************************************/	

function GetWindowWidth() { 

/*
VORSICHT: die Abfrage funktioniert nur "onload" der Seite - nicht vorher!
VORSICHT: Entgegen dem namen der Funktion wird bei Frames die Breite und Hoehe des Frames ausgespuckt.
*/

	if (self.innerWidth) {
		return self.innerWidth;
	}
	else if(document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	} 
	else if(document.body) {
		return document.body.clientWidth;
	}
	else return 0;
	
	// OLD

	switch (SmartAgent){
		case "Khtml":
		case "Netscape4":
		case "Netscape6":
		case "Opera5":
			my_width = window.innerWidth;
			break;
		case "MSIE5":
		case "MSIE5.5":
			my_width = document.body.clientWidth;
			break;
		default:
			my_width = document.body.clientWidth;
		break;
	}
	return my_width;
}

function GetWindowHeight() { 
/*
VORSICHT: die Abfrage funktioniert nur "onload" der Seite - nicht vorher!
VORSICHT: Entgegen dem namen der Funktion wird bei Frames die Breite und Hoehe des Frames ausgespuckt.
*/


	if(self.innerHeight) {
		return self.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	} 
	else if(document.body) {
		return document.body.clientHeight;
	}
	else return 0;
	
	// OLD
	switch (SmartAgent){
		case "Netscape4":
		case "Netscape6":
		case "Opera5":
		case "Khtml":
			my_height = window.innerHeight;
			break;
		case "MSIE5":
		case "MSIE5.5":
			//my_height = document.body.clientHeight;
			my_height = document.body.offsetHeight;
			break;
		default:
			my_height = document.body.clientHeight;
		break;
	}
	return my_height;
}



function GetScreenInfo() { 

/*
VORSICHT: die Abfrage funktioniert nur "onload" der Seite - nicht vorher!
*/
	var my_screen = new Array();
	switch (SmartAgent) {
		case "Netscape4":
		case "Netscape6":
		case "Opera5":
		case "MSIE5":
		case "MSIE5.5":
		case "Khtml":
		default:
			my_screen['width'] = screen.width;
			my_screen['height'] = screen.height;
			my_screen['availWidth']  = screen.availWidth;
			my_screen['availHeight'] = screen.availHeight;
			my_screen['colorDepth'] = screen.colorDepth;
	}
	return my_screen;
}


/*******************************************************
 *******************************************************
 
	USER SELECTION
	 
 *******************************************************
 */ function ____UserSelection________() {} /*
 *******************************************************/	

// w = window
function smartGetSelection(w,use_whitespace) {
		var mrCheckWhitespace = new RegExp('([ \\n\\t\\v\\r\\f]|%0D|%0A|%20)+', 'g');
		
		var pattern = 'all';
		if(!w) w = window;
		
		if(w
		&& w.getSelection) {
			pattern = escape(w.getSelection());
			if(use_whitespace) pattern = pattern.replace (mrCheckWhitespace,"%20");					
		}
		else if(w
		&& w.document
		&& w.document.getSelection) {
		
			pattern = escape(w.document.getSelection());
			if(use_whitespace) pattern = pattern.replace (mrCheckWhitespace,"%20");		
		} else if(w
		&& w.document
		&& w.document.selection
		&& w.document.selection.createRange
		) {
			TextRange = w.document.selection.createRange();
			pattern = TextRange.text;		
		}

/*
		switch (SmartAgent){
			case "Khtml":
				pattern = 'all'; // fix me!

			//	t=d.selection?d.selection.createRange().text:d.getSelection();
			//	t=getSelection();
			//	alert(t);

				break;
			case "Netscape4":
			case "Netscape6":
				pattern = escape(w.document.getSelection());
				if(use_whitespace) pattern = pattern.replace (mrCheckWhitespace,"%20");
				break;
			case "MSIE5":
				pattern = w.document.getSelection();
				break;
			case "MSIE5.5":
			case "Opera5":
				TextRange = w.document.selection.createRange();
				pattern = TextRange.text;
				break;
			default:
		}
		*/
		//alert('smartGetSelection: ' +pattern);
	return pattern;
}


/*******************************************************
 *******************************************************
 
	STRINGS
 
 *******************************************************
 */ function ____Strings________() {} /*
 *******************************************************/	

function str_pad(subj,len,padStr,padType) {
	rightPad = padType.match('/r/gi');
	if(rightPad) var retString = subj;
	else var retString = '';
	if(subj.length < len) {
		for(var i=0;i<(len - subj.length);i++) {
			retString += padStr;
		}
	}
	if(!rightPad) { retString += subj; }
	return retString;
}

function lpad(subj,len,padStr){
	return str_pad(subj,len,padStr,'L');
}

function rpad(subj,len,padStr){
	return str_pad(subj,len,padStr,'R');
}
