// JavaScript Document
/////////////////////////////////////////////
////////RSA//////////////////////////////////
/////////////////////////////////////////////
function gcd (a, b)
{
   var r;
   while (b>0)
   {
      r=a%b;
      a=b;
      b=r;
   }
   return a;
}

function rel_prime(phi)
{
   var rel=5;
   
   while (gcd(phi,rel)!=1)
      rel++;
   return rel;
}

function power(a, b)
{
   var temp=1, i;
   for(i=1;i<=b;i++)
      temp*=a;
    return temp;
}

function encrypt(N, e, M)
{
   var r,i=0,prod=1,rem_mod=0;
   while (e>0)
   {
      r=e % 2;
      if (i++==0)
         rem_mod=M % N;
      else
         rem_mod=power(rem_mod,2) % N;
      if (r==1)
      {
         prod*=rem_mod;
         prod=prod % N;
      }
      e=parseInt(e/2);
   }
   return prod;
}

function calculate_d(phi,e)
{
   var x,y,x1,x2,y1,y2,temp,r,orig_phi;
   orig_phi=phi;
   x2=1;x1=0;y2=0;y1=1;
   while (e>0)
   {
      temp=parseInt(phi/e);
      r=phi-temp*e;
      x=x2-temp*x1;
      y=y2-temp*y1;
      phi=e;e=r;
      x2=x1;x1=x;
      y2=y1;y1=y;
      if (phi==1)
      {
         y2+=orig_phi;
         break;
      }
   }
   return y2;
}

function decrypt(c, d, N)
{
   var r,i=0,prod=1,rem_mod=0;
   while (d>0)
   {
      r=d % 2;
      if (i++==0)
         rem_mod=c % N;
      else
         rem_mod=power(rem_mod,2) % N;
      if (r==1)
      {
         prod*=rem_mod;
         prod=prod % N;
      }
      d=parseInt(d/2);
   }
   return prod;
}


function openNew()
{
   var subWindow=window.open(
"Output.htm", "Obj","HEIGHT=400,WIDTH=600,SCROLLBARS=YES");
   var p=parseInt(document.Input.p.value);
   var q=parseInt(document.Input.q.value);
   var M=parseInt(document.Input.M.value);
   var N=p * q;
   var phi=(p-1)*(q-1);
   var e=rel_prime(phi);
   var c=encrypt(N,e,M);
   var d=calculate_d(phi,e);
   subWindow.document.Output.N.value=N;
   subWindow.document.Output.phi.value=phi;
   subWindow.document.Output.e.value=e;
   subWindow.document.Output.c.value=c;
   subWindow.document.Output.d.value=d;
   subWindow.document.Output.M.value=decrypt(c,d,N);
}

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
function bgImageColorOver(obj, nivel){
	try{
		ns='';
		if(nivel==1){
			ns='../';
		}else if(nivel==2){
			ns='../../';
		}
		obj.style.backgroundImage = 'url('+ns+'images/gradientAmarillo.jpg)' ;
		obj.style.backgroundRepeat = 'repeat-x';
	}catch(e){
		alert(e);
	}
}
function bgImageColorOut(obj){
	try{
		 
		obj.style.backgroundImage = '';
	 
	}catch(e){
		alert(e);
	}
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
function mostrarBoxBtn(obj){
	try{
			obj.style.display = "";
	 
	}catch(e){
		 
	}
}
function ocultarBoxBtn(obj){
	try{
		  
		obj.style.display = "none";
	 
	}catch(e){
		 
	}
}
////////////////////////////////////////
function mostrarBox(name){
	try{
		obj=document.getElementById(name);
		obj.style.display = "";
	 
	}catch(e){
		 
	}
}
function ocultarBox(name){
	try{
		obj=document.getElementById(name);
		obj.style.display = "none";
	 
	}catch(e){
		 
	}
}
////////////////////////////////////////////
////////////////////////////////////////////
function over(obj, color){
	try{
		 
		obj.style.background = color;
	 
	}catch(e){
		 
	}
}
function out(obj, color){
	try{
		 
		obj.style.background = color;
	 
	}catch(e){
		 
	}
}
////////////////////////////////////////////////////////////////////////////////////
function overCom(obj, color){
	try{
		 
		obj.style.background = color;
		obj.className='borderRound borderGris';
	 
	}catch(e){
		 
	}
}
function outCom(obj, color){
	try{
		 
		obj.style.background = color;
		obj.className='borderBlanco';
	 
	}catch(e){
		 
	}
}


////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
function textCounter(field, countfield, maxlimit) {
	try{
		if (field.value.length > maxlimit) {// if too long...trim it!
			field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
		}else{
			countfield.value = maxlimit - field.value.length;
		}
	}catch (ex){
		alert(ex);
	}
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
function es(obj,tipo){
	if(tipo=='s'){
		if (obj.value==""){
			obj.value="Buscar en turismovargas.com";
			obj.style.fontStyle="italic";	
			obj.style.color="#CCC";					
		}
	}else if(tipo=='u'){
		if (obj.value==""){
			obj.value="Usuario";
			obj.style.fontStyle="italic";	
			obj.style.color="#CCC";			
		}
	}else if(tipo=='c'){
		if (obj.value==""){
			obj.value="Contraseña";
			obj.style.fontStyle="italic";	
			obj.style.color="#CCC";
		}
	}
}
function de(obj){
		if (obj.value=="Buscar en turismovargas.com" || obj.value=="Usuario" || obj.value=="Contraseña"  ){
			obj.value="";
			obj.style.fontStyle="normal";
			obj.style.color="#000";
		}	
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") return inputString;
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
	
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length - 1, retValue.length);
	
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length - 1);
      ch = retValue.substring(retValue.length - 1, retValue.length);
   }
	
	// Note that there are two spaces in the string - look for multiple spaces within the string
   while (retValue.indexOf("  ") != -1) {
		// Again, there are two spaces in each of the strings
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user
}
 
function IsValidoStringUser(checkStr){
	
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
					if (j == checkOK.length){
						return false;
						break;
					}
	}
	return true;	
}

function IsValidoString(checkStr){
	
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZÑñabcdefghijklmnopqrstuvwxyzáéíóúÁÉÍÓÚ¡!?¿=., 0123456789"
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
					if (j == checkOK.length){
						return false;
						break;
					}
	}
	return true;	
}
function IsValidoStringPwd(checkStr){
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@%$#!¡&"

for (i = 0;  i < checkStr.length;  i++)
{
	ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
				if (j == checkOK.length){
					return false;
					break;
				}
		}
		return true;

}
function IsValidoNum(checkStr){
	var checkOK = " 0123456789.";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
					if (j == checkOK.length){
						return false;
						break;
					}
	}
	return true;	
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////