
function displayText(id,url) {

	var element = document.getElementById(id);

	if (!element) {
		alert("Bad id " + id +
			"passed to clientSideInclude." +
			"You need a div or span element " +
			"with this id in your page.");
		return;
	}

  element.innerHTML = serverFunction(url);
}


function isEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

function serverFunction(url)
{
	var req = false;
	// For Safari, Firefox, and other non-MS browsers
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = false;
		}
	} else if (window.ActiveXObject) {
		// For Internet Explorer on Windows
		try {
			   req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			req = false;
			}
		}
	}

	if (req) {
	// Synchronous request, wait till we have it all

	req.open('GET', url, false);
	req.send(null);

	if(req.responseText) {
       // force refresh on DB error, most likely a session time out.
       if (req.responseText.indexOf('!!refresh!!') != -1)
          {          
          window.location = "/";
          }

	   if(req.responseText.substring(0,7) == 'debug!!') {
	       alert(req.responseText.substring(7));
	   }
	}

	return(req.responseText);
	} else {
		alert("Sorry, your browser does not support " +
			"XMLHTTPRequest objects. This page requires " +
			"Internet Explorer 5 or better for Windows, " +
			"or Firefox for any system, or Safari. Other " +
			"compatible browsers may also exist.");
	}
}

function insertConfession()
{
     var form=document.getElementById('confession_form');
     if (!validateConfessionForm(form))
          return false;
     var url='index.php?req=insertconfession'+
               '&name='+encodeURIComponent(form.name.value)+
               '&zipcode='+encodeURIComponent(form.zipcode.value)+
               '&confession='+encodeURIComponent(form.confession.value)+
               '&captcha_code='+encodeURIComponent(form.captcha_code.value);

     ret=serverFunction(url);
     window.location.reload(true);
//     displayText('maincontent','index.php');
     return false;
}

function validateConfessionForm(form)
{
     var error_message = "The following errors occurred:\n";
     var errors=0;

/*     if (isEmpty(form.name))
          {
          error_message = error_message + "\t\nYou must enter your name.";
          errors=1;
          }*/
     if (isEmpty(form.confession))
          {
          error_message = error_message + "\t\nYou must enter your confession.";
          errors=1;
          }

     if (errors==0)
          return true;
     alert(error_message);
     return false;    
}

function displayResultsPage(pageno)
{
     var form=document.getElementById('confession_form');
     var url='index.php?pageno='+pageno+'&switch_id='+encodeURIComponent(form.switch_id.value);
//     ret=serverFunction(url);
     displayText('container',url);
     return false;
}

function absolve(confession_id)
{
     var form=document.getElementById('confession_form');
     var url='index.php?req=absolve'+
               '&confession_id='+confession_id;
     ret=serverFunction(url);
     var url='index.php?pageno='+encodeURIComponent(form.thispageno.value)+'&switch_id='+encodeURIComponent(form.switch_id.value);
     displayText('container',url);
     return false;
}     

function switchView(switch_id)
{
     var url='index.php?req=defaultDisplay'+
               '&switch_id='+switch_id;
     //ret=serverFunction(url);
     //var url='index.php?pageno=0';
     displayText('container',url);
     return false;
}     

