//create ajax request object
var request = null;
function createRequest() {
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	if (request == null)
		alert("Error creating request object!");
}
//end ajax request object function
function passwordHelp(loginName) {
	createRequest();
	var url = "/help/fp.aspx?login=" + encodeURI(loginName);
	//post the data via ajax
	request.open("POST", url, true);
	//use the function updateProcess to refresh the content of the main div with returned page data
	request.onreadystatechange = updatePasswordHelp;
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(url);
}
//this is the update function, all data passed back through this function will display in the modal password help window
function updatePasswordHelp() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var formData = request.responseText;
			document.getElementById("pHelp").innerHTML = formData;
		}
		else {
		alert("Error! Request status is " + request.status);
		}
	}
	else {
	document.getElementById("pHelp").innerHTML = "<p align='center'><img src='/images/loading.gif' align='center' alt='loading'></p>";
	}
}
//ajax registration step 1, post email address and "step" via querystring
function postStepOne() {
	//create ajax request
    createRequest();
    //get the value of the email textbox
    var txtEmailAddress = document.getElementById("txtEmailAddress").value;
	var url = "/rg/jsStep1.aspx?step=1&email=" + encodeURI(txtEmailAddress);
	//post the data via ajax
	request.open("POST", url, true);
	//use the function updateProcess to refresh the content of the main div with returned page data
	request.onreadystatechange = updateProcess;
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(url);
}
//ajax registration step 2, post 1st name, last name, password and "step" via querystring
function postStepTwo() {
	//create ajax request
    createRequest();
    //get textbox values
    var txtFirstName = document.getElementById("txtFirstName").value;
    var txtLastName = document.getElementById("txtLastName").value;
    var txtPassword = document.getElementById("txtCreatePassword").value;
    var txtConfirm = document.getElementById("txtConfirmPassword").value;
    var txtCompany = document.getElementById("txtCompanyName").value;
    
	var url = "/rg/jsStep1.aspx?step=2&fname=" + encodeURI(txtFirstName) + "&lname=" + encodeURI(txtLastName) + "&pword=" + encodeURI(txtPassword) + "&company=" + encodeURI(txtCompany) + "&confirm=" + encodeURI(txtConfirm);
	//post the data
	request.open("POST", url, true);
	//update the page with responseText
	request.onreadystatechange = updateProcess;
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(url);
}
//ajax registration step 3, post requested newsletters - selected via checkboxes, also the "step"
function postStepThree() {
	//create ajax request
    createRequest();
    //dim 3 variables and set them to the selected value of each checkbox on the page
    var intDaily;
    var intWeekly;
    var intResearch;
    if (document.getElementById("chkDaily").checked == true) {
		intDaily = 1;
    } else {
		intDaily = 0;
	}
    if (document.getElementById("chkWeekly").checked == true) {
		intWeekly = 1;
    } else {
		intWeekly = 0;
	}
	if (document.getElementById("chkResearch").checked == true) {
		intResearch = 1;
    } else {
		intResearch = 0;
	}
	var url = "/rg/jsStep1.aspx?step=3&daily=" + intDaily + "&weekly=" + intWeekly + "&research=" + intResearch;
	//post the data
	request.open("POST", url, true);
	//update the page with final step - confirmation and login. congratulations, we're all done!
	request.onreadystatechange = updateProcess;
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(url);
}
//if the user exists on our system, we need to post to a new page [via ajax for a change!]
function postKnownUserStepOne() {
	//create ajax request
    createRequest();
    var txtLoginName = document.getElementById("txtAJAXLoginName").value;
    var txtPassword = document.getElementById("txtAJAXPassword").value;
	var url = "/rg/jsStep1Recognised.aspx?step=1&login=" + encodeURI(txtLoginName) + "&pword=" + encodeURI(txtPassword);
	request.open("POST", url, true);
	request.onreadystatechange = updateProcess;
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(url);
}
function postKnownUserStepTwo() {
	//create ajax request
    createRequest();
    //get textbox values
    var txtFirstName = document.getElementById("txtFirstName").value;
    var txtLastName = document.getElementById("txtLastName").value;
	var url = "/rg/jsStep1Recognised.aspx?step=2&fname=" + encodeURI(txtFirstName) + "&lname=" + encodeURI(txtLastName);
	//post the data
	request.open("POST", url, true);
	//update the page with responseText
	request.onreadystatechange = updateProcess;
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(url);
}
//ajax registration step 3, post requested newsletters - selected via checkboxes, also the "step"
function postKnownUserStepThree() {
	//create ajax request
    createRequest();
    //dim 3 variables and set them to the selected value of each checkbox on the page
    var intDaily;
    var intWeekly;
    var intResearch;
    if (document.getElementById("chkDaily").checked == true) {
		intDaily = 1;
    } else {
		intDaily = 0;
	}
    if (document.getElementById("chkWeekly").checked == true) {
		intWeekly = 1;
    } else {
		intWeekly = 0;
	}
	if (document.getElementById("chkResearch").checked == true) {
		intResearch = 1;
    } else {
		intResearch = 0;
	}
	var url = "/rg/jsStep1Recognised.aspx?step=3&daily=" + intDaily + "&weekly=" + intWeekly + "&research=" + intResearch;
	//post the data
	request.open("POST", url, true);
	//update the page with final step - confirmation and login. congratulations, we're all done!
	request.onreadystatechange = updateProcess;
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(url);
}
//this is the update function, all data passed back through this function will display in the modal window
function updateProcess() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var formData = request.responseText;
			document.getElementById("display_content").innerHTML = formData;
		}
		else {
		alert("Error! Request status is " + request.status);
		}
	}
	else {
	document.getElementById("display_content").innerHTML = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p align='center'><img src='/images/loading.gif' align='center' alt='loading'></p>";
	}
}