/**
 * Javascript functions for shop system ver 3 (Shop3)
 */
 


/**
 * Helper function for insertToCart - insert OK
 */
baloons = new Array();
function updateOk(request) {
	/**
	 * Extract amount for current item in shopping cart
	 */
	var amount = 1;
	var regEx = new RegExp('value="([0-9]*)"', '') ;
	results = request.responseText.match(regEx)
	if (results) amount = results[1];
	
	/**
	 * Update info window / Create a baloon for for hovering on 
	 */
	id = 'boughtbaloon_'+lastclickedel;
	if (!$(id)) {
			new Insertion.After('tbl_buybtn'+lastclickedel, '<div id="'+id+'" class="boughtbaloon"></div>');
	}
	el = $(id);
	el.style.position = 'absolute';
	el.style.marginTop = '-16px';
	el.style.marginLeft = '90px';
	el.innerHTML = '<b>OK, lagt i <a href="/?checkout=1">handlekurv</a>' + ((amount>1) ? ' ('+amount+')' : '') + '</b>';
	$(id).show();
	//setTimeout(	function() { $(id).hide(); }, 5000);
	setTimeout(	function () { hideBaloons() }, 5000);
	baloons.push(id);
}

function hideBaloons() {
	baloons.each( function(familyMember){
			$(familyMember).hide();
	});
	
}

/**
 * Helper function for insertToCart - insert FAILED
 */
function updateFail() {
	alert('Feil - insert to cart feilet.');
}

/**
 * updateContentFields - updates fields that depends on shoppingcart-updates
 * Like number of items in cart etc
 */
function updateContentFields() {

		/**
		 * Update num items in cart if exists
		 */
		var contnumprods = $('shopcartnumproducts');
		if (contnumprods && contnumprods.tagName!='') {
			new Ajax.Updater('shopcartnumproducts', 'shop3/inserttocart.php', {
												parameters: { displaynumincart: 1 }
											});
		}
}

/**
 * Insert a product to shoppingcart
 */
function insertToCart(artid, prodno, amount, flag, altdesc, altprice) {
		lastclickedel = prodno;
		// var hassize = $('hassize'+prodno).value;
		var packel = document.getElementById('prodpack_'+prodno);
		if (packel) {
			// When a product has a minimum amount/packages.
			if (packel.tagName.match(/INPUT/i)==true) {
				amount = packel.options[packel.selectedIndex].value;
			} else {
				amount = packel.value;
			}
		}
		var tmpicParams = "artid: '" + artid + "', prodno: '" + prodno + "', amount: '" + amount + "'"+ ", flag: '" + flag + "'";	// Ajax REQUEST parameters
		var err = false;
		var notfound = '';
		var found = false;
		var foundaltprodno = '';
		var custcounter = 0;

//		$$('.prodcust').each(function(t) {
		$$('.prodartid'+artid).each(function(t) {
			custcounter++;
			if (t.type=='radio') {
				// as RADIO button
				if (t.checked==true) {
					tmpicParams += ", prodcust_" + t.name + ": '" + t.value + "'";
					foundaltprodno += t.title;
					found = true;
				} else if (!found) {
					notfound = t.name;
				}
			} else { 
				// as SELECT drop down
				if (t.options[t.selectedIndex].title) {
					if (foundaltprodno) foundaltprodno += ', ';
					foundaltprodno += t.options[t.selectedIndex].title;
				}
				var value = "";
				if (t.value=='') {
					if (t.tagName=='SELECT') {
						alert(''+t.options[0].title);	// display title for the select label.
					} else {
						alert(''+t.name);
					}
					err = true;
				} else {
					found = true;
					value = t.value;
					if (t.tagName=='SELECT') {
						// value = t.options[t.selectedIndex].title;
						value = t.options[t.selectedIndex].text;
					}
				}
				// tmpicParams += ", prodcust_" + t.name + ": '" + value + "'";
				tmpicParams += ", prodcust_" + t.name + ": '" + value + "'";
			}	// end of select
		});
		if (foundaltprodno!='') {
			tmpicParams += ", prodcust_altprodno: '" + foundaltprodno + "'";
		}
		
		// If not artid we test if we have alternate description and price
		if (!artid) {
			if (altdesc) {
				tmpicParams += ", prodcust_altdesc: '" + altdesc + "'";
				custcounter++;
				found = true;
			}

			if (altprice) {
				tmpicParams += ", prodcust_altprice: '" + altprice + "'";
				custcounter++;
				found = true;
			}
		}

		eval("icParams = {" + tmpicParams + "}");
		if (custcounter) {
			if (!found) err = true;
			if (err) {
				if (notfound) alert(''+notfound);
				return false;
			}
		}
		new Ajax.Updater('shopcartcontent', 'shop3/inserttocart.php', {
											parameters: icParams,
											onSuccess: updateOk, 
											onFailure: updateFail
										});
		
		/**
		 * If simple shopcart then update it
		 */
		if (!$('shopcartcontent')) displayCart();	// because only default cart type is displayed on insert to cart. 
		updateContentFields();
		return false;
}

/**
 * Show shoppingcart - for example on page reload
 */
function displayCart() {
		/**
		 * Update standard shoppingcart if exists
		 */
		var cont = $('shopcartcontent');
		if (cont && cont.tagName!='') {
			new Ajax.Updater('shopcartcontent', 'shop3/inserttocart.php', {
												parameters: { displayonly: 1 }
											});
		}

		var cont = $('shopcartcontenttable');
		if (cont && cont.tagName!='') {
			new Ajax.Updater('shopcartcontenttable', 'shop3/inserttocart.php', {
												parameters: { displayonlytable: 1 }
											});
		}

		/**
		 * Update simple shoppingcart if exists
		 */
		var contsimple = $('shopcartcontentsimple');
		if (contsimple && contsimple.tagName!='') {
			new Ajax.Updater('shopcartcontentsimple', 'shop3/inserttocart.php', {
												parameters: { displayonlysimple: 1 }
											});
		}

    updateContentFields();
}

function loggInnToggle() {
	jQuery('checkoutloginform').toggle(); 
	jQuery('checkoutinfoform').toggle(); 
	jQuery('logininfotext').toggle(); 
	return false;
}


Event.observe(window, 'load', displayCart);

