<?php
error_reporting(0);
session_start();

//set custom variables
$company_name = "Dundee Depot Dogs";
$submit_to = "apicton@pilotwebsolutions.com";
$sendfrom = "applications@dundeedepotdogs.com";



///Domain Name/////
$Domain="http://".$_SERVER['HTTP_HOST'];


if($_POST['submitted']){
	//form submitted - actions here
	//echo"submitted";
	//$submittedform = $_POST['formid'];
	$sendto = $_POST['email'];
	
		/*
		$captcha = "Select captcha from formData where recordkey ='$submittedform'" ; 
		$rtcap=mysql_query($captcha);          
		while($ntcap=mysql_fetch_array($rtcap)){
			$captcha = $ntcap[captcha];
		}
		mysql_close;
		if($captcha==0){
			//echo"captcha not used";
		}
		*/
		if(!empty($_SESSION['captcha'])){	//captcha is included on this form session...
	
			/** Validate captcha */
			if (!empty($_REQUEST['captcha'])) {	//if captcha is submitted by guest...
			if (trim(strtolower($_REQUEST['captcha']))!=($_SESSION['captcha'])) {
				$captcha_message = "Please enter a valid captcha code.";
				$submit_error=1;
			} else {
				//captcha code ok
				$submit_error=0;
			}

			$request_captcha = htmlspecialchars($_REQUEST['captcha']);

				
				unset($_SESSION['captcha']);
			} else{	//captcha not submitted
				$captcha_message = "Please enter a valid captcha code.";
			echo '<h2>'.$captcha_message.'</h2>';
				$submit_error=1;
			}
		}
	if($submit_error==0){	//if no captcha errors, continue processing form
	//echo "no captcha errors";
	//pull values posted by guest
	$msg = "Info submitted:\n\n";
	if ($_POST) {
	$kv = array();
	foreach ($_POST as $key => $value) {
    $kv[] = "$key=$value";
	if(($key!=="sendto")&&($key!=="sendfrom")&&($key!=="submitted")&&($key!=="formid")&&($key!=="captcha")){
		$msg .= $key . " = " . $value . "\n\n";
	}
	if($key=="email"){$guest_email=$value;}
	//test if field is required and populated
	$query3 = "Select required from formData where formID = " . $submittedform . " AND caption = '" . $key ."'";
//echo "qry = " . $query3;
	$rt3=mysql_query($query3);          
	while($nt3=mysql_fetch_array($rt3)){
		if($nt3[required]==1){	//this field is required
			if(empty($value)){$submit_error=1;}
		}
	}
	mysql_close;
	//get form specific settings
	$query3 = "Select success_msg, to_email, from_email from formData where recordKey = " . $submittedform;
	//"<br>qry = " . $query3;
	$rt3=mysql_query($query3);          
	while($nt3=mysql_fetch_array($rt3)){
		$success_msg = $nt3[success_msg];
		$submit_to = $nt3[to_email];
		$send_from = $nt3[from_email];
	}
	mysql_close;
	
	//echo "session_captcha=" . $_SESSION['captcha'] . "<br>";
	
	//$query_string = join("<br>", $kv);
	}
	if($submit_error>0){
		echo '<a name="message"><h3><font color="red">Please make sure all required fields (marked with a red asterisk) are filled in and try again.</font></h3></a>';
		$anchor = "message";
	}else{
		$anchor = "success";
		//echo "ready to send";
		$ok=1;
		//all is ok, requirements are met...proceed to save data and send email
		//echo $msg;
		//save submitted data to database for backup
		//$query1="insert into submissions(ip_address, email, data) values ('".$_SERVER['REMOTE_ADDR']."', '".$guest_email."', '".$msg."')";
		//mysql_query($query1);
	
			//get website name
			/*$query = "Select title from storePrefs";
			$rt=mysql_query($query);          // query executed 
			echo mysql_error();                    // if any error is there that will be printed to the screen
			if (mysql_num_rows($rt) == 0) {
				//not found, do nothing
			} else {
				while($nt=mysql_fetch_array($rt)){
					//echo $nt[title];
					$seotitle = $nt[title];
				}
			}
			mysql_close;
			*/
	//send email to site admin 
		$to = $submit_to; 
		$subject = "$company_name Online Employment Application submission"; 
		$headers = 'From: '. $guest_email . "\r\n";
		$sent = mail($to, $subject, $msg, $headers) ; 
		
	//send email to guest 
		$to = $guest_email; 
		$subject = "$company_name - Thanks for your request."; 
		$headers = 'From: '. $sendfrom . "\r\n";
		$sent = mail($to, $subject, $msg, $headers) ; 
		if($sent) {
				$result=$success_msg; 
			} else {
				$result= "We're sorry, but there may have been an error processing your request. Please send us an email at <a href='mailto:".$sendfrom."'>" . $sendfrom . "</a> to confirm that we got your request."; 
			}
	
		echo '<a name="success"><h3>'.$result.'</h3></a>';
		
		
		}	//end of form submission process
	
	
	
	
	
	
	}	//end of post loop
	}	else {
		//echo "captcha errors";
	}//end of no captcha error loop

} else {
	echo "";
}
?>





<?php
	if($ok==1){
		echo'<h1>Thank You!
		<br />
		<p>We have received your application and will be in touch with you soon!</p></h1>';
	} else{
		echo "There was a problem submitting your application:<br> $error";	
	}
?>
<br />
<br />
<br />
<br />
<br />

<br />



