
// construct an array that will hold the character of the plaintext.

var letter=new Array();
var row, column;

var my_letter=new Array();
//////////////////////////////////////////////////////////
// The following function create the array for railfence 
// when the user click button Railfence.
//////////////////////////////////////////////////////////

function railfence()
{
// construct an array that will hold the character of the plaintext.

var letter=new Array();
	// get the value of num textfield to construct row of array.
	row=parseInt(document.form1.num.value);
	// get the plainText
	var plainText1=document.form2.plainText.value;
	// Take out spaces from the plainText
	plainText=takeSpace(plainText1);
	// get the length of the plaintext and devide it with row
	// to get the column of the railfence
	column=Math.ceil(plainText.length/row);
	// loop through the column under row 
	// to assign the letter of the plaintext into array.
	for(i=0;i<row;i++)
	{
		letter[i]=new Array();
		my_letter[i]=new Array();//for showCipher function
		
		for(j=0;j<column;j++)
		{
		
			// get proper index to build railfence array.
		
			index=i+row*j;

			
			// if index is greater than text length then not initialize the array.
			
			if(index<plainText.length)
			{
			
				letter[i][j]=plainText.charAt(index);
				my_letter[i][j]=plainText.charAt(index);// for showCipher function
				//alert("["+i+"]["+j+"]="+letter[i][j]);
			
			}
			else
			{

				letter[i][j]=" ";
				my_letter[i][j]=" ";

			}
		
		}
	
	}







/*
	
	
	// make fixed space depend on row
	
	var fixsp="";
	
	for(l=0;l<row;l++)
	{
	fixsp+="  ";	
	}
	
	for(i=0;i<row;i++)
	{
		// make necessary space for each line.
	
		space="";
		for(k=1;k<=i+1;k++)
		{
		space+="  ";
		}	
		for(j=0;j<column;j++)
		{
		output+=letter[i][j]+fixsp;		
		}
		output+="<br>"+space;	
	}

////new code to show all the railfence in atable
	
	
*/
	var myarr=new Array()
	var k=0;
	for(var i=0;i<letter.length;i++)
		{
		myarr[i]=new Array()
		for(var j=0;j<letter.length*letter[0].length;j=j+letter.length)
			{
			myarr[i][j+i]=letter[i][k];
			k++;
			}
		k=0;
		}
		
	var output="<table border='0'>";
	for(var i=0;i<myarr.length;i++)
	{
	output=output+"<tr>"
		for(var j=0;j<myarr[i].length;j++)
		{
			if(myarr[i][j]!=null)
			{output=output+"<td>"+myarr[i][j]+"</td>";}
			else
			{output=output+"<td>&nbsp;</td>";}
		}
	output=output+"</tr>"
	}
	output=output+"</table>";
		
	
	
	

document.frm.output.value = "<font face='arial'><b>Create Rail Fence<br><br>Size of Shift : </b>" + document.form1.num.value +"<br><br>" + output + "</font>";
window.open("railfencewin.htm", "winP", "top=40, left=40, width=400, height=300,resizable=1, status=1");


////deleting the values of two arrays

for(var i=letter.length;i>=0;i--)
	{
	letter[i]=null;
	}
for(var i=myarr.length;i>=0;i--)
	{
	myarr[i]=null;
	}

}// end of function





/////////////////////////////////////////////////////////////////////////////////
// The following function take spaces from the plain Text
/////////////////////////////////////////////////////////////////////////////////



function takeSpace(str)
{

	// get length of the string

	lnth=str.length;

	// initialize a string variable that hold the no space string variable

	var nospstr="";

	for(i=0;i<lnth;i++)
	{

		if(str.charAt(i)!=" ")
		{

			nospstr+=str.charAt(i);

		}

	}

	return nospstr;

}


function showCipher()
{

		var show1="";
		for(i=0;i<row;i++)
		{
		for(j=0;j<column;j++)
			{
				//get index
			
				index=i+row*j;
			
				// if index is greater than number then not include the number
			
				if(index<document.form2.plainText.value.length && my_letter[i][j]!=" ")
				{
			
					show1+=my_letter[i][j];
			
				}
		
			}
	
		}
		
		
		document.form3.cipherText.value=show1.toUpperCase();
}


////////////////////////////////////////////////////////////////////
// The following function is used to decipher the decrypted text
////////////////////////////////////////////////////////////////////


function decipher()
{
	var letter1=new Array();

	// get the value of num textfield to construct row of array.
	
	row=parseInt(document.form1.num.value);
	
	// get the plainText
	
	var cipherText=document.form3.cipherText.value;
	
	
	// get the length of plaintext
	
	len=cipherText.length;	
	
	
	// get the length of the plaintext and devide it with row
	// to get the column of the railfence
	
	column=Math.ceil(cipherText.length/row);
	
	
	// reminder to get how many row will be full
	
	rem=len%row;
	
	// count will be used to traverse the plaintext
	
	var count=0;
	
	
	////////////////////////////////////////////////////////////////////////////
	// The following loop will be used to assign letter array for decipher.
	////////////////////////////////////////////////////////////////////////////
	
	
	for(i=0;i<row;i++)
	{
	
		letter1[i]=new Array();
	
		for(j=0;j<column;j++)
		{
			
			if(i+1>rem && j==column-1 && rem!=0)
			{
			
				char=" ";
			
			}
			else
			{
			
				char=cipherText.charAt(count);
				count++;
			
			}
		
			letter1[i][j]=char;
			
			//alert("["+i+"]["+j+"]="+char);
			
			
		
		}
	
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////
	// The following function is used to show the above array in alert.
	/////////////////////////////////////////////////////////////////////////////////
	
	var output="";
	
	for(i=0;i<row;i++)
	{
	
		for(j=0;j<column;j++)
		{
		
			output+=letter1[i][j]+"\t";
		
		}
		output+="<br>";
	
	}
	
//	alert(output);
	document.frm.output.value = "<font face='arial'><b>Create Rail Fence<br><br>Size of Shift : </b>" + document.form1.num.value +"<br><br>" + output + "</font>";
	window.open("railfencewin.htm", "winP", "top=40, left=40, width=400, height=300,resizable=1, status=1");

	
	/////////////////////////////////////////////////////////////////////////////////
	// The following loop will be used to show the resultant deciphered output.
	/////////////////////////////////////////////////////////////////////////////////
	
	
	var output="";
	
	for(j=0;j<column;j++)
	{
	
		for(i=0;i<row;i++)
		{
		
			output+=letter1[i][j];
		
		}
	
	}
	
	document.form2.plainText.value=output.toLowerCase();	
		

}



///////////////////////////////////////////////////////////
// The following function is used for puzzle
///////////////////////////////////////////////////////////


function setText()
{

	//document.form3.cipherText.value="DOLOHRKOTANEASNPYTTC";(PREVIOUSLY)
	document.form3.cipherText.value="DLTAOAHCNYEKTOTSPNR";//ACCORDING TO CD

}




/////////////////////////////////////////////////////////////
// The following function print the cipher text
/////////////////////////////////////////////////////////////

function printCipher()
{

	if((document.form3.cipherText.value=="")||(document.form3.cipherText.value==0))
	{
	
		alert("Sorry! No text to print");
	
	}
	else
	{
	
		window.open("relWin.htm","","top=100, left=300, width=200, height=150, resizable=1,status=1");
	
		//alert("dsfs");
	}
}





///////////////////////////////////////////////////////////
// The function incNum() and decNum() increase and decrease
// the number of textbox num.
///////////////////////////////////////////////////////////


function incNum()
{
	var number=parseInt(document.form1.num.value);

	// if the value of the textbox is 9 then set it 2
	
	if(number>=9)
	{
	
		document.form1.num.value="2";
		number=parseInt(document.form1.num.value);
	
	}
	else
	{
		number++;
		document.form1.num.value=number;
	}

}


function decNum()
{
	var number=parseInt(document.form1.num.value);

	// if the value of the textbox is 2 then set it 9

	if(number<=2)
	{
	
		document.form1.num.value="9";
		number=parseInt(document.form1.num.value);
	
	}
	else
	{
		number--;
		document.form1.num.value=number;
	}

}



//////////////////////////////////////////////////////////////////////
// The following function show options for printing cipher.
//////////////////////////////////////////////////////////////////////

function printCipher()
{
	document.form3.actual.value=document.form2.plainText.value;
	document.form3.encpt.value=document.form3.cipherText.value;

	printWin=window.open("optionWin.htm", "winP", "top=40, left=40, width=450, height=300, status=1");

}

function clearbox()
{
	document.form2.plainText.value ="";
	document.form3.cipherText.value ="";
}

