/**
 * pedir.js
 */

function compraDef() {
	var name=unescape(document.cookie);
	var array_unidades= new Array();
	var array_titulos= new Array();
	var longitud=document.forms[0].length/5;
	for (j=0;j<longitud;j++){
		array_unidades[j]=document.forms[0].elements[5*j].value;
		array_titulos[j]=document.forms[0].elements[5*j+1].value + " (" + document.forms[0].elements[5*j].value + " ejemplares)\n";
	}
	var coste=document.forms[1].elements[0].value;
	var discount=document.forms[1].elements[1].value;
	var gastos=document.forms[1].elements[2].value;
	var total=document.forms[1].elements[4].value;
	var totalx100=total*100;
	var today = new Date();
	var cero="0";//para que OrderId concatene los valores y no los sume
	var OrderId = cero + today.getDate() + today.getHours() + today.getMinutes() + today.getSeconds();
	var OrderDesc= array_titulos;

	//CABECERA
	window.document.open();//Borrado del documento actual	
	window.document.writeln("<HTML>");
	window.document.writeln("<HEAD>");
	window.document.writeln("<TITLE>Pedido de Publicaciones</TITLE>");
	window.document.writeln("<LINK REL=\"stylesheet\" HREF=\"imagenes/credi2.css\" TYPE=\"text/css\"> ");
	window.document.writeln("</HEAD>");
	window.document.writeln("<BODY BGCOLOR=\"#FFFFFF\">");

	//FUNCIONES DEL SCRIPT
	window.document.writeln("	<SCRIPT LANGUAGE=\"javascript\">");
	window.document.writeln("<!--");
	window.document.writeln("var bikky = document.cookie;");
	window.document.writeln("function getCookie(name) {");
	window.document.writeln("	var index = bikky.indexOf(name + \"=\");");
	window.document.writeln("	if (index ==-1) return null;");
	window.document.writeln("	index = bikky.indexOf(\"=\", index) + 1;");
	window.document.writeln("	var endstr = bikky.indexOf(\";\", index)");;
	window.document.writeln("	if (endstr ==-1) endstr = bikky.length;");
	window.document.writeln("	return unescape(bikky.substring(index, endstr));");
	window.document.writeln("}");
	window.document.writeln("var today = new Date();");
	window.document.writeln("var expiry = new Date(today.getTime() + 2*60*60*1000);");
	window.document.writeln("function	setCookie(name) {");
	window.document.writeln("	document.cookie = eval('\"' + name + '=\" + escape (' + name + ') + \"; expires=' + expiry.toGMTString() + '\"');");
	window.document.writeln("	bikky = document.cookie;");
	window.document.writeln("}");
	window.document.writeln("function blankArray(arrayLength) {");
	window.document.writeln("	this.length = arrayLength;");
	window.document.writeln("	for (var i=0; i < this.length; i++)");
	window.document.writeln("  this[i] = \"\";");
	window.document.writeln("}");
	window.document.writeln("function defineCookie(newData) {");
	window.document.writeln("  userData = newData[0];");
	window.document.writeln("  for (var i=1; i < newData.length; i++)");
	window.document.writeln("  userData += \"!\" + newData[i];");
	window.document.writeln("  setCookie(\"userData\");");
	window.document.writeln("}");
	window.document.writeln("var userData = getCookie(\"userData\")");
	window.document.writeln("if (userData == null)");
	window.document.writeln("  userData = \"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\";");
	window.document.writeln("var numVars = 1;");
	window.document.writeln("var start = userData.indexOf(\"!\") + 1;");
	window.document.writeln("while (start != 0) {");
	window.document.writeln("  numVars++;");
	window.document.writeln("  start = userData.indexOf(\"!\", start) + 1;");
	window.document.writeln("}");
	window.document.writeln("var data = new blankArray(numVars);");
	window.document.writeln("for (var i=0; i < data.length; i++) {");
	window.document.writeln("  var end = userData.indexOf(\"!\", start);");
	window.document.writeln("  if (end == -1) end = userData.length;");
	window.document.writeln("  data[i] = userData.substring(start, end);");
	window.document.writeln("  start = end + 1;");
	window.document.writeln("}");
	window.document.writeln("	function calculo() {");
	window.document.writeln("	var euro_total=0;");
	window.document.writeln("	var unidades_total=0;");
	window.document.writeln("	var precio_euro, gastos_envio, descuento, precio_def;");
	window.document.writeln("	var i;");
	window.document.writeln("	longitud=document.forms[0].length/4;");
	window.document.writeln("	for (i=0;i<longitud;i++) {");
	window.document.writeln("document.forms[0].elements[4*i].value=parseInt(document.forms[0].elements[4*i].value)		");
	window.document.writeln("		unidades=document.forms[0].elements[4*i].value;");
	window.document.writeln("		if (unidades<0){");
	window.document.writeln("		document.forms[0].elements[4*i].value=-1*unidades;");
	window.document.writeln("			unidades=document.forms[0].elements[4*i].value;");
	window.document.writeln("		}");
	window.document.writeln("		precio=document.forms[0].elements[4*i+2].value;");
	window.document.writeln("		euro_total=eval(euro_total + '+' + precio + '*' + unidades);");
	window.document.writeln("		unidades_total=eval(unidades_total + '+' + unidades);");
	window.document.writeln("	}");
	window.document.writeln("	descuento=eval(0.2 + '*' + euro_total);");
	window.document.writeln("	if (unidades_total==1){");
	window.document.writeln("		gasto_envio=1;");
	window.document.writeln("	}");
	window.document.writeln("	else {");
	window.document.writeln("		if (unidades_total==2){");
	window.document.writeln("			gasto_envio=2;");
	window.document.writeln("		}");
	window.document.writeln("		else {");
	window.document.writeln("			if (unidades_total==3){");
	window.document.writeln("			gasto_envio=3;");
	window.document.writeln("			}");
	window.document.writeln("			else {");
	window.document.writeln("				gasto_envio=0;");
	window.document.writeln("			}");
	window.document.writeln("		}");
	window.document.writeln("	}");
	window.document.writeln("	document.forms[1].elements[0].value=Math.round(euro_total*100)/100;");
	window.document.writeln("	document.forms[1].elements[1].value=Math.round(descuento*100)/100;");
	window.document.writeln("	document.forms[1].elements[2].value=Math.round(gasto_envio*100)/100;");
	window.document.writeln("	precio_def=eval(euro_total + '-' + descuento);");
	window.document.writeln("	precio_def=Math.round(precio_def*100)/100;");
	//window.document.writeln("	alert(precio_def);");
	window.document.writeln("	document.forms[1].elements[4].value=precio_def;");
	window.document.writeln("}");
	window.document.writeln("	//-->");
	window.document.writeln("<\/SCRIPT>");
	
	//CABECERA
	window.document.writeln("<img src=\"http:\/\/www.oei.es\/imagenes\/cabecera_banderas_flash.jpg\" width=\"748\" height=\"76\" />");
	
	//SITUACION
	/*window.document.writeln("<P CLASS=\"situacion\"><B>Est&aacute; en: </B>");
	window.document.writeln("<A HREF=\"http:\/\/www.oei.es/\" CLASS=\"situacion\">OEI</A> -");
	window.document.writeln("<A HREF=\"index.html\" CLASS=\"situacion\">Publicaciones</A>-");
	window.document.writeln("<A HREF=\"libros.htm\" CLASS=\"situacion\">Libros</A> - P&aacute;gina de compra</P>");*/

	//ENCABEZADO DE LA TABLA
	window.document.writeln("<FORM> ");
	window.document.writeln("<table width=\"740\" border=\"2\" cellspacing=\"3\" cellpadding=\"0\">");
    /*window.document.writeln("<tr bgcolor=\"#006699\"> ");
    window.document.writeln("<td bgcolor=\"#FF8040\" class=\"titulo\" align=\"CENTER\" height=\"40\" colspan=\"4\"> ");
    window.document.writeln("    <b>Pedido de publicaciones</b></td>");
    window.document.writeln("</tr>");*/
    window.document.writeln("<tr bgcolor=\"#009999\"> ");
    window.document.writeln("  <td bgcolor=\"#FBF9DD\"> ");
    window.document.writeln("    <p align=\"CENTER\" class=\"martinez\"><b>T&iacute;tulo</b></p>");
    window.document.writeln("  </td>");
    window.document.writeln("  <td bgcolor=\"#FBF9DD\" align=\"CENTER\"> ");
    window.document.writeln("    <p align=\"center\" class=\"martinez\"><b>Precio en Euros</b></p>");
    window.document.writeln("    </td>");
    window.document.writeln("  <td bgcolor=\"#FBF9DD\" align=\"CENTER\"> ");
    window.document.writeln("    <p align=\"center\" class=\"martinez\"><b>Cantidad</b></p>");
    window.document.writeln("  </td>");
    window.document.writeln("  <td bgcolor=\"#FBF9DD\" align=\"CENTER\"> ");
    window.document.writeln("    <p align=\"center\" ");
	window.document.writeln(" class=\"martinez\"><b>Descuento</b></p>");
    window.document.writeln("  </td>");
//quitamos la celda de anular pedido
    window.document.writeln("</tr>");

	
	//PROCESADO DE LOS TITULO
//	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 porcent_descuento;
	var dollar_total=0;
	var euro_total=0;
	var cantidad_descuento=0;	
	var i=0;
	var k=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);						
		if (titulo != "") {
			window.document.writeln("<tr bgcolor=\"#009999\" valign=\"MIDDLE\">"); 
      		window.document.writeln("<td valign=\"MIDDLE\" bgcolor=\"#FBF9DD\">"); 
        	window.document.writeln("<p>"+ titulo + "</p>");
      		window.document.writeln("</td>");
      		window.document.writeln("<td bgcolor=\"#FBF9DD\" valign=\"MIDDLE\" align=\"CENTER\"> ");
        	window.document.writeln("<p class=\"martinez\">" + precio_euro + "</p>");
        	window.document.writeln("</td>");
      		window.document.writeln("<td width=\"60\" bgcolor=\"#FBF9DD\" valign=\"MIDDLE\" align=\"CENTER\">"); 
        	window.document.writeln("<p class=\"martinez\">" + unidades + "</p>");
      		window.document.writeln("</td>");
      		window.document.writeln("<td width=\"60\" bgcolor=\"#FBF9DD\" valign=\"MIDDLE\" align=\"CENTER\">"); 
        	window.document.writeln("<p class=\"martinez\">" + porcentaje_descuento + "</p>");
        	window.document.writeln("<input type=\"HIDDEN\" name=\"data" + i + "\" value=\"" + titulo + "\">");
        	window.document.writeln("<input type=\"HIDDEN\" name=\"precio" + i + "\" value=\"" + precio_euro + "\">");
			window.document.writeln("<input type=\"HIDDEN\" name=\"rebaja" + i + "\" value=\""+ descuento + "\">");
      		window.document.writeln("</td>");
    		window.document.writeln("</tr>");		
			k=k+1;//esta variable controla la posicion en array_unidades
		}
		i=i+1;//esta variable controla la posicion en la cookie
		inicio_titulo=fin_titulo+1;
	}

	window.document.writeln("</TABLE>");
	window.document.writeln("</FORM>");

	//TABLA DE CALCULO	
	window.document.writeln("<FORM>");	
	window.document.writeln("	<TABLE CELLPADDING=\"0\" CELLSPACING=\"3\" BORDER=\"2\">");
	window.document.writeln("<TR>");
	window.document.writeln("<TD CLASS=\"titulo\">&nbsp;</TD>");
	window.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><P ALIGN=\"center\" CLASS=\"martinez\"><B>Euros</B></P>");
	window.document.writeln("</TD>");
	window.document.writeln("</TR>");
	window.document.writeln("<TR>");
	window.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\"><P ALIGN=\"CENTER\" CLASS=\"martinez\">Coste</P>");
//	window.document.writeln("de las publicaciones");
	window.document.writeln("</TD>");
	window.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><P ALIGN=\"CENTER\" CLASS=\"martinez\">" + coste + "</P>");
	window.document.writeln("</TD>");
	window.document.writeln("</TR>");
	window.document.writeln("<TR>");
	window.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\"><P ALIGN=\"CENTER\"");
	window.document.writeln("CLASS=\"martinez\">Descuento por adquisici&oacute;n v&iacute;a Internet</P>");
	window.document.writeln("</TD>");
	window.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><P ALIGN=\"CENTER\" CLASS=\"martinez\">" + discount + "</P>");
	window.document.writeln("</TD>");
	window.document.writeln("</TR>");
	/*window.document.writeln("<TR>");
	window.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\"><P ALIGN=\"CENTER\" CLASS=\"martinez\">Gastos");
	window.document.writeln("de env&iacute;o</P>");
	window.document.writeln("</TD>");
	window.document.writeln("<TD BGCOLOR=\"#FBF9DD\"><P ALIGN=\"CENTER\" CLASS=\"martinez\">" + gastos + "</P>");
	window.document.writeln("</TD>");
	window.document.writeln("</TR>");*/ //QUITAMOS LOS GASTOS DE ENVÍO
	window.document.writeln("<TR>");
	window.document.writeln("<TD CLASS=\"titulo\" BGCOLOR=\"#FF8040\" ALIGN=\"CENTER\"> ");
	window.document.writeln("<P ALIGN=\"CENTER\" CLASS=\"martinez\">");
	window.document.writeln("<B>Cantidad total a pagar</B></P>");
	window.document.writeln("</TD>");
	window.document.writeln("<TD BGCOLOR=\"#FBF9DD\"> <P ALIGN=\"CENTER\" CLASS=\"martinez\"><B>" + total + "</B></P>");
	window.document.writeln("</TD>");
	window.document.writeln("</TR>");
	window.document.writeln("</TABLE>");
	

	
	window.document.writeln("<P><B>Compruebe que su pedido es correcto.</B></P>");
	window.document.writeln("<P><INPUT TYPE=\"BUTTON\" NAME=\"atras\" VALUE=\"Volver atr&aacute;s\"");
	window.document.writeln("ONCLICK=\"history.back();\"><INPUT TYPE=\"BUTTON\" NAME=\"seguir\" VALUE=\"Seguir comprando\"");
	window.document.writeln("ONCLICK=\"location='catalogo_completo.htm';\"></P>");
	window.document.writeln("</FORM> ");

	window.document.writeln("<FORM METHOD=\"POST\" NAME=\"pago\"");
	window.document.writeln(" ACTION=\"/cgi-bin/escribe_prueba.cgi\">");

	window.document.writeln("<P>Introduzca los siguientes datos <b>obligatorios:</b></P>");
	window.document.writeln("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
	//Nombre y apellidos del titular
    window.document.writeln("<tr><td><P><b>Nombre y apellidos:</b></td><td><P><input type=\"text\" name=\"Ds_Merchant_Titular\">(obligatorio)</P></td></tr>");
	window.document.writeln("<tr><td><P><b>Direcci&oacute;n:</b></td><td><P><input type=\"text\" name=\"direccion\">(obligatorio)</P></td></tr>");
    window.document.writeln("<tr><td><P><b>Ciudad:</b></td><td><P><input type=\"text\" name=\"ciudad\">(obligatorio)</P></td></tr>");
	window.document.writeln("<tr><td><P><b>C&oacute;digo postal:</b></td><td><P><input type=\"text\" name=\"cp\">(obligatorio)</P></td></tr>");
    window.document.writeln("<tr><td><P><b>Pa&iacute;s:</b></td><td><P><input type=\"text\" name=\"pais\">(obligatorio)</P></td></tr></table>");

	window.document.writeln("<P>Si desea recibir un correo electr&oacute;nico");
	window.document.writeln("justificante de compra introduzca su direcci&oacute;n en el siguiente campo:");
	window.document.writeln("<BR><B>Correo electr&oacute;nico:</B> <INPUT TYPE=\"TEXT\" NAME=\"eMail\">");
	window.document.writeln("(opcional)</P>");

	//Importe
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_Amount\" VALUE=\"" + totalx100 + "\">");
	//Moneda
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_Currency\" VALUE=\"978\">");
	//Número de Pedido
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_Order\" VALUE=\"" + OrderId + "\">");
	//Descripción del producto
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_ProductDescription\" VALUE=\"Libros adquiridos mediante el Servicio de Publicaciones de la OEI\">");
	//Identificación de comercio: Código FUC
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_MerchantCode\" VALUE=\"014157507\">");
	//URL del comercio para la notificación on-line
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_MerchantURL\" VALUE=\"http://www.campus-oei.org/cgi-bin/notificacion.cgi\">");
	//URL OK
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_UrlOK\" VALUE=\"http://www.campus-oei.org/publicaciones/aceptado.htm\">");
	//URL KO
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_UrlKO\" VALUE=\"http://www.campus-oei.org/publicaciones/cancelado.htm\">");
	//Identificación del comercio
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_MerchantName\" VALUE=\"Publicaciones de la OEI\">");
	//Número de Terminal
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_Terminal\" VALUE=\"001\">");
	//Datos del comercio (aquí guardo los datos del pedido. En escribe.cgi le añadiré el e-mail y resto de datos)
	window.document.writeln("<INPUT TYPE=\"HIDDEN\" NAME=\"Ds_Merchant_MerchantData\" VALUE=\"" + OrderDesc + "\">");
	window.document.write("<INPUT TYPE=\"SUBMIT\" NAME=\"B\" VALUE=\"Enviar pedido\" ");
	window.document.writeln("ONCLICK=\"if (document.forms[2].elements[0].value == '') {alert('Debe rellenar el campo de direcci&oacute;n postal.\\nCuando se cargue la p&aacute;gina siguiente vuelva atr&aacute;s\\ncon la opci&oacute;n ATR&Aacute;S del navegador y rellene este campo.');};\"></P>"); 
	window.document.writeln("</FORM>");
	
}	

