// WYSIWYG and Contact Form
$('document').ready(function(){
	// Convert textarea to WYSIWYG editor.
	$('.wysiwyg').wysiwyg(
	{
		css : { fontSize : '10pt', color : '#000000', fontFamily : 'arial'  },

		controls: {
			separator00: {
				visible: true
			},
			insertOrderedList: {
				visible: true
			},
			insertUnorderedList: {
				visible: true
			},
			undo: {
				visible: true
			},
			redo: {
				visible: true
			},
			underline: {
				visible: true
			}
		}
	});

// Form Validation 
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false;}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{if (validate_required(author,"Name must be filled out!")==false)
  {author.focus();return false;}}

with (thisform)
{if (validate_required(email,"Email must be filled out!")==false)
  {email.focus();return false;}}

with (thisform)
{if (validate_required(comment,"Got nothing to say!")==false)
  {comment.focus();return false;}}
}
	
	
	
	// Validate User Comments	
	$('#author').blur(function(){
		if (this.value == "") {
			$('.authorx').fadeIn(200);
		}
	})
	$('#author').focus(function(){
		$('.authorx').fadeOut(200);
	})


	$('#url').blur(function(){
		if (this.value == "") {
			$('.urlx').fadeIn(200);
		}
	})
	$('#url').focus(function(){
		$('.urlx').fadeOut(200);
	})
	
	$('#sitename').blur(function(){
		if (this.value == "") {
			$('.sitenamex').fadeIn(200);
		}
	})
	$('#sitename').focus(function(){
		$('.sitenamex').fadeOut(200);
	})
	
	
	$('#cat').blur(function(){
		if (this.value == "") {
			$('.catx').fadeIn(200);
		}
	})
	$('#cat').focus(function(){
		$('.catx').fadeOut(200);
	})



	// Validate Email
	$('#email').blur(function(){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email = document.getElementById('email');
		if (this.value == "") {
			$('.emailx').fadeIn(200);
			$('.emailvx').fadeOut(200);	
		} else if(!filter.test(email.value)) {
			$('.emailvx').fadeIn(200);
		}else {
			$('.emailvx').fadeOut(200);			
		}
	})
	$('#email').focus(function(){
		$('.emailx').fadeOut(200);
	})

	
	// Message box	
		$('#comment').blur(function(){
		if (this.value == "") {
			$('.commentx').fadeIn(200);
		}
	})
	$('#comment').focus(function(){
		$('.commentx').fadeOut(200);
	})

	// Verification Code
	$('#code').blur(function(){
		var _this = this;
		$.ajax({
		   type: "POST",
		   url: "/wp-content/themes/welovewp_5/captcha/ajax_captcha.php",
		   data: "c="+this.value,
		   cache: false,
		   success: function(c){
				if (_this.value == "") {
					$('.codex').fadeIn(200);
					$('#randomcode').attr('src', $('#randomcode').attr('src')+'?'+new Date().getTime());
				}else if(c!="true"){
					$('.codevx').fadeIn(200);
					$('#randomcode').attr('src', $('#randomcode').attr('src')+'?'+new Date().getTime());
				}else{
					$('.codevx').fadeOut(200);
				}
		   }
		});		
		/*var rcode = document.getElementById('randomcode');
		if (this.value == "") {
			$('.codex').fadeIn(200);
		}else if(this.value != rcode.value){
			$('.codevx').fadeIn(200);
		}else{
			$('.codevx').fadeOut(200);
		}*/
	})
	
	$('#code').focus(function(){
		$('.codex').fadeOut(200);
	})

	// Submit Validate
	$('#submit').click(function(){
		var rcode = "";
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		var name = document.getElementById('author');
		var email = document.getElementById('email');
		var comment = document.getElementById('comment');
		var code = document.getElementById('code');
		var url = document.getElementById('url');
		var cat = document.getElementById('cat');
		var sitename = document.getElementById('sitename');
		
		var result = true;

		$.ajax({
		   type: "POST",
		   async:false,
		   url: "/wp-content/themes/welovewp_5/captcha/ajax_captcha.php",
		   data: "c="+$('#code').val(),
		   cache: false,
		   success: function(c){
				rcode=c;
		   }
		});		


		
		if(name.value == ""){
			$(".authorx").show(); result =  false;
		}
		if(email.value == ""){
			$(".emailx").show(); result =  false;
		} else if(!filter.test(email.value)) {
			$('.emailvx').show(); result =  false;
		}
		
		if(url.value == ""){
			$(".urlx").show(); result =  false;
		}
		if(sitename.value == ""){
			$(".sitenamex").show(); result =  false;
		}
		if(cat.value == ""){
			$(".catx").show(); result =  false;
		}
		if(comment.value == ""){
			$(".commentx").show(); result =  false;
		}
		if(code.value == ""){
			$('#randomcode').attr('src', $('#randomcode').attr('src')+'?'+new Date().getTime());
			$(".codex").show(); result =  false;
		}else if(rcode!="true"){
			$('#randomcode').attr('src', $('#randomcode').attr('src')+'?'+new Date().getTime());
			$(".codevx").show(); result =  false;
		}
		
		if(result){
			$(".authorx").hide();
			$(".emailx").hide();
			$(".commentx").hide();
			$(".codex").hide();
			$(".codevx").hide();
			$('#contactform').fadeOut(1000);
			return true;			
		}
		return false;
	})
}) //function ends