var cartonReq;
function getNextCarton(uri,id) {
   if(document.cartonForm.cartonDate.selectedIndex >  0) {
	document.cartonForm.cartonDate.selectedIndex--;
	getCarton(uri,document.cartonForm.serie.value,document.cartonForm.menuValue.value,id);
  }
}

function getPrevCarton(uri,id) {
	var length = document.cartonForm.cartonDate.length;
   if(document.cartonForm.cartonDate.selectedIndex <  length - 1) {
	document.cartonForm.cartonDate.selectedIndex++;
	getCarton(uri,document.cartonForm.serie.value,document.cartonForm.menuValue.value,id);
   }
}

function checkNavButtons() {
	if(document.cartonForm.cartonDate.selectedIndex ==  0) {
		document.getElementById("nextCarton").style.visibility = 'hidden';
	}
	else {
		document.getElementById("nextCarton").style.visibility = 'visible';
	}
	
	var length = document.cartonForm.cartonDate.length;
    
	if(document.cartonForm.cartonDate.selectedIndex ==  length - 1) {
		document.getElementById("prevCarton").style.visibility = 'hidden';
	}
	else {
		document.getElementById("prevCarton").style.visibility = 'visible';
	}
}

function showCarton(uri,id) {
	getCarton(uri,document.cartonForm.serie.value,document.cartonForm.menuValue.value,id);
}

function getCarton(uri, serie, menuValue, id) {
   checkNavButtons();
   markCartonMenu(menuValue);
   document.cartonForm.serie.value = serie;
   document.cartonForm.menuValue.value = menuValue;
   var date = document.cartonForm.cartonDate.value;
   new Ajax.Request(uri + "?service=ajaxSwitch&type=comics&comic=" + encodeURIComponent(serie) + "&date=" + encodeURIComponent(date),
  	{
    	method:'get',
    	onSuccess: function(transport){
     	document.getElementById(id).innerHTML = transport.responseText;
    }
   });
}

function markCartonMenu(menuValue) {
	var cartonMenuItems = document.getElementById("cartonMenu").childNodes;
	for (var i = 0; i < cartonMenuItems.length; i++) {
		var liNode = cartonMenuItems[i];
		if(liNode.nodeType == 1 && liNode.nodeName == 'LI') {
			var aNode = liNode.firstChild;
			if(aNode.firstChild.nodeValue == menuValue) {
				aNode.className="menuTab_ON ON";
			}
			else {
				aNode.className="menuTab OFF";
			}
		}
	}
}