﻿var xmlRequest;
var objGood;

function getXMLHTTPRequest() {
    var xRequest = null;
    if (window.XMLHttpRequest) {
        xRequest = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        xRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xRequest;
}

function ProcessGoodResponse() {
    if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {
            //요청한값 확인하기
            var rst = xmlRequest.responseText;
						
            if (rst == "0") {
                document.getElementById(objGood).innerHTML = Number(document.getElementById(objGood).innerHTML) + 1;
                alert('인정하였습니다.');
            } else {
                alert(rst);
            }
        }
        else {
            alert("Error while retrieving data!");
        }
    }
}


function saveCommunityGood(type, idx, obj) {	
	if (getCookie("UserId").length < 1) {
        location.href = "/user/login/login.aspx?return=" + escape(window.location.href.replace(getDomain(), ""));
        return;
    }
    
    objGood = obj;
    //요청 URL + 입력텍스트 값
    var myRandom = parseInt(Math.random()*99999999); // or myRandom = new Date().getTime();

    var Url = "/community/community_good.aspx?type=" + type  + "&idx=" + idx + "&rand=" + myRandom;
    xmlRequest = getXMLHTTPRequest();
    if (xmlRequest != null) {
        xmlRequest.onreadystatechange = ProcessGoodResponse;
        xmlRequest.open("GET", Url, true);
        xmlRequest.send(null);
    }
}

function getDomain() {
    var dns, arrDns, str; 
    dns = document.location.href; //<-- 현재 URL 얻어온다
    arrDns = dns.split("//"); //<-- // 구분자로 짤라와서
    str = arrDns[0]+"//"+arrDns[1].substring(0,arrDns[1].indexOf("/")); //<-- 뒤에부터 다음 / 까지 가져온다 
    return str;
}
