<!--#include file="../Includes/db_pub.asp" -->
<head>
<title>Welcome, mycityeats.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function validateEmail(addr,man,db) {
if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) alert('email address contains invalid characters');
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
}
function Valid()
{
 if(document.popfirst.email.value=="" ||  validateEmail(document.popfirst.email.value,true,false)==false)
 {
  alert("Please Enter Valid Email Address");
  return false;
 }
 if(document.popfirst.zip.value=="") 
 {
 alert("Please Enter Zip Code");
 return false;
 }
 document.popfirst.issubmit.value="1"
 return true;
}
</script>
</head>
<body bgcolor="#EFE0BE" style="margin:0px">
<%
dim myPSQL
dim frs
dim flag
flag=false

if session("name")="" or session("pass")="" then
Response.Redirect("default.asp")
end if

private function SendMail(ByVal subject,ByVal from,ByVal towhich,ByVal body)
on error goto 0
	dim host
	host = "smtp.mycityeats.com"
    Dim MyCDONTSMail
    Dim HTML
    Set MyCDONTSMail = CreateObject("CDONTS.NewMail")
   
    MyCDONTSMail.From= from
    MyCDONTSMail.To=towhich
    MyCDONTSMail.Subject=subject
    MyCDONTSMail.BodyFormat=0
    MyCDONTSMail.MailFormat=0
    MyCDONTSMail.Body=body
	On Error Goto 0
    MyCDONTSMail.Send
	on error resume next
    set MyCDONTSMail=nothing
	exit function

	Response.Write(err.description)
end function
Dim PI, EQUATOR_LAT_MILE
Private maxLat, minLat, maxLong, minLong
dim PQuery
PI = 3.14159265358979
EQUATOR_LAT_MILE = 69.172
Public Sub Calculate(ByVal Latitude, ByVal Longitude, ByVal Miles)
        maxLat = Latitude + Miles / EQUATOR_LAT_MILE
        minLat = Latitude - (maxLat - Latitude)
        maxLong = Longitude + Miles / (Cos(minLat * PI / 180) * EQUATOR_LAT_MILE)
        minLong = Longitude - (maxLong - Longitude)
End Sub
private function getRestaurants(distance) 
if Request("email")<>"" and Request("zip")<>"" then
		dim rs
		PQuery = "Select Latitude,Longitude from ZipCodes where ZipCode='" & Request("zip") & "'"
		set rs=Conn.execute(PQuery)
        if not rs.eof then
			Calculate rs(0),rs(1),distance
			dim rsResult
			dim Qr 
			Qr="Select distinct(ZipCode) from zipcodes where Latitude<=" & maxLat & " and Latitude>=" & minLat & " and Longitude<=" & maxLong & " and Longitude>=" & minLong & " "
			set rsResult=Conn.execute(Qr)
			myPSQL = myPSQL & " Zip in (  '" & rsResult(0) & "' "
			rsResult.movenext
			while not rsResult.eof
				myPSQL = myPSQL & ",'" & rsResult(0) & "' "
				rsResult.movenext
			wend
			myPSQL=myPSQL & ")"
		else
			myPSQL = myPSQL & "Zip in( '" & Request("zip") & "' )"
		end if
		
	SQL="select RestaurantID,Name from Restaurant where  " & myPSQL & ""
	myPSQL=""
	flag=true
	set frs=Conn.execute(SQL)	
	getRestaurants=frs.recordcount
else
exit function
end if
end function
dim records
records=1
dim tries
tries=1
while  records < 10 and tries <= 20 
records=getRestaurants(tries)
tries=tries+1	
wend 


'now send mails
dim msg
tries=0
if flag=true then
while not frs.eof and tries < 10
	msg=msg & "<a href=http://www.mycityeats.com/web/restaurant.asp?restaurantid=" & frs(0) & "> "& frs(1) &" </a>"
	msg=msg & "<br>"
	frs.movenext
	tries=tries+1
wend
end if

HTML = "<!DOCTYPE HTML PUBLIC""-//IETF//DTD HTML//EN"">"
    HTML = HTML & "<html>"
    HTML = HTML & "<head>"
    HTML = HTML & "<title>hiWelcome, mycityeats.com</title>"
    HTML = HTML & "</head>"
    HTML = HTML & "<body>"
	HTML=HTML & "<b> Selected Restaurants for You By <a href=http://www.mycityeats.com>mycityeats.com</a></b><br>"
	HTML=HTML & msg
    HTML = HTML & "</body>"
    HTML = HTML & "</html>"
		if Request("issubmit")="1" then
			SendMail "Restaurants For You","nick@mycityeats.com","" & Request("email") & "",HTML
	end if
	
%>
<form action="popup1.asp" method="post" name="popfirst" id="popfirst" style="margin:0px">
<table width="80%"  border="0" align="center" vspace="0" cellpadding="0" cellspacing="0" >
<tr>
    <td colspan="2" align="center" valign="top"><img src="images/header_r1_c1.gif" align="top" vspace="0"></td>
  </tr>
  
  <tr>
  <tr>
  <td colspan="2"></td></tr>
  <td></td>
    <td  align="left">&nbsp;</td>
  </tr>
  <tr>
  <% 
  dim com
  if Request("issubmit")="1" then
  com="Mail Sent"
  else
  com=""
  end if
  %>
    <td align="center"><b><font color="#0000FF"></font></font></b></td>
    <td align="left"><b><font color="#0000FF"><%= com %></font></b></td>
  </tr>
  <tr>
    <td width="30%" align="center">&nbsp;</td>
    <td width="70%">      <input type="hidden" name="issubmit" id="issubmit" value="0"></td>
  </tr>
  <tr>
    <td align="center">Please Enter Your Email</td>
    <td><input type="text" name="email" id="email2"></td>
  </tr>
  <tr>
    <td align="center">Please Enter Your Zip Code </td>
    <td><input type="text" name="zip" id="zip"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value=" Submit " onClick="return Valid();"></td>
  </tr>
</table>
</form>


</body>
</html>
