//======= AJAX handler
function AJAXrequest() {
  var http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('You are using an outdated browser!\nPlease upgrade to FireFox (http://www.getfirefox.com)');
	 return false;
  }
  return http_request; // return the connection
}

//======= INIT our ajax handler
var AJAX = AJAXrequest();

//======= AJAX ACTION: Sends answer to flat file and requests step 2 content
function request_STEP2(answer) {
	var sending = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="middle"><img src="surveys/images/preload.gif" width="48" height="35"></td></tr><tr><td height="29" align="center" valign="middle"><strong>Sending your response...</strong></td></tr></table>';
	document.getElementById('surveycontent').innerHTML=sending; // put the content into the div
	var poststr = 'answer=' + answer;
	var url = 'surveys/AJAX_processor.php?a=GetStep2';
	//alert(poststr);
	//alert(url);
	AJAX.open('POST', url, true);
	//alert('about to send');
	AJAX.onreadystatechange = recieve_STEP2;
	AJAX.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	AJAX.setRequestHeader('Content-length', poststr.length);
	AJAX.setRequestHeader('Connection', 'close');
	//alert('sending');
	AJAX.send(poststr);
	//alert('sent');
	if (answer == 'yes') {
		TINY.box.size('tinybox',444,540,4);	
	} else {
		TINY.box.size('tinybox',444,320,4);
	}
}

//======= AJAX ACTION: Recieves step 2 page content + adds into content div
function recieve_STEP2() {
  if (AJAX.readyState == 4) {
	 if (AJAX.status == 200) {
		//alert(AJAX.responseText);
		result = AJAX.responseText;
		document.getElementById('surveycontent').innerHTML=result; // put the content into the div
	 } else {
		alert('There was a problem processing your request, please try again in a moment');
	 }
  }
}

//======= AJAX ACTION: add user to the waiting list
function request_addtolist() {
	var useremail = document.getElementById('myemail').value;
	var username = document.getElementById('myname').value;
	if(useremail == '' || username == ''){
		alert('Opps! You have to give us your name and email address so we can contact you!');
	}else{
		var sending = '<table border="0" cellspacing="0" cellpadding="2"><tr><td align="center" valign="middle"><img src="surveys/images/small_loader.gif" width="16" height="16"></td><td align="center" valign="middle" nowrap>Adding you to the list...</td></tr></table>';
		document.getElementById('addtoliststatus').innerHTML=sending; // put the content into the div
		var poststr = 'name=' + username + '&email=' + useremail;
		var url = 'surveys/AJAX_processor.php?a=addtolist';
		//alert(poststr);
		//alert(url);
		AJAX.open('POST', url, true);
		//alert('about to send');
		AJAX.onreadystatechange = recieve_addtolist;
		AJAX.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		AJAX.setRequestHeader('Content-length', poststr.length);
		AJAX.setRequestHeader('Connection', 'close');
		//alert('sending');
		AJAX.send(poststr);
		//alert('sent');
	}
}

//======= AJAX ACTION: Recieves confirmation of adding to list, show response.
function recieve_addtolist() {
  if (AJAX.readyState == 4) {
	 if (AJAX.status == 200) {
		//alert(AJAX.responseText);
		result = AJAX.responseText;
		document.getElementById('addtoliststatus').innerHTML=result; // put the content into the div
	 } else {
		alert('There was a problem processing your request, please try again in a moment');
	 }
  }
}

//======= AJAX ACTION: send email to friend letting them know of the survey
function request_send2friend() {
	var friendemail = document.getElementById('friendemail').value;
	var friendname = document.getElementById('friendname').value;
	var yourname = document.getElementById('yourname').value;
	if(friendemail == '' || friendname == '' || yourname == ''){
		alert('Opps! You have to give us your friends name and email address so we can contact them, and also your name to let them know who sent them the email!');
	}else{
		document.getElementById('send2friend').disabled=true;
		var sending = '<table border="0" cellspacing="0" cellpadding="2"><tr><td align="center" valign="middle"><img src="surveys/images/small_loader.gif" width="16" height="16"></td><td align="center" valign="middle" nowrap>Sending...</td></tr></table>';
		document.getElementById('send2friendstatus').innerHTML=sending; // put the content into the div
		var poststr = 'name=' + friendname + '&email=' + friendemail + '&username=' + yourname;
		var url = 'surveys/AJAX_processor.php?a=send2friend';
		//alert(poststr);
		//alert(url);
		AJAX.open('POST', url, true);
		//alert('about to send');
		AJAX.onreadystatechange = recieve_send2friend;
		AJAX.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		AJAX.setRequestHeader('Content-length', poststr.length);
		AJAX.setRequestHeader('Connection', 'close');
		//alert('sending');
		AJAX.send(poststr);
		//alert('sent');
	}
}

//======= AJAX ACTION: Recieves confirmation of sent to friend, show response.
function recieve_send2friend() {
  if (AJAX.readyState == 4) {
	 if (AJAX.status == 200) {
		//alert(AJAX.responseText);
		result = AJAX.responseText;
		document.getElementById('send2friendstatus').innerHTML=result; // put the content into the div
		document.getElementById('send2friend').disabled=false;
	 } else {
		alert('There was a problem processing your request, please try again in a moment');
	 }
  }
}