// specify the client path - do not write the last forward slash
var path = "/2003/ar_2003_es";

// this global var prevents double form submission by an impatient user!
var isClicked = 0;


// Function adds a page to the print basket
// (adds page id to the cookie) 
function inv_AddPrintBasket(name, pageid) {
	printlist = getCookie('inv_print_basket')
	if (window.printlist == null) {
		document.cookie = "inv_print_basket=" + pageid + "; path=" + path;
	} else {
		results = printlist.search(pageid);
		if (results == -1) {
			addedid = pageid + "," + printlist;
			document.cookie="inv_print_basket=" + addedid + "; path=" + path;
		}

	}
	alert("Ha añadido la siguiente página a la cesta: \n- " + name);	
}


// Function removes a page from the print basket
// (removes page id to the cookie) 
function inv_RemovePage(name) {
	//remove the page from the print list
	if (isClicked==1) {
		return;
	}
	printlist1 = getCookie('inv_print_basket');
	var printlist1_array = printlist1.split(",");

	if (printlist1_array.length==1) {
		printlist2 = "";
		document.cookie = "inv_print_basket=" + printlist2 + "; path=" + path;
	} else {
		for (var loop=0; loop < printlist1_array.length; loop++) {
			if (!(name==printlist1_array[loop])) {
				if (window.printlist2) {
					printlist2 = printlist1_array[loop] + "," + printlist2;
				} else {
					printlist2 = printlist1_array[loop];
				}
			}
		}
		document.cookie="inv_print_basket=" + printlist2 + "; path=" + path;
	}
	// prevent double form submission
	isClicked = 1;
	document.form_print_basket.submit();
}


// Function removes all page from the print basket
// (assigns inv_print_basket NULL) 
function inv_RemoveAllPages() {
	//remove all entries
	makeNull = "";
	document.cookie = "inv_print_basket=" + makeNull + "; path=" + path;
	document.form_print_basket.submit();
}


// Function opens a popup window showing the contents of the print basket
function inv_OpenPrintBasket() {
	var date = new Date();
	var basketurl = '../printbasket/viewbasket.php' + "?" + date.getTime();
	wPrintBasket = window.open(basketurl, 'printbasketEADS', 'toolbar=0, scrollbars=1, location=0, statusbar=1, menubar=0, resizable=1, width=615, height=400, top=25, left=25');
	wPrintBasket.focus();
}


// Functions opens the printview of the pages
function inv_OpenPrintView(ids) {
	var date = new Date();
	var basketurl = '../printbasket/printview.php?ids=' + ids;
	wPrintView = window.open(basketurl, 'printviewEADS', 'toolbar=0, scrollbars=1, location=0, statusbar=1, menubar=0, resizable=1, width=615, height=550, top=25, left=25');
	wPrintView.focus();
}


// Function open the original page in the browser (opener window)
function inv_OpenOrgPage(url) {
	parent.opener.location=url;
}


// function reads the cookie
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	
	if (begin == -1) {
    	begin = dc.indexOf(prefix);
    	if (begin != 0) return null;
  	} else {
    	begin += 2;
	}
 	
	var end = document.cookie.indexOf(";", begin);
  	if (end == -1) {
		end = dc.length;
	}
	
	return unescape(dc.substring(begin + prefix.length, end));
}

// eof