addFunction('speedDomain', "null");
addFunction('moveMouseForm', "null");
addFunction('clearInput', "null");

var aFocus=new Array();		//global array ID inputs

function clearInput(){
	$(document).ready(function(){
		$('input.clearing').live('focusin', function(){
			if(!aFocus[$(this).attr('id')]){
				//alert('aFocus is exist' + $(this).attr('id'));
			//} else {
				aFocus[$(this).attr('id')] = true;
				$(this).val('');
			}
		});
	});
}

function moveMouseForm(){
	$(document).ready(function() {
		$('#boxes-login').live( 'mousemove', function(e) {	//click(function() {
			var top = $('#boxes-login').position().top;
			var left = $('#boxes-login').position().left;
			var bottom = top + 380;
			var right = left + 890;
			var mouseX = e.pageX;
			var mouseY = e.pageY;
			var half = Math.round((right-left)/2)+left;
			//$('#id-top').html('<br/>TOP : ' + top);
			//$('#id-bottom').html('<br/>BOTTOM : ' + bottom);
			//$('#id-left').html('<br />LEFT : ' + left);
			//$('#id-right').html('<br />RIGHT : ' + right);
			//$('#id-mousex').html('<br />MOUSEX : ' + mouseX);
			//$('#id-mousey').html('<br />MOUSEY : ' + mouseY);
			//$('#id-half').html('<br />HALF : ' + half);
			
			if (mouseX < half){
				//$('label#label-captcha').css('cssText', 'margin-left: 50px !important');
				  $(".label-captcha").animate({marginLeft: "50px"}, { queue:false, duration:500 } );

			} else {
				$(".label-captcha").animate({marginLeft: "480px"}, { queue:false, duration:500 } );
				//$('label#label-captcha').css({marginLeft: "480px"});
				//$('label#label-captcha').css('cssText', 'margin-left: 480px !important');
			}
		 });
	});

	
	
	

}

function validateForm(){
	var bools = true;
	//if($('#domain_name_available').val()){
		if ($('input:radio[name=service]:checked').val() == 'true' ){
			//bools = true;
		} else if ($('input:radio[name=service]:checked').val() == 'false' ){
			$('.error').html('');
			if(isURL($('#url').val())){
		    } else {
		    	$('#url').focus();
		    	$('#error-url').html('Podany URL nie jest prawidłowy. Podaj poprawny adres URL.');
					bools = false;
		    }
			if($('#title').val()){
				if($('#title').val().length > 2){
				} else {
					$('#title').focus();
					$('#error-title').html('Tytuł strony musi zawierać co najmniej 3 znaki');
					bools = false;
				}
			} else {
				$('#title').focus();
				$('#error-title').html('Wpisz tytuł strony.');
				bools = false;
			}
		} else {
			alert('Nie wybrano usługi w kroku 2!');
			bools = false;
		}
	//} else {
		//$('#domain-available-input').css( {border:'1px solid red'} );
		$('#domain-available-input').focus();
		//alert('Uwaga nie wybrano domeny!');
		//------------
		//$('#domain-available-input').css( {border:'1px solid #C0C0C0'} );
		$('#ok').css( {display:'none'} );
	    var name = $("#domain-available-input").val();
	    var domainSuffixId = $("#domain_suffix_id").val();
	    
	    $.ajax({
	      type: "POST",
	      url: '/domain-new_availability/',
	      data: 'name=' + name + '&domain_suffix_id=' + domainSuffixId,
	      dataType: "json",
	      async: false,
	      error: function (xhr, desc, exceptionobj) {
	        //alert(desc);
	        if (xhr.status==404) {
	          alert('Błąd 404 - Strona nie istnieje.');
	        }
	      },
	      beforeSend: function () {
	        $('#ajax-loader-domain-availability').css({display:"inline"});
	        $("#domain-available-input").removeClass('error-border');
	        $("#domain_suffix_id").removeClass('error-border');
	      },
	      success: function (json) {
	        $('.boxDomain .boxContent .check').css( {height:'auto'} );
	        if (json.isSuccess) {
	          $('#domain-availability-info').html(json.parameters.form);
	          $('#domain-available-input').css( {border:'1px solid #C0C0C0'} );
	          $("#domain_suffix_id").css( {border:'1px solid #C0C0C0'} );
	          if(json.parameters.availability)
	        	  bools = false;
	        } else { //error mean validation error
	        	$('#domain-availability-info').html(json.message);
	            $('#domain-availability-info').css( {color:'#ff0000'} );
	            $("#domain-available-input").css({border:'1px solid #ff0000'});
	            $("#domain_suffix_id").css({border:'1px solid #ff0000'});
	            bools = false;
	        }
	      },
	      complete: function () {
	        $('#ajax-loader-domain-availability').css( {display:"none"} );
	        $('#ok').css( {display:'inline'} );
	      }
	    });
		//------------
		
		
	//}
	return bools;
}

/**
 * img element have to have id eq captchaimg
 * "clickable" element have to have id eq refresh-captcha
 *
**/
function speedDomain() {
  $('#create-domain').live( 'click', function() {	
	  $('.error').html('');
	  	//if (validateForm()){
	  	validateForm();
				  if ($('#code').val() != ''){
					  $('#create-domain').css( {display:'none'} );
					  $.ajax({
					      type: "POST",
					      url: '/ajax-add_domain/',
					      data: 'domain=' + $('#domain_name_available').val() + 
					      		'&preinstall_script=' + seclmesct +
					      		
					      		'&recaptcha_challenge_field=' + $('input#recaptcha_challenge_field').val() +
								'&recaptcha_response_field=' + $('input[name=recaptcha_response_field]').val() +
					      		
					      		'&url=' + ($('#url').val()?$('#url').val():'') + 
					      		'&title=' + ($('#title').val()?$('#title').val():'') + 
					      		'&service=' + $('input:radio[name=service]:checked').val(), 
					      dataType: "json",
					      error: function (xhr, desc, exceptionobj) {
					        if (xhr.status==404) {
					          alert('Błąd 404 - Strona nie istnieje.');
					        }
					      },
					      beforeSend: function () {
					          $('#ajax-loader-user-login').css({display:"inline"});
					          $('.error').html('');
					      },
					      success: function (json) {
					        if (json.isSuccess) {
					        	$('#domain-create-info').html('');
					        	$("#boxes-ajax").html('');
					        	window.location =json.parameters.app_url + "konto/";
					        } else { //error mean validation error
					        	Recaptcha.create("6LfiK8ASAAAAAADIQcg0ZGhuEBQil2tGOibAuRh5","prv-recaptcha",{custom_translations : {instructions_visual : "Przepisz oba wyrazy:",refresh_btn : "Nowa kombinacja",help_btn : "Pomoc",audio_challenge : "Posłuchaj dźwięk",play_again : "Odtwórz jeszcze raz",cant_hear_this : "Zapisz jako plik MP3",instructions_audio : "Wpisz co usłyszałeś:",incorrect_try_again : "Błędny kod. Spróbuj ponownie.",custom_theme_widget: "recaptcha_widget"},lang : "pl",theme : "red","width" : 500,callback: Recaptcha.reload});
					        	$.each(json.parameters.error, function(key, value) {
			  						if(key == 'recaptcha_response_field'){
			  							setTimeout(function(){
			  								$('span#recaptcha_instructions_image').hide();
			  								$('span#recaptcha_instructions_error').html(value).css('cssText', 'display: inline !important; font-size: 7pt !important;');
			  							}, 500);
			  						} else {
			  							$("#error-"+key).html(value);
			  						}
			  					});
					        }
					      },
					      complete: function () {
					    	  $('#ajax-loader-user-login').css( {display:"none"} );
					          $('#create-domain').css( {display:'inline'} );
					      }
					    });
					  
				  } else {
					  $('#error-message').html('Uwaga pole z kodem nie może zostać puste!');
				  }
	  	//}
			 
  	return false;
  });
}
