﻿var lvtexte = new Array();
var savePos = -1;
var zweitfenster = null;
var zaehler = 0;

function openlvwindow(einheit, preis, kurztext, langtext) {
	openlvwindowkomplett("0", "", "", einheit, preis, "", kurztext, langtext);
}


function addToLV (id) {
	var myVar = lvtexte[id];
	openlvwindowkomplett(myVar[0], myVar[1], myVar[2], myVar[3], myVar[4], myVar[5], myVar[6], myVar[7]);
}

function addAllToLV() {
	openLV();
}

function kaufm(x) {
	x = zahlMitPunkt(x);
	var k = (Math.round(x * 100) / 100).toString();
	k += (k.indexOf('.') == -1)? '.00' : '00';
	var p = k.indexOf('.');
	return k.substring(0, p) + ',' + k.substring(p+1, p+3);
}

function zahlMitPunkt(x) {
	x = String(x);
	var p = x.indexOf(',');
	if (p > 0)
		return x.substring(0, p) + '.' + x.substring(p+1, x.length);
	else
		return x;
}


function openlvwindowInsertListe(liste) {
   	if(typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	if(typeof aktiv == 'undefined') {
		aktiv = window.setInterval("insertListe('" + liste + "')", 2000);
		zaehler = 1;
	} else {
		if (aktiv == 0) {
		aktiv = window.setInterval("insertListe('" + liste + "')", 2000);
		}
	}
}


function insertListe(liste) {
	alert(liste.length);
	alert(liste);
}


function getGDLAT(ID) {
	for (var a=0; a < GDL_ABS_ATs.length; a++) {
		if (GDL_ABS_ATs[a][0] == ID)
			return GDL_ABS_ATs[a];
	}
	return new Array("","","","");
}

function addSlashes(textString)  {
	return textString.replace(/('|")/g,"\\'"); 
}

function insertGDLAT(lvtitel, ht, sysText, Werte) {
	if (lvwindow.frames["unten"]) {
		if (typeof lvwindow.frames["unten"].loaded != 'undefined') {
			if (lvwindow.frames["unten"].loaded == true) {
				window.clearInterval(aktiv);
				aktiv=0;
				lvwindow.insertProdukt(5, lvtitel, "Systemtext ABSOLUT", ht, "", "", "", "");					
				lvwindow.insertProdukt(0, lvtitel, "Schiedel ABSOLUT", sysText, "", "", "", "");
	
				IDs = Werte.split("||");
				for (var a=0; a < IDs.length; a++) {
					var wert = IDs[a].split("|");
					var at = getGDLAT(wert[0]);
					if (at[0] != "") {
						if (at[2] == "") {
							var gpreis = "";
						} else {
							var gpreis = kaufm(zahlMitPunkt(wert[1]) * zahlMitPunkt(at[2]));
						}
						lvwindow.insertProdukt(0, "", at[1], addSlashes(at[3]), zahlMitPunkt(wert[1]), "Stck", at[2], gpreis);	
					}
				}
			}
		}
	}
}


function openlvwindowGDL(lvtitel, ht, sysText, IDs) {
	if(typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	if(typeof aktiv == 'undefined') {
		aktiv = window.setInterval("insertGDLAT('"  + lvtitel + "','" + ht + "','" +  sysText + "','" + IDs + "')", 2000);
		zaehler = 1;
	} else {
		if (aktiv == 0) {
			aktiv = window.setInterval("insertGDLAT('" + lvtitel + "','" + ht + "','" +  sysText + "','" + IDs + "')", 2000);
		}
	}
}





function openLV() {

	if (typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	aktivInterval = window.setTimeout("atReady()", 2000);
	
}


function atReady() {

	if (typeof lvwindow.frames["unten"]!= 'undefined') {
		if (typeof lvwindow.frames["unten"].insertProdukt != 'undefined') {
			sendAllData();
		} else setTimeout("atReady()", 1000);

	}else setTimeout("atReady()", 1000);

}



function sendAllData() {
	for (a=0; a < lvtexte.length; a++) {
		if (lvwindow.frames["unten"].insertPositionAt != -1){
			savePos = lvwindow.frames["unten"].insertPositionAt-1;
		}
		/*if (lvwindow.frames["unten"].insertPositionAt == -1){
			if (savePos < 0)
				savePos = 0;
			savePos++;
			lvwindow.frames["unten"].insertPositionAt = savePos;
		}*/
		if (savePos > -1) {
			savePos++;
			lvwindow.frames["unten"].insertPositionAt = savePos;
		}
		var xRows = lvwindow.frames["unten"].uniqueID;
		insertAT(lvtexte[a][0], lvtexte[a][1], lvtexte[a][6], lvtexte[a][7], lvtexte[a][2], lvtexte[a][3], lvtexte[a][4], lvtexte[a][5]);
		xRows = lvwindow.frames["unten"].uniqueID - xRows;
		if (savePos > -1) {
			if (xRows > 1)
				savePos = savePos + xRows -1;
		}
		if (a == lvtexte.length-1) {
				savePos = -1;
		}
	}
}

function openlvwindowkomplett(typ, titel, menge, einheit, preis, gpreis, kurztext, langtext) {
   	if (typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	if(typeof aktiv == 'undefined') {
		aktiv = window.setInterval("insertAT('" + typ + "', '" + titel + "', '" + kurztext + "', '" + langtext + "', '" + menge + "', '" + einheit + "', '" + preis + "', '" + gpreis + "')", 2000);
		zaehler = 1;
	} else {
		if (aktiv == 0) {
			aktiv = window.setInterval("insertAT('" + typ + "', '" + titel + "', '" + kurztext + "', '" + langtext + "', '" + menge + "', '" + einheit + "', '" + preis + "', '" + gpreis + "')", 2000);
		} else {
			insertAT(typ, titel, kurztext, langtext, menge, einheit, preis, gpreis);
		}
	}
}

function openlvwindowInsertArtID(ID) {
   	if(typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	if(typeof aktiv == 'undefined') {
		aktiv = window.setInterval("insertArtID('" + ID + "')", 2000);
		zaehler = 1;
	} else {
		if (aktiv == 0) {
			aktiv = window.setInterval("insertArtID('" + ID + "')", 2000);
		}
	}
}
function openlvwindowInsertSysIDs(IDs) {
   	if(typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	if(typeof aktiv == 'undefined') {
		aktiv = window.setInterval("insertSysIDs('" + IDs + "')", 2000);
		zaehler = 1;
	} else {
		if (aktiv == 0) {
			aktiv = window.setInterval("insertSysIDs('" + IDs + "')", 2000);
		}
	}
}

function openlvwindowInsertSysID(ID) {
   	if(typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	if(typeof aktiv == 'undefined') {
		aktiv = window.setInterval("insertSysID('" + ID + "')", 2000);
		zaehler = 1;
	} else {
		if (aktiv == 0) {
			aktiv = window.setInterval("insertSysID('" + ID + "')", 2000);
		}
	}
}
function openlvwindowInsertID(ID) {
   	if(typeof lvwindow == 'undefined') {
		lvwindow = window.open("", "cmslv", "");
	}
	if(typeof lvwindow == 'undefined'){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if(lvwindow.closed == true){
		lvwindow = window.open("/fileadmin/ATManager/ats/index.htm", "cmslv", "");
	}
	if (lvwindow.location.href == "about:blank"){
		lvwindow.location.href = "/fileadmin/ATManager/ats/index.htm";
	}
	lvwindow.focus();
	if(typeof aktiv == 'undefined') {
		aktiv = window.setInterval("insertID('" + ID + "', '')", 2000);
		zaehler = 1;
	} else {
		if (aktiv == 0) {
			aktiv = window.setInterval("insertID('" + ID + "', '')", 2000);
		}
	}
}

function insertID(ID) {
    window.clearInterval(aktiv);
	aktiv = 0;
	if (typeof lvwindow.document.frames["strg"]!= 'undefined') {
		if (typeof lvwindow.insertID != 'undefined') {
			lvwindow.insertID(ID);
			window.clearInterval(aktiv);
			aktiv = 0;
		} else {
			zaehler = zaehler + 1;
		}
	} else {
		zaehler = zaehler + 1;
	}
	if (zaehler > 50) {
		window.clearInterval(aktiv);
		aktiv = 0;
		alert("bernahme gescheitert!");
	}
}

function insertSysIDs(IDs) {
	if (typeof lvwindow.frames["strg"]!== 'undefined') {
		if ((typeof lvwindow.insertSysID !== 'undefined') &&
			(typeof lvwindow.frames["strg"].document !== 'undefined') &&
			(typeof lvwindow.frames["strg"].document.forms["form1"] !== 'undefined')) {
			window.clearInterval(aktiv);
			aktiv = 0;
			lvwindow.insertSysIDs(IDs);
		} else {
			zaehler = zaehler + 1;
		}
	} else {
		zaehler = zaehler + 1;
	}
	if (zaehler > 50) {
		window.clearInterval(aktiv);
		aktiv = 0;
	//	alert("bernahme gescheitert!");
	}
}

function insertSysID(ID) {
    //window.clearInterval(aktiv);
	//aktiv = 0;

	if (typeof lvwindow.frames["strg"]!== 'undefined') {
		if ((typeof lvwindow.insertSysID !== 'undefined') &&
			(typeof lvwindow.frames["strg"].document !== 'undefined') &&
			(typeof lvwindow.frames["strg"].document.forms["form1"] !== 'undefined')) {
			window.clearInterval(aktiv);
			aktiv = 0;
			lvwindow.insertSysID(ID);
		} else {
			zaehler = zaehler + 1;
		}
	} else {
		zaehler = zaehler + 1;
	}
	if (zaehler > 50) {
		window.clearInterval(aktiv);
		aktiv = 0;
		alert("bernahme gescheitert!");
	}
}

function insertArtID(ID) {
    window.clearInterval(aktiv);
	aktiv = 0;
	if (typeof lvwindow.frames["strg"]!= 'undefined') {
		if (typeof lvwindow.insertArtID != 'undefined') {
			lvwindow.insertArtID(ID);
			window.clearInterval(aktiv);
			aktiv = 0;
		} else {
			zaehler = zaehler + 1;
		}
	} else {
		zaehler = zaehler + 1;
	}
	if (zaehler > 50) {
		window.clearInterval(aktiv);
		aktiv = 0;
		alert("bernahme gescheitert!");
	}
}

function insertMultipleAT(typ, titel, kurztext, langtext, menge, einheit, epreis, gpreis, reset) {
	if (typeof lvwindow.frames["unten"]!= 'undefined') {
		if (typeof lvwindow.frames["unten"].insertProdukt != 'undefined') {
			if ((savePos != -1) || (lvwindow.frames["unten"].insertPositionAt != -1)){
				if  (lvwindow.frames["unten"].insertPositionAt != -1){
					savePos = lvwindow.frames["unten"].insertPositionAt;
				}
				if (lvwindow.frames["unten"].insertPositionAt == -1){
					savePos++;
					lvwindow.frames["unten"].insertPositionAt = savePos;
				}
			}
			lvwindow.insertProdukt(typ, titel, kurztext, langtext, menge, einheit, epreis, gpreis);
			if (reset) {
				savePos = -1;
			}
			window.clearInterval(aktiv);
			aktiv = 0;
		} else {
			zaehler = zaehler + 1;
		}
	} else {
		zaehler = zaehler + 1;
	}
	if (zaehler > 50) {
		window.clearInterval(aktiv);
		aktiv = 0;
		alert("bernahme gescheitert!");
	}
}

function insertAT(typ, titel, kurztext, langtext, menge, einheit, epreis, gpreis) {
	if (typeof lvwindow.frames["unten"]!= 'undefined') {
		if (typeof lvwindow.frames["unten"].insertProdukt != 'undefined') {
			lvwindow.insertProdukt(typ, titel, kurztext, langtext, menge, einheit, epreis, gpreis);
			if (typeof aktiv != 'undefined') {
				if (aktiv != 0)
					window.clearInterval(aktiv);
			}
			aktiv = 0;
		} else {
			zaehler = zaehler + 1;
		}
	} else {
		zaehler = zaehler + 1;
	}
	if (zaehler > 50) {
		window.clearInterval(aktiv);
		aktiv = 0;
		alert("bernahme gescheitert!");
	}
}
