var UA = navigator.userAgent.toLowerCase();
var OS = detectOS();
var BRW = detectBrowser();

//alert('OS: '+OS+' - BRW: '+BRW);

function detectOS() {
	if (checkIt('linux')) return "LINUX";
	else if (checkIt('x11')) return "UNIX";
	else if (checkIt('mac')) return "MAC"
	else if (checkIt('win')) return "WIN"
	else return "UNKNOWN";		
}

function detectBrowser() {
	if (checkIt('msie')) return "IE";
	else if (checkIt('firefox')) return "FIREFOX";
	else if (checkIt('safari')) return "SAFARI";
	else if (checkIt('opera')) return "OPERA";			
	else return "UNKNOWN";		
}

function checkIt(str) {	
	return UA.indexOf(str) + 1; 
}


//
// POPUP WINDOW
// 

function popWin(url,h,w,resize,scrollbar) {
	if (!h) h = 400;
	if (!w) w = 600;
	if (!resize) resize = 1;
	if (!scrollbar) scrollbar = 0;
	l = (screen.width-w)/2; 
	t = (screen.height-h)/2;	
	var winId = window.open(url,"_blank","toolbar=0,scrollbars="+scrollbar+",location=0,status=0,menubar=0,resizable="+resize+",width="+w+",height="+h+",left="+l+",top="+t);	
	winId.focus();	
}


//
// FLASH OBJECT
//

function attachFlashMovie(swfMovie,bgColor,w,h,flashVars) {
	if (!bgColor) bgColor = '#000000';	
	if (!w) w = '100%';
	if (!h) h = '100%';
	if (!flashVars) flashVars = '';
	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', w,
			'height', h,
			'src', swfMovie,
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'noscale',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', swfMovie,
			'bgcolor', bgColor,
			'name', swfMovie,
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', swfMovie,
			'salign', '',
			'FlashVars', flashVars
			); //end AC code
	}	
}	


//
// TOGGLE SHOW / HIDE
//

function toggleContent(id) {
	var obj = document.getElementById(id);
	obj.style.display = (obj.style.display == 'none') ? '' : 'none';
}


//
// TEXT FORMATTING
//

function formatTextField(type,fieldId) {
	if (type == "header")
		formatSelection('h2',fieldId);
	else if (type == "bold")
		formatSelection('b',fieldId);
	else if (type == "italic") 
		formatSelection('i',fieldId);
	else if (type == "underline")
		formatSelection('u',fieldId);
}

function formatSelection(tag,fieldId) {
	var aTag =  '<' + tag + '>';
	var eTag =  '</' + tag + '>';
	var input = document.getElementById(fieldId);
	resetSelection(aTag,eTag,input);
}

function formatLinkSelection(fieldId) {
	var oLnk = document.getElementById('formatLinkURL');
	var aTag =  '<a href="' + oLnk.value + '">';
	var eTag =  '</a>';
	var input = document.getElementById(fieldId);
	resetSelection(aTag,eTag,input);
}

function resetSelection(aTag,eTag,input) {
	input.focus();
	
	// IE
	if	(typeof document.selection != 'undefined') {
		
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = aTag + insText + eTag;
		range = document.selection.createRange();
		
		if	(insText.length == 0) {
			range.move('character', -eTag.length);
		} else {
			range.moveStart('character', aTag.length + insText.length + eTag.length);      
		}
		
		range.select();
	
	}   else  {  
	
	// FF, Chrome
	
	if	(typeof input.selectionStart != 'undefined') {
	
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
	
		var pos;
		if	(insText.length == 0) {
			pos = start + aTag.length;
		} else {
			pos = start + aTag.length + insText.length + eTag.length;
		}
			
		input.selectionStart = pos;
		input.selectionEnd = pos;
	
		}
	}
}
