var max_recipients = 20;
var now_recipients = 0;
var voucher_displayed = false;

function addRecipient(vN,vM,rY,nB)
{
	if (now_recipients < max_recipients)
	{
		var tr = document.createElement("tr");
		var tab = $("rec_tab");
				
		now_recipients++;
		
		tr.appendChild(_td(_text(now_recipients+"."),"right"));
		tr.appendChild(_td(_text("Name:")));
		tr.appendChild(_td(_input("text","name_to[]","fieldFix",vN,rY)));
		tr.appendChild(_td(_text("E-Mail:")));
		tr.appendChild(_td(_input("text","mail_to[]","fieldFix",vM,rY)));
		
		tab.appendChild(tr);
		
		if (now_recipients > 0)
		{
			if (nB)
			{
				$("btn_del_rec").style.display = "block";
				$("btn_delall_rec").style.display = "block";
			}
		}
		$("rec_span").innerHTML = now_recipients+" von "+max_recipients;
	}
	
	if (now_recipients+1 > max_recipients)
	{
		if (nB) $("btn_add_rec").disabled = true;
	}
}

function delRecipient()
{	
	if (now_recipients > 0)
	{		
		var tab = $("rec_tab");
		var row = tab.getElementsByTagName("tr");
		var node = row[row.length-1].firstChild;
		var cell = row[row.length-1].getElementsByTagName("td"); 
		
		now_recipients--;
		
		while (cell.length)
		{
			cell = row[row.length-1].getElementsByTagName("td")
			
			if (cell.length)
			{
				row[row.length-1].removeChild(node);
				node = row[row.length-1].firstChild;
			}
		}
		tab.removeChild(tab.lastChild);
		$("rec_span").innerHTML = now_recipients+" von "+max_recipients;
	}
	
	if (now_recipients == 0)
	{
		$("btn_del_rec").style.display = "none";
		$("btn_delall_rec").style.display = "none";
	}
	
	if (now_recipients < max_recipients)
	{
		$("btn_add_rec").disabled = false;
	}
}

function delAllRecipients()
{
	var anz = now_recipients;
	
	if (confirm(am.getMessage("delete_recipients")))
	{
		for (var i=0; i<anz; i++)
		{
			delRecipient();
		}
	}
}

function addVoucher(vN,vM,rY,nB)
{
	if (!voucher_displayed)
	{
		$("voucher_tab").style.display = 'block';
		$("button_mehr_tr").style.display = 'none';
		voucher_displayed = true;	
	}
}

function delVoucher(vN,vM,rY,nB)
{
	for (i=0; i < document.forms["postkarte"].elements["voucher_nr"].length; i++){
		document.forms["postkarte"].elements["voucher_nr"][i].checked = false;
	}		
	$("button_mehr_tr").style.display = 'block';
	$("voucher_tab").style.display = 'none';
	voucher_displayed = false;	
}

function showVoucher(nr)
{
	$("voucher_tab").style.display = 'block';
	$("btn_del_voucher").style.display = 'block';	
	$("voucher_nr_".concat(nr)).checked = true;
	voucher_displayed = true;	
}

function _td(data,a)
{	
	var td = document.createElement("td");
		if (a)
		{
			td.setAttribute("align",a);
		}
		td.appendChild(data);
	
	return td;
}

function _input(t,n,c,v,r)
{	
	var input = document.createElement("input");
		input.setAttribute("type",t);
		input.setAttribute("name",n);
		input.setAttribute(browserRelated("class","className"),c);
		if (v != "")
		{
			input.setAttribute("value",v);
			if (r) input.setAttribute("readonly","readonly");
		}
		input.setAttribute("onfocus","highlightForm(this,true);");
		input.setAttribute("onblur","highlightForm(this,false);checkMailForWww(this);");
	
	return input;
}

function _text(t)
{	
	var txt = document.createTextNode(t);
	return txt;
}

function selectCheckbox(_this)
{
	if (!_this.checked)
	{
		$("jcNow").checked = false;
		_this.checked = true;
	}
}

function toggleFx(_this)
{
	var div = $("fx_area").getElementsByTagName("div");
	var anz = div.length;
	
	for (var i=0; i<anz; i++)
	{
		div[i].style.display = "none";
		
		if (div[i].id == _this.id)
		{
			_this.style.display = "block";
		}
	}
}

function fx_setFont(_this,def)
{
	if (_this && !def) var font = _this.style.fontFamily;
	else var font = def;
	
	$("card_text").style.fontFamily 	 = font;
	$("card_from_name").style.fontFamily = font;
	$("card_from_mail").style.fontFamily = font;
	$("card_to_name").style.fontFamily 	 = font;
	$("card_to_mail").style.fontFamily 	 = font;
	
	if (_this && !def) $("fx_ip_font").value = font;
	else $("fx_ip_font").value = def;
}

function fx_setFontColor(_this,def)
{
	if (_this && !def)
	{
		if (isIE()) var color = _this.style.background;
		else
		{
			var background = _this.style.background.match(/(rgb\(.*\))/);
			var color = background[0];
		}
	}
	else var color = def;
		
	$("card_text").style.color 		= color;
	$("card_from_name").style.color = color;
	$("card_from_mail").style.color = color;
	$("card_to_name").style.color 	= color;
	$("card_to_mail").style.color 	= color;
	
	if (_this && !def)
	{
		if (isIE()) $("fx_ip_color").value = color;
		else
		{
			var bg = _this.style.background.match(/rgb\((.*)\)/);
			$("fx_ip_color").value = bg[1];
		}
	}
	else $("fx_ip_color").value = def;
}

function fx_setFontsize(_this,def)
{
	if (_this && !def)
	{
		$("card_text").style.fontSize = _this.style.fontSize;
		$("fx_ip_size").value = _this.style.fontSize;
	}
	else
	{
		$("card_text").style.fontSize = def;
		$("fx_ip_size").value = def;
	}
}

function fx_setBgcolor(_this,def)
{
	if (_this && !def) var background = _this.style.background;
	else var background = def;
		
	$("card_text").style.background 	 = background;
	$("card_from_name").style.background = background;
	$("card_from_mail").style.background = background;
	$("card_to_name").style.background 	 = background;
	$("card_to_mail").style.background 	 = background;
	
	if (_this && !def)
	{
		if (isIE()) $("fx_ip_bgcolor").value = background;
		else
		{
			var bg = _this.style.background.match(/rgb\((.*)\)/);
			$("fx_ip_bgcolor").value = bg[1];
		}
	}
	else $("fx_ip_bgcolor").value = def;
}

function fx_setStamp(_this,def)
{
	if (_this && !def)
	{
		$("card_stamp").style.background = _this.style.background;
		
		var bg = _this.style.background.match(/\/briefmarken\/(.*\.gif)/);
		$("fx_ip_stamps").value = bg[1];
	}
	else
	{
		$("card_stamp").style.background = "url(images/postkarte/briefmarken/"+def+")";
		$("fx_ip_stamps").value = def;
	}

}

function fx_setPattern(_this,def)
{
	var arr_desc = new Array("card_d_msg","card_d_fx","card_d_fh","card_d_fn","card_d_fm","card_d_rh","card_d_rn","card_d_rm","card_d_ar","card_d_effect");
	
	if (_this && !def)
	{
		if (_this.style.background.indexOf(".singlebg.") != -1) $("card_image").style.background = "#FFFFFF";
		else $("card_image").style.background = _this.style.background;
		
		$("card_form").style.background  = _this.style.background;
		
		for (i=0; i<arr_desc.length; i++)
		{
			if (_this.style.background.indexOf(".dark.") != -1) $(arr_desc[i]).style.color = "#FFFFFF";
			else $(arr_desc[i]).style.color = "#000000";
		}
		
		var jpg  = _this.style.background.match(/\/muster\/(.*\.jpg)/);
		var gif  = _this.style.background.match(/\/muster\/(.*\.gif)/);
		var bg   = (jpg) ? jpg[1] : gif[1];
		
		$("fx_ip_pattern").value = bg;
	}
	else
	{
		if (def.indexOf(".singlebg.") != -1) $("card_image").style.background = "#FFFFFF";
		else $("card_image").style.background = "url(images/postkarte/muster/"+def+")";
		
		
		$("card_form").style.background  = "url(images/postkarte/muster/"+def+")";
		
		for (i=0; i<arr_desc.length; i++)
		{
			if (def.indexOf(".dark.") != -1) $(arr_desc[i]).style.color = "#FFFFFF";
			else $(arr_desc[i]).style.color = "#000000";
		}
		$("fx_ip_pattern").value = def;
	}
}

function fx_setMusic(_this,def)
{
	//no sound
	if(_this == ""){
		$("fx_ip_music").value = "";
		return false;
	}
	var isPremiumSound = _this.value.search(/premium_.+/);
	if (isPremiumSound != -1){
		var file = PATH_HTTP+"ajx/fx_premium_music.php?f=";
	} else {
		var file = PATH_HTTP+"ajx/fx_music.php?f=";
	}
	if (_this && !def)
	{		
		if ($("fxMusicMute").src.indexOf("_stop.gif") != -1)
		{
			$("fxMusicEmbed").src = file+"blank";
			$("fxMusicMute").src = $("fxMusicMute").src.replace(/_stop.gif/,"_play.gif");
		}
		
		$("fx_ip_music").value = _this.value;
	}
	else
	{
		$("fxMusicEmbed").src  = file+def;
		$("fx_ip_music").value = def;
	}
}

function fx_playMusic(_this)
{
	var isPremiumSound = $("fx_ip_music").value.search(/premium_.+/);
	if (isPremiumSound != -1){
		var file = PATH_HTTP+"ajx/fx_premium_music.php?f=";
	} else {
		var file = PATH_HTTP+"ajx/fx_music.php?f=";
	}	
	
	if ($("fx_ip_music").value)
	{				
		if (_this.src.indexOf("_play.gif") != -1)
		{
			$("fxMusicEmbed").src = file+$("fx_ip_music").value;
			_this.src = _this.src.replace(/_play.gif/,"_stop.gif");
		}
		else
		{
			$("fxMusicEmbed").src = file+"blank";
			_this.src = _this.src.replace(/_stop.gif/,"_play.gif");
		}
	}
}


function fx_setEffect(_this,def)
{
	if (_this && !def) $("fx_ip_effects").value = _this.value;
	else $("fx_ip_effects").value = def;
}

function fx_setPreset(presets)
{
	if (presets)
	{
		presets = parsePropertyList(presets);
		
		for (key in presets)
		{		
			switch(key)
			{
				case "font"    : fx_setFont(false,presets[key]); break;
				case "color"   : fx_setFontColor(false,"#"+presets[key]); break;
				case "size"    : fx_setFontsize(false,presets[key]+"px"); break;
				case "bgcolor" : fx_setBgcolor(false,"#"+presets[key]); break;
				case "stamps"  : fx_setStamp(false,presets[key]); break;
				case "pattern" : fx_setPattern(false,presets[key]); break;
				case "music"   : fx_setMusic(false,presets[key]); break;
				case "effects" : fx_setEffect(false,presets[key]); break;
			}
		}
		
		$("fxUndo").style.display = "block";
	}
}

function fx_undo(presets)
{
	if (presets)
	{
		presets = parsePropertyList(presets);
		
		for (key in presets)
		{		
			switch(key)
			{
				case "font"    : fx_setFont(false,presets[key]); break;
				case "color"   : fx_setFontColor(false,"#"+presets[key]); break;
				case "size"    : fx_setFontsize(false,presets[key]+"px"); break;
				case "bgcolor" : fx_setBgcolor(false,"#"+presets[key]); break;
				case "stamps"  : fx_setStamp(false,presets[key]); break;
				case "pattern" : fx_setPattern(false,presets[key]); break;
				case "music"   : fx_setMusic(false,presets[key]); break;
				case "effects" : fx_setEffect(false,presets[key]); break;
			}
		}
	}
	
	$("fxPresetSelect").selectedIndex = 0;
	$("fxUndo").style.display = "none";
}

function parsePropertyList(plist)
{
	if (typeof(plist) == "string") 
	{
		var elems  = new Array();
		var keyval = new Array();
		keyval[0] = new Object();
		
		elems = plist.split(",");
		
		for (var i=0; i<elems.length; i++)
		{
			var tmp = new Array();
			tmp = elems[i].split(":");
			
			keyval[0][tmp[0]] = tmp[1];
		}
		
		return keyval[0];
	}
	
	return plist;
}

function setSmiley(code)
{
	$("card_text").focus();
	$("card_text").value += code+" ";
}

function setLocation(loc)
{
	location.href = PATH_HTTP+loc;
}

function checkFormValues()
{
	var error = "";
	var regex = new RegExp("^([a-zA-Z0-9\\-\\.\\_]+)(\\@)([a-zA-ZäÄöÖüÜ0-9\\-\\.]+)(\\.)([a-zA-Z]{2,4})$");
	var mail_from = $("card_from_mail").value.replace(/^\s+|\s+$/g, '');
	var mail_to = $("card_from_mail").value.replace(/^\s+|\s+$/g, '');
		
	if (!$("card_from_name").value) error += "- "+am.getMessage("name_from_empty")+"\n";
	if (!regex.test(mail_from)) error += "- "+am.getMessage("mail_from_invalid")+"\n";
	
	if (!$("card_to_name").value) error += "- "+am.getMessage("name_to_empty")+"\n";
	if (!regex.test(mail_to)) error += "- "+am.getMessage("mail_to_invalid")+"\n";
		
	if (error)
	{
		alert(am.getMessage("error_headline")+'\n\n'+error);
		return false;
	}
	
	return true;
}

function checkMailForWww(_this)
{
	var mail = _this.value.toLowerCase();
		
	if (mail.indexOf("www.") == 0)
	{
		var msg = am.getMessage("mail_is_domain");
		
		if (confirm(msg.replace(/#mail#/,_this.value)))
		{
			_this.value = mail.substr(4,_this.value.length);
		}
		
		return true;
	}
}

function tt_showBubble(txt,y,x)
{
	var x = getInt(x);
	var y = getInt(y);
	
	if (isIE())	y += -8;
	
	$("ttBubble").style.top  = y;
	$("ttBubble").style.left = x;
	$("ttMsg").innerHTML = txt;
	$("ttBubble").style.visibility = "visible";
	$("ttBubble").style.display = "block";
}

function tt_showBubble_weitere_empfaenger(txt,y,x)
{
	var x = getInt(x);
	var y = getInt(y);
	
	if (isIE())	y += -8;
	
	$("ttBubble_weitere_empfaenger").style.top  = y;
	$("ttBubble_weitere_empfaenger").style.left = x;
	$("ttMsg_weitere_empfaenger").innerHTML = txt;
	$("ttBubble_weitere_empfaenger").style.visibility = "visible";
	$("ttBubble_weitere_empfaenger").style.display = "block";
}

function tt_hideBubble()
{
	$("ttMsg").innerHTML = "";
	$("ttBubble").style.display = "none";
	$("ttBubble").style.visibility = "hidden";
}

function tt_hideBubble_weitere_empfaenger()
{
	$("ttMsg").innerHTML = "";
	$("ttBubble_weitere_empfaenger").style.display = "none";
	$("ttBubble_weitere_empfaenger").style.visibility = "hidden";
}

var i = 100;
var exec = 100;

function clearExec()
{
	exec = 100;
}

function twisterStartup() 
{
	if (i == 0) {
		i = 100;
		setTimeout("twisterStartup()",1000);
	} else { twister(); }
}

function twister() 
{
	moveBy( 15 - 30*Math.random() , 15 - 30*Math.random());
	if (i > 0 && exec>0) { exec--; i--; setTimeout("twister();", 10); // in ms
	} else { if (exec>0) setTimeout("twisterStartup()",10); }
}

function openMsg()
{
	window.alert('Ich sagte doch, NICHT den Knopf drücken!');
	window.alert('DU hast es doch getan!');
	window.alert('Und das auch noch am 1.April...');
	window.alert('Soviel Mut hätte ich dir gar nicht zugetraut.');
	window.alert('Und,..., würdest du es nochmal tun????');
	window.alert('Ich habe gehört, dass der neueste Computervirus ständig neue Fenster\n öffnet und darauf wartet, dass man OK klickt...');
	window.alert('Du hast schon wieder OK geklickt! Hast du keine Angst vor Viren????');
	window.alert('Na dann tippe doch mal hier OK!\n\nBankleitzahl und Kto.nr. teile ich dir noch mit!');
	window.alert('Und, lästig diese ständige klicken???\nDu wolltest ja nicht hören und musstest unbedingt klicken!');
	window.alert('Wenn doch alles im Leben so einfach wäre und man immer nur OK klicken müsste...!');
	window.alert('Und weil heute der 1. April ist, darfst du jetzt noch zehn-mal OK klicken. :)\n\nViel Spass!');

	window.alert('Tippe einfach OK!');
	window.alert('OK zum schliessen!');
	window.alert('Bald geschafft!');
	window.alert('Laaaaaaaaaaaaaaaaaangweilig!');
	window.alert('Mann, warum hast du nur geklickt?!');
	window.alert('Bitte einfach NUR OK klicken.');
	window.alert('OK, was sonst!');
	window.alert('Trainierst du für die OK-Klick Olympiade oder was?');
	window.alert('Hast du mitgezählt? Falls nicht, gibt es zehn neue OKs für Dich!!!!');
	window.alert('April, April!!!');
}
