
/**
 * Check for Speedy usage
 */

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

var divre = username.substr(1,1);
var date1 = '';
var login_url = '';

switch(divre) {
    case '1':
        datel = username.substr(3,1);
        switch(datel){
            case '2':
            case '3':
                login_url = 'https://divre1.telkomspeedy.com/session.php';
                break;
            case '1':
            case '4':
            case '5':
            case '6':
                login_url = 'https://divre1btm.telkomspeedy.com/session.php';
                break;
            case '7':
            case '8':
                login_url = 'https://divre1plg.telkomspeedy.com/session.php';
                break;
        }
        break;
    case '2':
        datel = username.substr(6,1);
        switch(datel){
            case '0':
            case '1':
            case '7':
                login_url = 'http://divre2.telkomspeedy.com/html/index.php';
                break;
            case '2':
                login_url = 'https://203.130.233.106/session.php';
                break;
        }
        break;
    case '3':
        login_url = 'https://divre3.telkomspeedy.com/session.php';
        break;
    case '4':
        login_url = 'https://divre4.telkomspeedy.com/session.php';
        break;
    case '5':
        //login_url = 'http://divre5.telkomspeedy.com/session.php';
        login_url = 'http://www2.telkomspeedy.com/divre5/session.php';
        break;
    case '6':
        datel = username.substr(3,1);
        switch(datel){
            case '1':
            case '2':
            case '3':
                login_url = 'https://divre6.telkomspeedy.com/session.php';
                break;
            case '6':
                login_url = 'https://divre6ptk.telkomspeedy.com/session.php';
                break;
        }
        break;
    case '7':
        datel = username.substr(0,6);
        switch(datel){
            case '171901':
            case '171902':
            case '171903':
                login_url = 'http://www2.telkomspeedy.com/timika/session.php';
                break;
        }
        break;
}

if (!login_url) {
    WScript.Echo("Username yang Anda masukkan tidak valid");
    WScript.Quit();
}


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();
    }
}

var cookie = '';
var body = '';
var logged_in = false;
var speedy_url = login_url.replace(/session\.php$/, 'index.php') + '?';

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();
}

WScript.Echo(speedy_url);

if (logged_in) {
    // they redirect it
    try {
        xmlhttp.open("GET", 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();
    }
}


