jQuery(document).ready(function() {

//http://malsup.com/jquery/form/#getting-started
//$('#checkForm').ajaxForm({
//	beforeSubmit: validate
// 	});
/* form validation */
  //for testing purposes uncomment and form wont submit to backend, instead will get alert
// prepare Options Object 
var options = { 
	beforeSubmit: validate,
    target:     '#divFeedback', 
    url:        '../../home/sendMail', //url to sendMail controller
    success:    makeAnswer
}; 
//change value for input hidden if js on
$('input.hidden').attr('value', 'isjs');

// pass options to ajaxForm 
	$('#checkForm').ajaxForm(options);

	function makeAnswer(){
		$('form#checkForm').slideUp();
		$('divFeedback').show('slow');
	}
//while this return false => form have wrong input. If ok then do ajax
 function validate(formData, jqForm, options){
 	if ($("form#checkForm").valid() == false) {
 		return false;
 	}
 	else { //form is ok, do things but only once!
			$('p.submitButton input').hide();
			$('p.submitButton input').parent().append("<img src='../../images/img_system/loading.gif' alt='.' />");
			return true;
		}
	} 
 	  
  // validate register form on keyup and submit
  //http://docs.jquery.com/Plugins/Validation
  //$("#form_register form").validate({
  $("form#checkForm").validate({
    rules: {
      TradeName: "required",
      Telephone: "required",
      Address1: "required",
      TownCity: "required",
      Country: "required",
      ZipCode: "required",
      Title: "required",
      FirstName: "required",
      Textarea: "required",
      LastName: "required",
      Email: {
        required: true,
        email: true
      },
      JobTitle: "required",
      password: {
        required: true,
        minlength: 5
      },
      psw: {
        required: true,
        minlength: 5
      },
      confirmPassword: {
        required: true,
        minlength: 5,
        equalTo: "#password"
      },
      username: "required",
      usr: "required"
    },
    messages: {
      TradeName: "Please enter your Company name",
      Telephone: "Please enter your telephone",
      Address1: "Please enter your address",
      TownCity: "Please enter your town city",
      Country: "Please enter your country",
      ZipCode: "Please enter your postal code",
      Title: "Please enter your title",
      FirstName: "Please enter your first name",
      Textarea: "Please enter enquiry",
      LastName: "Please enter your last name",
      Email: {
        required: "Please enter your email",
        email: "Please enter a valid email address"
      },
      JobTitle: "Please enter your job title",
      password: {
        required: "Please enter your password",
        minlength: "Your password must be <br />at least 5 characters long"
      },
      confirmPassword: {
        required: "Please provide a password",
        minlength: "Your password must be <br />at least 5 characters long",
        equalTo: "Please enter <br />the same password as above"
      },
      username: "Please enter your username"
    }
  });

//hide email address from spider

	$('span.mailme').each(function(index){
		$(this).attr("id", "mailNr" + index);
			var spt = $('span#mailNr' + index);
			var at = / at /;
			var dot = / dot /g;
			var addr = $(spt).text().replace(at,"@").replace(dot,".");
			$(spt).after('<a href="mailto:'+addr+'" title="Send an email">'+ addr +'</a>')
			.hover(function(){window.status="Send a letter!";}, function(){window.status="";});
			$(spt).remove();
		//});
		});
//UI play with link bg
	$('a.buttonLink').hover(function() {
		
	  $(this).addClass('moveBg');
	  $('.moveBg').hide();
	  		$('.moveBg').fadeIn('slow').css({'background-position':'100% 0%', 'float':'left', 'width':'auto'});
		}, function() {
			
	  $('.moveBg').show().css({'background-position':'0 100%'});
	});


	























//end of script
});
