function getEventSource(e)
{
	if(e.srcElement)
	{
		// for IE
		return e.srcElement;
	}
	else
	{
		// for other browsers
		return e.target;
	}
}


// Ignore enter button if it was not pressed on a button, so as not to postback to the server. 
function CancelEnter(e)
{	
	if(e.keyCode == 13)
	{
		var eventSource = getEventSource(e);
		if(eventSource.type == "button" || eventSource.type == "submit")
		{
			// allow the enter event to do the postback
			blockEvent(e, false);
		}
		else
		{
			// ignore enter event
			blockEvent(e, true);
		}
	}
}

function blockEvent(e, shouldBlock)
{
	if(shouldBlock)
	{
		// ignore event
		if( e.preventDefault ) { e.preventDefault(); }
		e.returnValue = false;
	}
	else
	{
		// allow the event
		e.returnValue = true;
	}
}

function pressBtnOnEnter(e, btnToPress)
{				
	if(e.keyCode == 13)
	{
		var oBtn = document.getElementById(btnToPress);
		oBtn.click();
		blockEvent(e, true);
	}
}

function addEvent(obj, evType, fn)
{
	if(obj != null)
	{
		if(obj.addEventListener)
		{
			obj.addEventListener(evType, fn, false);
		}
		else if(obj.attachEvent)
		{
			obj.attachEvent("on" + evType, fn);
		}
		else 
		{
			obj['on' + evType] = fn;
		}
	}
}

function openModelessDialog(url, winName, left, top, width, height, args)
{
	var newWin;
	if(window.showModelessDialog)
	{
		var features = "status:no;help=no;";
		if(width) features += "dialogWidth:" + width + "px;";
		if(height) features += "dialogHeight:" + height + "px;";
		if(left) features += "dialogLeft:" + left + ";";
		if(top) features += "dialogTop:" + top + ";";
		newWin = window.showModelessDialog(url, args, features);
	}
	//else if(window.openDialog)
	//{
	//	alert("openDialog");
	//	var features = 'modal=yes,dialog=yes,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+ width +',height=' + height + ',left=' + left + ',top=' + top;
	//	newWin = window.openDialog(url, "", features, args);
	//}
	else
	{
		//newWin = window.open(url, '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+ width +',height=' + height + ',left=' + left + ',top=' + top);
		var features = 'modal=yes,dependent=yes,dialog=yes,center=yes,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0';
		if(width) features += ',width='+ width;
		if(height) features += ',height=' + height;
		if(left) features += ',left=' + left;
		if(top) features += ',top=' + top;
		newWin = window.open(url, winName, features);
		newWin.focus();
	}
	
	if (newWin && newWin.top)
	{
		// popup has opened
		return newWin;
	} 
	else
	{
		// popup has been blocked
		return null;
	} 
}


function getDialogArgs()
{
	if(window.dialogArguments)
	{
		return window.dialogArguments
	}
	else if(window.arguments)
	{
		return window.arguments[0];
	}
	else 
	{
		return null;
	}
}

function getCallerWnd()
{
	if(opener)
	{
		return opener;
	}
	else
	{
		return getDialogArgs();
	}
}

function appendTableCell(tableRow)
{
	//var c = tableRow.insertCell();
	// insertCell is only supported by IE
	var c = document.createElement('td');
	tableRow.appendChild(c);

	return c;
}

function clearTable(table)
{
	while (table.rows.length > 0)
	{
		table.deleteRow(0);
	}
}

var splitCh = String.fromCharCode(1);
function copyArray(arr)
{
	return arr.join(splitCh).split(splitCh);
}
