var loadimg = "<center><img src='/images/load.gif'></center>";
var request = false;
var xmlHttp;
var coupon_id_fun;
var MainDivId;
var MailToNameId;
var MailToEmailId;
var MailFromNameId;
var MailFromEmailId;
var OptNoteTxtId;
var MailTopicId;
var MailEmailId;
var MailNameId;
var myString;

function createAjaxObject()
{
	try
	{
		request = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				request = false;
			}
		}
	}
}

function GetXmlHttpObject()
{
 var xmlHttp=null;
 try
 {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
 }
 catch (e)
 {
  // Internet Explorer
  try
  {
   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


function sendMailRequest_coupon(frm, domainUrl, domainUrlName, couponId, couponTitle, storeName,storeVal,couponLink,couponCode,expDate,couponRating)
{

	createAjaxObject();

	if(request)
	{
		MainDivId 	= "MAILSENTMSG_" + couponId;
		MailToNameId	= "NAMETO_" + couponId;
		MailToEmailId 	= "MAILTO_" + couponId;
		MailFromNameId	= "NAMEFROM_" + couponId;
		MailFromEmailId	= "MAILFROM_" + couponId;
		OptNoteTxtId 	= "NOTE_" + couponId;
		
		var mailtoname 	= document.getElementById(MailToNameId).value;
		var mailtoemail = document.getElementById(MailToEmailId).value;
		var mailfromname = document.getElementById(MailFromNameId).value;
		var mailfromemail = document.getElementById(MailFromEmailId).value;
		var optNote = document.getElementById(OptNoteTxtId).value;
		
		if(mailtoname == "")
		{
			alert("Please Enter Name Of Your Friend");
			document.getElementById(MailToNameId).select();
		}
		else if(mailtoemail == "")
		{
			alert("Please Enter Email Address Of Your Friend");
			document.getElementById(MailToEmailId).select();
		}
		else if(!isValidEmail(mailtoemail, MailToEmailId))
		{}
		else if(mailfromname == "")
		{
			alert("Please Enter Your Name");
			document.getElementById(MailFromNameId).select();
		}
		else if(mailfromemail == "")
		{
			alert("Please Enter Your Email Address");
			document.getElementById(MailFromEmailId).select();
		}
		else if(!isValidEmail(mailfromemail, MailFromEmailId))
		{}
		else if(optNote == "")
		{
			alert("Please Enter Personalized  Message");
			document.getElementById(OptNoteTxtId).select();
		}
		else
		{
			if(isValidEmail(mailtoemail, MailToEmailId))
			{
				//alert(mailData);
				var url ="/coupons/sendmail.php";
				
				url=url+"?mailtoemail=" + mailtoemail;
				url=url+"&mailfromname=" + mailfromname;
				url=url+"&mailfromemail=" + mailfromemail;
				url=url+"&mailtoname=" + mailtoname;
				url=url+"&optionalnote=" + optNote;
				url=url+"&domainUrl=" + domainUrl;
				url=url+"&domainUrlName=" + domainUrlName;
				url=url+"&couponId=" + couponId;
				url=url+"&couponTitle=" + couponTitle;
				url=url+"&storeName=" + storeName;
				url=url+"&storeVal=" + storeVal;
				url=url+"&couponLink=" + couponLink;
				url=url+"&couponCode=" + couponCode;
				url=url+"&expDate=" + expDate;
				url=url+"&couponRating="+couponRating;

				url=url+"&sid="+Math.random();
				//alert(url)
				couponid_fun = couponId;
				request.open("GET", url,true);
				request.onreadystatechange = getSendMailResponse;
				request.send(null);
			}
		}
	}
}


function getSendMailResponse()
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			var response = request.responseText;
			
			if(response == "SUCCESS")
			{
				document.getElementById(MainDivId).innerHTML = "<b><font color=green size=2>Mail Successfully Sent !!!</font></b>";
				document.getElementById(MailToNameId).value = "";
				document.getElementById(MailToEmailId).value = "";
				document.getElementById(MailFromNameId).value = "";
				document.getElementById(MailFromEmailId).value = "";
				document.getElementById(OptNoteTxtId).value = "";
				setTimeout("document.getElementById(MainDivId).innerHTML = \"\";",2500);
			}
		}
	}
}
function sendErrorMail(frm,couponId)
{
	createAjaxObject();

	if(request)
	{
		MainDivId 	= "MAILSENTMSG_" + couponId;
		MailNameId	= "NAME_" + couponId;
		MailEmailId 	= "MAIL_" + couponId;
		MailTopicId	= "TOPIC_" + couponId;
		//MailFromEmailId	= "MAILFROM_" + dealId;
		OptNoteTxtId 	= "COMMENT_" + couponId;

		var mailname 	= document.getElementById(MailNameId).value;
		var mailemail = document.getElementById(MailEmailId).value;
		var mailtopic = document.getElementById(MailTopicId).value;
		//var mailfromemail = document.getElementById(MailFromEmailId).value;
		var optNote = document.getElementById(OptNoteTxtId).value;

		if(mailname === "")
		{
			alert("Please Enter Your Name");
			document.getElementById(MailNameId).select();
			return false;
		}
		else if(mailemail !== "")
		{
			if(!isValidEmail(mailemail, MailEmailId)) { return false;}
		}
		if(mailtopic === "")
		{
			alert("Please Select Topic of the mail");
			document.getElementById(MailTopicId).select();
		}
		else if(optNote === "")
		{
			alert("Please Enter Comment");
			document.getElementById(OptNoteTxtId).select();
		}
		else
		{
				var url ="/coupons/sendmail_error.php?mailtoemail="+ mailemail +"&mailtoname="+ mailname +"&mailfromemail=dealverifier@alldealscoupons.com&mailname=Deal Controller&subject=Error Report ADC - " + mailtopic + "&optionalnote=" + optNote;
				url = url+"&couponid="+couponId;
				url = url+"&topic="+mailtopic;
				url = url+"&sid="+Math.random();
				//alert(url)
				coupon_id_fun = couponId;
				request.open("GET", url,true);
				request.onreadystatechange = statChanged_sendErrorMail;
				request.send(null);
		}
	}
}
function statChanged_sendErrorMail()
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			var response = request.responseText;
			/*if(response == "SUCCESS")
			{*/
				document.getElementById(MainDivId).innerHTML = "<b><font color=green size=2>Report Submitted Successfully !!!</font></b>";
				document.getElementById(MailNameId).value = "";
				document.getElementById(MailEmailId).value = "";
				document.getElementById(MailTopicId).value = "";
				document.getElementById(OptNoteTxtId).value = "";
				setTimeout("document.getElementById(MainDivId).innerHTML = \"\";",2500);
			/*}*/
		}
	}
}
function isValidEmail(emailaddress, mailtoTxtId)
{
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

	var returnval = emailfilter.test(emailaddress);
	if(returnval == false)
	{
		alert("Please Enter A Valid Email Address.")
		document.getElementById(mailtoTxtId).value = "";
		document.getElementById(mailtoTxtId).select();
		return false;
	}

	return true;
}

function cancelMailSend(couponId)
{	
	MainDivId 	= "MAILSENTMSG_" + couponId;
	MailToNameId	= "NAMETO_" + couponId;
	MailToEmailId 	= "MAILTO_" + couponId;
	MailFromNameId	= "NAMEFROM_" + couponId;
	MailFromEmailId	= "MAILFROM_" + couponId;
	OptNoteTxtId 	= "NOTE_" + couponId;
		
	document.getElementById(MailToNameId).value= "";
	document.getElementById(MailToEmailId).value= "";
	document.getElementById(MailFromNameId).value= "";
	document.getElementById(MailFromEmailId).value= "";
	document.getElementById(OptNoteTxtId).value= "";
}
function cancelMailSend_error(couponId)
{	MainDivId 	= "MAILSENTMSG_" + couponId;
	MailNameId	= "NAME_" + couponId;
	MailEmailId 	= "MAIL_" + couponId;
	MailTopicId	= "TOPIC_" + couponId;
	OptNoteTxtId 	= "COMMENT_" + couponId;

	document.getElementById(MailNameId).value= "";
	document.getElementById(MailEmailId).value= "";
	document.getElementById(MailTopicId).value= "";
	document.getElementById(OptNoteTxtId).value= "";
}

function tell_coupon_friend(domainUrl,domainUrlName,coupon_id,store_val,store_name,coupon_final_link,coupon_title_new,coupon_code_new,exp_date_new,couponRating)
{
document.getElementById("tell_"+coupon_id).style.display=(document.getElementById("tell_"+coupon_id).style.display!="none")? "none" : "";
	
 coupon_id_fun = coupon_id;
 xmlHttp=GetXmlHttpObject();
 if (xmlHttp==null)
  {
   alert ("Your browser does not support AJAX!");
   return;
  }
 document.getElementById("tell_"+coupon_id_fun).innerHTML=loadimg; 
 var url="/includes/tellafriend_coupon_ajax.php";
 /*url=url+"status=yes";*/
 url=url+"?domainUrl=" + domainUrl;
 url=url+"&domainUrlName=" + domainUrlName;
 url=url+"&coupon_id="+coupon_id_fun;
 url=url+"&store_val="+store_val;
 url=url+"&coupon_final_link="+coupon_final_link;
 url=url+"&store_name="+store_name;
 url=url+"&coupon_title_new="+coupon_title_new.replace("&","%26").replace("+","%2B").replace('"',"DOUBLEPERCENTHASH").replace("'","SINGLEPERCENTHASH").replace("?","%3F").replace(":","%3A").replace("=","%3D");
 url=url+"&coupon_code_new="+coupon_code_new;
 url=url+"&exp_date_new="+exp_date_new;
 url=url+"&couponRating="+couponRating;
 url=url+"&sid="+Math.random();
 //alert(url);
 xmlHttp.onreadystatechange=stateChanged_tell_coupon_friend;
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
 
}

function stateChanged_tell_coupon_friend()
{
if (xmlHttp.readyState==4)
 {
  document.getElementById("tell_"+coupon_id_fun).innerHTML=xmlHttp.responseText;
  
 }
}

/*Report an Error Start*/
function report_error(coupon_id)
{

document.getElementById("tell_"+coupon_id).style.display=(document.getElementById("tell_"+coupon_id).style.display!="none")? "none" : "";

 coupon_id_fun = coupon_id;
 xmlHttp=GetXmlHttpObject();
 if (xmlHttp==null)
  {
   alert ("Your browser does not support AJAX!");
   return;
  }
 document.getElementById("tell_"+coupon_id_fun).innerHTML=loadimg;
 var url="/includes/reporterror_coupons_ajax.php";
 url=url+"?&coupon_id="+coupon_id_fun;
 url=url+"&sid="+Math.random();
 xmlHttp.onreadystatechange=stateChanged_report_error;
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
 
}

function stateChanged_report_error()
{
if (xmlHttp.readyState==4)
 {
	   if (xmlHttp.status == 200)
        	{
			  document.getElementById("tell_"+coupon_id_fun).innerHTML=xmlHttp.responseText;
		}
 }
}
/*Report an Error End*/
