//Global XMLHTTP Request object
var XmlHttp; var XmlHttp1,XmlHttp2; var output; var count='value'; var head=''; var scanoutput=''; 
var portarray; var length; var arrindex; var stat='';

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}



function CreateXmlHttp1()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp1 = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp1 = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp1 && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp1 = new XMLHttpRequest();
	}
}



//function GetPorts() 
//{
//    var ip=document.getElementById('ctl00_mprCenterColumn_ctl00_ctl00_hidIp');
//    //var ip=document.getElementById('PortScanner1_1_hidIp');
//    var ip1=ip.value;
//    head="<table><tr align=\"left\"><td style=\" font-size:small; font-size:14px;\">"+
//    "<strong>Port Scan Result of IP :-</strong><span style=\"font-weight:bold;\">"+ip1+"</span></td></tr>"+
//    "<tr><td>&nbsp;</td></tr><tr><td align=\"center\" style=\"height: 14px\"><table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-color:Silver;border-width:1px;border-style:solid;width:400px;border-collapse:collapse;\">"+
//    "<tr style=\"color:menutext;background-color:Silver;font-size:12px;font-weight:bold;\"><th scope=\"col\">Status</th><th scope=\"col\">Port</th><th scope=\"col\">Information</th></tr>";
//	scanoutput=head;
//	var requestUrl = AjaxServerPageName+"?task=ports";
//	CreateXmlHttp();
//	// If browser supports XMLHTTPRequest object
//	if(XmlHttp)
//	{
//	    var timestamp = new Date();
//        var uniqueURI = requestUrl+ (requestUrl.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
//	    
//		//Setting the event handler for the response
//		XmlHttp.onreadystatechange = HandleResponse;
//		
//		//Initializes the request object with GET (METHOD of posting), 
//		//Request URL and sets the request as asynchronous.
//		try
//		{
//		XmlHttp.open("GET", uniqueURI,  true);
//		}
//		catch(e)
//		{
//		    alert('exception'+e);
//		}
//		
//		//Sends the request to server
//		XmlHttp.send(null);		
//	}
//	//alert('fun GetPorts ' +output);
//	return output;
//}

function ScanPorts(port) 
{
    
    showProgress();
    
	var requestUrl = AjaxServerPageName+"?port="+port; 
	//alert(requestUrl);
	
	// XmlHttp1=null;
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	
	if(XmlHttp)
	{	    
	    var timestamp = new Date();
        var uniqueURI = requestUrl+ (requestUrl.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
        
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponseScan;
	    
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", uniqueURI,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
	//alert('ScanPort fun' +output);
	
}


////called as result of ScanPorts
//function HandleResponseScan()
//{
//	
//	// To make sure receiving response data from server is completed
//	
//	if(XmlHttp.readyState == 4)
//	{
//	    // To make sure valid response is received from the server, 200 means response received is OK
//		if(XmlHttp.status == 200)
//		{		
//		    
//		    resp=XmlHttp.responseText;
//		    var pos=resp.indexOf('&');
//		    resp=resp.substr(0,pos);
//		    
//		    scanoutput=scanoutput+resp;
//		    //alert(scanoutput);
//		  
//		    if(arrindex==length-1)
//		    {
//		        updateprogress(arrindex+1);
//		    }
//		    fillgrid(scanoutput);
//		    
//		    //updateprogress(arrindex);
//		    
//		    if(arrindex<length-1)
//		    {
//	    	    arrindex++;
//    	        ScanPorts(portarray[arrindex]);
//    	        updateprogress(arrindex);
//      	    }
//    	    else
//    	    {
//    	        hideProgress();
//    	        showstatistics(); 
//       	    }
//		}
//		else
//		{	    	    
//		    
//		    alert("There was a problem retrieving data from the server.Please try again");
//		    divdynamic= document.getElementById('divdynamicprog');
//            divdynamic.style.width="0px";
//			cleargrids();
//		}
//	}
//}


function updateprogress(val)
{   
    //alert(val+' '+length);
    
    var width=(val/length)*220;
    //alert('width'+width);
    //alert();
    divdynamic= document.getElementById('divdynamicprog');
    divdynamic.style.width=width+"px";
}



function UpdateTestCount()
{
    
   var requestUrl = AjaxServerPageName+"?task=count";
   // alert(requestUrl);
 
   CreateXmlHttp();
    	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
	    var timestamp = new Date();
        var uniqueURI = requestUrl+ (requestUrl.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponsecount;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", uniqueURI,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	} 
}


function showstatistics()
{
  //alert('In showstatistics');
   var requestUrl = AjaxServerPageName+"?task=stat";
   //alert(requestUrl);
   CreateXmlHttp();
    	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{	 
	    var timestamp = new Date();
        var uniqueURI = requestUrl+ (requestUrl.indexOf("?") > 0 ? "&" : "?")+ "timestamp="+ timestamp.getTime();
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponseStat;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", uniqueURI,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	} 
	//alert("show statistics" +scanoutput);
}


//function HandleResponsecount()
//{
//	// To make sure receiving response data from server is completed
//	if(XmlHttp.readyState == 4)
//	{
//		// To make sure valid response is received from the server, 200 means response received is OK
//		if(XmlHttp.status == 200)
//		{		   
//		    count=XmlHttp.responseText;
//		    showstat(stat);
//		  
//		}
//		else
//		{
//		    //HandleResponse();
//		    divdynamic= document.getElementById('divdynamicprog');
//            divdynamic.style.width="0px";
//			cleargrids();
//			
//			alert("There was a problem retrieving data from the server.Please try again" );
//			
//		}
//	}
//}

////Called when response comes back from server
//function HandleResponse()
//{
//	// To make sure receiving response data from server is completed
//	if(XmlHttp.readyState == 4)
//	{
//		// To make sure valid response is received from the server, 200 means response received is OK
//		if(XmlHttp.status == 200)
//		{		   
//		    output=XmlHttp.responseText;
//		    var pos=output.indexOf('<');
//            var ports=output.substr(0,pos);
//		    scan(ports);
//		}
//		else
//		{
//		    //HandleResponse();
//		    divdynamic= document.getElementById('divdynamicprog');
//            divdynamic.style.width="0px";
//			cleargrids();
//		    alert("There was a problem retrieving data from the server.Please try again" );
//		   
//		}
//	}
//}


//function HandleResponseStat()
//{
//    
//    // To make sure receiving response data from server is completed
//	if(XmlHttp.readyState == 4)
//	{
//	   	// To make sure valid response is received from the server, 200 means response received is OK
//		if(XmlHttp.status == 200)
//		{	
//		    stat=XmlHttp.responseText;
//		    UpdateTestCount();
//		    
//		}
//		else
//		{
//		   // HandleResponseStat();
//		  divdynamic= document.getElementById('divdynamicprog');
//          divdynamic.style.width="0px";
//          cleargrids();
//		  alert("There was a problem retrieving data from the server.Please try again" );
//		  
//		}
//	}
//}
function scan(ports)
{
   //alert('In scan');
    portarray=ports.split(',');
    length=portarray.length;
    
    var output2;
    arrindex=0;
    output2=ScanPorts(portarray[arrindex]);
    
    
}

//function showProgress()
//{
//   dynamicprog=document.getElementById('divoutertab');
//   dynamicprog.style.display="block";
//   prog= document.getElementById('divprogress');
//   
//   /*prog.innerHTML="<object style=\"z-index:0\" data=\"../img/Scanning.swf\" type=\"application/x-shockwave-flash\"  hspace=\"0\" vspace=\"0\">"+
//                   "<param name=\"wmode\" value=\"transparent\" /><param name=\"movie\" value=\"img/Scanning.swf\" /></object>";*/
//   prog.style.display="block";                   
//   
//   pcimage= document.getElementById('divpc');
//   pcimage.style.display="none";
//   
//   label= document.getElementById('divlabel');
//   label.innerHTML="<span style=\"font-size:14px;font-weight:bold; color:red\" >Scanning...</span>";
//}



//function hideProgress()
//{
//   label= document.getElementById('divlabel');
//   label.innerHTML="<span style=\"font-size:14px;font-weight:bold; color:red\" >Completed</span>";
//   
//   prog= document.getElementById('divprogress');
//   prog.style.display="none";
//   
//   pcimage= document.getElementById('divpc');
//   pcimage.style.display="block";
//   //pcimage.innerHTML='<img src=\"img/cpu_still.gif\" />';
//   
//   /*dynamicprog=document.getElementById('divoutertab');
//   dynamicprog.style.display="none";
//   prog= document.getElementById('divprogress');
//   prog.style.display="none";
//   divdynamic= document.getElementById('divdynamicprog');
//   divdynamic.style.width="0px";*/
//}


//function cleargrids()
//{
//    grid=document.getElementById('divgrid');
//    grid.innerHTML='';
//    gridstat=document.getElementById('ctl00_mprCenterColumn_ctl00_ctl00_divstat');
//    //gridstat=document.getElementById('PortScanner1_1_divstat');
//    gridstat.innerHTML='';
//}


function fillgrid(gridcontent)
{
   grid=document.getElementById('divgrid');
   grid.innerHTML = gridcontent+"</table></table>";
}


//function showstat(stat)
//{    
//   if(stat.indexOf('table')>1)
//   {
//   //alert(count); 
//   var head="<tr><td style=\"font-size:12px\">Number of times this test has been used: "+count+"</td></tr><br> "+
//  
//   "<tr><td style=\"font-size:14px\"><strong>Statistics:-</strong></td></tr><br>";
//   //"<tr><td style=\"font-size:12px\">Number of times this test has been used: "+count+"</td></tr><br> ";
//   stat=head+stat;
//   gridstat=document.getElementById('ctl00_mprCenterColumn_ctl00_ctl00_divstat');
//   //gridstat=document.getElementById('PortScanner1_1_divstat');
//   gridstat.innerHTML=stat;
//   }
//   else
//   {
//    gridstat=document.getElementById('ctl00_mprCenterColumn_ctl00_ctl00_divstat');
//    //gridstat=document.getElementById('PortScanner1_1_divstat');
//    gridstat.innerHTML="<tr width=\"300\" ><td style=\"font-size:12px\">Number of times this test has been used: "+count+"</td></tr><br> ";
//   }
//   
//   //head=document.getElementById('spanhead');
//   head=document.getElementById('ctl00_mprCenterColumn_ctl00_ctl00_lblStartOnlinePortScan');
//   head.innerHTML='Scan Again';
//  
//}











