var q_c = 1;
var q_t = null;

function role_start()
{
	q_t = setTimeout("role_do()", 5000);
}

function role_do()
{
	clearTimeout(q_t);
	
	$("#q-"+q_c).fadeOut("1000", function() {
		q_c++;
		var all = $("#q-count").val();
		if(q_c >= all)
			q_c = 1;
		$("#q-"+q_c).fadeIn("1000");
		q_t = setTimeout("role_do()", 10000);
	});
}

function newsletter_process()
{
	$("#poll-vote").show();
	$("#poll-results").show();
	$("#poll-show").hide();

	var email = document.getElementById('email').value;
	
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'newsletter_process', action_type : 'ajax', email : email}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#newsletter").attr('innerHTML', msg[0]);
		}
	});
}

function clear_cart(main_div)
{
	$("#clear-cart-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'clear_cart', action_type : 'ajax'}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#"+main_div).attr('innerHTML', msg[0]);
			document.getElementById('basket-q').innerHTML = 0;
		}
	});
}

function add_to_cart(item_cat, item_id)
{
	var q = $("#"+item_cat+"-item_quantity-"+item_id).attr('value');
	var selIndex = document.getElementById('item-price-index-'+item_id).value;
	
	$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").hide();
	$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'add_to_cart', action_type : 'ajax', item_cat : item_cat, item_id : item_id, q : q, selIndex : selIndex}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").hide();
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").attr('innerHTML', msg[0]);
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").show();
			
			document.getElementById('basket-q').innerHTML = msg[1];
		}
	});
}

function remove_from_cart(item_cat, item_id)
{
	$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").hide();
	$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").show();
	
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'remove_from_cart', action_type : 'ajax', item_cat : item_cat, item_id : item_id}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").hide();
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").attr('innerHTML', msg[0]);
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").show();

			document.getElementById('basket-q').innerHTML = msg[1];
		}
	});
}

var last_item = 0;
var last_qty = 0;
function change_qty(val, id, cat)
{
	//alert(val + ", " + id + ", " + cat);
	var v = val;
	if(val == '')
		return;
		
	val = (parseInt(val) >= 0) ? parseInt(val) : 0;
	document.getElementById('qty-value-'+id).value = val;
	if((val == last_qty) && (id == last_item))
		return;
		
	var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	var promocode = document.getElementById('cart-promocode').value;
		
	$("#qty-value-"+id).attr('disabled', true);
	$("#change-qty-icon-"+id).show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'change_qty', action_type : 'ajax', item_cat : cat, item_id : id, q : val, shipping_id : shipping_id, promocode : promocode}),
		dataType: "html",
		success: function(msg)
		{
			last_item = id;
			last_qty = val;
		
			msg = ajax_retrieve_messages(msg);
			$("#change-qty-icon-"+id).hide();
			
			$("#cart-"+id+"-free-price").attr('innerHTML', msg[0]);
			$("#cart-"+id+"-tax").attr('innerHTML', msg[1]);
			$("#cart-"+id+"-price").attr('innerHTML', msg[2]);
			$("#cart-total-price").attr('innerHTML', msg[3]);
			$("#cart-shipping-price").attr('innerHTML', msg[5]);
			$("#cart-promocode-price").attr('innerHTML', msg[6]);
			
			document.getElementById('basket-q').innerHTML = msg[7];
			
			$("#qty-value-"+id).removeAttr('disabled');
		}
	});	
}

function change_shipping()
{
	var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	var promocode = document.getElementById('cart-promocode').value;
	
	$("#change-shipping-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'change_shipping', action_type : 'ajax', shipping_id : shipping_id, promocode : promocode}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#change-shipping-icon").hide();
			
			$("#cart-total-price").attr('innerHTML', msg[0]);
			$("#cart-shipping-price").attr('innerHTML', msg[1]);			
		}
	});		
}

function enter_promocode()
{
	var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	var promocode = document.getElementById('cart-promocode').value;
	
	$("#cart-promocode-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'enter_promocode', action_type : 'ajax', shipping_id : shipping_id, promocode : promocode}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#cart-promocode-icon").hide();
			
			$("#cart-total-price").attr('innerHTML', msg[0]);
			$("#cart-promocode-price").attr('innerHTML', msg[1]);			
		}
	});		
}

function remove_item(item_id, item_cat, main_div)
{
	var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	var promocode = document.getElementById('cart-promocode').value;
	
	$("#change-item-"+item_id+"-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'remove_item', action_type : 'ajax', item_id : item_id, item_cat : item_cat, shipping_id : shipping_id, promocode : promocode}),
		dataType: "html",
		success: function(msg)
		{
			$("#"+item_id).fadeOut('fast');
			msg = ajax_retrieve_messages(msg);

			if(msg[0] == 'none')
			{
				$("#"+main_div).attr('innerHTML', msg[1]);
				document.getElementById('basket-q').innerHTML = msg[2];
			}
			else
			{
				$("#cart-total-price").attr('innerHTML', msg[0]);
				$("#cart-shipping-price").attr('innerHTML', msg[1]);
				$("#cart-promocode-price").attr('innerHTML', msg[2]);
				document.getElementById('basket-q').innerHTML = msg[3];
			}
		}
	});
}

function change_item_price(id)
{
	var val = document.getElementById('item-prices-'+id).options[document.getElementById('item-prices-'+id).selectedIndex].value;
	val = val.split(";");
	document.getElementById('item-price-'+id).innerHTML = val[0];
	//document.getElementById('item-price-'+id+'-small').innerHTML = val[1];
	document.getElementById('item-price-index-'+id).value = document.getElementById('item-prices-'+id).selectedIndex;
}


function check_login_form()
{
	var pass = true;
	
	if(!field_check_text('email', 4, '#fff', '#ff8080'))
		pass = false;
		
	if(!field_check_text('password', 5, '#fff', '#ff8080'))
		pass = false;

	return pass;
}

function check_register_form()
{
	var pass = true;

	if(!field_check_text('name', 1, '#fff', '#ff8080'))
		pass = false;
	
	if(!field_check_text('surname', 1, '#fff', '#ff8080'))
		pass = false;

	if(!field_check_text('emailr', 5, '#fff', '#ff8080'))
		pass = false;
		
	if(!field_check_text('passwordr', 5, '#fff', '#ff8080'))
		pass = false;

	if(!field_check_text('address1', 1, '#fff', '#ff8080'))
		pass = false;
		
	if(!field_check_text('city', 1, '#fff', '#ff8080'))
		pass = false;

	if(!field_check_text('zip', 3, '#fff', '#ff8080'))
		pass = false;

	return pass;
}
