﻿function getProduct(obj,e)
{   
    var productId = document.getElementById(obj.id.replace("Img1","hiddenID").toString()).value.toString();
    var productName = document.getElementById(obj.id.replace("Img1","title")).innerHTML;
    var productPrice = document.getElementById(obj.id.replace("Img1","Price")).innerHTML;
    var productCurrency = document.getElementById(obj.id.replace("Img1","Currency")).innerHTML;
    
    getBasket(productId,productName,productPrice);
}

function getBasket(ID,Name,Price)
{
    var idS=null;
    var row=null;
    var newQuantityList ="";
    var newPriceList ="";
    
    var productIdList = document.getElementById("pId");
    var productQuantityList = document.getElementById("pQuantity");
    var productNameList = document.getElementById("pName");
    var productPriceList = document.getElementById("pPrice");
    
    
    var productIdListValue = productIdList.value.toString();
        var pIdListValue = productIdListValue.split(";");
        
        for(idS in pIdListValue)
        {
            if(pIdListValue[idS] == ID)
            {
                 row = idS.toString();
            } 
        }
    
    /*If Id is not null*/
    if(row!=null)
    {    
        var productQuantityListValue = productQuantityList.value.toString();
            var pQuantityListValue = productQuantityListValue.split(";");
            
        var productPriceListValue = productPriceList.value.toString();
            var pPriceListValue = productPriceListValue.split(";");
            
            pQuantityListValue[row] = parseFloat(pQuantityListValue[row]) + parseFloat (1);
            pPriceListValue[row] = parseFloat(pQuantityListValue[row]) + parseFloat (pQuantityListValue[row]);
                    
            for(var i in pQuantityListValue)
                {
                    if(pQuantityListValue[i]!="")
                    {
                        newQuantityList+= pQuantityListValue[i]+";";
                        newPriceList+=pPriceListValue[i]+";";
                    }   
                }
            productQuantityList.value = newQuantityList;
            productPriceList.value = newPriceList;
    }
    /*if Id is null new product*/
    else
    {
        productIdList.value +=ID+";";
        productNameList.value +=Name+";";
        productQuantityList.value+=1+";";
        productPriceList.value+=Price+";";
    }
    
//        
//    var productQuantityList = String(document.getElementById("pQuantity").value);
//        var pQuantityList = productQuantityList.split(";");
//        
//    var productPriceList = String(document.getElementById("pPrice").value);
//        var pPriceList = productPriceList.split(";");

    var myIdList = (document.getElementById("pId").value).split(";");
    var myQuantityList = (document.getElementById("pQuantity").value).split(";");
    var myNameList = (document.getElementById("pName").value).split(";");
    var myPriceList = (document.getElementById("pPrice").value).split(";");
    removeProduct();
    for(var i=0; i<myIdList.length-1;i++)
    { 
        createProduct(myNameList[i],myPriceList[i],myQuantityList[i]);
    }
    
}

function removeProduct()
{
    var basket = document.getElementById("basket_items");
    if ( basket.hasChildNodes() )
    {
        while ( basket.childNodes.length >= 1 )
        {
            basket.removeChild( basket.firstChild );       
        } 
    }
}

function createProduct(Name,Price,Quantity,Currency,Id)
{
    var basket = document.getElementById("basket_items");
    
    var leftDiv = document.createElement('div');
    basket.appendChild(leftDiv);
    leftDiv.style.styleFloat="left";
    leftDiv.style.width="180px";
    leftDiv.style.height="25px";
    
        var lblName = document.createElement('Label');
        leftDiv.appendChild(lblName);
        lblName.innerHTML = Name;
        lblName.style.width="130px";
        
//    var centerDiv = document.createElement('div');
//    basket.appendChild(centerDiv);
//    centerDiv.style.paddingLeft="10px";
//    centerDiv.style.width="50px";
//    centerDiv.style.height="25px";
    
        var lblQuantity = document.createElement('Label');
        leftDiv.appendChild(lblQuantity);
        lblQuantity.innerHTML = Quantity+" it.";
        lblQuantity.style.width="47px";
        lblQuantity.style.paddingLeft="20px";
    
    var rightDiv = document.createElement('div');
    basket.appendChild(rightDiv);
    rightDiv.style.styleFloat="right";
    rightDiv.style.width="160px";
    rightDiv.style.height="25px";
    
        var lblPrice = document.createElement('Label');
        rightDiv.appendChild(lblPrice);
        lblPrice.innerHTML = Price+" "+Currency;
        lblPrice.style.width="78px";
        
        var remove = document.createElement('a');
        rightDiv.appendChild(remove);
        remove.innerHTML = "Remove";
        var url ='javascript:removeMyBasket(\''+Id+'\');';
        remove.setAttribute("href",url);
        remove.style.width="78px";
      
}

function myBasket()
{
    var productIdList = document.getElementById("pId").value;
    var productQuantityList = document.getElementById("pQuantity").value;
    var productNameList = document.getElementById("pName").value;
    var productPriceList = document.getElementById("pPrice").value;
    var productCurrencyList = document.getElementById("pCurrency").value;
    
    
        var pIdListValue = productIdList.split(";");
        var pQuantityValue = productQuantityList.split(";");
        var pNameValue = productNameList.split(";");
        var pPriceValue = productPriceList.split(";");
        var pCurrencyValue = productCurrencyList.split(";");
        
    removeProduct();
    for(var i=0; i<pIdListValue.length-1;i++)
    { 
        createProduct(pNameValue[i],pPriceValue[i],pQuantityValue[i],pCurrencyValue[i],pIdListValue[i]);
    }
            
    var totalQuantity = document.getElementById("tQuantity");
    var totalPrice = document.getElementById("tPrice");
    
    var popupWin = document.getElementById('basket_items');
    if(popupWin.style.display=="block")
    {
        popupWin.style.display="none"
        
    }
    else
    {
        popupWin.style.display="block"
    } 
}

function removeMyBasket(Id)
{
    var productIdList = document.getElementById("pId").value;
    var productQuantityList = document.getElementById("pQuantity").value;
    var productNameList = document.getElementById("pName").value;
    var productPriceList = document.getElementById("pPrice").value;
    var productCurrencyList = document.getElementById("pCurrency").value;
    
    
        var pIdListValue = productIdList.split(";");
        var pQuantityValue = productQuantityList.split(";");
        var pNameValue = productNameList.split(";");
        var pPriceValue = productPriceList.split(";");
        var pCurrencyValue = productCurrencyList.split(";");
        
    removeProduct();
    for(var i=0; i<pIdListValue.length-1;i++)
    { 
        if(pIdListValue[i]!=Id){
            createProduct(pNameValue[i],pPriceValue[i],pQuantityValue[i],pCurrencyValue[i],pIdListValue[i]);
        }
    }
            
    var totalQuantity = document.getElementById("tQuantity");
    var totalPrice = document.getElementById("tPrice");
    
    var popupWin = document.getElementById('basket_items');
    if(popupWin.style.display=="block")
    {
        popupWin.style.display="block"
        
    }
    else
    {
        popupWin.style.display="block"
    } 
}
fmtMoney = function( n, c, d, t ) 
{
	var m = ( c = Math.abs( c ) + 1 ? c : 2, d = d || ",", t = t || ".", /(\d+)(?:(\.\d+)|)/.exec( n + "" ) ), x = m[1].length % 3;
	return ( x ? m[1].substr( 0, x ) + t : "" ) + m[1].substr( x ).replace( /(\d{3})(?=\d)/g, "$1" + t ) + ( c ? d + ( +m[2] ).toFixed( c ).substr( 2 ) : "" );
}