var jqf = jQuery.noConflict();
jqf(document).ready(function(){
	/**
	* set datepicker
	*/
	jqf(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });

	/**
	* Number field filter
	*/
	jqf('.JFormNumber').keydown(function(event){
		if (
			(event.keyCode >= 48 && event.keyCode <= 57)
			|| (event.keyCode >= 96 && event.keyCode <= 105)
			|| event.keyCode == 8  // backspace
			|| event.keyCode == 37 // left
			|| event.keyCode == 38 // up
			|| event.keyCode == 39 // right
			|| event.keyCode == 40 // down
			|| event.keyCode == 46 // delete)
		) {
			return true;
		}
		return false;
	});

	/**
	* Textarea char limit
	*/
	jqf('.JFormMaxLength').keyup(function(event) {
		var width = 0;

		var minLength = false;
		if (jqf('#' + this.id + 'CharsLineBox .minChars').length > 0) {
			minLength = jqf('#' + this.id + 'CharsLineBox .minChars').text();
		}
		var maxLength = false;
		if (jqf('#' + this.id + 'CharsLineBox .maxChars').length > 0) {
			maxLength = jqf('#' + this.id + 'CharsLineBox .maxChars').text();
		}

		jqf('#' + this.id + 'CharsLineBox .charsNumber').text(this.value.length);

		width = Math.round(100 / (maxLength / this.value.length));
		if (width > 100) {
			width = 100;
		}
		if (this.value.length > maxLength || this.value.length < minLength) {
			jqf('#' + this.id + 'CharsLineBox .writtenCharsLine').addClass('writtenCharsLineInvalid');
		}
		else {
			jqf('#' + this.id + 'CharsLineBox .writtenCharsLine').removeClass('writtenCharsLineInvalid');
		}
		jqf('#' + this.id + 'CharsLineBox .writtenCharsLine').css('width', width + '%');
	});
	// after max number of charsets disable typing into the texarea except special charset. / /
	jqf('.JFormMaxLength').keydown(function(event) {
		var maxLength = false;
		if (jqf('#' + this.id + 'CharsLineBox .maxChars').length > 0) {
			maxLength = jqf('#' + this.id + 'CharsLineBox .maxChars').text();
		}

		if (
			maxLength
			&& this.value.length >= maxLength
			&& event.keyCode != 8  // backspace
			&& event.keyCode != 37 // left
			&& event.keyCode != 38 // up
			&& event.keyCode != 39 // right
			&& event.keyCode != 40 // down
			&& event.keyCode != 46 // delete
		) {
			return false;
		}
	});


	/**
	*  AJAX - submit the form function
	*/
	jqf('.JFormForm').submit(function() {
		// Form validation //
		var errorString = false;
		var email=/^.+@.+\..{2,6}$/
		jqf('.JFormMandatory', this).each(function() {
			if (
				typeof this.value == 'undefined'
				|| this.value == ''
				|| (jqf(this).hasClass('JFormEmail') && email.test(this.value) == false)
			) {
				jqf('label[for=' + this.name + ']').addClass('error');
				if (errorString == false) {
					jqf(this).focus();
				}
				errorString = true;
			}
			else {
				jqf('label[for=' + this.name + ']').removeClass('error');
			}
		});

		// textarea checking max and min Length //
		jqf('.JFormMaxLength', this).each(function() {
			var minLength = false;
			if (jqf('#' + this.id + 'CharsLineBox .minChars').length > 0) {
				minLength = jqf('#' + this.id + 'CharsLineBox .minChars').text();
			}
			var maxLength = false;
			if (jqf('#' + this.id + 'CharsLineBox .maxChars').length > 0) {
				maxLength = jqf('#' + this.id + 'CharsLineBox .maxChars').text();
			}
			if (this.value.length > maxLength || this.value.length < minLength) {
				jqf('label[for=' + this.name + ']').addClass('error');
				if (errorString == false) {
					jqf(this).focus();
				}
				errorString = true;
			}
			else {
				if (
					!jqf(this).hasClass('JFormMandatory')
					|| (jqf(this).hasClass('JFormMandatory') && typeof this.value == 'undefined' && this.value == '')
				){
					jqf('label[for=' + this.name + ']').removeClass('error');
				}
			}
		});

		// print out error //
		if (errorString == true) {
			jqf('.JFormStatus').hide();
			jqf('.JFormStatusError').show();

			var destination = jqf('label.error').offset().top;
			jqf('html').animate({scrollTop: destination});
			return false;
		}

		thisForm = this;
		jqf(this).ajaxSubmit({
			success: function(responseText, statusText) {
				if (responseText == 'success') {
					// print success message //
					jqf('.JFormStatus').hide();
					jqf('.JFormStatusSuccess').show();

					// clear form //
					jqf('.JFormClear', thisForm).each(function() {
						jqf(this).attr('value', '');
					});
					jqf('.JForm[type=radio]', thisForm).each(function() {
						jqf(this).attr('checked', false);
					});
					jqf('.JForm[type=checkbox]', thisForm).each(function() {
						jqf(this).attr('checked', false);
					});

				}
				else if (responseText.match(/^redirect:/)) {
					var redirectLocation = responseText.replace(/redirect:/, '');
					window.location = redirectLocation;
				}
				else {
					// print error message //
					jqf('.JFormStatus').hide();
					jqf('.JFormStatusError').show();

					jqf('label[for=capcha]').addClass('error');
					jqf(this).focus();
				}
				var destination = jqf('.JFormStatus').offset().top;
				jqf('html').animate({scrollTop: destination});
			}
		});
		return false;

	});


});
