/*
Email Capture

This sends off a chunk of XML to capture the emails and other information of users who opt into emails. The information is stored in GDC.

Posting will fail without a collection id (cid). GDC validates the email address input, and posts with badly formed email addresses are not stored. The only fallout of a failed post is updating an (optional) feedback textarea.

Usage:		new EmailCapture(mgid, emailAddress, displayName, postalCode, userType, contestName,
							 cid, feedbackObj);
Example:	new EmailCapture('mgid:tw:user:flux.com:AAAAAAAAAAAAAAAAAAAAAA',
							 'aretha@atn.com', 'Aretha Franklin', '10036', 'Band', 'The Beat',
							 '139', document.getElementById('myTextarea') );

*/
var EmailCapture = function (mgid, emailAddress, displayName, postalCode, userType, contestName,
							 cid, feedbackObj)
{
	this.dataServiceUrl	= '/sitewide/dataservices/contest/capture_email.jhtml';
	this.mgid			= mgid;				//user's mgid
	this.emailAddress	= emailAddress;		//user's email address, the point of it all
	this.displayName	= displayName;		//user's display name
	this.postalCode		= postalCode;		//user's postal code
	this.userType		= userType;			//'band' or 'fan', generally from UserTypeSwitch
	this.contestName	= contestName;		//display name of a contest associated with this GDC collection
	this.cid			= cid;				//the GDC collection id
	this.feedbackObj	= feedbackObj;		//optional textarea for testing
	
	this.send();
}

EmailCapture.prototype.send= function() //post the xml blob off to the GDC listener
{		
		var xmlData = this.createXmlData();
		var thisPtr = this;	//a pointer to the original EmailCapture object. Ajax needs this or else it will lose reference to EmailCapture obj.	
		
		var ajaxReq = new Ajax.Request(
			this.dataServiceUrl,
			{
				method:'post',				
				contentType: 'text/xml',
				encoding:'', //must be no char encoding. gdc screws up with encoding
				postBody:xmlData,
				onSuccess: function(transport){
					var response = transport.responseText || "no response text";
					if(response.indexOf('response code=\"OK\"')>=0)
					{					
						thisPtr.processSuccess();
					}else
					{						
						thisPtr.processFailure();
					}
				},
				
				onLoading: function(){},
				
				onFailure: function(){
					thisPtr.processFailure();
				}
								
			}
		);
}


EmailCapture.prototype.createXmlData = function()
{
	var xmlData = ""+
				"<"+
				"answers collectionID=\""+this.cid+"\">"+						
				
				"<"+
				"answer tag=\"user_uri\""+
				">"+this.mgid+"<"+
				"/answer"+
				">"+

				"<"+
				"answer tag=\"user_email\""+
				">"+this.emailAddress+"<"+
				"/answer"+
				">"+
				
				"<"+
				"answer tag=\"user_displayname\""+
				">"+this.displayName+"<"+
				"/answer"+
				">"+
				
				"<"+
				"answer tag=\"user_postalcode\""+
				">"+this.postalCode+"<"+
				"/answer"+
				">"+
								
				"<"+
				"answer tag=\"user_type\""+
				">"+this.userType+"<"+
				"/answer"+
				">"+
				
				"<"+
				"answer tag=\"contest_name\""+
				">"+this.contestName+"<"+
				"/answer"+
				">"+
				
				"<"+
				"/answers"+
				">";
	return xmlData;
}


EmailCapture.prototype.processSuccess = function(){	
	if (this.feedbackObj)
		this.feedbackObj.value = "email captured";
}

EmailCapture.prototype.processFailure = function(){
	if (this.feedbackObj)
		this.feedbackObj.value = "email capture FAILED";
}
