/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {
	if($(".crmup_shop").length != 0) {
		var logininputfield;
		buttons();

		if($(".crmup_address").length != 0) {
			toogleVatField($("#country").val());
		}

		if($(".crmup_basket").length != 0) {
			$("#JSCHECK").hide();
			$(".crmup_basket .submit").show();
		}
	}
});

function toogleVatField(val) {
	if($.inArray(val, crmup_vat_countries) != -1) {
		$("#vat").parent().show();
	} else {
		$("#vat").parent().hide();
	}
}

function buttons() {

	$(".crmup_shop.crmup_product_single .productToCart").bind("click", function() {
		$(this).parent("form").submit();
	});

	$(".crmup_shop.crmup_basket .crmup_basket_right .proceedToCheckout .submit").bind("click", function() {
		$(this).parent().append('<input name="pTChO[send]" type="hidden" value="1" id="pTChO_send">');
		$(this).parent().parent().parent("form").submit();
	});


	$(".crmup_shop.crmup_register .standard_button").bind("click", function() {
		$(".crmup_shop.crmup_register .standard_button").unbind();
		var url = $(".crmup_shop.crmup_register form").attr("action");
		var data = $(".crmup_shop.crmup_register form").serialize();

		$.ajax({
			type:		'POST',
			url:		url,
			data:		data,
			dataType:	'json',
			success:	function(res) {
				if(res.state != "success") {
					alert("error!");
					buttons();
				} else {
					var email = $("#email1").val();
					$("#email1").parent().append("<b>" + email + "</b>");
					$("#email1").remove();
					$("#email2").parent().html("");
					$(".crmup_shop.crmup_register .standard_button").parent().parent().html("");
					logininputfield = $(".tx-crmupcrmmembership-pi4").html();
					$(".tx-crmupcrmmembership-pi4").html(res.content);
					buttons();

				}
			}

		});
	});

	$(".crmup_shop.crmup_register_continue .standard_button").bind("click", function() {
		$(".crmup_shop.crmup_register_continue .standard_button").unbind();
		var url = $(".crmup_shop.crmup_register_continue form").attr("action");
		var data = $(".crmup_shop.crmup_register_continue form").serialize();

		$.ajax({
			type:		'POST',
			url:		url,
			data:		data,
			dataType:	'json',
			success:	function(res) {
				if(res.state == "success") {
					$(".tx-crmupcrmmembership-pi4").html(logininputfield);
					$("#user").val(res.data.user);
					$("#pass").val(res.data.pass);
					$(".crmup_shop.crmup_login form").submit();
				} else {
					$.each(res.errors, function(index, value) {
						$("#" + index).addClass("error");
					});
					buttons();

				}
			}

		});
	});

	$(".crmup_shop.crmup_address #country").bind("change", function() {
		toogleVatField($(this).val());
	});
	$(".crmup_shop.crmup_address #country").bind("keyup", function() {
		toogleVatField($(this).val());
	});
	$(".crmup_shop.crmup_confirm_billing_cart tr.body").bind("mouseenter", function() {
		$(this).addClass("hover");
	});
	$(".crmup_shop.crmup_confirm_billing_cart tr.body").bind("mouseleave", function() {
		$(this).removeClass("hover");
	});

	$(".crmup_shop.crmup_summary form").submit(function() {
		if($(this).serialize() != "") {
			return true;
		} else {
			$(this).find(".terms").parent().parent().parent().parent().parent().css("border", "1px solid red")
			return false;
		}
	});

}
