$.noConflict();
jQuery(document).ready(function($) {
//$(document).ready(function() {
	var checkHeadline = function (ad_headline) {
		//remove all the class add the messagebox classes and start fading
		$("#hmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(0);
		$("#hmsgbox").css('display','inline');
		var ad_headline = $('#ad_headline').val();
		$.post(
			"validateFormAds.php",
			{check: 'ad_headline', headline: ad_headline},
			function(data) {
				if(data=='no') { //if ad_headline not avaiable
				  	$("#hmsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('X').addClass('messageboxerror').fadeTo(0,1);
						$("#hmsgbox").css('display','inline');
					});		
          		}				
		  		else if(data=='yes') {
		  			$("#hmsgbox").fadeTo(0,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('V').addClass('messageboxok').fadeTo(0,1);
						$("#hmsgbox").css('display','inline');	
					});
		  		}				
		  		else if(data=='short') {
		  			$("#hmsgbox").fadeTo(0,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('X').addClass('messageboxerror').fadeTo(0,1);
						$("#hmsgbox").css('display','inline');	
					});
		  		}
				else { 
				  	$("#hmsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('').addClass('messageboxno').fadeTo(0,1);
						$("#hmsgbox").hide();
					});		
          		}					
			}			
		)
	}
	
	var checkText = function (ad_text) {
		//remove all the class add the messagebox classes and start fading
		$("#tmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(0);
		$("#tmsgbox").css('display','inline');
		var ad_text = $('#ad_text').val();
		$.post(
			"validateFormAds.php",
			{check: 'ad_text', text: ad_text},
			function(data) {
				if(data=='no') { //if ad_text not avaiable
				  	$("#tmsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('X').addClass('messageboxerror').fadeTo(0,1);
						 $("#tmsgbox").css('display','inline');
					});		
          		}				
		  		else if(data=='yes')  { //if ad_text ok
		  			$("#tmsgbox").fadeTo(0,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('V').addClass('messageboxok').fadeTo(0,1);
						 $("#tmsgbox").css('display','inline');	
					});
		  		}
				else { 
				  	$("#tmsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('').addClass('messageboxno').fadeTo(0,1);
						$("#tmsgbox").hide();
					});		
          		}				
			}			
		)
	}
	
	var checkPhone = function (ad_phone) {
		//remove all the class add the messagebox classes and start fading
		$("#pmsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(0);
		$("#pmsgbox").css('display','inline');
		var ad_phone = $('#ad_phone').val();
		$.post(
			"validateFormAds.php",
			{check: 'ad_phone', phone: ad_phone},
			function(data) {
				if(data=='no') { //if ad_text not avaiable
				  	$("#pmsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('X').addClass('messageboxerror').fadeTo(0,1);
						$("#pmsgbox").css('display','inline');
					});		
          		}				
		  		else if(data=='yes')  { //if ad_text ok
		  			$("#pmsgbox").fadeTo(0,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('V').addClass('messageboxok').fadeTo(0,1);
						$("#pmsgbox").css('display','inline');	
					});
		  		}
				else { 
				  	$("#pmsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('').addClass('messageboxno').fadeTo(0,1);
						$("#pmsgbox").hide();
					});		
          		}				
			}			
		)
	}
	
	var checkEmail = function (email) {
		//remove all the class add the messagebox classes and start fading
		$("#emsgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(0);
		$("#emsgbox").css('display','inline');
		var email = $('#email').val();
		$.post(
			"validateFormAds.php",
			{check: 'email', email: email},
			function(data) {
				if(data=='no') { //if ad_text not avaiable
				  	$("#emsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('X').addClass('messageboxerror').fadeTo(0,1);
						$("#emsgbox").css('display','inline');
					});		
          		}				
		  		else if(data=='yes')  { //if ad_text ok
		  			$("#emsgbox").fadeTo(0,0.1,function() {  //start fading the messagebox
			  			//add message and change the class of the box and start fading
			 		 	$(this).html('V').addClass('messageboxok').fadeTo(0,1);
						$("#emsgbox").css('display','inline');	
					});
		  		}
				else { 
				  	$("#emsgbox").fadeTo(0,0.1,function() { //start fading the messagebox
						//add message and change the class of the box and start fading
			  			$(this).html('').addClass('messageboxno').fadeTo(0,1);
						$("#emsgbox").hide();
					});		
          		}				
			}			
		)
	}	
	
	$("#ad_headline").blur(checkHeadline);
	$("#ad_text").blur(checkText);
	$("#ad_phone").blur(checkPhone);
	$("#email").blur(checkEmail);
	//$('input[type="submit"]').hover(checkHeadline);
	//$('input[type="submit"]').hover(checkText);
	//$('input[type="submit"]').hover(checkPhone);
	//$('input[type="submit"]').hover(checkEmail);
	$('input[type="submit"]').hover(function() {
		$("#ad_headline").trigger( "blur" );
		$("#ad_text").trigger( "blur" );
		$("#ad_phone").trigger( "blur" );
		$("#email").trigger( "blur" );
	});

	//$('#submitform').click(function() {
		//X$("#ad_headline").trigger( "blur" );
		//X$("#ad_text").trigger( "blur" );
		//X$("#ad_phone").trigger( "blur" );
		//X$("#email").trigger( "blur" );
		//$("#safo").submit();
		//$("form#safo").submit();
		
	//	$.when( checkHeadline("#ad_headline"), checkText("#ad_text"), checkPhone("#ad_phone"), checkEmail("#email") )
	//	.then( function (h,t,p,e) { $("form#safo").submit(); } );
		
		//$('#safo').submit(function() {
		//	alert( "2" );
		//	//return submitbutton(this);
		//});		
	//});	
});