//javascript


//get a single nzb for file or collection
function getNow(type,id){
	window.location='nzb.php?'+id+'='+type;
}
function grab(me,type,id){
	if(type=='cid'){
		var title=document.getElementById(id+'ctitle').innerHTML;
	}else if(type=='fid'){
		var title=document.getElementById(id+'ftitle').innerHTML;
	}
	//strip out illegal file name components or the file wont save for the user
	//title=title.replace(/[\s\\\/:*?"<>|]/g,'');
	//now we set ALLOWABLE characters so unknow undesirables dont slip through
	title=safeTitle(title);
	//alert(title);
	window.location='nzb.php?'+id+'='+type+'&nn='+encodeURIComponent(title);
}
function safeTitle(t){
	//return t.replace(/[^!@#\$\%\^&\[\]\(\)0-9a-zA-Z\.\-\\\/]/g,'');
	return t.replace(/[^ !@#\$\%\^&\[\]\(\)\w\.\-\\\/]/g,'');
}
var cart='';
function cartObj(){
	this.type=new Array();
	this.id=new Array();
	this.title=new Array();
}
function initCart(cartempty){
	//here we read the cookie and build the cart contents
	//start out each page with # of items in the cart, or empty.
	cart=new cartObj();
	//if cookie, load it
	getCartCookie();
	//if there is stuff in cart, show the # of items
	if(cart.id.length>0){
		hideCart();
	}else{
		//else, use empty cart	
		var box=obj('queue');
		box.innerHTML='... Click \'queue\' to add items.';
	}
}

function minCart(){
//only show cart if there is something in it, otherwise hide element.
	cart=new cartObj();
	//if cookie, load it
	getCartCookie();
	//if there is stuff in cart, show the # of items
	if(cart.id.length>0){
		hideCart();
	}else{
		//else, use empty cart	
		var box=obj('queue');
		box.style.display='none';//innerHTML='Empty... Click \'queue\' to add items.';
	}
}

function hideCart(){
	var box=obj('queue');
	box.innerHTML=(cart.id.length)+' items in download cart... (<a style="color: black;" href=# onclick="showCart();return false;">Show items in queue</a>)';
}

function clearCart(){
	//clear out the cookie
	cart=new cartObj();
	var box=obj('queue');
	box.innerHTML='Empty...';
	writeCartCookie();
}

function processCart(){
	var x='';
	var uv='';
	for(x=0;x<cart.id.length;x++){
		uv+=cart.id[x]+'='+cart.type[x]+'&';
	}
	//strip out illegal file name components or the file wont save for the user
	//var t=cart.title[0].replace(/[\s\\\/:*?"<>|]/g,'');
	var t=safeTitle(cart.title[0]);
	//alert(title);
//	window.location='nzb.php?'+id+'='+type+'&nn='+encodeURIComponent(title);
	clearCart();
	window.location='nzb.php?'+uv+'nn='+encodeURIComponent(t);
}

function addMe(type,id){
	if(type=='cid'){
		var title=document.getElementById(id+'ctitle').innerHTML;
	}else if(type=='fid'){
		var title=document.getElementById(id+'ftitle').innerHTML;
	}
	var box=obj('queue');
	//title=title.substr(0,60)
	
	//check for duplicates
	var x='';
	for(x=0;x<cart.id.length;x++){
		if(cart.id[x]==id){box.innerHTML='Already in queue... (<a style="color: black;" href=# onclick="showCart();return false;">Show All</a>)';return;}
	}
	
	//add to cookie
	cart.type.push(type);
	cart.id.push(id);
	cart.title.push(title);
	writeCartCookie();
	box.innerHTML='Added: '+title.substr(0,60)+'... (<a style="color: black;" href=# onclick="showCart();return false;">Show All</a>)';
	//box.style.display='block';

}

function writeCartCookie(){
	for(x=0;x<cart.id.length;x++){
		//check and strip any existing |
		var strCook=cart.type[x]+'|'+cart.id[x]+'|'+cart.title[x].replace(/|/,"");
		sc('cartItem'+x,strCook,30);//expire after 30 days!!!
	}
	sc('cartItems',cart.id.length,30);
}

function clearCartCookie(){


}
function getCartCookie(){
	var cartItems=gc('cartItems');
	//if there is a value in the cookie....
	if(cartItems){
		var x='';
		//put it into the working array
		for(x=0;x<cartItems;x++){
			var strCook=gc('cartItem'+x);
			strCook=strCook.split('|');
			cart.type.push(strCook[0]);
			cart.id.push(strCook[1]);
			cart.title.push(strCook[2]);
		}
	}	
}

function gc(c_name){
	if (document.cookie.length>0){
	  var c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1){ 
		c_start=c_start + c_name.length+1 
		var c_end=document.cookie.indexOf(";",c_start)
		if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
       } 
	}
	return ""
}
function sc(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}


function showCart(foo){
	var cc='';//the cart contents
	var x='';
	cc='<a style="color: black;" href=# onclick="hideCart();return false;">Hide queue</a> | <a style="color: black;" href=# onclick="clearCart();return false;">Clear queue</a> | <a style="color: black;" href=# onclick="processCart();return false;">'+foo+'</a><br/>';
	for(x=0;x<cart.id.length;x++){
		cc+=(x+1)+'.'+cart.title[x].substr(0,60)+'...<br/>';
	}
	var box=obj('queue');
	box.innerHTML=cc;
}
