﻿function checkoutCart(){
    var topCookie =  top.document.cookie;
   var cart = topCookie.split(';');
   var tb = document.createElement("tbody");
   for (var x=0; x < cart.length; x++){
        var temp = cart[x].split('='); 
        pr_data = temp[0].split(':');
        var name = pr_data[0];
        var qty = pr_data[1];
        var price = pr_data[2];   
        var nprice = parseFloat(price) * 1;
        var newRow = document.createElement("tr");
    if (temp[1] == "product" || temp[1] == "design"){                
        var newName = document.createElement("td");
        newName.appendChild(document.createTextNode(name));
        var newQty = document.createElement("td");
		var quantity = "";
		if (qty.substring(0,1)=="x") quantity = qty;
		else quantity = "x"+qty;
        newQty.appendChild(document.createTextNode(quantity));
        var newPrice = document.createElement("td");
        newPrice.appendChild(document.createTextNode("\u00A3" + nprice.toFixed(2)));
        var rem = document.createElement("td");
        var remlink = document.createElement("a");
        remlink.setAttribute("href","javascript:deleteProduct("+x.toString()+");");
        remlink.appendChild(document.createTextNode("Remove"));
        rem.appendChild(remlink);
        newRow.appendChild(newQty);
        newRow.appendChild(newName);
        newRow.appendChild(newPrice);
        newRow.appendChild(rem);
        tb.appendChild(newRow);        
    }       
   }
   var totalRow = document.createElement("tr");
   var firstCol = document.createElement("td");
   firstCol.className="totalheader";
   var boldText = document.createElement("b");
   boldText.className = "bigger_text";
   boldText.appendChild(document.createTextNode("Total"));
   firstCol.appendChild(boldText);
   totalRow.appendChild(firstCol);
   var secCol = document.createElement("td");
   secCol.className = "totalheader";
   secCol.appendChild(document.createTextNode(" "));
   totalRow.appendChild(secCol);
   var thirdCol = document.createElement("td");
   thirdCol.className = "totalheader";
   thirdCol.appendChild(document.createTextNode(" "));
   
   var fourthCol = document.createElement("td");
   fourthCol.className = "totalheader";
   var sb = document.createElement("b");
   sb.className = "bigger_text";
   var cart = top.document.cookie.split(';');
	    var cval = parseFloat('0.0');
	    for (var x=0; x<cart.length; x++){
	        var temp = cart[x].split('=');
	        if (temp[1] == "product" || temp[1] == "design"){
	            var pr_st = temp[0].split(':');
	            cval = cval + parseFloat(pr_st[2]);
	        }
	    }
	    if (cval > 0){	  
	    var st = cval * 1;
	     var ct = document.createTextNode("\u00A3" + st.toFixed(2));	 
	    sb.appendChild(ct);
	    
	    }
	    else {
	    var ct = document.createTextNode("\u00A30.00");
	    sb.appendChild(ct);
	    }
	    fourthCol.appendChild(sb);
	   totalRow.appendChild(fourthCol);
	   totalRow.appendChild(thirdCol);
   tb.appendChild(totalRow);
   //last row
   var lastRow = document.createElement("tr");
   var col1 = document.createElement("td");
   col1.appendChild(document.createTextNode(" "));
   lastRow.appendChild(col1);
   var col2 = document.createElement("td");
   col2.appendChild(document.createTextNode(" "));
   lastRow.appendChild(col2);
   var col3 = document.createElement("td");
   col3.appendChild(document.createTextNode(" "));
   lastRow.appendChild(col3);
   var col4 = document.createElement("td");
   var c4link = document.createElement("a");
   c4link.setAttribute("href","javascript:deleteAllProducts();");
   c4link.appendChild(document.createTextNode("Remove All"));
   col4.appendChild(c4link);
   lastRow.appendChild(col4);
   
   tb.appendChild(lastRow);
   document.getElementById("prlist").appendChild(tb);
}

function deleteAllProducts(){
    var allCookies = top.document.cookie.split(';');
    for (var y=0; y < allCookies.length; y++){
        var spCookie = allCookies[y].split('=');
      //  var delCookie = spCookie[0]+"=";
        //top.document.cookie = delCookie;
        DeleteCookie(spCookie[0],"","");
        }
        loadTopCart();
        document.location = "basket.html";
   
}

function deleteAllProductsFinal(){
    var allCookies = top.document.cookie.split(';');
    for (var y=0; y < allCookies.length; y++){
        var spCookie = allCookies[y].split('=');
      //  var delCookie = spCookie[0]+"=";
        //top.document.cookie = delCookie;
        DeleteCookie(spCookie[0],"","");
        }
        loadTopCart();
   
}

function DeleteCookie (name,path,domain) {
   
     document.cookie = name + "=" +
     ((path) ? "; path=" + path : "") +
     ((domain) ? "; domain=" + domain : "") +
     "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function deleteProduct(index){
    var prods = top.document.cookie.split(';');
    var cookiename = prods[index].split('=');
  //  top.document.cookie = cookiename[0]+"=";
  DeleteCookie(cookiename[0],"","");
    document.location = "basket.html";
    loadTopCart();
}


function loadTopCart(){
     var cart = top.document.cookie.split(';');
	    var cval = parseFloat('0.0');
	    for (var x=0; x<cart.length; x++){
	        var temp = cart[x].split('=');
	        if (temp[1] == "product" || temp[1] == "design"){
	            var pr_st = temp[0].split(':');
	            cval = cval + parseFloat(pr_st[2]);
	        }
	    }
	    if (cval > 0){	  
	    var st = cval * 1;
	     var ct = top.document.createTextNode(st.toFixed(2));	 
	   var old = top.document.getElementById("cart_val").firstChild;
	    top.document.getElementById("cart_val").replaceChild(ct,old);
	    
	    }
	    else {
	    var ct = top.document.createTextNode("0.00");
	    var old = top.document.getElementById("cart_val").firstChild;
	    top.document.getElementById("cart_val").replaceChild(ct,old);
	    }
}

function checkoutCartFinal(){
    var vat = parseFloat("0");
    var topCookie =  top.document.cookie;
    var cart = topCookie.split(';');
    var neccessary = false;
   var tb = document.createElement("tbody");
   for (var x=0; x < cart.length; x++){
        var temp = cart[x].split('='); 
        pr_data = temp[0].split(':');
        var name = pr_data[0];
        var qty = pr_data[1];
        var price = pr_data[2];   
        var nprice = parseFloat(price) * 1;
        var newRow = document.createElement("tr");
    if (temp[1] == "product" || temp[1] == "design"){
        if (name.toLowerCase().search("hosting") < 0) neccessary = true;
        var newName = document.createElement("td");        
        newName.appendChild(document.createTextNode(name));
        var newQty = document.createElement("td");
		var quantity = "";
		if (qty.substring(0,1)=="x") quantity = qty;
		else quantity = "x"+qty;
        newQty.appendChild(document.createTextNode(quantity));
        var newPrice = document.createElement("td");
        newPrice.appendChild(document.createTextNode("\u00A3" + nprice.toFixed(2)));
        //vat checking
        var vatAmount = parseFloat(pr_data[4]);
        if (vatAmount>0){
        var fPrice = parseFloat(price);
        vat = vat + (fPrice * (vatAmount/100));        
        }
        //end vat checking
        newRow.appendChild(newQty);
        newRow.appendChild(newName);
        newRow.appendChild(newPrice);       
        tb.appendChild(newRow);        
    }       
   }
   //delivery adding
   if (neccessary == true) {
       var delRow = document.createElement("tr");
       var emptyCell = document.createElement("td");
       emptyCell.className = "upperborder";
       emptyCell.appendChild(document.createTextNode(" "));
       delRow.appendChild(emptyCell);
       var labelCell = document.createElement("td");
       labelCell.className = "upperborder";
       labelCell.setAttribute("align", "right");
       labelCell.appendChild(document.createTextNode("Delivery:"));
       var delPriceCell = document.createElement("td");
       delPriceCell.className = "upperborder";
       delPriceCell.appendChild(document.createTextNode("\u00A31.99"));
       delRow.appendChild(labelCell);
       delRow.appendChild(delPriceCell);
       tb.appendChild(delRow);
   }
   //end delivery adding
   //vat adding
   vatRow = document.createElement("tr");
   vatFCell = document.createElement("td");
   vatRow.appendChild(vatFCell);
   vatLabel = document.createElement("td");
   vatLabel.setAttribute("align","right");
   vatLabel.appendChild(document.createTextNode("+ VAT:"));
   vatRow.appendChild(vatLabel);
   var vv = vat * 1;
   vatValue = document.createElement("td");
   vatValue.appendChild(document.createTextNode("\u00A3" + vv.toFixed(2)));
   vatRow.appendChild(vatValue);
   tb.appendChild(vatRow);
   //end vat adding
   var totalRow = document.createElement("tr");
   var firstCol = document.createElement("td");
   firstCol.className="totalredheader";
   var boldText = document.createElement("b");
   boldText.className = "bigger_text";
   boldText.appendChild(document.createTextNode("Total:"));
   firstCol.appendChild(boldText);
   firstCol.setAttribute("align","right");  
   var secCol = document.createElement("td");
   secCol.className = "totalheader";
   secCol.appendChild(document.createTextNode(" "));
   totalRow.appendChild(secCol);
    totalRow.appendChild(firstCol);
   var fourthCol = document.createElement("td");
   fourthCol.className = "totalredheader";
   var sb = document.createElement("b");
   sb.className = "bigger_text";
   var cart = top.document.cookie.split(';');
	    var cval = parseFloat('0.0');
	    for (var x=0; x<cart.length; x++){
	        var temp = cart[x].split('=');
	        if (temp[1] == "product" || temp[1] == "design"){
	            var pr_st = temp[0].split(':');
	            cval = cval + parseFloat(pr_st[2]);
	        }
	    }
	    if (cval > 0){	  
	    cval = cval + vat;
	    var st = (cval * 1) + parseFloat("1.99");
	     var ct = document.createTextNode("\u00A3" + st.toFixed(2));	 
	    sb.appendChild(ct);
	    
	    }
	    else {
	    var ct = document.createTextNode("\u00A30.00");
	    sb.appendChild(ct);
	    }
	    fourthCol.appendChild(sb);
	   totalRow.appendChild(fourthCol);	   
   tb.appendChild(totalRow);
   //last row
   
   document.getElementById("prlist").appendChild(tb);
   initialCheck();
}

function initialCheck() {
    var basket = top.document.cookie;
    if (basket.length == 0) {
        alert("Your cart is empty");
        history.back();
    }
}