function createHttpRequest(){
    if(window.ActiveXObject){
        try{
            return new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                return new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e2){
                return null;
            }
        }       
    }else if(window.XMLHttpRequest){
        /*
        try{ 
            netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
        } catch (e){ 
            alert('Permission UniversalBrowserRead denied.');
        } 
        */               
        return new XMLHttpRequest();    
    }else{
        return null;    
    }    
}

var stylesheet = null;
var xslProc = null;
function getArticleHTML(xmlurl, xslurl){    
    var xmlhttp = false;
    xmlhttp = createHttpRequest();
    
    if(!xmlhttp) return "Your browser is not supported";        
    
    //document.domain = "joongangdaily.joins.com";
    
    // load XML
    xmlhttp.open("GET", xmlurl, false);
    xmlhttp.send(null);
    var xmlDoc = xmlhttp.responseXML;
    
    if(window.ActiveXObject){       
        // ¸Þ´º ¼³Á¤
        showMenu(xmlDoc.selectSingleNode('//channel/item/@code').text);                
        
        // load xsl
        xmlhttp.open("GET", xslurl, false);
        xmlhttp.send(null);
        stylesheet = xmlhttp.responseXML;
        
        var tmpHtml = xmlDoc.transformNode(stylesheet);
        document.getElementById("divArticle").innerHTML=tmpHtml;        
    }else{
        // ¸Þ´º ¼³Á¤
        var chNode = xmlDoc.getElementsByTagName("channel");
        var strCH  = chNode[0].childNodes[0].getAttribute("code");        
        showMenu(strCH);
        
        // load xsl
        if(xslProc==null){
            xslProc = new XSLTProcessor();
            xmlhttp.open("GET",xslurl,false);
            xmlhttp.send(null);
            xslProc.importStylesheet(xmlhttp.responseXML);
        }            

        var oResultFragment = xslProc.transformToFragment(xmlDoc,document);                 
        var oDiv = document.getElementById("divArticle");        
        if(oDiv.childNodes.length>0)oDiv.removeChild(oDiv.childNodes[0]);
        oDiv.appendChild(oResultFragment);     
        
        // Æ¯¼ö¹®ÀÚ Ã³¸®(firefox¶§¹®¿¡)
        tmpHtml = oDiv.innerHTML;        
        tmpHtml = replace(tmpHtml,"&lt;","<");
        tmpHtml = replace(tmpHtml,"&gt;",">");
        oDiv.innerHTML = replace(tmpHtml,"&amp;","&");                
    }   
    initFont();
 }
        

function getArticle(aid){
    aid = aid + '';
    dir1 = aid.substring(aid.length-3,aid.length-2);
    dir2 = aid.substring(aid.length-2,aid.length-1);
    dir3 = aid.substring(aid.length-1,aid.length);
    var article_xml = "http://joongangdaily.joins.com/_data/article_xml/"+ dir1+"/"+ dir2+"/"+ dir3+"/"+ aid +".xml";    	    
    var article_xsl = "http://joongangdaily.joins.com/_dev/xsl/article_body.xsl";
    
    getArticleHTML(article_xml,article_xsl);
    getComment(aid);     
    //location.replace("#");      
    window.scroll(0,0);
}


 
var comment_xslt = null;
var comment_xslProc = null; 
function getComment(aid){
    var xmlhttp = false;    
    var aid;
    
    aid = aid + '';
    dir1 = aid.substring(aid.length-3,aid.length-2);
    dir2 = aid.substring(aid.length-2,aid.length-1);
    dir3 = aid.substring(aid.length-1,aid.length);
    var default_xml = "http://joongangdaily.joins.com/_data/comment_xml/default.xml";
    var comment_xml = "http://joongangdaily.joins.com/_data/comment_xml/"+ dir1+"/"+ dir2+"/"+ dir3+"/"+ aid +".xml";    	    
    var comment_xsl = "http://joongangdaily.joins.com/_dev/xsl/comment.xsl";

    xmlhttp = createHttpRequest();    
    if(!xmlhttp) return "Your browser is not supported";        
    
    // load XML
    xmlhttp.open("GET", comment_xml, false);
    xmlhttp.send(null);
    if(xmlhttp.readyState == 4){
        if(xmlhttp.status == 200){
            var xmlDoc = xmlhttp.responseXML;
        }else{
            xmlhttp.open("GET", default_xml, false);
            xmlhttp.send(null);            
            var xmlDoc = xmlhttp.responseXML;
        }
    }else
        exit;    
    
    if(window.ActiveXObject){        
        // load xsl
        xmlhttp.open("GET", comment_xsl, false);
        xmlhttp.send(null);
        comment_xslt = xmlhttp.responseXML;
        
        var tmpHtml = xmlDoc.transformNode(comment_xslt);
        document.getElementById("divComment").innerHTML=tmpHtml;        
    }else{
        // load xsl
        if(comment_xslProc==null){
            comment_xslProc = new XSLTProcessor();
            xmlhttp.open("GET",comment_xsl,false);
            xmlhttp.send(null);            
            comment_xslProc.importStylesheet(xmlhttp.responseXML);
        }            

        var oResultFragment = comment_xslProc.transformToFragment(xmlDoc,document);                 
        var oDiv = document.getElementById("divComment");        
        if(oDiv.childNodes.length>0)oDiv.removeChild(oDiv.childNodes[0]);
        oDiv.appendChild(oResultFragment);     
        
        // Æ¯¼ö¹®ÀÚ Ã³¸®(firefox¶§¹®¿¡)
        tmpHtml = oDiv.innerHTML;        
        tmpHtml = replace(tmpHtml,"&lt;","<");
        tmpHtml = replace(tmpHtml,"&gt;",">");
        oDiv.innerHTML = replace(tmpHtml,"&amp;","&");        
    }
    
    setTimeout("setCommentInput(" + aid +")",100);
}    


function Comment_Login(){
    with(document.frmComment){
        if(txtId.value==""){
            alert("Enter UserID");    
            return false;
        }    
        if(txtPasswd.value==""){
            alert("Enter password");    
            return false;
        }    
        return true;
    }
}

function checkCommentInput(obj){
    if(obj.value.length>=350) obj.value = obj.value.substring(0,350);    
    document.getElementById("comment_len").innerHTML = "<strong>" + (350-obj.value.length) +"</strong>";    
}

// ³ªµµ ÇÑ¸¶µð ÀÔ·ÂÆû
function setCommentInput(aid){    
    
    strTemp = '';        
	strTemp += "<table width='100%' cellspacing='0' cellpadding='0' border='0'>"		
	strTemp += "<tr>"
	strTemp += "<td height='37'>"
	
	if(getCookieA("MemArray","MemID")==""){
    	strTemp += "<table width='100%' cellpadding='0' cellspacing='0' border='0'>"
    	strTemp += "<tr>"
    	strTemp += "<td class='times px15'><img src='http://images.joins.com/ui_jmn/daily07/icon_point03.gif' style='margin-right:4px;' align='absmiddle'/>My opinion</td>"
    	strTemp += "<td align='right'>ID<input type='text' name='txtId' id='' size='' class='inp_u' style='margin:0 12 0 5px; border-color:#CBCBCB; width:95px; height:19px; padding-top:2px;'/>Password<input type='password' name='txtPasswd' id='' size='' class='inp_u' style='margin:0 0 0 5px; border-color:#CBCBCB; width:95px; height:19px; padding-top:2px;'/><input type='image' src='http://images.joins.com/ui_jmn/daily07/btn_signin.gif' align='absmiddle' style='margin-left:7px'/></td>"
    	strTemp += "</tr>"
    	strTemp += "</table>"
    	strTemp += "<input type=hidden name=referer value='http://joongangdaily.joins.com/article/view.asp?aid="+ aid +"'>";

    }else{
    	strTemp += "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
    	strTemp += "<tr>";
    	strTemp += "<td class='times px15'><img src='http://images.joins.com/ui_jmn/daily07/icon_point03.gif' style='margin-right:4px;' align='absmiddle'>My opinion</td>";
    	strTemp += "<td align='right' class='dotum'><strong>"+ getCookieA("MemArray","MemName") +"</strong> ("+ getCookieA("MemArray","MemID") +")</td>";
    	strTemp += "</tr>";
    	strTemp += "</table>";
    	strTemp += "<input type=hidden name='subject' value=''>";    	
    }	
	strTemp += "</td>"
	strTemp += "</tr>"	
	if(getCookieA("MemArray","MemID")!=""){
    	strTemp += "<tr>";
    	strTemp += "<td style='padding-bottom:2px;'>";
    	strTemp += "	<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
    	strTemp += "	<tr>";
    	strTemp += "	<td><input type='checkbox' name='blog' value='Y' align='absmiddle' style='margin:0 3 0 7px;'>Add to my blog</td>";
    	strTemp += "	<td align='right'><input type='radio' name='com_icon' value='1'><img src='http://images.joins.com/ui_jmn/daily07/icon_yellow01.gif' width='17' height='16' align='absmiddle' style='margin:0 7 0 0px'><input type='radio' name='com_icon' value='2'><img src='http://images.joins.com/ui_jmn/daily07/icon_yellow02.gif' width='17' height='16' align='absmiddle' style='margin:0 7 0 0px'><input type='radio' name='com_icon' value='3'><img src='http://images.joins.com/ui_jmn/daily07/icon_yellow03.gif' width='17' height='16' align='absmiddle' style='margin:0 7 0 0px'><input type='radio' name='com_icon' value='4'><img src='http://images.joins.com/ui_jmn/daily07/icon_yellow04.gif' width='17' height='16' align='absmiddle' style='margin:0 7 0 0px'><input type='radio' name='com_icon' value='5'><img src='http://images.joins.com/ui_jmn/daily07/icon_yellow05.gif' width='17' height='16' align='absmiddle'></td>";
    	strTemp += "	</tr>";
    	strTemp += "	</table>";
    	strTemp += "</td>";
    	strTemp += "</tr>";    		
    }	
	strTemp += "<tr>"
	strTemp += "<td align='center'>";
	if(getCookieA("MemArray","MemID")=="")
	    strTemp += "<textarea name='com_content' class='inp_u' style='border-color:#CBCBCB; width:516px; height:71px; padding-top:2px; color:4d4d4d' disabled>Please log-in to post your opinion.</textarea></td>";
	else
	    strTemp += "<textarea name='com_content' wrap='hard' class='inp_u' style='border-color:#CBCBCB; width:516px; height:71px; padding-top:2px; color:4d4d4d' onKeyPress='checkCommentInput(this)'></textarea></td>";
	strTemp += "</tr>"
	strTemp += "<tr>"
	strTemp += "<td height='31'>"	
	
	strTemp += "	<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
	strTemp += "	<tr>";
	strTemp += "	<td  style='padding:2 0 0 0px;'><a href='http://bbs.joins.com/content.asp?board_idx=10&page=1&tb_name=jd_notice'><img src='http://images.joins.com/ui_jmn/daily07/btn_bulletin.gif' width='155' height='18'></a></td>";
	strTemp += "	<td style='padding:1 0 0 0px;' align='right'>Your have space [ <span class='blue' id='comment_len'><strong>0</strong></span> ] more letters<a href='JavaScript:saveComment("+ aid +")'><img src='http://images.joins.com/ui_jmn/daily07/btn_register.gif' width='49' height='19' align='absmiddle' style='margin-left:10px'></a></td>";
	strTemp += "	</tr>";
	strTemp += "	</table>";	
	
	strTemp += "</td>";
	strTemp += "</tr>";	
	strTemp += "</table>";	

	document.getElementById("comment_input").innerHTML = strTemp; 
}

function saveComment(aid){    
    with(document.frmComment){
        
        if(getCookieA("MemArray","MemID")=="") return;        
        
        // Joongangdaily ¼­ºñ½º µ¿ÀÇ È¸¿øÀÎÁö È®ÀÎ
        if(!CheckMemberReg()) return;
        
        if(com_content.value.length>=350) com_content.value = com_content.value.substring(0,350);                                
        icon_no = -1;
        for(i=0;i<com_icon.length;i++){
            if(com_icon[i].checked){
                icon_no = com_icon[i].value;    
                break;
            }             
        }
        if(icon_no==-1){
            alert("Select one of the icons");    
            return;
        }
        if(com_content.value.length==0){
            alert("Enter message");    
            com_content.focus();
            return;
        }        
        
        subject.value = document.getElementById("articleTitle").innerHTML;
        
        action = "http://joongangdaily.joins.com/comment/comment_save.asp?art_id="+ aid;
        target = "fraComment";
        submit();
        
        // ÀÎÄÚµù ¹®Á¦°¡ ÇØ°áµÇ¸é ¾Æ·¡ ¸ðµâ »ç¿ë
        /*
        var xmlhttp = false;    
        xmlhttp = createHttpRequest();    
        if(!xmlhttp) return "Your browser is not supported";        
    
        xmlhttp.open("POST","http://joongangdaily.joins.com/comment/comment_save.asp",false); // µ¿±âÈ­·Î Ã³¸®
        xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
        xmlhttp.send("mode=&art_id=" + aid + "&com_content="+ com_content.value +"&com_icon="+ icon_no);
        if(xmlhttp.readyState == 4){//COMPLETE
            if(xmlhttp.status == 200){ //200 = Á¤»óÃ³¸®
                getComment(aid); // 
            }else{
                alert(xmlhttp.responseText);
                alert("Error occured...");    
            }   
        }
        */
    }    
}


function delComment(aid,com_no){ 
    if(confirm("Are you sure?")){
        var xmlhttp = false;    
        xmlhttp = createHttpRequest();    
        if(!xmlhttp) return "Your browser is not supported";        
    
        xmlhttp.open("GET","http://joongangdaily.joins.com/comment/comment_save.asp?mode=del&art_id="+ aid +"&com_no="+ com_no,false); // µ¿±âÈ­·Î Ã³¸®
        xmlhttp.send(null);
        if(xmlhttp.readyState == 4){//COMPLETE
            if(xmlhttp.status == 200){ //200 = Á¤»óÃ³¸®
                if(xmlhttp.responseText=="INVALID"){
                    alert("Delete is denied...");
                }else
                    getComment(aid);
            }else{                
                alert("Error occured...");    
            }   
        }
    }
}





function checkImageSize(){   
    for(i=0;i<document.images.length;i++){
        if(document.images[i].width>542)
            document.images[i].width = 542;
    }
    setTimeout("checkImageSize()",500);
}
checkImageSize();
