<?php
/*
 * This file is part of Web Pro Manager.
 * 
 * Web Pro Manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Web Pro Manager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Web Pro Manager.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @package WebProManager
*/

require("manage/inc/stop_cache.php");
// connect to mysql
require("inc/dbconnect.php");
require("inc/config.php");


/* ***** No need to configure anything in this form. It is configured by the user during component editing. ***** */


// Get passed-in vars (from component settings)
$nCompID = (isset($_GET['cid']) ? $_GET['cid'] : NULL);
if(!$nCompID){ $nCompID = (isset($_POST['cid']) ? $_POST['cid'] : NULL); }

// Get content from component
if($nCompID){
	$nContentARRAY = nGetContentARRAY($nCompID,$_SESSION["nSFLang"]);
	if(isset($nContentARRAY[2]["nText"])){ $nEmailSubject = $nContentARRAY[2]["nText"]; }
	if(isset($nContentARRAY[1]["nTextarea"])){ $nThankYouMessage = $nContentARRAY[1]["nTextarea"]; }
	if(isset($nContentARRAY[3]["nText"])){ $nEmailTo = $nContentARRAY[3]["nText"]; }
	if(isset($nContentARRAY[4]["nText"])){ $nMailHost = $nContentARRAY[4]["nText"]; }
	if(isset($nContentARRAY[5]["nText"])){ $nMailUsername = $nContentARRAY[5]["nText"]; }
	if(isset($nContentARRAY[6]["nText"])){ $nMailPassword = $nContentARRAY[6]["nText"]; }
	if(isset($nContentARRAY[7]["nText"])){ $nUseSendMail = $nContentARRAY[7]["nText"]; } // 1 = No, 2 = Yes
	if($nUseSendMail==1){ $nUseSendMail = false; } else { $nUseSendMail = true; }
	// echo "<pre style=\"border:1px solid #555; overflow:auto; height:200px; width:100%;\">"; print_r($nContentARRAY); echo "</pre>";
} else {
	exit("");
}

// Get passed-in vars (from form visitor)
$nEmail = (isset($_POST['nEmail']) ? $_POST['nEmail'] : NULL);
$nName = (isset($_POST['nName']) ? $_POST['nName'] : NULL);
?>
<?php
// AJAX Specific vars
$nPassThru = "";
$nDivToUpdate = "ajaxContact";
$nPageInDiv = "contact_form.php";

include("inc/form_class.php");

$nFormName = "ContactForm";
$nForm1 = new Form();
$nForm1->DEBUG = false;
$nForm1->Name = $nFormName;
$nForm1->Action = $_SERVER['PHP_SELF'];
$nForm1->DontProcess = ""; //(multiple values accepted, comma-delimited)
$nForm1->SubmitText = " Send Message ";

// AJAX settings
$nForm1->SubmitIsJS = true;
$nForm1->SubmitToJS = "return submitForm('$nDivToUpdate','$nPageInDiv$nPassThru','$nFormName');return false;";

// Form styling
$nForm1->CleanMarkup = true;
$nForm1->SubmitClass = "button";
$nForm1->FieldTemplate = "[#Label#]<div class=\"input\" style=\"[#ItemStyle#]\">[#BeforeField#][#Field#][#AfterField#]<span class=\"info\">[#Caption#]</span></div>\n";
$nForm1->GroupTemplate = "<div id=\"[#ID#]\" style=\"[#Style#]\" class=\"[#Class#]\">\n[#Name#][#FieldCode#]\n</div>\n";
$nForm1->ButtonTemplate = "[#Buttons#]\n";

// Email settings
$nForm1->Action_email = $nEmailTo; //(multiple values accepted, comma-delimited)
$nForm1->Action_email_bodyFile = "inc/bodyfile.html";
if($nMailHost){ $nForm1->MailHost = $nMailHost; }
if($nMailUsername){ $nForm1->MailUser = $nMailUsername; }
if($nMailPassword){ $nForm1->MailPass = $nMailPassword; }
if($nUseSendMail){ $nForm1->MailUseSendMail = true; }
$nForm1->MailFrom = $nEmail;
$nForm1->EmailFromName = $nName;
$nForm1->EmailSubject = $nEmailSubject;

// Create groups
$nFormGroup1 = new FormGroup();
$nFormGroup1->Style = "margin-top:20px;";

// Create fields
$nFormField = new FormField("hidden","cid","");
$nFormField->Value = "$nCompID";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nName","Full name:");
$nFormField->RowSize = "Half";
$nFormField->RowStyle = "clear:both;float:none;";
$nFormField->Style = "width:90%;";
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nEmail","Email Address:");
$nFormField->RowSize = "Half";
$nFormField->RowStyle = "clear:both;float:none;";
$nFormField->Style = "width:90%;";
$nFormField = $nForm1->AddValidation("email",$nFormField);
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nPhone","Phone:");
$nFormField->RowSize = "Half";
$nFormField->RowStyle = "clear:both;float:none;";
$nFormField->Style = "width:90%;";
$nFormField = $nForm1->AddValidation("phone",$nFormField);
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("textarea","nComments","Your comments or questions:");
$nFormField->RowSize = "Half";
$nFormField->RowStyle = "clear:both;float:none;";
$nFormField->Style = "width:90%;height:120px;";
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormGroup1->AddField($nFormField);

// Finalize and display form
$nForm1->AddGroup($nFormGroup1);
$nFormToDisplay = $nForm1->GetForm();

// AJAX redirect if form submitted w/o error, otherwise display form (again, if necessary)
if($nForm1->HasProcessed){
?>
<div align="center" style="border:1px solid #CDCDCD; padding:10px; margin-top:20px;">
	<?php echo $nThankYouMessage;?>
</div>
<?php
} else {
	echo $nFormToDisplay;
}
?>
<?php
function nGetContentARRAY($nCompID,$nLangCode=""){
	global $DBName,$Link,$nDefaultLang;
	$nContentARRAY = array();
	if(!$nCompID){ return $nContentARRAY; }
	if(!$nLangCode){ $nLangCode = $nDefaultLang; }
	$nContentID = 1;
	$Query = "SELECT nText,nTextarea FROM nTranslate WHERE nItemID='$nCompID' AND nLang='$nLangCode' AND nType='comp' ORDER BY nOrderID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while($Row = mysql_fetch_array($Result)) {
		$nText = $Row["nText"];
		$nTextarea = $Row["nTextarea"];
		$nContentARRAY[$nContentID] = array("nText"=>"$nText","nTextarea"=>"$nTextarea");
		$nContentID++;
	}
	return $nContentARRAY;
}
?>
<?php
require("inc/db_close.php");
?>

