$(document).ready(function() {
	if ($.browser.msie) {
        $("input[type='text'].tbPhone").bind("focusin", function() {
            tbPhoneFocusIn($(this));
        });

        $("input[type='text'].tbPhone").bind("focusout", function() {
            tbPhoneFocusOut($(this));
        });
    } else {
        $("input[type='text'].tbPhone").focus(function() {
            tbPhoneFocusIn($(this));
        });

        $("input[type='text'].tbPhone").blur(function() {
            tbPhoneFocusOut($(this));
        });
    }

	function tbPhoneFocusIn(ptr) {
        if (ptr.val() == "код" ||
            ptr.val() == "телефон" ||
            ptr.val() == "добав.")
            ptr.val("");
    }

    function tbPhoneFocusOut(ptr) {
        if (ptr.val() == "") {
            if (ptr.hasClass("tbPhoneCode"))
                ptr.val("код");
            if (ptr.hasClass("tbPhoneNumber"))
                ptr.val("телефон");
            if (ptr.hasClass("tbPhoneAdd"))
                ptr.val("добав.");
        }
    }

	$('a.changeCaptchaLink').click(function() {
		var imgid = $(this).attr('captchaId');
		var imgsrc = $('#' + imgid).attr('src');
		$('#' + imgid).attr('src', imgsrc + new Date() + Math.random());
		return false;
	});
	
	// Little hack for Mozilla Firefox.
	if ($.browser.mozilla)
		$('a.changeCaptchaLink').trigger('click');

	// Order form saving actions.
	$('input.saveMeForm, select.saveMeForm').change(function() {
		var options = { path: '/', expires: 365 };
		var type = $(this).attr('type');
		var name = $(this).attr('name');

		if ('checkbox' == type) {
			$.cookie(name, $(this).attr('checked'), options);
		}
		if ('text' == type || 'password' == type || 'select' == this.tagName.toLowerCase()) {
			$.cookie(name, $(this).val(), options);
		}
		//console.log('%s is %s', name, this.tagName);
	});

	// Order form restore actions.
	$('input.saveMeForm, select.saveMeForm').each(function() {
		var name = $(this).attr('name');
		var value = $.cookie(name);
		var type = $(this).attr('type');

		if (value != null) {
			if ('checkbox' == type) {
				$(this).attr('checked', value === 'true');
			}
			if ('text' == type || 'password' == type || 'select' == this.tagName.toLowerCase()) {
				if (value)
					$(this).val(value);
			}
			if ('bagaz' == name && value === 'true')
				chng_bagaz();
			if ('pay' == name && value === 'true') {
				chng_pay();
				document.getElementById('bonus').checked = false;
				chng_bonus();
			}
			if ('bonus' == name && value === 'true') {
				chng_bonus();
				document.getElementById('pay').checked = false;
				chng_pay();
			}
		} else {
			//console.log('%s is %s', name, value);
		}
	});
});
