function FetchElement(id) {
	if (document.getElementById) {
		xfelement = document.getElementById(id);
	}
	else if (document.all) {
		xfelement = document.all[id];
	}
	else if (document.layers) {
		xfelement = document.layers[id];
	}
	return xfelement;
}

function ajax() {
	var xmlhttp, bComplete = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { xmlhttp = false; }}}
	if (!xmlhttp) return null;
	this.connect = function(sURL, sMethod, sVars, fnDone) {
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		try {
			xmlhttp.open(sMethod, sURL+"?"+sVars, true);
			xmlhttp.onreadystatechange = function(){
        		if (xmlhttp.readyState == 4 && !bComplete) {
				bComplete = true;
				fnDone(xmlhttp);
			}};
			xmlhttp.send('');
		}
		catch(z) { return false; }
		return true;
	};
	return this;
}

function save_smiley(file, querystr) {
	var Conn = new ajax();
	if (!Conn) {
		var curl = file+'?'+querystr;
		window.open(curl+'&cache=3',"","width=180,height=30,innerHeight=180,innerWidth=30,personalbar=no,resizable=no,scrollbars=no,location=no,locationbar=no,toolbar=no,statusbar=no,menubar=no");
	}
	else {
		var fnWhenDone = function (oXML) {
			var nfile = oXML.responseText;
			FetchElement('Message').value = prefix+nfile+suffix;
			FetchElement('URL').value = nfile;
			FetchElement('Message').focus();
		};
		var connect = Conn.connect(file, "get", querystr+'&cache=4', fnWhenDone);
		if (!connect) {
			var curl = file+'?'+querystr;
			window.open(curl+'&cache=3',"","width=180,height=30,innerHeight=180,innerWidth=30,personalbar=no,resizable=no,scrollbars=no,location=no,locationbar=no,toolbar=no,statusbar=no,menubar=no");
		}
	}
}

function SuchenUndErsetzen(QuellText, SuchText, ErsatzText) {   
	if ((QuellText == null) || (SuchText == null))           { return null; }
	if ((QuellText.length == 0) || (SuchText.length == 0))   { return QuellText; }
	if ((ErsatzText == null) || (ErsatzText.length == 0))    { ErsatzText = ""; }
    var LaengeSuchText = SuchText.length;
	var LaengeErsatzText = ErsatzText.length;
	var Pos = QuellText.indexOf(SuchText, 0);
	while (Pos >= 0) {
		QuellText = QuellText.substring(0, Pos) + ErsatzText + QuellText.substring(Pos + LaengeSuchText);
		Pos = QuellText.indexOf(SuchText, Pos + LaengeErsatzText);
	}
	return QuellText;
}

function enter(code) {
	FetchElement('Message').value = prefix+code+suffix;
	FetchElement('URL').value = code;
	FetchElement('Message').focus();
}
function showimage(source) {
	FetchElement('icons').src = source;
}

function update(cache) {
	frm = FetchElement('Gen');
	var sn = frm.sn[0].value;
	for (i=0; i<frm.sn.length; i++){
		if(frm.sn[i].checked == true){
			sn = frm.sn[i].value;
		}
	}
	var face = frm.face.value;
	var size = frm.size.value;
	var text = frm.text.value;
	var color = frm.color.value;
	var border = frm.border.value;
	var schildbg = frm.schildbg.value;
	var align = frm.align.value;
	text = escape(text);
	color = SuchenUndErsetzen(color, '#', "");	
	border = SuchenUndErsetzen(border, '#', "");	
	schildbg = SuchenUndErsetzen(schildbg, '#', "");
	
	if(face.length > 0) {
		face = '&face='+face;
	}
	else {
		face = '';
	}
	if(size.length > 0 && size != '12') {
		size = '&size='+size;
	}
	else {
		size = '';
	}
	if(color.length > 0 && color != 'CC0000') {
		color = '&color='+color;
	}
	else {
		color = '';
	}
	if(sn.length > 0) {
		sn = '&smilienummer='+sn;
	}
	else {
		sn = '';
	}
	if(schildbg.length > 0 && schildbg != 'FFFFFF') {
		schildbg = '&schildbg='+schildbg;
	}
	else {
		schildbg = '';
	}
	if(border.length > 0 && border != '000000') {
		border = '&border='+border;
	}
	else {
		border = '';
	}
	if(text.length > 0) {
		text = '&text='+text;
	}
	else {
		text = '';
	}
	if(align == 'R' || align == 'L') {
		align = '&align='+align;
	}
	else {
		align = '';
	}
	var file = surl+'index.php';
	var querystr = 'action=generate'+text+sn+size+face+color+schildbg+border+align;
	var url = file+'?'+querystr;
    if (cache == 3) {
    	save_smiley(file, querystr);
    }
    else {
        enter(url);
    	showimage(url);	
    }
}

function virtualcolor(id) {
	
	if (id == 'all') {
		virtualcolor(1,1);
		virtualcolor(2,1);
		virtualcolor(3,1);
		return;
	}
	
	var vc = FetchElement('vc'+id);
	var ic = FetchElement('ic'+id);
	
	var val = ic.value;
	
	if (val < 1) {
		color = '#000000';
	}
	else {
		if (val.indexOf('#') == -1) {
			color = '#'+val;
		}
		else {
			color = val;
		}
	}
	
	ic.value = color;
	
	vc.style.backgroundColor = color;
		
}
