#!/usr/bin/perl

#print "Content-type: application/rss+xml\n\n";
print "Content-type: text/xml\n\n";

require "config.idx";

$long = $ENV{'QUERY_STRING'};

if ($long) {
	@pairs=split(/&/,$long);
	foreach $item(@pairs) {
		($name,$content)=split (/=/,$item,2);
		$content=~tr/+/ /;
		$content=~ s/%(..)/pack("c",hex($1))/ge;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$content; }
		else { $INPUT{$name} = $content; }
	}
}
else {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
		else { $INPUT{$name} = $value; }
	}
}

use DBI;
my $dsn = "DBI:mysql:database=$d_name;host=localhost";
my $dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;

print qq~<?xml version="1.0" encoding="iso-8859-1"?>
<Listings>~;
my $uasth = $dbh->prepare("SELECT Price,AddressNumber,AddressDirection,AddressStreet,City,Zip,County,ListingOffice1Name,AgentName,AgentPhone,AgentID,MLSNUM,Bedrooms,YearBuilt,TotalSqFt,FullBaths,HalfBaths,Acres,AgentID,Remarks,vow_address from listings_res3 WHERE AgentID = '600'");
$uasth->execute;

while (($Price,$AddressNumber,$AddressDirection,$AddressStreet,$City,$Zip,$County,$ListingOffice1Name,$AgentName,$AgentPhone,$AgentID,$MLSNUM,$Bedrooms,$YearBuilt,$TotalSqFt,$FullBaths,$HalfBaths,$Acres,$AgentID,$Remarks,$vow_address) = $uasth->fetchrow_array()) {

#### MAKE CHANGES
$showpicture = &image_format("full","$data_mlsnumber");
if ($vow_address eq "Y") { $vow_address = "Yes"; } else { $vow_address = "No"; }
$image_url = &image_format("full",$MLSNUM);
$Remarks =~ s/\'/&apos;/gi; $Remarks =~ s/\’/&apos;/gi; $Remarks =~ s/\"/&quot;/gi; $Remarks =~ s/\&/&amp;/gi; $Remarks =~ s/\>/&gt;/gi; $Remarks =~ s/\</&lt;/gi; $Remarks =~ s/\<//gi; $Remarks =~ s/\>//gi; $Remarks =~ s/\-//gi; $Remarks =~ s/\–//gi;
$Baths = ($FullBaths+$HalfBaths);

print qq~
<Listing>
<Location>
  <StreetAddress>$AddressNumber $AddressDirection $AddressStreet</StreetAddress> 
  <City>$City</City> 
  <State>IN</State> 
  <Zip>$Zip</Zip> 
  <DisplayAddress>$vow_address</DisplayAddress> 
  </Location>
<ListingDetails>
  <Status>Active</Status> 
  <Price>$Price</Price> 
  <ListingUrl>http://$MLSNUM.ORRHomes.com</ListingUrl>
</ListingDetails>
<BasicDetails>
  <Description><![CDATA[$Remarks]]></Description> 
  <Bedrooms>$Bedrooms</Bedrooms> 
  <Bathrooms>$Baths</Bathrooms> 
  <LivingArea>$TotalSqFt</LivingArea> 
  <LotSize>$Acres</LotSize> 
  <YearBuilt>$YearBuilt</YearBuilt> 
</BasicDetails>
<Pictures>
  <Picture>
    <PictureUrl>$image_url</PictureUrl>
  </Picture>
</Pictures>
<Agent>
  <FirstName>Patrick and Ryan</FirstName> 
  <LastName>Orr</LastName> 
  <EmailAddress>Info\@ORRHomes.com</EmailAddress> 
  <OfficeLineNumber>7652121111</OfficeLineNumber> 
  <MobilePhoneLineNumber>7657449500</MobilePhoneLineNumber> 
  <FaxLineNumber>7653811111</FaxLineNumber> 
</Agent>
</Listing>
~;
}
$dbh->disconnect;

print qq~</Listings>\n~;