/**
 * Check for Speedy usage
 */

// change the username and password
var username = 'XXXX@telkom.net';
var password = 'XXXXX';

var xmlhttp = false;

// create xmlhttp object
try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		// no xml http, bail
		WScript.Echo("Error, cannot create XMLHTTP object");
		WScript.Quit();
	}
}

// Login first
var cookie = '';
var body = '';
var logged_in = false;
var login_url = "http://divre5.telkomspeedy.com/session.php";
var speedy_url = "http://divre5.telkomspeedy.com/?";

try {
	xmlhttp.open("POST", login_url, false);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			// retrieve session cookie
			cookie = xmlhttp.getResponseHeader('set-cookie');
			//check for login status
			body = xmlhttp.responseText;
			if (body.match(/location.href='\/\?'/)) {
				logged_in = true;
			} else {
				WScript.Echo("Error, login failed!");
				WScript.Quit();
			}
		}
	}
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send('username='+username+'&password='+password);
} catch(e) {
	WScript.Echo("Error, cannot connect to " + login_url);
	WScript.Quit();
}	

if (logged_in) {
	// they redirect it
	try {
		xmlhttp.open("POST", speedy_url, false);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				cookie = xmlhttp.getResponseHeader('set-cookie');
				body = xmlhttp.responseText;
                re = new RegExp('<b>Pemakaian Speedy</b>(.*?)</TD>.*?' + 
                    '<td[^>]*>Pemakaian</td><td[^>]*>(.*?)</td>.*?' + 
                    '<td[^>]*>Pemakaian</td><td[^>]*>(.*?)</td>.*?' + 
                    '<td[^>]*>Limit</td><td[^>]*>(.*?)</td>.*?'+ 
                    '<td[^>]*>Kelebihan</td><td[^>]*>(.*?)</td>', 'gi');
                match = re.exec(body);
                if (match && match.length >= 6) {
                    str = "Pemakaian Speedy " + match[1].replace(/(<[^>]+>|&nbsp;)/gi, '') + "\n";
                    str += "Bandwidth: " + match[3] + "\n";
                    str += "Terpakai: " + match[2] + "\n";
                    str += "Limit: " + match[4] + "\n";
                    str += "Kelebihan: " + match[5] + "\n";
                    WScript.Echo(str);
                }
			}
		}
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Cookie", cookie);
		xmlhttp.send('');
	} catch(e) {
		WScript.Echo("Error, cannot connect to " + speedy_url);
		WScript.Quit();
	}
}



