var jqf = jQuery.noConflict();
jqf(document).ready(function(){
	/**
	* Textarea char limit
	*/
	jqf('#c_comment').keyup(function(event) {
		jqf('#charsNumber').text(this.value.length);
		var width = 0;
		width = Math.round(100 / (1000 / this.value.length));
		if (width > 100) {
			width = 100;
		}
		if (this.value.length > 1000) {
			jqf('#writtenCharsLine').css('background-color', '#DD0000');
			jqf('#submitComplaintsForm').hide();
		}
		else {
			jqf('#writtenCharsLine').css('background-color', '#7EA1B5');
			jqf('#submitComplaintsForm').show();
		}
		jqf('#writtenCharsLine').css('width', width + '%');
	});
	// after 1000 charsets disable typing into the texarea except special charset. //
	jqf('#c_comment').keydown(function(event) {
		if (
			this.value.length >= 1000
			&& 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;
		}
	});

	/**
	*  submit the form
	*/
	jqf('#complaintsForm').submit(function() {
		var errorString = false;

		// validation //
		if (typeof(jqf('#c_type').attr('value')) == 'undefined' || jqf('#c_type').attr('value') == '') {
			errorString = true;
			jqf('label[for=c_type]').addClass('error');
			jqf('#c_type').addClass('error');
			jqf('#c_type').focus();
		}
		else {
			jqf('#c_type').removeClass('error');
			jqf('label[for=c_type]').removeClass('error');
		}

		if (typeof(jqf('#c_fname').attr('value')) == 'undefined' || jqf('#c_fname').attr('value') == '') {
			errorString = true;
			jqf('label[for=c_fname]').addClass('error');
			jqf('#c_fname').addClass('error');
			if (errorString == false) {
				jqf('#c_fname').focus();
			}
		}
		else {
			jqf('#c_fname').removeClass('error');
			jqf('label[for=c_fname]').removeClass('error');
		}

		email=/^.+@.+\..{2,6}$/
		if (typeof(jqf('#c_email').attr('value')) == 'undefined' || jqf('#c_email').attr('value') == '' || email.test(jqf('#c_email').attr('value')) == false) {
			if (errorString == false) {
				jqf('#c_email').focus();
			}
			errorString = true;
			jqf('#c_email').addClass('error');
			jqf('label[for=c_email]').addClass('error');
		}
		else {
			jqf('label[for=c_email]').removeClass('error');
			jqf('#c_email').removeClass('error');
		}

		if (typeof(jqf('#c_comment').attr('value')) == 'undefined' || jqf('#c_comment').attr('value') == '') {
			if (errorString == false) {
				jqf('#c_comment').focus();
			}
			errorString = true;
			jqf('label[for=c_comment]').addClass('error');
			jqf('#c_comment').addClass('error');
		}
		else {
			jqf('#c_comment').removeClass('error');
			jqf('label[for=c_comment]').removeClass('error');
		}

		if (errorString == true) {
			if (jqf('#complaintsFormBox h4.sent').length > 0) {
				jqf('#complaintsFormBox h4.sent').hide();
			}
			if (jqf('#complaintsFormBox h4.error').length == 0) {
				jqf('#complaintsFormBox').prepend('<h4 class="error">Please, fill out highlighted fields and try again.</h4>');
			}
			var destination = jqf('label.error').offset().top;
			jqf('html').animate({scrollTop: destination});
			return false;
		}

		//get data //
		queryString = 'c_fname=' + jqf('#c_fname').attr('value') + '&'
			+ 'c_type=' + jqf('#c_type').attr('value') + '&'
			+ 'c_title=' + jqf('#c_title').attr('value') + '&'
			+ 'c_surname=' + jqf('#c_surname').attr('value') + '&'
			+ 'c_address=' + jqf('#c_address').attr('value') + '&'
			+ 'c_address2=' + jqf('#c_address2').attr('value') + '&'
			+ 'c_address3=' + jqf('#c_address3').attr('value') + '&'
			+ 'c_address4=' + jqf('#c_address4').attr('value') + '&'
			+ 'c_address5=' + jqf('#c_address5').attr('value') + '&'
			+ 'c_email=' + jqf('#c_email').attr('value') + '&'
			+ 'c_phone=' + jqf('#c_phone').attr('value') + '&'
			+ 'c_siteloc=' + jqf('#c_siteloc').attr('value') + '&'
			+ 'c_fleetreg=' + jqf('#c_fleetreg').attr('value') + '&'
			+ 'c_comment=' + jqf('#c_comment').attr('value');

		// send form //
		jqf.ajax({
			type: 'POST',
			url: '/ajax/ComplaintsForm.ajax.php?js=1',
			data: queryString,
			success: function(msg) {
				if (msg == 'success') {
					if (jqf('#complaintsFormBox h4.error').length != 0) {
						jqf('#complaintsFormBox h4.error').hide();
					}
					jqf('#complaintsFormBox').prepend('<h4 class="sent">Message sent successfully.</h4>');

					// clear form //
					jqf('#c_fname').attr('value', '');
					jqf('#c_address').attr('value', '');
					jqf('#c_address2').attr('value', '');
					jqf('#c_address3').attr('value', '');
					jqf('#c_address4').attr('value', '');
					jqf('#c_address5').attr('value', '');
					jqf('#c_email').attr('value', '');
					jqf('#c_phone').attr('value', '');
					jqf('#c_comment').attr('value', '');
					jqf('#c_type').attr('value', '');
					jqf('#c_fleetreg').attr('value', '');
					jqf('#c_title').attr('value', '');
					jqf('#c_siteloc').attr('value', '');
				}
				else {
					if (jqf('#complaintsFormBox h4.error').length == 0) {
						jqf('#complaintsFormBox').prepend('<h4 class="error">Please, fill out highlighted fields and try again.</h4>');
					}
					jqf('#c_email').addClass('error');
					jqf('label[for=c_email]').addClass('error');
				}
				var destination = jqf('#complaintsFormBox').offset().top;
				jqf('html').animate({scrollTop: destination});
			}
		});
		return false;
	});
});
