/* クリックカウントのプログラムにリクエストを送る関数
 *  
 * JSONscriptRequestはパラメータでコールバックする関数
 * を指定しなければならないので、必ずリクエストURLにパラメータ
 * をつけなければいけない。
 * 
 */
function clickCount(from, DEST, id, link, target) {
    var cache = (new Date).getTime();
    var url = '/img/cl.PNG';
    url += '?from=' + from + '&id=' + id + '&cache=' + cache + '&DEST=' + DEST;
    var cache = (new Date).getTime();

	var oJsr = new JSONscriptRequest(url);
    // 要素を作成
	oJsr.buildScriptTag();
    // scriptタグを追加
	oJsr.addScriptTag();
        //var desturl = decodeURIComponent(DEST);
        var desturl = DEST;
	if (target == '_blank'){
      window.open(desturl);
	}
	else{
      link.href = desturl;
      return true;
    }
    link.href = "javascript:void(0);";
    return false;
}


/* 以下JSONP */

function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    //this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.noCacheIE = '';
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "EUC-JP"); // 日本語を扱う場合、ここを適切な文字コードに変更
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

/**
 * GETメソッドでリクエストを送信する
 * @param from fromパラメータの内容
 * @param DEST DESTパラメータの内容
 * @param id   idパラメータの内容
 **/
function sendGetRequest(from, DEST, id) {
    var url = "/img/cl.PNG?from=" + from + "&DEST=" + DEST + "&id=" + id;
    var request = createHttpRequest();
    request.open("GET", url, true);
    request.send("");
}

/**
 * ブラウザに合わせたHTTPRequestを作成する
 **/
function createHttpRequest() {
    // IE以外
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        try {
             return new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e) {
             try {
                  new ActiveXObject("Microsoft.XMLHTTP")
             } catch (e2) {
                  return null
             }
        }
    } else {
        return null
    }
}

