$(document).ready(function(){

	$('#block_order_call').mouseover(
		function(){
			 $('#block_order_call').css("display","block");
		}
	).mouseout(
		function(){
			$('#block_order_call').css("display","none");
		}
	);

	$('#button_show_order_call').mouseover(
		function() {
			$('#block_order_call').show('fast');
			$('#order_call_name').focus();
		}
	);

	/*
	$('#block_order_call').mouseover(
		function() {
			$('#block_order_call').show('fast');
		}
	);
	$('#block_order_call, #block_order_call table, #block_order_call table form').hover(
			function() {
				$('#block_order_call').show('fast');
			},
			function() {
				$('#block_order_call').hide('fast');
			}
	);
	*/
	city = $('#city_name').html();
	dayweek = Number($('#day_of_week').html());

	$('#call_hour, #call_minute').focus(function () {
    	check_ordertime(city, dayweek);
	});

	$('#block_order_call').mousemove(function () {
    	check_ordertime(city, dayweek);
	});

	$('#call_hour, #call_minute').keypress(function () {
    	check_ordertime(city, dayweek);
	});


    function check_ordertime (city_name, day_of_week) {    	order_call_status = true;
    	last_hour = 23;    	if(city_name == 'samara' && (day_of_week > 0 && day_of_week < 4)) {           	order_call_status = false;    	}
     	else if(city_name == 'eburg' && day_of_week < 5) {     		order_call_status = false;
     	}
     	else if(city_name != 'moscow' && city_name != 'eburg' && city_name != 'samara') {     		order_call_status = false;     	}
     	if(!order_call_status) {
	    	if(city_name == 'samara') {        		if($('#call_hour').val() >= 2 && $('#call_hour').val() < 11) {
					$('#send_order_call').css('display', 'none');
					$('#error_order_call_samara').css('display', 'block');
				}
				else {
					$('#send_order_call').css('display', 'block');
					$('#error_order_call_samara' + city_block).css('display', 'none');
				}	    	}
	    	else {            	if($('#call_hour').val() >= 23 || $('#call_hour').val() < 11) {
					$('#send_order_call').css('display', 'none');
					$('#error_order_call').css('display', 'block');
				}
				else {
					$('#send_order_call').css('display', 'block');
					$('#error_order_call').css('display', 'none');
				}	    	}
	    }
		if($('#call_hour').val() > 23) {
           	$('#call_hour').val(11);
		}	}

	$('#button_close_order_call, #button_close_order_call2').click(
		function() {
			$('#block_order_call').hide('fast');
		}
	);
	/*
	$('#order_call_time_now').click(
		function() {
			$('#order_call_hour_minute').hide('fast');
		}
	);
	*/
	$('#set_time').click(
		function() {
			/*$('#order_call_hour_minute').show('fast');*/
			$('#call_hour').focus();
		}
	);
	$('#call_hour, #call_minute').focus(function() {
		//$(this).val('');
	});

	$('#call_hour').keyup(
		function(){
			if($(this).val().length == 2){
				$('#call_minute').focus();
			}
		}
	);
});

function order_call_testPhone(phone)
{
	reg = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
	var result=reg.test(phone);
	return (result);
}

function order_call_testPhoneCode(phonecode)
{
	reg = /^[0-9][0-9][0-9]$/;
	var result=reg.test(phonecode);
	return (result);
}

function send_order_call(form){
	var errors = 0;

	$('#order_call_block .check_phonecode').each(function (i) {
		if((order_call_testPhoneCode(this.value) == false) || (this.value == '')){
			$(this).addClass('error');
			var error_text = $(this).attr("rel");
			alert(error_text);
			errors++;
		} else {
			$(this).removeClass('error');
		}
	});

	$('#order_call_block .check_phone').each(function (i) {
		if((order_call_testPhone(this.value) == false) || (this.value == '')){
			$(this).addClass('error');
			var error_text = $(this).attr("rel");
			alert(error_text);
			errors++;
		} else {
			$(this).removeClass('error');
		}
	});

	var errors_null = 0;
	$('#order_call_block .check_null').each(function (i) {
		if(this.value == ''){
			$(this).addClass('error');
			errors++;
			errors_null++;
		} else {
			$(this).removeClass('error');
		}
	});

	//check time
	exd = new Date();
	var current_hour = exd.getHours();
	var current_minute = exd.getMinutes();
	var set_time = $('#order_call_block #set_time').attr('checked');
	var hour = parseInt($('#order_call_block #call_hour').val());
	var minute = parseInt($('#order_call_block #call_minute').val());
	if(set_time != undefined && (hour < current_hour || !hour || !minute || hour > 23 || minute > 59 || (hour = current_hour && minute < current_minute))){
		alert('Неверно указали время звонка');
		$('#order_call_block #call_hour').addClass('error');
		$('#order_call_block #call_minute').addClass('error');
		errors++;
	} else {
		$('#order_call_block #call_hour').removeClass('error');
		$('#order_call_block #call_minute').removeClass('error');
	}

	if(errors_null > 0){
		alert('Выделенные поля обязательны для заполнения');
	}
	if(errors == 0){
		form.submit();
	} else {
		return false;
	}
}