<?php
//Contact//
// $Id: contact.php,v 1.20 2003/12/08 19:38:52 rcastley Exp $
/**
* Content code
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.20 $
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

// load the html drawing class
require_once( $mainframe->getPath( 'front_html' ) );

if (!isset( $op )) {
	$op = "";
}

//Load Vars
$con_id = intval( mosGetParam( $_REQUEST ,'con_id', 0 ) );
$contact_id = intval( mosGetParam( $_REQUEST ,'contact_id', -1 ) );

switch($op) {
	case "sendmail":
	sendmail( $con_id, $option );
	break;
	default:
	contactpage( $option, $contact_id, $database, $mosConfig_sitename, $Itemid);
	break;
}

function contactpage( $option, $contact_id, $database, $_sitename, $Itemid) {
	$database->setQuery( "SELECT id, name, con_position FROM #__contact_details"
	. "\nWHERE published='1'"
	. "\nORDER BY default_con DESC, ordering ASC"
	);
	$res = $database->query();
	$numrows = $database->getNumRows($res);
	if ($numrows != 0){
		$con = $database->loadObjectList();
		$i=0;
		foreach ($con as $condetail) {
			$contact_id_sel[$i] = $condetail->id;
			$contact_name_sel[$i] = $condetail->name;
			$contact_pos_sel[$i] = $condetail->con_position;
			$i++;
		}
		if ($contact_id==-1){$contact_id=$contact_id_sel[0];}
		
		$database->setQuery( "SELECT * FROM #__contact_details"
		."\n WHERE id='$contact_id'" );
		$rows = $database->loadObjectList();
		foreach ($rows as $row) {
			$id = $row->id;
			$con_name = $row->name;
			$con_position = $row->con_position;
			$address = $row->address;
			$suburb = $row->suburb;
			$state = $row->state;
			$country =$row->country;
			$postcode =$row->postcode;
			$telephone = $row->telephone;
			$fax = $row->fax;
			$misc = $row->misc;
			$image = $row->image;
			$imagepos = $row->imagepos;
			$ordering = $row->ordering;
			$hasEmail = trim( $row->email_to ) ? true : false;
		}
		contact_html::contactpage($contact_id_sel, $contact_name_sel, $contact_pos_sel, $id, $con_name, $con_position, $address, $suburb, $state, $country, $postcode, $telephone, $fax, $misc, $image, $imagepos, $hasEmail, $_sitename, $Itemid, $ordering);
	}
}

function sendmail( $con_id, $option ) {
	global $database, $Itemid;
	global $mosConfig_sitename;
	
	$database->setQuery( "SELECT email_to FROM #__contact_details WHERE id='$con_id'" );
	$email_to = $database->loadResult();
	
	$email = trim( mosGetParam( $_POST, 'email', '' ) );
	$text = trim( mosGetParam( $_POST, 'text', '' ) );
	$name = trim( mosGetParam( $_POST, 'name', '' ) );
	
	if (!$email || !$text || (is_email($email)==false)){
		echo "<script>alert (\""._CONTACT_FORM_NC."\"); window.history.go(-1);</script>";
		exit(0);
	}
	$text = ""._ENQUIRY_TEXT." ".$name. " (". $email . ")" . "\r \n".stripslashes($text);
	$subject = $mosConfig_sitename." "._ENQUIRY;
	$headers .= "From: ".$name." <".$email.">\r\n";
	$headers .= "Reply-To: <".$email.">\r\n";
	$headers .= "X-Priority: 3\r\n";
	$headers .= "X-MSMail-Priority: Low\r\n";
	$headers .= "X-Mailer: Mambo Open Source 4.5\r\n";
	
		@mail($email_to, $subject, $text, $headers);?>
		<script> alert("<?php echo _THANK_MESSAGE; ?>"); document.location.href='<?php echo sefRelToAbs("index.php?option=$option&amp;Itemid=$Itemid"); ?>';</script>
	<?php
}

function is_email($email){
	$rBool=false;
	
	if(preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $email)){
		$rBool=true;
	}
	return $rBool;
}
?>
