﻿
var siteKey = "10003";
var authenticationKey = "Nzg1MzAwLDEwNTA1NzY2LDEwNTE4MDEw";
var retUrl = "http://"+location.hostname+"/popup/UccUploadComplete.aspx";

/* Test */
//var uploadUrl = "http://123.212.193.85/partner/upload.htm";
/* Real Server */
var uploadUrl = "http://www.mgoon.com/partner/upload.htm";

function UccUoload(titlectl, contentctl) 
{
    document.charset = "utf-8";

    var postTitle = titlectl.value;
    var postContent = contentctl.document.body.innerHTML;
    postTitle = GetHtmlCodeReplaceHtmlTag(postTitle);
    postContent = GetHtmlTagRemoval(postContent);
    
	if (!postTitle || !trim(postTitle)) {
		alert('제목을 입력해주세요');
		titlectl.focus();
		return;
	}

	if(!CheckNullChar(postTitle))
	{
	  alert('제목에 입력할수 없는 문자가 있습니다.');
	  titlectl.focus();
		return;
	}
	
	if (!postContent) {
		alert("내용을 넣어주세요!");
		contentctl.document.body.focus();
		return;
	}
	
	var uccForm = CreateUccForm();	
    uccForm.title.value = postTitle;
    uccForm.desc.value = postContent;
    uccForm.siteCode.value = siteKey;
    uccForm.pKey.value = authenticationKey;
    uccForm.rtnURL.value = retUrl;

    var popWindow = window.open('about:blank','UccUploadWindow', "width=500,height=315,scroll=no"); 
    uccForm.target = 'UccUploadWindow';
    uccForm.action = uploadUrl;
    uccForm.method = 'post';
    uccForm.submit();
}

//폼 생성
function CreateUccForm()
{
    var uccform = document.getElementById("fmUcc");
    if(uccform == null)
    {
        var tmpform = document.createElement("form");
        tmpform.setAttribute("id", "fmUcc");
        tmpform.setAttribute("name", "fmUcc");
        tmpform.setAttribute("style", "display:none;");
        
        var tmptitle = document.createElement("input");
        tmptitle.setAttribute("type", "hidden");
        tmptitle.setAttribute("name", "title");
        tmptitle.setAttribute("id", "title");
        tmpform.appendChild(tmptitle);

        var tmpsiteCode = document.createElement("input");
        tmpsiteCode.setAttribute("type", "hidden");
        tmpsiteCode.setAttribute("name", "siteCode");
        tmpsiteCode.setAttribute("id", "siteCode");
        tmpform.appendChild(tmpsiteCode);

        var tmppKey = document.createElement("input");
        tmppKey.setAttribute("type", "hidden");
        tmppKey.setAttribute("name", "pKey");
        tmppKey.setAttribute("id", "pKey");
        tmpform.appendChild(tmppKey);
        
        var tmpdesc = document.createElement("input");
        tmpdesc.setAttribute("type", "hidden");
        tmpdesc.setAttribute("name", "desc");
        tmpdesc.setAttribute("id", "desc");
        tmpform.appendChild(tmpdesc);

        var tmprtnURL = document.createElement("input");
        tmprtnURL.setAttribute("type", "hidden");
        tmprtnURL.setAttribute("name", "rtnURL");
        tmprtnURL.setAttribute("id", "rtnURL");
        tmpform.appendChild(tmprtnURL);

        document.body.appendChild(tmpform);
        uccform = tmpform;
    }
    return uccform;
}

//html 태그들 모두 제거
function GetHtmlTagRemoval(text)
{
    var rtag = new RegExp("<[^>]*>", "g");
    var rtab = new RegExp("\\t", "g");
    var rspace = new RegExp("\\s{2,}", "g");
    var rcr = new RegExp("&nbsp;", "g");
    var tmp = text.replace(rtag, "");
    
    tmp = tmp.replace(rtab, " ");
    tmp = tmp.replace(rspace, " ");
    tmp = tmp.replace(rcr, "");
    
    return tmp;
}

//html 특수문자로 변환
function GetHtmlCodeReplaceHtmlTag(text)
{
    var rtagstart = new RegExp("<", "g");
    var rtagend = new RegExp(">", "g");  
    var tmp = text.replace(rtagstart, "&lt;");
    
    tmp = tmp.replace(rtagend, "&gt;");
    
    return tmp;
}

// 동영상 올리기
function SetUCC(fileName, videoId, thumbNail, encodingName) {
	var contents = GetMgoon(videoId);
    
    AddConent(contents, "bottom");
	AddFileList(fileName + " 0KB", encodingName + "|gpt|0|gpt|" + videoId + "|gpt|" + thumbNail);
}

// 엠군 태그 반환
function GetMgoon(videoID)
{
    return "<EMBED style=\"WIDTH: 500px; HEIGHT: 435px\" name=mgoon src=http://play.mgoon.com/Video/V" + videoID + " type=application/x-shockwave-flash>";
}

function WriteMgoonTag(videoID, width, height)
{
    var resourceUrl = 'http://play.mgoon.com/Video/V'+ videoID +'/';
  
    document.write("<object id='V"+ videoID +"' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,18,0' width='" + width + "px' height='" + height + "px' align='middle'>");
	document.write("<param name='allowScriptAccess' value='always' />");
	document.write("<param name='allowFullScreen' value='true' />");
	document.write("<param name='movie' value='"+ resourceUrl +"' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='bgcolor' value='#ffffff' />");
	document.write("<embed src='"+ resourceUrl +"' quality='high' bgcolor='#000000' width='" + width + "px' height='" + height + "px' name='V"+ videoID +"' align='middle' allowScriptAccess='always' allowFullScreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
	document.write("</object>");
}

