function checkform ( obj ){
  for ( var i=0; i<obj.elements.length; i++ )
  {
      if ( obj.elements[i].getAttribute( "required" ) != null )
      {
          if ( obj.elements[i].value == '' )
          {
                alert('Не заполнено обязательное поле!');
                return false;
          }
      }
  }
  return true;
}

function vote(brokID, voice){
	$.get("app/vote.php", { brokID: brokID, voice: voice },
			function(data){
				if(data == 1){
					alert("Ваш голос принят!");
					location.href=host;
				}
				else if(data == 2) alert("Вы не можете повторно голосовать!");				
			}
		);
}

function showAd(){
	var title = $("#ad_title").attr("value");
	var comm = $("#comm").attr("value");
	var ch_category =  $('#ch_category').val() || [];
	ch_category = ch_category.join(";");
	var ch_region =  $('#ch_region').val() || [];	
	ch_region = ch_region.join(";");
	var amount = $("#amount").attr("value"); 
	var oborot = $("#oborot").attr("value"); 
	var profit = $("#profit").attr("value"); 
	 
	var link = host+'app/show_ad.php?title='+encodeURIComponent(title)+'&comm='+encodeURIComponent(comm)+'&ch_category='+encodeURIComponent(ch_category)+'&ch_region='
				+encodeURIComponent(ch_region)+'&amount='+encodeURIComponent(amount)+'&oborot='+encodeURIComponent(oborot)+'&profit='+encodeURIComponent(profit);
	myWin = window.open( link, "displayWindow", "width=600,height=600,status=no,resizable=no,scrollbars=1,toolbar=no,menubar=no");  	
	myWin.document.close();
}

function onSubFormReg(){
 if(document.form_reg.login.value == 0){
   window.alert('Введите Логин!!!');
   return false;
 }
 else
 if(document.form_reg.password.value == 0){
   window.alert('Введите Пароль!!!');
   return false;
 }
 else
 if(document.form_reg.passpod.value == 0){
   window.alert('Введите Пароль подтверждения!!!');
   return false;
 }
 else
 if(document.form_reg.email.value == 0){
   window.alert('Введите e-mail!!!');
   return false;
 }
 else
 if(document.form_reg.code.value == 0){
   window.alert('Введите code!!!');
   return false;
 }
 else {
  var pass, passp;
  pass = document.form_reg.password.value;
  passp = document.form_reg.passpod.value;
  if(pass != passp){
   window.alert('Пароль не подтвержден!!!');
   return false;
   }
   else {
    return true;
   }
 }     
}


function onSubFormEdit(){
 if(document.form_reg.login.value == 0){
   window.alert('Введите Логин!!!');
   return false;
 }
 else
 if(document.form_reg.name.value == 0){
   window.alert('Введите Имя!!!');
   return false;
 }
 else
 if(document.form_reg.email.value == 0){
   window.alert('Введите e-mail!!!');
   return false;
 }
}

function onSubUpdPas(){
 if(document.form_pas.oldpassword.value == 0){
   window.alert('Введите старый PASSWORD!!!');
   return false;
 }
 else
 if(document.form_pas.password.value == 0){
   window.alert('Введите новый PASSWORD!!!');
   return false;
 }
 else
 if(document.form_pas.passpod.value == 0){
   window.alert('Введите PASSWORD подтверждения!!!');
   return false;
 }
 else {
  var pass, passp;
  pass = document.form_pas.password.value;
  passp = document.form_pas.passpod.value;
  if(pass != passp){
   window.alert('PASSWORD не подтвержден!!!');
   return false;
   }
   else {
    return true;
   }
 }
}

$(function(){
	$(".head_menu").hover(function() {
		var block = $(this);
		if (!block.hasClass('vis')){
			block.find(".sub_menu").toggle();
		}
			
	}, function() {
		var block = $(this);
		if (!block.hasClass('vis')){
                block.find(".sub_menu").toggle();
               // block.find(".sub_menu").slideToggle('fast');
		}
	});	
	//фон бокса
	//setTimeout(function() {$(".box").height($(document).height()) }, 1);
});


function is_int( mixed_var ){
	var y = parseInt(mixed_var * 1);  
	if (isNaN(y)) {  
		return false;
	}
	return mixed_var == y && mixed_var.toString() == y.toString();
} 

function onSubCalc(){
	var res = true;
	if(!is_int(document.form_calc.pribul.value)){
		$('#pribul').html('(!)');
		res = false;
	}
	else $('#pribul').html('');
 	
	if(!is_int(document.form_calc.dohod.value)){
		$('#dohod').html('(!)');
		res = false;
	}
	else $('#dohod').html('');
	
	if(!is_int(document.form_calc.mat_activi.value)){
		$('#mat_activi').html('(!)');
		res = false;
	}
	else $('#mat_activi').html('');
	
	if(!is_int(document.form_calc.nemat_activi.value)){
		$('#nemat_activi').html('(!)');
		res = false;
	}
	else $('#nemat_activi').html('');
	
	if(!res){
		window.alert('Введите данные корректно!!!');
		return false;
	}else{
		return true;
	}
}

function nextBan(thisObj){
		obj=$(thisObj).parents();
		var idAr=obj.attr('id').split(/[_]/);
		if($("#"+idAr[0]+"_"+(parseInt(idAr[1])+1)).length)
		{
		
			$("#"+idAr[0]+"_"+idAr[1]).css('display','none');
			$("#"+idAr[0]+"_"+(parseInt(idAr[1])+1)).css('display','inline');
		}
		else
		{
			$("#"+idAr[0]+"_"+idAr[1]).css('display','none');
			$("#"+idAr[0]+"_0").css('display','inline');
		}
}

function ch_stat1(n){
	if(n == 'w'){
		$("#stat1_month").css('display','none');
		$("#stat1_3month").css('display','none');
		$("#stat1_week").css('display','block');
		$("#stat1_butt1").css('background','#FFFFFF');
		$("#stat1_butt2").css('background','none');
		$("#stat1_butt3").css('background','none');
	}
	else if(n == 'm3'){
		$("#stat1_month").css('display','none');
		$("#stat1_3month").css('display','block');
		$("#stat1_week").css('display','none');
		$("#stat1_butt3").css('background','#FFFFFF');
		$("#stat1_butt1").css('background','none');
		$("#stat1_butt2").css('background','none');
	}
	else{
		$("#stat1_month").css('display','block');
		$("#stat1_week").css('display','none');
		$("#stat1_3month").css('display','none');
		$("#stat1_butt1").css('background','none');
		$("#stat1_butt2").css('background','#FFFFFF');
		$("#stat1_butt3").css('background','none');
	}
}

function ch_stat2(n){
	if(n == 'w'){
		$("#stat2_month").css('display','none');
		$("#stat2_3month").css('display','none');
		$("#stat2_week").css('display','block');
		$("#stat2_butt1").css('background','#EED37A');
		$("#stat2_butt2").css('background','none');
		$("#stat2_butt3").css('background','none');
	}
	else if(n == 'm3'){
		$("#stat2_month").css('display','none');
		$("#stat2_3month").css('display','block');
		$("#stat2_week").css('display','none');
		$("#stat2_butt1").css('background','none');
		$("#stat2_butt2").css('background','none');
		$("#stat2_butt3").css('background','#EED37A');
	}
	else{
		$("#stat2_month").css('display','block');
		$("#stat2_3month").css('display','none');
		$("#stat2_week").css('display','none');
		$("#stat2_butt1").css('background','none');
		$("#stat2_butt2").css('background','#EED37A');
		$("#stat2_butt3").css('background','none');
	}
}

function cnt(id, type, url){
	if(id > 0){
		 $.ajax({
		   type: "POST",
		   url: host+"app/cnt.php",
		   data: "id="+id+"&type="+type,
		   success: function($msg){
			//alert( "Data Saved: "+$msg);
			if(url == "" || url == undefined){
				location.href=host+"catalog/"+id+"/index.html";
			}
			else{
				location.href=url;
			}
		   }
		 });
		 
	}
	if(url != "" && url != undefined){
		location.href=url;
	}
}

function getBrowserInfo(){
 var t,v = undefined;
 if (window.opera) t = 'Opera';
 else if (document.all) {
  t = 'IE';
  var nv = navigator.appVersion;
  var s = nv.indexOf('MSIE')+5;
  v = nv.substring(s,s+1);
 }
 else if (navigator.appName) t = 'Netscape';
 return {type:t,version:v};
}

function bookmark(a){
 var url = window.document.location;
 var title = window.document.title;
 var b = getBrowserInfo();
 if (b.type == 'IE' && b.version >= 4) window.external.AddFavorite(url,title);
 else if (b.type == 'Opera') {
  a.href = url;
  a.rel = "sidebar";
  a.title = url+','+title;
  return true;
 }
 else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
 else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
 return false;
}

var now_pos1 = 1;
var now_pos2 = 3;
function more_novelties(cntBiz){
	for(var i=now_pos1; i<=now_pos2; i++){
		$("#biz"+i).css('display','none');
	}
	
	if(now_pos2<cntBiz) now_pos1 = now_pos2+1;
	else now_pos1 = 1;
	
	now_pos2 = now_pos1+2;
	if(now_pos2>cntBiz) now_pos2 = cntBiz;

	for(var i=now_pos1; i<=now_pos2; i++){
		$("#biz"+i).css('display','block');
	}	
}


/** COMM */

function str_replace(substr,newsubstr,str) {
 replace_count=0;
 while (str.indexOf(substr)>=0) {
   str=str.replace(substr,newsubstr);
   replace_count++;
 }
 return(str);
}

function editComm(id){	
	$('#link_'+id).html("<a class='regist_ob' href=\"javascript:saveComm('"+id+"');\">Сохр.</a>");
	$('#comm_'+id).html("<textarea id='comm_inp_"+id+"' cols='55' rows='5'>"+str_replace("<BR>","",str_replace("<br\>","",$('#comm_'+id).html()))+"</textarea>");
}

function updatePageComm(data) {

	  if(data != 0){
		$('#link_'+id_p).html("<a class='regist_ob' href=\"javascript:editComm('"+id_p+"');\">Ред.</a>");
		$('#comm_'+id_p).html(data);
	  }
}

function saveComm(id){
     id_p = id;
   $.post(host+"app/edit_comm.php",{id:id_p,comm:encodeURIComponent($("#comm_inp_"+id).val())},updatePageComm);
}

function updatePageDel(data) {
	  if(data == 1){
			$("#comment"+id_p).html('');
	  }
}

function delComm(id){
	id_p = id;
	$.post(host+"app/del_comm.php",{id:id_p},updatePageDel);
}

function showComm(){
	$("#allcomments").css('display','block');
	$("#showcomm").html('<a href="javascript:void(0);" onClick="javascript:noneComm();">Cвернуть</a>');
}

function noneComm(){
	$("#allcomments").css('display','none');
	$("#showcomm").html('<a href="javascript:void(0);" onClick="javascript:showComm();">Показать все</a>');
}

function submitForm(fName){
	$(fName).submit();
}
function imposeMaxLength(Object, MaxLen, event){
  var key = event.which;
  var res = (Object.value.length <= MaxLen ? true : false) ;
  if( res ){
	$("#txtleng").html(Object.value.length);
  }
  //alert(key);
  if(key == 13 || key == 0 || key == 8){
  
  	res = true;
  }
	
  return res;
}

