var intCurrentScreen = 1; // Indicates in which screen the user is
var MAX_SCREEN = 8; //Indicates total number of screens - controls logic + progressbar + next/previous buttons
blnAjaxHasChecked = false;


function goToNextScreen(){	
	//See if all required fields are filled, if not display error message and return
	if (! checkFieldsOkay(intCurrentScreen)){
		//alert is shown by checkFields itself
		return;
	}
	if (! document.survey){
		wioDebug("survey form not found");
		return;
	}
	objF = document.survey;
	//in some steps extra logic is needed
	switch (intCurrentScreen){
		case 1:
			//blnAjaxHasChecked is set by function after checknewuser
			if (blnAjaxHasChecked){
				break;
			}
			
			// first check if the given date is rrrreaaaallly a valid date
			if (! isValidDate(objF.birthdate.value)){
				showError(strInvalidDate);
				return;
			}
			
			//AJAX call to check if customer is new
			//prepare the query
			var ajaxQuery = "email=" + escape(objF.email.value);
			ajaxQuery += "&firstname=" + escape(objF.firstname.value);
			ajaxQuery += "&lastname=" + escape(objF.lastname.value);
			wioSetAjaxConfig(strURLCheckNewUser,"ajaxCheckNewUser()");
			GETRequest(ajaxQuery);
			//return here and call goToNextScreen from ajaxCheckNewUser if all is well
			return;
			break;
		case 2:
			blnAjaxHasChecked = false;
		case 3:
			//check date of last diet if necessary
			if (/true/i.test(getFormElementValue(objF.previousregimefollowed))){				
				if (! isValidDate(objF.previousregime_lastdate.value)){
					showError(strInvalidDate);
					return;
				}
			}
			break;
		case 6:
			//get all data to post by recursing the form elements and creating a 'querystring' (to post)
			strPostData = "";			
			strElemNames = "";
			for (var i = 0; i < objF.elements.length; i++){
				objElem = objF.elements[i];
				elemName = objF.elements[i].name;
				elemValue = getFormElementValue(objF.elements[elemName]) + "";
				//If the value is empty don't add it
				if (elemValue.length < 1){
					continue;
				}
				//If the elemenent is already treated, don't add it again
				if (strElemNames.indexOf("$" + elemName + "$") >= 0){
					//wioDebug("Element [" + elemName + "] already in [" + strElemNames + "]");
					continue;
				}else{					
					strElemNames += "$" + elemName + "$";
					//wioDebug("Added [" + elemName + "] to [" + strElemNames + "]");
				}
				//Adding it to the postData
				if (i > 0){ strPostData += "&";}
				strPostData += elemName + "=" + elemValue;
				//wioDebug("[" + elemName + "] = [" + elemValue + "]");
			}
			wioDebug("Postdata [" + strPostData + "]");
			//AJAX call to get HTML of confirmation and text of SQL
			wioSetAjaxConfig(strURLConfirmation,"ajaxDisplayConfirmation()");
			POSTRequest(strPostData);
				//Next steps are in function ajaxDisplayConfirmation();
				//check if ajax call is ok
				//put confirmation HTML in  'wrapperconfirmationoutput'
				//create POST data to send to step 8 and store in 'strPostEmailUpdatedatabase'
			break;
		case 7:	
			//RESULT SCREEN via AJAX
			document.getElementById("wrapperresultoutput").innerHTML = "";
			strPostData = "";	
			strPostData += "confirmationmsg=" + document.getElementById("wrapperconfirmationoutput").innerHTML;
			strPostData += "&qryInsertClient=" + document.getElementById("qryInsertClient").innerHTML;
			strPostData += "&qryInsertBMI=" + document.getElementById("qryInsertBMI").innerHTML;
			strPostData += "&qryInsertMotivation=" + document.getElementById("qryInsertMotivation").innerHTML;
			strPostData += "&qryInsertLifestyle=" + document.getElementById("qryInsertLifestyle").innerHTML;
			strPostData += "&qryInsertEatingHabits=" + document.getElementById("qryInsertEatingHabits").innerHTML;
			strPostData += "&qryInsertContactInfo=" + document.getElementById("qryInsertContactInfo").innerHTML;
			strPostData += "&qryInsertPersonalMessage=" + document.getElementById("qryInsertPersonalMessage").innerHTML;
			strPostData += "&country=" + getFormElementValue(objF.country);
			strPostData += "&coachid=" + getFormElementValue(objF.coachid);
			strPostData += "&coachlastname=" + getFormElementValue(objF.coachlastname);
			strPostData += "&coachfirstname=" + getFormElementValue(objF.coachfirstname);
			strPostData += "&coachemail=" + getFormElementValue(objF.coachemail);
			strPostData += "&coachphone=" + getFormElementValue(objF.coachphone);
			strPostData += "&custemail=" + getFormElementValue(objF.email);
			//AJAX call to send email and update database
			wioSetAjaxConfig(strURLHandleForm,"ajaxResultScreen()");
			POSTRequest(strPostData);			
			break;
	}
	
	//Now move to the next screen
	var nextScreen = intCurrentScreen +1;
	showButtons(nextScreen);
	switchScreens(nextScreen, intCurrentScreen);
	intCurrentScreen++;
}


function goToPreviousScreen(){
	var previousScreen = intCurrentScreen -1;
	showButtons(previousScreen);
	switchScreens(previousScreen, intCurrentScreen);
	intCurrentScreen--;
}

/* Shows NEXT and PREVIOUS buttons when moving in between screens */
function showButtons(intNextScreen){
	//intNextScreen is the screen you are going to, either 'next' or 'previous', it does not mean "next" by definition
	//wioDebug('Showing buttons for coming screen [' + intNextScreen + ']');
	if (! document.getElementById('divprevbutton')){ return;}
	if (! document.getElementById('divnextbutton')){ return;}
	if( intNextScreen > 1 && intNextScreen < MAX_SCREEN ){ //TODO, uncomment this line so previous button is deactivated in last step, and comment next line
	//if( intNextScreen > 1 ){ //uncomment this line while testing, it will permit you to go back after the result
		//show previous button
		document.getElementById("divprevbutton").style.display = 'block';
	}else{
		//hide previous button
		document.getElementById("divprevbutton").style.display = 'none';
	}
	if (intNextScreen < MAX_SCREEN){
		//show next button
		document.getElementById("divnextbutton").style.display = 'block';
	}else{
		//hide next button
		document.getElementById("divnextbutton").style.display = 'none';
	}
}

/* Makes next/previous screen visible and hides current*/
function switchScreens(intShowScreen, intHideScreen){
	idHide = "divscreen" + intHideScreen;
	idShow = "divscreen" + intShowScreen;
	//wioDebug('Hiding [' + idHide + '] and showing [' + idShow + ']');
	if (document.getElementById(idHide)){ 	
		document.getElementById(idHide).style.display = 'none';
	}else{wioDebug('not found ' + idHide);}
	if (document.getElementById(idShow)){
		document.getElementById(idShow).style.display = 'block';
	}else{wioDebug('not found ' + idShow);}
}

/* Changes the progress bar when moving in between screens */
function moveProgressBar(intShowScreen, intHideScreen){
	switchImg(document.images['pb' + intHideScreen]);
	switchImg(document.images['pb' + intShowScreen]);
}

/* 
Checks form input before moving to next screen
- Check all required fields from array list
- Check all optional fields from array list specifying (parent field, required value)
- TODO: add optional regex parameter to fields
Returns TRUE if all is well, otherwise FALSE
*/
function checkFieldsOkay(intCurrentScreen){
	if (!blnCheckFields || (intCurrentScreen < intwioCheckFieldsFromScreen)){ return true;}
	blnErrorOccurred = false;
	var strErrorMessage = strFillRequiredFields;
	strErrorMessage += "<ul>\n";
	
	//Check all of the REQUIRED FIELDS
	var arrReqFields = eval("arrRequiredFieldsScreen" + intCurrentScreen);	
	for (var i = 0; i < arrReqFields.length; i++){
		strFieldName = arrReqFields[i][0];
		//wioDebug("Checking [" + strFieldName + "]");
		strFieldValue = getFormElementValue(document.survey.elements[strFieldName]) + "";
		//wioDebug("Value found [" + strFieldValue + "]");
		if (strFieldValue.length <= 0){
			blnErrorOccurred = true;
			strErrorMessage += ("\t<li> " + arrReqFields[i][1] + "</li>\n");
			continue; //move to next field because there is no value to check
		}
		//check regex if present
		if (arrReqFields[i].length == 3){
			reToCheck = arrReqFields[i][2];
			if (! reToCheck.test(strFieldValue) ){
				strErrorMessage += ("\t<li> " + arrReqFields[i][1] + strInvalidField + "</li>\n");
				blnErrorOccurred = true;
			}
		}
	}
	
	//Check all of the OPTIONAL FIELDS if necessary
	var arrOptionalFields = eval("arrOptionalFieldsScreen" + intCurrentScreen);
	for (var i = 0; i < arrOptionalFields.length; i++){
		strFieldName = arrOptionalFields[i][0];
		strParentFieldName = arrOptionalFields[i][2];
		//wioDebug("Checking [" + strFieldName + "] linked with parent [" + strParentFieldName + "]");
		strFieldValue = getFormElementValue(document.survey.elements[strFieldName]) + "";
		strParentFieldValue = getFormElementValue(document.survey.elements[strParentFieldName]) + "";
		//wioDebug("Value found [" + strFieldValue + "] and parent value [" + strParentFieldValue + "]");
		strParentRequiredValue = new String(arrOptionalFields[i][3]);
		if (eval("/" + strParentRequiredValue +"/gi.test(strParentFieldValue)")){
			//wioDebug("This optional field is now required.");
		}else{
			//wioDebug("Remains optional because required value [" + strParentRequiredValue + "] is not in real value [" + strParentFieldValue + "]");
			continue; // check next element
		}
		if (strFieldValue.length <= 0){
			blnErrorOccurred = true;
			strErrorMessage += ("\t<li> " + arrOptionalFields[i][1] + "</li>\n");
			continue; //move to next field because there is no value to check
		}
		//check regex if present
		if (arrOptionalFields[i].length == 5){
			reToCheck = arrOptionalFields[i][4];
			if (! reToCheck.test(strFieldValue) ){
				strErrorMessage += ("\t<li> " + arrOptionalFields[i][1] + strInvalidField + "</li>\n");
				blnErrorOccurred = true;
			}
		}
	}
	
	strErrorMessage += "</ul>\n";
	if (blnErrorOccurred){
		showError(strErrorMessage);
		return false;
	}else{
		return true;
	}
}



/* show BMI info pane - Requires pre-existing DIV width "infopane" in page */
function showBmiInfo(){
	divInfopane = document.getElementById("bmipane");
	divInfopane.style.display = "block";
}

function hideBmiInfo(){
	divInfopane = document.getElementById("bmipane");
	divInfopane.style.display = "none";
}

/*
Auto-calculate BMI based on input values and display in non editable field.
Change background colour of BMI field based on result, config below
arrBMI = array of ranges (minValue, maxValue, strBackgroundColour, strTextColour
*/
arrBMI = new Array();
arrBMI[arrBMI.length] = new Array(0, 18.9, "#f9c23e", "#FFFFFF"); //Maigreur - orange
arrBMI[arrBMI.length] = new Array(18.9, 24.9, "#00ff00", "#FFFFFF"); // Poids idéal - green
arrBMI[arrBMI.length] = new Array(24.9, 29.9, "#a1b801", "#FFFFFF"); // Surpoids - green/orange= teal
arrBMI[arrBMI.length] = new Array(29.9, 34.9, "#f9c23e", "#FFFFFF"); // Obésité modérée - orange
arrBMI[arrBMI.length] = new Array(34.9, 39.9, "#ff8301", "#FFFFFF"); // Obésité sévère
arrBMI[arrBMI.length] = new Array(39.9, 9999, "#ff1100", "#FFFFFF"); // Obésité massive
strBMIDefaultBackgroundColour = "#CCC";
strBMIDefaultTextColour = "#FFF";


function calculateBmi(objForm,strFieldNameLength,strFieldNameWeight,strFieldNameBMI){
	//getFormElementValue
	floatLength = wioParseFloat(getFormElementValue(objForm.elements[strFieldNameLength]));
	floatWeight = wioParseFloat(getFormElementValue(objForm.elements[strFieldNameWeight]));
	
	floatBMI = "";
	strBackgroundColour = strBMIDefaultBackgroundColour;
	strTextColour = strBMIDefaultTextColour;
	
	//Calculate BMI only if all values are present and are numeric
	if (! isNaN(floatLength) && !isNaN(floatWeight)){
		//Calculate BMI and get colours from arrBMI
		floatLength = floatLength / 100; //change to meters
		floatBMI = (floatWeight / (floatLength * floatLength));
		floatBMI = parseInt(floatBMI * 100);
		floatBMI = wioParseFloat(floatBMI / 100);
		//wioDebug("BMI for length [" + floatLength + "] and weight [" + floatWeight + "] is [" + floatBMI + "]");
		
		for (var i = 0; i < arrBMI.length; i++){
			if (floatBMI > arrBMI[i][0] && floatBMI <= arrBMI[i][1]){
				strBackgroundColour = arrBMI[i][2];
				strTextColour = arrBMI[i][3];
				break;
			}
		}
	}
	//Change BMI value and layout if the field can be found
	if (objForm.elements[strFieldNameBMI]){
		objFieldBMI = objForm.elements[strFieldNameBMI];
		objFieldBMI.style.backgroundColor = strBackgroundColour;
		objFieldBMI.style.color = strTextColour;
		objFieldBMI.value = (parseInt(floatBMI * 100))/100;
	}else{
		wioDebug("BMI field [" + strFieldNameBMI + "] cannot be found.");
	}
}

/* Calculate targetloss based on given targetweight/targetloss  and update targetloss/targetweight field*/
function calculateTargetLossWeight(objForm, strFieldNameWeight, intTargetWeightLoss,strFieldNameTargetLossWeight){
	floatWeight = wioParseFloat(getFormElementValue(objForm.elements[strFieldNameWeight]));
	floatTarget= wioParseFloat(intTargetWeightLoss);
	if (isNaN(floatWeight) || isNaN(floatTarget)){
		wioDebug("Weight[" + floatWeight + "] or target[" + floatTarget + "] is not a number");
		return;
	}else{
		floatTargetNumber = floatWeight - floatTarget;
		//wioDebug("[" + floatWeight + "] - [" + floatTarget + "] = [" + (floatWeight - floatTarget) + "]");
		if (objForm.elements[strFieldNameTargetLossWeight]){
			objForm.elements[strFieldNameTargetLossWeight].value = (parseInt(floatTargetNumber * 100))/100;
		}
	}
}

/* update targetloss/targetweight when currentweight changes */
function recalculateTargets(objForm, intUpdatedWeight){
	floatTargetWeight = wioParseFloat(getFormElementValue(objForm.elements["targetweight"]));
	floatTargetLoss = wioParseFloat(getFormElementValue(objForm.elements["targetloss"]));
	
	//if targetweight is known, recalculate targetloss and update value and get the hell out
	if (! isNaN(floatTargetWeight) && !isNaN(intUpdatedWeight)){
		objForm.elements["targetloss"].value =  (parseInt((intUpdatedWeight - floatTargetWeight)*100))/100;
	}
	//if targetloss is known, recalculate targetweight, update value and let that be the end of it
	if (! isNaN(floatTargetLoss) && !isNaN(intUpdatedWeight)){
		objForm.elements["targetweight"].value = (parseInt((intUpdatedWeight - floatTargetLoss)*100))/100;
	}
}

/*
display function to write checkboxes|radioelements based on array of value|labels corresponding to bit values in database
!! naming convention for checkbox items: database column = 'fieldname_value'
	checkbox name = 'fieldname'
	different values corresponding to different columns with 'value' suffix'
	this to allow easy transformation to SQL and or HTML retrieval afterwards
Parameters:
- strType = 'radio' or 'checkbox'
- strFieldname = string used to identify input element
- arrLabelValue, (see config.js) list of values (depends on DB column names) and labels (language specific) + addOnChangeEvent
- intItemsPerLine, line layout is:
	<div class='radiocheckboxdiv'>
		<span class='radioitemx'><input type='checkbox|radio' value='xxx' onChange='optionaljs'> strLabel</span>
		<!-- repeated for intItemsPerLine -->
	</div>
- blnShowDiv: defaults to true, wether to yes or no use a <div class='radiocheckboxdiv'></div> tag around the input checkboxes
*/
function writeCheckboxRadio(strType, strFieldname, arrLabelValues, intItemsPerLine, blnShowDiv){
	 if (typeof blnShowDiv == "undefined") {
		blnShowDiv = true;
	}
	
	var itemcounter = 1;
	var strHTML = "";
	for (var i=0; i < arrLabelValues.length; i++){
		if (itemcounter == 1 && blnShowDiv){ 
			strHTML += "<div class='radiocheckboxdiv'>\n";
		}
		strHTML += "<p style='float:left' class='radioitem" + intItemsPerLine + "'>";
		strHTML += "<input type='" + strType + "' name='" + strFieldname + "'";
		switch (strType){
			case 'checkbox':
				//strHTML += " value='" + strFieldname + "_" + arrLabelValues[i][0].replace(/'/,"&acute;") +"'"; //Checkboxes have to repeat fieldname
				strHTML += " value='" + arrLabelValues[i][0].replace(/'/,"&acute;") +"'"; //Checkboxes have to repeat fieldname
				break;
			default:
				strHTML += " value='" + new String(arrLabelValues[i][0]).replace(/'/,"&acute;") +"'"; //Radiobutton and all others
				break;
		}
		if (arrLabelValues[i].length == 3){
			//dynamicblock
			if(strType == "checkbox"){
				strHTML += " onClick='showhideDynamic(this.checked,\"" + arrLabelValues[i][2] + "\")'"; //for checkboxes, place this on the value which will open up the new div
			}else{
				strHTML += " onClick='showhideDynamic(this.checked,\"" + arrLabelValues[i][2] + "\", \"" + arrLabelValues[i][0].replace(/'/,"&acute;") + "\")'"; //for radiobuttons add value which must be a boolean, only the 'true' value will show it
			}
			/* TODO - change code for radiobutton... two elements who can showhide the div, use another function */
		}
		strHTML += "/> " + arrLabelValues[i][1] +"</p>";
		if (itemcounter == intItemsPerLine && blnShowDiv){ 
			strHTML +="</div>\n";
		}
		itemcounter++;
		if (itemcounter > intItemsPerLine ){itemcounter = 1;} 
	}
	//close div tag if itemcounter has not reached end (itemcounter != 1)
	if (itemcounter != 1 && blnShowDiv){ strHTML +="</div>\n";}
	
	//wioDebug(strHTML);
	document.write(strHTML);
}

/*  showhideDynamic block if 'parent item' is checked. Only supports radio/checkbox parent items */
function showhideDynamic(blnIsChecked, strElementIdToShow, strOptionalBooleanFieldValue){
	 if (typeof strOptionalBooleanFieldValue == "undefined") {
		//wioDebug("No fieldvalue setting to default");
		blnOptionalBooleanFieldValue = true;
	}else{
		blnOptionalBooleanFieldValue = /false/i.test(strOptionalBooleanFieldValue) ? false : true;
	}
	//wioDebug("Fieldvalue [" + blnOptionalBooleanFieldValue + "]");
	
	if (document.getElementById(strElementIdToShow)){
		//wioDebug("Checked [" + blnIsChecked + "]");
		if (blnIsChecked && blnOptionalBooleanFieldValue){
			document.getElementById(strElementIdToShow).style.display = "block";
		}else{
			document.getElementById(strElementIdToShow).style.display = "none";
		}
	}else{
		wioDebug("Element with ID [" + strElementIdToShow + "] not found.");
		return;
	}
}

/* 
	Displays the progressbar images 
	linked with intCurrentScreen and MAX_SCREEN variables see on top
*/
PB_MAX_SUPPORTED_STEPS = 8; //limited by existing images
PB_FOLDER_DIR = "/images/progressbar/";
PB_DIVIDER_IMAGE = "divider.png";
PB_DIVIDER_WIDTH = 10;
PB_DIVIDER_HEIGHT = 15;
PB_ICON_WIDTH = 20;
PB_ICON_HEIGHT = 20;

function showProgressBar(){
	strHTML = "<!-- start progressbar -->\n";
	strHTML += "<table width='300' height='20' cellspacing='0' cellpadding='0'><tr>\n";
	if (MAX_SCREEN > PB_MAX_SUPPORTED_STEPS){
		wioDebug("Attention: you are trying to use more steps then is supported in graphical format.");
	}
	for (var i = 1; i <= MAX_SCREEN; i++){
		if (i > 1){ 
			strHTML += "<td width='" + PB_DIVIDER_WIDTH + "' height='" + PB_DIVIDER_HEIGHT + "'><img src='" + PB_FOLDER_DIR + PB_DIVIDER_IMAGE + "' width='" + PB_DIVIDER_WIDTH + "' height='" + PB_DIVIDER_HEIGHT + "' border='0' alt='Divider'/></td>\n";
		}
		strHTML += "<td width='" + PB_ICON_WIDTH + "' height='" + PB_ICON_HEIGHT + "'><img src='" + PB_FOLDER_DIR;
		strHTML += i + "_";
		strHTML += (i == intCurrentScreen) ? "on" : "of" ;
		strHTML += ".png' width='" + PB_ICON_WIDTH + "' height='" + PB_ICON_HEIGHT + "' border='0' alt='Step " + i + "' name='pb" + i + "'/></td>\n";
	}
	strHTML += "</tr></table>\n<!-- end progressbar -->\n";
	//wioDebug(strHTML);
	document.write(strHTML);	
}

// AJAX functionalities
//code called after confirmation screen PHP has run
function ajaxDisplayConfirmation(){
	wioDebug(xmlHttp.ResponseText);
	objXMLdoc = xmlHttp.responseXML;
	if (brw.ie){
		//Internet explorer
		if (objXMLdoc.parseError.errorCode != 0) {
			var myErr = objXMLdoc.parseError;
			wioDebug("You have error [" + myErr.reason + "]");
			return;
		} else {
			root = objXMLdoc.documentElement;
			//wioDebug("Node parsed [" + (root.parsed ? "true" : "false") + "]");			
		}
	}else{
		//Firefox does not support parseError
		wioDebug("node name: " + objXMLdoc.documentElement.nodeName);
		if (objXMLdoc.documentElement.nodeName=="parsererror"){
			errStr = objXMLdoc.documentElement.childNodes[0].nodeValue;
			errStr= errStr.replace(/</g, "&lt;");
			wioDebug("NS encountered an error [" + errStr + "]");
			return;
		}else{
			wioDebug("NS Parsed successfully");
		}
	}
	if (brw.ns){
		document.getElementById("wrapperconfirmationoutput").innerHTML = js_StripSlashes(objXMLdoc.getElementsByTagName("confirmedHTML")[0].childNodes[0].nodeValue);
		document.getElementById("qryInsertClient").innerHTML = objXMLdoc.getElementsByTagName("qryInsertClient")[0].childNodes[0].nodeValue;
		document.getElementById("qryInsertBMI").innerHTML = objXMLdoc.getElementsByTagName("qryInsertBMI")[0].childNodes[0].nodeValue;
		document.getElementById("qryInsertMotivation").innerHTML = objXMLdoc.getElementsByTagName("qryInsertMotivation")[0].childNodes[0].nodeValue;
		document.getElementById("qryInsertLifestyle").innerHTML = objXMLdoc.getElementsByTagName("qryInsertLifestyle")[0].childNodes[0].nodeValue;
		document.getElementById("qryInsertEatingHabits").innerHTML = objXMLdoc.getElementsByTagName("qryInsertEatingHabits")[0].childNodes[0].nodeValue;
		document.getElementById("qryInsertContactInfo").innerHTML = objXMLdoc.getElementsByTagName("qryInsertContactInfo")[0].childNodes[0].nodeValue;
		document.getElementById("qryInsertPersonalMessage").innerHTML = objXMLdoc.getElementsByTagName("qryInsertPersonalMessage")[0].childNodes[0].nodeValue;
		document.getElementById("qryInsertHowDoYouKnow").innerHTML = objXMLdoc.getElementsByTagName("qryInsertHowDoYouKnow")[0].childNodes[0].nodeValue;
	}else{
		document.getElementById("wrapperconfirmationoutput").innerHTML = js_StripSlashes(objXMLdoc.getElementsByTagName("confirmedHTML")[0].nodeTypedValue);
		document.getElementById("qryInsertClient").innerHTML = objXMLdoc.getElementsByTagName("qryInsertClient")[0].nodeTypedValue;
		document.getElementById("qryInsertBMI").innerHTML = objXMLdoc.getElementsByTagName("qryInsertBMI")[0].nodeTypedValue;
		document.getElementById("qryInsertMotivation").innerHTML = objXMLdoc.getElementsByTagName("qryInsertMotivation")[0].nodeTypedValue;
		document.getElementById("qryInsertLifestyle").innerHTML = objXMLdoc.getElementsByTagName("qryInsertLifestyle")[0].nodeTypedValue;
		document.getElementById("qryInsertEatingHabits").innerHTML = objXMLdoc.getElementsByTagName("qryInsertEatingHabits")[0].nodeTypedValue;
		document.getElementById("qryInsertContactInfo").innerHTML = objXMLdoc.getElementsByTagName("qryInsertContactInfo")[0].nodeTypedValue;
		document.getElementById("qryInsertPersonalMessage").innerHTML = objXMLdoc.getElementsByTagName("qryInsertPersonalMessage")[0].nodeTypedValue;
		document.getElementById("qryInsertHowDoYouKnow").innerHTML = objXMLdoc.getElementsByTagName("qryInsertHowDoYouKnow")[0].nodeTypedValue;
	}
}

//code run after checknewuser.php has run
function ajaxCheckNewUser(){	
	var strResult = xmlHttp.responseText;
	//wioDebug("Result from new user [" + strResult + "]");
	intResult = parseInt(strResult);
	if (isNaN(intResult)){
		showError(strGlobalError);
		return;
	}
	if (intResult == 1){
		//All is well
		blnAjaxHasChecked = true;
		goToNextScreen();
	}
	if(intResult < 0){
		//Error occurred
		showError(strGlobalError);
		return;
	}
	if (intResult == 0){
		//User exists alreay
		showError(strPreExistingUser);
		return;
	}
}

//code run to display result screen
function ajaxResultScreen(){
	document.getElementById("wrapperresultoutput").innerHTML = xmlHttp.responseText;
}

/* shows error Message in overlayer which can be hidden  - Requires pre-existing DIV with id "errorpane" in page*/
function showError(strErrorMessage){
	//Transform strErrorMessage from text to HTML

	divErrorPane = document.getElementById("errorpane");
	divErrorPane.innerHTML = "<a class='erorrpaneclose' href='javascript:hideError();' onClick='hideError();return false;'>x</a>" + strErrorMessage;
	divErrorPane.style.display = "block";
}

/* hides error pane, identified by id "errorpane" */
function hideError(){
	divErrorPane = document.getElementById("errorpane");
	divErrorPane.innerHTML = "";
	divErrorPane.style.display = "none";
}

function js_StripSlashes(strText){
	return strText.replace(/\\'/g,"'");
}
