var nextid;
var modclass = 'Start';
var userId;
var objectid;
var conn = new Ext.data.Connection();
// center
function validateToken() {
	conn.request({
		url : processstepperURL + 'checkUserLogin.do',
		method : 'GET',
		params : getFormContent(document.getElementById('validation')),
		success : function(responseObject) {
			userId = responseObject.responseText;
			if ('' != userId) {
				// user is valid, a token was received and the assessment can
				// start!
				Ext.Msg.alert('Validation successful', 'The provided user credentials are valid!')
				startbutton.enable();
				questionnairePanel.doLayout();
			} else {
				// show message that the user details are not valid
				Ext.Msg.alert('Validation Exception', 'The provided user credentials are not valid. Please try again!')
			}
		}
	});
}

function validateTokenAnonymous() {
	var d = new Date();
	var Min = d.getMinutes();
	var Hours = d.getHours();
	var MinOutput = ((Min < 10) ? "0" + Min : Min);
	var HoursOutput = ((Hours < 10) ? "0" + Hours : Hours);
	userId = d.getFullYear() + '' + d.getMonth() + 1 + '' + d.getDate() + '-' + HoursOutput + '' + MinOutput;
	// document.getElementById('token').value;
	startbutton.enable();
	questionnairePanel.doLayout();
}

Ext.onReady(function() {

	startbutton.on('click', function() {
		startQuestionnaire();
	});
});

function startQuestionnaire() {

	// hide start and enable all other navigationoptions
	loadingMask.show();
	startbutton.hide();
	prevbutton.show();
	nextbutton.show();
	// finishbutton.show();
	navigationPanel.hide();
	questionnairePanel.doLayout();

	conn.request({
		url : processstepperURL + 'create.do',
		params : 'processId=' + modelid + '&uniqueId=' + uniqueId + '&processName=' + processName + '&userId=' + userId + '&type=' + type + '&walkthrough=' + isWalkthrough,
		method : 'GET',
		success : function(responseObject) {
			var temp = new Array();
			temp = responseObject.responseText.split('@');
			userId = temp[0];
			contentHtml = temp[1];
			currentPage = temp[2];
			allPages = temp[3];
			pageLabel.setText('Page ' + currentPage + '  of ' + allPages);

			// enable and disable buttons
			prevbutton.disable();
			nextbutton.disable();
			if (currentPage != 1) {
				nextbutton.show();
				finishbutton.hide();
				nextbutton.enable();
				prevbutton.enable();
			}

			if (currentPage < allPages) {
				nextbutton.show();
				finishbutton.hide();
				nextbutton.enable();
			}
			if (currentPage == allPages) {
				nextbutton.hide();
				finishbutton.show();
				prevbutton.enable();
			}
			questionnaireContent.body.update('<form id=\"questionPage\">' + contentHtml + '</form>');
			nextbutton.enable();
			loadingMask.hide();
			document.location.href = window.location.href.split('#')[0] + '#create';

		},
		failure : function(responseObject) {
			loadingMask.hide();
			Ext.Msg.alert('Exception', 'An exception occured while loading the questionnaire - please check the entered address and retry.')
		}

	});
}