var current_coupon_report = 0;

function dsmcc_reportCoupon(id, from)
{
	var reason_val;
	var num_reasons = 5; 
	var num;

	document.getElementById('ReportCouponLoader').style.display = "block";

	for(r=0;r<num_reasons;r++)
	{
		num = r + 1;
		if(document.getElementById('reason'+ num +'_' + id).checked == true)
		{
			reason_val = document.getElementById('reason' + num + '_' + id).value;
			break;
		}	
	}	

	var data = {
		coupon_id:  document.getElementById('coupon_id_' + id).value,
		reason: reason_val, 
		description: document.getElementById('description_' + id).value,
		client_ip: document.getElementById('client_ip_' + id).value,
		token: '1e85394b7f40bdb4c539725ffee17918_2_3'
	};

	jQuery.post('/wp-content/plugins/dsmcoupons/includes/report.php', data, function(response) { 
		document.getElementById('ReportCoupon' + current_coupon_report).style.display = "none";
		document.getElementById('ReportCouponLoader').style.display = "none";
		
		if(response["errorMsg"] != undefined && response["errorMsg"] != "")
			jQuery("#ReportCouponMessage").html("<h1>Error</h1>" + response["errorMsg"])
		else
			jQuery("#ReportCouponMessage").html("<h1>Coupon Submitted</h1>" + response["message"])

		dsmcc_fadeInOutDiv("#ReportCouponMessage", 1);
		setTimeout("dsmcc_hideReportMessage()",2000);

	},'json');

	if(from == "onSubmit")
		return false;
}

function dsmcc_hideReportMessage()
{
	dsmcc_fadeInOutDiv("#ReportCouponMessage", 0);
}

function dsmcc_showReportCouponDiv(id)
{
	if(current_coupon_report != id)
	{
		if(current_coupon_report != 0)
		{
			dsmcc_closeReportCouponDiv(current_coupon_report);
		}
		current_coupon_report = id;
		var html = "";
		jQuery(document).ready( function () {
			coupon_name = document.getElementById('Desc' + id).innerHTML; 

			html += "<div class='ReportCouponContainer'>";
				html += "<input type='hidden' id='coupon_id_"+ id +"' value='"+ id +"' name='coupon_id' value='"+ id +"'>";
				html += "<input type='hidden' id='client_ip_"+ id +"' value='38.107.179.227' name='client_ip'>";
				html += "<h1><div style='float: left;'>Report a Coupon</div><div id='ReportCouponLoader' style='float: right;'><img src='/wp-content/plugins/dsmcoupons/images/loader.gif' border='0'></div><div class='clearfix'></div></h1>";
				html += "<h2>Coupon Being Reported</h2>";
				html += "<div class='cpTitle'>"+ coupon_name +"</div>";
				html += "<h2>Reason</h2>";
				html += "<input type='radio' name='reason' id='reason1_"+ id +"' value='No longer available' style='width: 20px !important;' checked> <label for='reason_1'>No longer available</label><br>";
				html += "<input type='radio' name='reason' id='reason2_"+ id + "' value='Different value in my region' style='width: 20px !important;'> <label for='reason_2'>Different value in my region</label><br>";
				html += "<input type='radio' name='reason' id='reason3_"+ id + "' value='Data entry error' style='width: 20px !important;'> <label for='reason_3'>Data entry error</label><br>";
				html += "<input type='radio' name='reason' id='reason4_"+ id + "' value='Duplicate' style='width: 20px !important;'> <label for='reason_4'>Duplicate</label><br>";
				html += "<input type='radio' name='reason' id='reason5_"+ id + "' value='Other' style='width: 20px !important;'> <label for='reason_5'>Other</label><br>";
				html += "<h2>Description</h2>";
				html += "<textarea id='description_"+ id + "'></textarea>";
				html += "<input class='button' type='button' value='Submit Coupon' onclick=\"dsmcc_reportCoupon("+ id +",'button');\">";
				html += "<input class='button' type='button' value='Cancel' onclick='dsmcc_closeReportCouponDiv("+ id +")'>";
			html += "</div>";
	
			jQuery("#ReportCoupon" + id).html(html);
		});
	}
}

function dsmcc_closeReportCouponDiv(id)
{
	current_coupon_report = 0;

	jQuery(document).ready( function () {
		jQuery("#ReportCoupon" + id).html("");
	});
}


function dsmcc_fadeInOutDiv(tagname, showhide)
{

    jQuery(document).ready(function() {
        if(showhide == 1)
        {
            jQuery(tagname).animate({opacity: "show"}, "fast");
        }   
        else if(showhide == 0)
        {
            jQuery(tagname).animate({opacity: "hide"}, "fast");
        }
        else
        {
            jQuery(tagname).animate({opacity: "toggle"}, "fast");
        }   
    });
}


jQuery(document).ready( function() {
	var dsmcc_message_div  = document.createElement("div");
	dsmcc_message_div.setAttribute("id","ReportCouponMessage");
	document.body.appendChild(dsmcc_message_div);
});

