﻿var cart_step = 1;

function delete_cart_item(item_id) {
	if (confirm(message_dl_cart_item)) {
		window.location.href = 'ajax.php?cmd=cart&todo=delete&item_id='+item_id;
	}
}

function cart_process_info() {
	cart_step = 1;
	jQuery('div.step2').hide();
	cart_process();
}

function track_page_view(funnel) {
	if (typeof funnels !== "undefined") { 
		//console.log(funnels[funnel]);
		pageTracker._trackPageview(funnels[funnel]);
	}	
}

function cart_process() {
	if(cart_step==1) {
		jQuery('div#cart_table_step1').html('');
		jQuery('div#cart_table_step1').hide();
		var u = 'ajax.php';
		var par_pn = 'cmd=cart&todo=checkout1';
		$('#bt_cart span').html(bt_continue);
		$.post(
		  u,
		  par_pn,
		  function(d_ata){	
				var patt1=/J-S_O-N/gi;
				if(d_ata.match(patt1)) {
					try {
					   var data = jQuery.parseJSON(d_ata); 
					} catch (e) {
					  alert(uncaught_exception);        
					  return -1;
					}	
					
					if(data.code==600) {
		     		alert(admin_md_0001);
		     		window.location.reload();
		     		return -1;  	     		
		     	}									
				} else {		  															  
	   			jQuery('div#cart_table_step2').html(d_ata);
	   			jQuery('div#cart_table_step2').show();
	   			jQuery('div.step1').hide();
	   			jQuery('div.step1_1').show();
	   			cart_step=2;
	   			track_page_view('s2');
	   		}
		  }
		);			
	} else if(cart_step==2) {		
		var u = 'ajax.php';
		var par_pn = jQuery('form[name=cart_form1]').serialize();
		
		$.post(
		  u,
		  par_pn,
		  function(d_ata){	
				var patt1=/J-S_O-N/gi;
				if(d_ata.match(patt1)) {
					try {
					   var data = jQuery.parseJSON(d_ata); 
					} catch (e) {
					  alert(uncaught_exception);  
					  track_page_view('s2');      
					  return -1;
					}	
					
					if(data.code==600) {
		     		alert(admin_md_0001);
		     		window.location.reload();
		     		return -1;  	     		
		     	}	
		     						
					if(data.code==500) {
		     		alert(out_of_date);
		     		window.location.reload();
		     		return -1;  	     		
		     	}					
		     	
				 if(data.code==300) {
		     		$('div#bs_'+data.section+'_message').removeClass('info').addClass('error').show();
		     		$('div#bs_'+data.section+'_message').html(data.message);	
		     		cart_step = 1;   
		     		return -1;  	     		
		     } 		     					
				} else {
	   			jQuery('div#cart_table_step2').html(d_ata);
	   			jQuery('div#cart_table_step2').show();
	   			jQuery('div.step1_1').hide();
	   			jQuery('div.step2').show();
	   			cart_step=3;
	   			track_page_view('s3');
	   		}
		  }
		);			
	} else if(cart_step==3) {		
		var u = 'ajax.php';
		var par_pn = jQuery('form[name=cart_form2]').serialize();
		
		$.post(
		  u,
		  par_pn,
		  function(d_ata){															  
			try {
			   var data = jQuery.parseJSON(d_ata); 
			} catch (e) {
			  alert(uncaught_exception); 
			  track_page_view('s3');         
			  return -1;
			}

			if(data.code==600) {
     		alert(admin_md_0001);
     		window.location.reload();
     		return -1;  	     		
     	}	
			
	     if(data.code==300) {
	     		$('div#py_'+data.section+'_message').removeClass('info').addClass('error').show();
	     		$('div#py_'+data.section+'_message').html(data.message);	     	     		
	     } else if(data.code==500) {
			     		alert(out_of_date);
			     		window.location.reload();
			     		return -1;  	     		
			  }	else {
	 				if(data.next != '') {
	     			window.location.href= data.next;	
	     		}     	
	     } 
		  }
		);			
	}
}

function reset_item() {
	jQuery('div.epn').html('');	
	jQuery('div.spn').show();	
	
	jQuery('a.b_e').show();	
	
	jQuery('a.b_s').hide();	
	jQuery('a.b_c').hide();	
}

function edit_item(id, l, p, q) {
	reset_item();
	
	jQuery('#item_'+id+'_X').hide();
	jQuery('#b_e_'+id).hide();

	
	jQuery('#item_'+id+'_O').html(admin_loading).show();
	
	
	var u = '/inc/_product_detail.php?prd_id='+p+'&item_id='+id+'&lang='+l+'&qty='+q+'&template=pn&publish=2';
	$.ajax({
	  url: u,
	  cache: false,
	  success: function(html){															  
	   	jQuery('#item_'+id+'_O').html(html);
	   	jQuery('#b_s_'+id).show();
			jQuery('#b_c_'+id).show();
	  }
	});		
}

function cancel_item(id) {
	jQuery('#item_'+id+'_O').hide();
	jQuery('#item_'+id+'_X').show();
	
	jQuery('#b_e_'+id).show();
 	jQuery('#b_s_'+id).hide();
	jQuery('#b_c_'+id).hide();	
}

function save_item(id, p) {		
	var par_pn = jQuery('form[name=cart_form]').serialize();
	//alert(par_pn);
	par_pn = par_pn.replace(/todo=update/i, 'todo=update_'+id);

	var _qty = parseInt(document.getElementById('P'+p+'_qty').value);															
	if(_qty > 0 && _qty!='NaN') {
	
	} else {
		alert(ws_cart_error_1);
		return;
	}

	var u = 'ajax.php';
	par_pn = 'cmd=cart&'+par_pn+'&cur_item_id='+item_id;
	
	$.post(
	  u,
	  par_pn,
	  function(d_ata){															  
			try {
			   var data = jQuery.parseJSON(d_ata); 
			} catch (e) {
			  alert(uncaught_exception);        
			  return -1;
			}
			
     if(data.code==300) {
     		$('div#P'+p+'_cart_message').removeClass('info').addClass('error').show();
     		$('div#P'+p+'_cart_message').html(data.message);	     	     		
     } else {
 				if(data.next != '') {
     			window.location.href= data.next;	
     		}     	
     }     			
	  }
	);	
	
}

function show_detail(prd_id) {
	$.ajax({
	  url: 'build.php?todo=product_detail&prd_id=' + prd_id,
	  cache: false,
	  success: function(html){															  
	  
	  $('#dialog').html(html);															  
	  
	  $('.modal').show();
	  }
	});		
}

function close_detail(prd_id) {
	
	$('.modal').hide();	
}

function go_to_diaper() {
	edit_diaper();
	hide_messages();
}

function go_to_cover() {
	var _step = parseInt(document.getElementById('cart_step_bar_step').innerHTML);
	if(_step == 1) {
		document.getElementById("cart_step_bar_error").innerHTML = document.getElementById("cart_step_bar_bug_hidden").innerHTML;
		if(document.getElementById("cart_step_bar_error").innerHTML !='' ) {
			document.getElementById("cart_step_bar_error").style.display = 'block';
		} else {
			document.getElementById("cart_step_bar_error").style.display = 'none';	
		}		
		$('#cart_step_bar_valide').hide();
		$('#cart_step_bar_notice').hide();				
		edit_diaper();
	} else if(_step >= 2) {
		edit_cover();
		hide_messages();
	} 
}

function go_to_accessory() {
	var _step = parseInt(document.getElementById('cart_step_bar_step').innerHTML);
	if(_step == 1) {
		document.getElementById("cart_step_bar_error").innerHTML = document.getElementById("cart_step_bar_bug_hidden").innerHTML;
		if(document.getElementById("cart_step_bar_error").innerHTML !='' ) {
			document.getElementById("cart_step_bar_error").style.display = 'block';
		} else {
			document.getElementById("cart_step_bar_error").style.display = 'none';	
		}		
		$('#cart_step_bar_valide').hide();
		$('#cart_step_bar_notice').hide();				
		edit_diaper();
	} else if(_step == 2) {
		document.getElementById("cart_step_bar_error").innerHTML = document.getElementById("cart_step_bar_bug_hidden").innerHTML;
		if(document.getElementById("cart_step_bar_error").innerHTML !='' ) {
			document.getElementById("cart_step_bar_error").style.display = 'block';
		} else {
			document.getElementById("cart_step_bar_error").style.display = 'none';	
		}		
		$('#cart_step_bar_valide').hide();
		$('#cart_step_bar_notice').hide();				
		edit_cover();
	} else if(_step >= 3) {
		edit_accessory();
		hide_messages();
	}
}

function set_active_tab(no) {
	var count = 1;

	$('div#cart_step_bar span').each(function() {
			if(count==no) {								
				this.className = 'text_activ';		
				//alert(this.className);			
			} else {
				this.className = 'text_inactiv';		
				//alert(this.className);
			}
			count++;
		});	
}

function copy_innerhtml(a, b) {
	eval('document.getElementById("'+a+'").innerHTML = document.getElementById("'+b+'").innerHTML;');
}

function display_element(a) {
	if(document.getElementById(a).innerHTML !='' ) {
		document.getElementById(a).style.display = 'block';
	} else {
		document.getElementById(a).style.display = 'none';	
	}		
}

function obj_locate(objs, id) {
	for ( var i in objs )
	{
	    if(objs[i]['id']==id) return( objs[i] );
	} 		
	
	return null;
} 

function synchronize_message() {		
	copy_innerhtml('text_cvLimit', 'text_cvLimit_hidden');
	copy_innerhtml('text_dpLimit', 'text_dpLimit_hidden');
	copy_innerhtml("cart_step_bar", "cart_step_bar_hidden");	
	copy_innerhtml("cart_step_bar_error", "cart_step_bar_error_hidden");
	
	var cur_step = parseInt(document.getElementById("cart_step_bar_cur_step").innerHTML);
	var kit_step = parseInt(document.getElementById("cart_step_bar_step").innerHTML);
	
	if(cur_step==1) {
		edit_diaper();
		$('#help_step_1').show();
		$('#help_step_2').hide();		
		$('#help_step_3').hide();		
	}
	
	if(cur_step==2) {
		edit_cover();
		$('#help_step_1').hide();
		$('#help_step_2').show();
		$('#help_step_3').hide();		
	}	

	if(cur_step==3) {
		edit_accessory();
		$('#help_step_1').hide();
		$('#help_step_2').hide();
		$('#help_step_3').show();		
	}	
		
	if(document.getElementById("cart_step_bar_error").innerHTML !='' && (cur_step==kit_step)) {
		document.getElementById("cart_step_bar_error").style.display = 'block';
	} else {
		document.getElementById("cart_step_bar_error").style.display = 'none';	
	}
	
	copy_innerhtml("cart_step_bar_valide", "cart_step_bar_valide_hidden");	
	display_element("cart_step_bar_valide");
			
	copy_innerhtml("cart_step_bar_valide", "cart_step_bar_valide_hidden");	
	display_element("cart_step_bar_valide");
			
	copy_innerhtml("cart_step_bar_notice", "cart_step_bar_notice_hidden");	
	display_element("cart_step_bar_notice");		
}	

var prefix = '';
//var kit_id = '<?= 'uuid' ?>';
//var kit_upc = '<?= 145 ?>';

function edit_diaper() {
	set_active_tab(1);
	$('#diaper_pane').show();
	$('#cover_pane').hide();
	$('#accessory_pane').hide();
	$('#help_step_1').show();
	$('#help_step_2').hide();
	$('#help_step_3').hide();
}

function edit_cover() {
	set_active_tab(2);
	$('#diaper_pane').hide();
	$('#cover_pane').show();
	$('#accessory_pane').hide();
	$('#help_step_1').hide();
	$('#help_step_2').show();	
	$('#help_step_3').hide();	
}

function edit_accessory() {
	set_active_tab(3);
	$('#diaper_pane').hide();
	$('#cover_pane').hide();
	$('#accessory_pane').show();
	$('#help_step_1').hide();
	$('#help_step_2').hide();	
	$('#help_step_3').show();	
}

function hide_messages() {
	$('#cart_step_bar_valide').hide();
	$('#cart_step_bar_notice').hide();
	$('#cart_step_bar_error').hide();	
}

function change_prefix(str) {
	prefix = str;
}

function set_specific() {
	eval('beans = '+ prefix + 'beans');
	eval('attribs = '+ prefix + 'attribs');	
	eval('available_items = '+ prefix + 'available_items');	
	eval('opts = '+ prefix + 'opts');		
	eval('prices = '+ prefix + 'prices');		
	eval('items = '+ prefix + 'items');	
	eval('jItems = '+ prefix + 'jItems');	
	eval('_item = '+ prefix + 'item');	
	eval('ini = ' + prefix + 'ini');
	eval('lowest = ' + prefix + 'lowest');
	eval('highest = ' + prefix + 'highest');	
	eval('msrp_lowest = ' + prefix + 'msrp_lowest');
	eval('msrp_highest = ' + prefix + 'msrp_highest');		
}

function lookatkids(cur, cur_value) {	
	set_specific();
	child = cur + 1;
	if(cur_value=='-1') { //no setup, all kids are blank		
		set_blank(child);
		update_selectbox(prefix + 'at_'+child);
		return;
	} else {
		//alert('haha');
		cleanup(cur, cur_value);	
	}
	//alert(cur_value);
	//return;
	//find out all available kids
	//alert('haha');
	cur_attribs = attribs[child];
	//alert(cur_attribs);
	reset_option(child);
	//return;
	
	for(var i=0; i < beans.length; i++) {
		if(beans[i] == 1) {
			//alert(cur_attribs);
			add_option(child, cur_attribs[i]);
		}	
	}
	
	//return;
	
	mask(child);				

}

function set_blank(child){	
	set_specific();	
	//alert(prefix + 'at_'+child);
	//return;
	var selObj = document.getElementById(prefix + 'at_'+child);

  for (var i = 1; i < selObj.options.length; i++) {    
    selObj.remove(i);
    //alert(i);
  }
  
  selObj.options.length=1;	
  selObj.selectedIndex = 0;	
  selObj.disabled = true;
  //hide child
  //selObj.style.display = 'none';
}

function add_option(child, v) {
	set_specific();
	cur_opts = opts[child];
	//alert(v);
	//return;
	
	for(var i=0; i< cur_opts.length; i++) {
		if(v==cur_opts[i][0]) {
			cur_opts[i][2] = 1;
		}
	}
	//alert(cur_opts);	
}

function reset_option(child) {
	set_specific();
	cur_opts = opts[child];	
	for(var i=0; i< cur_opts.length; i++) {
			//alert(cur_opts[i][2]);
			cur_opts[i][2] = 0;
	}		
	//alert(cur_opts);
}

function mask(child) {
	set_specific();
	old = document.getElementById(prefix + 'at_'+child).value;	
	set_blank(child);
	
	var selObj = document.getElementById(prefix + 'at_'+child);
  optionCntr = 1;

  var found = false;  
  for (var loop = 0; loop < opts[child].length; loop++) {
  	if(opts[child][loop][2] == 1) {
			selObj.options[optionCntr] = new Option(opts[child][loop][1], opts[child][loop][0]);
			if ( old == opts[child][loop][0]) {
				selObj.selectedIndex = optionCntr;	
				found = true;		
			}
			optionCntr++;
		}
	}
	
	//show child
	selObj.style.display = 'block';
	
	if(/*!found && */(optionCntr == 2/*at least one option*/)) {
		selObj.selectedIndex = 1;
		selObj.disabled = true;
	} else if(optionCntr == 1) {
		selObj.disabled = true;
	} else {
		selObj.disabled = false;	
	}
		
	update_selectbox(prefix + 'at_'+child);
}

function update_selectbox(id) {
	set_specific();	
	
	return; /* no fancy dropmenu */
	if(ini) return;
	if (!$.browser.msie) {
	  //id = 'at_'+child;
		$('#' + id + '_input').remove();
		$('#' + id + '_container').remove();
		$('#' + id).selectbox();  	
	}	
}

function run(a, nb_attribs) {	
	set_specific();
	for(var i=0; i < a.length; i++) {
		//alert((prefix + 'at_'+a[i]));
		lookatkids(a[i], document.getElementById(prefix + 'at_'+a[i]).value);	
	}	
	//alert(i);
	//if(a.length==0) {
	//	cleanup(0);
	//}	else {
		cleanup(nb_attribs - 1);
	//}
	
	var found = false;
	for(var i=0; i < beans.length; i++) {
		if(beans[i] == 1) {
			//touchdown
			//alert(prices[i]);
			found = true;	
			rewrite_price(prices[i]);
			rewrite_item_no(items[i]);
			rewrite_item_photo(_item[i]);
			item_id = _item[i];
			eval(prefix+'item_id = _item[i];');
			rewrite_available_items(available_items[i]);
			show_cart();	
		}	
	}
	
	if(!found) {		
		document.getElementById(prefix + 'price_box').innerHTML = '';	
		document.getElementById(prefix + 'msrp_box').innerHTML = '';	
		document.getElementById(prefix + 'item_no').innerHTML = '';
		hide_cart();	
		if(lowest != highest) {
			document.getElementById(prefix + 'price_box').innerHTML = '<p class="row"><span class="left xl">'+cart_price+' </span><span class="right2 xl">'+lowest + ' '+ cart_to +' ' + highest+'</span></p>';			
		} else {
			document.getElementById(prefix + 'price_box').innerHTML = '<p class="row"><span class="left xl">'+cart_price+' </span><span class="right xl">'+lowest+'</span></p>';			
		}
		
		if(msrp_lowest != '' || msrp_highest != '') {
			if(msrp_lowest != msrp_highest) {
				document.getElementById(prefix + 'msrp_box').innerHTML = '<p class="row"><span class="left">MSRP</span><span class="right">'+msrp_lowest + ' '+ cart_to +' ' + msrp_highest+'</span></p>';
			} else {
				document.getElementById(prefix + 'msrp_box').innerHTML = '<p class="row"><span class="left">MSRP </span><span class="right">'+msrp_lowest+'</span></p>';
			}		
		}
	}
}

function hide_cart() {
	//$('#cart_sku').hide();
	$('#'+prefix+'cart_available').hide();
	$('#'+prefix+'cart_qty').hide();
	$('#'+prefix+'cart_button').hide();	
}

function show_cart() {
	//$('#'+prefix+'cart_sku').show();
	$('#'+prefix+'cart_available').show();
	$('#'+prefix+'cart_qty').show();
	$('#'+prefix+'cart_button').show();	
}

function rewrite_price(prx) {
	var str = '';
	if(prx.length > 1) {
		str = '<p class="row th"><span class="left">Quantit&eacute;s</span><span class="left">MSRP</span><span class="right">'+cart_price+'</span></p>';
		for(var i=0; i < prx.length; i++) {
				str += '<p class="row tr"><span class="left">' + prx[i][0] + '' + '';
				if(i > 0 && prx[i][1]=='') {
					str += ' '+cart_more;
				} else {
					str += prx[i][1].replace('to', cart_to);
				}
				str += '</span>' + '<span class="left">&nbsp;' + prx[i][4] + '</span>';
				if(''!= prx[i][3]) {
					str += '<span class="right xl">' + prx[i][3] + '</span>' + '' + '<span class="right line">' + prx[i][2] + '</span>';	
				} else {
					str += '<span class="right xl">' + prx[i][2] + '</span>';	
				} 
				str += '' +'</p>';
		}
	} else {
		for(var i=0; i < prx.length; i++) {
				str = '<p class="row"><span class="left xl">'+cart_price+' </span>';
				str += '<span class="left">&nbsp;</span>';				
				if(''!= prx[i][3]) {
					str += '<span class="right xl">' + prx[i][3] + '</span>' + '' + '<span class="right line">' + prx[i][2] + '</span>';	
				} else {
					str += '<span class="right xl">' + prx[i][2] + '</span>';	
				} 
				str += '' +'</p>';				
		}
	}
	str += '';
	document.getElementById(prefix + 'price_box').innerHTML = str;	
	
	
	
	var str = '';
	if(prx.length > 1) {
		str = '';
	} else {
		for(var i=0; i < prx.length; i++) {
				if(prx[i][4] != '') {
					str = '<p class="row"><span class="left">MSRP </span><span class="right">';				
					str += prx[i][4];	
					str += '</span>' +'</p>';
				}
		}
	}
	str += '';
	document.getElementById(prefix + 'msrp_box').innerHTML = str;				
}

function rewrite_item_no(no) {
	document.getElementById(prefix + 'item_no').innerHTML = no;	
}

function rewrite_item_photo(item_id) {
	set_specific();
	//var img = document.getElementById(prefix + 'item_image');	
	obj = obj_locate(jItems, item_id);
	
	//img.src = '<?= '/media/' ?>' + obj['item_image_src'];
	//img.height = obj['item_image_height'];
	//img.width = obj['item_image_width'];
	//<span>Selection</span> 
	jQuery('#'+prefix+'photo_box').html('<img src="/media/'+obj['item_image_src']+'" alt="">').show();
}

function rewrite_available_items(no) {
	document.getElementById(prefix + 'nb_available').innerHTML = '<span class="left">Disponiblit&eacute; </span><span class="right">'+no+'</span>';	
}

function cleanup(cur, cur_value) {
	set_specific();
	for(var i=0; i < beans.length; i++) {
		
		beans[i] = 1;
		
		for(c=0; c <=cur; c++) {		
			cur_attribs = attribs[c];		
			if(cur_attribs[i] != document.getElementById(prefix + 'at_'+c).value) {
				beans[i] = 0;
			}	
		}
		
		
	}	
	//alert(beans);
}

function get_arr(n, ini) {
	if(n < 1) return new Array;	
	
	var as = new Array(n);
	for(var i=0; i<n; i++){
		as[i] = ini + i;	
	}	
	return as;
}

function add_to_cart(prefix) {
								var _qty = parseInt(document.getElementById(prefix+'qty').value);															
								if(_qty > 0 && _qty!='NaN') {
								
								} else {
									alert(ws_cart_error_1);
									return;
								}
              	
								jQuery('#'+prefix+'cart_message').html(bpat); 
								jQuery('#'+prefix+'cart_message').show();
								jQuery.ajax({
								  url: 'ajax.php?cmd=cart&entrance=ajax&todo=add_item&item_id=' + item_id + (true ? 'I' : 'I') + '&qty='+document.getElementById(prefix+'qty').value,
								  cache: false,
								  success: function(html){	
								  if(!html.match(/SeSSiontImeout/))	{	
								  	jQuery('a.amount').html(html);	
								  	jQuery('#'+prefix+'cart_message').html(added);										  
									  jQuery('#cart_message').html(added);
									  jQuery('span#top_cart').show();
									  //alert(jQuery.cookie('cart_value')+'$');
									  //jQuery('span#top_cart span.value').html('('+jQuery.cookie('cart_value')+'$'+')');
									  document.getElementById(prefix+'qty').value = '';
									} else {
										jQuery('#'+prefix+'cart_message').html(html);
									}
								  jQuery('#'+prefix+'cart_message').show();															  
								  }
								});        	
}

function cart_reload() {
	var _h = window.location.href;
	_h = _h.split("#")[0];
	window.location.href = _h.split("?")[0];
}
