// Sets cookie values. Expiration date is optional
//
function setCookie(name, value, expire) {
   document.cookie = name + "=" + escape(value)
   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}


function getCookie(Name) {
    var search = Name + "="
    if (document.cookie.length > 0) { // if there are any cookies
       offset = document.cookie.indexOf(search)
       if (offset != -1) { // if cookie exists
          offset += search.length
          // set index of beginning of value
          end = document.cookie.indexOf(";", offset)
          // set index of end of cookie value
          if (end == -1)
             end = document.cookie.length
          return unescape(document.cookie.substring(offset, end))
       }
    }
}



var req=null;
var console=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

function getXMLHTTPRequest() {
    var xRequest=null;


    if (window.XMLHttpRequest) {
        xRequest=new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined"){
        xRequest=new ActiveXObject("Microsoft.XMLHTTP");
    }

    return xRequest;
}

function sendRequest(url,params,HttpMethod){
    if (!HttpMethod){
        HttpMethod="POST";
    }

    var req=getXMLHTTPRequest();
    if (req){
        req.onreadystatechange=onReadyStateChange;
        req.open(HttpMethod,url,true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(params);
    }
}

function onReadyStateChange(){
    var ready=req.readyState;
    var data=null;
    if (ready==READY_STATE_COMPLETE) {
        //debug("READY_STATE_COMPLETE");
        data=req.responseText;
    } else{
        debug("loading...["+ready+"]");
    }

}


function sendEvent(eventCode, message) {
    try {
        versionId = document.getElementById("versionId");
        gameCode = document.getElementById("gameCode");

        var params = "nothing";
        sendRequest("http://sudoku.pl/event.php?eventCode="+eventCode, params, "POST");
    }
    catch(e) {
        // do nothing
    }

}
//
//
//function  aioOnReadyStateChange() {
//    info("aioOnReadyStateChange");
//}
//
//function AIO(url) {
//    this.request = getXMLHTTPRequest();
//    this.receiveFunction = null;
//    this.url = url;
//
//}
//
//AIO.prototype.send  = function() {
//    this.request.onreadystatechange=this.receiveFunction;
//    this.request.open("POST", url, true);
//    this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//    this.request.send(params);
//}
//
//AIO.prototype.setReceiveFunction  = function( f ) {
//    this.receiveFunction = f;
//
//}
//
//AIO.prototype.notify  = function() {
//
//}

