function popup(content,title){
  document.getElementById('popup_title').innerHTML=title;
  document.getElementById('msg_form').innerHTML=document.getElementById(content).innerHTML;
  document.getElementById('add_popup_button').innerHTML='<div onClick="document.getElementById(\'authform\').submit()" class="btn">Войти</div>';
  $('fb-modal').fade('in');
}

function deleteRowOrder(i,price,product_id){
  document.getElementById('basket_table_order2').deleteRow(i);
  jQuery.post("/ajax/basket.php",{product_id:product_id,mode:'delete'},function(data){document.getElementById('order_total_price').innerHTML=data;refresh_basket(2);});
}

function change_quantity(product_id){
  document.getElementById('popup_title').innerHTML='Введите количество';
  document.getElementById('add_popup_button').innerHTML='<div class="btn" onclick="add_to_basket('+product_id+',1)">Подтвердить</div>';
  document.getElementById('msg_form').innerHTML=document.getElementById('bquantity_box'+product_id).innerHTML;
  $('fb-modal').fade('in');
}

function deleteRow(i,price,product_id){
  document.getElementById('basket_table').deleteRow(i);
  if(document.getElementById('total_price').innerHTML=='0') document.getElementById('msg_form').innerHTML='Ваша корзина пуста';
  jQuery.post("/ajax/basket.php",{product_id:product_id,mode:'delete'},function(data){document.getElementById('total_price').innerHTML=data;refresh_basket();});
}

function fquantity(i,product_id,ibasket){
	b=ibasket=='1' ? 'b' : '';
  quant=parseInt(document.getElementById(b+'quantity'+product_id).value)+i;
	if(quant>0) document.getElementById(b+'quantity'+product_id).value=quant;
}

function buy(product_id,back_basket){
  document.getElementById('popup_title').innerHTML='Введите количество';
  document.getElementById('add_popup_button').innerHTML='<div class="btn" onclick="add_to_basket('+product_id+','+back_basket+')">Подтвердить</div>';
  document.getElementById('msg_form').innerHTML=document.getElementById('quantity_box'+product_id).innerHTML;
  disable_background(true);
  $('fb-modal').fade('in');
}

function refresh_basket(back_basket){
  jQuery.post("/ajax/basket.php",{mode:'info'},
   function(data){
   	  responce=data.split('|');
      /*document.getElementById('basket_count').innerHTML=responce[0];*/
   	  /*document.getElementById('basket_summa').innerHTML=responce[1];*/
   	  if(back_basket=='2'){
   	  	document.getElementById('order_total_price').innerHTML=responce[1];
   	  	if(responce[0]=='0') document.getElementById('pre_order').innerHTML='Ваша корзина пуста';
   	  }else{
   	  	if(responce[0]=='0') document.getElementById('msg_form').innerHTML='Ваша корзина пуста';
   	  }
  });
}

function add_to_basket(product_id,back_basket){
	b=back_basket=='1' ? 'b' : '';
  var quantity=document.getElementById(b+'quantity'+product_id).value;
  document.getElementById('popup_title').innerHTML='Корзина';
  document.getElementById('msg_form').innerHTML='Загрузка...';
  document.getElementById('add_popup_button').innerHTML='';
  jQuery.post("/ajax/basket.php",{product_id:product_id,mode:'add',quantity:quantity},
   function(data){
     if(back_basket=='2'){
     	document.getElementById('item_quantity'+product_id).innerHTML=quantity;
     	document.getElementById('item_summa'+product_id).innerHTML=parseInt(quantity)*parseInt(document.getElementById('item_price'+product_id).innerHTML);
     	$('fb-modal').fade('out');
     	disable_background(false);
     }else if(back_basket!='1') document.getElementById('msg_form').innerHTML=data; else show_basket();
     refresh_basket(back_basket);
  });
}

function show_basket(){
  document.getElementById('popup_title').innerHTML='Корзина';
  document.getElementById('msg_form').innerHTML='Загрузка...';
  $('fb-modal').fade('in');
  jQuery.post("/ajax/basket.php", {mode:'show'},
   function(data){
     if(data!='Ваша корзина пуста') document.getElementById('add_popup_button').innerHTML='<div class="btn" onclick="clear_basket()">Очистить</div>';
     document.getElementById('msg_form').innerHTML=data;
  });
}

function clear_basket(back_basket){
  jQuery.post("/ajax/basket.php", {mode:'clear'},
   function(data){
     if(back_basket=='2') document.getElementById('pre_order').innerHTML='Ваша корзина пуста';
     else{
       document.getElementById('msg_form').innerHTML='Ваша корзина пуста';
       document.getElementById('add_popup_button').innerHTML='';
     }
     refresh_basket();
  });
}

function open_close(div_id,i){
  if (!document.getElementById(div_id)) {
      return;
  }
  if(document.getElementById(div_id).style.display=='none'){
  	document.getElementById(div_id).style.display='block';
  	document.getElementById('arrow'+div_id).src='/images/treed'+i+'.gif';
  }else{
  	document.getElementById(div_id).style.display='none';
  	document.getElementById('arrow'+div_id).src='/images/treer'+i+'.gif';
  }
}

function disable_background(turn){
	document.getElementById('popupBackgroundFilter').style.height = getDocumentHeight() +'px';
	if(turn) document.getElementById('popupBackgroundFilter').style.display='block';
	else document.getElementById('popupBackgroundFilter').style.display='none';
}

function getDocumentHeight(){
  if(jQuery.browser.msie){
      var $temp = jQuery("<div>")
      .css("position","absolute")
      .css("left","-10000px")
      .append(jQuery("body").html());

      jQuery("body").append($temp);
       var h = $temp.height();
       $temp.remove();
       rh =  h;
   }
   else {
        rh = jQuery("body").height();
   }
   h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
   if (rh < h) {
       rh = h;
   }
   return rh;
}

function popup_icon(content,title){
  document.getElementById('popup_title').innerHTML=title;
  document.getElementById('msg_form').innerHTML=document.getElementById(content).innerHTML;
  document.getElementById('add_popup_button').innerHTML='';
  $('fb-modal').fade('in');
}

function question_post(){
  var question=document.getElementById('question').value;
  var iagree=document.getElementById('iagree').checked ? 1 : 0;
  if(question=='' || question=='Введите ваш вопрос'){
    alert('Поле заполнено некорректно');
  }else{
    document.getElementById('question_form').style.display='none';
    document.getElementById('question_result').innerHTML='<div align="left">Загрузка...</div>';
    jQuery.post("/ajax/general.php", {mode:'question',text:question,iagree:iagree},
      function(data){
        document.getElementById('question_result').innerHTML='<div align="left"><b>Большое спасибо за Ваш вопрос !</b><br />Вы будете проинформированы о получении ответа в ближайшее время.</div>';
        document.getElementById('questions').innerHTML=data;
        setTimeout(function(){
        	document.getElementById('question_form').style.display='block';
        	document.getElementById('question_result').innerHTML='';
        	document.getElementById('question').value='Введите ваш вопрос';
        },3000);
      }
    );
  }
}

function textCount(auth){
    if(auth){
        box = document.getElementById("comment_text").value;        
        if(box.length > 450){
        	document.getElementById('add_popup_text_count').innerHTML = 'Превышенно допустимое число символов';
			document.getElementById("comment_text").value = box.substring(0,450);
        }
        else{
        	document.getElementById('add_popup_text_count').innerHTML = 'Вы ввели: ' + box.length + ' символов.';
        }
    }
}


function comment_popup(content,title,auth){
  if(auth){
    document.getElementById('popup_title').innerHTML=title;
    document.getElementById('add_popup_text_count').innerHTML = 'Вы ввели: 0 символов';
    document.getElementById('msg_form').innerHTML=document.getElementById(content).innerHTML;
    document.getElementById('add_popup_button').innerHTML='<div onClick="comment_post()" class="btn">Отправить</div>';
  }else{
    $('pop_content').addEvent('click',function(e) {
        $('fb-modal').fade('out');
    });
  	document.getElementById('msg_form').innerHTML='Форма доступна только для зарегистрированных пользователей';
  	document.getElementById('add_popup_button').innerHTML='';
  }
  $('fb-modal').fade('in');
}

function comment_post(){
  var comment_text=jQuery('#comment_text').val();
  var product_id=jQuery('#product_id').val();
  if(comment_text==''){
    alert('Введите текст');
  }else{
    jQuery('#msg_form').css('display','none');
    jQuery('#msg_result').html('<div align="left">Загрузка...</div>');
    jQuery.post("/ajax/general.php", {mode:'comment',text:comment_text,product_id:product_id},
      function(data){
        if(data === 'false'){
           jQuery('#msg_result').html('<div align="left"><b style="color:red;">Ваш отзыв не будит опубликован на сайте, так как вы вышли!</b></div>');
           jQuery('#add_popup_button').html(''); 
        }else{
           jQuery('#msg_result').html('<div align="left">Ваш отзыв появится на сайте, после одобрения модератором</div>');
           jQuery('#add_popup_button').html(''); 
        }
        
        setTimeout(function(){
        	$('fb-modal').fade('out');
        	jQuery('#msg_form').css('display','block');
        	jQuery('#msg_result').html('');
        	jQuery('#comment_text').val('');
        },3000);
      }
    );
  }
}

function all_comments(product_id){
  jQuery('#popup_title').html('Все отзывы:');
  jQuery('#add_popup_button').html('');
  jQuery('#msg_form').html('<div align="left">Загрузка...</div>');
  $('fb-modal').fade('in');
  jQuery.post("/ajax/general.php", {mode:'allcomments',product_id:product_id},
    function(data){
    	jQuery('#msg_form').html(data);
    }
  );
}

function profile_popup(content,title,auth){
  if(auth){
    location.href='/club/?mode=profile';
  }else{
    document.getElementById('popup_title').innerHTML=title;
  	document.getElementById('msg_form').innerHTML='Форма доступна только для зарегистрированных пользователей';
  	document.getElementById('add_popup_button').innerHTML='';
 	  $('fb-modal').fade('in');
  }
}

function set_bio(val){
	for(var i=1;i<4;i++) document.getElementById('bio'+i).src='/images/unckeck.gif';
	document.getElementById('bio'+val).src='/images/check.gif';
	document.getElementById('bio').value=val;
}

function bio_agree(){
	agree=document.getElementById('agree');
	if(agree.value=='1'){
		document.getElementById('bioagree').src='/images/unckeck.gif';
		document.getElementById('agree').value=0;
	}else{
		document.getElementById('bioagree').src='/images/check.gif';
		document.getElementById('agree').value=1;
	}
}

function profile_post(edit){
  var err='';
  name=jQuery('#name').val();
  surname=jQuery('#name').val();
  interested=jQuery('#interested').val();
  photo=jQuery('#photo').val();
  experience=jQuery('#experience').val();
  like_product=jQuery('#like_product').val();
  motivation=jQuery('#motivation').val();
  innovations=jQuery('#innovations').val();
  thought=jQuery('#thought').val();
  agree=jQuery('#agree').val();
  ed_err=0;

  if(!edit && photo==''){
  	ed_err=1;
  }

  if(ed_err || name=='' || surname=='' || interested=='' || experience=='' || like_product=='' || motivation=='' || innovations=='' || thought==''){
  	err='- Заполнены не все обязательные поля формы<br />';
  }
  if(agree=='0'){
  	err+='- Вы должны подтвердить, что являетесь владельцем прав на использование фотографии';
  }

  if(err!=''){
    document.getElementById('popup_title').innerHTML='ОШИБКА';
  	document.getElementById('msg_form').innerHTML=err;
  	document.getElementById('add_popup_button').innerHTML='';
 	  $('fb-modal').fade('in');
  }else{
  	document.getElementById('profileforma').submit();
  }
}

function pofile_info(id){
  if(jQuery('#profile'+id+'_info').css('display')=='none'){
  	 jQuery('#profile'+id+'_info').css('display','block');
  	 jQuery('#profile'+id+'_photo').css('display','none');
  }else{
  	 jQuery('#profile'+id+'_info').css('display','none');
  	 jQuery('#profile'+id+'_photo').css('display','block');
  }
}

function showQuestions(id){
    var id = '#id_'+id;
    jQuery('.q_row').hide('slow');
    if(jQuery(id).css('display') == 'none')
        jQuery(id).show('slow');  
    else
        jQuery(id).hide('slow');
}
