jQuery(document).ready(function()

{

	$j('#advancedSettingsLink').click(function()

	{

		$j('#advancedSettings').slideToggle("slow");		

	})

});



function ajaxSend()

{

	threads 		= document.getElementById("txtThreads").value;

	glbdelay		= document.getElementById("txtDelay").value;

	glbdelay		= parseInt(glbdelay) * 1000;

	glbStartFrom 	= document.getElementById("txtStartFrom").value;

	glbID			= document.getElementById("campaignId").value;



	jQuery('#systemerror').html("");

	

	if (threads > 0 && threads < 100)

	{

		// it is fine.	

	}

	else

	{

		alert("Threads must be between 1 and 99");

		return false;

	}

	

	if (glbdelay >= 0 && glbdelay <= 5000000)

	{

		// it is fine.	

	}

	else

	{

		alert("Invalid Delay from Threads value");

		return false;

	}



	

	if (glbStartFrom > 0 && glbStartFrom < 99999)

	{

		// it is fine.	

	}

	else

	{

		alert("Invalid Start From value");

		return false;

	}

	

	// DISABLE START FROM AND THREADS TEXTBOXES

	// ON PRESSING THE START BUTTON.

	jQuery('#txtThreads').attr("disabled","disabled");

	jQuery('#txtDelay').attr("disabled","disabled");

	jQuery('#txtStartFrom').attr("disabled","disabled");



	jQuery('#startSending').hide();

	jQuery('#pauseResume').show();

	jQuery('#running').show();

	

//	alert("startfrom="+glbStartFrom+"&id="+glbID+"&delay="+glbdelay+"&threads="+threads);

	sendAjaxRequest(glbStartFrom, glbID, glbdelay, threads);

	

	return false;

}



function sendAjaxRequest(STARTFROM, ID, GLOBALDELAY, THREADS)

{

	jQuery.ajax({

		type: "GET",

		url: "ajaxsend.php",

		data: 	"startfrom="+STARTFROM+"&id="+ID+"&delay="+GLOBALDELAY+"&threads="+THREADS,

		dataType: 'json',

		error: function (data)

		{

			//jQuery('#systemerror').html("<div class='error'>" + data + "<br />Please try again.<div>")

			//JSONstring = JSON.stringify(data);

			jQuery('#debugtable').show();

			jQuery('#debugmessage').html(data.responseText);

			setTimeout("sendAjaxRequest(glbStartFrom, glbID, glbdelay, threads)",GLOBALDELAY);	

		},

		success: function (data)

		{

			

			// ONLY FETCH THE VALUES IF THE AJAX

			// IS NOT RETURNING ANY ERROR

			// If any error is returning show the error

			// and halt the process.

			if (typeof(data.systemerror) != 'undefined')

			{

				glbPause  =1;

				jQuery('#pauseResume').val('Resume');

				jQuery('#systemerror').html("<div class='error'>" + data.systemerror + "<br />Please try again.<div>");

				jQuery('#running').hide();

				

			}

			else

			{

				glbStartFrom = parseInt(data.start);

				manualPB2.setPercentage(parseInt(data.percentage));

				glbPercentage = data.percentage;

				

				//UPDATE THE VALUE OF SUCCESSFULLY SENT EMAIL LABEL

				//UNDER STATISTICS.



				var curSuccessfullySent = parseInt(jQuery('#successfullySent').text());

				var newSuccessfullySent = curSuccessfullySent + parseInt(data.success);

				jQuery('#successfullySent').text(newSuccessfullySent);

				// END OF SUCCESSFULLY SENT LABEL UPDATE /////////////

				

				

				//

				//UPDATE THE VALUE OF SUCCESSFULLY FAILED EMAIL LABEL

				//UNDER STATISTICS.

				//

				var curFailedEmails = parseInt(jQuery('#emailsFailed').text());

				var newFailedEmails = curFailedEmails + parseInt(data.failed);

				

				jQuery('#emailsFailed').text(newFailedEmails);	

				

				var failedEmailsStr = data.failedEmails;

				newFailedEmailsStr = failedEmailsStr.replace(/,/g, "<br />");

				if (failedEmailsStr == "")

				{

					

				}

				else

				{

					jQuery('#failedEamilsDiv').append('<br />'+newFailedEmailsStr);	

				}

				

				//jQuery('#failedEamilsDiv').append('<br />'+newFailedEmailsStr);

				

	

				// END OF FAILED EMAIL LABEL UPDATE /////////////

				

				if ((glbPercentage < 100) && (glbPause == 0))

				{

					//alert(data.start);

					

					//sendAjaxRequest(glbStartFrom, glbID, glbdelay, threads);

					setTimeout("sendAjaxRequest(glbStartFrom, glbID, glbdelay, threads)",glbdelay);

					

				}

				else if (glbPercentage >= 100)

				{

					updateFailedEmails(glbStartFrom, glbID, threads, newFailedEmails);

					// Add failed records into the database.	

				}

			}

		}

	});

}



function pauseOrResume()

{

	var curlabel = jQuery('#pauseResume').val();

	jQuery('#systemerror').html("");

	if (curlabel == 'Pause')

	{

		glbPause  =1;

		jQuery('#pauseResume').val('Resume');

		jQuery('#running').hide();

	}

	else

	{

		glbPause  =0;

		jQuery('#pauseResume').val('Pause');

		//sendAjaxRequest(glbStartFrom, glbID, glbdelay, threads);

		setTimeout("sendAjaxRequest(glbStartFrom, glbID, glbdelay, threads)",glbdelay);

		jQuery('#running').show();

	}

}



function seeFailedEmails()

{

	jQuery('#failedEamilsDiv').slideToggle("slow");			

}



function updateFailedEmails(STARTFROM, ID, THREADS, TOTALFAILED)

{

	jQuery.ajax({

		type: "GET",

		url: "ajaxsend.php",

		data: 	"startfrom="+STARTFROM+"&id="+ID+"&threads="+THREADS+"&totalfailed="+TOTALFAILED,

		dataType: 'json',

		error: function (data)

		{

			//jQuery('#systemerror').html("<div class='error'>" + data + "<br />Please try again.<div>")

		},

		success: function (data)

		{

			// campaign successfully sent.			

		}

	});

}

