/** Pops up a HTML dialog, can be modal **/
function popupDialog(msg, width, modal, toFocus) {
	var dlg = document.createElement("div");
	dlg.className = "dialog";
	dlg.id = "popupDialog";
	dlg.style.display = "block";
	dlg.style.width = width;
	dlg.innerHTML = msg;
	document.body.appendChild(dlg);
	dlg.style.left = (getScrollLeft()+getInnerWindowWidth()-getElWidth(dlg))/2+"px";
	dlg.style.top = (getScrollTop()+(getInnerWindowHeight()-getElHeight(dlg))/2)+"px";
	if (modal) {
		var div = document.createElement("div");
		div.id = "modalBackdrop";
		var h1 = getInnerWindowHeight();
		var h2 = getElHeight(document.body);
		var h = h1 > h2 ? h1 : h2;
		div.style.height = h+"px";
		document.body.appendChild(div);
		hideSelectObjects(div);
	}
	if (toFocus) {
		gbid(toFocus).focus();
	}
}

function dismissDialog() {
	showSelectObjects(gbid("popupDialog"));
	document.body.removeChild(gbid("popupDialog"));
	showSelectObjects(gbid("modalBackdrop"));
	document.body.removeChild(gbid("modalBackdrop"));
}

/**
Match a single expression. The expression to be matched should
be indicated in the tag by ().
tag - the re pattern
num - which () expression to match (1 is the first)
returns: the matched expression, or the empty string
*/
String.prototype.matchSingle = function(tag, num) {
	if (!num) { num = 1; }
	var found = this.match(tag);
	if (found) {
		return found[num];
	}
	return "";
}

/**
Match and dump returned into 'results'. Makes 'if (...)' nicer.
tag - the re pattern
results - an array which will hold results
returns: true if there's a match, false otherwise
*/
String.prototype.matchin = function(tag, results) {
	results.length = 0;
	if ( (m = this.match(tag)) ) {
		for (var i in m) {
			results[i] = m[i];
		}
		return true;
	}
	return false;
}

function fixIESelectInnerHTML(sel, innerhtml) {
	///Workaround IE bug;
	if (innerhtml && sel.options.length==0) {
		var div = document.createElement("div");
		div.innerHTML = '<select>'+innerhtml+'</select>';
		var newSel = div.firstChild;
		removeAllOptions(sel);
		for (var i=0; i<newSel.options.length; i++) {
			sel.options[i] = new Option(newSel.options[i].text, newSel.options[i].value);

			if (typeof(adjustCallback)!="undefined") { adjustCallback(sel.options, i); }
		}
	}
}


