nav = navigator.appName.toLowerCase();
var isOpera = 'opera'==nav;
var isIE = !('opera'==nav||'netscape'==nav);
var isFF = 'netscape'==nav;


function add_basket(type, id, file_id){
	JsHttpRequest.query('/ajax/ajax.php', {'action': 'add_basket', 'type': type, 'id': id, 'file_id': file_id},
    function(result, errors) {
    	if (errors){
			alert(errors);
    	} else {
    		if (result['result']){
    			load_basket();
    		} else {
    			alert("Ошибко");
			}
		}
	}, true);
   	return true;
}

function remove_basket(type, id){
	JsHttpRequest.query('/ajax/ajax.php', {'action': 'remove_basket', 'type': type, 'id': id},
    function(result, errors) {
    	if (errors){
			alert(errors);
    	} else {
    		if (result['result']){
    			alert("Ok");
    		} else {
    			alert("Ошибко");
			}
		}
	}, true);
   	return true;
}


function load_basket(lang){

	var base = document.getElementById('top_basket');

	if (lang=='ru')
	{
	var txt_basket = "Корзина";
	var txt_empty = "Корзина пустая";
	var txt_goods = "Товаров ";
	var txt_change = " изменить ";
	var txt_checkout = "| купить";
	}
	else
	{
	var txt_basket = "Кошик";
	var txt_empty = "Кошик пустий";
	var txt_goods = "Товарiв ";
	var txt_change = " змiнити ";
	var txt_checkout = "| купити";
	}

	while(base.firstChild){base.removeChild(base.firstChild);} // Очистка

	JsHttpRequest.query('/ajax/ajax.php', {'action': 'get_basket'},
    function(result, errors) {
    	if (errors){
    		alert(errors);
			base.appendChild(document.createTextNode(txt_empty));
    	} else {

    			/* картинка "корзина" */
    			t = document.createElement("IMG");
    			t.setAttribute("src", "img/telega.gif");
			t.setAttribute("border", "0");
			t.setAttribute("align", "absmiddle");
			t.setAttribute("hspace", "5");
    			base.appendChild(t);

    		if (result['result']){

    			/* Надпись "корзина" */
    			/* t = document.createElement("SPAN");
    			t.appendChild(document.createTextNode(txt_basket));
    			if (isIE) {t.setAttribute("className","basket_name");}
				else {t.setAttribute("class","basket_name"); }
    			base.appendChild(t);

			*/

    			/* Товары  */
    			t = document.createElement("SPAN");
    			t.appendChild(document.createTextNode(txt_goods));
    			count_goods = document.createElement("B");
    			count_goods.appendChild(document.createTextNode(result['result']['total']['total_count']));
    			t.appendChild(count_goods);
    			if (isIE) {t.setAttribute("className","basket_goods");}
				else {t.setAttribute("class","basket_goods"); }
    			base.appendChild(t);

    			/* Сумма */
    			t = document.createElement("SPAN");
    			t.appendChild(document.createTextNode(' - '+result['result']['total']['sum']+' грн.'));
    			if (isIE) {t.setAttribute("className","basket_sum");}
				else {t.setAttribute("class","basket_sum"); }
    			base.appendChild(t);

    			/* Кнопки */
    			t = document.createElement("SPAN");
    			change = document.createElement("A");
    			change.href = "/change_basket.php";
    			change.appendChild(document.createTextNode(txt_change));
    			t.appendChild(change);

    			checkout  = document.createElement("A");
    			checkout.href = "/checkout.php";
    			checkout.appendChild(document.createTextNode(txt_checkout));
    			t.appendChild(checkout);
    			if (isIE) {t.setAttribute("className","basket_links");}
				else {t.setAttribute("class","basket_links"); }
    			base.appendChild(t);


    		} else {
			t = document.createElement("SPAN");
    			t.appendChild(document.createTextNode(txt_empty));
    			if (isIE) {t.setAttribute("className","basket_goods");}
				else {t.setAttribute("class","basket_goods"); }
    			base.appendChild(t);
			}
		}
	}, true);

}

function add_subscribe(type, id, file_id){
	location.href='bookshelf.php?oper='+type+'&magazin_id='+id+'&period='+file_id;
}
function opennewsletter(type, book_id){
	emailwindow = dhtmlmodal.open('Покупка', 'iframe', '/files.php?id='+book_id+'&type='+type, '', 'width=100px,height=90px,padding-left=0px,padding-top=0px,center=1,resize=0,scrolling=0')
	emailwindow.onclose=function(){ //Define custom code to run when window is closed
		var theform=this.contentDoc.forms[0] //Access first form inside iframe just for your reference
		var thevalue=this.contentDoc.getElementById("file_id") //Access form field with id="emailfield" inside iframe
		var thetype=this.contentDoc.getElementById("file_type") //Access form field with id="emailfield" inside iframe
		if (thevalue.value==0){
			return true;
		} else {
			if (thetype.value=='download'){
				location.href='download.php?bf_id='+thevalue.value;
			} if (thetype.value=='bookcase') {
				location.href='bookshelf.php?oper=add&bf_id='+thevalue.value;
			} if (thetype.value=='buy'){
				add_basket('book', book_id, thevalue.value );
			}
			return true //allow closing of window
		}
		return true //allow closing of window
	}
}
function opennewsletter2(type, book_id){
	emailwindow = dhtmlmodal.open('Покупка', 'iframe', '/files_e12.php?id='+book_id+'&type='+type, '', 'width=620px,height=100%,center=1,resize=0,scrolling=0')
	emailwindow.onclose=function(){ //Define custom code to run when window is closed
		var theform=this.contentDoc.forms[0] //Access first form inside iframe just for your reference
		var thevalue=this.contentDoc.getElementById("file_id") //Access form field with id="emailfield" inside iframe
		var thetype=this.contentDoc.getElementById("file_type") //Access form field with id="emailfield" inside iframe
		if (thevalue.value==0){
			return true;
		} else {
			if (thetype.value=='download'){
				location.href='download_e12.php?bf_id='+thevalue.value;
			} if (thetype.value=='bookcase') {
				location.href='bookshelf.php?oper=add&bf_id='+thevalue.value;
			} if (thetype.value=='buy'){
				add_basket('book', book_id, thevalue.value );

			}
			return true //allow closing of window
		}
		return true //allow closing of window
	}
}



function opennewsletter3(type, book_id){
	emailwindow = dhtmlmodal.open('Покупка', 'iframe', '/files_e1.php?id='+book_id+'&type='+type, '', 'width=620px,height=200px,center=1,resize=0,scrolling=0')
	emailwindow.onclose=function(){ //Define custom code to run when window is closed
		var theform=this.contentDoc.forms[0] //Access first form inside iframe just for your reference
		var thevalue=this.contentDoc.getElementById("file_id") //Access form field with id="emailfield" inside iframe
		var thetype=this.contentDoc.getElementById("file_type") //Access form field with id="emailfield" inside iframe
		if (thevalue.value==0){
			return true;
		} else {
			if (thetype.value=='download'){
				location.href='download_e1.php?bf_id='+thevalue.value;
			} if (thetype.value=='bookcase') {
				location.href='bookshelf.php?oper=add&bf_id='+thevalue.value;
			} if (thetype.value=='buy'){
				add_basket('book', book_id, thevalue.value );

			}
			return true //allow closing of window
		}
		return true //allow closing of window
	}
}




function openprintorder(firstname, lastname, summa) {
	emailwindow = dhtmlmodal.open('Счет', 'iframe', '/bank_pay_out.php?firstname='+firstname+'&lastname='+lastname+'&summa='+summa, '', 'width=600px,height=570px,center=1,resize=0,scrolling=0')
	emailwindow.onclose=function(){ return true; }
}


function BookShelf(type, bookshelf_id){
		emailwindow = dhtmlmodal.open('x', 'iframe', '/bookshelf_a.php?id='+bookshelf_id+'&type='+type, '', 'width=500px,height=105px,center=1,resize=0,scrolling=0')
		emailwindow.onclose=function(){ //Define custom code to run when window is closed
		var theform=this.contentDoc.forms[0] //Access first form inside iframe just for your reference
		var thevalue=this.contentDoc.getElementById("tempfield") //Access form field with id="emailfield" inside iframe
		var thetype=this.contentDoc.getElementById("operation") //Access form field with id="emailfield" inside iframe
		var theid=this.contentDoc.getElementById("id") //Access form field with id="emailfield" inside iframe


		if (thevalue.value=='')
		{
			return true;

		} else
		{

			if (thetype.value=='rename'){
				location.href='bookshelf.php?oper=ren&id='+theid.value+'&name='+thevalue.value;

			}
			if (thetype.value=='del') {
				location.href='bookshelf.php?oper=del&id='+theid.value;
			}
			if (thetype.value=='add'){
				location.href='bookshelf.php?oper=add&name='+thevalue.value;
			}


			if (thetype.value=='remove'){

				location.href='bookshelf.php?oper=remove&file_id='+theid.value+'&bookshelf_id='+thevalue.value;
			}
			return true //allow closing of window
		}

		return true //allow closing of window
	}
}


function ChangeIconRegister(img) {
	document.getElementById('icon').src=img;
}

function ChangeBlock(param)
{
	if (document.getElementById(param).style.display=='none')
	{ document.getElementById(param).style.display='block'; }
	else
	{ document.getElementById(param).style.display='none'; }
}


function addBookmark(url, title)
{
  if (!url) url = location.href;
  if (!title) title = document.title;

  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement)
  {
    var a = document.createElement('A');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
  }
  else return false;

  return true;
}

function book_amount()
{
	var psc=document.special_offer_order.amount.value;
	var price=document.special_offer_order.price;
	price.value=20*psc;
	return true;
}
