var xmlHttp = createXmlHttpRequestObject();
var limit;
var total;
var id_cat;

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    try
    {
      xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.6.0");
    }
    catch(e) { }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function process()
{
   
  // only continue if xmlHttp isn't void
	
  if (xmlHttp)
  {
   // try to connect to the server
    try
    {

   xmlHttp.open("GET", "process_leadsnewsletter.php?limit=" +limit +"&id_cat=" +id_cat, true)
   xmlHttp.onreadystatechange = process_email;
	xmlHttp.send(null)
//alert("limit is:" +limit +"and total is:" +total);
 }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
 // setTimeout("process(limit);",15);
}

// function that handles the http response
function process_email()
{
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById('myDivElement');
 statusdiv = document.getElementById('status');
  // display the status of the request
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        
        statusdiv.innerHTML = "Sending Email to : " + limit + "&nbsp;&nbsp;Out of " +total;
        response = xmlHttp.responseText;
        // display the message
      // myDiv.innerHTML += "Request status: 4 (complete). Server said: <br/>";
        myDiv.innerHTML = response;
        //alert(limit);
        limit++;
        //alert("Limit is:" +limit +"and total is:" +total);
		
        if(limit <= total){
		//alert("yes");
		myDiv.innterHTML = "limit:" +limit + "and total:"+total;
        setTimeout("process();",10000);
        }else{
       myDiv.innerHTML = "Email Sent to  : " + total +"&nbsp;&nbsp;People<br>Completed" ;//+ "Out of " +total;     
       clearTimeout();
        }
      }
      catch(e)
      {
        // display error messages and stop updating the window
        alert("Error: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the XML data:\n" +
            xmlHttp.statusText);
    }
  }
  
  }
function timer(total,id_cat)
{
    window.limit=1;
    window.total= total;
	window.id_cat = id_cat;
    process();
    
  }

   
function test(limit,cat)
{
    alert("Limit is:" +limit+"and category is:" +cat);
}
