function $(id)
{
	return document.getElementById(id);
}

function checkAmount(obj)
{
	if (isNaN(obj.value) || obj.value < 1)
	{
		obj.value = 1;
	}
}

function checkVariantAmount(obj)
{
	if (obj.value != '' && (isNaN(obj.value) || obj.value < 0))
	{
		obj.value = 0;
	}
}

function openWindow(url, width, height, print)
{
	var w = null;
	w = window.open(url, '', 'width=' + width +',height=' + height);	
	w.focus();
	
	if(print == true) w.print();
}

function closeWindow(url)
{
	if (url) window.opener.location.href = url;
	window.opener.focus();
	window.close();
}

function validateMail(email)
{  
	if (email.match(/^[a-zA-Z0-9_\.-]{2,}@[a-zA-Z0-9-]{2,}\.([a-zA-Z0-9-]{1,}\.)*[a-zA-Z]{2,}$/))
	{
		return true;
	}
	else
	{  
		return false;
	}
}

function closeWindowByKeyDown(keyEvent)
{
	if (!keyEvent)
	{
		keyEvent = window.event;
	}
	if (keyEvent.which)
	{
    	key = keyEvent.which;
 	}
 	else if (keyEvent.keyCode)
 	{
    	key = keyEvent.keyCode;
    }
    
    if (key == 88)
    {
    	closeWindow();
    }
}

function confirmCommand(message)
{
	return confirm(unescape(message));
}

function sendOrder(disablebuttons, sendstatus)
{	
	if (sendstatus == false) return false;

	if (disablebuttons)
	{
		for (var i = 0; i < disablebuttons.length; i++)
		{
			if ($(disablebuttons[i]))
			{
				$(disablebuttons[i]).style.visibility = 'hidden';
			}
		}	
	}
	
	viewportheight = (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight;
		
	x = Math.round((document.body.clientWidth / 2) -109);
	y = Math.round((viewportheight / 2));

	$('waitbox').style.left = x + 'px';
	$('waitbox').style.top = y + 'px';
	//nn
	$('waitbox').left = x;
	$('waitbox').top = y;
	//op
	if (navigator.userAgent.toLowerCase().indexOf('opera') != -1)
	{
		$('waitbox').style.pixelLeft = x;
		$('waitbox').style.pixelTop = y - document.body.scrollTop;
	}
	
	// show div
	$('waitbox').style.visibility = 'visible';
	
	return true;
}

function centerLayer()
{
	$('waitbox').style.width = screen.availWidth / 4;

	x = document.body.clientWidth / 2 - $('waitbox').offsetWidth / 2 + document.body.scrollLeft;
	y = document.body.clientHeight / 2 - $('waitbox').offsetHeight / 2 + document.body.scrollTop;

	$('waitbox').style.position = 'absolute';
	$('waitbox').style.left = x ;
	$('waitbox').style.top = y ;

	setTimeout('centerLayer()', 100);
}

function toggle(toggleid, openimg, closeimg)
{
	if (toggleid[0] && $(toggleid[0]))
	{
		if ($(toggleid[0]).style.display == 'none') 
		{		
			$(toggleid[0]).style.display = 'block';
			if (toggleid[1] && $(toggleid[1]) && closeimg)
			{
				$(toggleid[1]).src = closeimg;
			}
		}
		else 
		{   
			$(toggleid[0]).style.display = 'none';
			if (toggleid[1] && $(toggleid[1]) && openimg)
			{
				$(toggleid[1]).src = openimg;
			}	
		}
	}
}

function checkLogin(login_type, mandatory_fields)
{
	var error = 0;
	var msg = 'Es sind folgende Fehler beim Login aufgetreten:\n';
	
	if ($('login_mud_email').value == '')
	{
		error = 1;
		msg += '- Bitte tragen Sie eine E-Mail-Adresse ein\n';
	}
	
	if ($('login_mud_password').value == '')
	{
		error = 1;
		msg += '- Bitte tragen Sie ein Passwort ein\n';
	}	
	
	if (error != 0)
	{
		alert(unescape(msg));
		return false;		
	}	
	return true;
}

function checkRegister(reg_type, mandatory_fields)
{
	// {{{
	var error = 0;
	var msg = "Es sind folgende Fehler bei der Registrierung aufgetreten:\n";
	
	if ($('reg_mud_email').value == "" ||
		!validateMail($('reg_mud_email').value))
	{
		error = 1;
		msg += "- Bitte tragen Sie eine g%FCltige E-Mail-Adresse ein\n";
	}
	if ($('reg_mud_email_confirm').value == "")
	{
		error = 1;
		msg += "- Bitte tragen Sie eine Wiederholung der E-Mail-Adresse ein\n";
	}
	if ($('reg_mud_email_confirm').value != "" && $('reg_mud_email').value != "" &&
		$('reg_mud_email_confirm').value != $('reg_mud_email').value	
	) {
		error = 1;
		msg += "- E-Mail-Adresse und die Wiederholung der E-Mail-Adresse stimmen nicht %FCberein\n";
	}	

	if ($('reg_mud_password').value == "")
	{
		error = 1;
		msg += "- Bitte tragen Sie ein Passwort ein\n";
	}	
	if ($('reg_mud_password_confirm').value == "") {
		error = 1;
		msg += "- Bitte trage eine Passwort-Wiederholung ein\n";
	}
	if ($('reg_mud_password_confirm').value != "" && $('reg_mud_password').value != "" &&
		$('reg_mud_password_confirm').value != $('reg_mud_password').value	
	) {
		error = 1;
		msg += "- Passwort und Passwort-Wiederholung stimmen nicht %FCberein\n";
	}	
	if ($('reg_mud_password').value != "" && ($('reg_mud_password').value).length < 5)
	{
		error = 1;
		msg += "- Ihr Passwort muss min. 5 Buchstaben und Zahlen enthalten\n";
	}	
	
	if ($('reg_mup_anrede_r').selectedIndex == 0)
	{
		error = 1;
		msg += "- Bitte w%E4hle Sie eine Anrede\n";
	}
	
	if ($('reg_mup_vorname_r').value == "")
	{
		error = 1;
		msg += "- Bitte tragen Sie einen Vornamen ein\n";
	}
	
	if ($('reg_mup_name_r').value == "")
	{
		error = 1;
		msg += "- Bitte tragen Sie einen Nachnamen ein\n";
	}
	
	if (error != 0)
	{
		alert(unescape(msg));		
		return false;		
	}	
	
	return true;
	// }}}
}

function MandatoryField(id, message, is_email, compare_to) {
	this.obj = $(id);
	this.id = id;
	this.message = message;
	this.is_email = is_email;
	this.compare_to = compare_to;

	this.validate = function() {
		var error = 0;
		
		if(this.obj.value == '')
		{
			error = 1;
			return false;
		}
		
		if(this.is_email == true && !validateMail(this.obj.value))
		{
			error = 1;
			return false;
		}
		
		if(compare_to)
		{
			for(var i = 0; i < compare_to.length; i++)
			{
				if(compare_to[i] != '' &&
				   $(compare_to[i]))
				{
					if(compare_to[i].value != this.obj.value)
					{
						return false;
					}
				}
			}
		}
		
		return true;		
	};
}
