$(document).ready(function(){
//	action();
});

function send_feedback() {
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var err = false;
	$('#feedback_working').show();
	$('#feedback_form .error').hide("slow").html('');

		if ($('#feedback_contact_name').val() == '') {
			$('#feedback_contact_name_error_log').html('Enter you name please.').show("slow");
			err = true;
		}

		if (!$('#feedback_email').val().match(email_reg)){
			$('#feedback_email_error_log').html('Email is not valid.').show("slow");
			err = true;
		}

		if ($('#feedback_subject').val() == '') {
			$('#feedback_subject_error_log').html('Enter subject please.').show("slow");
			err = true;
		}

		if ($('#feedback_enquiry').val() == '') {
			$('#feedback_enquiry_error_log').html('Enter enquiry please.').show("slow");
			err = true;
		}

		if ($('#feedback_captcha_code').val() == '') {
			$('#feedback_captcha_code_error_log').html('Enter antispam code.').show("slow");
			err = true;
		}

		if (err == true) {
			$('#feedback_working').hide("hide");
			return;
		}

	$.ajax({
		type: "GET",
		url: "/ajax/ajax_comments.php",
		async: false,
		data: "action=add_feedback&feedback_contact_name=" + $('#feedback_contact_name').val() + "&feedback_copany=" + $('#feedback_copany').val() + "&feedback_email=" + $('#feedback_email').val() + "&feedback_subject=" + $('#feedback_subject').val() + "&feedback_found=" + $('#feedback_found').val() + "&feedback_enquiry=" + $('#feedback_enquiry').val() + "&feedback_captcha_code=" + $('#feedback_captcha_code').val() + "&rand=" + Math.random(),
		success: function(data){

				if ($.trim(data) == 'true') {
					$('#feedback_form .error').hide().html('');
					$('#feedback_box_log').show();
					$('#feedback_form').hide();
					$("#feedback_form").each(function() {
						this.reset();
					});
				} else if (data == 'wrong capcha') {
					$('#feedback_captcha_code_error_log').html('Wrong antispam code.').show("show");
					$('#feedback_captcha_code').val('');
				}

			make_captcha('feedback_captcha');
			$('#feedback_working').hide("hide");
		}
	});
}

function add_comment(item_id) {
	var err = false;
	$('#commentform .error').hide().html('');

		if ($('#author').val() == '') {
			$('#comment_author_error_log').html('Enter you name please.').show();
			err = true;
		}

		if ($('#captcha_code').val() == '') {
			$('#captcha_code_error_log').html('Enter code please.').show();
			err = true;
		}

		if ($('#comment').val() == '') {
			$('#comment_error_log').html('Enter you comment please.').show();
			err = true;
		}

		if (err == true) {
			return;
		}

	$.ajax({
		type: "GET",
		dataType: "json",
		url: "/ajax/ajax_comments.php",
		async: false,
		data: "action=add_comment&captcha_code=" + $('#captcha_code').val() + "&author=" + $('#author').val() + "&comment=" + $('#comment').val() + "&item_id=" + item_id + "&rand=" + Math.random(),
		success: function(json){

				if (json.status == 'ERR') {

						for (i = 0; i < json.error.length; ++i) {

								if (json.error[i].field == 'captcha_code') { 
									$('#captcha_code_error_log').html(json.error[i].message).show();
									$('#captcha_code').val('');
									make_captcha('add_comment_captcha');
								} else {
									$('#' + json.error[i].field + '_error_log').html(json.error[i].message).show();
								}

						}

				} else {
					$('#comment_box').html('Thanks for your comment! It will appear when approved.');
				}

		}
	});
}

function recover_password() {
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var err = false;
	$('#recover_password_working').show();
	$('#recovery_form .error').hide().html('');

		if (!$('#recovery_email').val().match(email_reg)){
			$('#recovery_email_error_log').html('Email is not valid.').show("slow");
			err = true;
		}

		if ($('#recovery_captcha_code').val() == '') {
			$('#recovery_captcha_code_error_log').html('Enter antispam code.').show("slow");
			err = true;
		}

		if (err == true) {
			$('#recover_password_working').hide();
			return;
		}

	$.ajax({
		type: "GET",
		dataType: "json",
		url: "/ajax/ajax_comments.php",
		async: false,
		data: "action=recover_password&captcha_code=" + $('#recovery_captcha_code').val() + "&email=" + $('#recovery_email').val() + "&rand=" + Math.random(),
		success: function(json){

				if (json.status == 'ERR') {

						for (i = 0; i < json.error.length; ++i) {

								if (json.error[i].field == 'captcha_code') { 
									$('#recovery_captcha_code_error_log').html(json.error[i].message).show();
									$('#recovery_captcha_code').val('');
									make_captcha('recovery_captcha');
								} else {
									$('#recovery_' + json.error[i].field + '_error_log').html(json.error[i].message).show();
								}

						}

				} else {
					$('#recovery_form').html('Check Yuor email.');
				}

		}
	});

	$('#recover_password_working').hide();
}

function make_captcha(captcha) {
	$('#' + captcha).html('');
	$.ajax({
		type: "GET",
		url: "/ajax/ajax_comments.php",
		async: true,
		data: "action=make_captcha&rand=" + Math.random(),
		success: function(){
			$('#' + captcha).html('<img style="height:60px;" src="/kcaptcha/index.php?rand=' + Math.random() + '"/>');
		}
	});
}
/*
function is_logged_in(){
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_session.php",
					async: false,
					data: "action=is_logged_in&rand=" + Math.random()
				}).responseText;
}
*/