// JavaScript Document
/**
* 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 input_style = "margin-left:3px;width:120px;padding-top:0px;margin-top:5px;";
	
	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>";
	inner += "<label style='font-weight:bold;color:#000;'>First Name:</label><br /><input type='text' id='exmail_fname'  class='signup' style='"+input_style+"'/></div>";
	inner += "<div>";
	inner += "<label style='font-weight:bold;color:#000;'>Last Name:</label><br /><input type='text' id='exmail_lname'  class='signup' style='"+input_style+"'/></div>";
	inner += "<div>";
	inner += "<label style='font-weight:bold;color:#000;'>Email:</label><br /><input type='text' id='exmail_email'  class='signup' style='"+input_style+"'/></div>";
	inner += "<div>";
	inner += "<label>&nbsp;</label><input type='image' src='image/newsignup.gif' style='width: 80px; height: 24px;'class='newssubmit' 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 email = document.getElementById('exmail_email').value;
	var fname = document.getElementById('exmail_fname').value;
	var lname = document.getElementById('exmail_lname').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+"&listowner="+list_owner + "&listid=" + list_id+"&fname="+fname + "&lname=" + lname;
	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 email list: <br/><strong>The Surprise Shop - "+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_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;
}

