//<!--



var defaultEmail = '';
var defaultMessage = 'Join%20the%20gazillions%20who%20are%20receiving%20%3Cem%3EThe%20Dubai%20Enquirer%3C%2Fem%3E%20for%20free%20in%20their%20inbox%21';









var display;
var formIsActive = false;
var loaded = false;
var IE = document.all ? true : false;
var ajaxTarget = "http://co-lo.night-shade.org.uk/~nick/alex/saveEmail.php";
var req;



function setEmail(){
	display = document.getElementById('emailDiv');
	
	displayEmailForm();
	loaded = true;
}

function safeAppendChild(node,targetNode){
	if(targetNode.appendChild){
		return targetNode.appendChild(node);
	}
	if(targetNode.replaceChild && targetNode.childNodes){
		return targetNode.replaceChild(newE,targetNode.childNode[0]);
	}
	return false;
}		


function displayThankYou(){
	display.style.display = "none";
}

function displayEmailForm(){
	
	this.HTML = "<p id='emailFormHeader' style='font:500 12px arial, sans-serif;margin:0px;padding:0px;'>" + unescape(defaultMessage) + "</p>";
	this.HTML += "<p style='font:500 12px arial, sans-serif;margin:0px;padding:5px 0px 0px 0px;'>Email address*: <br /><input type='text' id='emailAddressBox' value='" + unescape(defaultEmail) + "' /></p><p style='font:500 12px arial, sans-serif;margin:0px;padding:5px 0px 0px 0px;'>Name: <br /><input type='text' id='emailNameBox' /></p>";
	this.HTML += "<input type='button' value='Subscribe' onclick='ajaxSend();' style='margin-top:5px;' id='emailButton' />";
	display.innerHTML = this.HTML;
	var inputs = display.getElementsByTagName('input');
	for(this.i=0;this.i<inputs.length;this.i++){
		inputs[this.i].onfocus = function () {formIsActive=true;};
		inputs[this.i].onblur = function () {formIsActive=false;};
	}
}

function ajaxSend(){
	if(!loaded){
		return false;
	}
	
	var emailAddress = document.getElementById('emailAddressBox').value;
	var name = document.getElementById('emailNameBox').value;
	if(!checkEmailAddress(emailAddress)){
		alert("That is not a valid email address");
		return false;
	}
	if(name.length<1){
		name = "Not given";
	}
	var head = document.getElementById('emailFormHeader');
	if(head){
		head.innerHTML = "Sending data...";
		head.style.backgroundColor = "red";
		head.style.color="white";
		head.style.padding = "3px";
	}
	document.getElementById('emailAddressBox').disabled = true;
	document.getElementById('emailNameBox').disabled = true;
	document.getElementById('emailAddressBox').disabled = true;
	document.getElementById('emailButton').disabled = true;
	
	var postVars = "name="+escape(name)+"&email="+escape(emailAddress);
	req  = XMLHTTPObject();
	
	try{
	req.open("POST",ajaxTarget);
	}
	catch(e){
		alert(e);
	}
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	req.send(postVars);
	
	req.onreadystatechange = function () {readyStateHandler();}
	return true;
	
}

function readyStateHandler (){
	if(req.readyState==4){
		if(req.status == 200){
			parseXML(req);
		}else{
			alert('We have a problem with our servers. Sorry!');
		}
	}
}


function parseXML(a){
	if(!a.responseXML){
		var head = document.getElementById('emailFormHeader');
		if(head){
			head.innerHTML = "We've had a server malfunction. Sorry!.";
			head.style.backgroundColor = "red";
			head.style.color="white";
			head.style.padding = "3px";
		}
		return false;
	}
	var de = a.responseXML.documentElement;
	var saved = de.getElementsByTagName('saved')[0];
	if(saved.getAttribute('complete')==1){
		var head = document.getElementById('emailFormHeader');
		if(head){
			head.innerHTML = "Your details have been saved.";
			head.style.backgroundColor = "#ddd";
			head.style.color="black";
			head.style.padding = "0px";
		}
		setCookie("signedUp",1);
		return true;
	}else{
		var head = document.getElementById('emailFormHeader');
		var XMLerror = de.getElementsByTagName('error')[0];
		XMLerror = XMLerror.getAttribute('text');
		if(head){
			head.innerHTML = unescape(XMLerror);
			head.style.backgroundColor = "red";
			head.style.color="white";
			head.style.padding = "3px";
		}
		return false;
	}
}



	
function checkEmailAddress(a){
	if(a.length<1){
		return false;
	}
	var array = a.split("@");
	if(array.length!=2){
		return false;
	}
	var array = array[1].split(".");
	if(array.length<2){
		return false;
	}
	return true;
}
	
	
	
	
	








/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function XMLHTTPObject() {
   var xmlhttp;
 if (window.ActiveXObject) {
    // Instantiate the latest Microsoft ActiveX Objects
    if (_XML_ActiveX) {
       xmlhttp = new ActiveXObject(_XML_ActiveX);
     } else {
     // loops through the various versions of XMLHTTP to ensure we're using the latest
     var versions = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP", 
"Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", 
"Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0"];
     for (var i = 0; i < versions.length ; i++) {
       try {
        // Try and create the ActiveXObject for Internet Explorer, if it doesn't work, try again.
        xmlhttp = new ActiveXObject(versions[i]);
         if (xmlhttp) {
          var _XML_ActiveX = versions[i];
          break;
        }
      }
      catch (e) {
         // TRAP
       } ;
     }
   ;
   }
  } // Well if there is no ActiveXObject available it must be firefox, opera, or something else
   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
       xmlhttp = false;
     }
   }
  return xmlhttp;
 }



// -->


window.onload = setEmail;
//window.onkeypress = handleKeyPress;