/*
Component eShop JavaScript
Created by Ioannis Sannos
http://www.isopensource.com
*/

function eshopLightOn(fel) {
	fel.style.backgroundColor='#EEEEEE';
}

function eshopLightOff(fel) {
	fel.style.backgroundColor='#FFFFFF';
}

function switchShipping() {
	var el = document.getElementById('shippingdata');
	var chbox = document.getElementById('shippingbilling');
	if (chbox.checked==true) {
		el.style.display = "none";
	} else {
		el.style.display = "";
	}
}

function eshopAddToCart(id) {
	var modserialObj = document.getElementById('modserial'+id);
	modserialObj.value = '';
	var form = document.getElementById('fmaddcart'+id);
	try {
		form.onsubmit();
	}
	catch(e){}
	form.submit();
}

function eshopAddToWish(id) {
	var modserialObj = document.getElementById('modserial'+id);
	modserialObj.value = '';
	var form = document.getElementById('fmaddcart'+id);
	form.task.value = 'addtowish';
	try {
		form.onsubmit();
	}
	catch(e){}
	form.submit();
}

function eshopUpdateCart() {
	var form = document.getElementById('fmshopupcart');
	try {
		form.onsubmit();
	}
	catch(e){}
	form.submit();
}

function eshopAddVariant(id, vgid) {
	var selbox = document.getElementById('variant'+id+'-'+vgid);
	var amount = selbox.options[selbox.selectedIndex].getAttribute('rel');
	if (typeof amount == "string") {
		var addamount = parseFloat(amount);
	} else {
		var addamount = amount;
	}

	var priceObj = document.getElementById('productprice'+id);
	var oldpricetxt = priceObj.innerHTML;
	if (typeof oldpricetxt == "string") {
		var oldprice = parseFloat(oldpricetxt);
	} else {
		var oldprice = amount;
	}

	var modserialObj = document.getElementById('modserial'+id);
	var modserial = modserialObj.value;
	if (modserial != '') {
		var serial = unserialize(modserial);
	} else {
		var serial = [];
	}

	var removeamount = 0;
	if (serial[vgid] != undefined) {
		removeamount = parseFloat(serial[vgid]);
	}

	serial[vgid] = addamount;
	var newserial = serialize(serial);
	modserialObj.value = newserial;

	var newprice = oldprice + addamount - removeamount;
	priceObj.innerHTML = newprice.toFixed(2);
}


function serialize( mixed_value ) {
    var _getType = function( inp ) {
        var type = typeof inp, match;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function" && ktype == "object") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}



function unserialize(data){
    var error = function (type, msg, filename, line){throw new window[type](msg, filename, line);};
    var read_until = function (data, offset, stopchr){
        var buf = [];
        var chr = data.slice(offset, offset + 1);
        var i = 2;
        while(chr != stopchr){
            if((i+offset) > data.length){
                error('Error', 'Invalid');
            }
            buf.push(chr);
            chr = data.slice(offset + (i - 1),offset + i);
            i += 1;
        }
        return [buf.length, buf.join('')];
    };
    var read_chrs = function (data, offset, length){
        buf = [];
        for(var i = 0;i < length;i++){
            var chr = data.slice(offset + (i - 1),offset + i);
            buf.push(chr);
        }
        return [buf.length, buf.join('')];
    };
    var _unserialize = function (data, offset){
        if(!offset) offset = 0;
        var buf = [];
        var dtype = (data.slice(offset, offset + 1)).toLowerCase();
        
        var dataoffset = offset + 2;
        var typeconvert = new Function('x', 'return x');
        var chrs = 0;
        var datalength = 0;
        
        switch(dtype){
            case "i":
                typeconvert = new Function('x', 'return parseInt(x)');
                var readData = read_until(data, dataoffset, ';');
                var chrs = readData[0];
                var readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "b":
                typeconvert = new Function('x', 'return (parseInt(x) == 1)');
                var readData = read_until(data, dataoffset, ';');
                var chrs = readData[0];
                var readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "d":
                typeconvert = new Function('x', 'return parseFloat(x)');
                var readData = read_until(data, dataoffset, ';');
                var chrs = readData[0];
                var readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "n":
                readdata = null;
            break;
            case "s":
                var ccount = read_until(data, dataoffset, ':');
                var chrs = ccount[0];
                var stringlength = ccount[1];
                dataoffset += chrs + 2;
                
                var readData = read_chrs(data, dataoffset+1, parseInt(stringlength));
                var chrs = readData[0];
                var readdata = readData[1];
                dataoffset += chrs + 2;
                if(chrs != parseInt(stringlength) && chrs != readdata.length){
                    error('SyntaxError', 'String length mismatch');
                }
            break;
            case "a":
                var readdata = {};
                
                var keyandchrs = read_until(data, dataoffset, ':');
                var chrs = keyandchrs[0];
                var keys = keyandchrs[1];
                dataoffset += chrs + 2;
                
                for(var i = 0;i < parseInt(keys);i++){
                    var kprops = _unserialize(data, dataoffset);
                    var kchrs = kprops[1];
                    var key = kprops[2];
                    dataoffset += kchrs;
                    
                    var vprops = _unserialize(data, dataoffset);
                    var vchrs = vprops[1];
                    var value = vprops[2];
                    dataoffset += vchrs;
                    
                    readdata[key] = value;
                }
                
                dataoffset += 1;
            break;
            default:
                error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
            break;
        }
        return [dtype, dataoffset - offset, typeconvert(readdata)];
    };
    return _unserialize(data, 0)[2];
}
