<?php 

function cloudfw_contact_form($atts){
	
	extract(cloudfw_make_var(array(
		'to'	=> NULL,
	), $atts));

	if ( ! empty($to) )
			$to = base64_encode($to);
	
	$uID = 'contact-form-' . cloudfw_randomizer(4);

	$out = '
		
		<script>

			jQuery(document).ready(function(){				
				jQuery("#'.$uID.'").CloudSendMessage();
			});

		</script>

	';

	$out .= '

	<form id="'.$uID.'" class="contact_form" method="post" action="">

		<p><label>'.__('Name:','cloudfw').'</label><input type="text" value="" name="name" id="name"></p> 

		<p><label>'.__('Email:','cloudfw').'</label> <input type="text" value="" name="email" id="email"></p>';

	
	$out .= '<p><textarea rows="8" cols="45" name="message" id="message"></textarea></p>';
	
	if ( ! empty($to) )
		$out .= '<input type="hidden" name="to" value="'.$to.'" >';

	$out .= '

	<input type="submit" value="'.__('Send Message','cloudfw').'" class="contactsubmit" name="submit" autocomplete="off">
	<input type="hidden" name="wp_contact_nonce" id="wp_contact_nonce" value="'.wp_create_nonce('cloudfw-contact').'" >

	</form>';

	return $out;

}

/*
 *    Callback: Contact Form
 *
 *    @since 1.0
**/
function cloudfw_ajax_contact_form_callback() {

	# Form Variables:
	//$name, $email, $message;

	$name = $_POST['name'];
	$email = $_POST['email'];
	$message = $_POST['message'];
	$to = $_POST['to'];
	$message = $_POST['message'];
	$msgSuccess = $_POST['msgSuccess'];
	$nonce = $_POST['wp_contact_nonce'];


	# Call WP Error Class
	$errors = new WP_Error();
	$result = array();
	$errorsClasses = array();


	if ( ! wp_verify_nonce( $nonce, "cloudfw-contact") )
		$errors->add('nonce', __('Your session failed. Please try again.', 'cloudfw') );
			

	if ( empty($name) ) {
		$errors->add('name', __('Please insert your name', 'cloudfw') );
		$errorsClasses[] = 'name';
	}
	if ( empty($email) ) {
		$errors->add('email', __('Please insert your email address', 'cloudfw') );
		$errorsClasses[] = 'email';
	}
	if ( ! empty($email) && ! is_email($email) ) {
		$errors->add('email', __('Email address which you insert is not valid', 'cloudfw') );
		$errorsClasses[] = 'email';
	}
	if ( empty($message) ) {
		$errors->add('message', __('Please type some message', 'cloudfw') );
		$errorsClasses[] = 'message';
	}


	# Check For Error
	if ( $errors->get_error_code() ) {
		
		$result['success'] = false;
		$result['error'] = true;
		$result['message'] = $errors->get_error_messages();
		$result['eClasses'] = $errorsClasses;

	} else {
		
		$sitename = get_bloginfo('name');
		$siteurl =  get_bloginfo('siteurl');
		if ( ! empty($to) )
			$to = base64_decode($to);

		if ( ! is_email($to) )
			$to = get_option('admin_email');

		$subject = "$sitename's message from $name";
		$body = "Site: $sitename ($siteurl) \n\nName: $name \n\nEmail: $email \n\nMessages: $message";
		$headers = "From: $name <$email>\r\n";
		$headers .= "Reply-To: $email\r\n";
		
		
		if( wp_mail( $to, $subject, $body, $headers ) ){
			
			$result['success'] = true;
			$result['error'] = false;
			$result['message'] = init_boxes(array('title' => $msgSuccess ? $msgSuccess : __('Your message sent successfully!','cloudfw')), '', 'success');

		} else {
			$result['success'] = false;
			$result['error'] = true;
			$result['message'] = __('There was an error while sending your message. Please try again.','cloudfw');			
		}
		

	}

	echo htmlspecialchars(json_encode( $result ), ENT_NOQUOTES);
	exit;

}