
var cids = {};
var formIds = {};

var orderStep = 'shipping';
var dialogAlertObject = {
		bgiframe: true,
    	resizable: false,
    	modal: true};

/**
 * New Address for shipping-Form
 */
function newAddress()
{
	$('#formNewAddress').show('slow');
}


function cancelNewAddress()
{
	$('#formNewAddress').hide('slow');
}

function clickAbleAddresses()
{
	$('.shippingAddress').click(function() {
		
		$('.shippingAddress').removeClass('activated');
		$(this).addClass('activated');
		
		selectAddress($(this).attr('id'));
		
	});	
}


function selectAddress(id)
{
	$.ajax({
		type: "POST",
		url: '/shop/communication/setshippingaddress',
		data: {'address' : id},
		success: function(responseText)
		{
		}
	});
}


function checkToNext(step)
{
	$.ajax({
		type: "POST",
		url: '/shop/communication/checkstep',
		data: {'check' : step},
		success: function(responseText)
		{
			responseArray = responseText.split('|');
			if(responseArray[1] == 'ok')
			{
				//$('<div id="newDialogText">Alles eingetragen was man braucht, weiter zum nächsten Step: '+ responseArray[2] +'</div>').dialog(dialogAlertObject);
				orderStep = responseArray[2];
				
				location.href=shopURLS[orderStep];
			}
			else
			{
				$('<div id="newDialogText">'+ responseArray[1] +'</div>').dialog(dialogAlertObject);
			}
		}
	});
}


function submitShopAjaxForm(formId, validationURL)
{
	var f = $("#"+formId);
	var paramString = "";
	f.find("input[type='radio'][checked], input[type='checkbox'][checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='file'], input[type='submit'], select, textarea")
	.each(function()
	{
		paramString += this.name || this.id || this.parentNode.name || this.parentNode.id;
		paramString += "=" + proofValue(this.value) +"&";
	});

	$('.errorHint').remove();
	$('#errorsDiv').hide();

	$.ajax({
		type: "POST",
		url: validationURL,
		data: paramString,
		success: function(responseText)
		{
			responseArray = responseText.split("|");
			if(responseArray[1] == "ok")
			{
				$('#addresses').html(responseArray[2]);
				cancelNewAddress();
				
				clickAbleAddresses();
			}
			else
			{
				placeErrors(responseArray);				
			}			
		}
	});	
	
	return false;	
	
}



function convert(articleId, type)
{
	str = (type == 'cart') ? 'Artikel in Warenkorb?' : 'Artikel in Merkliste?';
	
	$('<div id="newDialogText">'+ str +'</div>').dialog({
    	bgiframe: true,
    	resizable: false,
    	modal: true,
    	buttons: {
    		Abbrechen: function()
    		{
    			$(this).dialog('close');
    		},
			OK: function()
			{
    			$(this).dialog('close');
    			
    			var templateTR = $('#tableTR').html();
				var templateFULL = $('#tableFULL').html();	
				var postData = {
						'id': articleId,'type': type,
						'templateTR': templateTR, 'templateFULL': templateFULL,
						'user': $('#cu').val()
				};
				
				$.ajax({
					type: "POST",
					url: "/shop/communication/convert/",
					data: postData,
					success: function(responseText)
					{
						var arr = responseText.split('|');
						
						/**
						 * arr[2] - int
						 * Anzahl Items im Cart
						 */
						if(arr[2] == "0")
						{
							v = $('#noDataCart').html();
							$('#cartWrapper').html(v);
						}
						else
						{
							$('#articleList').html(arr[1]);
						}
						
						/**
						 * arr[3] - y | n 
						 * Reicht das Konto aus, um vom Guthaben zu bezahlen
						 */
						if(arr[3] == "n")
						{
							$('#orderButton').removeClass('orderEnabled');
							$('#orderButton').addClass('orderDisabled');
						}
						else
						{
							$('#orderButton').removeClass('orderDisabled');
							$('#orderButton').addClass('orderEnabled');
						}
						
					}
				});	
    			
    			
			}
    	}
    });
	
				
}


function saveItem(articleId, amount, type, response)
{
	if(type == 'cart')
	{		
		str = (amount == '1')
			? "Bei Ok wird die Anzahl um 1 erh&ouml;ht."
			: "Wollen Sie die Anzahl wirklich verringern?";
	}
	else
	{
		str = (amount == '1')
			? "Artikel auf die Merkliste?"
			: "Wollen Sie die Anzahl wirklich verringern?";		
	}

	$('<div id="newDialogText">'+ str +'</div>').dialog({
    	bgiframe: true,
    	resizable: false,
    	modal: true,
    	buttons: {
    		Abbrechen: function()
    		{
    			$(this).dialog('close');
    		},
			OK: function()
			{
    			rebuild(articleId, amount, type, response);
    			$(this).dialog('close');
			}
    	}
    });

}


function rebuild(articleId, amount, type, response)
{
	var templateTR = $('#tableTR').html();
	var templateFULL = $('#tableFULL').html();
	
	var postData = {
			'id': articleId, 'amount': amount, 'type': type, 'response': response, 
			'templateTR': templateTR, 'templateFULL': templateFULL,
			'user': $('#cu').val(), 'contentId' : cids[type]
		};
	
	$.ajax({
		type: "POST",
		url: "/shop/communication/set/",
		data: postData,
		success: function(responseText)
		{
			var arr = responseText.split('|');
			
			/**
			 * arr[2] - int
			 * Anzahl Items im Cart
			 */
			if(arr[2] == "0")
			{
				v = $('#noDataCart').html();			
				$('#cartWrapper').html(v);
				
				$('#'+ type +'_footerline').hide();
			}
			else
			{
				$('#articleList').html(arr[1]);
				$('#articleList_'+ type).html(arr[1]);
				$('#articleAmount_'+ type).html(arr[2]);
				$('#articleSum_'+ type).html(arr[4]);
					
				$('#'+ type +'_footerline').show();
			}
			
			/**
			 * arr[3] - y | n 
			 * Reicht das Konto aus, um vom Guthaben zu bezahlen
			 */
			if(arr[3] == "n")
			{
				$('#orderButton').removeClass('orderEnabled');
				$('#orderButton').addClass('orderDisabled');
			}
			else
			{
				$('#orderButton').removeClass('orderDisabled');
				$('#orderButton').addClass('orderEnabled');
			}
		}
	}); 
}

function deleteItems(type, all, articleId)
{
	$('<div id="newDialogText">Sollen wirklich alle Elemente gelöscht werden?</div>').dialog({
    	bgiframe: true,
    	resizable: false,
    	modal: true,
    	buttons: {
    		Abbrechen: function()
    		{
    			$(this).dialog('close');
    		},
			OK: function()
			{
    			var postData = {'type': type, 'all': all, 'id' : articleId, 
						'templateTR': $('#tableTR').html(),
						'contentId' : cids[type],
    					'templateFULL': $('#tableFULL').html()};
				
				$.ajax({
					type: "POST",
					url: "/shop/communication/delete/",
					data: postData,
					success: function(responseText)
					{
						var arr = responseText.split('|');
						if(arr[2] == "0")
						{
							v = $('#noDataCart').html();			
							$('#cartWrapper').html(v);
						}
						else
						{
							$('#articleList').html(arr[1]);
						}
					}
				});
    			$(this).dialog('close');
			}
    	}
    });	
}


function sendOrder(uri)
{
	if($('#orderButton').hasClass('orderEnabled'))
	{
		$("#dialogOrder").dialog({
	    	bgiframe: true,
	    	autoOpen: false,
	    	height: 100,
	    	resizable: false,
	    	modal: true,
	    	buttons: {
	    		Abbrechen: function()
	    		{
	    			$(this).dialog('close');
	    		},
				OK: function()
				{
					location.href= uri;
				  	$(this).dialog('close');
				}
	    	}
	    });
		$('#dialogOrder').dialog('open');		
		
	}
	else
	{
		$('#modalNoOrder').dialog({
			modal: true,
			resizable: false,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});
		$('#modalNoOrder').dialog('open');

	}
}

/**
 * MyOrders
 */

function showOrderDetails(id)
{
	$('.tableview tr').removeClass('aktiv');
	
	$('#tr_' + id).addClass('aktiv');
	
	$('.orderDetails').hide();
	postData = { 'id':id };
	
	$.ajax({
		type: "POST",
		url: "/shop/communication/orderdetails/",
		
		data: postData,
		success: function(responseText)
		{
			$('#orderDetails_'+id).html(responseText).show();
		}
	});
}

/**
 * MyOrders EOF
 */



$(document).ready(function() { clickAbleAddresses(); });

