function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function toggleSignupValue() {

    if (! document.getElementById) return;
    if (! document.getElementById('UserEmail')) return;
    var emailinput = document.getElementById('UserEmail');
	var inputvalue = emailinput.value;
    emailinput.onclick= function() {
	                        this.value = '';
                       }
    emailinput.onblur = function() {
	                        if(this.value == '') {
                                this.value = inputvalue;
                            }
                       }

   if (! document.getElementById('UserName')) return;
	var nameinput = document.getElementById('UserName');
	   var nameinputvalue = nameinput.value;
	   nameinput.onclick= function() {
	                           this.value = '';
	                      }
	   nameinput.onblur = function() {
	                           if(this.value == '') {
	                               this.value = nameinputvalue;
	                           }
						 }                       
}

/*
 * Create the XMLHttpRequest object
 */

var xmlhttp=false;
try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		xmlhttp = false;
	}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	xmlhttp = new XMLHttpRequest();
}

/*
 * Adds the JavaScript to the XHTML, so it is unobtrusive
 */

function prepareSignup() {
	
	// Make sure JavaScript is enabled
	if (!document.getElementById) return false; 
	if (!document.getElementById("signupForm")) return false;
	
	// add this in in case people hit enter instead of clicking on the submit link
	var signupForm = document.getElementById("signupForm");	
	signupForm.onsubmit = function() {
		doSignup();
		return false;
	}
}

/*
 * Uses AJAX to call newsletter/signup.php
 */

function doSignup() {

	// Make sure JavaScript is enabled
	if (!document.getElementById) return false;
	
	var valEmail = document.getElementById("UserEmail");
	var valName = document.getElementById("UserName");

	var signup = document.getElementById("signup");
	var url = "/signup";
	var params = 'data[User][email]='+ valEmail.value +'&data[User][name]='+ valName.value;
	
	xmlhttp.open("POST", url, true);
	//Send the proper header information along with the request
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {			
			if (xmlhttp.responseText != "")
				// Replace the signup, if we got a response from doSignup.php
				// It won't return anything, if they don't enter an email address
				signup.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(params);	
}

// Add the JavaScript to appropriate places so it's not obtrusive in the XHTML
addLoadEvent(prepareSignup);
addLoadEvent(toggleSignupValue);
