var modulehelp = new Array();
var HEXVALUES = "0123456789ABCDEF";

function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}

function getObject(id) {
	if (document.all) 
		return document.all(id)
	else if (document.getElementById) 
		return document.getElementById(id)
	else if (document.layers) 
		return document.layers[id]
	else
		return false;
}

function helpme(what) {
    var helptext = modulehelp[what];
    var xx = event.screenX+10;
    var yy = event.screenY+10;
    if (yy+255>window.screen.availHeight) yy = window.screen.availHeight-255;
    window.showModelessDialog(admin_dir+"/help.php",helptext,"dialogHeight: 250px; dialogWidth: 300px; dialogTop: "+yy+"px; dialogLeft: "+xx+"px; edge: Raised; center: No; help: No; resizable: No; status: No;");
}

function confirmSave(text) {
    var result = window.confirm(text);
    if (result == true) document.forms.editform.submit();
    return result;
}

function confirmReset(text) {
	var result = window.confirm(text);
	if (result == true) {
      if (backupHTML != null)
        formcontainer.innerHTML = backupHTML;
      else
        document.forms.editform.reset();
    }
    return result;
}

function initForm(data, input) {
	var oData, oInput;
	if (data.length == input.length) {
		for (i=0; i<data.length; i++) {
			oData = getObject(data[i]);
			oInput = getObject(input[i]);
			oInput.value = oData.innerHTML;
		}
	}
}

function edit(data, input) {
	var edit={}
	edit.src = data.innerHTML;
	edit.styledata=document.styleSheets;
	var result = window.showModalDialog("editor.php", edit ,"dialogWidth:800px;dialogHeight:600px;help:no;status:no;scroll:no;resizable:no;")
	if (result != null) {
		data.innerHTML = result;
		input.value = result;
	}
}

function color(data, input) {
    var result = window.showModalDialog("editor/selcolor.php", "" ,"dialogWidth:22em;dialogHeight:18em;help:no;status:no;scroll:no;resizable:no;");
  	if (result != null) {
  		result = validColor(result);
      data.value = result;
      input.style.backgroundColor = result;
      input.title = result;
    }
}

function link(input) {
	var result = window.showModalDialog("editor/dlg_ins_link.php", "" ,"dialogWidth:450px;dialogHeight:255px;help:no;status:no;scroll:no;resizable:no;");
	if (result != null)
		input.value = result['url'];
}

function image(input) {
	var result = window.showModalDialog("editor/dlg_ins_img.php", "" ,"dialogWidth:600px;dialogHeight:480px;help:no;status:no;scroll:no;resizable:no;");
	if (result != null) 
	{
		var start = result.indexOf(' SRC=')+5;
		var length = result.indexOf(' BORDER=')-start; 
		var src = result.substr(start, length);
		input.value = src;
	}
}

function listOver(a) {
	a.style.backgroundColor='#CCCCCC';
}
function listOut(a) {
	a.style.backgroundColor='';
}

function validColor(hex) {
	rgb = hexToRGB(hex);
	if (rgb[0] < 0 || rgb[1] < 0 || rgb[2] < 0) return '';
	return hex;
} 

function hexToRGB(hex) {
	hex += "";
	// turn hex into uppercase and cut out the interesting part
	hex = hex.toUpperCase().substr( (hex.charAt(0) == "#") ? 1 : 0, 6);
	var rgb = new Array(0,0,0);
	// convert the values
	for (j = 0; j < 3; j++) {
		rgb[j] = HEXVALUES.indexOf( hex.charAt(j*2) ) * 16 + HEXVALUES.indexOf( hex.charAt(j*2+1) );
	}
	return rgb;
}

function clickTemplate()
{
	if (window.event != null)
	{
		var oTag = window.event.srcElement;

		if (oTag.tagName == 'IMG' && oTag.id.substr(3, 7) == 'Picture')
		{
			oTpl.value = oTag.src;
			image(oTpl);
			iFn = 'imgName'+oTag.id.substr(10,2);
			document.all.editform[iFn].value = oTpl.value;
			oTag.src = oTpl.value;
		}

		if (oTag.tagName == 'DIV' && oTag.id.substr(3, 4) == 'Text')
		{
			oTpl.value = oTag.innerText;
			text(oTpl);
			oTag.innerText = oTpl.value;
			iFn = 'divText'+oTag.id.substr(7,1);
			document.all.editform[iFn].value = oTpl.value;
		}
	}
}

function text(input) {
	var edit = new Object();
	edit.src = input.value;
	var result = window.showModalDialog("editor/dlg_ins_text.php", edit ,"dialogWidth:600px;dialogHeight:480px;help:no;status:no;scroll:no;resizable:no;");
	if (result != null) 
	{
		input.value = result;
	}
} 

function fnGetNames(el){
	return document.getElementsByName(el);
}
