String.prototype.replaceChars = replaceChars;
String.prototype.trimText = trimText;
String.prototype.fixedSize = fixedSize;
String.prototype.fixedSizeRight = fixedSizeRight;

function Product (manuf, label, price, number, description) 
{
	this.manuf			= manuf;
	this.label			= label;
	this.price			= price;
	this.number			= number;
	this.description	= description;
	this.renderItem		= renderItem;
	this.renderInfo		= renderInfo;
}

function renderItem (row) 
{
	var html = "";
	var color = "";
	
	if (row%2)	color = "F2F2F2";
	else color = "E5E5E5";

	var shadow = row%2;
	
	var mymanuf = this.manuf;
	var mynumber = this.number;
	var myprice	= formatCurrency (this.price);
	
	html += '<tr>\n';
	html += '<td colspan="2" bgcolor="#' + color + '"><img src="pixel.gif" width="1" height="3"></td>\n';
	html += '<td class="stripe"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td colspan="3" bgcolor="#' + color + '"><img src="pixel.gif" width="1" height="3"></td>\n';
	html += '<td class="stripe"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '</tr>\n'; 
	
	html += '<tr>\n';
	html += '<td bgcolor="#' + color + '" class="hline" valign="bottom" align="right">' + mymanuf + '<br><img src="pixel.gif" width="1" height="5"></td>\n';
	html += '<td bgcolor="#' + color + '"><a name="item_' + row +'"><img src="pixel.gif" width="1" height="70"></a></td>\n';
	html += '<td class="stripe" align="center" valign="middle"><a href="javascript:void(0)" onMouseOver="window.status=\'Detailinformationen ansehen\';return true;"  onMouseOut="window.status=\'\';return true;" onClick="openInfoWindow(' + row + ');"><img src="prodpics/' + mynumber + '_s.jpg" border="0" alt="Detailinformationen ansehen"></a></td>\n';	
	html += '<td bgcolor="#' + color + '"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td bgcolor="#' + color + '" valign="bottom">\n';

	html += '<table width="390" border="0" cellspacing="0" cellpadding="0" bgcolor="#' + color + '">\n';
	html += '<tr>\n';
	html += '<td bgcolor="#' + color + '" width="221" valign="bottom" class="plain">' + this.label + '<br><img src="pixel.gif" width="1" height="5"></td>';
	html += '<td bgcolor="#' + color + '" width="84" valign="bottom" class="plain" align="right">' + myprice + '&nbsp;&euro;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="pixel.gif" width="1" height="5"></td>\n';
	html += '<td bgcolor="#' + color + '" width="85" valign="bottom" class="plain">' + mynumber + '<br><img src="pixel.gif" width="1" height="5"></td>\n';
	html += '</tr>\n';
	html += '</table>\n';

	html += '</td>\n';
	html += '<td bgcolor="#' + color + '" width="66" valign="bottom" align="center"><input type="text" class="quant" name="count_' + row + '" value="" size="2" onChange="checkinput(this);"><span class="plain">&nbsp;St&uuml;ck</span><br><img src="pixel.gif" width="1" height="5"></td>\n';
	html += '<td class="stripe"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '</tr>\n'; 

	html += '<tr>\n';
	html += '<td colspan="2" bgcolor="#' + color + '"><img src="pixel.gif" width="1" height="3"></td>\n';
	html += '<td class="stripe"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td colspan="3" bgcolor="#' + color + '"><img src="pixel.gif" width="1" height="3"></td>\n';
	html += '<td class="stripe"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '</tr>\n'; 
	
	return (html);
}


function renderCatalog() 
{
	var html = "";
	var catalog = getProductCatalog();
	var currentrowhead = 0;
	var tmp;
	
	for (i = 0; i < catalog.length; i++) {

		html += catalog[i].renderItem (i);		
	}

	return (html);
}

function renderInfo ()
{
	var html = "";
	
	var mynumber = this.number;
	var myprice	= formatCurrency (this.price);
//	var mylabel = this.label.replaceChars ("<br>", " ");
	var mylabel = this.label;
	var mymanuf = this.manuf;

	html += '<tr>\n';
	html += '<td colspan="2"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td><img src="pixel.gif" width="1" height="1"></td>\n';
//	html += '<td class="plain" valign="top" align="right">Anzahl: ' + mymanuf + '</td>\n';
	html += '<td class="plain" valign="top" align="right">&nbsp;</td>\n';
	html += '<td><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '</tr>\n';	

	html += '<tr>\n';
	html += '<td colspan="2" class="stripe"><img src="pixel.gif" width="1" height="15"></td>\n';
	html += '<td colspan="3"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '</tr>\n';
	
	html += '<tr>\n';
	html += '<td colspan="2" rowspan="2" class="stripe" valign="middle" align="center"><img src="prodpics/' + mynumber + '.jpg"></td>\n';
	html += '<td rowspan="2"><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '<td class="hline" valign="top">' + mylabel + '</td>\n';
	html += '<td><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '</tr>\n';

	html += '<tr>\n';
	html += '<td class="plain" valign="bottom"><p>' + this.description + '</p>\n';
	html += 'Preis: &euro; ' + myprice + '<br>Artikel-Nummer: ' + this.number + '</td>\n';
	html += '<td><img src="pixel.gif" width="1" height="1"></td>\n';
	html += '</tr>\n';
	
	return (html);
}

function formatCurrency (value)
{
	var currency = value.toString();
	
	if (currency.indexOf (".") >= 0)
	{
		var left = currency.substring (0, currency.indexOf ("."));
		var right = currency.substring (currency.indexOf (".")+1, currency.length);
		if (!left) left = "0";
		
		if (right.length < 2)	return (left + "," + right + "0");
		else return (left + "," + right);
	}
	else return (currency + ",00");
}


function formatNumber (number)
{
	return (number.substring (0, number.length-3) + "&nbsp;<b>" + number.substring (number.length-3, number.length) + "</b>");	
}


function shortNumber (number)
{
	return (number.substring (number.length-3, number.length));
}


function replaceChars (oldchars, newchars)
{
	var text = this;
	var olen = oldchars.length;
	var tmp = text.indexOf (oldchars);
	while (tmp >= 0)
	{
		text = text.substring (0, tmp) + newchars + text.substring (tmp+olen, text.length);
		tmp = text.indexOf (oldchars);
	}
	return (text);
}


function trimText (size)
{
	var text = this;
	var addon = "...";
	
	text = text.replaceChars ("<br>", " ");
	if (text.length > size)	text = text.substring (0, size-addon.length) + addon;

	return (text);
}


function fixedSize (size)
{
	var text = this;
	var textlen = text.length;
	
	if (textlen > size) return text.trimText (size);
	for (var i=0; i < (size - textlen); i++) text += " ";
	
	return (text);
}


function fixedSizeRight (size)
{
	var text = this;
	var textlen = text.length;
	
	if (textlen > size) return text.trimText (size);
	for (var i=0; i < (size - textlen); i++) text = " " + text;
	
	return (text);
}


function searchCatalog (string, begin)
{
	var catalog = getProductCatalog();
	string = string.toLowerCase();
	for (var i=begin; i < catalog.length; i++) if ((catalog[i].label.toLowerCase().indexOf (string) >= 0) || (catalog[i].number.indexOf (string) >= 0)) return i;
	return -1;
}


// debugging stuff
function dumpobject(ref) {
	var erg="";
	params = "width=560,height=500,resizable=1,status=1,scrollbars=1,toolbar=0,location=0,directories=0,menubar=1";
	fenster = window.open( "" , "prod", params);
	fenster.document.open();
	fenster.document.write("<font face=\"Courier New\" size=-1><br>");
	for(var i in ref) { 
		erg=erg+i+" = <b>"+ref[i]+"</b><br>";
	}
	fenster.document.write(erg);
	fenster.document.close();
}

function openKontakt() {
	
	var name="popupwin";
	var params="width=620,height=430,left=50,top=60,scrollbars=no,resizable=no,menubar=no";
	var url = "kontakt.htm";
	var kontakt = window.open(url, name, params)
}

function openAGB() {
	
	var name="popupwin";
	var params="width=620,height=430,left=50,top=60,scrollbars=1,resizable=no,menubar=no";
	var url = "agb.htm";
	var kontakt = window.open(url, name, params)
}

var popupWin=null;

