function obj(i){
	return document.getElementById(i);
}

function safeTitle(t){
	return t.replace(/[^ !@#\$\%\^&\[\]\(\)\w\.\-\\\/]/g,'');
}

var cart='';

function cartObj(){
	this.type=new Array();
	this.id=new Array();
	this.title=new Array();
}

function initCart(){
	//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='Empty Queue... Click \'Queue\' to add NZBs.';
	}
}

function hideCart(){
	var box=obj('queue');

	if(cart.id.length>9){box.innerHTML='NZB Queue Limit Reached (10 Limit)... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';return;}

	box.innerHTML=(cart.id.length)+' NZBs Queued... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';
}

function clearCart(){
	//clear out the cookie
	cart=new cartObj();
	var box=obj('queue');
	box.innerHTML='NZB Queue Is Empty...';
	writeCartCookie();
}

function processCart(){
	var x='';
	var uv='';
	for(x=0;x<cart.id.length;x++){
		uv+=cart.id[x]+'='+cart.type[x]+'&';		
	}
	var t=safeTitle(cart.title[0]);
		window.location='nzb-download-cart.php?'+uv+'name='+encodeURIComponent(t);
	clearCart();
}

function addMe(type,id){
	var title=document.getElementById('ctitle'+id).innerHTML;
	var box=obj('queue');

	//check for duplicates
	var x='';
	for(x=0;x<cart.id.length;x++){
		if(cart.id[x]==id){box.innerHTML='Already In NZB Queue... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';return;}
	}

	if(cart.id.length>9){box.innerHTML='Too Many In NZB Queue (10 Limit)... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';return;}
	
	//add to cookie
	cart.type.push(type);
	cart.id.push(id);
	cart.title.push(title);
	writeCartCookie();
	box.innerHTML='NZB Added: '+title.substr(0,80)+'... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';
}

function addMex(type,id, title){
	//same as above, except title is pulled through as param
    var box=obj('queue');
	//check for duplicates
	var x='';
	for(x=0;x<cart.id.length;x++){
		if(cart.id[x]==id){box.innerHTML='Already In NZB Queue... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';return;}
	}

	if(cart.id.length>9){box.innerHTML='Too Many In NZB Queue (10 Limit)... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';return;}
	
	//add to cookie
	cart.type.push(type);
	cart.id.push(id);
	cart.title.push(title);
	writeCartCookie();
	box.innerHTML='NZB Added: '+title.substr(0,80)+'... (<a href=# onclick="showCart();return false;">Show NZB Queue</a>)';
}

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 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 delete_nzb(nzb_id){
    var cartItems=gc('cartItems');
    
    //alert("Original num cart items: "+cartItems);
    document.cookie = "cartItem"+nzb_id+" = ;expires = Thu, 01-Jan-70 00:00:01 GMT;"; //remove cartitem
	sc('cartItems',cart.id.length-1,30); //update cartitems count
    
    
    var cartItems=gc('cartItems')
    //alert("Num cart items now: "+cartItems);
    var newID = 0;
    var oldID = '';
    var x = '';
    var cartIDs = new Array();
    var cartTitles = new Array();
    
    for(oldID=0; newID < cartItems; oldID++)
    {
        if(cart.id[oldID] != cart.id[nzb_id])
        {
            cartIDs[newID] = cart.id[oldID];
            //alert("Cart item: " + cartIDs[newID] + "\ndead nzb is : "+cart.id[nzb_id]);
            
            cartTitles[newID] = cart.title[oldID];
            newID++;
        }
        else
        {
            //alert("Del'd nzb found at id: " + cart.id[oldID] + " - " + cart.id[oldID]);
        }
    }
    
    clearCart();
    
    for(x=0; x < cartIDs.length; x++)
    {
        addMex('cid', cartIDs[x], cartTitles[x]);
    }
    
    /*
    //assigning index values for cart... defunkt
    for(oldID=0; oldID < cartItems; oldID++)
    {
        alert("checking : "+cart.title[oldID]);
        if(cart.id[oldID]){
            cart.id[oldID] = newID;
            alert(cart.id[oldID] + " becomes " + newID);
            newID++;
        }
    }
    */
    
	location.reload();//reload
}

function showCart(){
	var cc='';//the cart contents
	var x='';
    var cartItems=gc('cartItems');
    
	cc='<a href=# onclick="hideCart();return false;">Hide NZB Queue</a> | <a href=# onclick="clearCart();return false;">Clear NZB Queue</a> | <a href=# onclick="processCart();return false;">Get Queued NZBs Now</a> <a href=\"#\" onMouseover=\"ddrivetip(\'This queue can be used to download multiple NZBs as one download.\')\"; onMouseout=\"hideddrivetip()\">[?]</a><br/>';
	for(x=0;x<cartItems;x++){
		cc+=(x+1)+'. '+cart.title[x]+' <a href=# onclick="delete_nzb('+x+');return false;">[X]</a><br/>';
	}
	var box=obj('queue');
	box.innerHTML=cc;
}
