/** * Send a post request */ function send(url, params) { // Obtain an XMLHttpRequest instance var req = newXMLHttpRequest(); // Set the handler function to receive callback notifications // from the request object var handlerFunction = getReadyStateHandler(req, updateBaskets); req.onreadystatechange = handlerFunction; // Open an HTTP POST connection to the shopping cart servlet. // Third parameter specifies request is asynchronous. req.open("POST", url, true); // Specify that the body of the request contains form data req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Send form encoded data stating that I want to add the // specified item to the cart. var parameters = getParams(params); //log(parameters); req.send(parameters); } function getBasketIdFromRequest() { var uri = document.location.search; if (uri != null && uri != "") { uri = uri.substring(1, uri.length); } else { return null; } var reqparams = uri.split("&"); for (var j = 0; j < reqparams.length; j++) { if (reqparams[j].search("basketid") > -1) { return reqparams[j]; } } } function getParams(params) { var basketid = document.getElementById("basket-id").value; if (basketid != "") { var parameters = "basketid="+basketid+"&"+params; return parameters; } return params; } function log(parameters) { var table = document.getElementById("log"); table.innerHTML = ""; var tr = document.createElement("tr"); table.appendChild(tr); var td = document.createElement("td"); tr.appendChild(td); td.innerHTML = parameters; } /* * Returns a new XMLHttpRequest object, or false if this browser * doesn't support it */ function newXMLHttpRequest() { var xmlreq = false; if (window.XMLHttpRequest) { // Create XMLHttpRequest object in non-Microsoft browsers xmlreq = new XMLHttpRequest(); } else if (window.ActiveXObject) { // Create XMLHttpRequest via MS ActiveX try { // Try to create XMLHttpRequest in later versions // of Internet Explorer xmlreq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e1) { // Failed to create required ActiveXObject try { // Try version supported by older versions // of Internet Explorer xmlreq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { // Unable to create an XMLHttpRequest with ActiveX } } } return xmlreq; } /** * Returns a function that waits for the specified XMLHttpRequest * to complete, then passes its XML response * to the given handler function. * req - The XMLHttpRequest whose state is changing * responseXmlHandler - Function to pass the XML response to */ function getReadyStateHandler(req, responseXmlHandler) { // Return an anonymous function that listens to the // XMLHttpRequest instance return function () { // If the request's status is "complete" if (req.readyState == 4) { // Check that a successful server response was received if (req.status == 200 || req.status == 0) { // Pass the XML payload of the response to the // handler function responseXmlHandler(req.responseXML); } else { // An HTTP problem has occurred alert("Järjestelmässä tapahtui virhe. Yritä myöhemmin uudelleen. Virhekoodi: " + req.status); } } } } function updateBaskets(basketXML) { updateBasket(basketXML, "basket-table", false); if (document.getElementById("main-basket") != null) { updateBasket(basketXML, "main-basket", true); } } function onloadAction() { var basketid = document.getElementById("basket-id").value; if (basketid != null && basketid != "") { send('/ajax', "handler=basket&action=get"); } } function roundCents(sum) { return Math.round(100*sum)/100; } /** * Update the basket with content from XML */ function updateBasket(basketXML, currentTable, modifyView) { if (basketXML.getElementsByTagName("success").length > 0) { var basketid = document.getElementById("basket-id").value; send('/ajax', "handler=basket&action=remove"); document.location = "/jump?basketID="+basketid; return; } var basket = basketXML.getElementsByTagName("basket")[0]; var basketid = basket.getAttribute("id"); var complete = basket.getAttribute("complete"); document.getElementById("basket-id").value = basketid; var div = document.getElementById(currentTable); div.innerHTML = ""; var html = ""; html += "
\n"; var total = 0; var addedPriceTotal = 0; var basketProducts = basket.getElementsByTagName("basketproduct"); var gotSubscription = false; var gotDataSubscription = false; var gotDataHandset = false; var gotHandset = false; var gotAddService = false; var gotAccessory = false; for (var j = 0; j < basketProducts.length; j++) { var bp = basketProducts[j]; var type = bp.getAttribute("type"); var id = bp.getAttribute("productid"); if (type == "gsmsubscription") { gotSubscription = true; if (id == 641 || id == 642 || id == 731 || id == 732 || id == 790 || (id >= 932 && id <=936)) { gotDataSubscription = true; } } else if (type == "handsetmodel") { gotHandset = true; if (id == 24 || id == 90 || id == 111 || id == 150) { gotDataHandset = true; } } else if (type == "bundle") { gotHandset = true; gotDataHandset = true; } else if (type == "addservice") gotAddService = true; else if (type == "accessory") gotAccessory = true; } for (var j = 0; j < basketProducts.length; j++) { var bp = basketProducts[j]; var productid = bp.getAttribute("productid"); var name = bp.getAttribute("name"); var type = bp.getAttribute("type"); var pricelistid = bp.getAttribute("pricelistid"); var period = bp.getAttribute("period"); var priceElems = bp.getElementsByTagName("priceoption"); var selectedPrice; var params = bp.getElementsByTagName("parameter"); for (var t=0; t < params.length; t++) { if (params[t].getAttribute("name") == "price_option") { selectedPrice = params[t].getAttribute("value"); } } var priceOptions = false; if (priceElems.length > 1) { priceOptions = true; } var addedPrice = null; if (bp.getAttribute("addedprice") != null) { addedPrice = parseFloat(bp.getAttribute("addedprice")); } var price = null; if (bp.getAttribute("price") != null) { price = parseFloat(bp.getAttribute("price")); } var count = null; if (bp.getAttribute("count") != null) { count = parseInt(bp.getAttribute("count")); } var accessoryTotal = null; var accessoryTotalString = ""; if (count != null) { accessoryTotal = addedPrice * count; addedPriceTotal += accessoryTotal; accessoryTotalString = accessoryTotalString + roundCents(accessoryTotal); accessoryTotalString = addPadding(accessoryTotalString); } html += "\n"; if (modifyView) { if(count != null && count > 1 && type == "accessory") { html += ""; } if(count != null && count > 0 && type == "accessory") { html += ""; } if (productid != null && productid != 887) { html += ""; } } html += "\n" } if (modifyView) { html += ""; } if (basketProducts.length) { var text = getBasketText(basketProducts); if (text.length) { html += ""; } } html += "
" + name + "
\n"; var pricingmodel = bp.getAttribute("pricingmodel"); if (price > 0 && (addedPrice == null || addedPrice == 0) && type != "accessory") { // if (! (gotHandset || gotDataHandset || gotAddService)) { if (!gotSubscription && period != 0) { price = "0.00"; } // 6.4. PRINT PERIOD FOR DEBUGGING // html += "PERIOD: " + period + "
"; if (period == null || period == 0) { html += price + " €"; } else { html += price + " €/kk"; total += price; } // total += price; } else if (count == 1){ html += accessoryTotalString + " €"; html += ""; } else { html += ""+count+ " kpl / " + addedPrice + " € = " + accessoryTotalString + " €"; html += ""; } if (priceOptions) { html += "
Valitse laitteen maksutapa:
"; } } html += "
"; html += text; html += "
"; var totalString = ""+roundCents(total); totalString = addPadding(totalString); var addedPriceTotalString = "" + roundCents(addedPriceTotal); addedPriceTotalString = addPadding(addedPriceTotalString); if (basketProducts.length) { html += ""; html += ""; html += ""; //html += ""; //html += ""; html += ""; // html += ""; // html += ""; // html += ""; html += ""; html += ""; document.getElementById("order_info").style.display = "block"; } else { html += " "; } if (complete == "true") { // html += ""; html += ""; //html += "\"Tilaa\""; html += "
Yhteensä:"+ totalString + " €/kksad €/kk\"Tilaa\"

Kertaveloitus:

"+ addedPriceTotalString + " €
sad €/kk
"; if (!modifyView) { html += "Muokkaa\"Tilaa\"
Määräaikainen sopimus 24 kk
Määräaikainen sopimus
\"Tilaa\"
"; } html += ""; html += ""; } else { html += "

Et ole valinnut tuotteita" + "Valitse haluamasi " + "3G-palvelupaketti, " + "laite ja mahdolliset " + "lisäpalvelut.

"; } div.innerHTML = html; } function addPadding(string) { var paddingSize = -1; if (string.indexOf(".") == -1) { paddingSize = 2; string += "."; } else { paddingSize = 3 - string.substring(string.indexOf("."), string.length).length; } for (var x = 0; x < paddingSize; x++) { string += "0"; } return string; } function store() { send("/ajax","handler=basket&action=store"); } function getBasketText(basketProducts) { if (! basketProducts.length) return ""; var gotSubscription = false; var gotDataSubscription = false; var gotDataHandset = false; var gotHandset = false; var gotAddService = false; var gotAccessory = false; for (var j = 0; j < basketProducts.length; j++) { var bp = basketProducts[j]; var type = bp.getAttribute("type"); var id = bp.getAttribute("productid"); if (type == "gsmsubscription") { gotSubscription = true; if (id == 641 || id == 642 || id == 731 || id == 732 || id == 790 || (id >= 932 && id <=936)) { gotDataSubscription = true; } } else if (type == "handsetmodel") { gotHandset = true; if (id == 24 || id == 90 || id == 111 || id == 150 || id == 161) { gotDataHandset = true; } } else if (type == "bundle") { gotHandset = true; gotDataHandset = true; } else if (type == "addservice") gotAddService = true; else if (type == "accessory") gotAccessory = true; } var text = ""; if (! gotSubscription) { if (gotDataHandset) { text += "
Lisää ostoskoriin vielä MobiililaajakaistaPaketti!
"; } else { text += "
Lisää ostoskoriin vielä haluamasi liittymäpaketti!
"; } } if (! gotHandset) { if (gotDataSubscription) { text += "
Lisää palvelupakettiin vielä päätelaite!
"; } else { text += "
Lisää palvelupakettiin vielä haluamasi puhelin.
"; } } if (! gotAddService && gotSubscription) { if (gotDataSubscription) { text += "
Voit halutessasi lisätä palvelupakettiin myös saldopalvelun!"; } else { text += "
"; text += "Voit lisätä palvelupakettiisi myös edullisen mobiililaajakaistan!"; text += "
"; } } return text; }