jQuery(function($){
	$.ajaxSetup ({
		cache: false
	});
	var value = $(".coupon-code").val();
	$(".coupon-code").focus(function(e){
		if(this.value == "Enter Coupon Code" || this.value == "Insertar Cupón"){
			this.value = "";
			$(this).css("color","#000");
		}
	});
	
	$(".coupon-code").blur(function(e){
		if(this.value == ""){
			this.value = value;
			$(this).css("color","#999");
		}
	});
	
	$(".cart-submit").click(function(e){
		if($(".coupon-code").val() == "") return;
		if($(".coupon-code").val() == "Enter Coupon Code" || $(".coupon-code").val() == "Insertar Cupón") return;
		e.preventDefault();
		$.post("http://"+window.location.hostname+"/ajax/get_coupon_value/", { code: $(".coupon-code").val(),products: $("input[name$='custom']").val() }, function(data){
			if(data.error){
				alert(data.error);
			}else{
				$("input[name$='amount']").val(data.newvalue);
				$("input[name$='custom']").val($("input[name$='custom']").val()+"||"+$(".coupon-code").val());
				$("form[name$='paypal_form']").submit();
				//alert(data);
			}
		},'json');
	})
	.ajaxError(function(e, xhr, settings, exception) {
		alert('error in: ' + settings.url + ' \n'+'error:\n' + xhr.responseText );
	});
	
	$(".topic_link").click(function(e){
		e.preventDefault();
		var id = $(this).attr("id");
		$("#products_container").fadeOut(200,function(){
			$(this).html($("#products_"+id).html()).fadeIn(200);
		});
	});
	
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	};
	
	$("#send_email_button_oh_yea").live("click",function(){
		$(".error").css("color","darkred");
		var email = $(".mail-data input[name=email]").val();
		if(!isValidEmailAddress(email)){
			$(".error").html("This is not a valid email address");
			return false;
		}
		var subject = $(".mail-data input[name=subject]").val();
		if(subject == ""){
			$(".error").html("Subject is a required field");
			return false;
		}
		var message = $(".mail-data textarea[name=message]").val();
		if(message == ""){
			$(".error").html("Message is a required field");
			return false;
		}
		
		$(".error").css("color","darkblue");
		$(".error").html("Sending Email");
		$.post("http://"+window.location.hostname+"/ajax/send_email/",{ email:email, subject:subject, message:message },function(data){
			if(data.error){
				alert(data.error);
			}else{
				alert(data.success);
				$.modal.close();
			}
		},'json');
	});
	
	$("#go").live("click",function(){
		$.modal.close();
	});
});
