function setupConnection()
{
	var xmlHttp;
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try	{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function signup(oForm,authorEmail,form_url)
{
	//basic spam detection
	if (jQuery("#iamspam").attr('value') != "humans, please leave this text here") {
		show_error("fullname","'Contact us' ..if you are not a 'bot'.");
		return false;
	}

	var xmlHttp = setupConnection();
	var frm_elements = oForm.elements; 
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			//alert(xmlHttp.responseText); //echo reponse from the php (signupForm.php)
			if(xmlHttp.responseText == 'already') {
				show_error("email","Thanks, we already have you listed.");
			}else if(xmlHttp.responseText == 'success') {
				document.getElementById('signup-wrapper').innerHTML = '<div class="signup-message"><h3>Thanks for your interest in a StickyWorld account for your organisation</h3> <br /><br /> Our team will review your application and contact you shortly with details about our beta programme. <br /><br /> As we are now running out of accounts, we may need to contact you directly to confirm your proposed use of StickyWorld and to make sure we can support your need.  If you wish to make an appointment with our team for a demo, you can contact accounts@stickyworld.com. <br /><br /> You will find the team at the Collaboration Caf&#233; in the Building Centre throughout March. We will endeavour to deal with your interest as swiftly as possible, If you have not heard from us within seven days, please contact accounts@stickyworld.com.</div>';
			}else {	
				document.getElementById('signup-wrapper').innerHTML = xmlHttp.responseText;
			}
		}
	}
	
	if(validate_form(frm_elements))	{
		//hack for development testing
		//form_url="/wp-content/themes/stickyworld/php/signupForm.php";
		
		form_url = form_url + "?"

		for(i=0; i<frm_elements.length; i++) {
			if(frm_elements[i].type == "text" || frm_elements[i].type == "textarea") {
				form_url=form_url+frm_elements[i].name+"="+frm_elements[i].value+"&";
			}
		} 
		form_url=form_url+"authorEmail=" + authorEmail;
		form_url=form_url+"&sid="+Math.random();
		xmlHttp.open("GET",form_url,true);
		xmlHttp.send(null);
	}
}

function signupCafe(oForm,authorEmail,form_url)
{
	//basic spam detection
	if (jQuery("#iamspam-cafe").attr('value') != "humans, please leave this text here") {
		show_error("fullname","'Contact us' ..if you are not a 'bot'.");
		return false;
	}

	//register fields
	var firstname = "";
	var surname = "";
	var email = "";
	var organisation = "";
	var sticky = "http://cafe.stickyworld.com";	
	
	var xmlHttp = setupConnection();
	var frm_elements = oForm.elements; 
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var response = xmlHttp.responseText;
			if(response == 'already') {
				show_error("email-cafe","You've already joined the Caf&#233;!");
			}else if(response.substr(0,3) == 'sig') {
				var signature = response.substr(4);
				//register(firstname,surname,email,organisation,signature,sticky);
				
				jQuery('#signup-wrapper').html("<div class='signup-message'>Hi, " + firstname + ". <br /><br /> <h3>Thanks for your interest in StickyWorld.</h3> <br /><br /> We will add your name to the guestlist for the online Collaboration Caf&#233;. You can come on in from March 12th at cafe.stickyworld.com and view StickyRooms prepared by organisations on our beta programme. We will email you your membership details and password in due course. Check the calendar of events throughout March to see what\'s happening in the Caf&#233;. <br /><br /> Collaboration Caf&#233; Management.</div>");
				
			}else {
				jQuery('#signup-wrapper').html("Response: "+response); 
			}
		}
	}
	
	if(validate_form(frm_elements))	{
		//hack for development testing
		//form_url="/wp-content/themes/stickyworld/php/cafeForm.php";
		
		form_url = form_url + "?"

		for(i=0; i<frm_elements.length; i++) {
			if(frm_elements[i].type == "text" || frm_elements[i].type == "textarea") {
				form_url=form_url+frm_elements[i].name+"="+frm_elements[i].value+"&";
				if (frm_elements[i].name == "email-cafe") email = frm_elements[i].value;
				if (frm_elements[i].name == "firstname") firstname = frm_elements[i].value;
				if (frm_elements[i].name == "surname") surname = frm_elements[i].value;
				if (frm_elements[i].name == "organisation-cafe") organisation = frm_elements[i].value;
			}
		}
		
		form_url=form_url+"authorEmail=" + authorEmail;
		form_url=form_url+"&sid="+Math.random();
		xmlHttp.open("GET",form_url,true);
		xmlHttp.send(null);
	}
}

function register(r_fname,r_lname,r_email,r_organisation,r_sig,sticky_url)
{
	sticky_url = sticky_url + "/index.php?component=com_login&action=register_user&";
	sticky_url = sticky_url + "firstname="+r_fname+"&";
	sticky_url = sticky_url + "lastname="+r_lname+"&";
	sticky_url = sticky_url + "email="+r_email+"&";
	sticky_url = sticky_url + "organisation="+r_organisation+"&";
	sticky_url = sticky_url + "sig="+r_sig+"&callback=?";

	//make register call
	jQuery.getJSON(sticky_url,function(response_string){	
		//response in response_string
		if(response_string == "Success") {
			jQuery('#signup-wrapper').html("<div class='signup-message'>Hi, " + r_fname + ". <br /><br /> <h3>Thanks for your interest in StickyWorld.</h3> <br /><br /> We will add your name to the guestlist for the online Collaboration Caf&#233;. You can come on in from March 12th at cafe.stickyworld.com and view StickyRooms prepared by organisations on our beta programme. We will email you your membership details and password in due course. Check the calendar of events throughout March to see what\'s happening in the Caf&#233;. <br /><br /> Collaboration Caf&#233; Management.</div>");
		}else if(response_string.length > 17 && response_string.substr(0,17) == 'Email unavailable') {
			//we've account for you already
			jQuery('#signup-wrapper').html("<div class='signup-message'>Hi, " + r_fname + ". We've got an account ready for you. You will recieve login details on the 12th when we launch the caf&#233;. See you then!");
			//<div class='signup-message'>Hi, " + r_fname + ". We've already got an account for you. <a href='http://cafe.stickyworld.com'>Come on in!</a></div>
		} else if(response_string.length > 21 && response_string.substr(0,20) == 'Username unavailable'){
			//username used
			jQuery('#signup-wrapper').html("<div class='signup-message'>The script we are using to create your account is not all to smart just yet and has encountered a username conflict it can't resolve. Please contact us using the information provided on the contact page and we (real people) will resolve this for you.</div>");
		} else {
			//summin fubar
			jQuery('#signup-wrapper').html("<div class='signup-message'>The caf&#233; appears to be unavailable at the moment.. but we've taken note of your interest, thanks! Expect to hear from us soon.</div>");
		}
	});	
}

function show_error(for_id,error_message) {
	var err_div = jQuery("#"+for_id+"_error");
	if (err_div != null) {
		jQuery("#"+for_id+"_error .error-content").html(error_message);
		err_div.show(400);
	}
	//trigger active validation here?
}

function hide_error(for_id) {
	var err_div = jQuery("#"+for_id+"_error");
	if (err_div != null) {
		err_div.hide();
	}
}
  
function validate_form(frm_elements) {
    var valid = true;
	
	var ele_id = "";
	var ele_val = "";
	var val_pat = '';
	
	for(i=0; i<frm_elements.length; i++) {
	
		ele_id = frm_elements[i].id;
		ele_val = frm_elements[i].value;
	
		switch(ele_id) {
			case 'fullname':
				val_pat = /[a-zA-Z][a-zA-Z\-']+\s[a-zA-Z\s-']+[a-zA-Z]/;
					if (ele_val == "") {
						show_error(ele_id,"We need to know your name");
						valid = false;
					} else if (ele_val.match(val_pat)==null) {		
						show_error(ele_id,"Full name, please");
						valid = false;
					} else {
						hide_error(ele_id);
					}
				break;
			
			case 'firstname_org':
			case 'surname_org':
				val_pat = /[a-zA-Z][a-zA-Z\-\s']+/;
					if (ele_val == "") {
						show_error('name_org',"We need to know your name and surname");
						valid = false;
					} else if (ele_val.match(val_pat)==null) {		
						show_error('name_org',"Your name and surname, please");
						valid = false;
					} else {
						hide_error('name_org');
					}
				break;
			
			case 'firstname':
			case 'surname':
				val_pat = /[a-zA-Z][a-zA-Z\-\s']+/;
					if (ele_val == "") {
						show_error('name',"We need to know your name and surname");
						valid = false;
					} else if (ele_val.match(val_pat)==null) {		
						show_error('name',"Your name and surname, please");
						valid = false;
					} else {
						hide_error('name');
					}
				break;
			
			case 'type-other':
					if (ele_val == "") {
						show_error(ele_id,"What type of organisation?");
						valid = false;
					} else {
						hide_error(ele_id);
					}
				break;
			
			case 'organisation':
					if (ele_val == "") {
						show_error(ele_id,"The name of your organisation?");
						valid = false;
					} else {
						hide_error(ele_id);
					}
				break;
			
			case 'email':
			case 'email-cafe':
				val_pat = /^[a-zA-Z]+[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-zA-Z]{2,4}$/;
					if (ele_val == "") {
						show_error(ele_id,"Where do you want your login details sent?");
						valid = false;
					} else if (ele_val.match(val_pat)==null) {
						show_error(ele_id,"That doesn't seem right");
						valid = false;
					} else {
						hide_error(ele_id);
					}
				break;
				
			case 'primaryuse':
			case 'primaryuse-cafe':
			case 'amount':
			case 'amount-cafe':
					if (ele_val == "") {
						show_error(ele_id,"What are your needs?");
						valid = false;
					} else {
						hide_error(ele_id);
					}
				break;
				
			case 'preferredurl':
				val_pat = /^[a-zA-Z0-9_.-]+$/;
					if (ele_val == "") {
						show_error(ele_id,"You'd like your online space to be where?");
						valid = false;
					}else if (ele_val.match(val_pat)==null) {
						show_error(ele_id,"Only a-z, 0-9 hyphens and underscores, please");
						valid = false;
					} else {
						hide_error(ele_id);
					}
				break;
		}
	} 

    return valid;
}