
var btn = {
    init : function() {
        if (!document.getElementById || !document.createElement || !document.appendChild) return false;
        as = btn.getElementsByClassName('cbtn(.*)');
        for (i=0; i<as.length; i++) {
            if ( as[i].tagName == "INPUT" && ( as[i].type.toLowerCase() == "submit" || as[i].type.toLowerCase() == "button" ) ) {
                var a1 = document.createElement("a");
                a1.appendChild(document.createTextNode(as[i].value));
                a1.className = as[i].className;
                a1.id = as[i].id;
                as[i] = as[i].parentNode.replaceChild(a1, as[i]);
                as[i] = a1;
                as[i].style.cursor = "pointer";
            }
            else if (as[i].tagName == "A") {
                var tt = as[i].childNodes;
            }
            else { return false };
            var i1 = document.createElement('i');
            var i2 = document.createElement('i');
            var s1 = document.createElement('span');
            var s2 = document.createElement('span');
            s1.appendChild(i1);
            s1.appendChild(s2);
            while (as[i].firstChild) {
              s1.appendChild(as[i].firstChild);
            }
            as[i].appendChild(s1);
            as[i] = as[i].insertBefore(i2, s1);
        }
        // The following lines submits the form if the button id is "submit_btn"
		if (document.getElementById('reset_btn')) {
			btn.addEvent(document.getElementById('reset_btn'),'click',function() {
			var form = btn.findForm(this);
			form.reset();
			});
		}

        // The following lines resets the form if the button id is "reset_btn"
		if (document.getElementById('submit_btn')) {
			btn.addEvent(document.getElementById('submit_btn'),'click',function() {
			var form = btn.findForm(this);
			form.submit();
			});
		}
    },
    findForm : function(f) {
        while(f.tagName != "FORM") {
            f = f.parentNode;
        }
        return f;
    },
    addEvent : function(obj, type, fn) {
        if (obj.addEventListener) {
            obj.addEventListener(type, fn, false);
        }
        else if (obj.attachEvent) {
            obj["e"+type+fn] = fn;
            obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
            obj.attachEvent("on"+type, obj[type+fn]);
        }
    },
    getElementsByClassName : function(className, tag, elm) {
        var testClass = new RegExp("(^|\s)" + className + "(\s|$)");
        var tag = tag || "*";
        var elm = elm || document;
        var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
        var returnElements = [];
        var current;
        var length = elements.length;
        for(var i=0; i<length; i++){
            current = elements[i];
            if(testClass.test(current.className)){
                returnElements.push(current);
            }
        }
        return returnElements;
    }
}

btn.addEvent(window,'load', function() { btn.init();} );


function doedit()
{
	var obj=document.forms['doit'];
	if(obj.pass.value == "")
	{
		alert("암호를 입력하세요");
		return;
	}

	obj.action="/window/write_gasa.html";
	obj.submit();
}

function dodel()
{
	var obj=document.forms['doit'];
	if(obj.pass.value == "")
	{
		alert("암호를 입력하세요");
		return;
	}

	obj.mode.value="del";
	obj.action="/window/do.html";
	obj.submit();
}

function dosyncedit()
{
	var obj=document.forms['doit'];
	if(obj.pass.value == "")
	{
		alert("암호를 입력하세요");
		return;
	}

	obj.action="/window/sync_write.html";
	obj.submit();
}

function dosyncdel()
{
	var obj=document.forms['doit'];
	if(obj.pass.value == "")
	{
		alert("암호를 입력하세요");
		return;
	}

	obj.mode.value="syncdel";
	obj.action="/window/do.html";
	obj.submit();
}


function copy_url(no)
{
	bResult = window.clipboardData.setData("Text","http://gasazip.com/?"+no);
	if (bResult)
		alert('가사주소가 클립보드에 저장되었습니다.');
}

function copy_surl(no)
{
	bResult = window.clipboardData.setData("Text","http://gasazip.com/?s"+no);
	if (bResult)
		alert('싱크가사주소가 클립보드에 저장되었습니다.');
}


function view_othergasa(title, ititle, isinger)
{
	req = createHttpRequest() 
	req.onreadystatechange = function()
	{ 
		if( req.readyState == 4 )
		{ 
			othergasa.innerHTML = req.responseText; 
		}
	}
	req.open("GET", "/window/ajax_othergasa.html?title="+title+"&ititle="+ititle+"&isinger="+isinger);
	req.send("");
}

function view_relation(no)
{
	req = createHttpRequest() 
	req.onreadystatechange = function()
	{ 
		if( req.readyState == 4 )
		{ 
			relagasa.innerHTML = req.responseText; 
		}
	}
	timeinfo = new Date();
	req.open("GET", "/window/ajax_relation.html?no="+no+"&rnd="+timeinfo.getSeconds());
	req.send("");
}

function contents_cp()
{
	if (window.event)
	{
		window.event.returnValue = true;
		window.setTimeout('attach_gasa()', 25);
	}
}

function attach_gasa()
{
	if (window.clipboardData) // IE
	{
		var txt = window.clipboardData.getData('Text');

		txt = txt + '\r\n( 출처 : 가사집 http://gasazip.com/'+ gasa_url +' )\r\n';

		var result = window.clipboardData.setData('Text', txt);
	}
}

function contents_scp()
{
	if (window.event)
	{
		window.event.returnValue = true;
		window.setTimeout('attach_sgasa()', 25);
	}
}

function attach_sgasa()
{
	if (window.clipboardData) // IE
	{
		var txt = window.clipboardData.getData('Text');

		txt = txt + '\r\n( 출처 : 가사집 http://gasazip.com/s'+ gasa_url +' )\r\n';

		var result = window.clipboardData.setData('Text', txt);
	}
}


//오프젝트 가져오기
function getObject(objectId) {
// checkW3C DOM, then MSIE 4, then NN 4.
//
	if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId);
	}
	else if (document.all && document.all(objectId)) {
		return document.all(objectId);
	}
	else if (document.layers && document.layers[objectId]) {
		return document.layers[objectId];
	} else {
		return false;
	}
}
