/**
* This is the AIS Media ExcerpoMail Javascript Connector.
* @author Paulo Delgado <pdelgado@aismedia.com>
* @modified Adam Morrison <amorrison@aismedia.com>
* Copyright ? 2006 - AIS Media
* Modified 12/6/2007
* www.aismedia.com
*/
function exmail(container_id, list_owner, list_id) {
	if (container_id == null || list_owner == null || list_id == null) {return;}
	var container = document.getElementById(container_id);
	if (!container) return;
	
	var row_style = "display:block; clear:left;margin:2px 0px;";
	var label_style = "float:left; width:100px;";
	
	var inner = new String();
	inner += "<input type='hidden' id='exmail_list_owner' value='"+list_owner+"' />";
	inner += "<input type='hidden' id='exmail_list_id' value='"+list_id+"' />";
	inner += "<div style='"+row_style+"'>";
	inner += "<label style='"+label_style+"'>First name:</label><input type='text' id='exmail_fname' size=20 /></div>";
	inner += "<div style='"+row_style+"'>";
	inner += "<label style='"+label_style+"'>Last name:</label><input type='text' id='exmail_lname' size=20 /></div>";
	inner += "<div style='"+row_style+"'>";
	inner += "<label style='"+label_style+"'>Email:</label><input type='text' id='exmail_email' size=20 /></div>";
	inner += "<div style='"+row_style+"'>";
	inner += "<label style='"+label_style+"'>&nbsp;</label><input type='button' value='Go' onclick='subscribeEmail()' /></div>";
	
	container.innerHTML = inner;
}
function subscribeEmail() {
	var xmlhttp = initXMLHTTP();
	var list_owner = document.getElementById('exmail_list_owner').value;
	var list_id = document.getElementById('exmail_list_id').value;
	var fname = document.getElementById('exmail_fname').value;
	var lname = document.getElementById('exmail_lname').value;
	var email = document.getElementById('exmail_email').value;
	if(email.length == 0 || email.indexOf('@') == -1) {
		alert('The email address provided does not appear to be valid.');
		return;
	} else if (list_owner.length == 0 || list_id.length == 0) {
		alert('This form is not setup properly.');
		return;
	}
	var getString = "/jsconnector/index.php?action=add&email="+email+"&fname="+fname+"&lname="+lname+"&listowner="+list_owner + "&listid=" + list_id;
	xmlhttp.open("GET",getString,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			xmlhttp.responseXML;
			var xml = xmlhttp.responseXML;
			error = xml.getElementsByTagName('error');
			if(error.length > 0) {
				var errors = "";
				for(var i=0 ; i< error.length; i++) {
					errors += error[i].firstChild.nodeValue + "\n";
				}
				alert(errors);
			} else {
				//var campaign_name = xml.getElementsByTagName('campaign')[0].firstChild.nodeValue;
				//var list_name = xml.getElementsByTagName('list')[0].firstChild.nodeValue;
				var result = xml.getElementsByTagName('requestResult')[0];
				var campaign = result.getElementsByTagName('campaign')[0];
				var campaign_name = campaign.text;
				if (campaign_name == null) campaign_name = campaign.textContent;
				var message = "You have been successfully subscribed to the following newsletter: <br/><strong>"+campaign_name+"</strong>."; 
				var msg = document.createTextNode(message);
				var span = document.createElement('p');
				span.setAttribute('class', 'ExcerpoMailResponse');
				span.innerHTML = message;		
				var prnt = document.getElementById('exmail_email').parentNode;
				prnt.appendChild(span);
				
				
				document.getElementById('exmail_fname').value = '';
				document.getElementById('exmail_lname').value = '';
				document.getElementById('exmail_email').value = '';
			}
		}
	}
	xmlhttp.send(null);
	return false;
}
function initXMLHTTP() {
	var xmlhttp;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
  	try {
  		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 	} catch (e) {
  		try {
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  		} catch (E) {
   			xmlhttp=false
  		}
 	}
	@else
 	xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 	try {
	  		xmlhttp = new XMLHttpRequest();
	 	} catch (e) {
	  		xmlhttp=false;
	 	}
	}
	return xmlhttp;
}


