// Basket Submit Functions
function updateBasket()
{
	if ($('#dataform').length > 0)
	{
		if (typeof xt_form == 'function')
		{
			xt_form($('#dataform')[0], 'F', '7', 'Shop_Basket_Update', 'N', true);
		}
		else
		{
			$('#dataform').submit();
		}
	}

	return false;
}

function submitBasket(nextStepUrl)
{
	if ($('#dataform').length > 0)
	{
		$('#dataform').attr('action', nextStepUrl);

		if (typeof xt_form == 'function')
		{
			xt_form($('#dataform')[0], 'F', '7', 'Shop_Checkout_Submit', 'N', true);
		}
		else
		{

			$('#dataform').submit();
		}
	}

	return false;
}

function submitNotificationData()
{
	if ($('#notficationform').length > 0)
	{
		if (typeof xt_form == 'function')
		{
			if (checkNotificationDataForm())
			{
				xt_form($('#notficationform')[0], 'F', '7', 'Shop_Notification_Submit', 'N', true);
			}
			else
			{
				xt_form($('#notficationform')[0], 'F', '7', 'Shop_Notification_Submit', 'N', false);
			}
		}
		else
		{
			if (checkNotificationDataForm())
			{
				$('#notficationform').submit();
			}
		}
	}

	return false;
}

function submitClientData()
{
	if ($('#clientdataform').length > 0)
	{
		if (typeof xt_form == 'function')
		{
			if (checkClientDataForm())
			{
				xt_form($('#clientdataform')[0], 'F', '7', 'Shop_Clientdata_Submit', 'N', true);
			}
			else
			{
				xt_form($('#clientdataform')[0], 'F', '7', 'Shop_Clientdata_Submit', 'N', false);
			}
		}
		else
		{
			if (checkClientDataForm())
			{
				$('#clientdataform').submit();
			}
		}
	}

	return false;
}

function submitPaymentData(paymentGatewayMode, paymentGatewaySelectionMsg)
{
	if ($('#paymentdataform').length > 0)
	{
		if (typeof xt_form == 'function')
		{
			if (checkPaymentDataForm(paymentGatewayMode, paymentGatewaySelectionMsg))
			{
				xt_form($('#paymentdataform')[0], 'F', '7', 'Shop_Shipping_Payment_Submit', 'N', true);
			}
			else
			{
				xt_form($('#paymentdataform')[0], 'F', '7', 'Shop_Shipping_Payment_Submit', 'N', false);
			}
		}
		else
		{
			if (checkPaymentDataForm(paymentGatewayMode, paymentGatewaySelectionMsg))
			{
				$('#paymentdataform').submit();
			}
		}
	}

	return false;
}

function submitCommentData(maxlen)
{
	if ($('#commentdataform').length > 0)
	{
		if (typeof xt_form == 'function')
		{
			if (checkCommentDataForm(maxlen))
			{
				xt_form($('#commentdataform')[0], 'F', '7', 'Shop_Comment_Submit', 'N', true);
			}
			else
			{
				xt_form($('#commentdataform')[0], 'F', '7', 'Shop_Comment_Submit', 'N', false);
			}
		}
		else
		{
			if (checkCommentDataForm(maxlen))
			{
				$('#commentdataform').submit();
			}
		}
	}

	return false;
}

function submitConfirmationData()
{
	if ($('#confirmationdataform').length > 0)
	{
		if (typeof xt_form == 'function')
		{
			if (checkConfirmationDataForm())
			{
				xt_form($('#confirmationdataform')[0], 'F', '7', 'Shop_Order_Submit', 'N', true);
			}
			else
			{
				xt_form($('#confirmationdataform')[0], 'F', '7', 'Shop_Order_Submit', 'N', false);
			}
		}
		else
		{
			if (checkConfirmationDataForm())
			{
				$('#confirmationdataform').submit();
			}
		}
	}

	return false;
}

function countChars(textid, maxlen)
{
	if ($('#' + textid).length > 0)
	{
		$('#' + textid + '_counter').val($('#' + textid).val().length + " von " + maxlen + " Zeichen");
	}

	return true;
}

function checkNotificationDataForm()
{
	if( $('#notification_read:checked').length > 0 )
	{
		return true;
	}
	else
	{
		alert("Fehler bei der Eingabe ...\nSie müssen das Lesen des Leitfadens bestätigen...");
		return false;
	}
}

// Clientdata validation and Submit Functions
function checkClientDataForm()
{
	var formalEmailCheckRE = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	var bStatus = true;
	var Msg = "Fehler bei der Eingabe ... \n";

	var curInvoiceCountryCode = $('#invoice_country').val();
	if (curInvoiceCountryCode.length != 0)
	{
		for( var i=0; bStatus && i < forbiddenCountries.length; i++ )
		{
			if( curInvoiceCountryCode == forbiddenCountries[ i ] )
			{
				Msg += ' - Im Feld - Rechnungsempfänger Land - muss ein anderer Eintrag ausgewählt sein.\n';
				Msg += '   Bitte bestellen Sie im All-Clad Shop für das von Ihnen ausgewählte Land...\n';
				bStatus = false;
			}
		}
	}

	if( bStatus )
	{
		if( $('#invoice_sex').val().length == 0 || $('#invoice_sex').val() == '0' )
		{
			Msg += ' - Im Feld - Rechnungsempfänger Anrede - muss ein Eintrag ausgewählt werden...\n';
			bStatus = false;
		}

		if( $('#invoice_name_first').val().length == 0 )
		{
			Msg += ' - Das Feld - Rechnungsempfänger Vorname - darf nicht leer sein...\n';
			bStatus = false;
		}

		if( $('#invoice_name_last').val().length == 0 )
		{
			Msg += ' - Das Feld - Rechnungsempfänger Nachname - darf nicht leer sein...\n';
			bStatus = false;
		}

		if( $('#invoice_street').val().length == 0 )
		{
			Msg += ' - Das Feld - Rechnungsempfänger Strasse - darf nicht leer sein...\n';
			bStatus = false;
		}

		if( $('#invoice_zip').val().length == 0 )
		{
			Msg += ' - Das Feld - Rechnungsempfänger Plz. - darf nicht leer sein...\n';
			bStatus = false;
		}

		if( $('#invoice_city').val().length == 0 )
		{
			Msg += ' - Das Feld - Rechnungsempfänger Ort - darf nicht leer sein...\n';
			bStatus = false;
		}

		if( $('#invoice_phone').val().length == 0 )
		{
			Msg += ' - Das Feld - Rechnungsempfänger Telefon - darf nicht leer sein...\n';
			bStatus = false;
		}

		if( $('#invoice_email').val().length == 0 )
		{
			Msg += ' - Das Feld - Rechnungsempfänger EMail-Adresse - darf nicht leer sein...\n';
			bStatus = false;
		}
		else
		{
			if( !formalEmailCheckRE.test($('#invoice_email').val()) )
			{
				Msg += ' - Das Feld - Rechnungsempfänger EMail-Adresse enthält einen ungültigen Wert...\n';
				bStatus = false;
			}
		}

		if( $('#invoice_vatid_data').css('visibility') == 'visible' )
		{
			if ( $('#invoice_vatid').val().length != 0 )
			{
				if( $('#invoice_country').val().length != 0 )
				{
					if ( $('#invoice_vatid').val().length > 2 )
					{
				    	var vatidCountryCode = $('#invoice_vatid').val().substr(0, 2).toUpperCase();
						if (countryCodes[vatidCountryCode] != $('#invoice_country').val())
						{
							Msg += ' - Das Feld - Rechnungsempfänger VAT enthält für dieses Land keinen gültigen Wert...\n';
							bStatus = false;
						}
						else
						{
							var vatidCheck = validateVatID($('#invoice_vatid').val());
							if (!vatidCheck.isValid)
							{
								if (vatidCheck.status == '' || vatidCheck.status == 'INVALID_INPUT')
								{
									Msg += ' - Das Feld - Rechnungsempfänger VAT enthält einen ungültigen Wert...\n';
									bStatus = false;
								}
								else
								{
									Msg += ' - Das Feld - Rechnungsempfänger VAT kann zur Zeit nicht überprüft werden.\n';
									Msg += '   Bitte versuchen Sie es etwas später nochmals...\n';
									bStatus = false;
								}
							}
						}
					}
					else
					{
						Msg += ' - Das Feld - Rechnungsempfänger VAT enthält einen ungültigen Wert...\n';
						bStatus = false;
					}
				}
			}
		}

		if (!isBusinessOrder)
		{
			if( $('#invoice_affiliate').val().length == 0 || $('#invoice_affiliate').val() == '0' )
			{
				Msg += ' - Im Feld - Vertriebspartner - muss ein Eintrag ausgewählt werden...\n';
				bStatus = false;
			}
		}

		if( $('#shipto:checked').length > 0 )
		{
			if( $('#shipto_sex').val().length == 0 || $('#shipto_sex').val() == '0' )
			{
				Msg += ' - Im Feld - Lieferadresse Anrede - muss ein Eintrag ausgewählt werden...\n';
				bStatus = false;
			}

			if( $('#shipto_name_first').val().length == 0 )
			{
				Msg += ' - Das Feld - Lieferadresse Vorname - darf nicht leer sein...\n';
				bStatus = false;
			}
			if( $('#shipto_name_last').val().length == 0 )
			{
				Msg += ' - Das Feld - Lieferadresse Nachname - darf nicht leer sein...\n';
				bStatus = false;
			}

			if( $('#shipto_street').val().length == 0 )
			{
				Msg += ' - Das Feld - Lieferadresse Strasse - darf nicht leer sein...\n';
				bStatus = false;
			}

			if( $('#shipto_zip').val().length == 0 )
			{
				Msg += ' - Das Feld - Lieferadresse Plz. - darf nicht leer sein...\n';
				bStatus = false;
			}

			if( $('#shipto_city').val().length == 0 )
			{
				Msg += ' - Das Feld - Lieferadresse Ort - darf nicht leer sein...\n';
				bStatus = false;
			}

			if( $('#shipto_country').length > 0 )
			{
				if( $('#invoice_vatid_data').css('visibility') == 'visible' )
				{
					if( $('#shipto_country').val().length != 0 )
					{
						if( $('#shipto_country').val() == defaultCountryCode &&
							$('#invoice_vatid').val().length != 0 )
						{

							Msg += ' - Für eine Lieferadresse in ' + $('#shipto_country option:selected').text() + ' ist eine Rechnungsstellung ohne Mwst. nicht möglich.\n';
							Msg += '   Bitte löschen Sie den Eintrag im Feld Rechnungsempfänger VAT...\n';
							bStatus = false;
						}
					}
				}
			}

			if ($('#shipto_vatid_data').css('visibility') == 'visible')
			{
				if ($('#shipto_vatid').val().length != 0)
				{
					if ($('#shipto_country').val().length != 0)
					{
						if ($('#invoice_vatid').length != 0)
						{
							if ($('#shipto_vatid').val().length > 2)
							{
						    	var vatidCountryCode = $('#shipto_vatid').val().substr(0, 2).toUpperCase();
								if (countryCodes[vatidCountryCode] != $('#shipto_country').val())
								{
									Msg += ' - Das Feld - Lieferadresse VAT enthält für dieses Land keinen gültigen Wert...\n';
									bStatus = false;
								}
								else
								{
									if ($('#shipto_vatid').val() != $('#invoice_vatid').val())
									{
										if ($('#shipto_vatid').val().substr(0, 2).toUpperCase() != $('#invoice_vatid').val().substr(0, 2).toUpperCase())
										{
											var vatidCheck = validateVatID($('#shipto_vatid').val());
											if (!vatidCheck.isValid)
											{
												if (vatidCheck.status == '' || vatidCheck.status == 'INVALID_INPUT')
												{
													Msg += ' - Das Feld - Lieferadresse VAT enthält einen ungültigen Wert...\n';
													bStatus = false;
												}
												else
												{
													Msg += ' - Das Feld - Lieferadresse VAT kann zur Zeit nicht überprüft werden.\n';
													Msg += '   Bitte versuchen Sie es etwas später nochmals...\n';
													bStatus = false;
												}
											}
										}
										else
										{
											Msg += ' - Das Feld - Lieferadresse VAT darf bei gleichem Rechnungsempfänger und Lieferadresse Land nicht angegeben werden...\n';
											bStatus = false;
										}
									}
									else
									{
										Msg += ' - Das Feld - Rechnungsempfänger VAT und Lieferadresse VAT dürfen nicht gleich sein...\n';
										bStatus = false;
									}
								}
							}
							else
							{
								Msg += ' - Das Feld - Lieferadresse VAT enthält einen ungültigen Wert...\n';
								bStatus = false;
							}
						}
						else
						{
							Msg += ' - Das Feld - Rechnungsempfänger VAT darf nicht leer sein...\n';
							bStatus = false;
						}
					}
				}
			}
		}
	}

	if( !bStatus )
	{
		alert( Msg );
	}

	return bStatus;
}

function toggleShipToFields()
{
	if( $('#shipto:checked').length > 0 )
	{
		$('#delivery').css('visibility', 'visible');
		$('#delivery_info').css('display', 'none');
		checkShiptoVAT();
	}
	else
	{
		$('#delivery').css('visibility', 'hidden');
		$('#delivery_info').css('display', 'block');
		$('#shipto_vatid_data').css('visibility', 'hidden');
	}

	return true;
}

function checkInvoiceVAT()
{
	var objCountry = $('#invoice_country');

	if( objCountry.length > 0 )
	{
		if( objCountry[0].selectedIndex >= 0 && objCountry[0].selectedIndex < objCountry[0].options.length )
		{
			if( objCountry.val() != defaultCountryCode )
			{
				if (countryEUMemberStatus[objCountry.val()])
				{
					if( $('#invoice_company').val().length != 0 )
					{
						$('#invoice_vatid_data').css('visibility', 'visible');
						checkShiptoVAT();
					}
					else
					{
						$('#invoice_vatid_data').css('visibility', 'hidden');
						$('#shipto_vatid_data').css('visibility', 'hidden');
					}
				}
				else
				{
					$('#invoice_vatid_data').css('visibility', 'hidden');
					$('#shipto_vatid_data').css('visibility', 'hidden');
				}
			}
			else
			{
				$('#invoice_vatid_data').css('visibility', 'hidden');
				$('#shipto_vatid_data').css('visibility', 'hidden');
			}
		}
		else
		{
			$('#invoice_vatid_data').css('visibility', 'hidden');
			$('#shipto_vatid_data').css('visibility', 'hidden');
		}
	}

	return true;
}

function checkShiptoVAT()
{
	if( $('#shipto:checked').length > 0 )
	{
		var objCountry = $('#shipto_country');
		if( objCountry.length > 0 )
		{
			if ($('#invoice_vatid_data').css('visibility') == 'visible' )
			{
				if( objCountry[0].selectedIndex >= 0 && objCountry[0].selectedIndex < objCountry[0].options.length )
				{
					if( objCountry.val() != defaultCountryCode )
					{
						if (countryEUMemberStatus[objCountry.val()])
						{
							if( $('#shipto_company').val().length != 0 )
							{
								$('#shipto_vatid_data').css('visibility', 'visible');
							}
							else
							{
								$('#shipto_vatid_data').css('visibility', 'hidden');
							}
						}
						else
						{
							$('#shipto_vatid_data').css('visibility', 'hidden');
						}
					}
					else
					{
						$('#shipto_vatid_data').css('visibility', 'hidden');
					}
				}
				else
				{
					$('#shipto_vatid_data').css('visibility', 'hidden');
				}
			}
			else
			{
				$('#shipto_vatid_data').css('visibility', 'hidden');
			}
		}
	}

	return true;
}

// Clientdata validation and Submit Functions
function checkPaymentDataForm(paymentGatewayMode, paymentGatewaySelectionMsg)
{
	var bStatus = true;
	var Msg = "Fehler bei der Eingabe ... \n";

	if( $('#payment').val().length == 0 || $('#payment').val() == '0' )
	{
		Msg += ' - Im Feld - Zahlungsmethode - muss ein Eintrag ausgewählt sein...\n';
		bStatus = false;
	}
	else if ( paymentGatewayMode.toUpperCase() == 'DISABLED' && $('#payment').val() == 'CC' )
	{
		Msg += paymentGatewaySelectionMsg;
		bStatus = false;
	}

	if ($('#coupon_code').val().length != 0)
	{
		var couponCodeCheck = validateCouponCode($('#coupon_code').val());
		if (!couponCodeCheck.isValid)
		{
			if (couponCodeCheck.status == '' || couponCodeCheck.status == 'INVALID_INPUT')
			{
				Msg += ' - Das Feld - Gutschein enthält einen ungültigen Wert...\n';
				bStatus = false;
			}
			else if (couponCodeCheck.status != '')
			{
				Msg += ' - Das Feld - Gutschein kann zur Zeit nicht überprüft werden...\n';
				bStatus = false;
			}
		}
	}

	if( !bStatus )
	{
		alert( Msg );
	}

	return bStatus;
}

function checkCommentDataForm(maxlen)
{
	var Msg = "Fehler bei der Eingabe ... \n";
	var bStatus = true;

	if ($('#comment').val().length > maxlen)
	{
		Msg += ' - Das Feld - Ihre Nachricht an uns - darf maximal ' + maxlen + ' Zeichen lang sein...\n';
		bStatus = false;
	}

	if( !bStatus )
	{
		alert( Msg );
	}

	return bStatus;
}

function checkConfirmationDataForm()
{
	var bFormValid = true;
	var Msg = '';

	if ( $('#accept_terms:checked').length == 0)
	{
		Msg += 'Sie haben die AGB nicht akzeptiert.\n';
		bFormValid = false;
	}

	//	if error, do...
	if( !bFormValid )
	{
		alert( Msg );
	}

	return bFormValid;
}

// AJAX Wrapper Funktionen
// Überprüft eine UST-ID unter Nutzung des VIES VAT number validation
// der Europäischen Kommission
function validateVatID(vatid)
{
	var objVatCheck = new Object();

	objVatCheck.isValid = false;
	objVatCheck.vatid = vatid;
	objVatCheck.status = '';

	// encode data
	var encodedData = $.toJSON([vatid]);

	$.ajax
	(
		/*
			Festlegung:
			- Hinter der server script datei wird mit '/' getrennt
			der Funktionsname angegeben
			- Daten / Payload wird JSON encodiert
			- AJAX callbacks werden ausschliesslich per HTTP POST
			angesprochen (Sicherheit XSS)
			- AJAX callbacks müssen für den POST request immer für die
			Daten als Content-Type application/json setzen (Sicherheit)
			- Die Ausgabe des Scripts ist immer JSON encodiert und hat
			Content-Type application/json
		*/
		{
			url: 'all-ajax-basket.php/validateVatID',
			type: 'POST',
			contentType: 'application/json',
			async: false,
			data: encodedData,
			dataType: 'json',
			success: function(data, textStatus, XMLHttpRequest)
			{
				if (typeof(data) == 'object' && data.hasOwnProperty('status') && data.hasOwnProperty('is_valid') && data.hasOwnProperty('error'))
				{
					if (data.status == 'success')
					{
						objVatCheck.isValid = (data.is_valid == 'true');
						objVatCheck.status = data.error;
					}
					else if (data.status == 'error')
					{
						if (ajaxDebugLevel > 0)
						{
							if (data.hasOwnProperty('errmsg') && data.hasOwnProperty('errctx'))
							{
								alert('validateVatID - \'' + data.errmsg + '\'\n' + data.errctx);
							}
							else
							{
								alert('validateVatID - AJAX Anforderung fehlgeschlagen');
							}
						}
					}
				}
			}
		}
	);

	return objVatCheck;
}

// AJAX Wrapper Funktionen
// Überprüft die Gültigkeit eines Gutschein-Codes
function validateCouponCode(coupon_code)
{
	var objCouponCodeCheck = new Object();

	objCouponCodeCheck.isValid = false;
	objCouponCodeCheck.couponcode = coupon_code;
	objCouponCodeCheck.status = '';

	// encode data
	var encodedData = $.toJSON([coupon_code]);

	$.ajax
	(
		/*
			Festlegung:
			- Hinter der server script datei wird mit '/' getrennt
			der Funktionsname angegeben
			- Daten / Payload wird JSON encodiert
			- AJAX callbacks werden ausschliesslich per HTTP POST
			angesprochen (Sicherheit XSS)
			- AJAX callbacks müssen für den POST request immer für die
			Daten als Content-Type application/json setzen (Sicherheit)
			- Die Ausgabe des Scripts ist immer JSON encodiert und hat
			Content-Type application/json
		*/
		{
			url: 'all-ajax-basket.php/validateCouponCode',
			type: 'POST',
			contentType: 'application/json',
			async: false,
			data: encodedData,
			dataType: 'json',
			success: function(data, textStatus, XMLHttpRequest)
			{
				if (typeof(data) == 'object' && data.hasOwnProperty('status') && data.hasOwnProperty('is_valid') && data.hasOwnProperty('error'))
				{
					if (data.status == 'success')
					{
						objCouponCodeCheck.isValid = (data.is_valid == 'true');
						objCouponCodeCheck.status = data.error;
					}
					else if (data.status == 'error')
					{
						if (ajaxDebugLevel > 0)
						{
							if (data.hasOwnProperty('errmsg') && data.hasOwnProperty('errctx'))
							{
								alert('validateCouponCode - \'' + data.errmsg + '\'\n' + data.errctx);
							}
							else
							{
								alert('validateCouponCode - AJAX Anforderung fehlgeschlagen');
							}
						}
					}
				}
			}
		}
	);

	return objCouponCodeCheck;
}

