



//
//
// remove element from Quotation.	
//
//
var deletingLineId;
function deleteElement(id) {
	deletingLineId = id;
	QuotationService.deleteQuotationline(id, afterDeleteElement);
} 

function afterDeleteElement(quotation) {
	lineId = deletingLineId;
	deletingLineId = null;
	if (quotation != null) {
		var line = null;
		for (var i = 1; i <= 6; i++) {
			line = $("line"+lineId+"_"+i);
			if (line != null) {
				line.parentNode.removeChild(line);
			}
		}
		
		updateQuotationTotal(quotation);
	}
}

function toCustomerForm(){
  document.beregner.action="?action=SHOWCUSTOMERFORM";
  document.beregner.submit();	
}

function toCalculator(){
  document.beregner.action="?action=SHOWCALCULATOR";
  document.beregner.qtLine.value='';
  document.beregner.submit();	
}

function editLine(id) {
  location.href = "?action=EDITLINE&currentline=" + id;
}



function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function visVejledning(tekstDiv) {
  var obj = document.getElementById("vejledning");
  if (document.all){
	toggleSelects("hide");
  }
  obj.innerHTML = document.getElementById(tekstDiv).innerHTML;
  obj.style.padding='15px';
  obj.style.border='solid 1px #000000';
  positionDiv();
  opacity("vejledning", 0, 100, 500);
}

function fjernVejledning() {
  opacity("vejledning", 100, 0, 500);
  if (document.all){
	toggleSelects("show");
  }
  document.getElementById("vejledning").innerHTML = "";
}

function toggleSelects(action) {
	var svn=document.getElementsByTagName("SELECT");
	for (a=0;a<svn.length;a++){
		if (action == "show")
			svn[a].style.visibility="visible";
		else
			svn[a].style.visibility="hidden";				
	}
}

var w, h, div;
var offSetX = 50;
var started = false;
function positionDiv () {
  if (document.layers) {
      div = document.vejledning;
      w = div.document.width;
      h = div.document.height;
   }
  else if (document.all) {
     div = document.all.vejledning;
     w = div.offsetWidth;
     h = div.offsetHeight;
   }
  else if (document.getElementById) {
     div = document.getElementById('vejledning');
     w = div.offsetWidth;
     h = div.offsetHeight;
  }
  if (document.layers) {
    div.left = window.pageXOffset + window.innerWidth - w - offSetX;
    div.top = window.pageYOffset + window.innerHeight - h;
  }
  else if (document.all) {
    div.style.posLeft = document.body.scrollLeft + document.body.clientWidth - w - offSetX;
    div.style.posTop = document.body.scrollTop + document.body.clientHeight - h;
  } else if (document.getElementById) {
    div.style.left = (window.pageXOffset + window.innerWidth - w - offSetX) + 'px';
    div.style.top = (window.pageYOffset + window.innerHeight - h) + 'px';
  }
} 

function toggleDelivery() {
	var section = document.getElementById('deliverysection');
	var deliveryToggle = document.getElementById('brug_lev_adresse');
	if (deliveryToggle.checked) {
		section.style.display = "block";
	} else {
		section.style.display = "none";
	}
}
	

function saveQuotationline(id) {
	var line = {
		quotationLineId: id
	};
	if ($("rebate"+id)) {		line.rebatePercentage 	= NET2Util.parseNumber(DWRUtil.getValue("rebate"+id)); }
	if ($("quantity"+id)) {		line.quantity	 		= parseInt(DWRUtil.getValue("quantity"+id)); }
	if ($("price"+id)) { 		line.unitPrice 			= NET2Util.parseNumber(DWRUtil.getValue("price"+id)); }
	if ($("productNumber"+id)){	line.productNumber		= DWRUtil.getValue("productNumber"+id); }
	if ($("productName"+id)) {	line.name				= DWRUtil.getValue("productName"+id); }
	if ($("productWidth"+id)) {	line.width				= NET2Util.parseNumber(DWRUtil.getValue("productWidth"+id)); }
	if ($("productHeight"+id)){	line.height				= NET2Util.parseNumber(DWRUtil.getValue("productHeight"+id)); }
	
	var quotationlineOptions = new Array();
	var inputs = document.getElementsByName("quotationline"+ id +"OptionId");
	for (i = 0; i < inputs.length; i++) {
		var optionId  = inputs[i].value;
		quotationlineOptions[quotationlineOptions.length] = {
			quotationLineOptionId: optionId,
			optionName: $("quotationlineOption" + optionId + "Name").value,
			optionValue: $("quotationlineOption" + optionId + "Value").value
		}
	}
	if (quotationlineOptions.length > 0) {
		line.quotationlineoptions = quotationlineOptions;
	}
	
	QuotationService.saveQuotationline(line, afterSaveQuotationline);
}

function afterSaveQuotationline(line) {
	var id = line.quotationLineId;
	var qt = line.quotation;
	DWRUtil.setValue("actualPrice"+id, 	NET2Util.formatCurrency(line.actualPrice));
	if ($("rebate"+id)) {
		DWRUtil.setValue("rebate"+id, 		NET2Util.formatNumber(line.rebatePercentage, 1));	
	}
	DWRUtil.setValue("quantity"+id, 	line.quantity);
	DWRUtil.setValue("price"+id, 		NET2Util.formatCurrency(line.unitPrice));
	DWRUtil.setValue("productNumber"+id,line.productNumber);
	DWRUtil.setValue("productName"+id, 	line.name);
	DWRUtil.setValue("productWidth"+id,	NET2Util.formatCurrency(line.width));
	DWRUtil.setValue("productHeight"+id,NET2Util.formatCurrency(line.height));

	updateQuotationTotal(qt);
}


function updateQuotationTotal(qt) {
	// Quotation total values
	if ($("quotationFullNumber")) {	DWRUtil.setValue("quotationFullNumber",		qt.fullNumber); }
	if ($("ShippingType")) { QuotationService.getShippingTypes(afterChangeShippingType); }
	DWRUtil.setValue("vatPrice", 				NET2Util.formatCurrency(qt.vat));
	DWRUtil.setValue("totalPriceIncShipping", 	NET2Util.formatCurrency(qt.totalPriceIncludingShipping));
	DWRUtil.setValue("shippingPrice", 			NET2Util.formatCurrency(qt.shipping));
	DWRUtil.setValue("totalPrice", 				NET2Util.formatCurrency(qt.totalPrice));
	DWRUtil.setValue("totalElementCount",		qt.totalQuantity);
}


function showUploadForm(lineId) {
	var pos = null;
	if (lineId == "Quotation") {
		pos = getAnchorPosition("attachQuotationDocument");
	} else {
		pos = getAnchorPosition("attachDocument" + lineId);
	}
	
	DWRUtil.setValue("uploadQuotationLineId", lineId);
	var form = $("uploadForm");
	with (form.style) {
		top = pos.y;
		left = pos.x;
		display = "block";
	}
}

function hideUploadForm() {
	$("uploadForm").style.display = "none";
}

var documentID;
function deleteAttachment(id, type) {
	documentID = id;
	if (type == "Quotation") {
		QuotationService.deleteQuotationAttachment(id, afterDeleteAttachment);
		documentID = type + id;
	} else {
		QuotationService.deleteQuotationlineAttachment(id, afterDeleteAttachment);
	}
}

function afterDeleteAttachment() {
	var id = documentID;
	documentID = null;
	var td = $("document"+id);
	td.parentNode.removeChild(td);
}


function changeStatus(newStatus, id, controlcode, callback) {
	if (callback == null) {
		callback = afterChangeStatus;
	}
	var comment = prompt("Knyt en kommentar til statusskiftet?");
	if (comment != null) {
		QuotationService.changeQuotationStatus(newStatus, comment, { quotationID: id, controlCode: controlcode }, callback );
	}
}

function changeSupplier(supplierId, qId, controlcode) {
	QuotationService.changeSupplier({ quotationID: qId, controlCode: controlcode }, supplierId );
}

function changeResponsible(newResponsible, id, controlcode ) {
	QuotationService.changeResponsibleUser( { quotationID: id, controlCode: controlcode }, newResponsible );
}

function afterChangeStatus() {
	// for now: do nothing
}

function addQuotationlineOption(lineId) {
	var option = {
		optionName: $("newQuotationlineOptionName" + lineId).value,
		optionValue: $("newQuotationlineOptionValue" + lineId).value
	}
	QuotationService.addQuotationlineOption(option, lineId, afterAddQuotationlineOption);
}

function afterAddQuotationlineOption(option) {
	if (option != null) {
		var newrow = document.createElement("tr");
		newrow.setAttribute("id", "quotationlineOption" + option.quotationLineOptionId + "Row")

		var td = document.createElement("td");
		td.setAttribute("valign", "top");
		td.setAttribute("style", "padding: 0px");
		td.setAttribute("height", "18");
		newrow.appendChild(td);
		
		var id = document.createElement("input");
		id.setAttribute("type", "hidden");
		id.setAttribute("name", "quotationline" + option.quotationline.quotationLineId + "OptionId");
		id.setAttribute("value", option.quotationLineOptionId);
		td.appendChild(id);
		
		var name = document.createElement("input");
		name.setAttribute("name", "quotationlineOption" + option.quotationLineOptionId + "Name");
		name.setAttribute("id", "quotationlineOption" + option.quotationLineOptionId + "Name");
		name.setAttribute("value", option.optionName);
		name.setAttribute("type", "text");
		name.setAttribute("class", "optionname");
		td.appendChild(name);
		
		td = document.createElement("td");
		td.setAttribute("valign", "top");
		td.setAttribute("style", "padding: 0px");
		td.setAttribute("height", "18");
		newrow.appendChild(td);
		
		var value = document.createElement("input");
		value.setAttribute("name", "quotationlineOption" + option.quotationLineOptionId + "Value");
		value.setAttribute("id", "quotationlineOption" + option.quotationLineOptionId + "Value");
		value.setAttribute("value", option.optionValue);
		value.setAttribute("type", "text");
		value.setAttribute("class", "optionvalue");
		td.appendChild(value);
		
		var a = document.createElement("a");
		a.setAttribute("href", "#");
		a.setAttribute("title", "Slet detalje");
		a.setAttribute("onclick", "deleteQuotationlineOption(" + option.quotationLineOptionId + ", " + option.quotationline.quotationLineId + ")");
		td.appendChild(document.createTextNode(" "));
		td.appendChild(a);
		
		var img = document.createElement("img");
		img.setAttribute("src", "/export/system/modules/dk.net2vinduer.web/resources/icons/delete.png");
		img.setAttribute("border", "0");
		img.setAttribute("height", "16");
		img.setAttribute("width", "16");
		a.appendChild(img);
		
		var lastTr = $("lastQuotationline"+ option.quotationline.quotationLineId +"OptionRow");
		lastTr.parentNode.insertBefore(newrow, lastTr);
		
		
		var lineId = option.quotationline.quotationLineId;
		$("newQuotationlineOptionName" + lineId).value = "";
		$("newQuotationlineOptionValue" + lineId).value = "";
		DWRUtil.setValue("quotationFullNumber",	option.quotationline.quotation.fullNumber);
	}
}

var deletingOptionId;
function deleteQuotationlineOption(optionId, lineId) {
	deletingOptionId = optionId;
	QuotationService.deleteQuotationlineOption(optionId, lineId, afterDeleteQuotationlineOption);
}

function afterDeleteQuotationlineOption(line) {
	var optionId = deletingOptionId;
	deletingOptionId = null;
	if (line != null) {
		var tr = $("quotationlineOption" + optionId + "Row");
		tr.parentNode.removeChild(tr);

		DWRUtil.setValue("quotationFullNumber",		line.quotation.fullNumber);
	}
}

function addQuotationline() {
	var line = { }; // new object
	line.rebatePercentage 	= NET2Util.parseNumber(DWRUtil.getValue("newRebate")); 
	line.quantity	 		= parseInt(DWRUtil.getValue("newQuantity")); 
	line.unitPrice 			= NET2Util.parseNumber(DWRUtil.getValue("newPrice")); 
	line.productNumber		= DWRUtil.getValue("newProductNumber"); 
	line.name				= DWRUtil.getValue("newName"); 
	line.width				= NET2Util.parseNumber(DWRUtil.getValue("newWidth")); 
	line.height				= NET2Util.parseNumber(DWRUtil.getValue("newHeight"));
	
	QuotationService.addQuotationline(line, afterAddQuotationline);
}

function afterAddQuotationline(quotationline) {
	if (quotationline != null) {
		document.location.reload();
	}
}



function changeShippingType() {
	QuotationService.changeShippingType(DWRUtil.getValue("ShippingType"), afterChangeShippingType);	
}

function afterChangeShippingType(shippingTypes) {
	DWRUtil.removeAllOptions("ShippingType");
	DWRUtil.addOptions("ShippingType", shippingTypes, "value", "label");
	for (var i in shippingTypes) {
		if (shippingTypes[i].selected) {
			DWRUtil.setValue("ShippingType", shippingTypes[i].value);
			DWRUtil.setValue("shippingPrice", NET2Util.formatCurrency(shippingTypes[i].price));
		}
	}
}



function sendStatusMail() {
	QuotationService.sendStatusNotificationMail();	
}

function sendStatusMailFor(id, cc) {
	 QuotationService.sendStatusNotificationMailFor( {quotationID: id, controlCode: cc } );
}
