var Postmaster = function() {
}

//-------------------------------------------------------------------------------
Postmaster.prototype.completionStatus = 0;
Postmaster.prototype.getCompletionStatus = function() {return this.completionStatus;};
Postmaster.prototype.setCompletionStatus = function(completionStatus) {this.completionStatus = completionStatus;};
//-------------------------------------------------------------------------------
Postmaster.prototype.debugging = false;
Postmaster.prototype.getDebugging = function() {return this.debugging;};
Postmaster.prototype.setDebugging = function(debugging) {this.debugging = debugging; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.doIfError;
Postmaster.prototype.getDoIfError = function() {return this.doIfError;};
Postmaster.prototype.setDoIfError = function(doIfError) {this.doIfError = doIfError; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.doIfSuccess;
Postmaster.prototype.getDoIfSuccess = function() {return this.doIfSuccess;};
Postmaster.prototype.setDoIfSuccess = function(doIfSuccess) {this.doIfSuccess = doIfSuccess; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.doForObject;
Postmaster.prototype.getDoForObject = function() {return this.doForObject;};
Postmaster.prototype.setDoForObject = function(doForObject) {this.doForObject = doForObject; return 1;};
//-------------------------------------------------------------------------------
var Friend = function(name, email, status, statusComments) {
 this.name = name;
 this.email = email;
 this.status = status;
 this.statusComments = statusComments;
};

Postmaster.prototype.friends = new Array();

Postmaster.prototype.addFriend = function(name, email) {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend is running...");};
 
 var haveFriend = this.haveFriend(email);
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend: haveFriend="+haveFriend);};
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend: this.friends.length="+this.friends.length);};
 
 if(!haveFriend) {
  var friend = new Friend (name, email);
  if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend: this.friends.length="+this.friends.length);};
  this.friends[this.friends.length] = friend;
  if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend: Friend is added");};
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend: this.friends.length="+this.friends.length);};
  return 1;
 } else {
  if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend: Friend is not added");};
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.addFriend: this.friends.length="+this.friends.length);};
  return 0;
 }; 
};

Postmaster.prototype.clearFriendsList = function() {
 this.friends = new Array();
}

Postmaster.prototype.getFriend = function(number) {
 return this.friends[number];
};

Postmaster.prototype.getFriendByEmail = function(email) {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.getFriendByEmail...");};
 for(var i = 0; i < this.friends.length; i++) {
  var friend = this.getFriend(i);
  if(friend.email == email) {
   if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.getFriendByEmail: friend.email="+friend.email);};
   return friend;
  };
 };
 return false;
};

Postmaster.prototype.haveFriend = function(email) {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.haveFriend...");};
 if(!this.getFriendByEmail(email)) {
  return false;
 } else {
  return true;
 };
}

Postmaster.prototype.setFriend = function(number, friend) {
 this.friends[number] = friend;
 return 1;
};

Postmaster.prototype.getFriends = function() {return this.friends;};

Postmaster.prototype.setFriends = function(friends) {this.friends = friends; return 1;};

Postmaster.prototype.getFriendsCount = function() {return this.friends.length;};
//-------------------------------------------------------------------------------
Postmaster.prototype.fromName;
Postmaster.prototype.getFromName = function() {return this.fromName;};
Postmaster.prototype.setFromName = function(fromName) {this.fromName = fromName; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.httpRequest;
//-------------------------------------------------------------------------------
Postmaster.prototype.languageOfInterface;
Postmaster.prototype.getLanguageOfInterface = function() {return this.languageOfInterface;};
Postmaster.prototype.setLanguageOfInterface = function(languageOfInterface) {
 this.languageOfInterface = languageOfInterface; 
 return 1;
};
//-------------------------------------------------------------------------------
Postmaster.prototype.labels="";
Postmaster.prototype.getLabels = function() {return this.labels;};
Postmaster.prototype.setLabels = function(labels) {this.labels = labels; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.pageTitle="";
Postmaster.prototype.getPageTitle = function() {return this.pageTitle;};
Postmaster.prototype.setPageTitle = function(pageTitle) {this.pageTitle = pageTitle; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.pageURL="";
Postmaster.prototype.getPageURL = function() {return this.pageURL;};
Postmaster.prototype.setPageURL = function(pageURL) {this.pageURL = pageURL; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.postmasterLog; 
Postmaster.prototype.getLog = function() {return this.postmasterLog;};
Postmaster.prototype.setLog = function(postmasterLog) {
 this.postmasterLog = postmasterLog; 
 if(this.debugging && this.postmasterLog) {this.postmasterLog.println("Postmaster.setLog: "+"postmasterLog is running...");}; 
 return 1;
};
//-------------------------------------------------------------------------------
Postmaster.prototype.postmasterURL;
Postmaster.prototype.getPostmasterURL = function() {return this.postmasterURL;};
Postmaster.prototype.setPostmasterURL = function(postmasterURL) {this.postmasterURL = postmasterURL; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.receiveCopy=0;
Postmaster.prototype.getReceiveCopy = function() {return this.receiveCopy;};
Postmaster.prototype.setReceiveCopy = function(receiveCopy) {this.receiveCopy = receiveCopy; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.server;
Postmaster.prototype.getServer = function() {return this.server;};
Postmaster.prototype.setServer = function(server) {this.server = server; return 1;};
//-------------------------------------------------------------------------------
Postmaster.prototype.sid = "";
Postmaster.prototype.getSID = function() {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.getSID is running...");};
 return this.sid;
};
Postmaster.prototype.setSID = function(sid) {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.setSID is running...");};
 this.sid = sid; 
 return 1;
};
Postmaster.prototype.getSid = function() {return this.getSID();};
Postmaster.prototype.setSid = function(sid) {return this.setSID(sid);};
//-------------------------------------------------------------------------------
Postmaster.prototype.text;
Postmaster.prototype.getText = function() {return this.text;};
Postmaster.prototype.setText = function(text) {this.text = text; return 1;};


//-------------------------------------------------------------------------------
Postmaster.prototype.responseEvent = function(httpRequest, obj) {obj.response(httpRequest);};
Postmaster.prototype.response = function(httpRequest) {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.response is running...");};
 this.httpRequest = httpRequest;
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.response: "+"this.httpRequest.readyState="+this.httpRequest.readyState);};
 if (this.httpRequest.readyState == 4) {
  if (this.httpRequest.status && this.httpRequest.status == 200) {
   if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.response: "+"this.httpRequest.status="+this.httpRequest.status);};
   if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.response: "+"this.httpRequest.responseXML: ok");};  
   
   try {
    if(this.httpRequest.getAllResponseHeaders && this.httpRequest.getAllResponseHeaders()) {this.postmasterHeaders = this.httpRequest.getAllResponseHeaders();};     
   } catch (e) {
    if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.response: "+"Postmaster.response this.httpRequest.getAllResponseHeaders() exception...");};
   }
   
   this.parseResponse(this.httpRequest.responseXML); 
  } else {
   if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.response: "+"There was a problem with the request.");};
   this.completionStatus = -10;
  }
 } else {
  if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.response: "+"Not yet...");};
 };
}

//-------------------------------------------------------------------------------
Postmaster.prototype.parseResponse = function(responseXML) {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.parseResponse...");};
 
 this.responseXML = responseXML;
 
 if(this.responseXML) {
  if(this.responseXML.getElementsByTagName("status") && this.responseXML.getElementsByTagName("status").item(0) && this.responseXML.getElementsByTagName("status").item(0).firstChild) {
   this.status = this.responseXML.getElementsByTagName("status").item(0).firstChild.data;
   if(this.debugging && this.getLog()) {this.getLog().println("Postmaster: this.status = "+this.status);};
  };
  if(this.responseXML.getElementsByTagName("statusComments") && this.responseXML.getElementsByTagName("statusComments").item(0) && this.responseXML.getElementsByTagName("statusComments").item(0).firstChild) {
   this.statusComments = this.responseXML.getElementsByTagName("statusComments").item(0).firstChild.data;
   if(this.debugging && this.getLog()) {this.getLog().println("Postmaster: this.statusComments = "+this.statusComments);};
  };
  
  var friendsCount = this.getFriendsCount();
  if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.parseResponse: friendsCount="+friendsCount);};
  for(var i = 0; i < friendsCount; i++) {
   var friendNumber = i+1;
   var friend = this.getFriend(i);
   if(this.responseXML.getElementsByTagName("friend"+friendNumber+"Status") && this.responseXML.getElementsByTagName("friend"+friendNumber+"Status").item(0) && this.responseXML.getElementsByTagName("friend"+friendNumber+"Status").item(0).firstChild) {
    friend.status = this.responseXML.getElementsByTagName("friend"+friendNumber+"Status").item(0).firstChild.data;
    if(this.debugging && this.getLog()) {this.getLog().println("Postmaster: friend.status = "+friend.status);};
   };
   if(this.responseXML.getElementsByTagName("friend"+friendNumber+"StatusComments") && this.responseXML.getElementsByTagName("friend"+friendNumber+"StatusComments").item(0) && this.responseXML.getElementsByTagName("friend"+friendNumber+"StatusComments").item(0).firstChild) {
    friend.statusComments = this.responseXML.getElementsByTagName("friend"+friendNumber+"StatusComments").item(0).firstChild.data;
    if(this.debugging && this.getLog()) {this.getLog().println("Postmaster: this.statusComments = "+this.statusComments);};
   };
   this.setFriend(i, friend);
  }
  
  if (this.status == "success") {
   this.doIfSuccess(this.doForObject, this);
   return 1;
  } else {
   this.doIfError(this.doForObject, this);
   return 0;
  };
 } else {
  return 0;
 };
}

//-------------------------------------------------------------------------------
Postmaster.prototype.send = function() {
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.send is running...");};

 var url = this.getPostmasterURL()
 var query = "sid="+escape(this.getSID())
			 +"&fromName="+escape(this.getFromName())
			 +"&url="+escape(this.getPageURL())
			 +"&languageOfInterface="+escape(this.getLanguageOfInterface())
			 +"&text="+escape(this.getText())
			 +"&receiveCopy="+escape(this.getReceiveCopy())
			 +"&labels="+escape(this.getLabels())
			 +"&pageTitle="+escape(this.getPageTitle());

 var friendsCount = this.getFriendsCount();
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.send: friendsCount="+friendsCount)};
 for(var i = 0; i < friendsCount; i++) {
  var friend = this.getFriend(i);
  var friendNumber = i+1;
  if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.send: friendNumber="+friendNumber)};
  query += "&toName"+friendNumber+"="+escape(friend.name);
  query += "&toEmail"+friendNumber+"="+escape(friend.email);
 };
 
 if(this.debugging && this.getLog()) {this.getLog().println("Postmaster.send: url="+url+"; query="+query)};
 this.server.makeRequest("POST", url, query, this.responseEvent, this);
}