OpenVMS Source Code Demos

AJAX_VDSL_PORT_MTCE.JS

//===========================================================
// title    : ajax_vdsl_port_mtce_102.js
// author   : Neil Rieck
// created  : 2009.02.12
// last edit: 2010.04.23
// caveat   : this experimental code requires more work like:
//		1) not sending passwords in the clear
//		2) add code to arm/cancel the timer
//===========================================================

var state = 0;
var timeOutId = 0;
var response = null;
var user = "";
var pass = "";
var port = "";		// this is really "xsam:port"
var rules = "";
var status1 = "";
var status2 = "";
var debug = "";
var lc_pattern = /[a-z]/;

function ajax_event_handler() {
    document.getElementById("dbg1").innerHTML = "ready state: "+ response.readyState;
    if (response.readyState == 4) {
//	alert("handler-a: ready state: "+ response.readyState +" handler: status: "+ response.status);
        if (response.status == 200)
        {   var resp$ = response.responseText;
//	    alert("response:"+ resp$);
	    var xxx;
	    xxx = resp$.indexOf("-i-");			// cuz we first send back "-i-success" or "-e-error"
	    if (xxx == 0) {
		document.getElementById("msg").value = "Done";
		var new_data;
		var new_class = status2.toLowerCase();	// portstatus and class currently mean the same thing
		if (lc_pattern.test(status1)){
			new_data = status2.toLowerCase();
		}
		else
		{	new_data = status2.toUpperCase();
		}
		document.getElementById(port).innerHTML=new_data;
		document.getElementById(port).className=new_class;
	    } else {
		document.getElementById("msg").value = resp$
	    }
	    document.getElementById("dbg2").innerHTML = resp$;
        } else
        {
//	    alert("handler-b: ready state: "+ response.readyState +" handler: status: "+ response.status);
            document.getElementById("dbg1").innerHTML = "HTML error: "+ response.status;
	    document.getElementById("dbg2").innerHTML = "";
        }
	state = 0;					// reset
	if (timeOutId) clearTimeout(timeOutId);
    }
}

function start_ajax(msg) {
    response = null;
    if (typeof XMLHttpRequest == "undefined")
        XMLHttpRequest = function(){
            try { return new ActiveXObject("Msxml2.XMLHTTP.6.0")} catch (e) {}
            try { return new ActiveXObject("Msxml2.XMLHTTP.3.0")} catch (e) {}
            try { return new ActiveXObject("Msxml2.XMLHTTP")    } catch (e) {}
            try { return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) {}
            throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.")
        }
    if (msg != "") {
        response = new XMLHttpRequest();
        if (response != null) {
	    response.onreadystatechange = ajax_event_handler;	// fire this function after receive
	    response.open("GET", msg, true);			// async=true (don't wait)
	    response.send(null);				//
	    init_timer();					//
	}
    }
}

function timer_job() {
    var previous;
    previous = state;						// copy state
    state = 0;							// init
    alert ("oops, the timer expired at state: "+ previous);	// display copied state
}

function init_timer() {
	if (timeOutId) clearTimeout(timeOutId);			// cancel previous
	timeOutId = setTimeout("timer_job();", 10000);		// 10 seconds
}

function init_ajax() {
    user    = document.getElementById("user").value;		// read INPUT objects
    pass    = document.getElementById("pass").value;		//
    port    = document.getElementById("port").value;		//
    rules   = document.getElementById("rules").value;		//
    status1 = document.getElementById("oldstatus").value;	//
    status2 = document.getElementById("newstatus").value;	//
    if ((user == "") || (pass == ""))				//
    {
	state = 0;
	document.getElementById("msg").value = "Error: blank username or password";
	return;
    }
    if ((port == "") || (status1 == ""))			//
    {
	state = 0;
	document.getElementById("msg").value = "Error: please click on the port you wish to change";
	return;
    }
    switch (state) {
        case 0:
	    document.getElementById("msg").value = "Connecting";
	    start_ajax( "/vdslscripts/vdsl_field_location_port_mtce?ajax_mode=CHANGE_PORT_STATUS&user="+ user +"&pass="+ pass +"&port="+ port +"&status1="+ status1 +"&status2="+ status2 +"&rules="+ rules);
	    state = state + 1;
	    break;
	case 1:
	    document.getElementById("msg").value = "Please wait until the current transaction finishes";
	    state = state + 1;
	    break;
	case 2:
	    document.getElementById("msg").value = "Please wait until the current transaction finishes";
	    state = state + 1;
	    break;
	default:
	    state = 0;
	    break;
    }
}

function debug_monitor() {
    debug = document.getElementById("debug").value;
//  alert("debug: "+ debug);
    switch (debug) {
	case "debug":
	case "DEBUG":
	    document.getElementById("dbg9").style.visibility="visible";
	    document.getElementById("dbg9").style.height="20px";
	    break;
	default:
	    document.getElementById("dbg9").style.visibility="hidden";
	    document.getElementById("dbg9").style.height=0;
	    break;
    }
}

function rules_monitor() {
    rules = document.getElementById("rules").value;
//  alert("rules: "+ rules);
}

Back to Home
Neil Rieck
Waterloo, Ontario, Canada.