var in_progress = new Array();
function fnAddProductToMyCart( selectedAction, prod_id )
{
	var prod_ammount = $("#form_p_"+prod_id+" input[@name='prod_ammount']").val();
	var lang = $("#form_p_"+prod_id+" input[@name='lang']").val();
	//alert(prod_ammount);
	//alert(prod_id);
	//alert(lang);
	
	if(in_progress[prod_id]==1) {
		//alert("In progress...");
		//console.log("In progress...");
	} else {
		fnAddProductToMyCartAjax( selectedAction, prod_id, prod_ammount, lang );
	}
}
	function fnAddProductToMyCartAjax( selectedAction, prod_id, prod_ammount, lang )
	{
		in_progress[prod_id] = 1;
		$.post(
			"/ajax/ajax_cart.php"
			,{ "action": selectedAction, "prod_id": prod_id, "prod_ammount": prod_ammount, "lang": lang }
			,function(responseText, textStatus, XMLHttpRequest) {
				// Callback function!!!   	//alert("done");
				//console.log(responseText);      //alert(textStatus); // success
				$(responseText).each(function(key, serverData) {
					//alert("a");
					if(serverData.result==1) {
						//alert("success");
						//console.log(serverData);
						if(serverData.position=="alert") {
							myAlert(serverData.data);
						}
						if(serverData.position=="update_cart") {
							$("#cart").html(serverData.data);
						}
					}
				});
				in_progress[prod_id] = 0;
			}
			,"json"
		), function() {
			// $(divId).slideDown('slow');
		};
	}

function changeCount(ammount, product_id, lang, user_id, fingerprint)
{
	$("#priceallid"+product_id+"").load("/ajax/ajax_changecount.php", {
		ammount: ammount, 
		product_id: product_id, 
		lang: lang,
		user_id:user_id, 
		fingerprint: fingerprint
	});
}

/*
$(document).ready(function(){
	if ($("#alert").length > 0){
    	// do something here
    	//myAlert("Success");
	} else {
		//alert("not exists");
	}
	// a better?
	if ( $("#alert").is('*') ){
    	// do something here
    	//myAlert("Success");
	} else {
		//alert("not exists");
	}
});
*/

function myAlert(text) {
	//$('#'+div_id).find('div.jqmAlertContent').html(msg);
	//$('#alert').find('div#').html(msg);
	//$("body").append('<div id="alert" class="flora"></div>');
	if ( $("#alert").is('*') ){
    	// ok, the ID exists
	} else {
		// ID not exists - add it
		$('<div id="alert" class="flora"></div>').appendTo('body');
	}
	
	$("#alert").html(text);
	
	$('#alert').dialog(
					{
						"modal": true
						,"overlay": { backgroundColor: '#000000', opacity: 0.4 }
						
						//,"show": $(this).slideDown(1200)
						//,"show": $(this).fadeIn(1200) // All this is not working (modal dissappears)
						
						,"resizable": false
						,"draggable": true
						,"title": 'Info'
						,"buttons": {
							//"Ok": function() { alert("Ok"); }
							//,"Cancel": function() { $(this).dialog("close"); }
							"Close": function() { $(this).dialog("close"); }
						}
						,"height": 250
					}
	);
	//setTimeout("$('#alert').dialog('close');", 15000); // close after NN sec
	//setTimeout("$('#alert').dialog('destroy');", 4000); // destroy after NN sec
}

function getCurrentAddress()
{
	var addr_id = $("#form_address").val();
	var lang = $("input[@name='ajax_lang']").val();
	
	var cour_id = $("#form_courier").val();
	//alert(cour_id);
	
	getCurrentAddressAjax("get_current_address", lang, addr_id, cour_id);
}
	function getCurrentAddressAjax(selectedAction, lang, addr_id, cour_id)
	{
		$.post(
			"/ajax/ajax_cart.php"
			,{ "action": selectedAction, "addr_id": addr_id, "cour_id": cour_id, "lang": lang }
			,function(responseText, textStatus, XMLHttpRequest) {
				// Callback function!!!   	//alert("done");
				//console.log(responseText);      //alert(textStatus); // success
				$(responseText).each(function(key, serverData) {
					//alert("a");
					if(serverData.result==1) {
						//alert("success");
						//console.log(serverData);
						if(serverData.position=="alert") {
							myAlert(serverData.data);
						}
						if(serverData.position=="update_chosen_address") {
							$("#address_chosen").html(serverData.data);
						}
						if(serverData.position=="update_chosen_courier") {
							$("#courier_chosen").html(serverData.data);
						}
						if(serverData.position=="update_order_totals_table") {
							$("#table_order_totals").html(serverData.data);
						}
					}
				});
			}
			,"json"
		), function() {
			// $(divId).slideDown('slow');
		};
	}


function getCurrentInvoicedata()
{
	var comp_id = $("#form_invoicedata").val();
	var lang = $("input[@name='ajax_lang']").val();
	
	getCurrentInvoicedataAjax("get_current_invoicedata", lang, comp_id);
}
	function getCurrentInvoicedataAjax(selectedAction, lang, comp_id)
	{
		$.post(
			"/ajax/ajax_cart.php"
			,{ "action": selectedAction, "comp_id": comp_id, "lang": lang }
			,function(responseText, textStatus, XMLHttpRequest) {
				// Callback function!!!   	//alert("done");
				//console.log(responseText);      //alert(textStatus); // success
				$(responseText).each(function(key, serverData) {
					//alert("a");
					if(serverData.result==1) {
						//alert("success");
						//console.log(serverData);
						if(serverData.position=="alert") {
							myAlert(serverData.data);
						}
						if(serverData.position=="update_chosen_invoicedata") {
							$("#invoicedata_chosen").html(serverData.data);
						}
					}
				});
			}
			,"json"
		), function() {
			// $(divId).slideDown('slow');
		};
	}

