<?php
ini_set('error_reporting', 0);
ini_set('display_errors',0);
$forceIP = $_REQUEST['fip'];
$yourIP = ($forceIP != "") ? $forceIP : $_SERVER['REMOTE_ADDR'];
$yourCityState = lookupCityState($yourIP);
$arrCityState = split(", ",$yourCityState);
$yourCity = $arrCityState[0];
$yourState = $arrCityState[1];
$yourCountry = $arrCityState[2];
$yourRegion = ( $yourCountry=="USA" ) ? $yourState : $yourCountry;

//if($yourCountry != "" && $yourCountry != "USA") { header("location:missingkids.js"); }

$arrSponsors = array();
	$arrSponsors[0] = array("XiCast", "http://xicast.com/?ref=amberalert");
	$arrSponsors[1] = array("Watchlist", "http://watchlist.xicast.com/?ref=amberalert");
	$arrSponsors[2] = array("Wholinked","http://wholinked.com/?ref=amberalert");
	$arrSponsors[3] = array("eCompendium", "http://ecompendium.us/?ref=amberalert");
	$arrSponsors[4] = array("SoCalCars","http://socalcars.com/?ref=amberalert");
	$arrSponsors[5] = array("BonApetito","http://bonapetito.net/?ref=amberalert");
	
	$curSponsorID = (rand()%5);
	$sponsorURL = $arrSponsors[$curSponsorID][1];
	$sponsorName = $arrSponsors[$curSponsorID][0];

	//override sponsor randomizer:
	$sponsorURL = "http://listlistener.com";
	$sponsorName = "ListListener.com";

	$referer = $_SERVER['HTTP_REFERER'];

// log referer
	$hostname_AmberAlert = "localhost";
	$database_AmberAlert = "amberalert";
	$username_AmberAlert = "root";
	$password_AmberAlert = "HdV1080i";
	$baAmber = mysqli_connect($hostname_AmberAlert,$username_AmberAlert,$password_AmberAlert,$database_AmberAlert) or trigger_error(mysql_error(),E_USER_ERROR);


	mysqli_select_db($baAmber,$database_AmberAlert);
	$fqry="Call UpdateReferer('".$referer."', 0)";
	
	$rsDisplay = mysqli_query($baAmber,$fqry) or die(mysql_error());
	while ($row_rsFeatured = mysqli_fetch_assoc($rsDisplay)) {}
	mysqli_close($baAmber); 


// loookup amber alert local to state

$postTitle = "";
$postImageURL = "";
$postDescription = "";
$postDate = "";
$postSiteURL = "";

	$msdb = odbc_connect('cubbins','sa','HdV1080i');

	$yourCat = yourCategory($yourState);

	$queryUS = "Select top 1 postid, posttitle, imageurl, importdate, url from posts where sourceid = 3 and imageurl is not null ORDER BY NEWID()";

	$queryState = "Select top 1 postid, posttitle, imageurl, importdate, url from posts where sourceid = 3 and imageurl is not null and categoryid=$yourCat ORDER BY NEWID()";
	
	$queryUK = "Select top 1 postid, posttitle, imageurl, importdate, url from posts where sourceid = 3 and imageurl is not null and categoryid=173 ORDER BY NEWID()";
	
	//if country is known
	if ($yourCountry != ""){
		//echo "Country not null (".$yourCountry.")<BR>"; 
		
		
		//is US?
		if ($yourCountry=="USA") {
		
		
			//echo "Country == USA (".$yourCountry.")<BR>";
		
			// is State known?
			if ($yourState != "") {
				
				//echo "State not null (".$yourState.")<BR>";
				
				$query = ($yourCat>0) ? $queryState : $queryUS;
			} else {
				
				//echo "State is null (".$yourState.")<BR>";
				$query = $queryUS;
			}
		} else {
			
			//echo "Country is NOT USA (".$yourCountry.")<BR>";
			$query = $queryUK;	
		}

	//else use US
	} else {
		
		//echo "Country IS null (".$yourCountry.")<BR>";
		
		$query = $queryUS;
	}
	
	//echo "Using ".$query."<BR>";
	
	$results = odbc_exec($msdb,$query);

	while ($row=odbc_fetch_array($results)) {

				$postTitle = $row['posttitle'];
				$postImageURL = $row['imageurl'];
				//$postDescription = $row['htmlcontent'];
				$postDate = $row['importdate'];
				
				$arrPostDate = split(" ",$postDate);
				$strpostDate = $arrPostDate[0];
				$postSiteURL = $row['url'];
				
				$arrTitle = split(":",$postTitle);
				$subtitle = $attTitle[1];


	}

	odbc_close($msdb);

	// error checking
	if ($postTitle =="" || $postImageURL == ""){
		
		$subject = "Error with AMBER Alert Widget (amber.js)";
		$body = "The following conditions failed for an AMBER Alert Widget:<BR>";
			$body .= "Location: ".$yourCity.", ".$yourState.", ".$yourCountry.", ".$yourRegion."<BR>";
			$body .= "IP: ".$yourIP."<BR>";
			$body .= "PostTitle or Image URL was null<BR>";
			$body .= "SQL Query:".$query;
		
		$to = "aowens@xicast.com";
	    $headers = "From: AMBER Alert Support <support@websiteamberalert.com>\r\n";
		$headers .= "Reply-To: AMBER Alert Support <support@websiteamberalert.com>\r\n";
		$headers .= "BCC: support@websiteamberalert.com\r\n";
		$headers .= "MIME-Version: 1.0;\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1;\r\n";
		$headers .= "Return-Path:  AMBER Alert Support <support@websiteamberalert.com>\r\n";
		$headers .= "Errors-To:  AMBER Alert Support <support@websiteamberalert.com>\r\n";
		ini_set('sendmail_from','support@websiteamberalert.com');
		mail($to,$subject,$body,$headers);
		
	}
	
	
	
	
?>
document.write('<style type="text/css">');
document.write('<!-- ');
document.write('.daastyle1 {	color: #FFFFFF;	font-weight: bold;	font-size: xx-small;	font-family: Geneva, Arial, Helvetica, sans-serif; background-color:#ea7129; padding:0;} ');
document.write('.daastyle2 {	color: #000000;	font-weight: bold;	font-size: xx-small;	font-family: Geneva, Arial, Helvetica, sans-serif; text-decoration:none; background-color:#eead4f; padding:0;} ');
document.write('.daastyle4 {font-family: Geneva, Arial, Helvetica, sans-serif; font-size: xx-small; padding:0;} ');
document.write('.daastyle6 {color: #FFFFFF; font-weight: normal; font-size:xx-small; padding:0;} ');
document.write('.daa {	border: 1px solid #333333; padding: 0px; margin: 0px;} ');
document.write('.daaxslink {	font-size: xx-small;	font-weight: bold;	color: #FFFFFF;	text-decoration: none;	font-family: Geneva, Arial, Helvetica, sans-serif; background-color:#eead4f; padding:0;} ');
document.write('.daaxxslink {	font-size: xx-small;	font-weight: normal;	color: #FFFFFF;	text-decoration: none;	font-family: Geneva, Arial, Helvetica, sans-serif; background-color:#ea7129; padding:0;} ');
document.write('.daaxxslink2 {	font-size: x-small;	font-weight: normal;	color: #FFFFFF;	text-decoration: none;	font-family: Geneva, Arial, Helvetica, sans-serif; background-color:#ea7129; padding:0;} ');
document.write('--> ');
document.write('</style> ');
document.write('<div style="padding: 0px; margin: 0px;border-style:none;"><table width="125" border="0" cellpadding="0" cellspacing="0" class="daa" style="padding: 0cm; margin: 0px; border-style:daa;"> ');
document.write('  <tr class="daatr"><td colspan="2" bgcolor="#ea7129" style="	border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #B34E11;padding: 0px; margin: 0px;"><div align="center" class="daastyle1"><a href="HTTP://websiteamberalert.com" class="daaxxslink2" style="color:#ffffff;padding: 0px; margin: 0px;" >M I S S I N G</a> </div></td></tr> ');
document.write('  <tr class="daatr"><td colspan="2" bgcolor="#eead4f"><div align="center" class="daastyle2"><B>Have you seen <BR>me in <?=$yourRegion ?>?</B></div></td></tr> ');
document.write('  <tr class="daatr"><td colspan="2" bgcolor="#eead4f" align="center" style="	border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #B34E11;padding: 0px; margin: 0px;"><a href="<?= $postSiteURL ?>" target="_New" class="daastyle2"  style="color:#000000" ><img src="<?= $postImageURL ?>" width="64" border="0" /><BR>Read More &raquo;</a></td></tr> ');
document.write('  <tr class="daatr"><td colspan="2" bgcolor="#ea7129"><div align="center" class="daastyle1" style="padding: 0px; margin: 0px;"><a href="<?= $postSiteURL ?>" target="_New" class="daaxxslink" style="color:#ffffff; padding: 0px; margin: 0px;" ><?= str_replace(": ","<BR>",$postTitle) ?></a></div></td></tr> ');
document.write('  <tr class="daatr"><td colspan="2" bgcolor="#ea7129" style="	border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #B34E11;"><div align="center" class="daastyle1">Missing Since:<BR> <?= $strpostDate ?></div></td></tr> ');
document.write('  <tr class="daatr"><td bgcolor="#eead4f" colspan="2" align="center" style="	border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #B34E11;"><a href="http://www.missingkids.com/missingkids/servlet/PageServlet?LanguageCountry=en_US&PageId=992" class="daastyle2" target="_New" style="color:#000000" >REPORT SIGHTING&raquo;</a></td></tr> ');
document.write('  <tr class="daatr"><td bgcolor="#ea7129" colspan="2" align="center" style="	border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #B34E11;"><a href="http://desktopamberalert.com" class="daaxxslink" style="color:#ffffff" target="_New">You Can Help &raquo;</a></td></tr> ');
document.write('  <tr class="daatr"><td bgcolor="#ea7129" colspan="2" align="center"><a href="<?= $sponsorURL ?>" class="daaxxslink" target="_New" style="color:#ffffff" >Sponsored By <?= $sponsorName ?></a></td></tr> ');
document.write('</table></div> ');
document.write('<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">');
document.write('</script>');
document.write('<script type="text/javascript">');
document.write('_uacct = "UA-126995-38";');
document.write('urchinTracker();');
document.write('</script>');

<?


function lookupCityState($yourIP){
// This code demonstrates how to lookup the country, region, city,
// postal code, latitude, and longitude by IP Address.
// It is designed to work with GeoIP/GeoLite City

// Note that you must download the New Format of GeoIP City (GEO-133).
// The old format (GEO-132) will not work.

include("geoipcity.inc");
include("geoipregionvars.php");

// uncomment for Shared Memory support
// geoip_load_shared_mem("/usr/local/share/GeoIP/GeoIPCity.dat");
// $gi = geoip_open("/usr/local/share/GeoIP/GeoIPCity.dat",GEOIP_SHARED_MEMORY);

$gi = geoip_open("/scripts/GeoIP/Data/GeoIPCity.dat",GEOIP_MEMORY_CACHE);
// $gi = geoip_open("/usr/local/share/GeoIP/GeoIPCity.dat",GEOIP_STANDARD);

$record = geoip_record_by_addr($gi,$yourIP);

/*
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n";
print $record->city . "\n";
print $record->postal_code . "\n";
print $record->latitude . "\n";
print $record->longitude . "\n";
print $record->dma_code . "\n";
print $record->area_code . "\n";
*/

$strResult =  $record->city.", ".$record->region.", ".$record->country_code3;
geoip_close($gi);
return $strResult;
}

function RandomSponsor(){



}

function yourCategory($yourState){
	$arrState = array();

	$arrState['AL']=116;
	$arrState['AK']=114;
	$arrState['AZ']=118;
	$arrState['AR']=117;
	$arrState['CA']=119;
	$arrState['CO']=120;
	$arrState['CT']=121;
	$arrState['DE']=123;
	$arrState['FL']=124;
	$arrState['GA']=125;
	$arrState['HI']=126;
	$arrState['ID']=128;
	$arrState['IL']=129;
	$arrState['IN']=130;
	$arrState['IA']=127;
	$arrState['KS']=131;
	$arrState['KY']=132;
	$arrState['LA']=133;
	$arrState['ME']=134;
	$arrState['MD']=135;
	$arrState['MA']=136;
	$arrState['MI']=137;
	$arrState['MN']=138;
	$arrState['MS']=139;
	$arrState['MO']=140;
	$arrState['MT']=141;
	$arrState['NE']=142;
	$arrState['NV']=143;
	$arrState['NH']=144;
	$arrState['NJ']=145;
	$arrState['NM']=146;
	$arrState['NY']=147;
	$arrState['NC']=148;
	$arrState['ND']=149;
	$arrState['OH']=150;
	$arrState['OK']=151;
	$arrState['OR']=152;
	$arrState['PA']=153;
	$arrState['RI']=154;
	$arrState['SC']=155;
	$arrState['SD']=156;
	$arrState['TN']=157;
	$arrState['TX']=158;
	$arrState['UT']=159;
	$arrState['VT']=160;
	$arrState['VA']=161;
	$arrState['WA']=162;
	$arrState['DC']=122;
	$arrState['WV']=164;
	$arrState['WI']=163;
	$arrState['WY']=165;

	return $arrState[$yourState];
}
?>
