
/*

POZOJ
(cc) s1e, majx, ~GAF

--------------------------------------
JAHVE - SHOPPINGCART AJAX
--------------------------------------

*/

_cart = {

	// Function parses the xml that describes the books of the genre and passes results
	// to the next function which then constructs the html and displays the new
	// data on the fly.
	parse_xml : function(xml)
	{
		// We check if the xml dom contains any nodes.
		if ( xml.hasChildNodes )
		{
			// We get the book parent nodes in an array.
			var books = xml.getElementsByTagName('book');

			if ( books.length > 0 )
			{
				// We prepare the parent container for dynamic content.
				this.prepare_parent_element();

				// We loop through all of the books.
				for ( var x = 0; x < books.length; x++ )
				{
					var z = 0;
					var book_parameters = new Array(6);

					for ( var y = 1; y < books[x].childNodes.length; y++ )
					{
						if ( books[x].childNodes[y].firstChild ) book_parameters[z] = books[x].childNodes[y].firstChild.nodeValue;
						z++; y++;
					}
					alert(book_parameters[0] + book_parameters[1] + book_parameters[2] + book_parameters[3] + book_parameters[4] + book_parameters[5]);
					this.popup_book(book_parameters[0], book_parameters[1], book_parameters[2], book_parameters[3], book_parameters[4], book_parameters[5]);
					//alert(books[x].childNodes[1].firstChild.nodeValue+' '+books[x].childNodes[3].firstChild.nodeValue+' '+books[x].childNodes[5].firstChild.nodeValue+' '+books[x].childNodes[7].firstChild.nodeValue+' '+books[x].childNodes[9].firstChild.nodeValue+' '+books[x].childNodes[11].firstChild.nodeValue);
				}
			}
		}
	},


	// Function prepares the parent element container which will house the new dynamic book data,
	// deletes the old container and replaces it with the new one.
	prepare_parent_element : function()
	{
		// Prepare variables.
		var element;

		// Create a new container.
		element = document.createElement('li');
		// Set the class attribute of the new element to c.
		element.setAttribute('id', this.id_element_container_prefix+this.id_element);

		// Create a text node from the genre string and append it to the container.
		element.appendChild(document.createTextNode(this.element));

		// Get the existing element.
		var existing_element = document.getElementById(this.id_element_container_prefix+this.id_element);
		// And remove it.
		existing_element.parentNode.removeChild(existing_element);

		// If we have element after this one we use the insertBefore.
		if ( document.getElementById(this.id_element_container_prefix+(this.id_element+1)) )
		{
			this.container.insertBefore(element, document.getElementById(this.id_element_container_prefix+(this.id_element+1)));
		}
		// Otherwise appendChild.
		else
		{
			this.container.appendChild(element);
		}

		// Returns the new element into the classwide variable.
		this.parent_container = element;
	},


	// Function forks a new element with book data and pushes it into the strucure.
	popup_book : function(zanr, naziv, avtor, leto, cena, cena_akcija)
	{
		// We create a parent and child container element and append the child to the parent.
		var new_book = this.create_new_book_container(zanr, naziv, avtor, leto, cena, cena_akcija);

		// We push the new div into the DOM.
		this.parent_container.appendChild(new_book);
	},


	// Function forks a new element and populates it. Then it returns the new element.
	create_new_book_container : function(book_id, zanr, naziv, avtor, leto, cena, cena_akcija)
	{
		// Create variables.
		var element;
		var list_items = new Array(6);

		// Create a new container.
		element = document.createElement('ul');
		// Set the class attribute of the new element to c.
		//div.className = 'c';

		// Create three list_items for author information.
		for ( var x=1; x <= list_items.length-1; x++ ) {
			list_items[x] = document.createElement('li');
		}

		// Add text to list_items.
		var anchor = document.createElement('a');
		anchor.setAttribute('href', this.book_view_page_link + book_id);
		anchor.appendChild(document.createTextNode(naziv));
		list_items[1].appendChild(anchor);
		list_items[2].appendChild(document.createTextNode(avtor));
		list_items[3].appendChild(document.createTextNode(leto));
		list_items[4].appendChild(document.createTextNode(cena));
		list_items[5].appendChild(document.createTextNode(cena_akcija));


		// Append list_items to main comment container.
		for ( var x=1; x <= list_items.length-1; x++ ) {
			element.appendChild(list_items[x]);
		}

		// Return the new div.
		return element;
	},


	// Function changes the quantity of a product in the cart.
	spremeni_kolicino : function(artikel_id, nova_kolicina)
	{
		return this.request_page('kosarica_spremeni_kolicino.php', artikel_id, nova_kolicina);
	},


	// Function empties the users cart.
	izprazni : function()
	{
		// We setup element variable and child array.
		var kosarica_container = findobj('kosarica');
		var kosarica_produkti = kosarica_container.getElementsByTagName('li');

		if ( kosarica_produkti.length > 0 )
		{
			// We empty all of the child elements of the container.
			for ( x = 0; x <= kosarica_produkti.length; x++ )
			{
				kosarica_container.removeChild(kosarica_produkti[x]);
			}

			// At last, we call up the page to do the database mangling.
			return this.request_page('kosarica_izprazni.php');
		}
		else return false;
	},


	// Function deletes a product out of the cart.
	izbrisi_artikel : function(artikel_id, container_id)
	{
		// First we check if the request for deletion went through smoothly.
		if ( !this.request_page('kosarica_izbrisi_artikel.php', artikel_id) )
		{
			// If so we find the container that displays the deleted data on the page.
			var display_container = findobj(container_id);
			if ( display_container )
			{
				// If we can find the container we delete him.
				display_container.parentNode.removeChild(display_container);
				// And return false to not refresh the page by following the link.
				return false;
			}
			// If we can't find the container we return true so we refresh the page.
			else return true;
		}

	},


	// Function adds a product into the cart with the given quantity.
	dodaj_artikel : function(artikel_id, kolicina)
	{
		if ( (!artikel_id || !kolicina) || (artikel_id == "" || kolicina == "") || (typeof artikel_id != "undefined" || typeof kolicina != "undefined") )
		{
			// Init variables.
			var artikel_id = document.getElementById('id').value;
			var kolicina = document.getElementById('kolicina').value;
		}

		if ( artikel_id && kolicina )
		{
			var xml = this.request_page('kosarica_dodaj_artikel.php', artikel_id, kolicina);
			if ( xml )
			{
				alert(xml);
				return false;
			}
		}
		else return true;
	},


	// Function requests a given page with the given parameters and returns any potential xml.
	request_page : function(url, parm1, parm2)
	{
		var xmlhttp = setupXmlHttp(false);
		var path = '_xmlhttp/' + url + '?k=k';
		if ( parm1 ) path += '&x=' + parm1;
		if ( parm2 ) path += '&y=' + parm2
		alert(path);
		if ( xmlhttp ) {
			_notice.change('Pošiljam podatke košarici...')
			xmlhttp.open("GET", path, true);
			xmlhttp.onreadystatechange = function()
			{
				if ( xmlhttp.readyState == 4 )
				{
					if (xmlhttp.status == 200)
					{
						// We retrieve the XML encoded comment data.
						var xml = xmlhttp.responseXML;

						// We let the user know he sux.
						_notice.change('Košarica osvežena!');
						// If the xml exists return it.
						if ( xml ) return xml;
						else return false;
					}
					else alert(xmlhttp.status);
				}
			}
			xmlhttp.send(null);
			return false;
		}
		else return true;
	},


	// Function creates a reference for the main container for the comments.
	init : function()
	{
		_notice.change('Košarica inicializirana!');
	}
}
