﻿//var urlWs = "http://m.medatual.com.br/ws/";
var urlWs = "http://localhost:49916/ws/";

function HTMLObjSet(eObj) {
    
    var rObj = false;
    
    if(document.all) {
        if(document.all[eObj]) {
            rObj = document.all[eObj];
        }
    } else {
        if(document[eObj]) {
            rObj = document[eObj];
        } else {
            if(document.getElementById(eObj)) {
                rObj = document.getElementById(eObj);
            }
        }
    }
    
    return rObj;
}

function ws(url) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status != 404) {
                return eval("(" + xhr.responseText + ")");
            } else {
                window.alert("erro no acesso ao servidor (404)");
            }
        }
    }
    xhr.send(null);
}

function postwith(to, p) {
    alert('post to' + to);
    var myForm = document.createElement("form");
    myForm.method = "post";
    myForm.action = to;
    for (var k in p) {
        var myInput = document.createElement("input");
        myInput.setAttribute("name", k);
        myInput.setAttribute("value", p[k]);
        myForm.appendChild(myInput);
    }
    document.body.appendChild(myForm);
    myForm.submit();
    document.body.removeChild(myForm);
}

function onBtnAcessar() {
    if (HTMLObjSet('cmpLogin').value != '' && HTMLObjSet('cmpSenha').value != '') {
        var url = urlWs + 'GetClientByLogin.aspx?&ac=' + HTMLObjSet('cmpLogin').value + '&as=' + HTMLObjSet('cmpSenha').value;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status != 404) {
                    data = eval("(" + xhr.responseText + ")");
                    if (data != undefined) {
                        if (data.rows[0] != undefined) {
                            alert(data);
                            if (data.rows[0].alunoId != 0) {
                                alert(data.key);
                            } else {
                                window.alert("login/senha incorreto(s)");
                            }
                        }
                        else {
                            window.alert("data.rows[0] undefined");
                        }
                    }
                    else {
                        window.alert("data undefined");
                    }
                }
                else {
                    window.alert("erro no acesso ao servidor (404)");
                }
            }
        }
        xhr.send(null);
    }
    else {
        window.alert("login/senha inválido(s)");
    }
}
