/***********************************************************************************\
*		JavaScript Modul	:	browser.js
*		Purpose				:	Модуль для проверки корректности заволнения формы комментариев
*       Description			:
*		Author				:	Dmitriy	Soroka
\***********************************************************************************/
/*---------------------------------------
* Function :	checkCommentsForm
* Purpose  :	Проверка корректности заполнения формы комментариев
* Arguments:	none
* Return   :	bool
\--------------------------------------*/
function checkCommentsForm( news_id )
{
	cookie_name = "news_" + news_id;

	if ( getCookie( cookie_name ) )
	{
		alert("Добавление невозможно. Попробуйте повторить операцию через 10 секунд");
		return false;
	}
	author_name		= ( document.getElementById('author_name') ) ? document.getElementById('author_name').value : false;
	author_email	= ( document.getElementById('author_email') ) ? document.getElementById('author_email').value : false;
	comment_text	= ( document.getElementById('comment_text') ) ? document.getElementById('comment_text').value : false;

	var re = /.+/;

	if( !author_name.match(re) ) 
	{
		alert("Введите имя");
		return false;
	} 

	if( !comment_text.match(re) ) 
	{
		alert("Введите комментарий.");
		return false;
	} 

	re1 = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	
	if ( author_email.match(re) && ! author_email.match(re1)) 
	{
		alert('Вы ввели неправильный Email');
		return false;
	}
	
	var today = new Date();
	var expires = new Date();
	expires.setTime(today.getTime() + 1000*10*1);
	setCookie( cookie_name, 1, expires );

	return true;
};


function setCookie(name, value, expire) 
{   
	document.cookie = name + "=" + escape(value)   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

function getCookie(Name) 
{   
	var search = Name + "=";
	if (document.cookie.length > 0) 
	{ 
		// if there are any cookies      
		offset = document.cookie.indexOf(search);       
		if (offset != -1) 
		{ 
			// if cookie exists          
			offset += search.length;          
			// set index of beginning of value         
			end = document.cookie.indexOf(";", offset);          
			// set index of end of cookie value         
			if (end == -1)
			{
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset, end));
		}    
	}

	return false;
}
