
function createTooltip(obj)
{
	$('#'+obj).easyTooltip();
}

function openModal(htm, w, h)
{
	$(window).keydown(function(e){
		if( e.which == 27 )
			$.modal.close();
	});

	$.modal(htm, {
		containerCss: {
			height: h,
			width: w,
			backgroundColor: '#fff',
			border: '3px solid #ccc'
		}
	});
}

function closeModal()
{
	$.modal.close();
}

function findParent(curr, tagName)
{
	while (curr != null && curr.tagName != tagName)
		curr = curr.parentNode;
		
	return curr;
}

function findNext(curr, tagName)
{
	if (curr == null)
		return null;

	if (curr.tagName == tagName)
		return curr;

	for(i=0; i < curr.childNodes.length; i++) // && curr.childNodes.item(i)
	{
		var ret = findNext(curr.childNodes.item(i), tagName);
		if (ret != null)
			return ret;
	}
	
	return findNext(curr.nextSibling, tagName);
}

function inBetween(str, start, end)
{
	var i=0;
	if (start != null && start != "")
	{
		i = str.indexOf(start);
		if (i==-1)
			return "";
	}
	else
		start = "";

	var x=str.indexOf(end, i+start.length);
	if (x==-1 || end==null || end=="")
		return str.substr(i+start.length);
	
	return str.substr(i+start.length, x-i-start.length);
}

function GetParentXOffset(obj)
{
	var offset = 0;

	while(obj != null) {
		offset += obj.offsetLeft;
		obj = obj.offsetParent;
	}

	return offset;
}

function GetParentYOffset(obj)
{
	var offset = 0;

	while(obj != null) {
		offset += obj.offsetTop;
		obj = obj.offsetParent;
	}

	return offset;
}

function getElement(aID)
{
	return (document.getElementById) ? document.getElementById(aID) : document.all[aID];
}

function AjaxGetData(url)
{
	var xmlhttp = GetXMLHttpRequestObj();
			
	xmlhttp.onreadystatechange = function() { AjaxGetDataReadyHandler(xmlhttp); };
	xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(null);
}

function AjaxGetDataReadyHandler(obj)
{
	if (obj.readyState == 4)
	{
		if (obj.status == 200)
		{
			if (obj.responseText == "")
			{
//				alert("ajax error : responseText = null");
				return;
			}

			eval(obj.responseText);
		}
		else
		{
//			alert("ajax error : "+obj.status);
		}
	}
}

function convertEventToIE(aEvent)
{
	var myEvent = aEvent ? aEvent : window.event; 

	if (!myEvent.srcElement)
		myEvent.srcElement = myEvent.target;

	try
	{
		if (!myEvent.fromElement)
			myEvent.fromElement = myEvent.currentTarget;
	}
	catch(ex)
	{}
	
	return myEvent;
}

function GetXMLHttpRequestObj()
{
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
		return new XMLHttpRequest()
	// code for IE
	else if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP")
		
	return null;
}

function inlineLabelEdit(objid, data)
{
	this.obj = getElement(objid);
	this.data = data;
	this.inputObj = null;

	this.Init = function(objid, data)
	{
		this.obj.thethis = this;
		this.obj.onmouseover = function() { this.thethis.onMouseOver(); };
		this.obj.onmouseout = function() { this.thethis.onMouseOut(); };
		this.obj.onclick = function() { this.thethis.onClick(); };
		this.obj.title = "Click to Edit";
	}
	
	this.onMouseOver = function()
	{
		this.obj.style.backgroundColor='beige';
	}
		
	this.onMouseOut = function()
	{
		this.obj.style.backgroundColor='white';
	}

	this.onClick = function()
	{
		var inp = document.createElement('INPUT');
		this.inputObj = inp;
		inp.style.position='absolute';
		inp.style.left = GetParentXOffset(this.obj);
		inp.style.top = GetParentYOffset(this.obj);
		inp.style.width = this.obj.style.width;
		inp.style.height = this.obj.style.height;
		inp.style.backgroundColor='beige';
		inp.value = this.obj.innerHTML;
//		inp.style.border='1 solid black'
//		inp.style.padding=1;
		inp.thethis = this;
		inp.onmouseout = function() { this.thethis.onMouseOut(); };
		
		inp.style.visibility = 'visible';

		document.body.insertBefore(inp, document.body.firstChild);
	}

	this.Init(objid, data);
}

function isiPhone() {
	return navigator.userAgent.indexOf('iPod; U; CPU') > -1 || navigator.userAgent.indexOf('iPhone; U; CPU') > -1 || navigator.userAgent.indexOf('iPad; U; CPU') > -1;
}

var _cp_flash = "";
function gotFlash(major, minor, build) {
	if (major == null) major = 0;
	if (minor == null) minor = 0;
	if (build == null) build = 0;

	function getFlashVersion(desc) {
		_cp_flash = desc;
		var matches = desc.match(/[\d]+/g);
		matches.length = 3;  // To standardize IE vs FF
		return matches; //.join('.');
	}

	var hasFlash = false;
	var flashVersion;

	if (navigator.plugins && navigator.plugins.length) {
		var plugin = navigator.plugins['Shockwave Flash'];
		if (plugin) {
			hasFlash = true;
			if (plugin.description) {
				flashVersion = getFlashVersion(plugin.description);
			}
		}

		if (navigator.plugins['Shockwave Flash 2.0']) {
			hasFlash = true;
			flashVersion = '2.0.0.11';
		}

	} else if (navigator.mimeTypes && navigator.mimeTypes.length) {
		var mimeType = navigator.mimeTypes['application/x-shockwave-flash'];
		hasFlash = mimeType && mimeType.enabledPlugin;
		if (hasFlash) {
			flashVersion = getFlashVersion(mimeType.enabledPlugin.description);
		}

	} else {
		try {
			// Try 7 first, since we know we can use GetVariable with it
			var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
			hasFlash = true;
			flashVersion = getFlashVersion(ax.GetVariable('$version'));
		} catch (e) {
			// Try 6 next, some versions are known to crash with GetVariable calls
			try {
				var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
				hasFlash = true;
				flashVersion = '6.0.21';  // First public version of Flash 6
			} catch (e) {
				try {
					// Try the default activeX
					var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
					hasFlash = true;
					flashVersion = getFlashVersion(ax.GetVariable('$version'));
				} catch (e) {
					// No flash
				}
			}
		}
	}

	if (major == null || !hasFlash)
		return hasFlash;

	if (major < flashVersion[0] ||
				(major == flashVersion[0] && minor < flashVersion[1]) ||
				(major == flashVersion[0] && minor == flashVersion[1] && build <= flashVersion[2]))
		return true;

	return false;
}
