var MCB_PRS_STATE_LOADING						=	0;
var MCB_PRS_STATE_NONE							=	1;
var MCB_PRS_STATE_IN_PROGRESS					=	2;
var MCB_PRS_STATE_IN_PROGRESS_REDIRECT			=	3;
var MCB_PRS_STATE_COMPLETE						=	100;
var MCB_PRS_STATE_FAILURE						=	1001;

glob_prs_step										= -1;

function showPRSStep(step){
	/*
	Loop through each div who's ID starts with 'sms_step_', and show only the one specified in step, eg: sms_step_1
	*/

	if ( glob_prs_step == step ){
		return true;
	} else {
		glob_prs_step = step
	}

	steps = document.getElementsByTagName('div');
	c 		= steps.length
	for(i=0; i < c ; i++){
		t = steps[i]
		if ( t.id.substr(0,9) == 'prs_step_'){
			/* debug(t.id); */
			if ( t.id.substr(9) == step){
				t.style.display = 'block';
				glob_sms_step = step;
			} else {
				t.style.display = 'none';
			}
		}
	}
}

function processPRSState(state){

	if ( document.getElementById("simulate_dropdown") ) {
		/**
		If the user is running in test mode, there will be a html select called 'simulate_dropdown', listing all the states. Select the correct one:
		*/
		o = document.getElementById('simulate_dropdown').options;
		for ( i=0 ; i< o.length ; i++ ) {
			if ( o[i].value == state ) {
				o[i].selected = true;
				break;
			}
		}
	}
	
	glob_state = state;
	
	switch(state){
		case MCB_PRS_STATE_IN_PROGRESS_REDIRECT: /* For payment zones that have early redirects */
		case MCB_PRS_STATE_COMPLETE:
		clearInterval(glob_prs_check_interval);
		showPRSStep(state);
/*		glob_state = MCB_PRS_STATE_COMPLETE;*/
		/*glob_redirect_enabled = 1;*/
		if ( ! glob_simulated ) {
			setTimeout("redirectOnComplete()", 10000);
		}
		break;

		case MCB_PRS_STATE_FAILURE:
		case MCB_PRS_STATE_LOADING:
		case MCB_PRS_STATE_NONE:
		case MCB_PRS_STATE_IN_PROGRESS:
		clearInterval(glob_prs_check_interval);
		showPRSStep(state);
		glob_prs_check_interval = setInterval('pollPRS()', 10000);
		break;
	}
}

function pollPRS(){

	state = __getUrl("xmlrpc_prs_new_poll?prsid=" + glob_prsid + "&site=" + glob_siteid);
	state = parseInt(state);
	if ( state == 0 ) {
		return false;
	}
	processPRSState(state);
}


if ( glob_prsid > 0 ){
	var glob_prs_check_interval = setInterval('pollPRS()', 1000);
}


