function popup_window( id, width, height ) {
	if (!width) width = 900;
	if (!height) height = 800;
	wnd = window.open( "/prod_popup.php?id=" + id, "_blank", "width="+width+",height="+height+",status=1,toolbar=0,resizable=1,scrollbars=1,menubar=1");
	wnd.focus();
}

function std_popup_window( url, width, height ) {
	if (!width) width = 300;
	if (!height) height = 200;
	wnd = window.open( url, "_blank", "width="+width+",height="+height+",status=0,toolbar=0,resizable=0,scrollbars=0,menubar=0");
	wnd.focus();
}

function popup_news_window( id, width, height ) {
	if (!width) width = 900;
	if (!height) height = 800;
	wnd = window.open( "/news_popup.php?id=" + id, "_blank", "width="+width+",height="+height+",status=1,toolbar=0,resizable=1,scrollbars=1,menubar=1");
	wnd.focus();
}

function max_popup_window( url ) 
{
	wnd = window.open( url, "_blank", "width="+screen.width+",height="+screen.height+",status=0,toolbar=0,resizable=1,scrollbars=1,menubar=0");
	wnd.focus();
}

function show_div( id )
{
	close_all();

	s = document.getElementById(id).style;

	if (s.display == 'block')
	{
		s.display = 'none';
	}
	else 
	{
		s.display = 'block';
		document.location.replace( "#"+id );
		//scrollTo(id);
	}
}

function close_all()
{
	s = document.getElementById('user_data').style;
	s.display = 'none';
	s2 = document.getElementById('add_pic').style;
	s2.display = 'none';
}

function open_poll( one ){
	act = document.pollform.act.value;
	poll_id = document.pollform.poll_id.value;
	
	if( one == "1" ) { // radio
		q = "&q=" + get_radio_value( document.pollform.q );
	}
	
	if( one == "0" ) { // checkbox
		q = get_checkbox_values( document.pollform ); 
	}
	
	wnd = window.open( "/poll_action.php?act="+act+"&poll_id=" + poll_id + q, "_blank", "width=300,height=150,status=0,toolbar=0,resizable=0,scrollbars=0,menubar=0");
	wnd.focus();
}

function open_claim(){
	name = document.claimform.name.value;
	email = document.claimform.email.value;
	phone = document.claimform.phone.value;
	comment = document.claimform.comment.value;
		
	wnd = window.open( "/claim_action.php?name="+name+"&email=" +email+"&phone=" +phone+"&comment=" +comment, "_blank", "width=300,height=150,status=0,toolbar=0,resizable=0,scrollbars=0,menubar=0");
	wnd.focus();
}

function get_radio_value( obj ) {
	var i=0; var q=0;
    while((i<obj.length)&&(obj[i].checked!=1)) {i++;}
   	if (i!=obj.length) q =obj[i].value;
	return q;
}

function get_checkbox_values( obj )
{
 	var ret="";

	for( i = 0; i < obj.elements.length; i++ )
	{
		var item=obj.elements[i];
		if( item.checked==true && item.value > 0 ) ret = ret + "&q[]=" + item.value;
	}
	return ret;
}

function open_compare(product_id) {
	wnd = window.open( '/compare.php?action=add&id=' + product_id,  '_compare' ); 
	wnd.focus();
}


imgname = "";
description = "";

function start_window(img, desc, width, height){

	nw = window.open("./popup_map.html", "_blank", "width="+width+",height="+height+",status=no,toolbar=no,menubar=no");
    imgname=img;
    description=desc;
}

function checkReg() 
{
	var error = '';
	var obj = document.regform;
	if ( obj.cat_id.value <= 0 ) error += "Вы должны выбрать категорию для вашего сайта!\n";
	if ( obj.url.value == '' ) error += __warning_empty( 'URL cайта' );
	if ( obj.sname.value == '' ) error += __warning_empty( 'Название cайта' );
	if ( obj.sdescr.value == '' ) error += __warning_empty( 'Описание cайта' );
	if ( obj.fio.value == '' ) error += __warning_empty( 'Ф.И.О. контактного лица' );
	if ( obj.email.value == '' ) error += __warning_empty( 'E-mail' );
	if ( obj.passwd1.value == '' ) error += __warning_empty( 'Пароль' );
	if ( obj.passwd2.value == '' ) error += __warning_empty( 'Пароль, подтверждение' );
	if ( obj.passwd1.value != obj.passwd2.value ) error += "Пароли не совпадают!\n";
	
	return __check( error, obj );
}

function checkLogin() 
{
	var error = '';
	var obj = document.loginform;
	if ( obj.url.value == '' ) error += __warning_empty( 'URL cайта' );
	if ( obj.passwd.value == '' ) error += __warning_empty( 'Пароль' );
	
	return __check( error, obj );
}

function checkForgot() 
{
	var error = '';
	var obj = document.forgotform;
	if ( obj.url.value == '' ) error += __warning_empty( 'URL' );
	
	return __check( error, obj );
}

(function () {
    window.checkPhoto = function() {
       	var error = '';
		var obj = document.picform;
		if ( obj.name.value == '' ) error += __warning_empty( 'Название фотографии' );
		if ( obj.name.lenght > 254 ) error += __warning_len( 'Название фотографии', 254 );
		if ( obj.description.value == '' ) error += __warning_empty( 'Комментарий автора' );
		if ( obj.cat_id.value == '' || obj.cat_id.value <= 0 ) error += __warning_empty( 'Раздел галереи' );
		return __check( error, obj );
    };
})();

function checkTopic() 
{
	var error = '';
	var obj = document.topicform;
	if ( obj.title.value == '' ) error += __warning_empty( 'Заголовок' );
	if ( obj.comment.value == '' ) error += __warning_empty( 'Сообщение' );
	
	return __check( error, obj );
}

function topicPreview()
{
	var error = '';
	var obj = document.topicform;
	
	document.topicform.action.value = "preview";
	document.topicform.submit();
}

function checkPost() 
{
	var error = '';
	var obj = document.postform;
	if ( obj.title.value == '' ) error += __warning_empty( 'Заголовок' );
	if ( obj.comment.value == '' ) error += __warning_empty( 'Сообщение' );
	
	return __check( error, obj );
}

function postPreview()
{
	var error = '';
	var obj = document.postform;
	
	document.postform.action.value = "preview";
	document.postform.submit();
}

function goForum() 
{
	var error = '';
	var forum_id = document.selforums.forum_id.value;
	
	document.location.href = "/forum/topics.php?forum_id="+forum_id;
}

function __warning_empty( name ) {
	return ' - Поле \'' + name + '\' не может быть пустым!\n';
}

function __warning_len( name, len ) {
	return ' - В поле \'' + name + '\' не может быть больше ' + len + ' символов!\n';
} 

function __check( error, object ) {
	if ( error != '' ) { alert( 'Внимание!\n\n' + error ); return false; } 
	else { return true; }
}

