/* $Id: coupon.js 445 2008-12-12 22:13:58Z vincent $ */
$(document).ready(function(){
	var bindBehaviors = function() {
	   	$("#coupons_table tbody tr .trClick").click(function (e) {
			$(".nc_popup").hide();
			$("#coupBackgroundPopup").show();
			var couponId = $(this).parent().attr("id");
			$.get("/coupons/popup", { couponid: couponId },
			function(data){
				var xPos = e.pageX;
				var yPos = e.pageY;
				$('#wrapper').append('<div class="nc_popup" style="position:absolute;top:' + yPos + 'px; left:'+xPos+'px;">' + data + '</div');
				bindBehaviors1();
			});
		});
	 }
	
	var bindBehaviors1 = function() {
		$(".closepopup").click(function () {
			$(".nc_popup").hide();
			$("#coupBackgroundPopup").hide();
		});
	 }
	
	bindBehaviors();
	
	postSubmit();
	
	

	
	
	$("#postcoupon .required").prepend('<span class="setrequired">*</span>');
	
	$("#coupon_search .promtreg").click(function(){
		$("#mustreg").dialog({
					resizable: false,
					height:140,
					modal: true,
					buttons: {
						Close: function() {
							$(this).dialog('close');
							$(this).dialog('destroy');
						},
						Register: function() {
							$(this).dialog('close');
							$(this).dialog('destroy');
							window.open('/register/');
						}
					},
					title: 'Registration Required',
					position: "center",
					zIndex: 30000000
		});
		return false;
	});
	
	// post coupon errors
	if ($("#error_product").length > 0) {
		$("#product_name").wrap('<div class="postCouponError errors"></div>');
		$("#product_errors").html($("#error_product").text());
		$("#product_errors").show();
	};
	
	if ($("#error_categoryid").length > 0) {
		$("#categoryId").wrap('<div class="postCouponError errors"></div>');
		$("#cat_errors").html($("#error_categoryid").text());
		$("#cat_errors").show();
	};
	
	if ($("#error_sourceid").length > 0) {
		$("#sourceId").wrap('<div class="postCouponError errors"></div>');
		$("#sourceId_errors").html($("#error_sourceid").text());
		$("#sourceId_errors").show();
	};
	
	
	if ($("#error_storeSpecific").length > 0) {
		$("#storeSpecificDiv").wrap('<div class="postCouponError errors"></div>');
		$("#storeSpecif_errors").html($("#error_storeSpecific").text());
		$("#storeSpecif_errors").show();
	};
	
	if ($("#error_value").length > 0) {
		$("#formvalue").wrap('<div class="postCouponError errors"></div>');
		$("#value_errors").html($("#error_value").text());
		$("#value_errors").show();
	};
	
	if ($("#error_url").length > 0) {
		$("#urlPrint").wrap('<div class="postCouponError errors"></div>');
		$("#url_errors").html($("#error_url").text());
		$("#url_errors").show();
	};
	
	if ($("#error_inserttype").length > 0) {
		$("#insertType").wrap('<div class="postCouponError errors"></div>');
		$("#insertType_errors").html($("#error_inserttype").text());
		$("#insertType_errors").show();
	};
	
	if ($("#error_sundaySup").length > 0) {
		$("#sundaySupDiv").wrap('<div class="postCouponError errors"></div>');
		$("#sundaySup_errors").html($("#error_sundaySup").text());
		$("#sundaySup_errors").show();
	};
	
	
	if ($("#error_expirytype").length > 0) {
		$("#expire_wrap").wrap('<div class="postCouponError errors"></div>');
		$("#expire_errors").html($("#error_expirytype").text());
		$("#expire_errors").show();
	};
	
	$("#postcoupon #expirytype").bind("change", function(){
		var selected = $(this).attr("value");
		if (selected == 'date') {
			$("#postcoupon #validUntil").slideDown();
		}
		
		else {
			$("#postcoupon #validUntil").hide();
			$("#postcoupon #validUntil").clearForm();
		}
	});
	
	
	
	$("#postcoupon #storeSpecific").bind("change", function(){
		var selected = $(this).attr("value");
		if (selected != 1) {
			$("#postcoupon #storeidDiv").slideDown();
		}
		
		else {
			$("#postcoupon #storeidDiv").slideUp();
			$("#storeid").attr("value", '');
		}
	});	
	
	if ($("#postcoupon #urlPrint label").is(':visible') && $("#sourceid").attr("value") != 10) {
		var urlLabel = $("#postcoupon #urlPrint label").html();
		$("#postcoupon #urlPrint label").html('<span class="setrequired">*</span>' + urlLabel );
	};
	
	if ($("#postcoupon #sundaySupDiv label").length == 1) {
		var urlLabelInsert = $("#postcoupon #insertType label").html();
		$("#postcoupon #insertType label").html('<span class="setrequired">*</span>' + urlLabelInsert );
		
		var urlLabelSS = $("#postcoupon #sundaySupDiv label").html();
		$("#postcoupon #sundaySupDiv label").html('<span class="setrequired">*</span>' + urlLabelSS );
	};
	
	$("#inserttype").bind("change", function(){
		if ($(this).attr("value") != '') {
			$("#postcoupon #insertType").slideDown();
			if ($("#postcoupon #insertType").hasClass("founderror")) {
				$("#insertType_errors").hide();
				$("#postcoupon #insertType").removeClass("founderror")
				$("#insertType").parent().removeClass("postCouponError errors");
			}
		};

	});

	$("#postcoupon #sourceid").bind("change", function(){
		var selected = $(this).attr("value");

		if (selected == 8 || selected == 9 || selected == 10) {
			if (selected == 10) {
				$("#postcoupon #urlPrint label").html('Coupon URL: ');
			}
			
			else {
				if ($("#postcoupon #urlPrint label").text() != '*Coupon URL:') {
					var urlLabel = $("#postcoupon #urlPrint label").html();
					$("#postcoupon #urlPrint label").html('<span class="setrequired">*</span>' + urlLabel );
				}
			}
			
			$("#postcoupon #urlPrint").slideDown();
		}
		else {
			$("#postcoupon #urlPrint").slideUp();
			$("#postcoupon #url").clearForm();
		}
		
		if (selected == 0 && selected != '') {
			$("#postcoupon #sourceDiv").slideDown();
			$("#source").clearForm();
		}
		else {
			$("#postcoupon #sourceDiv").slideUp();
			$("#source").clearForm();
		}
		
		if (selected == 12 || selected == 4) {
			$("#postcoupon #sundaySupDiv").slideDown();

		}
		else {
			$("#sundaySupDiv").slideUp();
			$("#postcoupon #insertType").slideUp();
			$("#sundaySup").clearForm();
			$("#insertType").clearForm();
		}
		
		if (selected == 12) {
			$("#postcoupon #insertType").slideDown();
			
		}
		
		
	});
	
	if ($("#postcoupon #storeSpecific").attr("value") != 1 && $("#postcoupon #storeSpecific").attr("value") != '') {
		$("#postcoupon #storeidDiv").show();
	}
	else {
		$("#postcoupon #storeidDiv").hide();
	}
	
	
	
	if ($("#postcoupon #sourceid").attr("value") == 12 || $("#postcoupon #sourceid").attr("value") == 4) {
		$("#postcoupon #sundaySupDiv").show();
		
	}
	else {
		$("#postcoupon #sundaySupDiv").hide();
	}
	
	if ($("#postcoupon #sourceid").attr("value") == 12) {

	}
	else {
		$("#postcoupon #insertType").hide();
	}
	
	
	
	if ($("#postcoupon #sourceid").attr("value") == 8 || $("#postcoupon #sourceid").attr("value") == 9 ||$("#postcoupon #sourceid").attr("value") == 10) {
		$("#postcoupon #urlPrint").show();
	}
	
	else {
		$("#postcoupon #urlPrint").hide();
	};
	
	if ($("#postcoupon #sourceid").attr("value") == 0 && $("#postcoupon #sourceid").attr("value") != '') {
		$("#postcoupon #sourceDiv").show();
	}
	
	else {
		$("#postcoupon #sourceDiv").hide();
	};
	
	$('#filterCouponSearch #publish_date').datepicker({
		dateFormat: 'mm-dd-yy'
	});
	
	
	// Coupons Filters select
	$(".couponfilters").bind("change", function(){
		
		if ($(this).attr("value") == "12") {
			$("#filterCouponSearch #insertTypeWrap").show();
			$("#filterCouponSearch #publish_date_wrap").show();
			
			
			bindBehaviors();
		}
		else {
			$("#filterCouponSearch #insertTypeWrap").hide();
			$("#filterCouponSearch #publish_date_wrap").hide();
			$("#filterCouponSearch #inserttype").attr("value", "");
			$("#filterCouponSearch #publish_date").attr("value", "");
		}
	});
	

	
	$("#filterCouponSearch #reset").click(function(){
		var loc = window.location= '/coupons';
		resetForm("filterCouponSearch");
		$.get("/coupons/", function(data){
			$("#coupon_left").html(data);
			bindBehaviors();
			return false;
		});
		return false;
	});
	
	$("#advancedSearch #reset").click(function(){

		
		
		$(':input','#advancedSearch')
		 .not(':button, :submit, :reset, :hidden')
		 .val('')
		 .removeAttr('checked')
		 .removeAttr('selected');

		var loc = window.location= '/coupons/advanced-search';
		
		return false;
	});
	
	
	$("#coupBackgroundPopup").click(function () {
		$(this).hide();
		$(".nc_popup").hide();
	});
	

	
	if($('.toggleselect').length > 0){
		var tog = false;
		$('.toggleselect').click(function() {
			$("input[type=checkbox]").attr("checked",!tog);
			if(tog == true){
				$(this).text("Select");
			}
			else {
				$(this).text("Unselect");
			}
			tog = !tog;
		});
	}
	
	if($('#addinfo').attr("value").length > 0){
		var hightlight = $("#addinfo").attr("value");
		$(".additionalinfotd").highlight(hightlight, "hightlight");
	}

	
});

function postSubmit() {
	$("#postcoupon #submitImage").click(function(){
		var formErrors = new Array();
		if (formErrors[0]) {
		};
		var soruceID = $("#postcoupon #sourceid").attr("value");
		
		// Checked Form Elements
		var printUrlValue = $("#postcoupon #url").attr("value");
		
		if (soruceID == 8 && !printUrlValue ) {
			if (!$("#postcoupon #urlPrint").hasClass("founderror")) {
				$("#urlPrint").wrap('<div class="postCouponError errors"></div>');
				$("#url_errors").html('If "Printable" or "Restaurant Printable" is selected from "Coupon Type" please enter a valid URL');
				$("#url_errors").show();
				$("#urlPrint").addClass("founderror");
			};
			formErrors[2] = 1;
		}
		if (soruceID == 8 && printUrlValue){
			
			if ($("#postcoupon #urlPrint").hasClass("founderror")) {
				$("#url_errors").hide();
				$("#postcoupon #urlPrint").removeClass("founderror")
				$("#urlPrint").parent().removeClass("postCouponError errors");
			}
		}
		
		if (soruceID == 9 && !printUrlValue ) {
			if (!$("#postcoupon #urlPrint").hasClass("founderror")) {
				$("#urlPrint").wrap('<div class="postCouponError errors"></div>');
				$("#url_errors").html('If "Printable" or "Restaurant Printable" is selected from "Coupon Type" please enter a valid URL');
				$("#url_errors").show();
				$("#urlPrint").addClass("founderror");
			};
			formErrors[2] = 1;
		}
		
		if (soruceID == 9 && printUrlValue ) {
			if ($("#postcoupon #urlPrint").hasClass("founderror")) {
				$("#url_errors").hide();
				$("#postcoupon #urlPrint").removeClass("founderror")
				$("#urlPrint").parent().removeClass("postCouponError errors");
			}
		}
		
		

		if (soruceID == 12 && $("#postcoupon #sundaySup").attr("value") == '') {
			if (!$("#postcoupon #sundaySupDiv").hasClass("founderror")) {
				$("#sundaySupDiv").wrap('<div class="postCouponError errors"></div>');
				$("#sundaySup_errors").html('If "Sunday Supplement" is selected from "Coupon Type" you must enter a date');
				$("#sundaySupDiv").addClass("founderror");
			};
			
			$("#sundaySup_errors").show();
			formErrors[0] = 1;
		}
		else {
			if ($("#postcoupon #sundaySupDiv").hasClass("founderror")) {
				$("#sundaySup_errors").hide();
				$("#postcoupon #sundaySupDiv").removeClass("founderror")
				$("#sundaySupDiv").parent().removeClass("postCouponError errors");
			}
		}
		
		
		if (soruceID == 12 && $("#postcoupon #inserttype").attr("value") == '') {
			if (!$("#postcoupon #insertType").hasClass("founderror")) {
					$("#insertType").wrap('<div class="postCouponError errors"></div>');
					$("#insertType_errors").html('If "Printable" or "Restaurant Printable" is selected from "Coupon Type" please enter a valid URL');
				$("#insertType").addClass("founderror");
			};
			
		
			$("#insertType_errors").show();
			formErrors[1] = 1;
		}
		
		if (formErrors[0] || formErrors[1] || formErrors[2])
		{
			return false;
		};
	});
	
	jQuery.fn.highlight = function (str, className)
	{
	    var regex = new RegExp(str, "g");

	    return this.each(function ()
	    {
	        this.innerHTML = this.innerHTML.replace(
	            regex,
	            "<span class=\"" + className + "\">" + str + "</span>"
	        );
	    });
	};
}