// JavaScript Document
var xmlHttp;
var pageId;
var queryId;
var userArray=[];
var queryRunning=false;
var sendCount=0;
function createXMLHttpRequest(){
	try {
		//firefox, opera & Safari
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
				xmlHttp=null;	
			}
		}
		
	}
}
createXMLHttpRequest();
function uploadEmailAds(){
	if(xmlHttp!=null){
		queryId=1;
		var url="includes/javascript-data-queries.php";
		params = 'id='+queryId;
		var count = 0;
		var startValue=sendCount;
		var endValue = sendCount+1;
		if(endValue>emails.length){
			endValue = emails.length;
		}
		for(var i=startValue;i<endValue;i++){
			params += '&e'+count+'='+emails[i];
			document.getElementById('loaderText').value = document.getElementById('loaderText').value + '\n Sending Email To: '+emails[i];
			document.getElementById('loaderText').scrollTop = document.getElementById('loaderText').scrollHeight;
			count++;
		}
		params += '&ad='+advertId;
		if(count==100){
			params += '&end=1';
		} else {
			var diff = emails.length-sendCount;
			params += '&end=1';
		}
		xmlHttp.open("POST",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
}
function populateInfo(){
	if(xmlHttp!=null){
		queryId=0;
		var url="includes/javascript-data-queries.php";
		params = 'id='+queryId;
		var count = 0;
		var startValue=sendCount;
		var endValue = sendCount+100;
		if(endValue>emails.length){
			endValue =  emails.length;
		}
		for(var i=startValue;i<endValue;i++){
			params += '&e'+count+'='+emails[i];
			count++;
		}
		if(count==100){
			params += '&end=100';
		} else {
			var diff = emails.length-sendCount;
			params += '&end='+diff;
		}
		xmlHttp.open("POST",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
}
function stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		switch(queryId){
			case 0://loads the page
				if((sendCount+100)>emails.length){
					sendCount=emails.length;	
					document.getElementById('populator').innerHTML = '100% Complete';
					document.getElementById('loadingImage').src = 'images/waitingbar.jpg';
				} else {
					sendCount+=100;
					var per = Math.round(((sendCount/emails.length)*100));
					document.getElementById('populator').innerHTML = per+'% Complete';
					document.getElementById('loadingBar').style.width = per+'%';
					populateInfo();
				}
				break;
			case 1://loads the page
				if((sendCount+1)>emails.length){
					sendCount=emails.length;	
					document.getElementById('populator').innerHTML = '100% Complete '+emails.length+' of '+emails.length+' emails.';
					document.getElementById('loadingImage').src = 'images/waitingbar.jpg';
				} else {
					sendCount+=1;
					var per = Math.round(((sendCount/emails.length)*100));
					document.getElementById('populator').innerHTML = per+'% Complete '+sendCount+' of '+emails.length+' emails.';
					document.getElementById('loadingBar').style.width = per+'%';
					document.getElementById('loaderText').value = document.getElementById('loaderText').value + ' - '+xmlHttp.responseText;
					document.getElementById('loaderText').scrollTop = document.getElementById('loaderText').scrollHeight;
					uploadEmailAds();
				}
				break;
		}
	}
}