var xmlhttp = null;

/* POSTによるデータ送信 */
function art(){

    /* XMLHttpRequestオブジェクト作成 */
    if (xmlhttp == null) {
        xmlhttp = createXmlHttp();
    } else {
        /* 既に作成されている場合、通信をキャンセル */
        xmlhttp.abort();
    }

      
    /* レスポンスデータ処理方法の設定 */
    xmlhttp.onreadystatechange = handleHttpEvent;

	var time = new Date();
	
	var tmp = time.getTime();
	var salt = Math.random() * (tmp - Math.floor(tmp) + 1);

	somethingth = getCookie("somethingth");
	if(somethingth == "")	somethingth = 1;
	else somethingth++;
	
    /* HTTPリクエスト実行 */
	query = "platform=" + navigator.platform
		+ "&width="+ screen.width
		+ "&height="+ screen.height
		+ "&referrer=" + document.referrer
		+ "&url=" + document.URL
		+ "&salt=" + salt
		+ "&title=" + URLencode(document.title)
		+ "&somethingth=" + somethingth;


    xmlhttp.open("POST", "/art/proxy.php");
    xmlhttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
    xmlhttp.send(query);
}

/* レスポンスデータ処理 */
function handleHttpEvent(){
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
			if(xmlhttp.responseText.match(/session=(.{32})/)){
				document.cookie = "_art_s=" + RegExp.$1 + "; path=/";
			}
			document.cookie = "somethingth=" + somethingth + "; path=/";
		}
    }
}

/* XMLHttpRequest生成 */
function createXmlHttp(){
    if (document.all) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else if (document.implementation) {
        return new XMLHttpRequest();
    } else {
        return null;
    }
}

function URLencode(str){

    var s0, i, s, u;
    s0 = "";

    for (i = 0; i < str.length; i++){
        s = str.charAt(i);
        u = str.charCodeAt(i);
        if (s == " "){s0 += "+";}
        else {
            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
                s0 = s0 + s;
            }
            else {
                if ((u >= 0x0) && (u <= 0x7f)){
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                }
                else if (u > 0x1fffff){
                    s0 += "%" + (0xf0 + ((u & 0x1c0000) >> 18)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else if (u > 0x7ff){
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else {
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}


function getCookie(key,  tmp1, tmp2, xx1, xx2, xx3) {
    tmp1 = " " + document.cookie + ";";
    xx1 = xx2 = 0;
    len = tmp1.length;
    while (xx1 < len) {
        xx2 = tmp1.indexOf(";", xx1);
        tmp2 = tmp1.substring(xx1 + 1, xx2);
        xx3 = tmp2.indexOf("=");
        if (tmp2.substring(0, xx3) == key) {
            return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
        }
        xx1 = xx2 + 1;
    }
    return("");
}

