var wishlist;

$(document).ready(function(){    
    if($.cookie("wishlist") === null){
        wishlist = {"records": []};
    }
    else{
        wishlist = json2array($.cookie("wishlist"));
    }
    
    buildWishList();
});

function isUnsignedInteger(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}

function addCommas(nStr){
    nStr += '';
	
    x  = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	
    var rgx = /(\d+)(\d{3})/;
    
	while(rgx.test(x1)){
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
    
	return x1 + x2;
}

function addItemToWishList(id,name,price){
    quantity = $("#wli_" + id).attr("value");
    
    if(quantity.substr(0,1) > 0 && isUnsignedInteger(quantity)){
        var i = 0;
        var isInList = false;
        
        $.each(wishlist.records,function (){
            if(this.itemid == id){
                wishlist.records[i].quantity = (this.quantity * 1) + (quantity * 1);
                isInList = true;
            }
            
            i++;
        });
        
        if(isInList == false){
            wishlist.records.push({"itemid":"","itemname":"","quantity":"","price":""});
        
            wishlist.records[wishlist.records.length-1].itemid   = id;
            wishlist.records[wishlist.records.length-1].itemname = id + '-' + escape(name);
            wishlist.records[wishlist.records.length-1].quantity = quantity;
            wishlist.records[wishlist.records.length-1].price    = price;
        }
        
        $.cookie("wishlist",array2json(wishlist));
        
        buildWishList();
        
        $("#wli_" + id).attr("value","1");
    }
}

function buildWishList(){
    var wishlistTable = '<table class="wltable" width="100%" cellspacing="0">';
    
    wishlistTable += '<tr>';
    wishlistTable += '<td colspan="2" align="center">Kívánság lista</td>';
    wishlistTable += '</tr>';  
    wishlistTable += '<tr>';
    wishlistTable += '<td colspan="2"><hr /></td>';
    wishlistTable += '</tr>';
    wishlistTable += '</table>';
    
    wishlistTable += '<div id="wl-data">';
    wishlistTable += '<table class="wltable" width="100%" cellspacing="0">';
    
    if(wishlist.records.length > 0){    
        pricesum = true;
        psum     = 0;
        
        $.each(wishlist.records,function (){
            wishlistTable += '<tr bgcolor="#efefef">';
            wishlistTable += '<td align="left"><span>' + unescape(this.itemname);
            
            if(wltype == '1'){
                wishlistTable += '&nbsp;(' + this.quantity + '&nbsp;db)';   
            }
            
            wishlistTable += '</span></td>';
            wishlistTable += '<td align="right"><img src="http://domainlakopark.hu/home/images/delete-icon.png" onclick="removeItemFromWishList(' + "'" + this.itemid + "'" + ');" style="cursor:pointer;" /></td>';
            wishlistTable += '</tr>';
            wishlistTable += '<tr>';
            wishlistTable += '<td colspan="2" style="line-height: 1px;">&nbsp;</td>';
            wishlistTable += '</tr>';
            
            if(this.price == 0 || this.price == ''){
                pricesum = false;
                psum = 0;
            }
            
            if(pricesum == true){
                psum = psum + (this.price * this.quantity);
            }
        });
        
        if(psum > 0){
            wishlistTable += '<tr>';
            wishlistTable += '<td colspan="2"><hr /></td>';
            wishlistTable += '</tr>';
            
            wishlistTable += '<tr>';
            wishlistTable += '<td colspan="2">Összesen: <b>' + addCommas(psum.toString()) + ' Ft</b></td>';
            wishlistTable += '</tr>';
        }
        
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2"><hr /></td>';
        wishlistTable += '</tr>';        
        wishlistTable += '</table>';        
        wishlistTable += '</div>';
        
        wishlistTable += '<div id="wl-msg" style="display:none;"></div>';
        
        wishlistTable += '<div id="wl-form">';
        wishlistTable += '<form id="wl" name="wl" action="http://' + document.domain + '/sendwishlist" method="post" onsubmit="return false;">';
        wishlistTable += '<table class="wltable" width="100%" cellspacing="0">';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2" align="left">Név:</td>';
        wishlistTable += '</tr>';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2"><input type="text" id="wl-name" name="wl-name" style="width: 150px;" /></td>';
        wishlistTable += '</tr>';

        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2" align="left">Telefonszám:</td>';
        wishlistTable += '</tr>';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2"><input type="text" id="wl-tel" name="wl-tel" style="width: 150px;" /></td>';
        wishlistTable += '</tr>';

        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2" align="left">E-mail cím:</td>';
        wishlistTable += '</tr>';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2"><input type="text" id="wl-mail" name="wl-mail" style="width: 150px;" /></td>';
        wishlistTable += '</tr>';
        
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2" align="left">Kérdés:</td>';
        wishlistTable += '</tr>';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2"><textarea id="wl-megj" name="wl-megj" style="width: 150px;height: 100px;"></textarea></td>';
        wishlistTable += '</tr>';
        wishlistTable += '</table>';
        
        wishlistTable += '<table class="wltable" width="100%" cellspacing="0">';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2"><hr /></td>';
        wishlistTable += '</tr>';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2"><img src="http://domainlakopark.hu/home/images/clear.jpg" title="Kívánság lista törlése" onclick="clearWishList();" style="cursor:pointer;" />&nbsp;&nbsp;<img src="http://domainlakopark.hu/home/images/checkout.jpg" title="Kívánság lista elküldése" onclick="sendWishlist();" style="cursor:pointer;" /></td>';
        wishlistTable += '</tr>';
        wishlistTable += '</table>';
        wishlistTable += '</form>';
        wishlistTable += '</div>';
    }
    else{
        wishlistTable += '<table class="wltable" width="100%" cellspacing="0">';
        wishlistTable += '<tr>';
        wishlistTable += '<td colspan="2" align="center"><p style="font-weight: normal;">A kívánság listára össze tudja válogatni azokat a termékeket, amelyek felkeltették az érdeklődését. Ha kész, küldje el nekünk kérdéseivel együtt!</p></td>';
        wishlistTable += '</tr>';        
        wishlistTable += '</table>';  
    }
    
    $("#wishlist").html(wishlistTable);
}

function removeItemFromWishList(itemid){    
    newWishlist = {"records": []};
    
    $.each(wishlist.records,function (){
        if(this.itemid != itemid){
            newWishlist.records.push({"itemid":"","itemname":"","quantity":"","price":""});
            
            newWishlist.records[newWishlist.records.length-1].itemid   = this.itemid;
            newWishlist.records[newWishlist.records.length-1].itemname = this.itemname;
            newWishlist.records[newWishlist.records.length-1].quantity = this.quantity;
            newWishlist.records[newWishlist.records.length-1].price    = this.price;
        }
    });
    
    wishlist = newWishlist;
    
    $.cookie("wishlist",array2json(wishlist));
    
    buildWishList();
}

function clearWishList(){
    wishlist = {"records": []};
    
    $.cookie("wishlist",array2json(wishlist));
    
    buildWishList();   
}

function array2json(arrayData){
	var wishlistDecoded = '';
	
	$(arrayData.records).each(function(){
		wishlistDecoded += '{"itemid":"' + this.itemid + '","itemname":"' + this.itemname + '","quantity":"' + this.quantity + '","price":"' + this.price + '"},';
	});
	
	wishlistTextify = 'var wishlist = {"records": [' + wishlistDecoded.substr(0,wishlistDecoded.length-1) + ']};';
	
	return wishlistTextify;
}

function json2array(jsonData){
	eval(jsonData);
	return wishlist;
}

function togleWishform(){
    if($("#wl-form").css("display") == 'block'){
        $("#wl-form").hide();
        $("#wl-msg").attr("innerHTML",'<p align="center"><strong>Adatfeldolgozás folyamatban</strong></p>');
        $("#wl-msg").show();
    }
    else{
        $("#wl-form").show();
        $("#wl-msg").attr("innerHTML",'');
        $("#wl-msg").hide();
    }
}

function sendWishlist(){
    wlname = escape(encodeURI($("#wl-name").attr("value")).replace(/\+/g,'%2B'));
    wltel  = escape(encodeURI($("#wl-tel").attr("value")).replace(/\+/g,'%2B'));
    wlmegj = escape(encodeURI($("#wl-megj").attr("value")).replace(/\+/g,'%2B'));
    wlmail = escape(encodeURI($("#wl-mail").attr("value")).replace(/\+/g,'%2B'));
    wllist = escape(encodeURI($("#wl-data").attr("innerHTML")).replace(/\+/g,'%2B'));
    wlurl  = $("#wl").attr("action");
    
    togleWishform();
    
    $.ajax({
        type: 'POST',
        data: "wl-name=" + wlname + "&wl-tel=" + wltel + "&wl-mail=" + wlmail + "&wl-megj=" + wlmegj + "&wl-list=" + wllist,
        url: wlurl,
        success: function(data){
            if(data.substr(0,2) == "ok"){
                $("#wl-msg").attr("innerHTML",'<p align="center" style="background-color: #D8EBBC;border: 1px solid #4C9135;"><strong>' + data.substr(3) + '</strong></p>');
                window.setTimeout("clearWishList();",2000);
            }
            else{
                $("#wl-msg").attr("innerHTML",'<p align="center" style="background-color: #EAA3A1;border: 1px solid #AF3B3B;"><strong>' + data + '</strong></p>');
                window.setTimeout("togleWishform();",1500);
            }
        }
    });
}
