window.onload = function() {
	setTimeout(function() {
		$(
			'tx-srfeuserregister-pi1-username',
			'tx-srfeuserregister-pi1-email',
			'tx-srfeuserregister-pi1-password',
			'tx-srfeuserregister-pi1-password_again',
			'tx-srfeuserregister-pi1-first_name',
			'tx-srfeuserregister-pi1-last_name',
			'tx-srfeuserregister-pi1-date_of_birth',
			'tx-srfeuserregister-pi1-address',
			'tx-srfeuserregister-pi1-city',
			'tx-srfeuserregister-pi1-zip',
			'tx-srfeuserregister-pi1-static_info_country',
			'tx-srfeuserregister-pi1-telephone',
			'tx-srfeuserregister-pi1-newsletter',
			'tx-srfeuserregister-pi1-payment_owner',
			'tx-srfeuserregister-pi1-payment_account',
			'tx-srfeuserregister-pi1-payment_bank',
			'tx-srfeuserregister-pi1-payment_bank_no',
			'tx-srfeuserregister-pi1-payment_iban',
			'tx-srfeuserregister-pi1-payment_swift',
			'tx-srfeuserregister-pi1-payment_uid'
		).each(function(sp) {
			if (sp != undefined) {
				var label = sp.parentNode.previousElementSibling.firstElementChild;
				
				try {
					var error = sp.previousElementSibling;
				} catch(err) {
					var error = undefined;
				}
				var input = sp;
				input.defaultValue = input.value;
				
				if (label.textContent.match(/ \*$/) != null || error != undefined) {
					if (input.value == '' || error != undefined) {
						label.setAttribute('class', 'obligatory');
					}
					new Form.Element.Observer(input, 1, function() {
						var	label = this.element.parentNode.previousElementSibling.firstElementChild;
						try {
							var error = sp.previousElementSibling;
						} catch(err) {
							var error = undefined;
						}
						if ((this.getValue() == '' && label.textContent.match(/ \*$/) != null) || (input.defaultValue == input.value && error != undefined)) {
							label.setAttribute('class', 'obligatory');
						} else {
							label.setAttribute('class', '');
							if (error != undefined) {
								error.style.color = '#FF6F6F';
							}
						}
						
					});
				}
			}
		});
		/*
		$(
			'invoice_email',
			'tx-srfeuserregister-pi1-email',
			'invoice_address',
			'tx-srfeuserregister-pi1-address',
			'invoice_city',
			'tx-srfeuserregister-pi1-city',
			'invoice_zip',
			'tx-srfeuserregister-pi1-zip',
			'invoice_country',
			'tx-srfeuserregister-pi1-static_info_country',
			'tx-srfeuserregister-pi1-first_name',
			'tx-srfeuserregister-pi1-last_name',
			'invoice_name'
		).each(function(sp) {
			if (sp != undefined) {
				sp.onfocus = function() {
					this.focused = true;
				};
				sp.onblur = function() {
					this.focused = false;
					var input = this;
					if (input.disabled == false) {
						if (input.id == 'invoice_email') {
							$('tx-srfeuserregister-pi1-email').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-email') {
							$('invoice_email').value = input.value;
						}
						
						else if (input.id == 'invoice_address') {
							$('tx-srfeuserregister-pi1-address').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-address') {
							$('invoice_address').value = input.value;
						}
						
						else if (input.id == 'invoice_city') {
							$('tx-srfeuserregister-pi1-city').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-city') {
							$('invoice_city').value = input.value;
						}
						
						else if (input.id == 'invoice_zip') {
							$('tx-srfeuserregister-pi1-zip').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-zip') {
							$('invoice_zip').value = input.value;
						}
						
						else if (input.id == 'invoice_country') {
							var cou = $('tx-srfeuserregister-pi1-static_info_country');
							for (var i=0; i < cou.options.length; i++) {
								if (cou.options[i].text.toLowerCase().match(input.value.toLowerCase())) {
									cou.selectedIndex = i;
									break;
								}
							}
						} else if (input.id == 'tx-srfeuserregister-pi1-static_info_country') {
							var cou = $('tx-srfeuserregister-pi1-static_info_country');
							$('invoice_country').value = cou.options[cou.selectedIndex].text
						}
						
						else if (input.id == 'tx-srfeuserregister-pi1-first_name' || input.id == 'tx-srfeuserregister-pi1-last_name') {
							$('invoice_name').value = $('tx-srfeuserregister-pi1-first_name').value + $('tx-srfeuserregister-pi1-last_name').value;
						} else if (input.id == 'invoice_name') {
							$('tx-srfeuserregister-pi1-first_name').value = input.value.match(/^[^ ]+/)[0];
							$('tx-srfeuserregister-pi1-last_name').value  = input.value.replace($('tx-srfeuserregister-pi1-first_name').value + " ", '');
						}
					}
				};

				new Form.Element.Observer(sp, 1, function() {
					var input = this.element;
					if (input.disabled == false && input.focused == true) {
						if (input.id == 'invoice_email') {
							$('tx-srfeuserregister-pi1-email').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-email') {
							$('invoice_email').value = input.value;
						}
						
						else if (input.id == 'invoice_address') {
							$('tx-srfeuserregister-pi1-address').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-address') {
							$('invoice_address').value = input.value;
						}
						
						else if (input.id == 'invoice_city') {
							$('tx-srfeuserregister-pi1-city').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-city') {
							$('invoice_city').value = input.value;
						}
						
						else if (input.id == 'invoice_zip') {
							$('tx-srfeuserregister-pi1-zip').value = input.value;
						} else if (input.id == 'tx-srfeuserregister-pi1-zip') {
							$('invoice_zip').value = input.value;
						}
						
						else if (input.id == 'invoice_country') {
							var cou = $('tx-srfeuserregister-pi1-static_info_country');
							for (var i=0; i < cou.options.length; i++) {
								if (cou.options[i].text.toLowerCase().match(input.value.toLowerCase())) {
									cou.selectedIndex = i;
									break;
								}
							}
						} else if (input.id == 'tx-srfeuserregister-pi1-static_info_country') {
							var cou = $('tx-srfeuserregister-pi1-static_info_country');
							$('invoice_country').value = cou.options[cou.selectedIndex].text
						}
						
						else if (input.id == 'tx-srfeuserregister-pi1-first_name' || input.id == 'tx-srfeuserregister-pi1-last_name') {
							$('invoice_name').value = $('tx-srfeuserregister-pi1-first_name').value + $('tx-srfeuserregister-pi1-last_name').value;
						} else if (input.id == 'invoice_name') {
							$('tx-srfeuserregister-pi1-first_name').value = input.value.match(/^[^ ]+/)[0];
							$('tx-srfeuserregister-pi1-last_name').value  = input.value.replace($('tx-srfeuserregister-pi1-first_name').value + " ", '');
						}
					}
				});
			}
		});
		*/
	}, 2000);
};

var flag_bold = Array(false,false);
var flag_italic = Array(false,false);
var flag_under = Array(false,false);

var colorLevels = Array('0', '3', '6', '9', 'C', 'F');
var colorArray = Array();
var flag_color = Array("000000","000000");
var flag_size = Array("16","16");
var flag_font = Array("arial","arial");
var flag_shirt_color = "";
var flag_scale = Array (100,100);
var flag_img_w = Array (0,0);
var flag_img_h = Array (0,0);

var shirt_w = 150;
var shirt_h = 212; //240

var min_left = 109;
var min_top  = 73;
var img_offset_x = 8;
var img_offset_y = 8;
var flag_init_done = false;
var shirt_side = 0;

var editor = Array ('editor','back_editor');
var imgupload = Array ('imgupload','back_imgupload');
var image = Array ('image','back_image');
var side = Array ('','back_');

var menu_active = false;

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function chr(CharCode)
{
	return String.fromCharCode(CharCode);
}

function switch_feedback() {
	if ($('feedback').style.left == '0px')
		$('feedback').style.left = '-10px';
	else
		$('feedback').style.left = '0px';
}

function switch_visible(id,val) {
	if (val) {
		document.getElementById(id).style.display='block';
	} else {
		document.getElementById(id).style.display='none';
	}
	return false;
}

function createRequestObject() {
    var xmlhttp;
    
    try {
    	
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        var MSXML_XMLHTTP_PROGIDS = new Array(
          'MSXML2.XMLHTTP.5.0',
          'MSXML2.XMLHTTP.4.0',
          'MSXML2.XMLHTTP.3.0',
          'MSXML2.XMLHTTP',
          'Microsoft.XMLHTTP');
        var success = false;
        for (var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
            try {
                xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
                success = true;
            } catch (e) {}
        }
        if (! success) {
            alert ('No AJAX-Support for this browser :(!');
        }
    }
    return xmlhttp;
}

function callInProgress(xmlhttp) {
    switch ( xmlhttp.readyState ) {
        case 1:
              case 2:
              case 3:
            return true;
        break;
        default:
            return false;
        break;
    }
}

var http = createRequestObject();

function getTags(parent_uid,language_uid) {
    if (!callInProgress(http))
      {      	
        http.open('get', './fileadmin/scripts_public/tags.php?parent_uid='+parent_uid+'&language_uid='+language_uid,true);
        http.onreadystatechange = getTagsHandle;
        http.send(null);
      }
}

function getTagsHandle () {
	if(http.readyState == 4){
        var response = http.responseText;
        if ($('tags'))
        	$('tags').innerHTML = response;
        if ($('tags'))
        	Effect.Appear ($('tags'));
    }
}

function checkCoupon (coupon,mode) {
		if (!callInProgress(http))
    {
        http.open('get', 'http://'+document.domain+'/fileadmin/scripts_public/coupon.php?coupon='+coupon+'&mode='+mode,true);
        http.onreadystatechange = checkBurgerking2;
        http.send(null);
    }
}
function getClipartDim (fileName) {
    if (!callInProgress(http))
    {    	
        http.open('get', 'http://'+document.domain+'/fileadmin/scripts_public/clipart.php?file='+fileName,true);
        http.onreadystatechange = getClipartDimHandle;
        http.send(null);
    }
}

function getClipartDimHandle () {
	if(http.readyState == 4){
		var response = http.responseText;
		response = response.split (';');
		flag_scale[shirt_side] = 100;    
		$(side[shirt_side]+'image_scale').value = 100;

		myparent = $(imgupload[shirt_side]).parentNode;
		$(imgupload[shirt_side]).parentNode.removeChild($(imgupload[shirt_side]));
		dummy = document.createElement("IMG");		
		dummy.id = imgupload[shirt_side];
		dummy.style.position = 'absolute';		
		dummy.style.top = '80px';
		dummy.style.left = '118px';
		dummy.style.zIndex = '29';				
		$('shirt').appendChild(dummy);
		new Draggable(imgupload[shirt_side],{revert:false}); 	

    	$(imgupload[shirt_side]).src    = response[0];    	    	
    	$(imgupload[shirt_side]).width  = response[1];	    	
    	$(imgupload[shirt_side]).height = response[2];
    	
    	l = (img_offset_x + min_left + Math.round((shirt_w - response[1]) / 2));
    	//t = (img_offset_y + min_top  + Math.round((shirt_h - response[2]) / 2));
    	t = (img_offset_y + min_top);
    	$(imgupload[shirt_side]).style.left = l+"px";
    	$(imgupload[shirt_side]).style.top  = t+"px";
        $(side[shirt_side]+'image_left').value = l - min_left - img_offset_x;
        $(side[shirt_side]+'image_top').value  = t - min_top  - img_offset_y;

    	flag_img_w[shirt_side] = response[1];
    	flag_img_h[shirt_side] = response[2];
    	$(side[shirt_side]+'image_width').value  = flag_img_w[shirt_side];
    	$(side[shirt_side]+'image_height').value = flag_img_h[shirt_side];
    	
    	$(image[shirt_side]).value = response[0]; 
    	$('cc').style.display = "block";
    	
    	debug();
	}
}

function initSelect (e) {
	while ($(e).length > 0) {
		$(e).options[0] = null;	
	}
}

function styleEditor () {

	$(editor[shirt_side]).style.color = '#'+flag_color[shirt_side];
	$(editor[shirt_side]).style.fontSize = flag_size[shirt_side]+"px";
	$(editor[shirt_side]).style.lineHeight = (parseInt(flag_size[shirt_side])+2)+"px";
	
	if (flag_bold[shirt_side]==1) 
		$(editor[shirt_side]).style.fontWeight = "bold";
	else
		$(editor[shirt_side]).style.fontWeight = "normal";
	if (flag_italic[shirt_side]==1) 
		$(editor[shirt_side]).style.fontStyle = "italic";
	else
		$(editor[shirt_side]).style.fontStyle = "normal";

	if (flag_under[shirt_side]==1) {
		$(editor[shirt_side]).style.textDecoration = "underline";
	} else
		$(editor[shirt_side]).style.textDecoration = "none";
	switch (flag_font[shirt_side]) {
		case "times":	$(editor[shirt_side]).style.fontFamily = "Times, Times New Roman";
						break;
		case "arial":	$(editor[shirt_side]).style.fontFamily = "Helvetica, Arial, sans-serif";
						break;
		case "comic":	$(editor[shirt_side]).style.fontFamily = "Comic Sans MS";
						break;
		case "courier":	$(editor[shirt_side]).style.fontFamily = "Courier New, Courier";
						break;
		case "palatino":$(editor[shirt_side]).style.fontFamily = "Palatino Linotype";
						break;
		case "verdana":	$(editor[shirt_side]).style.fontFamily = "Verdana";
						break;
		case "trebuchet":$(editor[shirt_side]).style.fontFamily = "Trebuchet MS";
						break;
		case "tahoma":$(editor[shirt_side]).style.fontFamily = "Tahoma";
						break;
	}
	
	debug ();
}

function toggleShirtSide (id) {	
	if (language == 0) {
		back = 'Back';
		front = 'Front';
	} else if (language == 2) { // ungarisch
		back = 'hátulja';
		front = 'eleje';
	} else if (language == 3) { // franzoesisch
		back = 'Dos';
		front = 'Devant';
	} else if (language == 4) { // flaemisch
		back = 'achter';
		front = 'voor';
	} else if (language == 5) { // italienisch
		back = 'Indietro';
		front = 'Avanti';
	} else if (language == 6) { // tschechisch
		back = 'dozadu';
		front = 'dop&#345;edu';
	} else if (language == 7) { // englisch
		back = 'Back';
		front = 'Front';
	} else if (language == 8) { // slowenisch
		back = 'Zadaj';
		front = 'Spredaj';
	} else if (language == 9) { // slowakisch
		back = 'Dozadu';
		front = 'Dopredu';
	} else {
		back = 'Hinten';
		front = 'Vorne';
	}
	if (!shirtsides[id]) shirtsides[id] = 0;
	
	if (shirtsides[id]==0) {
		$('sidelink'+id).innerHTML = '&lt; '+front+' &gt;';		
		shirtsides[id]=1;
		$('shirtimage'+id).src = shirts[id][1];
		$('shirtdesign'+id).src = shirts[id][3];
	} else {
		$('sidelink'+id).innerHTML = '&lt; '+back+' &gt;';		
		shirtsides[id]=0;
		$('shirtimage'+id).src = shirts[id][0];
		$('shirtdesign'+id).src = shirts[id][2];
	}
}

function setTag (tag) {	
	//temp = ' ' + $('tagstring').value + ' ';
	if ($('tagstring').value.indexOf(tag)==-1) {
		if ($('tagstring').value != '') $('tagstring').value = ', ' + $('tagstring').value;
		$('tagstring').value = tag + $('tagstring').value
	}
}

function addColor(r, g, b) {
	var red = colorLevels[r];
    var green = colorLevels[g];
    var blue = colorLevels[b];
    addColorValue(red, green, blue);
}
     
function addColorValue(r, g, b) {
	colorArray[colorArray.length] = '#' + r + r + g + g + b + b;
}     

function genColors() {
	addColorValue('0','0','0');
	addColorValue('3','3','3');
	addColorValue('6','6','6');
	addColorValue('8','8','8');
	addColorValue('9','9','9');                
	addColorValue('A','A','A');
	addColorValue('C','C','C');
	addColorValue('E','E','E');
	addColorValue('F','F','F');                                
		
	for (a = 1; a < colorLevels.length; a++)
		addColor(0,0,a);
	for (a = 1; a < colorLevels.length - 1; a++)
		addColor(a,a,5);
	
	for (a = 1; a < colorLevels.length; a++)
		addColor(0,a,0);
	for (a = 1; a < colorLevels.length - 1; a++)
		addColor(a,5,a);
		
	for (a = 1; a < colorLevels.length; a++)
		addColor(a,0,0);
	for (a = 1; a < colorLevels.length - 1; a++)
		addColor(5,a,a);			
		
	for (a = 1; a < colorLevels.length; a++)
		addColor(a,a,0);
	for (a = 1; a < colorLevels.length - 1; a++)
		addColor(5,5,a);
		
	for (a = 1; a < colorLevels.length; a++)
		addColor(0,a,a);
	for (a = 1; a < colorLevels.length - 1; a++)
		addColor(a,5,5);
	
	for (a = 1; a < colorLevels.length; a++)
		addColor(a,0,a);			
	for (a = 1; a < colorLevels.length - 1; a++)
		addColor(5,a,5);
		
	return colorArray;
}

function setColor (c)
{		
	flag_color[shirt_side] = c.substr(1);
	$(side[shirt_side]+'font_color').value = c.substr(1);
	togglePanel ("colorpicker");		
	
	$(editor[shirt_side]).style.color = c;	
	debug ();
}

function getColorTable() {		
	 
     var colors = genColors();
  	 var tableCode = '';
  	 var perline = 9;
     tableCode += '<table border="0" cellspacing="1" cellpadding="1">';
     for (i = 0; i < colors.length; i++) {
          if (i % perline == 0) { tableCode += '<tr>'; }
          tableCode += '<td bgcolor="#FFFFFF"><a style="outline: 1px solid #000000; color: ' 
          	  + colors[i] + '; background: ' + colors[i] + ';font-size: 10px;" title="' 
          	  + colors[i] + '" href="javascript:setColor(\'' + colors[i] + '\');">&nbsp;&nbsp;&nbsp;</a></td>';
          if (i % perline == perline - 1) { tableCode += '</tr>'; }
     }
     if (i % perline != 0) { tableCode += '</tr>'; }
     tableCode += '</table>';
  	 return tableCode;
 }
 
function resizeEditor()
{
	//if ($(editor[shirt_side]).value.match(new RegExp('(\\n)','g'))) {		
	//	lines = $(editor[shirt_side]).value.match(new RegExp('(\\n)','g')).length + 1;
	//} else {
	//	lines = 1;
	//}
	//$(editor[shirt_side]).style.height = (lines*( Math.round(flag_size[shirt_side]*1.3) ) )+"px";
}
 
function setSize (s)
{
	flag_size[shirt_side] = s;
	$(side[shirt_side]+'font_size').value = s;
	$(editor[shirt_side]).style.fontSize = s+"px";
	$(editor[shirt_side]).style.lineHeight = (parseInt(s)+2)+"px";
	
	togglePanel ("sizepicker");	
	resizeEditor();
	debug ();
}

function setFont (f)
{
	flag_font[shirt_side] = f;
	$(side[shirt_side]+'font').value = f;
	togglePanel ("fontpicker");	
	styleEditor();
	debug ();
}
 
function getSizeTable()
{	
	var tableCode = '<select id="sizetable" size=5 onclick="setSize(this.value);" style="width:100px;height:200px;">';
	var sel = '';
	for (i = 10; i <= 40; i=i+2)
	{
		sel = '';
		if (i == 16 && !flag_size[shirt_side]) 
			sel = "selected";
		else if (flag_size[shirt_side] == i)
			sel = "selected";
			
		label = '';
		switch (i) {
			case 4:  label = '';	
			case 6:  label = '';	
			case 8:  label = '';	
			case 10: label = '';	
			case 12: label = '';	
			case 14: label = '';	
			case 16: label = '';	
			case 18: label = '';	
			case 20: label = '';	
			case 22: label = '';	
			case 24: label = '';	
			case 26: label = '';	
			case 28: label = '';	
			case 30: label = '';	
			case 32: label = '';	
			case 34: label = '';	
			case 36: label = '';	
			case 38: label = '';	
			case 40: label = '';	
		}
		label = Math.round((i / 16) * 100) + "%";
		tableCode += '<option '+sel+' value="'+i+'">'+label;
	}	
	tableCode += '</select>';
	return tableCode;
}

function getFontTable()
{
	var tableCode = '<select size=8 onclick="setFont(this.value);" style="width:200px;">';
	tableCode += '<option selected value="arial">Arial';
	tableCode += '<option value="comic">Comic Sans MS';
	tableCode += '<option value="courier">Courier';
	//tableCode += '<!-- <option value="palatino">Palatino Linotype -->';
	tableCode += '<option value="tahoma">Tahoma';
	tableCode += '<option value="times">Times New Roman';
	tableCode += '<option value="trebuchet">Trebuchet MS';
	tableCode += '<option value="verdana">Verdana';
	
	tableCode += '</select>';
	return tableCode;
}

function togglePanel(p,val)
{
	if (val != null) {
		document.getElementById(p).style.display = val;
	} else {
		if (document.getElementById(p).style.display == 'block') {
			document.getElementById(p).style.display = "none";
		} else {
			document.getElementById(p).style.display = "block";
		}
	}
}
     
function toggleButton(button)
{	
	var fileName;
	
	if (button == "b_bold") {
		if (document.getElementById (editor[shirt_side]).style.fontWeight != "bold") {
			fileName = 'h_bold.png';
			flag_bold[shirt_side] = true;
			$(editor[shirt_side]).style.fontWeight = "bold";
			document.getElementById (side[shirt_side]+"font_bold").value = 1;
		} else {
			fileName = 'b_bold.png';
			flag_bold[shirt_side] = false;
			$(editor[shirt_side]).style.fontWeight = "normal";
			document.getElementById (side[shirt_side]+"font_bold").value = 0;
		}
	} else if (button == "b_italic") {
		if (document.getElementById (editor[shirt_side]).style.fontStyle != "italic") {
			fileName = 'h_italic.png';
			flag_italic[shirt_side] = true;
			$(editor[shirt_side]).style.fontStyle = "italic";
			document.getElementById (side[shirt_side]+"font_italic").value = 1;
		} else {
			fileName = 'b_italic.png';
			flag_italic[shirt_side] = false;
			$(editor[shirt_side]).style.fontStyle = "normal";
			document.getElementById (side[shirt_side]+"font_italic").value = 0;
		}
	} else if (button == "b_under") {
		if (document.getElementById (editor[shirt_side]).style.textDecoration != "underline") {
			fileName = 'h_ul.png';
			flag_under[shirt_side] = true;
			$(editor[shirt_side]).style.textDecoration = "underline";
			document.getElementById (side[shirt_side]+"font_underline").value = 1;
		} else {
			fileName = 'b_ul.png';
			flag_under[shirt_side] = false;
			$(editor[shirt_side]).style.textDecoration = "none";
			document.getElementById (side[shirt_side]+"font_underline").value = 0;
		}
	}
	$(button).src = "/fileadmin/i/" + fileName;
	debug ();	
	$("editor").focus();
}

function toggleLogIn() {

	if ($("register").style.display=="none")
		$("register").style.display = "";
	else
		$("register").style.display = "none";

	if ($("loginmask").style.display=="none")
	{
		$("loginmask").style.display = "";
		if ($("uploadframe")) { $("uploadframe").style.display = "none"; }
	}
	else 
		$("loginmask").style.display = "none";
	

	
}

function toggleUpload() {
	
	if (document.getElementById("uploadframe").style.display=="none")
	{
		document.getElementById("uploadframe").style.display = "";
		if (document.getElementById("loginmask"))
			document.getElementById("loginmask").style.display = "none";
		if (document.getElementById("loginmask"))
			document.getElementById("register").style.display = "none";
	}
	else 
		document.getElementById("uploadframe").style.display = "none";	
}

function centerDesign() {
	w = (img_offset_x + min_left + Math.round((shirt_w - $(side[shirt_side]+'imgupload').width) / 2));
	$(side[shirt_side]+'imgupload').style.left = w + "px";
	$(side[shirt_side]+'image_left').value = (w - min_left - img_offset_x);
	
	//h = 1+ (img_offset_y + min_top  + Math.round((shirt_h - $('imgupload').height) / 2));
	//if (h < min_top) h = min_top;
	h = img_offset_y + min_top;
	$(side[shirt_side]+'imgupload').style.top  = h + "px";
	$(side[shirt_side]+'image_top').value = (h  - min_top - img_offset_y);
	
	debug();
}

function clearDesign() {	
	var now = new Date();
	
	$(imgupload[shirt_side]).src = 'fileadmin/i/blank.gif';	
	$(image[shirt_side]).value = ''; 
	$('cc').style.display = "none";
	$(imgupload[shirt_side]).src = 'fileadmin/i/blank.gif';	
}

function chgClass (theClass, val) {
	elements = document.getElementsByClassName(theClass);
	for (i=0; i < elements.length; i++) {
		elements[i].className = theClass + ' ' + val;		
	}
}

function newWindow(newContent,x,y,scroll)
{
	if (scroll == 1)
    {
		stil = "right=10, top=10,width="+x+",height="+y+", toolbar=no,scrollbars=yes, resizable=yes";
	} else {
    	stil = "right=10, top=10,width="+x+",height="+y+", toolbar=no,scrollbars=no, resizable=no";
	}
    winContent = window.open(newContent, '_blank',stil);
}

function resetImage () {
	frames['uploadframe'].location.href = '/'+	upload_path;	
}

function processImage (fileName,new_w,new_h,new_maxzoom)
{
	$(side[shirt_side]+'image').value = fileName;
	$(side[shirt_side]+'imgupload').src = './fileadmin/magic/files/'+fileName;
	$(side[shirt_side]+'imgupload').style.width = new_w+'px';
	$(side[shirt_side]+'imgupload').style.height = new_h+'px';
	
	setTimeout ('document.getElementById(\'uploadframe\').style.display=\'none\';'+
			'frames[\'uploadframe\'].location.href = \'/\'+upload_path;'+			
			'flag_img_w[shirt_side] = '+new_w+';'+
			'flag_img_h[shirt_side] = '+new_h+';'+
			'$(side[shirt_side]+\'image_width\').value  = '+new_w+';'+
			'$(side[shirt_side]+\'image_height\').value = '+new_h+';'+
			'centerDesign();'+
			'$(\'cc\').style.display = "block";',2000);	
	
	flag_scale[shirt_side] = 100;	
	$(side[shirt_side]+'image_scale').value = flag_scale[shirt_side];		
	debug();
}

function zoom (f) 
{	
	f = parseInt(f);
	flag_scale[shirt_side] = parseInt(flag_scale[shirt_side]);
	
	flag = false;
	if ($('uploadframe')) {
		if ($('uploadframe').style.display == 'none')
			flag = true;
	} else {
		flag = true;
	}
	if (true)
	{
        if (! (flag_img_w[shirt_side]>0)) {
            flag_img_w[shirt_side] = $(side[shirt_side]+'imgupload').width;
            flag_img_h[shirt_side] = $(side[shirt_side]+'imgupload').height;
            $(side[shirt_side]+'image_width').value  = flag_img_w[shirt_side];
            $(side[shirt_side]+'image_height').value = flag_img_h[shirt_side];
	    }

		if ((flag_scale[shirt_side]+f) >= 0 && (flag_scale[shirt_side]+f) <= 100) {
			flag_scale[shirt_side] = flag_scale[shirt_side] + f;		
		}

		$(side[shirt_side]+'imgupload').style.width = (flag_img_w[shirt_side] / 100) * flag_scale[shirt_side]+'px';
		$(side[shirt_side]+'imgupload').style.height = (flag_img_h[shirt_side] / 100) * flag_scale[shirt_side]+'px';
		
		$(side[shirt_side]+'image_scale').value = flag_scale[shirt_side];
	}
	debug();
}

function toggleSide(mode) {
	if (language == 0) {
		back = 'Back';
		front = 'Front';	
	} else if (language == 2) {
		back = 'hátulja';
		front = 'eleje';
	} else {
		back = 'Hinten';
		front = 'Vorne';
	}
	
	if (mode == 0) {	
		if (shirt_side==0) {
			$('back_editorframe').style.display = 'block';
			$('back_imgupload').style.display = 'block';
			$('editorframe').style.display = 'none';
			$('imgupload').style.display = 'none';
			$('sidelink').innerHTML = '&lt; '+front+' &gt;';		
			shirt_side=1;
		} else {
			$('back_editorframe').style.display = 'none';
			$('back_imgupload').style.display = 'none';
			$('editorframe').style.display = 'block';
			$('imgupload').style.display = 'block';
			$('sidelink').innerHTML = '&lt; '+back+' &gt;';
			shirt_side=0;
		}	
		$('shirt_inner').style.backgroundImage = 'url(fileadmin/i/shirts/'+gfx [$('mediatypes_uid').value][$('color').value][shirt_side]+')';	
	} else {		
		if (shirt_side==0) {
			if ($('sidelink'))
				$('sidelink').innerHTML = '&lt; '+front+' &gt;';					
			if ($('imgupload'))
				$('imgupload').style.display = 'none';
			if ($('back_imgupload'))
				$('back_imgupload').style.display = 'block';
			shirt_side=1;						
		} else {
			if ($('sidelink'))
				$('sidelink').innerHTML = '&lt; '+back+' &gt;';
			$('imgupload').style.display = 'block';
			$('back_imgupload').style.display = 'none';
			shirt_side=0;
		}			
		if ($('shirt'))		
			$('shirt_inner').style.backgroundImage = 'url(fileadmin/i/shirts/'+shirt_thumb[shirt_side]+')';	
	}	
	if ($('color'))
		checkColors ($('color').value);
	if ($(editor[shirt_side]))
		$(editor[shirt_side]).focus();
}

function loadImage (fileName)
{
    fileName = fileName.substr(0,fileName.length-4)+"2.png";
    getClipartDim (fileName);    
}

function checkColors (sel) {
	
	if (sel!='' && $('reload').value == '') {

		if (colors[sel][1]=='000000') {
						
			if (flag_color[0]=='000000') {				
				$(editor[0]).style.color = '#FFFFFF';	
				flag_color[0] = 'FFFFFF';
				$(side[0]+'font_color').value = 'FFFFFF';	
			}
			if (flag_color[1]=='000000') {				
				$(editor[1]).style.color = '#FFFFFF';	
				flag_color[1]= 'FFFFFF';
				$(side[1]+'font_color').value = 'FFFFFF';			
			}
			$('imgtextcontainer').style.borderColor = '#FFFFFF';
			
		} else {			
			
			if (flag_color[0]=='FFFFFF') {			
				$(editor[0]).style.color = '#000000';	
				flag_color[0] = '000000';
				$(side[0]+'font_color').value = '000000';
			}			
			if (flag_color[1]=='FFFFFF') {
				$(editor[1]).style.color = '#000000';	
				flag_color[1] = '000000';
				$(side[1]+'font_color').value = '000000';
			}
			$('imgtextcontainer').style.borderColor = '#000000';
		}
	}
	debug();	
}

function initMeta()
{
	$('tagstring').value = '';	
}

function replace(Expression, Find, Replace)
{
	var temp = Expression;
	var a = 0;

	for (var i = 0; i < Expression.length; i++) 
	{
		a = temp.indexOf(Find);
		if (a == -1)
			break
		else
			temp = temp.substring(0, a) + Replace + temp.substring((a + Find.length));
	}

	return temp;
}

function init() {		
	if ($('reload').value != 3) {
		
		//new Draggable('editorframe',{revert:false});
 		new Draggable('imgupload',{revert:false}); 	
 	
 		//new Draggable('back_editorframe',{revert:false});
 		new Draggable('back_imgupload',{revert:false}); 	
 	}
 	 	
 	if ($('reload').value != '' && $('reload').value != 4) { 		 	
 		
 		flag_color[0] = $('font_color').value;
 		flag_color[1] = $('back_font_color').value;
 		
 		flag_size[0] = $('font_size').value;
 		flag_size[1] = $('back_font_size').value; 		 		
 		
 		if ($('sizetable'))
 			$('sizetable').value = flag_size[0];
 		
		flag_font[0] = $('font').value;
		flag_font[1] = $('back_font').value; 		
 		
 		flag_bold[0] = $('font_bold').value;
 		flag_bold[1] = $('back_font_bold').value;
 		
 		flag_italic[0] = $('font_italic').value;
 		flag_italic[1] = $('back_font_italic').value;
 		
 		flag_under[0] = $('font_underline').value;
 		flag_under[1] = $('back_font_underline').value;
 		
 		flag_scale[0] = $('image_scale').value;
 		flag_scale[1] = $('back_image_scale').value;
 		
 		if (flag_size[1]!="")
 			$('back_editor').style.fontSize = flag_size[1]+"px"; 		
 		if (flag_color[1]!="")
 			$('back_editor').style.color = '#'+flag_color[1];
 		
 		if (flag_size[0]!="")
 			$('editor').style.fontSize = flag_size[0]+"px"; 		
 		if (flag_color[0]!="")
 			$('editor').style.color = '#'+flag_color[0];
 		
 		if (flag_bold  [1]==1) $('back_editor').style.fontWeight = "bold";
 		if (flag_italic[1]==1) $('back_editor').style.fontStyle = "italic";
 		if (flag_under [1]==1) $('back_editor').style.textDecoration = "underline"; 		
 		
 		flag_shirt_color = $('colors_uid').value; 		 		
 	}
 	
 	if ($('reload').value != '') {
 		if ($('image').value!='') {
 			
 			if ($('cc'))
 				$('cc').style.display = 'block';
 			if ($('image').value.substr(0,18) != 'fileadmin/clipart/' &&
 			    $('image').value.substr(0,14) != 'fileadmin/api/') { 			
 				$('imgupload').src = 'fileadmin/magic/files/'+$('image').value;
 			} else {
	 			$('imgupload').src = $('image').value;	 			
	 		}
	 		$('imgupload').style.left = min_left + img_offset_x + parseInt($('image_left').value)+"px";
	 		$('imgupload').style.top  = min_top  + img_offset_y + parseInt($('image_top').value)+"px";	 		
	 		$('imgupload').style.width  = $('image_width').value+'px';
	 		$('imgupload').style.height = $('image_height').value+'px';	 			 		
 		}
 		
 		if ($('back_image').value!='') { 			
	 		if ($('back_image').value.substr(0,18) != 'fileadmin/clipart/') 
 				$('back_imgupload').src = 'fileadmin/magic/files/'+$('back_image').value;
 			else
	 			$('back_imgupload').src = $('back_image').value;
	 		$('back_imgupload').style.left = min_left + img_offset_x + parseInt($('image_left').value)+"px";
	 		$('back_imgupload').style.top  = min_top  + img_offset_y + parseInt($('image_top').value)+"px";	 		
	 		$('back_imgupload').width  = $('back_image_width').value;
	 		$('back_imgupload').height = $('back_image_height').value;
 		}
 		
 		zoom(0); 		
 		debug();
 		
 		if ($('reload').value == 2) {

 			$('text').value = replace ($('text').value, "###$$$###", '\n'); 			
 			$('back_text').value = replace ($('back_text').value, "###$$$###", '\n'); 			
 			
 			$(editor[0]).value = $('text').value;	
 			$(editor[1]).value = $('back_text').value;
 		} else if ($('reload').value == 3) {
 			
 			$('text').value = replace ($('text').value, "###$$$###", '<br/>'); 			 			
 			$('text').value = replace ($('text').value,  ' ', '&nbsp;');
 			
 			$('back_text').value = replace ($('back_text').value, "###$$$###", '<br/>'); 			 			
 			$('back_text').value = replace ($('back_text').value,  ' ', '&nbsp;');
 			
 			$(editor[0]).innerHTML = $('text').value;	
 			$(editor[1]).innerHTML = $('back_text').value; 			
 		}
 	} else {
 		$(side[0]+'text_left').value = 0;
 		$(side[1]+'text_left').value = 0;
 	
 		$(side[0]+'text_top').value = 0;
 		$(side[1]+'text_top').value = 0;		
 		
 	 	$(side[0]+'image_left').value = 0;
 		$(side[1]+'image_left').value = 0;
 	
 		$(side[0]+'image_top').value = 0;
 		$(side[1]+'image_top').value = 0;
 		
 		$(side[0]+'font_color').value = flag_color[0];
	 	$(side[1]+'font_color').value = flag_color[1];
	 	
	 	$(side[0]+'font_size').value = flag_size[0];
	 	$(side[1]+'font_size').value = flag_size[1];
	 	
	 	$(side[0]+'font').value = flag_font[shirt_side];
	 	$(side[1]+'font').value = flag_font[shirt_side];  	
 	}

 	styleEditor(); 	 	
 	debug();
 	
 	if ($('reload').value != 3) {
		Droppables.add('shirt', 
		{
			onDrop: function (element)
		   	{
		   		var new_img_left = element.style.left.substr(0,element.style.left.length-2);	
		   	 	var new_img_top  = element.style.top.substr(0,element.style.top.length-2);	
		   	 		
		   		dummy_w = flag_img_w;	   	 	
				if (element.style.width!="")
					dummy_w = element.style.width.substr(0,element.style.width.length-2);
				dummy_h = flag_img_h;	   	 	
				if (element.style.height!="")
					dummy_h = element.style.height.substr(0,element.style.height.length-2);
					
		   	 	if (new_img_left < (img_offset_x+min_left))	element.style.left = img_offset_x+min_left+'px';
		   	 	if ((parseInt(new_img_left) + parseInt(dummy_w)) > (img_offset_x + min_left + shirt_w)) {
		   	 		element.style.left = (parseInt(img_offset_x) + parseInt(min_left) + parseInt(shirt_w) - parseInt(dummy_w))+'px';		   	 	
		   	 	}
		   	 			   	 	
		   	 	if (new_img_top  < (img_offset_y+min_top)) 	element.style.top  = img_offset_y+min_top+'px';
		   	 	if ((parseInt(new_img_top) + parseInt(dummy_h)) > (img_offset_y + min_top + shirt_h)) {
		   	 		element.style.top  = (parseInt(img_offset_y) + parseInt(min_top) + parseInt(shirt_h) - parseInt(dummy_h))+'px';
		   	 	}
		   	 		
		   	 	if (element.id == "editorframe" || element.id == "back_editorframe") {	
		   	 		$(side[shirt_side]+'text_left').value = element.style.left.substr(0,element.style.left.length-2) - min_left;
		   	 		$(side[shirt_side]+'text_top').value  = element.style.top.substr(0,element.style.top.length-2) - min_top;	   	 	
		   	 	} else if  (element.id == "imgupload" || element.id == "back_imgupload") {
		   	 		$(side[shirt_side]+'image_left').value = element.style.left.substr(0,element.style.left.length-2) - min_left - img_offset_x;
		   	 		$(side[shirt_side]+'image_top').value  = element.style.top.substr(0,element.style.top.length-2) - min_top - img_offset_y;
		   	 	}	   	 	
		   	 	debug();
		   	}
		} );
		Droppables.add('body', 
		{
			onDrop: function (element)
		   	{
		   		dropOffroad (element);
		   	}
		} );
		Droppables.add('content', 
		{
			onDrop: function (element)
		   	{
		   		dropOffroad (element);
		   	}
		} );	
	}
}

function dropOffroad (element) {
	element.style.left = img_offset_x+min_left+'px';
 	element.style.top  = img_offset_y+min_top+'px';
 	if (element.id == "imgtextcontainer") {
 		$('text_left').value = 0;	
 		$('text_top').value  = 0;	
 	} else if  (element.id == "imgupload" || element.id == "back_imgupload") {
 		$(side[shirt_side]+'image_left').value = 0;	
 		$(side[shirt_side]+'image_top').value  = 0;	
 	}	   	 	
 	debug();
 	debug2("image offroad!  repositioning...");
}

function debug2 (msg) {
	//$('debugstate').innerHTML = msg+"<br/>"+$('debugstate').innerHTML;
}

function part(msg) {
	if (msg.substr(0,18) == 'fileadmin/clipart/') msg = msg.substr(18);
	return msg.substr(0,8)+"...";
}

function debug() {
	if ($('debugval')) {
		
		$('debugval').innerHTML = '<table>'+
							   '<tr><td width=90><b>var</b></td><td width=60><b>front</b></td><td><b>back</b></td>'+
						       '<tr><td>text_left: 			</td><td>'+$('text_left').value + 'px'+'</td><td>'+$('back_text_left').value+'px</td></tr>'+
						       '<tr><td>text_top: 			</td><td>'+$('text_top').value  + 'px'+'</td><td>'+$('back_text_top').value +'px</td></tr>'+
							   '<tr><td>flag_color: 		</td><td>'+flag_color[0]		+'</td><td>'+flag_color[1]			+'</td></tr>'+
							   '<tr><td>flag_size: 			</td><td>'+flag_size[0]			+'</td><td>'+flag_size[1]			+'</td></tr>'+
							   '<tr><td>flag_font: 			</td><td>'+flag_font[0]			+'</td><td>'+flag_font[1]			+'</td></tr>'+						   
							   '<tr><td>flag_italic:		</td><td>'+flag_italic[0]		+'</td><td>'+flag_italic[1]			+'</td></tr>'+
							   '<tr><td>flag_bold:  		</td><td>'+flag_bold[0]			+'</td><td>'+flag_bold[1]			+'</td></tr>'+
							   '<tr><td>flag_under: 		</td><td>'+flag_under[0]		+'</td><td>'+flag_under[1]			+'</td></tr>'+
							   '<tr><td>flag_scale: 		</td><td>'+flag_scale[0]		+'</td><td>'+flag_scale[1]			+'</td></tr>'+						   	   
							   '<tr><td>image_left:			</td><td>'+$('image_left').value+'px</td><td>'+$('back_image_left').value+'px</td></tr>'+
							   '<tr><td>image_top:			</td><td>'+$('image_top').value +'px</td><td>'+$('back_image_top').value+'px</td></tr>'+
							   
							   '<tr><td>image:				</td><td>'+part($('image').value)+'</td><td>'+part($('back_image').value)+'</td></tr>'+
							   '<tr><td>imgupload_left:		</td><td>'+$('imgupload').style.left+'</td><td>'+$('back_imgupload').style.left+'</td></tr>'+
							   '<tr><td>imgupload_top:		</td><td>'+$('imgupload').style.top +'</td><td>'+$('back_imgupload').style.top +'</td></tr>'+
							   '<tr><td>imgupload_width:	</td><td>'+$('imgupload').style.width+'</td><td>'+$('back_imgupload').style.width+'</td></tr>'+
							   '<tr><td>imgupload_height:	</td><td>'+$('imgupload').style.height+'</td><td>'+$('back_imgupload').style.height +'</td></tr>'+
							   '<tr><td>flag_shirt_color:	</td><td>'+flag_shirt_color		+'</td><td>						  </td></tr>'+					
							   '</table>';
	}
}

function toggleDebug()
{
	if ($('debugval').style.display!='none') {
		$('debugval').style.display='none';  
		$('debugstate').style.display='none';  				
		$('debugheader').innerHTML = '<b><a href="javascript:toggleDebug();">debug (+)</a></b>';		
	} else {
		$('debugval').style.display='block';
		$('debugstate').style.display='block';		
		$('debugheader').innerHTML = '<b><a href="javascript:toggleDebug();">debug (-)</a></b>';
	}
}


function showToolbar() {
	new Effect.Appear('textformat', {duration: 0.5});
	//$('textformat').style.display = "";
}

function hideToolbar() {
	if(!menu_active)
		new Effect.Fade('textformat', {duration: 0.5});
}

function chgColor (sel) {	

	for (i = 0; i < c.length; i++) {		
		if (c[i] == sel) { 						
			debug2($('mediatypes_uid').value+"x"+sel+"x"+shirt_side);
			$('shirt_inner').style.backgroundImage = 'url(fileadmin/i/shirts/'+gfx [$('mediatypes_uid').value][sel][shirt_side]+')';			
			//flag_shirt_color = colors [sel][1];
			//$('colors_uid').value = c[i];			
			$('colors_uid').value = gfx [$('mediatypes_uid').value][sel][2];
		}
	}	
	checkColors (sel);
	debug();
}

function chgMediaTypes()
{	
	var mcc = Array();
	
	initSelect ('color');
	
	if (media_colors[$('mediatypes_uid').value] != null) {
		mcc = media_colors[$('mediatypes_uid').value].split (",");
	}
	for (i = 0; i < c.length; i++) {
		for (j = 0; j < mcc.length; j++) {
			if (mcc[j] == c[i]) {
				$('color').options[$('color').length] = new Option(colors[c[i]][0],c[i]);
			}
		}
	}
	if (typeof burgerking == 'undefined') {
		
	} else {
		if (burgerking) {
			if ($('mediatypes_uid').value == 78) {
				$('burgerking_text_queen').style.display='block';
				$('burgerking_text_king').style.display='none';
			} else if ($('mediatypes_uid').value == 76) {
				$('burgerking_text_queen').style.display='none';
				$('burgerking_text_king').style.display='block';
			} else {
				$('burgerking_text_queen').style.display='none';
				$('burgerking_text_king').style.display='none';
			}
		}
		if (bob || derStandard || burgerking) {
			s = 'size';
			m = 'mediatypes_uid';
			while ($(s).length > 0)
			{
				$(s).options[$(s).length-1] = null;
			}	
			
			mss = media_sizes[$(m).value];
			mss = mss.split (",");
			
			if (mss.length>0 && mss[0]!='') {
				for (i = 0; i < mss.length; i++) {		
		     		$(s).options[$(s).length] = 
		     			new Option(sizes[mss[i]],mss[i],false,false);
				}	
			}
		}
	}	
	
	chgColor($('color').options[$('color').selectedIndex].value);
	
	debug();	
}