<?PHP
//================================================================================
// PLEASE DO NOT REMOVE THIS HEADER!!!
//
// COPYRIGHT NOTICE
// This script is licensed under the GPL
// 
// Copyright 2007-2008 Alias 454 Studios and Brandon Keep (c) All rights reserved.
// Created 11/18/2007   
// Brandon Keep,    http://www.openautoclassifieds.com
//                  http://www.alias454studios.com/scripts/
//
// Last Modified 04-12-2008 by
// Brandon Keep,    http://alias454studios.com
//================================================================================
// This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//================================================================================

require_once './Includes/configs/functions.php';
require_once './Includes/libs/Smarty.class.php';
include_once "./Includes/language/$site_lang.php";

$smarty = new Smarty;

$smarty->assign("manufacturerslist",getSearchList("make"));
$smarty->assign("modelslist",getSearchList("model"));
$smarty->assign("typeslist",getSearchList("vehicle_type"));

//Check if single user mode 1 or multi-user mode 0
if ($site_mode == 0) {
	$smarty->assign("site_mode",1);
}
//The user is already logged in.
if (isset($_SESSION["logged_in"])){
	$smarty->assign("logged_in",1);
	//check privs to find out if user 
	//is a seller, admin or user
	$status = checkPrivs();
	$smarty->assign("$status",1);
}

if ($referers)
	checkReferer($referers);

if (!empty($_GET["seller"])) {
	$smarty->assign('seller',$_GET["seller"]);
}
if (!empty($_GET["record"])) {
	$smarty->assign("record",$_GET["record"]);
}

//get our data from the post procedure if exists
if (isset($_POST["submit"])) {
	//checkCaptcha();
	$res = checkSubmitForm($_POST["from_email"],$_POST["from_name"],$_POST["subject"],$_POST["msg"],$_POST["captcha"],$_SESSION["security_code"]);
	
	// Registration was successful
	if ($res == 99) {
		$smarty->assign("success",CONTACT_SUCCESS);
		unset($_SESSION["security_code"]);
		
		if (isset($_POST["member"]))
			$member = CONTACT_MEMBER;
		if (isset($_POST["news"]))
			$news = CONTACT_NEWSLETTER;
			
		if (isset($_POST["from_name"]))
			$from_name = $_POST["from_name"];				
		if (isset($_POST["company"]))
			$company = $_POST["company"];
		if (isset($_POST["from_email"]))
			$from_email = $_POST["from_email"];
		if (isset($_POST["phone"]))
			$phone = $_POST["phone"];
		if (isset($_POST["msg"]))
			$msg = $_POST["msg"];
		if (isset($_POST["subject"]))
			$subject = $_POST["subject"];

		if (isset($_POST["seller"])) {
			$seller = (int) $_POST["seller"];
			$to_email = getSellerEmail($_POST["seller"]);

			if (!empty($_POST["listing"]) && is_numeric($_POST["listing"])) {
				$listing = (int) $_POST["listing"];
				$records_detail = getListing($listing,"","","");
				$year = $records_detail[0]["year"];
				$make = $records_detail[0]["make"];
				$model = $records_detail[0]["model"];
			}

			if (isset($to_email)) {
				//build email to be sent to seller from lang file
				$body = eregi_replace("%LISTINGID%","$listing",CONTACT_SELLER_BODY);
				//contact info from sender
				$body = eregi_replace("%NAME%","$from_name", $body);
				$body = eregi_replace("%COMPANY%","$company", $body);
				$body = eregi_replace("%FROM_EMAIL%","$from_email", $body);
				$body = eregi_replace("%PHONE%","$phone", $body);
				//vehicle info
				$body = eregi_replace("%YEAR%","$year", $body);
				$body = eregi_replace("%MAKE%","$make", $body);
				$body = eregi_replace("%MODEL%","$model", $body);
				//additional message
				$body = eregi_replace("%MSG%","$msg", $body);
				//build subject
				$tmp_subject = eregi_replace("%NAME%","$from_name",CONTACT_SELLER_SUBJECT);
				$subject = eregi_replace("%ADDITIONAL%","$subject", $tmp_subject);

				sendEmail($to_email,$subject,$body,$from_name,$from_email);
			
			} else {
				$smarty->assign("error_message",INVALID_SELLER); 
			}
		} else {
			$to_email = $admin_email;
			
			//build email to be sent to site admin from lang file
			$body = eregi_replace("%NAME%","$from_name",CONTACT_US_BODY);
			$body = eregi_replace("%COMPANY%","$company", $body);
			$body = eregi_replace("%FROM_EMAIL%","$from_email", $body);
			$body = eregi_replace("%PHONE%","$phone", $body);
			$body = eregi_replace("%MSG%","$msg", $body);
			$body = eregi_replace("%MEMBER%","$member", $body);
			$body = eregi_replace("%NEWS%","$news", $body);
			$subject = eregi_replace("%ADDITIONAL%","$subject",CONTACT_US_SUBJECT);
			
			sendEmail($to_email,$subject,$body,$from_name,$from_email);
		}
	} else {
		unset($_SESSION["security_code"]);

		//keep users data in the form for resubmission
		$smarty->assign("formfromname",$_POST["from_name"]);
		$smarty->assign("formcompany",$_POST["company"]); 
		$smarty->assign("formphone",$_POST["phone"]); 
		$smarty->assign("formfromemail",$_POST["from_email"]);
		$smarty->assign("formsubject",$_POST["subject"]);
		$smarty->assign("formmsg",$_POST["msg"]);

		//Errors during form submission	
		if ($res == 1) {
			$smarty->assign("error_message",NOT_VALID_EMAIL); 
			$smarty->assign("from_mail_error",1); 
		}
		if ($res == 2) {
			$smarty->assign("error_message",EMPTY_NAME); 
			$smarty->assign("from_user_error",1); 
		} 
		if ($res == 3){
			$smarty->assign("error_message",EMPTY_SUBJECT); 
			$smarty->assign("subject_error",1); 
		} 
		if ($res == 4) {
			$smarty->assign("error_message",EMPTY_MSG); 
			$smarty->assign("msg_error",1); 
		}
		if ($res == 5) {
			$smarty->assign("error_message",SECURITY_CODE_MISMATCH); 
			$smarty->assign("cap_error",1); 
		}
		if ($res == 6) {
			$smarty->assign("error_message",EMPTY_SECURITY_CODE); 
			$smarty->assign("cap_error",1); 
		}
	}
}

$smarty->display('contact.tpl');
?>