/**

 *   ISPDetRep.js -

 *   @version   $Name:  $

 *   @module

 *

 *   @purpose

 *   @see

 *

 *   @author    B.Jayakumar  (jayakumar@kuruvi.ooty.tenet.res.in)

 *

 *   @created        Tue 26-Aug-2008 12:08:51

 *   Last modified:  Thu 30-Oct-2008 10:47:37 by root

 *   $Id: ISPDetRep.js,v 1.11 2008/12/08 08:52:01 raj Exp $

 *

 *   @bugs

 *

 *   Change Log:        <Date> <Author>

 *              <Changes>

 */


var http = getHTTPObject(); // We create the XMLHTTPRequest Object



/*-------------------------------------------------------------------------

 *    getHTTPObject -- Get the HTTP object

 *    Args: Nothing

 *    Returns: HTTP object

 -------------------------------------------------------------------------*/

function getHTTPObject(){

    var xmlhttp;

    if (window.XMLHttpRequest) {

	xmlhttp = new XMLHttpRequest();

    } else if (window.ActiveXObject) {

	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    } return xmlhttp;

}



/*-------------------------------------------------------------------------

 *    getRegionList -- Get the list of region for the given country

 *    Args: argCountry -- Country name

 *    Returns:

 -------------------------------------------------------------------------*/

function getRegionList(argCountry)

{
  var sendVal = "";

  sendVal += "ISPSumRepOperation.php?report=getRegionList&country=" + escape(argCountry);

  var elementSel = document.getElementById('selectState');

  for(var incLen = 0; elementSel.length = 0; incLen++)

    {

      elementSel.options[elementSel.length - 1] = null;

    }



  document.getElementById("selectState").options[0] = new Option("------SELECT------");

  document.getElementById("selectState").options[0].value = "";



  var elementSel = document.getElementById('selectISP');

  for(var incLen = 0; elementSel.length > incLen; incLen++)

    {

      elementSel.options[elementSel.length - 1] = null;

    }

  document.getElementById("selectISP").options[0] = new Option("------SELECT------");

  document.getElementById("selectISP").options[0].value = "";



  var elementSel = document.getElementById('selectDownloadSpeed');

  for(var incLen = 0; elementSel.length > incLen; incLen++)

    {

      elementSel.options[elementSel.length - 1] = null;

    }

  document.getElementById("selectDownloadSpeed").options[0] = new Option("------SELECT------");

  document.getElementById("selectDownloadSpeed").options[0].value = "";



  hideTypeTest();

  document.getElementById("ISPSummaryReportView").innerHTML = "";

  document.getElementById("footNote1").innerHTML = "";



  if(argCountry != "")

    {

      /*

       * Send the details.

       */

      http.open("GET", sendVal, true);

      http.onreadystatechange = displayRegionList;

      http.send(null);

    }

}



/*-------------------------------------------------------------------------

 *    displayRegionList -- Display the list of region for the given country

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function displayRegionList()

{

    /*

     * Check for the states and the status

     * according to the result display the message.

     */

    if (http.readyState == 4)

	{

	    if (http.status == 200)

		{

		    var message = http.responseText;

		    var result = message.split("^");

		    document.getElementById("selectState").options[0] = new Option("------SELECT------");

		    document.getElementById("selectState").options[0].value = "";

		    var j = 0;

		    for(var i = 0; i < result.length - 1; i++)

			{

			    j = i + 1;

			    document.getElementById("selectState").options[j] = new Option(result[i]);

			    document.getElementById("selectState").options[j].value = result[i];

			}

		}

	    else

		{

		    alert("Unable to update database on the server.\nPlease try again, if the problem persist, contact the administrator.");

		}

	}

    else

	{

	    // State = 0 - 4 are the intermediate states, so just show loading.

	}

}



/*-------------------------------------------------------------------------

 *    getISPList -- Get the list of ISP for the given region

 *    Args: argRegion -- region name

 *    Returns:

 -------------------------------------------------------------------------*/

function getISPList(argRegion)

{

  var sendVal = "";

  sendVal += "ISPSumRepOperation.php?report=getISPList&region=" + escape(argRegion);

  sendVal += "&selectCountry=" + escape(document.getElementById("selectCountry").value);



  var elementSel = document.getElementById('selectISP');

  for(var incLen = 0; elementSel.length = 0; incLen++)

    {

      elementSel.options[elementSel.length - 1] = null;

    }

  document.getElementById("selectISP").options[0] = new Option("------SELECT------");

  document.getElementById("selectISP").options[0].value = "";



  var elementSel = document.getElementById('selectDownloadSpeed');

  for(var incLen = 0; elementSel.length > incLen; incLen++)

    {

      elementSel.options[elementSel.length - 1] = null;

    }

  document.getElementById("selectDownloadSpeed").options[0] = new Option("------SELECT------");

  document.getElementById("selectDownloadSpeed").options[0].value = "";



  hideTypeTest();



  document.getElementById("ISPSummaryReportView").innerHTML = "";

  document.getElementById("footNote1").innerHTML = "";

  if(argRegion != "")

    {

      /*

       * Send the details.

       */

      http.open("GET", sendVal, true);

      http.onreadystatechange = displayISPList;

      http.send(null);

    }

}



/*-------------------------------------------------------------------------

 *    displayISPList -- Display the list of ISP for the given region

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function displayISPList()

{

    /*

     * Check for the states and the status

     * according to the result display the message.

     */

    if (http.readyState == 4)

	{

	    if (http.status == 200)

		{

		    var message = http.responseText;

		    var result = message.split("^");

		    document.getElementById("selectISP").options[0] = new Option("------SELECT------");

		    document.getElementById("selectISP").options[0].value = "";

		    var j = 0;

		    for(var i = 0; i < result.length - 1; i++)

			{

			    j = i + 1;

			    document.getElementById("selectISP").options[j] = new Option(result[i]);

			    document.getElementById("selectISP").options[j].value = result[i];

			}

		}

	    else

		{

		    alert("Unable to update database on the server.\nPlease try again, if the problem persist, contact the administrator.");

		}

	}

    else

	{

	  // State = 0 - 4 are the intermediate states, so just show loading.

	}

}



/*-------------------------------------------------------------------------

 *    getDownloadSpeedValues -- Get the list of download speed for the given region

 *    Args: argDownSpeen -- download speen name

 *    Returns:

 -------------------------------------------------------------------------*/

function getDownloadSpeedValues(argDownSpeen)

{

  var sendVal = "";

  sendVal += "ISPSumRepOperation.php?report=getDownloadSpeedValues&ISPName=" + escape(argDownSpeen);

  var elementSel = document.getElementById('selectDownloadSpeed');

  for(var incLen = 0; elementSel.length = 0; incLen++)

    {

      elementSel.options[elementSel.length - 1] = null;

    }

  document.getElementById("selectDownloadSpeed").options[0] = new Option("------SELECT------");

  document.getElementById("selectDownloadSpeed").options[0].value = "";



  hideTypeTest();



  document.getElementById("ISPSummaryReportView").innerHTML = "";

  document.getElementById("footNote1").innerHTML = "";

  if(argDownSpeen != "")

    {

      /*

       * Send the details.

       */

      http.open("GET", sendVal, true);

      http.onreadystatechange = displayDownloadSpeed;

      http.send(null);

    }

}



/*-------------------------------------------------------------------------

 *    displayDownloadSpeed -- Display the list of ISP for the given region

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function displayDownloadSpeed()

{

    /*

     * Check for the states and the status

     * according to the result display the message.

     */

    if (http.readyState == 4)

	{

	    if (http.status == 200)

		{

		    var message = http.responseText;

		    var result = message.split("^");

		    document.getElementById("selectDownloadSpeed").options[0] = new Option("------SELECT------");

		    document.getElementById("selectDownloadSpeed").options[0].value = "";

		    var j = 0;

		    for(var i = 0; i < result.length - 1; i++)

			{

			    j = i + 1;

			    document.getElementById("selectDownloadSpeed").options[j] = new Option(result[i]);

			    document.getElementById("selectDownloadSpeed").options[j].value = result[i];

			}

		}

	    else

		{

		    alert("Unable to update database on the server.\nPlease try again, if the problem persist, contact the administrator.");

		}

	}

    else

	{

	    // State = 0 - 4 are the intermediate states, so just show loading.

	}

}



/*-------------------------------------------------------------------------

 *    getISPType -- Get the list of ISP Types

 *    Returns:

 -------------------------------------------------------------------------*/

function getISPType(argDownSpeed)

{

  var sendVal = "";



  sendVal += "ISPSumRepOperation.php?report=getISPType&Dwld_Speed=" + escape(argDownSpeed);

  sendVal += "&region=" + document.getElementById("selectState").value;

  sendVal += "&selectCountry=" + document.getElementById("selectCountry").value;

  sendVal += "&ISP_Name=" + document.getElementById("selectISP").value;



  hideTypeTest();



  document.getElementById("ISPSummaryReportView").innerHTML = "";

  document.getElementById("footNote1").innerHTML = "";

  if(argDownSpeed != "")

    {

      /*

       * Send the details.

       */

      http.open("GET", sendVal, true);

      http.onreadystatechange = displayISPType;

      http.send(null);

    }

}



/*-------------------------------------------------------------------------

 *    displayISPType -- Display the list of ISP type

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function displayISPType()

{

    /*

     * Check for the states and the status

     * according to the result display the message.

     */

    if (http.readyState == 4)

	{

	    if (http.status == 200)

		{

		    var message = http.responseText;

		    var result = message.split("^");

		    document.getElementById("selectISPType").options[0] = new Option("------SELECT------");

		    document.getElementById("selectISPType").options[0].value = "";

		    var j = 0;

		    for(var i = 0; i < result.length - 1; i++)

			{

			    j = i + 1;

			    document.getElementById("selectISPType").options[j] = new Option(result[i]);

			    document.getElementById("selectISPType").options[j].value = result[i];

			}

		}

	    else

		{

		    alert("Unable to update database on the server.\nPlease try again, if the problem persist, contact the administrator.");

		}

	}

    else

	{

	    // State = 0 - 4 are the intermediate states, so just show loading.

	}

}



/*-------------------------------------------------------------------------

 *    displayTypeTest -- Display the type of test

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function displayTypeTest()

{

  if(document.getElementById("selectDownloadSpeed").value == "")

    {

      hideTypeTest();

      document.getElementById("ISPSummaryReportView").innerHTML = "";

      document.getElementById("footNote1").innerHTML = "";

    }

  else

    {

      document.getElementById("typeOfTestDiv").style.visibility = "visible";

      document.getElementById("typeOfTestDispDiv").style.visibility = "visible";

    }

}



/*-------------------------------------------------------------------------

 *    hideTypeTest -- Hide the type of test

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function hideTypeTest()

{

  document.getElementById("typeOfTestDiv").style.visibility = "hidden";

  document.getElementById("typeOfTestDispDiv").style.visibility = "hidden";

  document.getElementById("selectTypeTest").value = "";

}



/*-------------------------------------------------------------------------
 *  compareGivenDates -- Compare between two dates.
 *    Args: startDate
 *    Args: endDate
 *    Returns:
 -------------------------------------------------------------------------*/
function compareGivenDates(startDate,endDate)
{
  var startDateArray = startDate.split(/s*-s*/);
  var endDateArray = endDate.split(/s*-s*/);
  if(endDateArray[2] < startDateArray[2])
    {
      return false;
    }

  if(endDateArray[2] == startDateArray[2])
    {
      if(endDateArray[1] < startDateArray[1])
	{
	  return false;
	}
    }

  if(endDateArray[2] == startDateArray[2])
    {
      if(endDateArray[1] == startDateArray[1])
	{
	  if(endDateArray[0] < startDateArray[0])
	    {
	      return false;
	    }
	}
    }
  return true;
}  /*  End of compareGivenDates            End of compareGivenDates   */

/*-------------------------------------------------------------------------

 *    getDateTableValues -- Get the list of values to be displayed

 *    Args: argCountry -- Country name

 *    Args: argRegion -- Region name

 *    Returns:

 -------------------------------------------------------------------------*/

function getDateTableValue()

{

    var sendVal = "";

    sendVal += "ISPSumRepOperation.php?report=getDateTableValues";
    sendVal += "&selectCountry=" + escape(document.getElementById("selectCountry").value);
    sendVal += "&selectState=" + escape(document.getElementById("selectState").value);
    sendVal += "&selectISP=" + escape(document.getElementById("selectISP").value);
    sendVal += "&selectDownloadSpeed=" + escape(document.getElementById("selectDownloadSpeed").value);
    sendVal += "&startDate=" + escape(document.getElementById("startDate").value);
    sendVal += "&endDate=" + escape(document.getElementById("endDate").value);
    sendVal += "&selectTypeTest=" + escape(document.getElementById("selectTypeTest").value);
    sendVal += "&ispType=Residential";
    var errorMsg = "";
    document.getElementById("ISPSummaryReportView").innerHTML = "";
    document.getElementById("footNote1").innerHTML = "";

    if(document.getElementById("selectCountry").value == "") {
      errorMsg += "Select valid country\n";
    }
    if(document.getElementById("selectState").value == "") {
      errorMsg += "Select valid region\n";
    }
    if(document.getElementById("selectISP").value == "") {
      errorMsg += "Select valid ISP name\n";
    }
    if(document.getElementById("selectDownloadSpeed").value == "") {
      errorMsg += "Select valid download speed\n";
    }
    if(document.getElementById("startDate").value == "") {
      errorMsg += "Select valid start date\n";
    }
    if(document.getElementById("endDate").value == "") {
      errorMsg += "Select valid end date\n";
    }
    if(!compareGivenDates (document.getElementById("startDate").value, document.getElementById("endDate").value)){
      errorMsg += "End date should be greater than start date.";
    }

    if(errorMsg == ""){
      /*

       * Send the details.

       */
      http.open("GET", sendVal, true);
      http.onreadystatechange = displayTableValues;
      http.send(null);
    } else {
      alert(errorMsg);
    }
}


/*-------------------------------------------------------------------------

 *    getTableValues -- Get the list of values to be displayed

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function getTableValues()

{

    var selectISP = document.getElementById("selectISP").value;

    var selectState = document.getElementById("selectState").value;

    var selectCountry = document.getElementById("selectCountry").value;

    var selectDownloadSpeed = document.getElementById("selectDownloadSpeed").value;

    var selectISPType = "Residential";

    var selectTypeTest = document.getElementById("selectTypeTest").value;



    var sendVal = "";

    sendVal += "ISPSumRepOperation.php?report=getISPTableValues";

    sendVal += "&country=" + escape(selectCountry);

    sendVal += "&region=" + escape(selectState);

    sendVal += "&ISP=" + escape(selectISP);

    sendVal += "&DownloadSpeed=" + escape(selectDownloadSpeed);

    sendVal += "&ISPType=" + escape(selectISPType);

    sendVal += "&TypeTest=" + escape(selectTypeTest);



    if(selectTypeTest == "")

	{

	  document.getElementById("ISPSummaryReportView").innerHTML = "";

	  document.getElementById("footNote1").innerHTML = "";

	}

    else

	{

	    /*

	     * Send the details.

	     */

	    http.open("GET", sendVal, true);

	    http.onreadystatechange = displayTableValues;

	    http.send(null);

	}

}



/*-------------------------------------------------------------------------

 *    displayTableValues -- Display the values to be displayed

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function displayTableValues()

{

    /*

     * Check for the states and the status

     * according to the result display the message.

     */

    if (http.readyState == 4)

	{

	    if (http.status == 200)

		{

	

		    var message = http.responseText;

		    var results = message.split("#");

		    var displayTable = "";

		   

		      /*Included by ragu[15/09/2008] - To display total number test executed*/
		      var selectISP = document.getElementById("selectISP").value;
		      var selectState = document.getElementById("selectState").value;
		      var selectCountry = document.getElementById("selectCountry").value;
		      var selectDownloadSpeed = document.getElementById("selectDownloadSpeed").value;
		      var selectISPType = "Residential";
		      var selectTypeTest = document.getElementById("selectTypeTest").value;
		      displayTable += "<div style='font-family:Arial;font-size:14px;'>Country : " + selectCountry + ".<br>Region : " + selectState + ".<br>ISP Name : " + selectISP + ".<br>Download speed : " + selectDownloadSpeed + ".<br>Type of Test : " + selectTypeTest + ".<br></div>";

		    displayTable += "<div style='font-family:Arial;font-size:14px;'>Total number of test(s) executed:"+results[2]+"</div>";

   	

		    /*Commented by ragu[15/09/2008]*/	

		    //displayTable += displayISPSummaryReport(results[0],"Week End");

		

		    /*Included by ragu[15/09/2008]*/

		    /*Modified by vinoth[20/09/2008] - To display the weekend*/

		    displayTable += displayISPSummaryReport(results[0],"Week End("+results[3]+")");

		

	   	    /*Commented by ragu[15/09/2008]*/	

		    //displayTable += displayISPSummaryReport(results[1],"Week Day");

	

	 	    /*Included by ragu[15/09/2008]*/

		    /*Modified by vinoth[20/09/2008] - To display the weekday*/

		    displayTable += displayISPSummaryReport(results[1],"Week Day("+results[4]+")");

		    document.getElementById("ISPSummaryReportView").innerHTML = displayTable;

		    document.getElementById("footNote1").innerHTML = "<font size='2' face='Arial, Helvetica, sans-serif'>* We have given here all possible speeds. Please note some of them might not be offered by your ISP.";



		}

	    else

		{

		    alert("Unable to update database on the server.\nPlease try again, if the problem persist, contact the administrator.");

		}

	}

    else

	{

	    // State = 0 - 4 are the intermediate states, so just show loading.

	}

}



/*-------------------------------------------------------------------------

 *    displayISPSummaryReport -- Display the values to be displayed

 *    Args: 

 *    Returns:

 -------------------------------------------------------------------------*/

function displayISPSummaryReport(argVal,typeOfDay)

{

    var tableValues = argVal.split("@");

    var displayTable = "";

    displayTable += "<table width='100%' cellpadding='2' cellspacing='2'>";

    displayTable += "<tr bgcolor='#CC3333'><td height=20 colspan=7 class='tablesubheading'>";

    displayTable += "<font color='#FFFFFF' size='2' face='Arial, Helvetica, sans-serif'><strong>"+typeOfDay+"</strong></font></td></tr>";

    displayTable += "<tr>";

    displayTable += "<td width='16%' height='20' bgcolor='#CC3333' class='tablesubheading'> ";

    displayTable += "<div align='center'><strong><font size='2' face='Arial, Helvetica, sans-serif'>Time of ";

    displayTable += "Day </font></strong></div></td>";

    displayTable += "<td width='16%' bgcolor='#CC3333' class='tablesubheading'> ";

    displayTable += "<div align='center'><strong><font size='2' face='Arial, Helvetica, sans-serif'>Download ";

    displayTable += "Speed (kbps)</font></strong></div></td>";

    displayTable += "<td width='12%' bgcolor='#CC3333' class='tablesubheading'> ";

    displayTable += "<div align='center'><strong><font size='2' face='Arial, Helvetica, sans-serif'>Upload ";

    displayTable += "Speed (kbps)</font></strong></div></td>";

    displayTable += "<td width='14%' bgcolor='#CC3333' class='tablesubheading'> ";

    displayTable += "<div align='center'><strong><font size='2' face='Arial, Helvetica, sans-serif'>RTT (ms)";

    displayTable += "<td width='14%' bgcolor='#CC3333' class='tablesubheading'>";

    displayTable += "<div align='center'><strong><font size='2' face='Arial, Helvetica, sans-serif'>Jitter (ms)</font></strong></div></td>";

    displayTable += "<td width='15%' bgcolor='#CC3333' class='tablesubheading'> ";

    displayTable += "<div align='center'><strong><font size='2' face='Arial, Helvetica, sans-serif'>Availability ";

    displayTable += " (%)</font></strong></div></td>";

    displayTable += "<td width='15%' bgcolor='#CC3333' class='tablesubheading'> ";

    displayTable += "<div align='center'><strong><font size='2' face='Arial, Helvetica, sans-serif'>Packet ";

    displayTable += "Loss (%)</font></strong></div></td>";

    displayTable += "</tr>";

	 

    for(var incTbl = 0; incTbl < tableValues.length - 1; incTbl++)

	{

	    var rowValues = tableValues[incTbl].split("^");

	    displayTable += "<tr bgcolor='#F8EADA'>";

	    for(var incVal = 0; incVal < rowValues.length - 1; incVal++)

		{

		  /* Added by Raj for displaying the times*/

		  if(rowValues[incVal] == "15:00 PM")

		    {

		      rowValues[incVal]="3:00 PM";

		    }



		  if(rowValues[incVal] == "18:00 PM")

		    {

		      rowValues[incVal]="6:00 PM";

		    }



		  if(rowValues[incVal] == "20:00 PM")

		    {

		      rowValues[incVal]="8:00 PM";

		    }



		  if(rowValues[incVal] == "23:00 PM")

		    {

		      rowValues[incVal]="11:00 PM";

		    }



		  /*------*/



		  displayTable += "<td align=right height='20' class='tablesubheading'> <font size='2' face='Arial, Helvetica, sans-serif'>" + rowValues[incVal] + "</td>";

		}

	    displayTable += "</tr>";

	}

    return displayTable;

}



/**

 *  $Log: ISPDetRep.js,v $

 *  Revision 1.11  2008/12/08 08:52:01  raj

 *  Updated the javascript file for handling region lists

 *

 *  Revision 1.10  2008/10/30 08:39:22  raj

 *  Removing of ISP_Type Business

 *

 *  Revision 1.9  2008/10/20 04:58:23  raj

 *  Spelling mistake change

 *

 *  Revision 1.8  2008/10/18 07:13:49  raj

 *  Final Changes Updations

 *

 *  Revision 1.7  2008/10/14 11:24:53  raj

 *  Updation of beta testing bugs/suggesstion

 *

 *  Revision 1.6  2008/10/06 08:38:45  raj

 *  Updated changes reported in beta testing

 *

 *  Revision 1.5  2008/09/20 07:13:06  ragu

 *  to specify weekend for different isp or countries

 *

 *  Revision 1.2  2008/09/04 07:07:01  raj

 *  ReleaseQA_V11

 *

 *  Revision 1.1  2008/08/28 07:40:14  raj

 *  ReleaseQA_V10

 *

 *

 *  Local Variables:

 *  time-stamp-active: t

 *  time-stamp-line-limit: 20

 *  time-stamp-start: "Last modified:[  ]+"

 *  time-stamp-format: "%3a %02d-%3b-%:y %02H:%02M:%02S by %u"

 *  time-stamp-end: "$"

 *  End:

 *                         End of ISPDetRep.js

 */

