/**
 * Javascript functions for shop system ver 4 (Shop4)
 */
 

/**
 * Helper function for insertToCart - insert OK
 */
function ReadCookie(cookieName) {
  var theCookie=""+document.cookie;
  var ind=theCookie.indexOf(cookieName);
  if (ind==-1 || cookieName=="") return ""; 
  var ind1=theCookie.indexOf(';',ind);
  if (ind1==-1) ind1=theCookie.length; 
  return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
      

/**
 * Show shoppingcart - for example on page reload
 */
function displayCart() {
		/**
		 * Update standard shoppingcart if exists
		 */
		var cont = document.getElementById('abexeroomcontainer');
		if (cont && cont.tagName!='') {
			return;	// ignore erooms
		}
		
		cont = document.getElementById('shopcartcontent');
		if (cont && cont.tagName!='') {
			jQuery('#shopcartcontent').load("shop3/inserttocart.php?displayonly=1");
		}

		cont = document.getElementById('shopcartcontenttable');
		if (cont && cont.tagName!='') {
			jQuery('#shopcartcontenttable').load("shop3/inserttocart.php?displayonlytable=1");
		}

		/**
		 * Update simple shoppingcart if exists
		 */
		var contsimple = document.getElementById('shopcartcontentsimple');
		if (contsimple && contsimple.tagName!='') {
			jQuery('#shopcartcontentsimple').load("shop3/inserttocart.php?displayonlysimple=1");
		}

		/**
		 * Update bare minimum shoppingcart if exists
		 */
		var contsimple = document.getElementById('shopcartcontentbareminimum');
		if (contsimple && contsimple.tagName!='') {
			jQuery('#shopcartcontentbareminimum').load("shop3/inserttocart.php?displayonlybareminimum=1");
		}
                updateContentFields();
}

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

/**
 * 
 */
function shop4addToCart(randi) {
		ib = 0;
		mandatoryi = jQuery('#'+randi+' .mandatory').length;
		jQuery('#'+randi+' .mandatory').each(function(){ 
			var val = jQuery(this).val();
			if (!val) {
				// empty
			} else if (val.length>0) {
				ib++;	// ok
			} 
		});
		if (mandatoryi!=ib) {
			alert('Oppgi data i alle felt.'); 
			return false;
		}
		
		jQuery('#insertedtocartmsg'+randi).fadeIn('slow');
		jQuery('#insertedtocartmsg'+randi).html('Vennligst vent...');
		jQuery.post('/cpclass/run/cpshop4/addtocart.php', jQuery('#'+randi).serialize(), function(data) {
			// addedtocarta373221709392 = 1; 
			jQuery('#insertedtocartmsg'+randi).show();
			jQuery('#insertedtocartmsg'+randi).html(data);
			// jQuery('#'+randi).remove();	// remove add to cart form.
			jQuery('#'+randi+' .mandatory').each(function(){
				jQuery(this).val('');
			});
			jQuery('#'+randi+'x').fadeOut('slow');
			jQuery('#'+randi+'xbuy').fadeIn('slow');
		});
		return false;
}



jQuery(document).ready(function(){
  // displayCart();
});



