var obj_xml_combo_noms, obj_xml_buy;

function menuSelect(form){
  var URL=form.pagina.options[form.pagina.selectedIndex].value;

    if (URL!="none") {
      if (URL!="") {
      	if(URL != '../forum/index.php') top.frames.location.href=URL;
      	else window.open('../forum/index.php');
      }
      else {open(URL,"")}
      form.pagina.options[form.pagina.selectedIndex].selected = true
    }

}

function ltrim(cadena)
{
	while (cadena.charAt(0) == ' ') cadena = cadena.substr(1, cadena.length - 1);
  return cadena;
}

function rtrim(cadena)
{
	while (cadena.charAt(cadena.length - 1) == ' ') cadena = cadena.substr(0, cadena.length - 1);
	return cadena;
}

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

function obtenir_element(id)
{
	if(document.all)
  {
  	return document.all[id];
  }
  else if(document.layers)
  {
  	return document.layers[id];
  }
  else if(document.getElementById)
  {
  	return	document.getElementById(id);
  }
}

function obtenir_obj_xml()
{ 
  var obj_xml = null;
  try
  {
  	obj_xml = new XMLHttpRequest();
  }
  catch (e)
  {
  	try
  	{
  		obj_xml = new ActiveXObject("Msxml2.XMLHTTP");
  	}
    catch (e)
    {
    	obj_xml = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  
  return obj_xml;
}

function crear_combo_noms(tipus)
{	
	obj_xml_combo_noms = obtenir_obj_xml();
	if(obj_xml_combo_noms == null)
	{
		alert('El teu navegador no suporta AJAX.');
		return;
	} 
	
	obj_xml_combo_noms.onreadystatechange = canvi_estat_crear_combo_noms;
	obj_xml_combo_noms.open('POST', '../f_auxiliars/crear_combo_noms.php', true);
	obj_xml_combo_noms.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	obj_xml_combo_noms.setRequestHeader('Charset', 'ISO-8859-1');
	obj_xml_combo_noms.send('Tipus=' + tipus);
}

function canvi_estat_crear_combo_noms()
{ 
	var doc_xml;
	var num_noms, nom, id;
	var llista_noms, opcio;
	
	if(obj_xml_combo_noms.readyState == 4 || obj_xml_combo_noms.readyState == 'complete')
	{
		llista_noms = obtenir_element('combo_noms');
		
		var doc_xml = obj_xml_combo_noms.responseXML.documentElement;
		num_noms = doc_xml.getElementsByTagName('num_noms')[0].childNodes[0].nodeValue;
		
		for(i = 0; i < num_noms; i++)
		{
			nom = doc_xml.getElementsByTagName('nom_' + i)[0].childNodes[0].nodeValue;
			id = doc_xml.getElementsByTagName('id_' + i)[0].childNodes[0].nodeValue;

			opcio = document.createElement("OPTION");
			opcio.text = nom;
			opcio.value = './index.php?item_id=' + id;
			llista_noms.options.add(opcio);
		}
	}
}

function get_element(id)
{
	if(document.all)
  {
  	return document.all[id];
  }
  else if(document.layers)
  {
  	return document.layers[id];
  }
  else if(document.getElementById)
  {
  	return	document.getElementById(id);
  }
}

function amplada_visualitzacio()
{
	var amplada = 0;
	
	if(document.documentElement && document.documentElement.clientWidth) amplada = document.documentElement.clientWidth;
	else if(document.body && document.body.clientWidth)	amplada = document.body.clientWidth;
	else if(window.innerWidth) amplada = window.innerWidth - 18;

	return amplada;
}

function alcada_visualitzacio()
{
	var alcada = 0;
	
	if(document.documentElement && document.documentElement.clientHeight) alcada = document.documentElement.clientHeight;
	else if(document.body && document.body.clientHeight) alcada = document.body.clientHeight;
	else if(window.innerHeight) alcada = window.innerHeight - 18;
		
	return alcada;
}

function posicio_scroll()
{
  var posicio = 0;

  if(document.documentElement && document.documentElement.scrollTop) posicio = document.documentElement.scrollTop;
  else if(document.body && esta_definit(document.body.scrollTop)) posicio = document.body.scrollTop;
  else
  {
  	if(es_numeric(obtenir_element(capa_actual).scrollTop)) posicio = 0;
  }
  	
  return posicio;
}

function es_numeric(n)
{
  for(var i = 0; i < arguments.length; i++)
  {
  	if(typeof(arguments[i]) != 'number') return false;
  }
  return true;
}

function esta_definit()
{
  for(var i = 0; i < arguments.length; i++)
  {
  	if(typeof(arguments[i]) == 'undefined') return false;
  }
  return true;
}

function posicionar_centre_pantalla(id)
{
	var objecte = get_element(id);
	
	if(objecte)
	{	
		var amplada_objecte = objecte.offsetWidth ? objecte.offsetWidth : objecte.style.width ? parseInt(objecte.style.width) : 0;
		var alcada_objecte = objecte.offsetHeight ? objecte.offsetHeight :  objecte.style.height ? parseInt(objecte.style.height) : 0;
			
		var coordenada_x = (amplada_visualitzacio() - amplada_objecte) / 2;
			
		if(coordenada_x < 0) coordenada_x = 0;
			
		objecte.style.left = coordenada_x + 'px';
	}
}

function mail_validation(text)
{
	var mailres = true;
	var str = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
	var arroba = text.indexOf("@",0);
  var point = text.lastIndexOf(".");
	
  if ((text.lastIndexOf("@")) != arroba) arroba = -1;

	for (var counter = 0 ; counter < text.length ; counter++)
	{
		if (str.indexOf(text.substr(counter, 1),0) == -1)
		{
			mailres = false;
			break; 
   	} 
  }

  if ((arroba > 1) && (arroba + 1 < point) && (point + 1 < (text.length)) && (mailres == true) && (text.indexOf("..",0) == -1)) mailres = true;
  else mailres = false;
  
  return mailres;
}

function recalculate_bank_transfer(id_jewel)
{	
	var id_country, vat;
	
	obj_xml_buy = get_xml_obj();
	if(obj_xml_buy == null)
	{
		alert('Your browser not supports AJAX.');
		return;
	} 
	
	id_country = trim(document.Bank_transfer.Id_country.options[document.Bank_transfer.Id_country.selectedIndex].value);
	vat_number = trim(document.Bank_transfer.VAT_number.value);
	
	obj_xml_buy.onreadystatechange = recalculate_bank_transfer_submit;
	obj_xml_buy.open('POST', './recalculate.php', true);
	obj_xml_buy.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	obj_xml_buy.setRequestHeader('Charset', 'ISO-8859-1');
	obj_xml_buy.send('Id=' + id_jewel + '&Id_country=' + id_country + '&VAT_number=' + vat_number);
}

function recalculate_bank_transfer_submit()
{ 
	var xml_doc;
	var id, price, cost_shipment, tax, vat_ok, ship_insurance, total;
	var output;
	
	if(obj_xml_buy.readyState == 4 || obj_xml_buy.readyState == 'complete')
	{
		var xml_doc = obj_xml_buy.responseXML.documentElement;
		id = xml_doc.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		price = xml_doc.getElementsByTagName('price')[0].childNodes[0].nodeValue;
		cost_shipment = xml_doc.getElementsByTagName('cost_shipment')[0].childNodes[0].nodeValue;
		tax = xml_doc.getElementsByTagName('tax')[0].childNodes[0].nodeValue;
		vat_ok = xml_doc.getElementsByTagName('vat_ok')[0].childNodes[0].nodeValue;
		ship_insurance = xml_doc.getElementsByTagName('ship_insurance')[0].childNodes[0].nodeValue;
		total = xml_doc.getElementsByTagName('total')[0].childNodes[0].nodeValue;

		if(total != '-') 
		{
			output = '<span class="red_text"><b>Total shipment<\/b><\/span><br>\n';
			output = output + '<b>Price:<\/b> ' + price + ' &euro;<br>\n';
			output = output + '<b>Cost shipment:<\/b> ' + cost_shipment + ' &euro;<br>\n';
			if(tax != 0) output = output + '<b>TAX (16 % of piece price):<\/b> ' + tax + ' &euro;<br>\n';
			else output = output + '<b>TAX:<\/b> 0 &euro;<br>\n';
			if(ship_insurance != 0) output = output + '<b>Shiping insurance(1.25 % of piece price):<\/b> ' + ship_insurance + ' &euro;<br>\n';
			output = output + '<b>Total:<\/b> ' + total + ' &euro;\n';
			
			get_element('bank_transfer_data_layer').innerHTML = output;
			document.Bank_transfer.VAT_ok.value = vat_ok; 
			document.Bank_transfer.Total.value = total; 
		}
	}
}

function recalculate_paypal(id_jewel)
{	
	var id_country, vat;
	
	obj_xml_buy = get_xml_obj();
	if(obj_xml_buy == null)
	{
		alert('Your browser not supports AJAX.');
		return;
	} 
	
	id_country = trim(document.PayPal.Id_country.options[document.PayPal.Id_country.selectedIndex].value);
	vat_number = trim(document.PayPal.VAT_number.value);
	
	obj_xml_buy.onreadystatechange = recalculate_paypal_submit;
	obj_xml_buy.open('POST', './recalculate.php', true);
	obj_xml_buy.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	obj_xml_buy.setRequestHeader('Charset', 'ISO-8859-1');
	obj_xml_buy.send('Id=' + id_jewel + '&Id_country=' + id_country + '&VAT_number=' + vat_number);
}

function recalculate_paypal_submit()
{ 
	var xml_doc;
	var id, price, cost_shipment, tax, vat_ok, ship_insurance, total;
	var output;
	
	if(obj_xml_buy.readyState == 4 || obj_xml_buy.readyState == 'complete')
	{
		var xml_doc = obj_xml_buy.responseXML.documentElement;
		id = xml_doc.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		price = xml_doc.getElementsByTagName('price')[0].childNodes[0].nodeValue;
		cost_shipment = xml_doc.getElementsByTagName('cost_shipment')[0].childNodes[0].nodeValue;
		tax = xml_doc.getElementsByTagName('tax')[0].childNodes[0].nodeValue;
		vat_ok = xml_doc.getElementsByTagName('vat_ok')[0].childNodes[0].nodeValue;
		ship_insurance = xml_doc.getElementsByTagName('ship_insurance')[0].childNodes[0].nodeValue;
		total = xml_doc.getElementsByTagName('total')[0].childNodes[0].nodeValue;

		if(total != '-') 
		{
			output = '<span class="red_text"><b>Total shipment<\/b><\/span><br>\n';
			output = output + '<b>Price:<\/b> ' + price + ' &euro;<br>\n';
			output = output + '<b>Cost shipment:<\/b> ' + cost_shipment + ' &euro;<br>\n';
			if(tax != 0) output = output + '<b>TAX (16 % of piece price):<\/b> ' + tax + ' &euro;<br>\n';
			else output = output + '<b>TAX:<\/b> 0 &euro;<br>\n';
			if(ship_insurance != 0) output = output + '<b>Shiping insurance(1.25 % of piece price):<\/b> ' + ship_insurance + ' &euro;<br>\n';
			output = output + '<b>Total:<\/b> ' + total + ' &euro;\n';
			
			get_element('paypal_data_layer').innerHTML = output;
			document.PayPal.VAT_ok.value = vat_ok; 
			document.PayPal.amount.value = total; 
		}
	}
}

function get_xml_obj()
{ 
  var xml_obj = null;
  try
  {
  	xml_obj = new XMLHttpRequest();
  }
  catch (e)
  {
  	try
  	{
  		xml_obj = new ActiveXObject("Msxml2.XMLHTTP");
  	}
    catch (e)
    {
    	xml_obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  
  return xml_obj;
}