
var batchNum = 0;
var groupNum = 0;

var attachmentFileIDs = new Array();

function addAttachment(id, fileName) {
	
	attachmentFileIDs.push(id);
	var attachmentHTML = "<p id='attachment"+id+"' class='attachment'>"+fileName+" - <a class='removeattachment' onclick='removeAttachment("+id+", true);'>remove</a></p>";
	
	$("attachments").insert(attachmentHTML);
	$("attachment_file_ids").setValue(attachmentFileIDs.toJSON());
	
}

function removeAttachment(id, deleteFile) {

	attachmentFileIDs = attachmentFileIDs.without(id);
	$("attachment_file_ids").setValue(attachmentFileIDs.toJSON());	
	$("attachment"+id).remove();
	
	if (deleteFile)
		deleteAttachmentFile(id);
	
}

function deleteAttachmentFile(id) {	
	new Ajax.Request("/ajax/deletefile.php?fid="+id);	
}

function AddBatch(timenow) {
	batchNum++;
	
	var batchHTML = "\
		<div id='batch"+batchNum+"' class='batch'>\
			<p style='padding:0px;padding-bottom:10px;'><label for='amount"+batchNum+"'>Number to send : </label><input type='text' name='amount"+batchNum+"' id='amount"+batchNum+"' style='width: 50px' onkeypress='return numbersonly(this, event);'></p>\
			<p style='padding:0px;padding-bottom:10px;'><label for='batchtime"+batchNum+"'>At time : </label><input type='text' name='batchtime"+batchNum+"' id='batchtime"+batchNum+"' value='"+timenow+"' readonly><img id='cal"+batchNum+"' class='calendaricon' src='/assets/images/calendar/calendar.jpg' width='16' height='16' border='0' alt='Pick a date' ></p>\
		</div>";
							
	$("batchlist").insert(batchHTML);
	var id = "batchtime"+batchNum;
	$('cal'+batchNum).observe('click', function () {
		displayCalendar($(id), 'dd/mm/yyyy hh:ii', $(id), true);
	});	
	
}
function RemoveBatch() {
	if (batchNum != 0) { 
		$("batch"+batchNum).remove();
		batchNum--;
	}
}


function AddGroup(options) {
	groupNum++;
	
	var groupHTML = "\
		<div id='sendgroup"+groupNum+"'  style='position: relative; float: left; clear: left;margin-bottom:5px;'>\
			<select  name='sendgroup"+groupNum+"'>\
				"+options+"\
			</select>\
		</div>\
		";
							
	$("sendgroups").insert(groupHTML);
	
	
}

function AddGroupInput(options) {
	groupNum++;
	
	var groupHTML = "\
		<div id='sendgroup"+groupNum+"'  style='position: relative; float: left; clear: left; margin-bottom: 5px;'>\
			<input name='groupname"+groupNum+"' value='' style='width: 250px'>\
		</div>\
		";
							
	$("sendgroups").insert(groupHTML);
	
	
}



function RemoveGroup() {
	if (groupNum != 0) { 
		$("sendgroup"+groupNum).remove();
		groupNum--;
	}
}

function ValidateMarketingEmail() {

	var errorHTML = "<ul>";
		
	var subject = $F("subject") != "";	
	if (!subject)
		errorHTML += "<li class='errortext'>Enter a subject for the email</li>";
		
	var body = $F("body") != "";	
	if (!body)
		errorHTML += "<li class='errortext'>Enter a body for the email</li>";
		
	var groups = false;
	$$("#sendgroups select").each(function (o) {
		if (o.getValue() != -1) groups = true; 
	});
	if (!groups)
		errorHTML += "<li class='errortext'>Select groups to send the email to</li>";
	
		
	if ($("dostagger").checked) {
		var amounts = true;
		for (var i = 0; i <= batchNum; i++) {
			if ($("amount"+i).getValue() == "") amounts = false;
		}
		if (!amounts)
			errorHTML += "<li class='errortext'>Specify the number of people to send the email to</li>";		
	}
	errorHTML += "</ul>";
	
	if (errorHTML.length > 15) {
		$('errorlist').update(errorHTML);		
		$('errorcontainer').style.display = "block";	
		$('errorlist').scrollTo();
		return false;
	}
	else {
		return true;
	}
}

function ValidateNewsletterSend() {

	var errorHTML = "<ul>";
			
		
	var groups = false;
	$$("#sendgroups select").each(function (o) {
		if (o.getValue() != -1) groups = true; 
	});
	if (!groups)
		errorHTML += "<li class='errortext'>Select groups to send the email to</li>";
	
		
	if ($("dostagger").checked) {
		var amounts = true;
		for (var i = 0; i <= batchNum; i++) {
			if ($("amount"+i).getValue() == "") amounts = false;
		}
		if (!amounts)
			errorHTML += "<li class='errortext'>Specify the number of people to send the email to</li>";		
	}
	errorHTML += "</ul>";
	
	if (errorHTML.length > 15) {
		$('errorlist').update(errorHTML);		
		$('errorcontainer').style.display = "block";	
		$('errorlist').scrollTo();
		return false;
	}
	else {
		return true;
	}
}

function SelectAllContacts() {
	$$("#addressform input[type='checkbox']").each(function(o) {
		o.checked = true;
	});
}
function DeSelectAllContacts() {
	$$("#addressform input[type='checkbox']").each(function(o) {
		o.checked = false;
	});
}	
function AddSelectedToGroup() {
	var groupVal = $("groups").getValue();
	$$("#addressform input[type='checkbox']").each(function(o) {
		if (o.checked) {			
			var index = o.identify().replace("select", "");			
			var groups = $("groups"+index).innerHTML.split(",");
			var alreadyInGroup = false;
			groups.each(function (g) {
				if (g.gsub(" ","") == groupVal.gsub(" ","")) {
					alreadyInGroup = true;
				}
			});
			if (!alreadyInGroup) {
				if ($("groups"+index).innerHTML == "") {					
					$("groups"+index).innerHTML += $("groups").getValue();
					$("groups_field"+index).setValue($("groups_field"+index).getValue()+$("groups").getValue());
				}
				else {
					$("groups"+index).innerHTML += ", "+$("groups").getValue();
					$("groups_field"+index).setValue($("groups_field"+index).getValue()+", "+$("groups").getValue());
				}
			}
		}
	});
}

function RemoveSelectedFromGroup() {
	$$("#addressform input[type='checkbox']").each(function(o) {
		if (o.checked) {
			var index = o.identify().replace("select", "");
			
			var groups = $("groups"+index).innerHTML.split(",");
			var inGroups = false;
			var keepGroups = [];
			groups.each(function (g) {
				if (g.gsub(" ", "") == $("groups").getValue().gsub(" ", "")) {
					inGroups = true;
				}
				else {
					keepGroups.push(g);
				}
			});
			
			if (inGroups) {
				$("groups"+index).innerHTML = keepGroups.join(", ");				
				$("groups_field"+index).setValue(keepGroups.join(", "));
				
				if ($("groups"+index).innerHTML.startsWith(",")) {
					$("groups"+index).innerHTML = $("groups"+index).innerHTML.sub(",", "");
					$("groups_field"+index).setValue($("groups_field"+index).getValue().sub(",", ""));
				}
				
			}
			
		}
	});
}

function RemoveSelectedContacts() {
	$$("#addressform input[type='checkbox']").each(function(o) {
		if (o.checked) {
			var index = o.identify().replace("select", "");
			if ($("contact"+index) != null) {
				$("contact"+index).remove();				
			}
			
		}
	});
}

var modifiedField = false;

function fieldModified() {
	modifiedField = true;
	$("resultscontainer").update("<p class='message' style='color:red;'>Please click save before leaving otherwise data will not be saved.</p>");
}

function ContactsPage(letter, start) {
	if (modifiedField) {
		alert("Warning: Changes you have made to these contacts will not be saved if you navigate away from this page. Click the save contacts button to save your changes first.")
		modifiedField = false;
	}
	else {

		$$("#addresses_pagination .selected").each(function(o) {
			o.removeClassName("selected");
		});
		
		$(letter).addClassName("selected");
		var url = '/gotmymail/addresses_management.php?action=editcontactspage&letter='+letter+'&start='+start;
		// notice the use of a proxy to circumvent the Same Origin Policy.
		
		$("loader").show();
		
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
		  	$("addresses_container").update(transport.responseText);
		  	$("loader").hide();
		  }
		});
	}
}

function SaveAllContacts() {
	if (modifiedField) {
		$('addressform').request({
			  parameters: { action:'saveajaxcontacts' },
			  onFailure: function(result) {
				  $("resultscontainer").update(result.responseText);
			  },
			  onSuccess: function(result){ 
				  
				  
				  $("resultscontainer").update(result.responseText);
				  modifiedField = false;
				  

				  var url = '/gotmymail/addresses_management.php?action=editcontactspage&letter='+$('letter').getValue();
				  
				  $("loader").show();
					
				  	  new Ajax.Request(url, {
					  method: 'get',
					  onSuccess: function(transport) {
					  	$("addresses_container").update(transport.responseText);
					  	$("loader").hide();
					  }
				  });
				  
				  
			  }
		});
	}
}


