
// Loads file
function WriteResults(xml, xsl, stepId) {
	var out = MakeDOM(null);
	xml.transformNodeToObject(xsl, out);

	var stepN = 3;
	if (objectType == 2) stepN = 1;
	else if (objectType == 1) stepN = 2;

	for (i = 1; i <= 3 ; i++ )
	{
		document.getElementById("step2" + i).innerHTML = "";
	}

	var div = document.getElementById("step" +stepId + stepN);
	if(div) {
		if(div.style.display == "none") {
			div.style.display = "block";
		}
		div.innerHTML =out.xml;
	}
}

// string file name
function LoadDOM(file) {
	var dom;
	try {
		dom = MakeDOM(null);
		dom.load(file);
	}
	catch (e) {
		alert(e.description);
	}
	return dom;
}

// Gets MSXML Document Object Model
function MakeDOM(progID) {
	if (progID == null) {
		progID = "msxml2.DOMDocument.3.0";
	}
	var dom;
	try {
		dom = new ActiveXObject(progID);
		dom.async = false;
		dom.validateOnParse = false;
		dom.resolveExternals = false;
		dom.preserveWhiteSpace = true;
	}
	catch (e) {
		alert(e.description);
	}
	return dom;
}

function FormatPrice(price){
	if((!price || price == 0) ) return "0,00";

	price = RoundPrice(price);

	var fraction = Math.round((price - Math.floor(price)) * 100);
	if(fraction < 10)	fraction = "0" + fraction;
	var fr = fraction;
	price = Math.floor(price);

	var newPrice = "";
	price = '' + price;
	var div = 1000;

	while(price > 1000){
		var fraction2 = Math.floor(price - Math.floor(price / div) * div);

		if(fraction2 < 10)			fraction2 = "00" + fraction2;
		else if(fraction2 < 100)	fraction2 = "0" + fraction2;
		
		newPrice = " " + fraction2 + newPrice;
		price = Math.floor(price / div);
		fraction = Math.floor((price - Math.floor(price)) * 100);
	}
	return price + newPrice + "," + fr;
}

function RoundPrice(price) {
	return Math.round(price * 100) /100;
}

// changes motor sum, according to unit price and motors count
function SubtractSum(rowId) {
	var uprice = document.getElementById("totalPrice_"+ rowId).innerHTML;
	var count=document.getElementById("data["+rowId+"][quantity]").value;

	if(!countArray[rowId]) countArray[rowId] = 0;

	if(uprice != "&nbsp;" && count!= "&nbsp;") {
		if(uprice.indexOf("&nbsp;") != -1) uprice = uprice.replace("&nbsp;","");
		if(uprice.indexOf(" ") != -1) uprice = uprice.replace(" ","");
		if(uprice.indexOf(",") != -1) uprice = uprice.replace(",",".");

		//uprice = RoundPrice(parseFloat(uprice) * 100 / parseFloat(100 -PVM));
		//uprice = RoundPrice(parseFloat(uprice) * (parseFloat(100) +parseFloat(PVM))/100);
		countArray[rowId] = count;

		SubtractTotalSums(RoundPrice(parseFloat(uprice)));
	}
}

function ReplaceRowWithHidden(rowId, oldRowId, name, hiddenName) {
	document.getElementById(name + "_"+ rowId).innerHTML = document.getElementById(name + "_"+ oldRowId).innerHTML;

	while(document.getElementById(name + "_"+ rowId).innerHTML.indexOf("["+oldRowId+"]") != -1) {
		var tmp = document.getElementById(name + "_"+ rowId).innerHTML;
		document.getElementById(name + "_"+ rowId).innerHTML = tmp.replace("data["+oldRowId+"]", "data["+rowId+"]");

		tmp = document.getElementById(name + "_"+ rowId).innerHTML;
		document.getElementById(name + "_"+ rowId).innerHTML = tmp.replace("data["+oldRowId+"]", "data["+rowId+"]");
	}
}

function ReplaceRow(rowId, oldRowId, name) {
	document.getElementById(name + "_"+ rowId).innerHTML = document.getElementById(name + "_"+ oldRowId).innerHTML;
}


function DeleteRow(rowId) {
	document.getElementById("price_"+ rowId).innerHTML = "&nbsp;";
	//document.getElementById("cuttingCount_"+ rowId).innerHTML = "&nbsp;";
	document.getElementById("cuttingPrice_"+ rowId).innerHTML ="&nbsp;";
	document.getElementById("remnant_"+ rowId).innerHTML = "&nbsp;";
	//document.getElementById("remnantPrice_"+ rowId).innerHTML = "&nbsp;";
	document.getElementById("totalPrice_"+ rowId).innerHTML = "&nbsp;";

	document.getElementById("dimension_"+ rowId).innerHTML = "&nbsp;";
	//document.getElementById("parameters_"+ rowId).innerHTML = "&nbsp;";
	document.getElementById("quantity_"+ rowId).innerHTML = "&nbsp;";

	document.getElementById("name_"+ rowId).innerHTML = "&nbsp;<input type='hidden' value='' id='data["+rowId+"][id]' name='data["+rowId+"][id]'/>";
}

// writes totals sums
function SubtractTotalSums(cost) {
	AddTotalSums(parseFloat(0-cost));
	totalSum = RoundPrice(totalSum);
}

function AddRow (table) {
	totalObjectsRowsCount++;
	var rowId = totalObjectsRowsCount;

	var row = table.insertRow(rowId);
	row.id = "row_" + totalObjectsRowsCount;

	AddCell(0, "nr", row, "", totalObjectsRowsCount+"."); // nr
	AddHiddenCell(1, "name", row,"id"); // name
	AddCell(2, "dimension", row, "", "&nbsp;"); // code
	//AddCell(3, "parameters", row, "", "&nbsp;"); // parameters
	AddCell(3, "quantity", row, "", "&nbsp;"); // quantity
	AddCell(4, "price", row, "", "&nbsp;"); 
	//AddCell(5, "cuttingCount", row, "", "&nbsp;"); 
	AddCell(5, "cuttingPrice", row, "text-align:right", "&nbsp;");
	AddCell(6, "remnant", row, "text-align:right", "&nbsp;");
	//AddCell(9, "remnantPrice", row, "text-align:right", "&nbsp;"); 
	AddCell(7, "totalPrice", row, "text-align:right", "&nbsp;"); 

	col=row.insertCell(8);
	col.style.cssText = "text-align:center";
	col.innerHTML="<input type='image' src='"+img_dir+"recyclebin.gif' onclick='Delete(" + totalObjectsRowsCount + ")' />";
}

function AddCell(id, name, row, style, text) {
	col=row.insertCell(id);
	col.id = name + "_" + totalObjectsRowsCount;
	col.name = "data[" + totalObjectsRowsCount + "][" + name + "]";
	col.style.cssText = style;
	col.innerHTML = text;
}

function AddHiddenCell(id, name, row, hiddenName) {
	col=row.insertCell(id);
	col.id = name + "_" + totalObjectsRowsCount;
	col.name = name + "_" + totalObjectsRowsCount;

	col.innerHTML="&nbsp; <input type='hidden' value='' id='data[" + totalObjectsRowsCount + "][" +hiddenName +"]' name='data[" + totalObjectsRowsCount + "][" + hiddenName+ "]'/>";
}
