/* written by Jeff Lane 
Please listen to Heart of Cygnus! */
// Objects
var w = window;
var __ = document;
var ie = (w.XMLHttpRequest) ? false : true;

// Function objects
var _ = function (x){return __.getElementById(x);}
var _st = function (a,b){w.setTimeout(a,b);}
var _sr = function (a,b,c){a.setRequestHeader(b,c);}
var _sd = function (a,v){if(v==null){return a.style.display}else{a.style.display=v;}}
var _dom= function (s){
							var o;
							if(!ie)
							{
								p=new DOMParser();
								o=p.parseFromString(s,"text/xml");
							}
							else
							{
								o=new ActiveXObject("Microsoft.XMLDOM");
								o.async="false";
								o.loadXML(s);
							} 
							return o;
						}
var _xr = function (){if(w.XMLHttpRequest){return new XMLHttpRequest();}else{return new ActiveXObject("Microsoft.XMLHTTP");}}
var _gbtn= function (o,n){return o.getElementsByTagName(n);}
// Booleans
var mbPop = false;

function SaveEmail(nUnsubscribe)
{
	var sEE = ie ? "txtEmailIE" : "txtEmail";
	var sEP = ie ? "divPleaseIE" : "divPlease";
	oE = _(sEE);
	sEmail = oE.value;
	if (!validateEmail(sEmail))
	{
		oE.value="";
		oE.focus();
		oP = _(sEP);
		oP.innerHTML = "PLEASE ENTER A VALID COSMIC EMAIL.";
		return;
	}
	oE.disabled=true;
	var xH = _xr();
	var url = "subscribe.php";
	if(nUnsubscribe==null) nUnsubscribe=0;
	var params = "txtEmail="+sEmail+"&txtUnsubscribe="+nUnsubscribe.toString();
	xH.open("POST", url, true);
	
	//Send the proper header information along with the request
	_sr(xH,"Content-type", "application/x-www-form-urlencoded");
	_sr(xH,"Content-length", params.length);
	_sr(xH,"Connection", "close");
	
	xH.onreadystatechange=function(){
									if(xH.readyState==4 && xH.status==200)
									{
										__.body.style.cursor="auto";
										var o=_dom(xH.responseText);
										if(_gbtn(o,"error")[0].childNodes[0].nodeValue=="0")
										{
											if(ie)
											{
												FadeInJoinIE();
											}
											else
											{
												FadeInJoin();_st("FadeOutJoin()", 2000);
											}
										}
										else
										{
											oE.disabled=false;
											alert(_gbtn(o,"msg")[0].childNodes[0].nodeValue);
										}
									}
								};
	xH.send(params);
}
function Unsubscribe()
{
	var sEE = "txtEmailUnsubscribe";
	var sEP = "divPlease";
	oE = _(sEE);
	sEmail = oE.value;
	if (!validateEmail(sEmail))
	{
		oE.value="";
		oE.focus();
		oP = _(sEP);
		oP.innerHTML = "PLEASE ENTER A VALID EMAIL.";
		return;
	}
	oE.disabled=true;
	var xH = _xr();
	var url = "subscribe.php";
	var params = "txtEmail="+sEmail+"&txtUnsubscribe=1";
	xH.open("POST", url, true);
	
	//Send the proper header information along with the request
	_sr(xH,"Content-type", "application/x-www-form-urlencoded");
	_sr(xH,"Content-length", params.length);
	_sr(xH,"Connection", "close");
	
	xH.onreadystatechange=function(){
									if(xH.readyState==4 && xH.status==200)
									{
										__.body.style.cursor="auto";
										var o=_dom(xH.responseText);
										if(_gbtn(o,"error")[0].childNodes[0].nodeValue=="0")
										{
											showUnsubscribe();
										}
										else
										{
											oE.disabled=false;
											alert(_gbtn(o,"msg")[0].childNodes[0].nodeValue);
										}
									}
								};
	xH.send(params);	
}
function showUnsubscribe()
{
	var oE = _("txtEmailUnsubscribe");
	oE.value = "";
	oE.disabled = false;
	oE.focus();
	var oT = _("divThanks");
	_sd(oT,"inline-block");
}
function SendEmail()
{
	sEE = ie ? "txtFromIE" : "txtFrom";
	sEP = ie ? "divPleaseContactIE" : "divPleaseContact";
	sES = ie ? "txtSubjectIE" : "txtSubject";
	sEB = ie ? "txtBodyIE" : "txtBody";
	
	oE = _(sEE);
	sEmail = oE.value;
	oP = _(sEP);
	if (!validateEmail(sEmail))
	{
		oE.value="";
		oE.focus();
		oP.innerHTML = "PLEASE ENTER A VALID COSMIC EMAIL.";
		return;
	}    
	oSub = _(sES);
	sSubject = oSub.value;
	oBod = _(sEB);
	sBody = oBod.value;
	if(sBody=="")
	{
		oBod.focus();
		oP.innerHTML = "PLEASE TYPE IN AN INTERPLANETARY MESSAGE.";
		return;
	}
	oE.disabled=true;
	oSub.disabled=true;
	oBod.disabled=true;
	var xH = _xr();
	var url = "sendemail.php";
	var params = "txtFrom="+sEmail+"&txtSubject="+escape(sSubject)+"&txtBody="+escape(sBody);
	xH.open("POST", url, true);
	
	//Send the proper header information along with the request
	_sr(xH,"Content-type", "application/x-www-form-urlencoded");
	_sr(xH,"Content-length", params.length);
	_sr(xH,"Connection", "close");
	
	xH.onreadystatechange=function()
						  {
							  if(xH.readyState==4 && xH.status==200)
							  {
								  __.body.style.cursor="auto"; 
								  var o=_dom(xH.responseText);
								  if(_gbtn(o,"error")[0].childNodes[0].nodeValue=="0")
								  {
									  if(ie)
									  {
										  FadeInContactIE();
									  }
									  else
									  {
										  FadeInContact();
										  _st("FadeOutContact()",2000);
									  }
								   }
								   else
								   {
									   	oE.disabled=true;
										oSub.disabled=true;
										oBod.disabled=true;
									   alert(_gbtn(o,"msg")[0].childNodes[0].nodeValue);
								   }
								}
						   }
	xH.send(params);

}

function validateEmail(sEmail){var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;return emailRegEx.test(sEmail);}

function showContact()
{
	if (ie) 
	{
		goto("contact.html");
		return;
	}

	if (mbPop) return;
	ContactDisplay("inline-block");
	obj = _("txtFrom");
	obj.focus();
}

function ContactDisplay(val)
{
	var oPop = _("popupContainer");
	var oCon = _("divContact");
	_sd(oPop,val);
	_sd(oCon,val);
	mbPop = (val=="none" ? false : true);
	
}

function showJoin()
{
	if (ie) 
	{
		goto("join.html");
		return;
	}
	if (mbPop) return;
	JoinDisplay("inline-block");
	obj = _("txtEmail");
	obj.disabled = false;
	obj.focus();
}


function JoinDisplay(val)
{
	var oPop = _("popupContainer");
	var oJoin = _("divJoin");
	_sd(oPop,val);
	_sd(oJoin,val);
	//Set the module scoped pop up indicator
	mbPop = (val=="none" ? false : true);
}



function FadeInJoin()
{
	var obj = _("divThanks");
	if (_sd(obj)=="none")
		_sd(obj,"inline-block");
	if (ie) return;
	var nOpacity = new Number(obj.style.opacity);
	if (nOpacity == 1  || nOpacity > 1)
		return;
	else
	{
		nOpacity += (.01 * 5);
		obj.style.opacity = nOpacity.toFixed(2).toString();
		_st("FadeInJoin()", 5);
	}
}

function FadeInContact()
{
	var obj = _("divContactThanks");
	if (obj.style.display=="none")
		obj.style.display="inline-block";
	if (ie) return;
	var nOpacity = new Number(obj.style.opacity);
	if (nOpacity == 1  || nOpacity > 1)
		return;
	else
	{
		nOpacity += (.01 * 5);
		obj.style.opacity = nOpacity.toFixed(2).toString();
		_st("FadeInContact()", 5);
	}	
}

function FadeOutJoin()
{
	if (ie)
	{
		FadeOutJoinIE();
		return;
	}
	var obj = _("divThanks");

	var nOpacity = new Number(obj.style.opacity);
	if (nOpacity == 0  || nOpacity < 0)
	{
		var oEmail = _("txtEmail");
		oEmail.value = "";
		oEmail.focus();
		_st("JoinDisplay('none')",10);
		//obj.style.display="none";		
		oPlease = _("divPlease");
		oPlease.innerHTML = "ENTER YOUR INTERGALACTIC EMAIL.";
		return;
	}
	else
	{
		nOpacity -= (.01 * 5);
		obj.style.opacity = nOpacity.toFixed(2).toString();
		_st("FadeOutJoin()", 5);
	}
}
function FadeOutContact()
{
	if (ie)
	{
		FadeOutContactIE();
		return;
	}
	var obj = _("divContactThanks");

	var nOpacity = new Number(obj.style.opacity);
	if (nOpacity == 0  || nOpacity < 0)
	{
		var oEmail = _("txtFrom");
		oEmail.value = "";
		oEmail.focus();
		var oSub = _("txtSubject");
		oSub.value="";
		var oBod = _("txtBody");
		oBod.value = "";
		_st("ContactDisplay('none')",10);
		//obj.style.display="none";		
		oPlease = _("divPleaseContact");
		oPlease.innerHTML = "SEND US A COSMIC MESSAGE.";
		return;
	}
	else
	{
		nOpacity -= (.01 * 5);
		obj.style.opacity = nOpacity.toFixed(2).toString();
		_st("FadeOutContact()", 5);
	}

}
function FadeInJoinIE()
{
	var obj = _("divThanksIE");
	obj.style.display="inline-block";
	var oEmail = _("txtEmailIE");
	oEmail.value = "";
	oEmail.disabled=false;
	oEmail.focus();
	oPlease = _("divPleaseIE");
	oPlease.innerHTML = "ENTER YOUR INTERGALACTIC EMAIL.";		
}
function FadeInContactIE()
{
	var obj = _("divContactThanksIE");
	obj.style.display="inline-block";	
	var oEmail = _("txtFromIE");
	oEmail.value = "";
	oEmail.disabled=false;
	oEmail.focus();
	var oSub = _("txtSubjectIE");
	oSub.value="";
	oSub.disabled=false;
	var oBod = _("txtBodyIE");
	oBod.value = "";
	oBod.disabled=false;
	oPlease = _("divPleaseContactIE");
	oPlease.innerHTML = "SEND US A COSMIC MESSAGE.";		
}
function FadeOutJoinIE()
{
	var obj = _("divThanksIE");
	_sd(obj, "none");
}
function FadeOutContactIE()
{
	var obj = _("divContactThanksIE");
	_sd(obj, "none");
}

function goto(sURL){ window.location = sURL; }
