var isIE = window.ActiveXObject?true:false;
var msgs = {
  "exist": "Книга с таким ISBN уже публиковалась на сайте!",
  "notExist": "Книги с таким ISBN в базе нет",
  "noCat": "Пожалуйста, выберите категорию",
  "noFmt": "Пожалуйста, выберите формат"
}
String.prototype.trim = function() { 
  return this.replace(/^\s*|\s*$/g, "");
}
function GO(id) {
  return document.getElementById(id);
}
function escapem(s) {
  var spesials = {"?":"3F","&":"26","=":"3D","+":"2B"};
  if (s)
    for (i in spesials)
      s = s.replace(RegExp("\\"+i,"g"),"%"+spesials[i]);
  else 
    s= "";
  return s;
}  
function checkISBN(func,ignorCat) {
  antiDyatel();
/*
  if (!ignorCat && GO("xfield[format]").value==0) {
    alert(msgs["noFmt"]);
    return false;
  }
*/
  var obj = GO("category");
  if (!ignorCat && obj && obj.selectedIndex<1) {
    alert(msgs["noCat"]);
    return false;
  }
  var res = true;
  var url;
  if (GO("xfield_holder_isbn").style.display=="" && GO("xfield[isbn]").value) {
    var ISBN = GO("xfield[isbn]").value.trim().replace(/^978-*/,"");
    ISBN = ISBN.replace(/[^0-9xх,]*/gi,"").replace(/x|х|Х/g,"X");
    url = "index.php?do=isbn&v="+escapem(ISBN);
    ajax.onReadyStateChange = func;
    ajax.sendRequest(url);
    res = false;
  }
  return res;
}
function isbnResult(s) {
  if (s>0) 
    alert(msgs["exist"]);
  else 
    alert(msgs["notExist"]);
}
function isbnOK(s) {
  if (s>0) {
    alert(msgs["exist"]);
  } else {
    if(document.entryform.title.value == '' || document.entryform.short_story.value == '')
      alert('У вашей статьи должен быть хотя бы заголовок и краткая версия');
    else
      document.entryform.submit();
  }
}
function fixPNG(element) {
  if (/MSIE (5\.5|6|7).+Win/.test(navigator.userAgent)) {
    var src;
    if (element.tagName=="IMG") {
      if (/\.png$/.test(element.src)) {
        src = element.src;
        element.src = "/templates/Custom/images/1x1t.gif"; 
      }
    } else {
      src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      if (src) {
        src = src[1]; 
        element.runtimeStyle.backgroundImage = "none"; 
      }
    }
    if (src) {
      element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"',sizingMethod='scale')";
    }
  }
}
function go(a) {
  document.location.href = a;
}
function updateCounter() {
  if (!counterLoading) {
    var obj = GO("LiveInternet");
    if (obj) obj.src = obj.src.replace(/;[0-9\.]+$/,";"+Math.random());
  }
}
function openPartnerShop(partnerName,id) {
  if (!id) return;
  var ID_PARTNER,url;
  switch (partnerName) {
    case "ozon":
      ID_PARTNER = "all-ebooks";
      url = "http://www.ozon.ru/context/detail/id/"+id+"/?partner="+ID_PARTNER;
      break;
    case "labirint":
      ID_PARTNER = "3872";
      url = "http://www.labirint.ru/books/"+id+"/?p="+ID_PARTNER;
      break;
  }
  window.open(url,"product"+id);
}
function addCheckButton() {
  var s = "проверить ISBN";
  var o = document.createElement("INPUT");
  o.type = "button";
  o.className = "bbcodes1";
  o.style.width = "120";
  o.style.padding = "0 0 0 0";
  o.style.margin = "0 5 0 5";
  o.value = s;
  o.textContent = s;
  o.onclick = function() { checkISBN(isbnResult,1) };
  GO("xfield[isbn]").parentNode.appendChild(o);
}
function addHelpText(xName,Text) {
  var o = document.createElement("SPAN");
  if (o) {
    o.className = "HelpText";
    o.innerHTML = Text;
    GO(xName).parentNode.appendChild(o);
  }
}
function setHelpHints() {
  addHelpText("xfield[cover]","http://i022.radikal.ru/0803/73/e882139d761d.jpg");
  addHelpText("xfield[author]","Выдревич Г.С.");
  addHelpText("xfield[isbn]","5-699-25757-7");
  addHelpText("xfield[publisher]","Эксмо, Терция");
  addHelpText("xfield[year]","2008");
  addHelpText("xfield[pages]","64");
  if (isIE) addHelpText("xfield[lang]","русский");
  if (isIE) addHelpText("xfield[format]","DJVU");
  addHelpText("xfield[size]","3.4 Мб");
  addHelpText("xfield[quality]","удовлетворительное");
  addHelpText("xfield[link_txt1]","rapidshare.com");
  addHelpText("xfield[link1]","http://rapidshare.com/files/96437161/bluda_iz_masa_i_pticy_v_gorsh.rar");
  addHelpText("xfield[link_txt2]","depositfiles.com");
  addHelpText("xfield[link2]","http://depositfiles.com/files/3886713");
  addHelpText("xfield[link_txt3]","ifolder.ru");
  addHelpText("xfield[link3]","http://ifolder.ru/5574864");
  addHelpText("xfield[passwd]","www.all-eBooks.com");
  addHelpText("xfield[ozon_id]","1436575");
  addHelpText("xfield[seria]","Название серии книг. Например: Хочу все сдать!");

  var s = 'Ссылки на хостинги картинок <a href="http://www.radikal.ru/" target="_blank">radikal.ru</a>, '+
    '<a href="http://fastpic.ru/" target="_blank">fastpic.ru</a>';
  $("#xfield_holder_cover").after('<tr><td></td><td>'+s+'</td></tr>');
  s = '<div>Выможете загрузить свой файл на <a href="http://depositfiles.com/signup.php?ref=demonit" target="_blank">depositfiles.com</a> прямо от сюда</div>'+
  '<object width="350" height="60">'+
  '<param name="movie" value="http://img3.depositfiles.com/flash/DepositUploader2.swf?ref=demonit&member_passkey=hbz4gl92ec9ep5w1"></param>'+
  '<param name="menu" value="false"></param>'+
  '<param name="scale" value="noScale"></param>'+
  '<param name="allowFullScreen" value="true"></param>'+
  '<param name="allowscriptaccess" value="always"></param>'+
  '<param name="wmode" value="transparent"></param>'+
  '<embed src="http://img3.depositfiles.com/flash/DepositUploader2.swf?ref=demonit&member_passkey=hbz4gl92ec9ep5w1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" menu="false" scale="noScale" wmode="transparent" width="350" height="60"></embed>'+
  '</object>';
  $("#xfield_holder_link1").after('<tr><td></td><td>'+s+'</td></tr>');
}
function onUnload() {
  return true;
  var isIE = document.all?1:0;
  var flag = getCookie("PopupOpened");
  if (isIE && !flag && event.clientY<0) {
    var expire = new Date()
    expire.setUTCMilliseconds(expire.getUTCMilliseconds()+3600*12*1000);
    setCookie("PopupOpened",1,expire);
    window.open("http://gravicapa.info/");
  }
}
function setCookie(name, value) {
  var argv = setCookie.arguments;  
  var argc = setCookie.arguments.length;  
  var expires = (argc>2)?argv[2]:null;  	 
  var domain  = (argc>4)?argv[4]:null;  
  var secure  = (argc>5)?argv[5]:false;  
  var path = "/"; 
  document.cookie = name+"="+escape(value)+
    ((expires==null)?"":("; expires="+expires.toGMTString()))+
    ((path==null)?"":("; path="+path))+
    ((domain==null)?"":("; domain="+domain))+
    ((secure==true)?"; secure=":"");
}
function getCookie(name) {
  var prefix = name + "="
  var cookieStartIndex = document.cookie.indexOf(prefix)
  if (cookieStartIndex==-1)
    return null
  var cookieEndIndex = document.cookie.indexOf(";",cookieStartIndex+prefix.length)
  if (cookieEndIndex == -1)
    cookieEndIndex = document.cookie.length
  return unescape(document.cookie.substring(cookieStartIndex+prefix.length,cookieEndIndex));
}
function antiDyatel() {
  GO("category").options[1].selected = false;
  GO("category").options[2].selected = false;
  if (GO("short_story").value==GO("full_story").value)
    GO("full_story").value = "";
  var o = GO("xfield[size]");
  o.value = o.value.replace(/(\d)(mb|мб)/gi,"$1 Мб");
  for (i=1; i<5; i++) {
    o = GO("xfield[link_txt"+i+"]");
    o.value = o.value.trim().replace(/http:\/\/|Скачать с /gi,"").replace(/\/$/gi,"");
  }
  o = GO("postTitle");
  o.value = o.value.trim().replace(/\.$/,"");

  o = GO("xfield[size]");
  if (/^[0-9\.,]+$/.test(o.value.trim()))
    o.value += " Мб";
}

