/*
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){}
	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 oldpricetxt = priceObj.innerHTML;
	if (typeof oldpricetxt == "string") {
		var oldprice = parseFloat(oldpricetxt);
	} else {
		var oldprice = oldpricetxt;
	}

	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;

	if (decimals == 0) {
		newprice = parseInt(newprice);
		priceObj.innerHTML = newprice;
	} else {
		priceObj.innerHTML = newprice.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;
}

