// JavaScript Document
/***********************************************************************
 * Fichero: sha1.js
 * Descripción: JavaScript Algoritmo SHA-1 para mensajes de 
 *              longbits máxima de 55 caracteres.
 *
 * Autor: PG - SERMEPA 10/12/1999
 *
 ***********************************************************************/
var H1 = 0x67452301;
var H2 = 0xefcdab89;
var H3 = 0x98badcfe;
var H4 = 0x10325476;
var H5 = 0xc3d2e1f0;

function hash(cadena)
{
/***********DEBUG *******************************/
/*alert("La cadena de entrada al hash es: " + cadena);*/

/***********DEBUG *******************************/

	buffer=new Array();

	/* Número de bytes */
	var longitud=cadena.length;
/***********DEBUG *******************************/
/*alert("La longitud de la cadena es: " + longitud);*/
/***********DEBUG *******************************/
	var longbits=longitud*8;
/*alert("La longitud de la cadena en bits es: " + longbits);*/
    var cadenaLen=String.fromCharCode(0x0,0x0,0x0,0x0,((longbits >>> 24) & 0xff),((longbits >>> 16) & 0xff),((longbits >>> 8) & 0xff),(longbits  & 0xff));
	var index=0;
    var padding=String.fromCharCode(0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
								0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
								0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
								0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
								0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
								0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
								0x0,0x0,0x0,0x0);


    iniciar();

	if(longitud >= 64){
		/* Procesar bloques de 64 */
		for (indice = 0; indice + 63 < longitud; indice += 64){
			transformar(cadena.substr(indice,64));
			index+=64}
	}
  longitud-=index;

  /* Padding */  
  padLen = (longitud < 56) ? (56 - longitud) : (120 - longitud);

  /**alert("padLEn es: " + padLen);
  alert("cadenaLen es: " + cadenaLen);
  alert("padding es: " + padding.substr(0,padLen-56));
**/
  if(longitud < 56)
  {
	transformar(cadena.substr(index)+padding.substr(0,padLen)+cadenaLen);
	
  }else{

	transformar(cadena.substr(index)+padding.substr(0,padLen-56));
/***********DEBUG *******************************/
/**alert(word_string(H1)+":"+word_string(H2)+":"+word_string(H3)+":"+word_string(H4)+":"+word_string(H5));
**/
/***********DEBUG *******************************/
	transformar(padding.substr(padLen-56, 56)+cadenaLen);

  }


	/*
	 * convertir a string
	 */
	  return (word_string(H1)+word_string(H2)+word_string(H3)+word_string(H4)+word_string(H5));

}/* Fin función hash */

function iniciar(){

H1 = 0x67452301;
H2 = 0xefcdab89;
H3 = 0x98badcfe;
H4 = 0x10325476;
H5 = 0xc3d2e1f0;
}
function transformar(cadena)
{
/*alert("transformar("+cadena+")");*/
	var Y1 = 0x5a827999;
	var Y2 = 0x6ed9eba1;
	var Y3 = 0x8f1bbcdc;
	var Y4 = 0xca62c1d6;

	buffer=new Array();
	words=new Array();

/*alert(word_string(H1)+":"+word_string(H2)+":"+word_string(H3)+":"+word_string(H4)+":"+word_string(H5));*/

	for(i=0;i<cadena.length;i++){
	  buffer[i]=cadena.charCodeAt(i);
      /*alert("buffer["+i+"]"+buffer[i]);*/}


	/* Pasar  bloque de 64 bytes a 16 words */
	for(i=0,j=0;i<64;i+=4,j++){
	  words[j]=((buffer[i + 0] << 24) & 0xff000000)|((buffer[i + 1] << 16) & 0x00ff0000) |
			   ((buffer[i + 2] <<  8) & 0x0000ff00)|(buffer[i + 3] & 0x000000ff);		   

	  }
/***********DEBUG *******************************/
/**for(k=0;k<16;k++)
alert("word"+k+":"+word_string(words[k]));
***/
/***********DEBUG *******************************/
	/* Procesar */

	for (i = 16; i <= 79; i++)
	{
		t = words[i - 3]^ words[i - 8]^ words[i - 14]^ words[i - 16];
		words[i] = circularRotate(t, 1);
	}

	// Comienzo calculo SHA1
	var A = H1;
	var B = H2;
	var C = H3;
	var D = H4;
	var E = H5;
	var t;
	/*
	 * vuelta 1
	 */
	for (j = 0; j <= 19; j++)
	{
		t = circularRotate(A, 5) + f(B, C, D) + E + words[j] + Y1;
		E = D;	D = C;	C = circularRotate(B, 30);	B = A;	A = t;
	}

	/*
	 * vuelta 2
	 */
	for (j = 20; j <= 39; j++)
	{
		t = circularRotate(A, 5) + h(B, C, D) + E + words[j] + Y2;
		E = D;	D = C;	C = circularRotate(B, 30);	B = A;	A = t;
	}

	/*
	 * vuelta 3
	 */
	for (j = 40; j <= 59; j++)
	{
		t = circularRotate(A, 5) + g(B, C, D) + E + words[j] + Y3;
		E = D;	D = C;	C = circularRotate(B, 30);	B = A;	A = t;
	}

	/*
	 * vuelta 4
	 */
	for (j = 60; j <= 79; j++)
	{
		t = circularRotate(A, 5) + h(B, C, D) + E + words[j] + Y4;
		E = D;	D = C;	C = circularRotate(B, 30);	B = A;	A = t;
	}

	H1 += A;
	H2 += B;
	H3 += C;
	H4 += D;
	H5 += E;

}

function circularRotate(x,n)
{
	return (x << n) | (x >>> (32 - n));
}/* Fin función circularRotate */
function f(u,v,w)
{
	return ((u & v) | ((~u) & w));
}

function h(u,v,w)
{
		return (u ^ v ^ w);
}

function g(u,v,w)
{
		return ((u & v) | (u & w) | (v & w));
}
function word_string(a)
{
 b1 = ((a >>> 28) & 0xf); 
 b2 = ((a >>> 24) & 0xf); 
 b3 = ((a >>> 20) & 0xf);
 b4 = ((a >>> 16) & 0xf);
 b5 = ((a >>> 12) & 0xf);
 b6 = ((a >>> 8) & 0xf);
 b7 = ((a >>> 4) & 0xf);
 b8 = (a & 0xf);
 return (b1.toString(16)+b2.toString(16)+b3.toString(16)+b4.toString(16)+ 
         b5.toString(16)+b6.toString(16)+b7.toString(16)+b8.toString(16));
}
function decode(a)
{    
 b1 = ((a >>> 24) & 0xff); 
 b2 = ((a >>> 16) & 0xff);
 b3 = ((a >>> 8) & 0xff);
 b4 = (a & 0xff);
 return (String.fromCharCode(b1,b2,b3,b4));
}

function hmac(clave, texto){
	/* H(clave XOR opad, H(clave XOR ipad, texto)) */
	/* clave menor de 64 bytes */
	
	key_1=new Array();
	key_2=new Array();
	words=new Array();

	for(index=0;index<clave.length;index++)
	  key_1[index]=clave.charCodeAt(index);

	for(;index<64;index++)
	  key_1[index]=0x0;

/***********DEBUG *******************************/
/***	for(k=0;k<64;k++)
alert("Buffer["+k+"]: "+ key_1[k]);***/
/***********DEBUG *******************************/
	for(index=0;index<64;index++){
	  key_2[index]=key_1[index]^0x5c;
	  key_1[index]^=0x36;
	  }
/***********DEBUG *******************************/
/***	for(k=0;k<64;k++)
alert("key_1["+k+"]: "+ key_1[k]+"key_2["+k+"]: "+ key_2[k]);***/
/***********DEBUG *******************************/
clave_1=String.fromCharCode(key_1[0],key_1[1],key_1[2],key_1[3],key_1[4],key_1[6],key_1[7],key_1[8],key_1[9],key_1[10],
						    key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],
							key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],
							key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],
							key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],
							key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],key_1[0],
							key_1[0],key_1[0],key_1[0],key_1[0]);
	
	/* Append con texto */	
	hash(String.fromCharCode(key_1)+texto);
	var digest=decode(H1)+decode(H2)+decode(H3)+decode(H4);

	/* Append con clave */	
	return(hash(String.fromCharCode(key_2)+digest));

}


function normalizar(precio) {
	var precio2="";
	if (precio=="") {
		alert("");
		return false;
	} else {
		precio=precio.replace(",",".");
		if (isNaN(precio)) {
			alert("Introduzca un importe válido");
			document.formul.Ds_Merchant_Amount.focus();
			document.formul.Ds_Merchant_Amount.focus();
			return false;
		} else {
			if (precio.indexOf(".") == -1){
					precio2 = precio + "00";
		   }  else if (precio.indexOf(".") != -1){				
					decimales = precio.substring(1+precio.indexOf("."),precio.length);
					precio2 = precio.substring(0,precio.indexOf(".")) + (decimales.length < 2 ? (decimales.length==0 ? "00":decimales + "0") : decimales.substring(0,2) );
		   }
		 }
	}
	return precio2;
}

function calc_Order() {
	var fecha = new Date()
	var anyo
	var mes
	var dia
	var hours
	var min
	var seg
	
	anyo = fecha.getYear();
    mes = fecha.getMonth() + 1;
	dia = fecha.getDate();
	hours = fecha.getHours();
	min = fecha.getMinutes();
	seg = fecha.getSeconds();
	anyo = anyo % 100;
	def = anyo * 10000000000 + mes * 100000000 + dia * 1000000 + hours* 10000 + min * 100 + seg;
		
    document.formul.Ds_Merchant_Order.value=def;
}


function calculo()
{
	var precio = normalizar(document.formul.Ds_Merchant_Amount.value);
	if (precio!=false) {
		var price_converted = new Number(precio);
		document.formul.Ds_Merchant_Amount.value=price_converted;
		var x = document.formul.Ds_Merchant_Amount.value + document.formul.Ds_Merchant_Order.value + document.formul.Ds_Merchant_MerchantCode.value  + document.formul.Ds_Merchant_Currency.value  + document.formul.DS_Merchant_TransactionType.value	+ document.formul.Ds_Merchant_MerchantSignature.value;
		document.formul.Ds_Merchant_MerchantSignature.value=hash(x);
	}

}

function validartexto()
{	
	if (document.formulario.desde.value == "boletin" ) {
		document.getElementById("errcheck").style.display="none";
	}
	document.getElementById("cajaempresa").style.color = "#000000";
	document.getElementById("cajatelefono").style.color = "#000000";
	document.getElementById("cajacorreo").style.color = "#000000";	
	if ((document.formulario.desde.value == "presupuesto" ) || (document.formulario.desde.value == "pedido" )) {
		document.getElementById("cajaobservaciones").style.color = "#000000";	
	}
	
	var iChars = "!@#$%^&*()+¡¿~=-[]\\\';·_,./{}|\":<>?";
	var mChars = "!#$%^&*()+¡¿~=[]\\\';·,/{}|\":<>?";
	var error = true;
	
  if (document.formulario.empresa.value.length == 0) {
	  document.getElementById("cajaempresa").style.color = "#FF0000";
	  error = false;
  } else {
	  if (document.formulario.email.value.length == 0) {
		  if (document.formulario.telefono.value.length == 0) {
			  alert("Debe ingresar un correo o un telefono de contacto");
			  document.getElementById("cajatelefono").style.color = "#FF0000";
			  document.getElementById("cajacorreo").style.color = "#FF0000";
			  error = false; 
		  } else {
			  if (document.formulario.desde.value != "boletin") {
				  if (document.formulario.observaciones.value.length == 0) {
					  document.getElementById("cajaobservaciones").style.color = "#FF0000";
					  error = false; 
				  }
			  }
		  }		  
	  } else {
		  if (validarmail(document.formulario.email.value) == false) {
			  document.getElementById("cajacorreo").style.color = "#FF0000";
			  error = false; 
		  } else {
			  if (document.formulario.observaciones.value.length == 0) {
				  document.getElementById("cajaobservaciones").style.color = "#FF0000";
				  error = false; 
			  }
		  }
	  }
  }
  
  if ((error) && (document.formulario.desde.value == "boletin")) {
	   if (document.formulario.acepto.checked == false) {
		document.getElementById("errcheck").style.display="";
		error = false; }
  }
  
  return error;
}

function validarmail(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	       return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   return false;
		 }

 		 return true;					
}


function limpiartexto()
{
	/*
	document.formulario.nombre.value = "";
  	document.getElementById("errnombre").style.display="none";
	
	document.formulario.mail.value = "";
  	document.getElementById("errmail").style.display="none";
	
	document.formulario.asunto.value = "";
  	document.getElementById("errasunto").style.display="none";
	
	document.formulario.mensaje.value = "";
  	document.getElementById("errmensaje").style.display="none";
*/
}