/* form */
var bStatus = 0;
var action = 0;
function showFeedback()
{
	/* анимированное открытие */
	var container = $('container');
	if(bStatus == 0)
	{
		var toHeight = container.offsetHeight;
		container.style.overflow = 'hidden';

//		container.style.display = 'none';
		container.style.visibility = 'visible';
//		container.style.display = 'block';
		if(Opera == true)
		{
				container.style.display = 'none';
				container.style.display = 'block';
				container.style.height = 'auto';
//				document.title = container.offsetHeight;
				container.style.border = 'solid 1px red;';
				bStatus = 1;
		}
		else
		{
		container.style.height = 1 + 'px';
			if(action == 0){
				action = 1;
				var openTween = new Tween(container, 'height', Math.linearTween, 0, toHeight, 5);
			}
			openTween.onMotionFinished = function(){
				container.style.overflow = 'visible';
				container.style.width = container.offsetWidth;
				container.style.className = 'restoreWidth';
				container.style.height = 'auto';
				$('name').focus();
				$('l_v').style.height = container.offsetHeight;
				$('r_v').style.height = container.offsetHeight;
				/* stupid opera */
	//			container.style.visibility = 'visible';
//				container.style.width = container.offsetWidth;
				bStatus = 1;
				action = 0;
			}
		}
		var link = $('linkInner');
		link.className = 'opened';
	}
	else
	{
		closeForm()
		bStatus = 0;
	}
	return false;
}
/* browser detectio */
var Opera = window.opera?true:false;

function closeForm()
{
	if(bStatus == 1)
	{
		var container = $('container');

		if(Opera == true)
		{
			container.style.visibility = 'hidden';
			container.style.display = 'hidden';
			container.style.height = 'auto';
			container.className = 'restoreWidth';
			bStatus = 0;
		}
		else
		{
		container.style.height = container.offsetHeight + 'px';
		container.style.overflow = 'hidden';

			if(action == 0){
				action = 1;
				var openTween = new Tween(container, 'height', Math.linearTween, container.offsetHeight, 0, 5);
			}
			openTween.onMotionFinished = function(){
				container.style.visibility = 'hidden';
				container.style.height = 'auto';
				container.className = 'restoreWidth';
				bStatus = 0;
				action = 0;
			}
		}
		var link = $('linkInner');
		link.className = 'closed';

	}
	return false;
}

function init()
{
	var submit = $('submit');
	submit.disabled = 'true';
	submit.style.color = 'silver';
	var form = new FormWorker($('form'));
	form.testChanges();
	document.body.onclick = function(){
		if(bStatus == 1 && over == 0 && Opera == false){
			closeForm();
		}
	}
	var container = $('container');
	container.onmouseover = function(){
		over = 1;
	}
	container.onmouseout = function(){
		over = 0;
	}
}
var over = 0;
function $(sId)
{
	return document.getElementById(sId);
}

/* FormWorker */
function FormWorker(oForm)
{
	this.form = oForm;
	this.submit = this.takeSubmitButton();
}
FormWorker.prototype = {
	testChanges : function()
	{
		this.giveChangeTestToTags('input');
		this.giveChangeTestToTags('textarea');
	},
	giveChangeTestToTags : function(sTag)
	{
		var fields = this.form.getElementsByTagName(sTag), oThis = this;
		for(var count = 0; count < fields.length; count++){
			if(fields[count].type != 'submit')
			{
				fields[count].onfocus = function()
				{
					this.LastValue = this.value;
//						document.title = 'focus'
						readyState()
				}
				fields[count].onkeyup = function(button)
				{
					if(this.LastValue != this.value)
					{
						button ? (button.which != '13' ? oThis.submit.disabled = false : null) : null
//ie					oThis.submit.disabled = false;
//						document.title = 'up'
						readyState()
					}
				}

			}
		}
	},
	takeSubmitButton : function()
	{
		var fields = this.form.getElementsByTagName('input');
		for(var count = 0; count < fields.length; count++){
			if(fields[count].type == 'submit'){
				return fields[count];
			}
		}
	},
	clearFields : function(sTag)
	{
		var fields = this.form.getElementsByTagName(sTag), oThis = this;
		for(var count = 0; count < fields.length; count++){
			if(fields[count].type != 'submit')
			{
				fields[count].value = null;
			}
		}
	},
	$ : function(sId)
	{
		return document.getElementById(sId)
	}
}

function readyState()
{
	if($('text').value.length == 0 || $('name').value.length == 0)
	{
		$('submit').disabled = true;
		$('submit').style.color = 'silver';
	}
	else
	{
		$('submit').disabled = false;
		$('submit').style.color = 'black';
	}
}

function sendMail()
{
	var r = new xR;
	if($('email').value)
	{
		var string = 'name=' + encodeURI($('name').value) + '&text=' + encodeURI($('text').value) + '&email=' + $('email').value;
	}
	else
	{
		var string = 'name=' + encodeURI($('name').value) + '&text=' + encodeURI($('text').value);
	}
	r.xHttp('/', 'POST', string + '&ajax=on');
	$('submit').disabled = true;
	$('submit').style.color = 'silver';
	$('text').value = '';
	closeForm()
	$('linkInner').innerHTML = 'Спасибо, хотите отправить еще?';
	return false;
}

function xR()
{
	this.init();
}
xR.prototype = {
	init: function()
	{
		this.oR = window.XMLHttpRequest ?  new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null)
	},
	xHttp: function(url, method, string)
	{
		this.oR.open(method, url, true);
		var _this = this;
		function handler(){_this.insert()}
		if(method == 'GET')
		{
			this.oR.send(null);
			this.oR.onreadystatechange = handler;
		}
		if(method == 'POST' && string)
		{
			this.oR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.oR.setRequestHeader("Content-length", string.length);
			this.oR.setRequestHeader("Connection", "close");
			this.oR.send(string);
			this.oR.onreadystatechange = handler;
//			$('Debug').innerHTML = 'Отправлено.'
		}
	},
	insert: function()
	{
//		alert(this.oR)
		if(this.oR.readyState == 4 && this.oR.bStatus == 200){
//			this.div.innerHTML = this.oR.responseText;
		}
//		$('Debug').innerHTML = [this.oR.readyState, this.oR.bStatus];
	}
}
