var def_value = 
{ 
	'itstreet':'улица',
	'ithouse':'дом',
	'itflat':'кв.'
};


function onsubmit1()
{
	//test_cookie();

	if ( $('#itfio').val().length == 0)
	{
		$('#itfio').select();
		$('#itfio').focus();
		alert('Введите ФИО.');
		return false;
	}

	if ( ($('#itstreet').val().length == 0) || ($('#itstreet').val() == def_value['itstreet']) )
	{
		$('#itstreet').select();
		$('#itstreet').focus();
		alert('Введите улицу.');
		return false;
	}	

	if ( ($('#ithouse').val().length == 0) || ($('#ithouse').val() == def_value['ithouse']) )
	{
		alert('Введите дом.');
		$('#ithouse').select();
		$('#ithouse').focus();
		return false;
	}	

	if ( $('#itmtel').val()==='' && $('#ithtel').val()===''  )
	{
		$('#itmtel').select();
		$('#itmtel').focus();
		alert('Введите хотя бы один контактный телефон.');
		return false;
	}
	else if(checkInput.amountDigits( $('#itmtel').val() )<7 && checkInput.amountDigits( $('#ithtel').val() )<7)
	{
		$('#itmtel').select();
		$('#itmtel').focus();
		alert('Введите хотя бы один верный контактный телефон.');
		return false;
	}
	
	if ( $('#itprivate_key').val().length == 0 )
	{
		$('#itprivate_key').focus();
		alert('Введите код с картинки.');
		return false;
	}

/*	if ( ($('#ithour').val().length != 2) )
	{
		$('#ithour').select();
		$('#ithour').focus();
		alert('Заполниче поле "Часы"');
		return false;
	}

	if ( ($('#itmin').val().length != 2) )
	{
		$('#itmin').select();
		$('#itmin').focus();
		alert('Заполниче поле "Мин"');
		return false;
	}*/
	
	$.each($("#itstreet, #ithouse, #itflat"), function(index, obj)
	{
		if ( $(obj).val() == def_value[$(obj).attr('id')] )
		{
			$(obj).val();
		}
	});

	$('#iform').submit();
	
}


function test_cookie()
{
	if ( $.cookie("icorder") == 1)
		window.location.reload(false);
}



var checkInput = {
    strip : function(id, expr) {
        var array = ['input', 'propertyChange','textinput', 'keyUp', 'keydown', 'change'],
            i = array.length;
	
	

        while(i--) {
                id['on' + array[i]] = function(e) {
		     var code;
			var ua = navigator.userAgent.toLowerCase();
		     if(!e) e = window.event;
		     if(e.which) code =e.which;
		     else if(e.keyCode) code=e.keyCode;
			if (ua.indexOf("opera") != -1) {
				this.value =  this.value.replace(expr, '');
			}


		     if( e.shiftKey && ( code==48 || code==57 || code==36 || code==35 || code==187 ) )
			{ 
				//this.value =  this.value.replace(expr, '');							
				return true;
			}
			
		     else if( e.shiftKey && ( code != 48 || code != 57 || code != 36 || code != 35 || code != 187 ))
			{
				//this.value =  this.value.replace(expr, '');
		        	return false;
			}
		     else if(code===37 || code===107 || code===39 || code===8 || code===46 || code===35 || code===36 || code===189 || code===32 || code===9 || code===109)
			return true;		     

 		     else if( ( code > 47 && code < 58 ) || ( code > 95 && code < 106 ) )
			return true;

		     else			
			 return false;				


                 }; 
          }
    },

    amountDigits : function(inputString) {

	if(inputString==='') return 0;
	
	var countDigits = inputString.match(/\d/g);

	if(countDigits)
		return countDigits.length;
	else
		return 0;
	
	return countDigits;
    }
    
};





$(document).ready(function()
{
		//setTimeout('test_cookie()', 500);
		var version = 7.0;
		
		if($.browser.msie) version = $.browser.version;

		if( version != 6.0 )
		{
		    checkInput.strip(document.getElementById('itmtel'), /[^\d\s\-\(\)\+]+/g);
		    checkInput.strip(document.getElementById('ithtel'), /[^\d\s\-\(\)\+]+/g);
		}

		

		if ( $("#itderror").attr('innerHTML') != '')
		{
			$("input.captcha").focus();
			$("input.captcha").select();
		}
		else
		{
			$("#itfio").focus();
			$("#itfio").select();			
		}
		
		$.each($("#itstreet, #ithouse, #itflat"), function(index, obj)
		{
			if ( ($(obj).val().length == 0) || ($(obj).val() == def_value[$(obj).attr('id')]) )
			{
				$(obj).css('color', '#a0a0a0');
				$(obj).val(def_value[$(obj).attr('id')]);
			}
		});

		
		if ($('#itmin').val().length != 2)
		{
			$('#itmin').css('color', '#a0a0a0');
			$('#itmin').val('мин');
		}
		else
			$('#itmin').css('color', '#000');

		if ($('#ithour').val().length != 2)
		{
			$('#ithour').css('color', '#a0a0a0');
			$('#ithour').val('часы');
		}
		else
			$('#ithour').css('color', '#000');

		
		$("input.txt, select, input.captcha").keypress(function (e) 
		{
			if (e.which == 13)
			{	
				if (this.name == 'private_key')
					onsubmit1();
								
				var search = this.tabIndex + 1;

				if (search == 10)
				{
					$("input.captcha").focus();
					$("input.captcha").select();
					$(this).blur();
				}
				
				$.each($("input, select"), function(index, obj)
				{
					if ( search ==  obj.tabIndex)
					{
						obj.focus();
						return false;
					}
				});
			}
		});
		
		
		$('#ithour, #itmin').focus(function (e)
		{
			if ( ($(this).val().length != 2) )
			{
				$(this).css('color', '#000');
				$(this).val('');
				$(this).attr('maxlength', 2);
			}
		});

		$('#ithour, #itmin').keypress(function (e)
		{
			if ( (e.which >= 48) && (e.which <= 57) ) 
			{
				return true;
			}
			else if (e.which == 8)
				return true;
			else if (e.which == 0)
				return true;
			
			return false;
		});

		$('#ithour').blur(function (e)
		{
			if ( ($('#ithour').val().length == 1) )
			{
				$('#ithour').val('0' + $('#ithour').val());
			}
			if ( ($('#ithour').val().length != 2)  || ( parseInt($('#ithour').val()) > 23) )
			{
				$('#ithour').css('color', '#a0a0a0');
				$('#ithour').val('часы');
			} 
		});

		$('#itmin').blur(function (e)
		{				
			if ( ($('#itmin').val().length == 1) )
			{
				$('#itmin').val('0' + $('#itmin').val());
			}
			if ( ($('#itmin').val().length != 2) || ( parseInt($('#itmin').val()) > 59) )
			{
				$('#itmin').css('color', '#a0a0a0');
				$('#itmin').val('мин');
			} 
		});
		
		
		$('#itstreet, #ithouse, #itflat').blur(function (e)
		{
			if ( ($(this).val().length == 0) || ($(this).val() == def_value[$(this).attr('id')]) )
			{
				$(this).css('color', '#a0a0a0');
				$(this).val(def_value[$(this).attr('id')]);
			}
		});
		
		$('#itstreet, #ithouse, #itflat').focus(function (e)
		{
			if ( ($(this).val() == def_value[$(this).attr('id')]) )
				$(this).val('');
			
			$(this).css('color', '#000');
		});
		
		
		
		
});		


