var bikky = document.cookie;
function getCookie(name) {
	var index = bikky.indexOf(name + "=");
	if (index ==-1) return null;
	index = bikky.indexOf("=", index) + 1;
	var endstr = bikky.indexOf(";", index);
	if (endstr ==-1) endstr = bikky.length;
	return unescape(bikky.substring(index, endstr));
}
var today = new Date();
var expiry = new Date(today.getTime() + 2*60*60*1000);

function setCookie(name) {
	document.cookie = eval('"' + name + '=" + escape (' + name + ') + "; expires=' + expiry.toGMTString() + '"');
	bikky = document.cookie;
}
function blankArray(arrayLength) {
	this.length = arrayLength;
	for (var i=0; i < this.length; i++)
  this[i] = "";
}
function defineCookie(newData) {
  userData = newData[0];
  for (var i=1; i < newData.length; i++)
  userData += "!" + newData[i];
  setCookie("userData");
}
var userData = getCookie("userData")
if (userData == null)
  userData = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";//hay que poner como minimo tantos como articulos
//Mirar los botones ver Compras porque usan también las admiraciones y debe haber tantas como aqui.
var numVars = 1;
var start = userData.indexOf("!") + 1;
while (start != 0) {
  numVars++;
  start = userData.indexOf("!", start) + 1;
}
var data = new blankArray(numVars);
for (var i=0; i < data.length; i++) {
  var end = userData.indexOf("!", start);
  if (end == -1) end = userData.length;
  data[i] = userData.substring(start, end);
  start = end + 1;
}

//************************************************************************************

function calculo() {
	var euro_total=0;
	var unidades_total=0;
	var rebaja_total=0;
	var precio_euro, gastos_envio, descuento, precio_def, rebaja;
	var i;
	longitud=document.forms[0].length/5;
	for (i=0;i<longitud;i++) {
		document.forms[0].elements[5*i].value=parseInt(document.forms[0].elements[5*i].value)
		unidades=document.forms[0].elements[5*i].value;
		if (unidades<0){
			document.forms[0].elements[5*i].value=-1*unidades;
			unidades=document.forms[0].elements[5*i].value;
		}
		precio=document.forms[0].elements[5*i+2].value;
		euro_total=eval(euro_total + '+' + precio + '*' + unidades);
		rebaja=document.forms[0].elements[5*i+3].value;
		rebaja_total=eval(rebaja_total + '+' + rebaja + '*' + '0.01' + '*' + precio+ '*' + unidades);
		unidades_total=eval(unidades_total + '+' + unidades);
	}	
	descuento=rebaja_total;
	/*if (unidades_total==1){
		gasto_envio=1;
	}
	else {
		if (unidades_total==2){
			gasto_envio=2;
		}
			else {
				if (unidades_total==3){
				gasto_envio=3;
				}
				else {
					gasto_envio=0;
				}
			}
	}*/
	gasto_envio=0; //QUITAMOS LO DE LOS GASTOS DE ENVÍO.
	document.forms[1].elements[0].value=Math.round(euro_total*100)/100;
	document.forms[1].elements[1].value=Math.round(descuento*100)/100;
	document.forms[1].elements[2].value=Math.round(gasto_envio*100)/100;
	//precio_def=eval(euro_total + '-' + descuento);
	precio_def=eval(euro_total + '-' + descuento + '+' + gasto_envio);
	window.document.writeln("precio_def=Math.round(precio_def*100)/100;");
	//	alert(precio_def);
	document.forms[1].elements[4].value=precio_def;
}

//************************************************************************************

function newWinExplorer() {
	//CABECERA
	document.open();
	ventana=window;
	//ventana.document.writeln(unescape(document.cookie));
	ventana.document.writeln("<HTML>");
	ventana.document.writeln("<HEAD>");
	ventana.document.writeln("<TITLE>Pedido de Publicaciones</TITLE><SCRIPT LANGUAGE=\"JavaScript\" SRC=\"pedir.js\"><\/SCRIPT>");
	ventana.document.writeln("<SCRIPT LANGUAGE=\"JavaScript1.2\" SRC=\"oscar_cookie.js\"><\/SCRIPT>");
	ventana.document.writeln("<LINK REL=\"stylesheet\" HREF=\"imagenes/credi2.css\" TYPE=\"text/css\"> ");
	ventana.document.writeln("</HEAD>");
	ventana.document.writeln("<BODY BGCOLOR=\"#FFFFFF\">");
	//FUNCIONES DEL SCRIPT
	ventana.document.writeln("<SCRIPT LANGUAGE=\"javascript\">");
	ventana.document.writeln("<!--");
	ventana.document.writeln("var bikky = document.cookie;");
	ventana.document.writeln("function getCookie(name) {");
	ventana.document.writeln("	var index = bikky.indexOf(name + \"=\");");
	ventana.document.writeln("	if (index ==-1) return null;");
	ventana.document.writeln("	index = bikky.indexOf(\"=\", index) + 1;");
	ventana.document.writeln("	var endstr = bikky.indexOf(\";\", index)");;
	ventana.document.writeln("	if (endstr ==-1) endstr = bikky.length;");
	ventana.document.writeln("	return unescape(bikky.substring(index, endstr));");
	ventana.document.writeln("}");
	ventana.document.writeln("var today = new Date();");
	ventana.document.writeln("var expiry = new Date(today.getTime() + 2*60*60*1000);");
	ventana.document.writeln("function	setCookie(name) {");
	ventana.document.writeln("	document.cookie = eval('\"' + name + '=\" + escape (' + name + ') + \"; expires=' + expiry.toGMTString() + '\"');");
	ventana.document.writeln("	bikky = document.cookie;");
	ventana.document.writeln("}");
	ventana.document.writeln("function blankArray(arrayLength) {");
	ventana.document.writeln("	this.length = arrayLength;");
	ventana.document.writeln("	for (var i=0; i < this.length; i++)");
	ventana.document.writeln("  this[i] = \"\";");
	ventana.document.writeln("}");
	ventana.document.writeln("function defineCookie(newData) {");
	ventana.document.writeln("  userData = newData[0];");
	ventana.document.writeln("  for (var i=1; i < newData.length; i++)");
	ventana.document.writeln("  userData += \"!\" + newData[i];");
	ventana.document.writeln("  setCookie(\"userData\");");
	ventana.document.writeln("}");
	ventana.document.writeln("var userData = getCookie(\"userData\")");
	ventana.document.writeln("if (userData == null)");
	ventana.document.writeln("  userData = \"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\";");
	ventana.document.writeln("var numVars = 1;");
	ventana.document.writeln("var start = userData.indexOf(\"!\") + 1;");
	ventana.document.writeln("while (start != 0) {");
	ventana.document.writeln("  numVars++;");
	ventana.document.writeln("  start = userData.indexOf(\"!\", start) + 1;");
	ventana.document.writeln("}");
	ventana.document.writeln("var data = new blankArray(numVars);");
	ventana.document.writeln("for (var i=0; i < data.length; i++) {");
	ventana.document.writeln("  var end = userData.indexOf(\"!\", start);");
	ventana.document.writeln("  if (end == -1) end = userData.length;");
	ventana.document.writeln("  data[i] = userData.substring(start, end);");
	ventana.document.writeln("  start = end + 1;");
	ventana.document.writeln("}");

	ventana.document.writeln("	function calculo() {");
	ventana.document.writeln("	var euro_total=0;");
	ventana.document.writeln("	var unidades_total=0;");
	ventana.document.writeln("	var rebaja_total=0;");
	ventana.document.writeln("	var precio_euro, gastos_envio, descuento, precio_def, rebaja;");
	ventana.document.writeln("	var i;");
	ventana.document.writeln("	longitud=document.forms[0].length/5;");
	ventana.document.writeln("	for (i=0;i<longitud;i++) {");
	ventana.document.writeln("document.forms[0].elements[5*i].value=parseInt(document.forms[0].elements[5*i].value)		");
	ventana.document.writeln("		unidades=document.forms[0].elements[5*i].value;");
	ventana.document.writeln("		if (unidades<0){");
	ventana.document.writeln("		document.forms[0].elements[5*i].value=-1*unidades;");
	ventana.document.writeln("			unidades=document.forms[0].elements[5*i].value;");
	ventana.document.writeln("		}");
	ventana.document.writeln("		precio=document.forms[0].elements[5*i+2].value;");
	ventana.document.writeln("		euro_total=eval(euro_total + '+' + precio + '*' + unidades);");

	ventana.document.writeln("		rebaja=document.forms[0].elements[5*i+3].value;");
	ventana.document.writeln("		rebaja_total=eval(rebaja_total + '+' + rebaja + '*' + '0.01' + '*' + 'precio' + '*' + unidades);");

	ventana.document.writeln("		unidades_total=eval(unidades_total + '+' + unidades);");
	ventana.document.writeln("	}");

	ventana.document.writeln("	descuento=rebaja_total;");

	/*ventana.document.writeln("	if (unidades_total==1){");
	ventana.document.writeln("		gasto_envio=1;");
	ventana.document.writeln("	}");
	ventana.document.writeln("	else {");
	ventana.document.writeln("		if (unidades_total==2){");
	ventana.document.writeln("			gasto_envio=2;");
	ventana.document.writeln("		}");
	ventana.document.writeln("		else {");
	ventana.document.writeln("			if (unidades_total==3){");
	ventana.document.writeln("			gasto_envio=3;");
	ventana.document.writeln("			}");
	ventana.document.writeln("			else {");
	ventana.document.writeln("				gasto_envio=0;");
	ventana.document.writeln("			}");
	ventana.document.writeln("		}");
	ventana.document.writeln("	}");*/
	ventana.document.writeln("	gasto_envio=0;") //quitamos los gastos de envío
	ventana.document.writeln("	document.forms[1].elements[0].value=Math.round(euro_total*100)/100;");
	ventana.document.writeln("	document.forms[1].elements[1].value=Math.round(descuento*100)/100;");
	ventana.document.writeln("	document.forms[1].elements[2].value=Math.round(gasto_envio*100)/100;");
	ventana.document.writeln("	precio_def=eval(euro_total + '-' + descuento + '+' + gasto_envio);");
	ventana.document.writeln("	precio_def=Math.round(precio_def*100)/100;");
	//ventana.document.writeln("	alert(precio_def);");
	ventana.document.writeln("	document.forms[1].elements[4].value=precio_def;");
	ventana.document.writeln("}");
	

	ventana.document.writeln("	//-->");
	ventana.document.writeln("<\/SCRIPT>");
	//CABECERA
	ventana.document.writeln("<img src=\"http:\/\/www.oei.es\/imagenes\/cabecera_banderas_flash.jpg\" width=\"748\" height=\"76\" />");
	
	//SITUACION
	/*ventana.document.writeln("<P CLASS=\"situacion\"><B>Est&aacute; en: </B>");
	ventana.document.writeln("<A HREF=\"http:\/\/www.oei.es/\" CLASS=\"situacion\">OEI</A> -");
	ventana.document.writeln("<A HREF=\"index.html\" CLASS=\"situacion\">Publicaciones</A>-");
	ventana.document.writeln("<A HREF=\"libros.htm\" CLASS=\"situacion\">Libros</A> - P&aacute;gina de compra</P>");*/
	//ENCABEZADO DE LA TABLA
	ventana.document.writeln("<FORM> ");
	ventana.document.writeln("<table width=\"740\" border=\"2\" cellspacing=\"3\" cellpadding=\"0\">");
    //ventana.document.writeln("<tr bgcolor=\"#006699\"> ");
    //ventana.document.writeln("<td bgcolor=\"#FF8040\" class=\"titulo\" align=\"CENTER\" height=\"40\" colspan=\"5\"> ");
    //ventana.document.writeln("    <b>Pedido de publicaciones</b></td>");
    //ventana.document.writeln("</tr>");
    ventana.document.writeln("<tr bgcolor=\"#009999\"> ");
    ventana.document.writeln("  <td bgcolor=\"#FBF9DD\"> ");
    ventana.document.writeln("    <p align=\"CENTER\" class=\"martinez\"><b>T&iacute;tulo</b></p>");
    ventana.document.writeln("  </td>");
    ventana.document.writeln("  <td bgcolor=\"#FBF9DD\" align=\"CENTER\"> ");
    ventana.document.writeln("    <p align=\"center\" class=\"martinez\"><b>Precio en Euros</b></p>");
    ventana.document.writeln("    </td>");
    ventana.document.writeln("  <td bgcolor=\"#FBF9DD\" align=\"CENTER\"> ");
    ventana.document.writeln("    <p align=\"center\" class=\"martinez\"><b>Cantidad</b></p>");
    ventana.document.writeln("  </td>");
    ventana.document.writeln("  <td bgcolor=\"#FBF9DD\" align=\"CENTER\"> ");
    ventana.document.writeln("    <p align=\"center\" ");
	ventana.document.writeln(" class=\"martinez\"><b>Descuento</b></p>");
    ventana.document.writeln("  </td>");
    ventana.document.writeln("  <td bgcolor=\"#FBF9DD\" align=\"CENTER\" valign=\"MIDDLE\"> ");
    ventana.document.writeln("   <p align=\"center\" class=\"martinez\"><b>Anular pedido</b></p>");
    ventana.document.writeln("  </td>");
    ventana.document.writeln("</tr>");

	//PROCESADO DE LOS TITULOS
	var name=unescape(document.cookie);
	var inicio_datos=name.indexOf("userData=") + 9;
	var fin_cookie=name.length;
	var inicio_titulo=inicio_datos;
	var fin_titulo;
	var titulo;
	var dollar;
	var euro;
	var rebaja;
	var porcentaje_descuento;
	var dollar_total=0;
	var euro_total=0;	
	var cantidad_descuento=0;
	var i=0;
	while (inicio_titulo<fin_cookie) {	
		fin_titulo=name.indexOf("!",inicio_titulo);
		articulo=name.substring(inicio_titulo,fin_titulo);
		dollar=articulo.indexOf("$",0);
		euro=articulo.indexOf("#",0);
		rebaja=articulo.indexOf("%",0);
		if (rebaja==-1){ //no hay descuento
			rebaja=fin_titulo;
			descuento=0;
			porcentaje_descuento="-";
		}else{
			descuento=articulo.substring(rebaja+1,fin_titulo);
			porcentaje_descuento=descuento+'%';
		}
		titulo=articulo.substring(0,dollar);
		precio_dollar=articulo.substring(dollar+1,euro);
		precio_euro=articulo.substring(euro+1,rebaja);
		//alert(cantidad_descuento);
		if (titulo != "") {
			ventana.document.writeln("<tr bgcolor=\"#009999\" valign=\"MIDDLE\">"); 
      		ventana.document.writeln("<td valign=\"MIDDLE\" bgcolor=\"#FBF9DD\">"); 
        	ventana.document.writeln("<p>"+ titulo + "</p>");
      		ventana.document.writeln("</td>");
      		ventana.document.writeln("<td bgcolor=\"#FBF9DD\" valign=\"MIDDLE\" align=\"CENTER\"> ");
        	ventana.document.writeln("<p class=\"martinez\">" + precio_euro + "</p>");
        	ventana.document.writeln("</td>");
      		ventana.document.writeln("<td width=\"60\" bgcolor=\"#FBF9DD\" valign=\"MIDDLE\" align=\"CENTER\">"); 
        	ventana.document.writeln("<input type=\"TEXT\" name=\"unidades" + i + "\" size=\"4\" value=\"1\">");
      		ventana.document.writeln("</td>");
      		ventana.document.writeln("<td width=\"60\" bgcolor=\"#FBF9DD\" valign=\"MIDDLE\" align=\"CENTER\">"); 
        	ventana.document.writeln("<p class=\"martinez\">" + porcentaje_descuento + "</p>");
      		ventana.document.writeln("</td>");
      		ventana.document.writeln("<td width=\"60\" bgcolor=\"#FBF9DD\" valign=\"MIDDLE\" align=\"CENTER\"> ");
        	ventana.document.writeln("<input type=\"HIDDEN\" name=\"data" + i + "\" value=\"" + titulo + "\">");
        	ventana.document.writeln("<input  width=\"60\" type=\"HIDDEN\" name=\"precio" + i + "\" value=\"" + precio_euro + "\">");
        	ventana.document.writeln("<p> ");
          	ventana.document.writeln("<input type=\"HIDDEN\" name=\"rebaja" + i + "\" value=\""+ descuento + "\">");
          	ventana.document.writeln("<input type=\"BUTTON\" name=\"nocomprar" + i + "\" value=\"Anular\" onClick='with (this.form) {data["+ i + "]=\"\";} defineCookie(data);newWinExplorer();history.back();newWinExplorer()'>");
        	ventana.document.writeln("</p>");
      		ventana.document.writeln("</td>");
    		ventana.document.writeln("</tr>");

				
		}
		i=i+1;
		inicio_titulo=fin_titulo+1;
	}
	ventana.document.writeln("</TABLE>");
	ventana.document.writeln("</FORM>");
	// PERIODO DE VACACIONES -----------------------------------------------------------------
	//ventana.document.writeln("<P><B>Por motivo del periodo vacacional, los pedidos que se realicen entre el 29 de Julio y el 31 de Agosto ser&aacute;n atendidos en Septiembre.");
	// PERIODO DE VACACIONES -----------------------------------------------------------------
	//TABLA DE CALCULO
	ventana.document.writeln("<FORM>");
	
	ventana.document.writeln("	<TABLE CELLPADDING=\"0\" CELLSPACING=\"3\" BORDER=\"2\">");
	ventana.document.writeln("<TR>");
	ventana.document.writeln("<TD CLASS=\"titulo\">&nbsp;</TD>");
	ventana.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><P ALIGN=\"center\" CLASS=\"martinez\"><B>Euros</B></P>");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("</TR>");
	ventana.document.writeln("<TR>");
	ventana.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\"><P ALIGN=\"CENTER\" CLASS=\"martinez\">Coste</P>");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><INPUT TYPE=\"TEXT\" NAME=\"coste\" SIZE=\"4\" ALIGN=\"MIDDLE\">");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("</TR>");
	ventana.document.writeln("<TR>");
	ventana.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\"><P ALIGN=\"CENTER\"");
	ventana.document.writeln("CLASS=\"martinez\">Descuento por adquisici&oacute;n v&iacute;a Internet</P>");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><INPUT TYPE=\"TEXT\" NAME=\"descuento\" SIZE=\"4\" ALIGN=\"MIDDLE\">");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("</TR>");
	//QUITAMOS LOS GASTOS DE ENVÍO
	/*ventana.document.writeln("<TR>");
	ventana.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\"><P ALIGN=\"CENTER\" CLASS=\"martinez\">Gastos");
	ventana.document.writeln("de env&iacute;o</P>");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><INPUT TYPE=\"TEXT\" NAME=\"gastos\" SIZE=\"4\" ALIGN=\"MIDDLE\">");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("</TR>");*/ 
	ventana.document.writeln("<TR>");
	ventana.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\" ALIGN=\"CENTER\"> ");
	ventana.document.writeln("<P ALIGN=\"CENTER\" CLASS=\"martinez\">");
	ventana.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"gastos\" SIZE=\"4\" ALIGN=\"MIDDLE\"><INPUT TYPE=\"BUTTON\" NAME=\"Calcular\" VALUE=\"Cantidad total a pagar\" ONCLICK=\"calculo();\"></P>"); 
	//Mantengo el antiguo campo de gastos de envío porque el acceso a cada campo es por índice, pero lo pongo oculto para que no se vea.
	ventana.document.writeln("</TD>");
	ventana.document.writeln("<TD BGCOLOR=\"#FBF9DD\"> <INPUT TYPE=\"TEXT\" NAME=\"total\" SIZE=\"4\" ALIGN=\"MIDDLE\">");
	ventana.document.writeln("</TD>");
	ventana.document.writeln("</TR>");
	ventana.document.writeln("</TABLE>");
	
	
	
	ventana.document.writeln("<P><INPUT TYPE=\"BUTTON\" NAME=\"Button1\" VALUE=\"Seguir comprando\"");
	ventana.document.writeln("ONCLICK=\"location='catalogo_completo.htm';\"><INPUT TYPE=\"BUTTON\" NAME=\"Pedir\" VALUE=\"Confirmar pedido\" ONCLICK=\"calculo();if(document.forms[1].elements[0].value==0){alert('No tiene selecionado ning&uacute;n art&iacute;culo');location='catalogo_completo.htm';}else{calculo();compraDef();}\"></P>");
	ventana.document.writeln("</FORM>");
	document.close();
}


