//
// Strings
//

var szCookieName = "leuze";

var szBasketNoOrderPosition = "Ihr Merkzettel ist leer!";
var szBasketRemoveAll = "Soll der Merkzettel wirklich gelöscht werden?"; 
var szBasketNameError = "Bitte tragen Sie den Namen ein, unter dem Sie die Bestellung speichern möchten? \nBitte verwenden Sie keine Leerzeichen und keine Satzzeichen";

var szBasketHeader = "Anfrage"; 
var szBasketText = "Bitte FAXEN Sie diese Anfrage an 07021 9413-31. Wir pr&uuml;fen die Artikel auf Verf&uuml;gbarkeit und schicken Ihnen umgehend ein verbindliches Angebot."; 
var szBasketArticle = "Artikel";
var szBasketNumber = "Artikel-Nr";
var szBasketAmount = "Menge";
var szBasketPriceArt = "";

var szBasketPrice = "";
var szBasketDelete = "Löschen";
var szBasketPhone = "Telefon: ";
var szBasketEmail = "E-Mail: ";

var szBasketOrdered = "Der Artikel wurde auf den Merkzettel übernommen.";

var szBasketSavedAdress = "Die Adresse wurde abgespeichert.";




// variables
//
cookieItems = new Array("theName","theArtnr","thePrice","Writing","theInfo","theQuantity");
var totPrice;


function StringNachFloat(wert)
{
	if( wert=='' )
		return 0;
	return parseFloat(wert.replace(",","."));
}

function FloatNachString(xwert)
{
	wert=String(xwert);
	pos=wert.indexOf(".");
	if(pos > 0)
		nwert=wert.substring(0,pos)+","+wert.substring(pos+1,pos+3);
	if(pos == 0)
		nwert= "0,"+wert.substring(pos+1,pos+3);
	if(pos == -1)
		nwert=wert+",00";
	
	posk=nwert.indexOf(",");
	laenge=nwert.length;
	nachkomma = nwert.substring(posk+1,laenge+1);
	
	if(nachkomma.length < 2)
		nwert = nwert+"0";
		
	return(nwert);
}

//
// cookie
//

function getCookie(name)
{
	var cname = name + "=";     
	var dc = document.cookie;    
	if (dc.length > 0)
	{
		begin = dc.indexOf(cname);
		if (begin != -1)
		{
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1) 
				end = dc.length;

			return unescape(dc.substring(begin, end));
		}
	}
	return null;
}

function setCookie(name, value, expires, path, domain, secure) 
{
	document.cookie = name + "=" + escape(value) + 
	((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
	((path == null) ? "" : "; path=" + path) +
	((domain == null) ? "" : "; domain=" + domain) +
	((secure == null) ? "" : "; secure");
}

function delCookie( name, path, domain ) 
{
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path == null) ? "" : "; path=" + path) +
		((domain == null) ? "" : "; domain=" + domain) +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

//
// storeUserDates speichert die Daten des Benutzers
//
function storeUserDates()
{
	str1 = document.kontakt.Vorname.value;
	str2 = document.kontakt.Name.value;
	str3 = document.kontakt.Strasse.value;
	str4 = document.kontakt.PLZ.value;
	str5 = document.kontakt.Ort.value;
	str6 = document.kontakt.Telefon.value;
	str7 = document.kontakt.Email.value;
	str8 = document.kontakt.Firma.value;
	str9 = document.kontakt.KdNr.value;
	str10 = document.kontakt.KdStatus.value;

	wert = str1+"~"+str2+"~"+str3+"~"+str4+"~"+str5+"~"+str6+"~"+str7+"~"+str8+"~"+str9+"~"+str10+"~~~~";
	expiration = new Date(new Date().getTime() + 63072000000);
	setCookie( 'userdates', wert, expiration );

	alert( szBasketSavedAdress );

    //document.kontakt.submit();
}

function fillContactControls()
{
	KontaktCookie = getCookie('userdates');
	if(KontaktCookie) {
		Kontakt = KontaktCookie.split("~");

		document.kontakt.Vorname.value=Kontakt[0];
		document.kontakt.Name.value=Kontakt[1];
		document.kontakt.Strasse.value=Kontakt[2];
		document.kontakt.PLZ.value=Kontakt[3];
		document.kontakt.Ort.value=Kontakt[4];
		document.kontakt.Telefon.value=Kontakt[5];
		document.kontakt.Email.value=Kontakt[6];
		document.kontakt.Firma.value=Kontakt[7];
		document.kontakt.KdNr.value=Kontakt[8];
		document.kontakt.KdStatus.value=Kontakt[9];
	}
}

//
// buyItem - überführt Artikel in den Warenkorb
//
function buyItem( name, artnr, price, writing, info )
{
	quantity = 1;

	if(getCookie(szCookieName))
	{
		var MyCookie = getCookie(szCookieName);

		var itemExists = MyCookie.indexOf("@" + artnr + "@");
		if( itemExists > 0 ) {

			sepp = MyCookie.substr( itemExists, 32 );
			items = sepp.split("@");
			newAmount = parseInt(items[2]) + 1;

			oldString = "@" + artnr + "@" + items[2] + "@"
			newString = "@" + artnr + "@" + newAmount + "@"

			MyCookie = MyCookie.replace(oldString, newString);
		} else {
			MyCookie = MyCookie + "["+name+"@"+artnr+"@"+quantity+"@"+price+"@"+writing+"@"+info+"@"+"]"
		}
		setCookie(szCookieName, MyCookie);
	} else {
		wert = "["+name+"@"+artnr+"@"+quantity+"@"+price+"@"+writing+"@"+info+"@"+"]"
		setCookie(szCookieName, wert);
	}


// alert( szBasketOrdered );

    document.location.reload();
}

//
// anzahl der artikel
//
function setQuantity( quantityFieldName )
{
	newQuantity = parseInt(document.kontakt['Menge_'+quantityFieldName].value);
	artnr = document.kontakt['Artikel_'+quantityFieldName].value;

	if(getCookie(szCookieName))
	{
		var MyCookie = getCookie(szCookieName);

		var itemExists = MyCookie.indexOf("@" + artnr + "@");
		if( itemExists > 0 ) {

			sepp = MyCookie.substr( itemExists, 32 );
			items = sepp.split("@");

			oldString = "@" + artnr + "@" + items[2] + "@"
			newString = "@" + artnr + "@" + newQuantity + "@"

			MyCookie = MyCookie.replace(oldString, newString);
		}
		setCookie(szCookieName, MyCookie);
	}
    document.location.href = "wk.htm";
}

//
// showBasketInfo - Zeigt Warenkorb in einer Tabelle an
//
function showBasketCount()
{
	itemStart = 0;
	itemIndex = 0;
	count = 0;

	fullList = getCookie(szCookieName);
	if(fullList) {

		for (var i=0; i<=fullList.length; i++) {

			itemStart = fullList.indexOf("[", itemStart);
			itemEnd = fullList.indexOf("]", itemStart);
 
			if(itemStart == -1 || itemEnd == -1) {
				break;
			}
			count++;
			itemStart = itemEnd;

		}
	}
	document.write(count);
}

function showBasketInfo()
{
	itemStart = 0;
	itemIndex = 0;
	totPrice = 0;

	fullList = getCookie(szCookieName);
	if(fullList) {

		for (var i=0; i<=fullList.length; i++) {

			itemStart = fullList.indexOf("[", itemStart);
			itemEnd = fullList.indexOf("]", itemStart);
 
			if(itemStart == -1 || itemEnd == -1) {
				break;
			}

			line = fullList.substring(itemStart+1, itemEnd);
			cookieItems = line.split("@");

			itemTotalPrice = StringNachFloat(cookieItems[3]) * cookieItems[2];
			totPrice = totPrice + itemTotalPrice;
			itemStart = itemEnd;

			document.writeln('<table cellSpacing="0" cellPadding="2" border="0"><tr>');
			document.writeln('<td class="infoBoxContents">' + cookieItems[2]+' x '+cookieItems[0] + '</td>');
			document.writeln('</tr></table>');
		}
		document.writeln('<hr>');
		document.writeln('<table cellSpacing="0" cellPadding="2" border="0"><tr>');
		document.writeln('<td class="infoBoxContents"></td>');
		document.writeln('</tr></table>');
	}
}

//
// showItems - Zeigt Warenkorb in einer Tabelle an
//
function showItems() 
{
	if(!getCookie(szCookieName)) {
		document.writeln(szBasketNoOrderPosition);
		return;
	}
	
	itemIndex=0;
	document.writeln('<table cellspacing="1" cellPadding="2" border="0" width="100%">');
	document.writeln('<tr>');
	document.writeln('<td><b>' + szBasketNumber + '</b></td>');
	document.writeln('<td><b>' + szBasketArticle + '</b></td>');
	document.writeln('<td><b>' + szBasketAmount + '</b></td>');
	document.writeln('<td align="right"><b>' + szBasketPriceArt + '</b></td>');
	document.writeln('<td align="right"><b>' + szBasketPrice + '</b></td>');

	document.writeln('</tr>');

	myCookie = getCookie(szCookieName);
	fullList = myCookie.substring(0, myCookie.length);
	totPrice = 0;
	itemList = 0;

	for (var i=0; i<=fullList.length; i++) {
		if (fullList.substring(i, i+1) == '[') {
			itemStart = i+1;
			itemIndex = 0;
		}
		if (fullList.substring(i, i+1) == ']') {

			itemEnd = i;
			cookieItems[itemIndex] = fullList.substring(itemStart, itemEnd);
			itemTotalPrice = StringNachFloat(cookieItems[3]) * cookieItems[2];
			itemList=itemList+1;
			totPrice = totPrice + itemTotalPrice;
			
			document.writeln('<tr>');
			document.writeln('<td class="infoBoxContents">'+cookieItems[1]);
			document.writeln('<input type="hidden" name="Artikel_'+itemList+'" value="'+cookieItems[1]+'"></td>');

			document.writeln('<td class="infoBoxContents">'+cookieItems[0]+'</td>');
			document.writeln('<td class="infoBoxContents">');
			document.writeln('<input size=4 name="Menge_' + itemList + '"');
			document.writeln('value="' + cookieItems[2] + '" onChange="setQuantity('+itemList+')"></td>');

			// document.writeln('<td class="infoBoxContents" align="right">'+cookieItems[3]+'</td>');
			// document.writeln('<td class="infoBoxContents" align="right">'+FloatNachString(itemTotalPrice)+'</td>');
			document.writeln('<td class="infoBoxContents" align="center">'+'<a class="butD" href="javascript:removeItem('+itemList+',\''+cookieItems[1]+'\')">' + szBasketDelete +'</a></td>');
			document.writeln('</tr>');
		} 

  		if (fullList.substring(i,i+1) == '@') 
  		{
			cookieItems[itemIndex] = fullList.substring(itemStart, i);
			itemStart = i+1;
			itemIndex = itemIndex + 1;
		}
	}
	document.writeln('<tr style="display: none;" >');
	document.writeln('<td class="infoBoxContents" colspan="4"></td>');
	document.writeln('<td align="right" style="border-top: 1px solid #000" class="infoBoxContents"><strong>' + FloatNachString(totPrice) + "</strong></td>");
	document.writeln('<td class="infoBoxContents"></td>');
	document.writeln('</tr>');
	document.writeln('</table>');
}

function removeItem( itemNo, artNo ) 
{
	newItemList = "";
	itemList = 0;

	myCookie = getCookie(szCookieName);
	fullList = myCookie.substring(0, myCookie.length);

	for(var i=0; i<=fullList.length; i++) 
	{
		if (fullList.substring(i,i+1) == '[') 
		{
			itemStart = i+1;
		}
		if (fullList.substring(i,i+1) == ']') 
		{
			itemEnd = i;
			itemList=itemList+1;
			
			var item = fullList.substring(itemStart, itemEnd);
			if( item.indexOf("@" + artNo + "@") < 0 )
				newItemList = newItemList+'['+fullList.substring(itemStart, itemEnd)+']';
		}
	}

	setCookie(szCookieName, newItemList);
	location = "wk.htm";
}

//
// clearBasket - removes all items from the basket
//
function clearBasket() 
{
	if( confirm( szBasketRemoveAll ) )
	{
		delCookie(szCookieName);
		location = "wk.htm";
	}
}

function openOrder( filename )
{
	var MyCookie = getCookie(filename);
	if(MyCookie)
	{
		setCookie( szCookieName, MyCookie );
	}
    document.location.href = "wk.htm";
}

function deleteOrder( order )
{
	var allOrders = getCookie('orders');
	orders = "";

	if( allOrders ) {
		all = allOrders.split("|");
		for( i=0; i<all.length; i++ ) {
			if( all[i] != order )
				orders = orders + all[i] + "|";
		}
		expiration = new Date(new Date().getTime() + 63072000000);
		setCookie( 'orders', orders, expiration );
	}
	delCookie( order );
    document.location.href = "wk.htm";
}

function saveOrder()
{
	filename = document.kontakt.filename.value;
	if( filename == "" ) {
		alert(szBasketNameError);
		return;
	}

	expiration = new Date(new Date().getTime() + 63072000000);

	orders = getCookie('orders');
	if( !orders )
		orders = "";

	if( itemExists = orders.indexOf(filename) < 0 ) {
		orders = orders + "|" + filename;
		setCookie( 'orders', orders, expiration );
	}

	var MyCookie = getCookie(szCookieName);
	if(MyCookie)
	{
		setCookie( filename, MyCookie, expiration );
	}
    document.location.href = "wk.htm";
}

function showOrders()
{
	var allOrders = getCookie('orders');

	if( allOrders ) {
		all = allOrders.split("|");
		for( i=0; i<all.length; i++ ) {
			if( all[i] != "" ) {
				document.writeln('<tr><td align="left">');
				document.writeln('<a href="javascript:openOrder(\'' + all[i] +'\')">' + all[i] + '</a>');
				document.writeln('</td><td>');
				document.writeln('<a class="butH" href="javascript:deleteOrder(\'' + all[i] +'\')">' + szBasketDelete +'</a>');
				document.writeln('</td></tr>');
			}
		}
	}
}

//
// printForm - builds a printable page
//
function printForm() 
{
	printWin = window.open("", "printFrame");

	printWin.document.clear();
	printWin.document.open();
	printWin.document.writeln('<body bgcolor="ffffff">');
	printWin.document.writeln('<table border="0" cellpadding="8">');
	printWin.document.writeln('<tr><td>');

	printWin.document.writeln(document.kontakt.Anrede.value + ' ');
	printWin.document.writeln(document.kontakt.Vorname.value + ' ');
	printWin.document.writeln(document.kontakt.Name.value + '<br>');
	printWin.document.writeln(document.kontakt.Firma.value + '<br>');
	printWin.document.writeln(' KD-NR.: ');
	printWin.document.writeln(document.kontakt.KdNr.value + '<br>');
	
	printWin.document.writeln(document.kontakt.Strasse.value + '<br>');
	printWin.document.writeln(document.kontakt.PLZ.value + ' ');
	printWin.document.writeln(document.kontakt.Ort.value + '<br><br>');

	printWin.document.writeln(szBasketPhone + document.kontakt.Telefon.value + '<br>');
	printWin.document.writeln(szBasketEmail + document.kontakt.Email.value + '<br><br>');
	printWin.document.writeln(' Status: ');
	printWin.document.writeln(document.kontakt.KdStatus.value + '<br><br><br>');
	
	printWin.document.writeln('TCS Textiles GmbH<br>');
	printWin.document.writeln('KATRIN LEUZE -COLLECTION-<br>');
	printWin.document.writeln('Fabrikstr. 18<br>');
	printWin.document.writeln('D-73277 Owen / Teck<br>');
	printWin.document.writeln('</td></tr></table>');

	if(!getCookie(szCookieName)) {
		printWin.document.writeln(szBasketNoOrderPosition);
		return;
	}

	itemIndex=0;
	printWin.document.writeln('<table cellSpacing="4" cellPadding="2" border="0">');
	printWin.document.writeln('<tr><td><p>&nbsp;</td></tr>');

	printWin.document.writeln('<tr><td colspan="3"><p><b>'+szBasketHeader+'</b><br></td></tr>');
	printWin.document.writeln('<tr><td colspan="3"><p>'+szBasketText+'</td></tr>');
	printWin.document.writeln('<tr><td><p>&nbsp;</td></tr>');

	printWin.document.writeln('<tr>');
	printWin.document.writeln('<td><p><b>' + szBasketNumber + '</b></td>');
	printWin.document.writeln('<td><b>' + szBasketArticle + '</b></td>');
	printWin.document.writeln('<td><b>' + szBasketAmount + '</b></td>');
	// printWin.document.writeln('<td><b>' + szBasketPriceArt + '</b></td>');
	// printWin.document.writeln('<td><b>' + szBasketPrice + '</b></td>');
	printWin.document.writeln('</tr>');
	printWin.document.writeln('<tr><td colspan="5"><hr></td></tr>');

	myCookie = getCookie(szCookieName);
	fullList = myCookie.substring(0, myCookie.length);
	totPrice = 0;
	itemList = 0;

	for (var i=0; i<=fullList.length; i++) {
		if (fullList.substring(i, i+1) == '[') {
			itemStart = i+1;
			itemIndex = 0;
		}
		if (fullList.substring(i, i+1) == ']') {

			itemEnd = i;
			cookieItems[itemIndex] = fullList.substring(itemStart, itemEnd);
			itemTotalPrice = StringNachFloat(cookieItems[3]) * cookieItems[2];
			itemList=itemList+1;
			totPrice = totPrice + itemTotalPrice;
			
			printWin.document.writeln('<tr>');
			printWin.document.writeln('<td>'+cookieItems[1]+'</td>');
			printWin.document.writeln('<td>'+cookieItems[0]+'</td>');
			printWin.document.writeln('<td>'+cookieItems[2]+'</td>');
			// printWin.document.writeln('<td>'+cookieItems[3]+'</td>');
			// printWin.document.writeln('<td>'+FloatNachString(itemTotalPrice)+'</td>');
			printWin.document.writeln('</tr>');
		} 

  		if (fullList.substring(i,i+1) == '@') 
  		{
			cookieItems[itemIndex] = fullList.substring(itemStart, i);
			itemStart = i+1;
			itemIndex = itemIndex + 1;
		}
	}

	printWin.document.writeln('<tr><td colspan="5"><hr></td></tr>');
	printWin.document.writeln('<tr>');
	printWin.document.writeln('<td colspan="4"></td>');
	printWin.document.writeln('<td></td>');
	printWin.document.writeln('</tr>');
	printWin.document.writeln('</table></body></html>');
	printWin.document.close();
}
