var xmlHttp
var loadimage1="<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><center><img src='/images/load.gif'><br/><font color=#cccccc>Loading...</font></center><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";
function GetXmlHttpObject()
{ 
	var xmlHttp=null;
	try
	{ 
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{ 
		try
		{ 
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
  return xmlHttp;
}
/***********************************   Paging Starts From Here   *********************************/
var currentPage='';
function page(page,dealtype,sort_by)
{	
	//alert(page);
	//alert(dealtype);
	//alert(sort_by);
	if(page=="")
	if(currentPage=="")
	page=1;
	else
	page=currentPage;
	else
	currentPage=page;
	 xmlHttp=GetXmlHttpObject();
	 if (xmlHttp==null)
	 {
	 	alert ("Your browser does not support AJAX!");
	 	return;
	 }
	if(document.viewForm.deals_per_page.value!="")
	{
		var deals_per_page=document.viewForm.deals_per_page.value;
	}
	else
		var deals_per_page="35";

	if(document.viewForm.view.value!="")
	{
		var viewType=document.viewForm.view.value;
	}
	else
		var viewType="expanded";
		
	if(dealtype=="")
		dealtype="default";

	 var url="/deals/showdeals.php?page="+page;
	 url=url+"&view="+viewType;
	 url=url+"&dealtype="+dealtype;
	 url=url+"&sort_by="+sort_by;
	 url=url+"&dealsPerPage="+deals_per_page;
	 url=url+"&sid="+Math.random();
	 //alert(url);
	 xmlHttp.onreadystatechange=pagingResult;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function pagingResult()
{
	document.getElementById("dealdiv").innerHTML=loadimage1;
	if (xmlHttp.readyState==4)
 	{
 		
 		//alert(document.getElementById("dealdiv").innerHTML);
 		document.getElementById('dealdiv').innerHTML=xmlHttp.responseText;
 	}
}
/***********************************   Paging Ends At Here   *********************************/

/***********************************   Past Deals Starts At Here ************************/

function redirect(year,month,day)
{
	var link_date="";
	link_date=year+"-0"+month+"-0"+day;
	document.getElementById("pastdealcalendar").style.display=(document.getElementById("pastdealcalendar").style.display!="none")? "none" : "none";
	//link_date_fun=link_date;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp===null)
		{alert("Your browser does not support AJAX!");return;}
	var url="/deals/pastdeals/index.php";
	url=url+"?status=yes";
	url=url+"&link_date="+link_date;
	url=url+"&sid="+Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged_redirect;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	//window.location="/deals/showpastdeals.php?link_date="+link_date;
}

function stateChanged_redirect()
{
	//alert(xmlHttp.responseText)
	document.getElementById("pastdealdiv").innerHTML=loadimage1;
	if(xmlHttp.readyState==4)
	{
		document.getElementById("pastdealdiv").innerHTML=xmlHttp.responseText;
	}
}


/***********************************   Past Deals Ends At Here ************************/


/***********************************   Redirect to Search Pages Starts At Here ************************/

function alert_search(msg)
{t_obj=document.getElementById('searchQuery');
 alert(msg);
 t_obj.value='';
 t_obj.focus();
}

function validate(opt,type)
{
	if( type=="deals" )
	{
		tval=document.SearchForm.keyword.value.replace(/^\s+|\s+$/g,"");

		if((tval=="Enter Search Keywords" || tval==="" )  && (opt==0))
		{
			alert_search("Please Enter Keyword To Search");
			return false;
		}
		else
		{
			tval=tval.replace("'","");
			tval=tval.replace('"','');
			tval=tval.replace(/ /g,"+");
			tval=tval.replace(/\//g,"+");
			tval=tval.replace(/\\/g,"+");
			tval=tval.replace(/#/g,"ANDPERCENThash");

			if (tval==="")
				{window.location.href="/";}
			else
			{
				tval=tval.replace("&","%26"); 
				tval=tval.replace(" ","%20"); 
				document.SearchForm.action="/deals/search/index.php?keyword="+tval;
				document.SearchForm.submit();
			}
		}
	}
	else if( type=="coupons")
	{
		tval=document.SearchForm.keyword.value.replace(/^\s+|\s+$/g,"");
		if((tval=="Enter Search Keywords" || tval==="" )  && (opt===1))
		{
			alert_search("Please Enter Keyword To Search");
			return false;
		}
		else
		{
			if (tval==="")
				{window.location.href="/";}
			else
			{
				tval=tval.replace(/ /g,"+");
				tval=tval.replace(/\//g,"+");
				tval=tval.replace(/\\/g,"+");
				tval=tval.replace(/#/g,"ANDPERCENThash");
				tval=tval.replace(/&/g,"ANDPERCENTamp;");
				tval=tval.replace(/'/g,"ANDPERCENTsinglequote;");
				tval=tval.replace(/"/g,"ANDPERCENTdoublequote;");
				document.SearchForm.action="/coupons/search/index.php?keyword="+tval;
				document.SearchForm.submit();
			}
		}
	}
}

/***********************************   Redirect to Search Pages Ends At Here ************************/



/***********************************   Email Validation for Tell a Friend Start At Here ************************/
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;
}
/***********************************   Email Validation for Tell a Friend Start At Here ************************/

/***********************************   Tell a Friend Button OnClick Start At Here ************************/
function sendMailRequest(frm, dealId, mailData) {common_sendMailRequest(frm, dealId, mailData, 1);}
function sendMailRequest_Coupons(frm, dealId, mailData) {common_sendMailRequest(frm, dealId, mailData, 2);}

function getSendMailResponse() {common_getSendMailResponse();}
function getSendMailResponse_Coupons() {common_getSendMailResponse();}
/***********************************   Tell a Friend Button OnClick Start At Here ************************/

/***********************************   Tell a Friend Button OnClick Action Start At Here ************************/
function common_sendMailRequest(frm, dealId, mailData, opt)
{
	var url="";
 	if(opt==2)
  	{
  		var mail_type='';
   		if(frm.mail_type[0].checked) {mail_type=frm.mail_type[0].value;}
   		else if(frm.mail_type[1].checked) {mail_type=frm.mail_type[1].value;}
  	}
 	xmlHttp=GetXmlHttpObject();
	if (xmlHttp===null)
		{alert("Your browser does not support AJAX!");return;}
 	MainDivId="MAILSENTMSG_" + dealId;
	MailToNameId="NAMETO_" + dealId;
	MailToEmailId="MAILTO_" + dealId;
	MailFromNameId="NAMEFROM_" + dealId;
	MailFromEmailId="MAILFROM_" + dealId;
	OptNoteTxtId="NOTE_" + dealId;

	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))
		{
		if(opt==2)
		  {url="/sendmail.php?mailtoemail=" + mailtoemail + "&mailfromname=" + mailfromname + "&mailfromemail=" + mailfromemail + "&mailtoname=" + mailtoname + "&" + mailData + "&optionalnote=" + optNote +"&mail_type=" + mail_type +"&sid="+Math.random();}
		 if(opt==1)
		  {url="/sendmail.php?mailtoemail=" + mailtoemail + "&mailfromname=" + mailfromname + "&mailfromemail=" + mailfromemail + "&mailtoname=" + mailtoname + "&" + mailData + "&optionalnote=" + optNote + "&dealsflag=yes&sid="+Math.random();}
		 //alert(url);
		 myString=mailData;
		 coupon_id_fun=dealId;
		 xmlHttp.open("GET", url,true);
		 if(opt==1) {xmlHttp.onreadystatechange=getSendMailResponse;}
		 if(opt==2) {xmlHttp.onreadystatechange=getSendMailResponse_Coupons;}
		 xmlHttp.send(null);
		}
	} 
}


/***********************************   Tell a Friend Button OnClick Actions Response Start At Here ************************/

function common_getSendMailResponse()
{
	if(xmlHttp.readyState==4)
    	{
    		if(xmlHttp.status==200)
       		{
       			var response=xmlHttp.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);
				var rExp=/Awesome Coupon /gi;
				var results=myString.search(rExp);
				if(results > 0)
	 			{
	 				setTimeout("document.getElementById(\"tell_\"+coupon_id_fun).style.display=\"none\";",5000);
	 			}
	 				results=0;
			}
       		}
    	}
}
/***********************************   Tell a Friend Button OnClick Actions Response Ends At Here ************************/

/***********************************   Tell a Friend cancel Button OnClick Actions Starts At Here ************************/
function cancelMailSend(dealId)
{MainDivId="MAILSENTMSG_" + dealId;
 MailToNameId="NAMETO_" + dealId;
 MailToEmailId="MAILTO_" + dealId;
 MailFromNameId="NAMEFROM_" + dealId;
 MailFromEmailId="MAILFROM_" + dealId;
 OptNoteTxtId="NOTE_" + dealId;

 document.getElementById(MailToNameId).value="";
 document.getElementById(MailToEmailId).value="";
 document.getElementById(MailFromNameId).value="";
 document.getElementById(MailFromEmailId).value="";
 document.getElementById(OptNoteTxtId).value="";
}
/***********************************   Tell a Friend cancel Button OnClick Actions Starts At Here ************************/

function sendMailUI(mailDivId, mailData, id)
{//var sendmailui="<B>Email Address : </B><INPUT TYPE=\"TEXT\" NAME=\"MAILTO_"+ id +"\" ID=\"MAILTO_"+ id +"\">&nbsp;<INPUT TYPE=\"BUTTON\" NAME=\"SENDMAIL\" onClick=\"javascript:sendMailRequest(this.form, 'MAILTO_"+ id +"', 'NOTE_"+ id +"', '"+ mailData +"', '"+ mailDivId +"');\" VALUE=\"SEND\">&nbsp;<INPUT TYPE=\"BUTTON\" NAME=\"CANCEL\" onClick=\"javascript:cancelMailSend('"+mailDivId+"')\" VALUE=\"CANCEL\">";
 var prevUIData="<TABLE BORDER=0 align=center cellspacing=\"0\" cellpadding=\"0\"><TR><TD width=\"100%\" COLSPAN=5><TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" ><TR><TD class=\"hline\" WIDTH=100%></TD></TR></TABLE></TD></TR><TR><TD class=\"vline\"><img src=\"images/clear.gif\"></TD><TD>";
 var mainUIData="<TABLE BORDER=0 width=100% align=center><TR align=left><TD valign=center ALIGN=RIGHT><B>Email&nbsp;Address&nbsp;:</B></TD><TD><INPUT CLASS=\"textinput\" TYPE=\"TEXT\" NAME=\"MAILTO_"+ id +"\" ID=\"MAILTO_"+ id +"\" size=30></TD><TD><INPUT CLASS=\"button\" TYPE=\"BUTTON\" NAME=\"SENDMAIL\" onClick=\"javascript:sendMailRequest(this.form, 'MAILTO_"+ id +"', 'NOTE_"+ id +"', '"+ mailData +"', '"+ mailDivId +"');\" VALUE=\"SEND\">&nbsp;<INPUT CLASS=\"button\" TYPE=\"BUTTON\" NAME=\"CANCEL\" onClick=\"javascript:cancelMailSend('"+mailDivId+"')\" VALUE=\"CANCEL\"></TD></TR><TR align=left><TD valign=center ALIGN=RIGHT><B>Optional&nbsp;Note&nbsp;:</B></TD><TD colspan=2><TEXTAREA class=\"textinput\" NAME=\"NOTE_"+ id +"\" ID=\"NOTE_"+ id +"\" COLS=50></TEXTAREA></TD></TR></TABLE>";
 var nextUIData="</TD><TD class=\"vline\"><img src=\"images/clear.gif\"></TD></TR><TR><TD width=\"100%\" COLSPAN=5><TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><TR><TD class=\"hline\" WIDTH=100%></TD></TR></TABLE></TD></TR></TABLE>";
 sendmailui=prevUIData + mainUIData + nextUIData;
 document.getElementById(mailDivId).innerHTML=sendmailui;
}