function openPopup(url, title, width, height) {
	openPopupLayer(url, title, width, height, 0);
}

function openPopupLayer(url, title, width, height, layer) {
	var left = (screen.width/2) - width/2 + layer * 100;
	var top = (screen.height/2) - height *(.65) - layer * 100;
	var accWin;
	accWin = window.open(url,title,'width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top);
	accWin.focus();
}

function hide(id) {
    if (document.getElementById(id) != null) {
        document.getElementById(id).style.display= 'none';
    }
}

function show(id) {
    if (document.getElementById(id) != null) {
        document.getElementById(id).style.display= '';
    }
}

function setVisible(id, visible){
	if(visible){
		show(id);
	}
	else {
		hide(id);
	}
}

function setEnabled(id, enabled) {
    if (document.getElementById(id) != null) {
        document.getElementById(id).disabled = enabled;
    }
}

function getSelectedValue(dropDown){
	var index = dropDown.selectedIndex;
	
	if(index == -1){
		return null;
	}
	else {
		return dropDown.options[index].value;
	}
}

function setSelectedValue(dropDown, value){
	if(value == null){
		return;
	}
	var options = dropDown.options;
	for(i=0; i<options.length; i++){
		if(options[i].value == value){
			dropDown.selectedIndex = i;
			return;
		}
	}
}

function contains(array, element){
	if(array.length == 0){
		return false;
	}
	for(var i=0; i<array.length; i++){
		if(array[i] == element){
			return true;
		}
	}
	return false;
}

function containsAny(array, elementsToCheck){
	if(array == null){
		return false;
	}
	for(var i=0; i<elementsToCheck.length; i++){
		if(contains(array, elementsToCheck[i])){
			return true;
		}
	}
	return false;
}

function arrayLength(array){
	if(array == null){
		return 0;
	}
	else {
		return array.length;
	}
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

var collapsedSections = {};
function siblingTree(node)
{
	if (node)
	{
		var rex = new RegExp("(\\W+|^)sibling-collapse(\\W+|$)");
		var section = node.attributes.sectionHeader.value;

		if (rex.test(node.className))
		{
			node.className = node.className.replace(rex, "$1sibling-expand$2")	
			collapsedSections[section] = true;
		}
		else
		{
			rex = new RegExp("(\\W+|^)sibling-expand(\\W+|$)");
			node.className = node.className.replace(rex, "$1sibling-collapse$2")	
			collapsedSections[section] = null;
		}
		
		var sibling = node.nextSibling;
		while (sibling) 
		{
			if (sibling && sibling.attributes && sibling.attributes.section) 
			{
				var sectionsString = sibling.attributes.section.value;
				var siblingSections = sectionsString.split(',');
				for(i = 0; i<siblingSections.length; i++){
					siblingSections[i] = trim(siblingSections[i]);
				}
				
				var visible = true;
				for(col in collapsedSections){
					if(collapsedSections[col] != null){
						if(contains(siblingSections, col)){
							visible = false;
							break;
						}
					}
				}
				
				rex = new RegExp("(\\W+|^)sibling-collapsed(\\W+|$)");
				if(visible){
					if (rex.test(sibling.className)) 
					{
						sibling.className = sibling.className.replace(rex, "$2")	
					} 
				}
				else {
					if (!rex.test(sibling.className)) 
					{
						sibling.className += (sibling.className.length > 0) ? " sibling-collapsed"  : "sibling-collapsed";
					} 
				}
				
			}
			sibling = sibling.nextSibling;
		}
	}
}

//	Shows or hides the rows in a table matching some criteria. 
//	The subject to showing/hiding shoul be marked with a custom (non-default)
//	HTML attribute and it should have a specific value.
//	
//	Parameters:
//	tableId - the id of the table
//	marker - the name of the custom HTML attribute, marking the rows to change their visibility state
//	value - the value which the marker should have in order the row to be processed
//	visible - whether to show or hide the row
function showHideRows(tableId, marker, value, visible){
	var table = document.getElementById(tableId);
	if(table != null){
		var rows = table.getElementsByTagName('tr');
		for(i=0; i<rows.length; i++){
			var row = rows[i];
			var markerAttribute = eval('row.attributes.' + marker);
			if(	markerAttribute != null && 
				markerAttribute.value != null &&
				markerAttribute.value == value){
				if(visible){
					row.style.display = '';
				}
				else {
					row.style.display = 'none';
				}
			}
		}
	}
}

function replacePostBackUrl(url){
	if(url == null){
		return;
	}
	for(i=0; i< document.forms.length; i++){
		var form = document.forms[i];
		form.action = url;		
	}
}

function bindDropDown(ddl, emptyValueDisplay, objects, dataField, formatString, displayFields){
//	alert(String.format('client bind: {0}', objects.length));

	var optionIndex = 0;	
	ddl.options.length = 0;
	if(emptyValueDisplay != null){
		ddl.options[optionIndex++] = new Option(emptyValueDisplay, "");
	}
	
	for(i in objects){
		var object = objects[i];
		
		if(typeof object == 'function'){
			continue;
		}
		
		var option = new Option();
		
		option.value = object[dataField];
		
		var displayValues = jQuery.map(displayFields, function(element){
			return object[element];
		});
		
		option.text = String.formatArray(formatString, displayValues);
		
		ddl.options[optionIndex ++] = option;
	}	
}

function formatString(object, formatString, displayFields){
	if( arguments.length == 0 ){
		return null;
	}

	var str = arguments[0];
	for(var i=1;i<arguments.length;i++)
	{
		var re = new RegExp('\\{' + (i-1) + '\\}','gm');
		str = str.replace(re, arguments[i]);
	}

	return str;
}

function Rgb2Hex(red, green, blue)
{
	var decColor = blue + 256 * green + 65536 * red;
	var stringColor = decColor.toString(16);
	while(stringColor.length < 6){
		stringColor = '0' + stringColor;
	}
	return stringColor;
}

String.format = function()
{
    if( arguments.length == 0 )
        return null;

    var str = arguments[0];
    for(var i=1;i<arguments.length;i++)
    {
        var re = new RegExp('\\{' + (i-1) + '\\}','gm');
        str = str.replace(re, arguments[i]);
    }

    return str;
}

String.formatArray = function(formatString, values)
{
    var str = formatString;
    for(var i=0;i<values.length;i++)
    {
        var re = new RegExp('\\{' + i + '\\}','gm');
        str = str.replace(re, values[i]);
    }

    return str;
}


function CoverButtons(upperLeftButton, bottomRightButton, text){
	ulPos = findPosition(upperLeftButton)
	brPos = findPosition(bottomRightButton);
	
	CoverArea(
		ulPos[0], 
		ulPos[1], 
		brPos[0] + bottomRightButton.offsetWidth,
		brPos[1] + bottomRightButton.offsetHeight,
		text
	);
}

function CoverArea(upperLeftX, upperLeftY, bottomRightX, bottomRightY, text)
   {
	    if(!document.getElementById('cover')) newelement('cover');
		
		var ediv = document.getElementById('cover');
		var idiv = document.getElementById('cover_int');
		var iframe = document.getElementById('cover_iframe');
		
		var width = bottomRightX - upperLeftX;
		var height = bottomRightY - upperLeftY;
	    
		var contentDiv;
		if(is_ie){
			contentDiv = idiv;
		}
		else {
			contentDiv = ediv;
		}
		
		contentDiv.innerHTML = 
			'<table class="button" style="text-align: center; width: ' + width +   'px" '  + 
			'cellpadding="2" cellspacing="0"><tr><td>' + text + '</td></tr></table>';
		
		var cover = ediv;
		cover.style.left = upperLeftX + "px"; 
		cover.style.top = upperLeftY + "px"; 
		cover.style.width = width + "px";
		cover.style.height = height + "px";
		
		ediv.style.display = 'block';
		
		if(is_ie){
			iframe.style.width = ediv.offsetWidth;
			iframe.style.height = ediv.offsetHeight;
			iframe.style.display = 'block';
		}

		return false;
	}	

function show_warning(el_offset, msg, warn_container, left_offset) {
	if (document.getElementById(warn_container)) return;
	if (left_offset == undefined) left_offset = 10;
	
	$("body:first").append("<div id='"+warn_container+"' style='display: none'>"+msg+"</div>");
	
	offset = el_offset.offset();
	width = el_offset.outerWidth();
	
	$("#"+warn_container).css({
		border: '2px solid #999',
		'background-color': '#FFFF99',
		padding: '0 4px',
		position: 'absolute',
		left: offset.left+width+left_offset,
		top: offset.top
	}).show("slow");
}

function layDown(anchorId, divId, x, y, width, height){
	var zero = $("#" + anchorId).offset();
	
	var div = $("#" + divId);
	div.width(width);
	div.height(height);
	
	div.css('left', zero.left + x);
	div.css('top', zero.top + y);
}

function coverDiv(sourceDiv, imageUrl){
	if(imageUrl == undefined){
		imageUrl = null;
	}

	var coverHtml = 
		'<div style="position:absolute; background-color: LightGrey">' + 
		(imageUrl != null ? '<img style="position:relative" src="' + imageUrl + '">' : '') + 
		'</div>';
	

	var covering = $(coverHtml);

	covering.attr('id', generateCoverId(sourceDiv.attr('id')));
	$("body").append(covering);

	var pos = sourceDiv.offset();
	
	covering.css('left', pos.left);
	covering.css('top', pos.top);
	
	var width = sourceDiv.width();
	var height = sourceDiv.height();
	
	covering.width(width);
	covering.height(height);
	
	if(imageUrl != null){
		var image = covering.find("img");
		var imageWidth = image.width();
		var imageHeight = image.height();
	
		image.css('left', Math.round((width - imageWidth) / 2));
		image.css('top', Math.round((height - imageHeight) / 2));
	}
	
	covering.css("opacity", "0.5");
	
	return covering;
}

function uncoverDiv(sourceDiv){
	var cover = $("#" + generateCoverId(sourceDiv.attr('id')));
	cover.remove();
}

function generateCoverId(originalId){
	return "cover_" + originalId;
}
