jQuery(function(){
    var last_value = "";
    jQuery(".b-feedback .required, .b-form .required, .b-feedback textarea, .b-form textarea").focus(function() {
        if (!jQuery(this).hasClass("modified")) {
            last_value = this.value; 
            this.value = "";
        }
    });
    jQuery(".b-feedback input, .b-form input, .b-feedback textarea, .b-form textarea").blur(function() {
        if (!jQuery(this).hasClass("modified")) {
            if (!this.value) this.value = last_value;
            else jQuery(this).addClass("modified")
        }
    });
});

// texts for webforms
jQuery(function(){
	if (typeof jQuery['cookie'] == 'undefined') {
		jQuery.cookie = function(name, value, options) {
		    if (typeof value != 'undefined') { // name and value given, set cookie
		        options = options || {};
		        if (value === null) {
		            value = '';
		            options.expires = -1;
		        }
		        var expires = '';
		        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
		            var date;
		            if (typeof options.expires == 'number') {
		                date = new Date();
		                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		            } else {
		                date = options.expires;
		            }
		            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		        }
		        // CAUTION: Needed to parenthesize options.path and options.domain
		        // in the following expressions, otherwise they evaluate to undefined
		        // in the packed version for some reason...
		        var path = options.path ? '; path=' + (options.path) : '';
		        var domain = options.domain ? '; domain=' + (options.domain) : '';
		        var secure = options.secure ? '; secure' : '';
		        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
		    } else { // only name given, get cookie
		        var cookieValue = null;
		        if (document.cookie && document.cookie != '') {
		            var cookies = document.cookie.split(';');
		            for (var i = 0; i < cookies.length; i++) {
		                var cookie = jQuery.trim(cookies[i]);
		                // Does this cookie string begin with the name we want?
		                if (cookie.substring(0, name.length + 1) == (name + '=')) {
		                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
		                    break;
		                }
		            }
		        }
		        return cookieValue;
		    }
		};
	}
	
	if (document.location.pathname == '/webforms/posted/' && typeof document.location.hash == 'string') {
		var hash = document.location.hash.replace(/#/, '');
		hash = decodeURIComponent(hash);
		var spl = hash.split("|"); // 0 - from, 1 - status, 2 - fname & lname
		
		var content = jQuery('.b-content_inner').eq(0);
		var html = '';
		
		// регистрация на семинар
		if (spl.length == 3 && spl[0] == '/seo/keyseo/seminar2/') {
			jQuery('title').get(0).text = 'Регистрация на семинар';
			html = '<h1>Регистрация на семинар</h1>';
			html += '<p>';
			if (spl[1] == 0) { // reg unsuccessed
				html += spl[2] + ', просим Вас заполнить все поля, обозначенные звездочкой *!<br />';
				html += 'Пожалуйста, <a href="' + spl[0] + '">вернитесь</a> к началу регистрации.';
			} else { // reg successed
				html += spl[2] + ', регистрация прошла успешно!<br/>';
				html += 'Наш менеджер свяжется с вами для подтверждения вашего участия!';
			}
			html += '</p>';
			html += '<p>Информация о наших услугах - на сайтах:<br />\
			<a href="http://www.bdbd.ru/">bdbd.ru</a> |\
			<a href="http://webprofy.ru/">webprofy.ru</a> |\
			<a href="http://www.mediaguru.ru/">mediaguru.ru</a> |\
			<a href="http://www.webvisor.ru/">webvisor.ru</a> |\
			<a href="http://corpguru.ru/">corpguru.ru</a> |\
			<br />\
			<a href="http://rbsnetwork.ru/">rbsnetwork.ru</a> |\
			<a href="http://www.miralinks.ru/">miralinks.ru</a> |\
			<a href="http://webeffector.ru/">webeffector.ru</a> |\
			<a href="http://www.kongru.ru/">kongru.ru</a> \
			</p>';
			
		} else if (spl.length == 2 && spl[0] == '/about/trolley/') { // троллейбус bdbd.ru

			jQuery('title').get(0).text = 'Где вы видели наш фирменный троллейбус? Вспомните и получите скидку!';
			html = '<h1>Где вы видели наш фирменный троллейбус? Вспомните и получите скидку!</h1>';
			if (spl[1] == 'B') {
				jQuery.cookie('dsc', 'trolley', { path: '/', domain: 'bdbd.ru' }); // set cookie for trolley discount
				html += '<p>Вы правы! Садовое кольцо!!!</p>\
				<p>3% от стоимости продвижения сайта - наш подарок! Активируйте вашу скидку,<br />\
				отправив информацию через форму <a href="/order/">заказа</a>.</p>';
			} else {
				jQuery.cookie('dsc', null); // remove cookie for trolley discount
				var err = [
					'Нет, нет, нет! <a href="' + spl[0] + '">Вспоминаем тщательнее</a>...',
					'Ну, куда же вы жмете, ей богу... <a href="' + spl[0] + '">Еще разочек</a>, будьте любезны...',
					'А вот и не угадали... Вес не взят. <a href="' + spl[0] + '">Новая попытка</a>.',
					''
				];
				var len = err.length - 1;
				var num = parseInt(Math.random() * len, len);
				html += '<p>' + err[num] + '</p>';
			}
			html += '</p>';
			
		}
		
		if (html.length > 0) { content.html(html); }
	}
	
});

