var glob_simulated	=	false;
var glob_state 		= -1;



function simulate_grant(){
    r = __getUrl("xmlrpc_test_grant?siteid=" + glob_siteid + "&method=grant");
    if ( r == "OK") {
	alert("Grant request sent.");
    } else if ( r == "NOTEST") {
	alert("Testing is disabled on this payment zone");
    } else {
	alert("There was an error, please try again");
    }
}

function simulate_mo(){
    r = __getUrl("xmlrpc_test_grant?siteid=" + glob_siteid + "&method=mo&smsid=" + glob_smsid);
    if ( r == "OK") {
	alert("Incoming message simulated");
    } else if ( r == "NOTEST") {
	alert("Testing is disabled on this payment zone");
    } else {
	alert("There was an error, please try again");
    }
}

function simulate_revoke(){
    r = __getUrl("xmlrpc_test_grant?siteid=" + glob_siteid + "&method=revoke");
    if ( r == "OK") {
	alert("Revoke request sent.");
    } else if ( r == "NOTEST") {
	alert("Testing is disabled on this payment zone");
    } else {
	alert("There was an error, please try again");
    }
}

function simulate(i){
    i = parseInt(i);

    if ( i == 2001 ) {
	simulate_grant();
	simulate(MCB_SMS_STATE_COMPLETE);
	return;
    }

    if ( i == 2002 ) {
	simulate_revoke();
	clearInterval(glob_prs_check_interval);
	glob_prs_check_interval = setInterval('pollPRS()', 1000);
	processSMSState(MCB_SMS_STATE_LOADING);
	return;
    }

    if ( i == 2003 ) {
	simulate_mo();
	clearInterval(glob_sms_check_interval);
	glob_sms_check_interval = setInterval('pollSMS()', 1000);
	processSMSState(MCB_SMS_STATE_LOADING);
	return;
    }
	
    if ( glob_smsid > 0 ){
	processSMSState(i);
	if ( i == MCB_SMS_STATE_LOADING ) {
	    clearInterval(glob_sms_check_interval);
	    glob_sms_check_interval = setInterval('pollSMS()', 1000);
	} else {
	    glob_simulated = true;
	    clearInterval(glob_sms_check_interval);
	}
    } else if (glob_prsid > 0 ){
	processPRSState(i);
	if ( i == MCB_PRS_STATE_LOADING ) {
	    clearInterval(glob_prs_check_interval);
	    glob_prs_check_interval = setInterval('pollPRS()', 1000);
	} else {
	    glob_simulated = true;
	    clearInterval(glob_prs_check_interval);
	}
    }
}


function debug(s){
    if (! console ) {
	return false;
    }
    try {
	console.log(s);
    } catch(e) {
    }
}


function __getUrl(url) {
    var xmlrpc_obj
    try {
	xmlrpc_obj = new XMLHttpRequest;
    } catch(e) {
	try {
	    xmlrpc_obj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
	    try {
		xmlrpc_obj = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch(e) {
		alert("Your web browser does not support this feature. Please upgrade your web browser.");
	    }
	}
    }

    xmlrpc_obj.open("GET", url, false);
    xmlrpc_obj.send(null);
    if (xmlrpc_obj.status == 200)   {
	return xmlrpc_obj.responseText;
    } else {
	return false;
    }
}







function redirectOnComplete(){
    /*
	MCB_SMS_STATE_COMPLETE is the same as MCB_PRS_STATE_COMPLETE ( 100 )
	
	If this function is changed, test in all browsers to make sure it doesn't cause a permission error because of cross-site-scripting
	*/
    if ( glob_redirect_enabled == 0 ) {
	return ;
    }
	
    if ( MCB_SMS_STATE_COMPLETE == glob_state || MCB_PRS_STATE_IN_PROGRESS_REDIRECT == glob_state || MCB_SMS_STATE_EARLY_REDIRECT == glob_state || MCB_SMS_STATE_DEFERRED_MESSAGES == glob_state) {
	try {
	    window.parent.location.href = glob_redirect_on_complete
	} catch (e){
	    document.location.href = glob_redirect_on_complete;
	}
    }
}



function confirmDoubleOptIn(){
    accepted = document.getElementById("sms_double_optin_checkbox").checked;

    if ( accepted != true ) {
	alert("You must accept the terms and conditions before pressing continue");
	return false;
    }
    processSMSState(MCB_SMS_STATE_LOADING);

    r = __getUrl("xmlrpc_sms_double_optin?smsid=" + glob_smsid + "&site=" + glob_siteid + "&accepted=1");
    if ( r == "OK") {
	return true;
    } else if ( r == "UNACCEPTED" ) {
	alert("You must accept the terms and conditions before pressing continue");
    } else if ( r == "NO_MO" ) {
	document.getElementById("sms_double_optin_checkbox").checked = false;
	alert("You must send the text message as per the instructions before agreeing to the terms and conditions");
    } else {
	alert("There was an error processing your request. Please try again in a moment.");
    }
    processSMSState(MCB_SMS_STATE_NONE);
    return true;
}

function enterMSISDN(){
    var msisdn = document.getElementById('enter_msisdn_msisdn').value;
    r = __getUrl("xmlrpc_sms_actions?act=submit_msisdn&msisdn="+msisdn+"&smsid=" + glob_smsid + "&site=" + glob_siteid + "&accepted=1");
    if ( r == "OK") {
	processSMSState(MCB_SMS_STATE_ENTER_PIN);
	return true;
    } else if ( r == "FAIL" ) {
	alert("Please check your telephone number and try again");
    } else {
	alert("There was an error processing your request. Please try again in a moment.");
    }
}

function enterPIN(){
    var code = document.getElementById('enter_pin_pin').value;
    r = __getUrl("xmlrpc_sms_actions?act=enter_pin&code="+code+"&smsid=" + glob_smsid + "&site=" + glob_siteid + "&accepted=1");
    if ( r == "OK") {
	processSMSState(MCB_SMS_STATE_COMPLETE) ;
	return true;
    } else if ( r == "FAIL" ) {
	alert("Please check the PIN and try again");
    } else {
	alert("There was an error processing your request. Please try again in a moment.");
    }
}






