/*
Component IOS Eshop JavaScript
Created by Ioannis Sannos
http://www.isopensource.com
*/

function eshopajaxobj() {
    var xo;
    if(window.XMLHttpRequest){ // Non-IE browsers
        xo = new XMLHttpRequest();
    } else if (window.ActiveXObject){ // IE
        xo=new ActiveXObject("Msxml2.XMLHTTP");
        if (!xo) {
            xo=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xo;
}

var eshophttp = eshopajaxobj();


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){}
    if (document.getElementById('check_show_variants') != undefined) {
        var selbox1 = document.getElementById('variant'+id+'-1');
        if (selbox1 != undefined) {
           var amount1 = selbox1.options[selbox1.selectedIndex].getAttribute('value'); 
        }
         
        var selbox2 = document.getElementById('variant'+id+'-2');
        if (selbox2 != undefined) {
           var amount2 = selbox2.options[selbox2.selectedIndex].getAttribute('value'); 
        }

        var selbox4 = document.getElementById('variant'+id+'-4');
        if (selbox4 != undefined) {
           var amount4 = selbox4.options[selbox4.selectedIndex].getAttribute('value'); 
        }
        
        var selbox7 = document.getElementById('variant'+id+'-7');
        if (selbox7 != undefined) {
           var amount7 = selbox7.options[selbox7.selectedIndex].getAttribute('value'); 
        }
        
        var selbox8 = document.getElementById('variant'+id+'-8');
        if (selbox8 != undefined) {
           var amount8 = selbox8.options[selbox8.selectedIndex].getAttribute('value'); 
        }

        var selbox9 = document.getElementById('variant'+id+'-9');
        if (selbox9 != undefined) {
           var amount9 = selbox9.options[selbox9.selectedIndex].getAttribute('value'); 
        }
        
        var selbox10 = document.getElementById('variant'+id+'-10');
        if (selbox10 != undefined) {
           var amount10 = selbox10.options[selbox10.selectedIndex].getAttribute('value'); 
        }
        
        var selbox11 = document.getElementById('variant'+id+'-11');
        if (selbox11 != undefined) {
           var amount11 = selbox11.options[selbox11.selectedIndex].getAttribute('value'); 
        }
        
        var selbox12 = document.getElementById('variant'+id+'-12');
        if (selbox12 != undefined) {
           var amount12 = selbox12.options[selbox12.selectedIndex].getAttribute('value'); 
        }                                                        
        
        var selbox13 = document.getElementById('variant'+id+'-13');
        if (selbox13 != undefined) {
           var amount13 = selbox13.options[selbox13.selectedIndex].getAttribute('value');
        }
        
        var selbox14 = document.getElementById('variant'+id+'-14');
        if (selbox14 != undefined) {
           var amount14 = selbox14.options[selbox14.selectedIndex].getAttribute('value'); 
        }
        
        if ((amount1 != undefined && amount1 == 0) || (amount2 != undefined && amount2 == 0) || (amount4 != undefined && amount4 == 0) || (amount7 != undefined && amount7 == 0) || (amount8 != undefined && amount8 == 0) || (amount9 != undefined && amount9 == 0) || (amount10 != undefined && amount10 == 0) || (amount11 != undefined && amount11 == 0) || (amount12 != undefined && amount12 == 0) || (amount13 != undefined && amount13 == 0) || (amount14 != undefined && amount14 == 0) ) {
            alert ('Προσοχή! Δεν έχετε ορίσει τις επιλογές');
        } else {
          form.submit();  
        } 
    } else {
        form.submit();
    }
    
}

function eshopUpdateCart() {
	var form = document.getElementById('fmshopupcart');
	try {
		form.onsubmit();
	}
	catch(e){}
	form.submit();
}


/* CUSTOM SERIALIZE BY I. SANNOS */
function doserial(arr) {
	var str = '';
	//mootools version
	arr.each(function(price, vid) {
		if (price != undefined) { str += vid+':'+price+','; }
	});
	//native javascript version
	//for (vid in arr) { str += vid+':'+arr[vid]+','; }
	return str;
}


/* CUSTOM UNSERIALIZE BY I. SANNOS */
function dounserial(str) {
	var serial = [];
	if (str == '') { return serial; }
	var firstarr = str.split(',');
	for (i=0;i<firstarr.length;i++) {
		var firstval = firstarr[i];
		if ((firstval != '') && firstval.indexOf(':')) {
			var secarr = firstval.split(':');
			var thirdkey = secarr[0];
			serial[thirdkey] = secarr[1];
		}
	}
	return serial;
}


function eshopAddVariant(id, vgid, decimals) {

	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 normpriceObj = document.getElementById('normalprice'+id);
    
	var oldpricetxt = priceObj.innerHTML;
    if (normpriceObj != undefined) {
        var normoldpricetxt = normpriceObj.innerHTML;
    }
    
    
	if (typeof oldpricetxt == "string") {
		var oldprice = parseFloat(oldpricetxt);
	} else {
		var oldprice = oldpricetxt;
	}

    if (typeof normoldpricetxt == "string") {
		var noldprice = parseFloat(normoldpricetxt);
	} else {
		var noldprice = normoldpricetxt;
	}
    
	var modserialObj = document.getElementById('modserial'+id);
	var modserial = modserialObj.value;
	if (modserial != '') {
		var serial = dounserial(modserial);
	} else {
		var serial = [];
	}

	var removeamount = 0;
	if (serial[vgid] != undefined) {
		removeamount = parseFloat(serial[vgid]);
	}

	serial[vgid] = addamount.toFixed(decimals);

	var newserial = doserial(serial);
	modserialObj.value = newserial;

	var newprice = oldprice + addamount - removeamount;
    var newpricenorm = noldprice + addamount - removeamount;

	if (decimals == 0) {
		newprice = parseInt(newprice);
		priceObj.innerHTML = newprice;
        normpriceObj.innerHTML = newpricenorm+' €';
	} else {
		priceObj.innerHTML = newprice.toFixed(decimals);
        normpriceObj.innerHTML = newpricenorm.toFixed(decimals)+' €';
	}
}


/************ search ***************/

/* LOAD PRODUCT TYPE EXTRA FIELDS */
function eshoploadextra() {
	var selbox = document.getElementById('ptid');
	var ptid = selbox.options[selbox.selectedIndex].value;
	ptid = parseInt(ptid);

	var xbox = document.getElementById('eshopsearchextra');
	xbox.innerHTML = '';
	if (ptid == 0) {
		xbox.style.display = 'none';
		return;
	}
	xbox.style.display = 'block';
    var rnd = Math.random();
    try {
        eshophttp.open('POST', 'index2.php');
        eshophttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        eshophttp.setRequestHeader('charset', 'utf-8');
        eshophttp.onreadystatechange = function () {
			if (eshophttp.readyState == 4) {
				if (eshophttp.status!=200) {
					alert('Error, please retry');
				} else {
					xbox.innerHTML = eshophttp.responseText;
				}
			}
		};
        eshophttp.send('option=com_eshop&task=loadextra&ptid='+ptid+'&rnd='+rnd);
    }
    catch(e){}
    finally{}
}

/* LOAD NESTED SUB-CATEGORIES */
function eshoploadcategories(level) {
	var selbox = document.getElementById('level'+level+'cid');
	var cid = parseInt(selbox.options[selbox.selectedIndex].value);
	var conbox = document.getElementById('subcategoriesbox');
	if (cid > 0) {
		var nlevel = level + 1;
		document.getElementById('cid').value = cid;
		conbox.style.display = 'block';
		for (var i=1; i < 10; i++) {
			var zlevel = level + i;
			if (document.getElementById('subcon'+zlevel)) {
				var conbox2 = document.getElementById('subcon'+zlevel);
				var selbox2 = document.getElementById('level'+zlevel+'cid');
				conbox2.removeChild(selbox2);
				conbox.removeChild(conbox2);
			}
		}
    	var rnd = Math.random();
    	try {
        	eshophttp.open('POST', 'index2.php');
        	eshophttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        	eshophttp.setRequestHeader('charset', 'utf-8');
        	eshophttp.onreadystatechange = function () {
				if (eshophttp.readyState == 4) {
					if (eshophttp.status!=200) {
						alert('Error, could not load sub-categories!');
					} else {
						var data = eshophttp.responseText;
						if (data != '') {
							var subconbox = document.createElement('DIV');
							subconbox.setAttribute('id', 'subcon'+nlevel);
							subconbox.setAttribute('class', 'eshopsubselect'+nlevel);
							subconbox.innerHTML = data;
							conbox.appendChild(subconbox);
						}
					}
				}
			};
        	eshophttp.send('option=com_eshop&task=loadsubselect&cid='+cid+'&level='+level+'&rnd='+rnd);
    	}
		catch(e){}
    	finally{}
	} else {
		var xlevel = level - 1;
		if (xlevel < 0) { xlevel = 0; }
		if (document.getElementById('level'+xlevel+'cid')) {
			var selbox2 = document.getElementById('level'+xlevel+'cid');
			cid = parseInt(selbox2.options[selbox2.selectedIndex].value);
			document.getElementById('cid').value = cid;
		}

		for (var i=1; i < 10; i++) {
			var zlevel = level + i;
			if (document.getElementById('subcon'+zlevel)) {
				var conbox3 = document.getElementById('subcon'+zlevel);
				var selbox3 = document.getElementById('level'+zlevel+'cid');
				conbox3.removeChild(selbox3);
				conbox.removeChild(conbox3);
			}
		}
	}
}


/* pagination for search results */
function eshopgotopage(pg, catid) {
	if (pg < 1) { pg = 0; }
	if (catid < 1) { catid = 0; }
	var frm = document.getElementById('fmeshopsearch');
	document.getElementById('searchpage').value = pg;
	document.getElementById('cid').value = catid; //solution for nested AJAX categories
	frm.submit();
}


/* submit form in order to search for other products with the same extra value */
function eshopsearchextra(extraid, extravalue) {
	extraid = parseInt(extraid);
	if (extraid > 0 && document.getElementById('fmeshopsearchextra')) {
		var frm = document.getElementById('fmeshopsearchextra');
		var inputbox = document.createElement('input');
		inputbox.setAttribute('type', 'hidden');	
		inputbox.setAttribute('name', 'extrafield['+extraid+']');
		inputbox.value = extravalue;
		frm.appendChild(inputbox);
		try { frm.onsubmit(); }
		catch(e){}
		frm.submit();
	}
}

function eshopsearchextraptid(extraid, extravalue, ptid) { 
	if (document.getElementById('fmeshopsearchextra')) {
		if (document.getElementById('searchptid')) {
			ptid = parseInt(ptid);
			document.getElementById('searchptid').value = ptid;
		}
	}
	eshopsearchextra(extraid, extravalue);
}

/* FULL SCREEN POPUP WINDOW */
function eshoppopupfull(url) {
	params = 'width='+screen.width+', height='+screen.height+', top=0, left=0, fullscreen=yes, scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no';
	fullwin = window.open(url,'eshopfullpopup', params);
	if (window.focus) { fullwin.focus(); }
	return false;
}

