function setEmail()
{
	var priceField = $("#email-box");
	if(priceField.length == 0)
		return;

	var html = "zakaz";
	html = html + "@";
	html = html + "happiness-shop.ru";

	priceField.html(html);
	priceField.attr("href", "mailto: " + html)
}


function showComment(id, show)
{
	var show_container = document.getElementById("ob_show_"+id);
	var hide_container = document.getElementById("ob_hide_"+id);
	
	if(show) 	{
		show_container.className = '';
		hide_container.className = 'hidden';
	} else {
		show_container.className = 'hidden';
		hide_container.className = '';
	}
}

function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function showInfoMsg(content) 
{
	var msg_block = document.getElementById("info_msg");
	msg_block.innerHTML = content;
}

/* FAQ-block controller */
FaqForm = function() { this.constructor(); };
FaqForm.prototype = {
	constructor: function() {
		this.infoContainer = $("#faq_info");
		
		this.questionField = new FieldLabel($("#faq_question"), "Не можете определиться, какой подарок подойдет для ваших близких?\n\nСпросите нас - мы ответим!");
		this.emailField = $("#faq_email");
		
		this.sendButton = $("#send_button");
		this.sendButton.click($.proxy(this.sendQuestion, this));
	},
	
	sendQuestion: function()
	{
		if(this.validateForm() == false)
		{
			this.showError("Необходимо корректно заполнить поля.");
			return;
		}
		
		$.ajax({
			url: "/ajax/faq_sendQuestion.php",
			data: ({question: this.questionField.Value(), email: this.emailField.val()}),
			success: $.proxy(function(result){
				switch(result) {
					case "invalid":
						this.showError("Необходимо заполнить все поля.");
						break;
					case "ok":
						this.showSuccess("Вопрос отправлен.");
						break;
					default:
						this.showError("К сожалению произошла ошибка.");
						break;
				}
			},this),
			error: $.proxy(function(error){
				this.showError("К сожалению произошла ошибка.");
			},this)
		});
	},

	validateForm: function() {
		if(!this.questionField.Value()|| !this.emailField.val())
			return false;
		
		if(!validateEmail(this.emailField.val()))
			return false;
		
		return true;
	},
	
	showSuccess: function(text) {
		this._resetInfo();
		this.infoContainer.addClass("success");	
		this.infoContainer.text(text);
		this.infoContainer.show();		
	},
	
	showError: function(text) {
		this._resetInfo();
		this.infoContainer.addClass("error");	
		this.infoContainer.text(text);
		this.infoContainer.show();
	},
	
	_resetInfo: function() {
		if(this.infoContainer.hasClass("success"))
			this.infoContainer.removeClass("success");
		if(this.infoContainer.hasClass("error"))
				this.infoContainer.removeClass("error");
		this.infoContainer.text("");
		this.infoContainer.hide();
	}
};

FieldLabel = function() { 
	this.constructor.apply(this, arguments); 
};
FieldLabel.prototype = {
	constructor: function(field, label) {
		
		this.field = field;
		this.label = label || "Введите значение";
		
		this.value = null;
		this.showLabel();
		
		this.field.focusin($.proxy(this.onFocusIn,this));
		this.field.focusout($.proxy(this.onFocusOut,this));
	},
	
	Value: function() {
		return this.value;
	},
	
	hideLabel: function()
	{
		this.field.val("");
		this.field.css("color", "#353535");
	},
	
	showLabel: function()
	{
		this.field.val(this.label);
		this.field.css("color", "gray");
	},
	
	onFocusIn: function()
	{
		var fieldValue = this.field.val();
		if(this.value == null) {
			this.hideLabel();
		}
	},
	
	onFocusOut: function()
	{
		var fieldValue = this.field.val();
		if(fieldValue == "") {
			this.showLabel();
			this.value = null;
		} else {
			this.value = fieldValue;
		}	
	}
};
