
var count = 11;

var total_price = 0;

var ids    = new Array(count);
var books  = new Array(count);
var prices = new Array(count);
var thumbnail  = new Array(count);

ids[0] = 1;
ids[1] = 2;
ids[2] = 3;
ids[3] = 4;
ids[4] = 5;
ids[5] = 6;
ids[6] = 7;
ids[7] = 8;
ids[8] = 9;
ids[9] = 10;
ids[10] = 11;
ids[11] = 12;
ids[12] = 13;
ids[13] = 14;
ids[14] = 15;
ids[15] = 16;


books[0] = 'La Restauración Meiji';
books[1] = 'Sintoismo, El camino de los Cami.';
books[2] = 'Mitos y Leyendas de Japón';
books[3] = 'El Japón Fantasmal';
books[4] = 'Kabuki, teatro tradicional Japonés';
books[5] = 'El Mundo Fantástico en la Literatura japonesa';
books[6] = 'Japón, un intento de interpretación';
books[7] = 'Urashima, El pescador';
books[8] = 'Momotaro, El niño melocotón';
books[9] = 'La Esencia del Judo';
books[10] = 'Titulo 000';
books[11] = 'Titulo 001';
books[12] = 'Titulo 002';
books[13] = 'Titulo 003';
books[14] = 'Titulo 004';
books[15] = 'Titulo 005';

prices[0] = 28;
prices[1] = 12;
prices[2] = 26;
prices[3] = 15;
prices[4] = 20;
prices[5] = 21;
prices[6] = 22;
prices[7] = 18;
prices[8] = 18;
prices[9] = 55;
prices[10] = 55;
prices[11] = 0;
prices[12] = 0;
prices[13] = 0;
prices[14] = 0;
prices[15] = 0;

thumbnail[0] = "img/libros/miniaturas/meiji.png";
thumbnail[1] = "img/libros/miniaturas/sinto.png";
thumbnail[2] = "img/libros/miniaturas/mitos.png";
thumbnail[3] = "img/libros/miniaturas/fantasmal.png";
thumbnail[4] = "img/libros/miniaturas/kabuki.png";
thumbnail[5] = "img/libros/miniaturas/fantastico.png";
thumbnail[6] = "img/libros/miniaturas/japon.png";
thumbnail[7] = "img/libros/miniaturas/urashima.png";
thumbnail[8] = "img/libros/momotaro.png";
thumbnail[9] = "img/libros/miniaturas/judo.png";
thumbnail[10] = "img/libros/miniaturas/none.png";
thumbnail[11] = "img/libros/miniaturas/none.png";
thumbnail[12] = "img/libros/miniaturas/none.png";
thumbnail[13] = "img/libros/miniaturas/none.png";
thumbnail[14] = "img/libros/miniaturas/none.png";
thumbnail[15] = "img/libros/miniaturas/none.png";


function addBook(index)
{
	var book_count = document.getElementById("book_count" + index);
	
  if (books[index] && !book_count)
  {
    var tbody = document.getElementById('book_list').getElementsByTagName("TBODY")[0];

    var row = document.createElement("TR");
    var td1 = document.createElement("TD");
    var td2 = document.createElement("TD");
    var td3 = document.createElement("TD");
		var td4 = document.createElement("TD");
		var td5 = document.createElement("TD");
		
		row.id = 'row_book' + index;
		
		td2.style.width = '600px';
		td3.style.textAlign = 'center';
		td4.style.width = '17px';
		td5.style.width = '150px';
		td5.style.textAlign = 'center';

    var label1 = document.createElement("LABEL");
    label1.id  = "book_price" + index; 

    var input1 = document.createElement("INPUT");
		input1.type = "text";
    input1.id  = "book_count" + index;
		input1.name  = "book_count" + index;
    input1.value  = 1;
    input1.setAttribute("onchange", "changePrice(" + index + ");");
		input1.style.width = '20px';

		var img = document.createElement("IMG");
		img.src = thumbnail[index];
		
		var img2 = document.createElement("IMG");
		img2.src = 'img/remove.png';
		img2.style.width  = '16px';
		img2.style.height = '16px';
		img2.style.cursor = 'pointer';
		img2.title = 'Borrar libro';
		img2.alt   = 'Borrar libro';
		img2.setAttribute("onclick", "removeBook(" + index + ");");
		
		var hidden   = document.createElement("INPUT");
		hidden.type  = 'hidden';
		hidden.name  = 'ids[]';
		hidden.value = index;

		td1.appendChild(img);
    td2.appendChild (document.createTextNode(books[index]));
    td3.appendChild (input1);
		td4.appendChild(img2);
    td5.appendChild (label1);

    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
		row.appendChild(td4);
		row.appendChild(td5);
		row.appendChild(hidden);
		
    tbody.insertBefore(row, tbody.firstChild);
		
		if (!document.getElementById('total_price'))
		{
		
			row = document.createElement("TR");
			td1 = document.createElement("TD");
			td2 = document.createElement("TD");
			td3 = document.createElement("TD");
			//td4 = document.createElement("TD");
 			td5 = document.createElement("TD");
			
			row.id = 'row_send';
			
			label1 = document.createElement("LABEL");
			label1.appendChild(document.createTextNode('Gastos envio'));
			td3.appendChild(label1);
			td3.colSpan = "2";
			
			var label2 = document.createElement("LABEL");
			label2.appendChild(document.createTextNode('+5,00 €'));
			td5.appendChild(label2);
			
			td3.style.textAlign = 'right';
			td5.style.textAlign = 'center';
			
			row.appendChild(td1);
			row.appendChild(td2);
			row.appendChild(td3);
			//row.appendChild(td4);
			row.appendChild(td5);
			
			tbody.appendChild(row);
			
			row = document.createElement("TR");
			td1 = document.createElement("TD");
			td2 = document.createElement("TD");
			td3 = document.createElement("TD");
			//td4 = document.createElement("TD");
			td5 = document.createElement("TD");
			
			row.id = 'row_total';
			
			label1 = document.createElement("LABEL");
			label1.id = 'total_price';
			
			label2 = document.createElement("LABEL");
			label2.appendChild(document.createTextNode('TOTAL'));
			
			td3.appendChild(label2);
			td5.appendChild(label1);
			td3.style.textAlign = 'right';
			td3.style.fontSize = '26px';
			td3.colSpan = "2";
			td5.style.textAlign = 'center';
			td5.style.fontSize = '26px';
			
			row.appendChild(td1);
			row.appendChild(td2);
			row.appendChild(td3);
			//row.appendChild(td4);
			row.appendChild(td5);
			
			tbody.appendChild(row);
		}
  }
	else if(books[index] && book_count)
	{
		if (isNaN(book_count.value))
			book_count.value = 1;
		else
			book_count.value = parseInt(book_count.value) + 1;
	}
	
	changePrice(index);
}

function removeBook(index)
{
  var tbody = document.getElementById('book_list').getElementsByTagName("TBODY")[0];
	var row = document.getElementById('row_book' + index);

	if (row)
	{
		if(tbody.childNodes.length <= 4)
		{
			if (document.getElementById('row_send'))
				tbody.removeChild(document.getElementById('row_send'));
				
			if (document.getElementById('row_total'))
				tbody.removeChild(document.getElementById('row_total'));
				
			tbody.removeChild(row);
			total_price = 0;
		}
		else
		{
			var label = document.getElementById('book_price' + index);
			
			current_price = parseInt(label.lastChild.data);
			total_price -= current_price;
		
			label = document.getElementById('total_price');
		
			if (label.lastChild)
				label.removeChild(label.lastChild);
			
			label.appendChild(document.createTextNode(total_price + 5 + ",00 €"));
			tbody.removeChild(row);
		}
	}
}

function changePrice(index)
{
  var label = document.getElementById('book_price' + index);
  var input = document.getElementById('book_count' + index);

  var total = 0;

  if (label && input)
  {
    if (!isNumber(input.value) || parseInt(input.value) < 1)
			input.value = 1;
		else if (parseInt(input.value) > 10)
		{
			alert('Para pedidos superiores a 10 unidades, contacte con nosotros.');
			input.value = 10;
		}

    total = prices[index] * input.value;
		
		var current_price = 0;
 
		if(label.lastChild)
		{
			current_price = parseInt(label.lastChild.data);
			label.removeChild(label.lastChild);
		}
			
    label.appendChild(document.createTextNode(total + ",00 €"));
		total_price += total - current_price;
		
		label = document.getElementById('total_price');
		
		if (label.lastChild)
			label.removeChild(label.lastChild);
			
		label.appendChild(document.createTextNode(total_price + 5 + ",00 €"));
  }
}


function isNumber(text)
{
  var validChars = "0123456789";
  var number = true;
  var char;

  for (i = 0; i < text.length && number == true; i++) 
  { 
    char = text.charAt(i); 
     if (validChars.indexOf(char) == -1)
       number = false;
  }
  return number;
}

function checkData()
{
	var form1 = document.getElementById('form1');
	var tbody = document.getElementById('book_list').getElementsByTagName("TBODY")[0];
	
	if(tbody.childNodes.length < 4)
	{
		alert('Debes seleccionar algún libro.');
		return false;
	}
	
	var name    = trim(form1.nombre.value);
	var phone   = trim(form1.telefono.value);
	var email   = trim(form1.email.value);
	var address = trim(form1.direccion.value);
	
	if (name == '')
	{
		alert('El nombre no puede estar vacio.');
		form1.nombre.focus();
		return false;
	}
	
	if (phone == '')
	{
		alert('El teléfono no puede estar vacio.');
		form1.telefono.focus();
		return false;
	}
	
	if (!isNumber(phone))
	{
		alert('El teléfono solo puede contener números.');
		form1.telefono.focus();
		return false;
	}
	
	if (phone.length < 9)
	{
		alert('El teléfono debe ser válido.');
		form1.telefono.focus();
		return false;
	}
	
	if (email == '')
	{
		alert('El e-mail no puede estar vacio.');
		form1.email.focus();
		return false;
	}
	
	if (!checkEmail(email))
	{
		alert('El e-mail debe ser un e-mail válido.');
		form1.email.focus();
		return false;
	}
	
	if (address == '')
	{
		alert('La dirección no puede estar vacia.');
		form1.direccion.focus();
		return false;
	}

	return true;
}

function checkEmail(email) 
{              
	var filter = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,6})$/;
	return filter.test(email);
}

function ltrim(s) {
   return s.replace(/^\s+/, "");
}

function rtrim(s) {
   return s.replace(/\s+$/, "");
}

function trim(s) {
   return rtrim(ltrim(s));
}
