#!/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 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) = $uasth->fetchrow_array()) {


	my $pasth = $dbh->prepare("select firstname,lastname,title,phone1name,phone1number,phone2name,phone2number,phone3name,phone3number,phone4name,phone4number,email,homepage,profile,image from agents where display = '1' AND mlsid = '$AgentID' ORDER BY agentid ASC LIMIT 0,1");
	$pasth->execute;
	while (($a_firstname,$a_lastname,$a_title,$a_phone1name,$a_phone1number,$a_phone2name,$a_phone2number,$a_phone3name,$a_phone3number,$a_phone4name,$a_phone4number,$a_email,$a_homepage,$a_profile,$a_image) = $pasth->fetchrow_array()) {


$image_url = &image_format("thumbnail",$MLSNUM);

$Baths = ($FullBaths+$HalfBaths);

$Remarks =~ s/\'//gi;
$Remarks =~ s/\’//gi;
$Remarks =~ s/\"//gi;
$Remarks =~ s/\&//gi;
$Remarks =~ s/\<//gi;
$Remarks =~ s/\>//gi;
$Remarks =~ s/\-//gi;
$Remarks =~ s/\–//gi;

if ($AgentID eq "122") {
$AgentName = "The ORR Home Selling Team";
$AgentPhone = "765-212-1111";
$setsiteurl = "www.ORRHomes.com";
$setsitename = "http://www.ORRHomes.com";
$setemail =  "Ryan\@ORRHomes.com";
}
else {
$setsiteurl = "www.MuncieMLS.com";
$setsitename = "http://www.MuncieMLS.com";
$setemail =  "Info\@EagleSold.com";
}

print qq~      
	<Listing>
      <Address>
	    <commons:preference-order>1</commons:preference-order>
	    <commons:address-preference-order>1</commons:address-preference-order>
	    <commons:FullStreetAddress>$AddressNumber $AddressDirection $AddressStreet</commons:FullStreetAddress>
	    <commons:City>$City</commons:City>
	    <commons:StateOrProvince>IN</commons:StateOrProvince>
	    <commons:PostalCode>$Zip</commons:PostalCode>
	  </Address>

	 </Listing>
~;
}
}

$dbh->disconnect;

print qq~</Listings>\n~;