//Load some default store stuff
$(document).ready(function() {
});


function clone_field ( where, name ) {
	var box = document.getElementById(name);
	
	$(function() {
		$('#'+name).clone().appendTo('#'+where);
	});
	
	$("#"+name).last().change( function() { return; } );
	
	if ( navigator.userAgent.toLowerCase().indexOf('chrome') < 0 ) {
		box.value = null;	
	} 
}

function add_inventory () {
	var option = document.getElementById('option_input');
	var qty = document.getElementById('qty_input');
	var all_inventory = document.getElementById('all_inventory');
	var option_list = document.getElementById('option');
	var qty_list = document.getElementById('inventory');
	
	//Verification
	if ( option.value.length == 0 || isNaN(parseInt(qty.value)) || qty.value.length == 0 ) {
		alert ( 'You must enter a option or a type, and the quantity field may only include numbers.' );
		return;
	}
	
	$("#option").append("<option value='"+option.value+"'>"+option.value+"</option>");
	$("#inventory").append("<option value='"+qty.value+"'>"+qty.value+"</option>");
	$("#all_inventory").append("<option value='"+option.value+"'>"+option.value+" - "+qty.value+"pcs </option>");
	
	/*Add to the options list 
	var option_option = document.createElement('OPTION');
	option_option.text = option.value;
	option_option.value = option.value;
	alert ( '1' );
	option_list.options.add(option_option, null);
	alert ( '2' );
	//Add to the inventory list
	var qty_option = document.createElement('OPTION');
	qty_option.text = parseInt(qty.value);
	qty_option.value = parseInt(qty.value);
	qty_list.add(qty_option, null);
	alert ( '2' );
	//Add to the display list
	var all_option = document.createElement('OPTION');
	all_option.text = option.value + " - " + parseInt(qty.value) + "pcs";
	all_option.value = option.value;
	all_inventory.add(all_option, null);
	*/
	
	option.value = '';
	qty.value = '';
	
	
	listbox_select_all ( 'option' );
	listbox_select_all ( 'inventory' );
}

function remove_inventory () {
	var option = document.getElementById('option_input');
	var qty = document.getElementById('qty_input');
	var all_inventory = document.getElementById('all_inventory');
	var option_list = document.getElementById('option');
	var qty_list = document.getElementById('inventory');
	
	var index = all_inventory.selectedIndex;
	
	option.value = option_list.options[index].value;
	qty.value = qty_list.options[index].value;
	
	option_list.remove(index);
	qty_list.remove(index);
	all_inventory.remove(index);
}

function listbox_select_all (which) {	
	var box = document.getElementById ( which );
	for ( var x=0; x < box.options.length; x++ ) {
		box.options[x].selected = true;
	}

	if ( box.style.visibility == 'visible' )
		box.focus();
}


function add_review () {
	var rating_box = document.getElementById('rating');
	var review_box = document.getElementById('review');
	var user_id_box = document.getElementById('user_id');
	var product_id_box = document.getElementById('product_id');
	var msg_box = document.getElementById('message');
	var do_action_box = document.getElementById('doaction');
	var id_box = document.getElementById('id');
	
	msg_box.style.visibility = 'visible';
	msg_box.innerHTML = "<img src='https://s3.amazonaws.com/pr_img/loading.gif'>";
	var data_str =  "rating="+rating_box.value+"&review="+review_box.value+"&user_id="+user_id_box.value+"&product_id="+product_id_box.value+"&doaction="+do_action_box.value+"&id="+id_box.value;
	$(function() { 
		$.ajax({
			   type: "POST",
			   url: "/store/ajax_review", 
			   data: data_str, 
			   success: function(data) { $('#message').html(data); }
			   });
	});
	
	setTimeout("hide_message();", 5000);
}

function hide_message() {
	var box = document.getElementById('message');
	
	$(function() {
			   $('#message').fadeOut(1000, function() { box.style.visibility = 'hidden'; });
	});
    
    location.reload(true);
}


function add_to_cart() {
	var add_to_cart = document.getElementById('add_to_cart');
	var product_id = document.getElementById('product_id').value;
	var add_to_cart = document.getElementById('add_to_cart');
	
	
	var type = document.getElementById('type'+product_id);
	var index=0;
	var qty=true;
	var option_id=true;
	var total=0;
	
	
	var data_str="id="+product_id+"&type="+type.value;
	while ( qty ) {
		qty =document.getElementById('qty'+product_id+index);
		option_id=document.getElementById('option'+product_id+index);
		if ( qty && option_id && qty.value ) {
			data_str = data_str + "&option["+index+"]="+option_id.value+"&qty["+index+"]="+qty.value;
			total += qty.value;
		}
		index++;
	} 
	
	if ( total <= 0 ) {
		return;
	}
	
	add_to_cart.innerHTML = "<img src='/assets/img/loading.gif'>";
	
	$(function() { 
		$.ajax({
			   type: "POST",
			   url: "/store/ajax_add_to_cart", 
			   data: data_str,
			   dataType: 'JSON',
			   success: function(data) {
					var obj = jQuery.parseJSON(data);
					$('#cart_qty').html(obj.cart_qty);
					$('#add_to_cart').html(obj.msg); 
				 }
			});
	});
}


function update_cart (rowid, qty, price, which) {
	var cart = document.getElementById('cart_'+which);
	cart.innerHTML = "<img src='/assets/img/loading.gif' style='float: none; margin-left:auto; margin-right:auto;'>";
	
	var data_str="rowid="+rowid+"&qty="+qty+"&price="+price;
	$(function() { 
		$.ajax({
			   type: "POST",
			   url: "/store/ajax_update_cart", 
			   data: data_str,
			   dataType: 'JSON',
			   success: function(data) { 
				   var obj = jQuery.parseJSON(data);
					$('#cart_qty').html(obj.cart_qty); 
					get_cart(which); 
				 }
			});
	});
	
}

function get_cart(which) {
	$(function() { 
		$.ajax({
			   type: "POST",
			   url: "/store/ajax_get_cart/"+which,  
			   success: function(data) { if (data) $('#cart_'+which).html(data); }
			   });
	});
}


function delete_image ( id ) {
	$(function() { 
		$.ajax({
			   type: "POST",
			   url: "/store/ajax_delete_image/", 
			   data: 'id='+id,
			   success: function(data) { $("#"+id).html(''); }
			   });
	});
}

function make_title_image ( product_id, image_id ) {
	$(function() { 
		$.ajax({
			   type: "POST",
			   url: "/store/ajax_set_title_image/"+product_id+"/"+image_id,
			   success: function(data) { $("#"+image_id).append ( "<br>New Title Image<br>"+data ); }
			   });
	});
}
	
