
//
// JS common library for http://2lib.ru/ ( http://lib.align.ru/ )
//

//
// Get cookie value
//
function GetCookie(name) {
 // Prepare a string
 var string = "; "+document.cookie+';';
 // Find index
 var start = string.indexOf("; "+name+"=");
 // Check if found
 if (start>=0) {
    // Find end of cookie
    var stop = string.indexOf(";",start+2);
    // Return correct cookie value
    return string.substr(start+3+name.length,stop-start-name.length-3);
 }
 return '';
}

//
// Hash with values of checkBoxes
//
var boxValues = new Object;

//
// Load values for checkBoxes from cookies & setting checkBoxes
//
function loadBoxValues() {
 // Get cookie 
 var cookie = GetCookie('boxValues');
 // Split data
 var i = 0;
 var tries = 0; // For avoiding DEAD LOCKS
 var x;
 while ( (x = cookie.indexOf(',',i)) != -1 ) {
    // check for DEAD LOCK
    tries++;
    if (tries>150) return;
    
    // Get new ID
    var id = cookie.substr(i,x-i);
    // Save ID
    boxValues[id] = 1;
    i = x+1;
    
    // Try to configure checkBox
    if ( typeof(document.forms["getForm"]["i"+id]) != 'undefined') { 
	document.forms["getForm"]["i"+id].checked = true;
    }
 }
 showCount();
}

//
// Action for changing checkBox status
//
function onBoxChange(id) {
 // Check for limit overrun
 var LIMIT_CHECK_COUNT = 10;

 var currentFlag = (document.getElementById('i'+id).checked)?1:0;

 if (currentFlag) {
  var checkedCount = 0;
  for (var k in boxValues)
    if (boxValues[k]) checkedCount++;
 
  if (checkedCount >= LIMIT_CHECK_COUNT) {
   document.getElementById('i'+id).checked = false;
   alert('You can select up to '+LIMIT_CHECK_COUNT+' books at the same time!');
   return;
  }
 }
 
 
 // Save new value
 boxValues[id] = currentFlag;
 // Save new cookie
 var cValue = '';
 for (var k in boxValues) {
    if (boxValues[k]) cValue=cValue+k+',';
 }
 document.cookie='boxValues='+cValue;
 // update counter
 showCount();
}

//
// Action for button (add/delete from list) click
//
function onButtonClick(id) {
 // Get current value
 var val = (typeof(boxValues[id]) == 'undefined')?0:boxValues[id];
 // Change value
 if (val) val=0; else val=1;

 boxValues[id] = val; 
 // Save new cookie
 var cValue = '';
 for (var k in boxValues) {
    if (boxValues[k]) cValue=cValue+k+',';
 }
 document.cookie='boxValues='+cValue;
 // Change text
 initButtonValue (val);
 // update counter
 showCount();
}

function initButtonValue (val) {
 if (val) {
  document.forms["getForm"].PackButton.value = 'Из списка для архивации';
 } else {
  document.forms["getForm"].PackButton.value = 'В список для архивации';
 } 
}

//
// Show count of marked books
//
function showCount() {
 var cnt = 0;
 for (var k in boxValues) {
    if (boxValues[k]) cnt++;
 }
 document.getElementById('countBooks').innerHTML = cnt;
}


function SetCookie(sName, sValue)
{
 date = new Date();
 date.setHours(date.getHours()+24*3);
 document.cookie = sName + "=" + escape(sValue) + "; expires" + date.toGMTString();
}

function DelCookie(sName) { document.cookie = sName + "=; expires Fri, 31 Dec 1999 23:15"; }

function ShowCookies()    { alert(document.cookie.split(';').join('\n')); }



// Обрабатываем нажатие кнопки "Скачать"
function makeList()
{
 var list = GetCookie('boxValues');
 if (list.length) { 
  var newUrl = 'http://lib.align.ru/cgi-bin/download.pl?id='+list+'&packer='+chainForm.packer.value+'&charset='+chainForm.charset.value;
  window.open(newUrl);
 } else {
  alert('Вам необходимо сначала отметить книги!');
 }
}

//
// Delete books from list
//
function dropList()
{
 // Clear cookie
 document.cookie="boxValues=";
 // Clear array & unchecboxes
 for (var id in boxValues) {
    if (boxValues[id] && ( typeof(document.forms["getForm"]["i"+id]) != 'undefined')) {
	document.forms["getForm"]["i"+id].checked = false;
    }
    boxValues[id] = 0;
 }
 showCount();
}




