<?php
/**
 * @package Youtech Contact
 * @version 2.5
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @copyright (c) 2012 YouTech Company. All Rights Reserved.
 * @author YouTech Company http://www.smartaddons.com
 * 
 */
defined('_JEXEC') or die;

if ($pretext != ""){
?>
<div class="yt_introtext"><?php echo $pretex; ?></div>
<br />
<br />
<?php } ?>
<?php
	$ytcinstance = time();
	$fname = "yt_contact_" . $ytcinstance;
?>
<div class="yt_contact_holder">
<form name="yt_contact" method="post" action="" id="<?php echo $fname; ?>">
	<input class="inputbox namebox" id="namebox_<?php echo $ytcinstance; ?>" style="font-size:100%; font-family:arial; width:<?php echo $width; ?>" type="text" value="<?php echo 'Email ';//$nametext; ?>" name="name" />
	<input class="inputbox emailbox" id="emailbox_<?php echo $ytcinstance; ?>" style="font-size:100%; font-family:arial; width:<?php echo $width; ?>" type="text" value="<?php echo $emailtext; ?>" name="email" /><br />
	<input class="inputbox subjectbox" id="subjectbox_<?php echo $ytcinstance; ?>" style="font-size:100%; font-family:arial; width:<?php echo $width; ?>" type="text" value="<?php echo $subjecttext; ?>" name="subject" /><br />
	<textarea id="messagebox_<?php echo $ytcinstance; ?>" rows="" cols="" class="inputbox textarea" style="font-size:100%; font-family:arial; overflow:auto;height:<?php echo $height; ?>" name="message"><?php echo $bodytext; ?></textarea><br />
	<input class="inputbox spambox" id="spambox_<?php echo $ytcinstance; ?>" style="font-weight:bold; font-size:100%; font-family:arial; width:<?php echo $width; ?>" type="text" value="<?php echo $random_number; ?>" name="verif_box" />
	<input id="email_address_<?php echo $ytcinstance; ?>" type="hidden" value="" name="email_address" />
	<input class="button" type="button" value="<?php echo $sendtext; ?>" name="sndcontact"/>
</form>
</div>
<?php
	// define contact state
	setcookie("ytc_contact_state",(md5($random_number).'youtech'));
	JHTML::_('behavior.mootools');
	ob_start();
?>
	//<![CDATA[
	window.addEvent('domready', function(){
		var $currentForm = $('<?php echo $fname; ?>');
		if ($currentForm['name']){
			$($currentForm['name']).addEvent('click', ytc_clearname);
			$($currentForm['name']).addEvent('focus', ytc_clearname);
		}
		if ($currentForm['email']){
			$($currentForm['email']).addEvent('click', ytc_clearemail);
			$($currentForm['email']).addEvent('focus', ytc_clearemail);
		}
		
		if ($currentForm['subject']){
			$($currentForm['subject']).addEvent('click', ytc_clearsubject);
			$($currentForm['subject']).addEvent('focus', ytc_clearsubject);
		}		
		if ($currentForm['message']){
			$($currentForm['message']).addEvent('click', ytc_clearbody);
			$($currentForm['message']).addEvent('focus', ytc_clearbody);
		}
		
		if ($currentForm['verif_box']){
			$($currentForm['verif_box']).addEvent('click', ytc_clearspam);
			$($currentForm['verif_box']).addEvent('focus', ytc_clearspam);
		}
		
		if ($currentForm['sndcontact']){
			$($currentForm['sndcontact']).addEvent('click', ytc_submit);
		}
		
		var ytc_spam_text =  "<?php empty($spamtext) ? $spamtext : $spamtext . ' ' . $spamtext; ?>";
		
		function ytc_clearbody() {
			if ($currentForm['message'].getValue() == "<?php echo $bodytext; ?>") {
				$currentForm['message'].value = "";
			}
			if ($currentForm['name'].getValue().length < 1) {
				$currentForm['name'].value = "<?php echo $nametext; ?>";
			}
			if ($currentForm['email'].getValue().length < 1) {
				$currentForm['email'].value = "<?php echo $emailtext; ?>";
			}
			if ($currentForm['subject'].getValue().length < 1) {
				$currentForm['subject'].value = "<?php echo $subjecttext; ?>";
			}
			if ($currentForm['verif_box'].getValue().length < 1) {
				$currentForm['verif_box'].value = ytc_spam_text;
			}
			return false;
		}
		
		function ytc_clearname() {
			if ($currentForm['name'].getValue() == "<?php echo $nametext; ?>") {
				$currentForm['name'].value = "";
			}
			if ($currentForm['message'].getValue().length < 1) {
				$currentForm['message'].value = "<?php echo $bodytext; ?>";
			}
			if ($currentForm['email'].getValue().length < 1) {
				$currentForm['email'].value = "<?php echo $emailtext; ?>";
			}
			if ($currentForm['subject'].getValue().length < 1) {
				$currentForm['subject'].value = "<?php echo $subjecttext; ?>";
			}
			if ($currentForm['verif_box'].getValue().length < 1) {
				$currentForm['verif_box'].value = ytc_spam_text;
			}
			return false;
		}
		
		function ytc_clearemail() {
			if ($currentForm['email'].getValue() == "<?php echo $emailtext; ?>") {
				$currentForm['email'].value = "";
			}
			if ($currentForm['name'].getValue().length < 1) {
				$currentForm['name'].value = "<?php echo $nametext; ?>";
			}
			if ($currentForm['message'].getValue().length < 1) {
				$currentForm['message'].value = "<?php echo $bodytext; ?>";
			}
			if ($currentForm['subject'].getValue().length < 1) {
				$currentForm['subject'].value = "<?php echo $subjecttext; ?>";
			}
			if ($currentForm['verif_box'].getValue().length < 1) {
				$currentForm['verif_box'].value = ytc_spam_text;
			}
			return false;
		}
		
		function ytc_clearsubject() {
			if ($currentForm['subject'].getValue() == "<?php echo $subjecttext; ?>") {
				$currentForm['subject'].value = "";
			}
			if ($currentForm['name'].getValue().length < 1) {
				$currentForm['name'].value = "<?php echo $nametext; ?>";
			}
			if ($currentForm['message'].getValue().length < 1) {
				$currentForm['message'].value = "<?php echo $bodytext; ?>";
			}
			if ($currentForm['email'].getValue().length < 1) {
				$currentForm['email'].value = "<?php echo $emailtext; ?>";
			}
			if ($currentForm['verif_box'].getValue().length < 1) {
				$currentForm['verif_box'].value = ytc_spam_text;
			}
			return false;
		}
		
		function ytc_clearspam() {
			if ($currentForm['verif_box'].getValue() == ytc_spam_text) {
				$currentForm['verif_box'].value = "";
			}
			if ($currentForm['name'].getValue().length < 1) {
				$currentForm['name'].value = "<?php echo $nametext; ?>";
			}
			if ($currentForm['message'].getValue().length < 1) {
				$currentForm['message'].value = "<?php echo $bodytext; ?>";
			}
			if ($currentForm['email'].getValue().length < 1) {
				$currentForm['email'].value = "<?php echo $emailtext; ?>";
			}
			if ($currentForm['subject'].getValue().length < 1) {
				$currentForm['subject'].value = "<?php echo $subjecttext; ?>";
			}
			return false;
		}
		
		function ytc_isValidEmail(email_str) {
			var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			if (emailPattern.test(email_str)) {
				alert('<?php echo $thankyou; ?>');
				$currentForm.submit();
			} else {
				alert('<?php echo $emailerror; ?>');
			}
		}
		
		function ytc_submit() {
		
			if ($currentForm['verif_box'].getValue() == ytc_spam_text
					|| $currentForm['subject'].getValue() == "<?php echo $subjecttext; ?>"
					|| $currentForm['name'].getValue() == "<?php echo $nametext; ?>"
					|| $currentForm['email'].getValue() == "<?php echo $emailtext; ?>"
					|| $currentForm['message'].getValue() == "<?php echo $bodytext; ?>") {
				console.log($currentForm['verif_box'].getValue());	
				console.log(ytc_spam_text);
				console.log($currentForm['subject'].getValue());	
				console.log("<?php echo $subjecttext; ?>");	
				console.log($currentForm['name'].getValue());	
				console.log("<?php echo $nametext; ?>");	
				console.log($currentForm['email'].getValue());	
				console.log("<?php echo $emailtext; ?>");	
				console.log($currentForm['message'].getValue());	
				console.log("<?php echo $bodytext; ?>");
				console.log('<?php echo $notcomplete; ?>');
				return false;
			}
		
			if ($currentForm['verif_box'].getValue() != "<?php echo $random_number; ?>") {
				alert('<?php echo $spamerror; ?>');
				return false;
			}
		
			var contact_message = $currentForm['message'].getValue();
				
			if (contact_message.indexOf("<?php echo $ex1 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex2 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex3 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex4 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex4 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex5 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex6 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex7 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex8 ?>") >= 0) {
				return false;
			}
		
			if (contact_message.indexOf("<?php echo $ex9 ?>") >= 0) {
				return false;
			} else {
				$currentForm['email_address'].value = "<?php echo $email_address ?>";
				var email_str = $currentForm['email'].getValue();
				ytc_isValidEmail(email_str);
			}
		}
	});	
	// ]]>
<?php 
	$js_script = ob_get_contents();
	ob_end_clean();
	$document =& JFactory::getDocument();
	$document->addScriptDeclaration($js_script);
?>
