/**
 * JavaScript for P_BLOG
 *
 * $Id: scripts.js,v 1.2 2005/03/31 10:16:44 macha Exp $
 */


/**
 * Toggle Button DOM
 */
function toggle(targetId) {
    var target = document.getElementById(targetId);
    if (target.style.display == "block") {
        target.style.display = "none";
    } else {
        target.style.display = "block";
    }
    return false;
}


/**
 * ADMIN MODE TABLE STATUS
 */
function selectTables(check){
    var i;
    for (i = 0; i < document.forms.dbtables.tables.length; i++) {
        document.forms.dbtables.tables[i].checked = check;
    }
}



/**
 * INPUT CHECK
 *
 */
function inputCheck() {
    if (document.forms.addform.name.value == '') {
        alert('タイトルが入力されていません。');
        document.forms.addform.name.focus();
        return false;
    } else if (document.forms.addform.category.value == '') {
        alert('カテゴリーが入力されていません。');
        document.forms.addform.category.focus();
        return false;
    } else if (document.forms.addform.comment.value == '') {
        alert('コメントが入力されていません。');
        document.forms.addform.comment.focus();
        return false;
    }
    return true;
}

function inputCheck_e() {
    if (document.forms.addform.name.value == '') {
        alert('No Title');
        document.forms.addform.name.focus();
        return false;
    } else if (document.forms.addform.category.value == '') {
        alert('No Category');
        document.forms.addform.category.focus();
        return false;
    } else if (document.forms.addform.comment.value == '') {
        alert('No Comment');
        document.forms.addform.comment.focus();
        return false;
    }
    return true;
}

function inputCheckBin() {
    if (document.forms.addform.bin_title.value == '') {
        alert('タイトルが入力されていません。');
        document.forms.addform.bin_title.focus();
        return false;
    } else if (document.forms.addform.binfile.value == '') {
        alert('ファイルが選択されていません。');
        document.forms.addform.binfile.focus();
        return false;
    } else if (document.forms.addform.bin_category.value == '') {
        alert('カテゴリーが入力されていません。');
        document.forms.addform.bin_category.focus();
        return false;
    } else if (document.forms.addform.bincomment.value == '') {
        alert('コメントが入力されていません。');
        document.forms.addform.bincomment.focus();
        return false;
    }
    return true;
}

function inputCheckBin_e() {
    if (document.forms.addform.bin_title.value == '') {
        alert('No Title');
        document.forms.addform.bin_title.focus();
        return false;
    } else if (document.forms.addform.binfile.value == '') {
        alert('No File');
        document.forms.addform.binfile.focus();
        return false;
    } else if (document.forms.addform.bin_category.value == '') {
        alert('No Category');
        document.forms.addform.bin_category.focus();
        return false;
    } else if (document.forms.addform.bin_comment.value == '') {
        alert('No Comment');
        document.forms.addform.bin_comment.focus();
        return false;
    }
    return true;
}

/*Input Check for Forum*/
function ForumInputCheck() {
    if (document.forms.addform.user_name.value == '') {
        alert('名前が入力されていません。');
        document.forms.addform.user_name.focus();
        return false;
    } else if (document.forms.addform.title.value == '') {
        alert('タイトルが入力されていません。');
        document.forms.addform.title.focus();
        return false;
    } else if (document.forms.addform.comment.value == '') {
        alert('コメントが入力されていません。');
        document.forms.addform.comment.focus();
        return false;
    } else if (document.forms.addform.user_pass.value == '') {
        alert('パスワードが入力されていません。');
        document.forms.addform.user_pass.focus();
        return false;
    }
    return true;
}

function ForumReplyCheck() {
    if (document.forms.addform.user_name.value == '') {
        alert('名前が入力されていません。');
        document.forms.addform.user_name.focus();
        return false;
    } else if (document.forms.addform.comment.value == '') {
        alert('コメントが入力されていません。');
        document.forms.addform.comment.focus();
        return false;
    } else if (document.forms.addform.keystring.value == '') {
        alert('認証コードが入力されていません。');
        document.forms.addform.keystring.focus();
        return false;
    } else if (document.forms.addform.user_pass.value == '') {
        alert('パスワードが入力されていません。');
        document.forms.addform.user_pass.focus();
        return false;
    }
    return true;
}


/**
 * CONFIRM DELETION
 *
 */
function confirmDelete() {
    if (document.forms.del.id.value != '') {
        if (! confirm('削除してもよろしいですか？')) {
	    document.forms.del.id.focus();
	    return false;
	}
    }
    return true;
}

function confirmDelete_e() {
    if (document.forms.del.id.value != '') {
        if (! confirm('OK to delete this?')) {
	    document.forms.del.id.focus();
	    return false;
	}
    }
    return true;
}


/**
 * TAG BUTTON FUNCTIONS
 *
 * Implemented : by Gabriele Caniglia <www.musimac.it>
 * Modified    : by Kazufumi Tomori
 *
 */

/* --- BASE FUNCTION ---- */
function insertAtCursor(comment, myValue) {
    //IE support
    if (document.selection) {
        comment.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    } else if(comment.selectionStart || comment.selectionStart == '0') { //MOZILLA/NETSCAPE support
        var startPos = comment.selectionStart;
        var endPos = comment.selectionEnd;
        comment.value = comment.value.substring(0, startPos) + myValue + comment.value.substring(endPos, comment.value.length);
    } else {
        comment.value += myValue;
    }
}
/*
'tag_value' comes from the HTML markup (DOM API) and explains itself enough; :-)
'bin' is boolean: true for targetting the binaries form textarea, false for the logs form textarea;
*/
function Tag(tag_value, bin) {
	cmt_txt = bin ? document.forms.addform.bincomment : document.forms.addform.comment;
	var TagLookup = {
		'p'			:	'<p></p>',
		'div'		:	'<div class=""></div>',
		'span'		:	'<span class=""></span>',
		'strong'	:	'<strong></strong>',
		'img'		:	'<img src="./resources/" width="" height="" alt="" />',
		'a'			:	'<a href="" title=""></a>',
		'ul'		:	'<ul>\n<li></li>\n</ul>',
		'ol'		:	'<ol>\n<li></li>\n</ol>',
		'quote'		:	'<blockquote cite="http://" title="">\n<p></p>\n</blockquote>',
		'br'		:	'<br />',
		'abbr'      :   '<abbr title=""></abbr>',
		'tag'		:	'&lt;&gt;'
	};
    cmt_txt.focus();
    insertAtCursor(cmt_txt, TagLookup[tag_value]);
}


function wrap(elem, cls_val, attr) {
    // Class definition
    if (cls_val != '') {
        cls = ' class="' + cls_val + '"';
    } else {
        cls = '';
    }
    // Attribute
    if (attr != '') {
        attr = ' ' + attr + '=""';
    } else {
        attr = '';
    }
    // Switch by UA 
    if ((document.getSelection) && (!window.opera)) { // for Mozilla
        comment = document.getElementById('comment');
        var selLength = comment.textLength;
        var selStart  = comment.selectionStart;
        var selEnd    = comment.selectionEnd;
        if (selEnd == 1 || selEnd == 2) { selEnd = selLength; }
        var str1 = (comment.value).substring(0, selStart);
        var str2 = (comment.value).substring(selStart, selEnd);
        var str3 = (comment.value).substring(selEnd, selLength);
        comment.value = str1 + '<' + elem + cls + attr + '>' + str2 + '</' + elem + '>' + str3;
        comment.focus();
    } else if ((document.selection) && (!window.opera)) { // for WinIE
        var str = document.selection.createRange().text;
        document.getElementById('comment').focus();
        var sel = document.selection.createRange();
        sel.text = "<" + elem + cls + attr + ">" + str + "</" + elem + ">";
        return;
    } else if (window.getSelection) { // for Safari
        comment = document.getElementById('comment');
        var str = window.getSelection();
        comment.value += '<' + elem + cls + attr + '>' + str + '</' + elem + '>';
        comment.focus();
    } else {
        comment = document.getElementById('comment');
        comment.value += '<' + elem + cls + attr + '></' + elem + '>';
    }
}

function tagImg(){
	var imguri = prompt("画像のURIを入力してください", "http://");
	if (imguri == null) {
		return;
	}
	setToText("<img src=\"" + imguri + "\" />");
}

function tagAUri(){
	var uri = prompt("リンク先のURIを入力してください", "http://");
	if (uri == null) {
		return;
	}
	var text = prompt("リンクする文字列を入力して下さい", "");
	if (text == null) {
		text = "";
	}
	setToText("<a href=\"" + uri + "\">" + text + "</a>");
}

function tagAImg(){
	var imguri = prompt("画像のURIを入力してください", "http://");
	if (imguri == null) {
		return;
	}
	var uri = prompt("リンク先のURIを入力して下さい", "http://");
	if (uri == null) {
		uri = "";
	}
	setToText("<a href=\"" + uri + "\"><img src=\"" + imguri + "\" /></a>");
}

function tagBr(){
	setToText("<br />");
}

function changeFont() {
	var fsize = document.forms.addform.fSize.value;
	var fcolor = document.forms.addform.fColor.value;
	
	var startTag = "<font size=\"" + fsize + "\" color=\"" + fcolor + "\" >";
	var endTag = "</font>";

	if (document.forms.addform.fBold.checked ) {
		startTag = startTag + "<b>";
		endTag = "</b>" + endTag;
	}

	if (document.forms.addform.fItalic.checked) {
		startTag = startTag + "<i>";
		endTag = "</i>" + endTag;
	}

	var txt = prompt("文字列を入力して下さい", "");
	if (txt == null) {
		txt = "";
	}

	setToText(startTag + txt + endTag);
}

function setToText(tagStr) {
    if ((document.getSelection) && (!window.opera)) { // for Mozilla
        comment = document.getElementById('comment');
        var selLength = comment.textLength;
        var selStart  = comment.selectionStart;
        var selEnd    = comment.selectionEnd;
        if (selEnd == 1 || selEnd == 2) { selEnd = selLength; }
        var str1 = (comment.value).substring(0, selStart);
        var str2 = (comment.value).substring(selStart, selEnd);
        var str3 = (comment.value).substring(selEnd, selLength);
        comment.value = str1 + tagStr + str2 + str3;
        comment.focus();
    } else if ((document.selection) && (!window.opera)) { // for WinIE
        var str = document.selection.createRange().text;
        document.getElementById('comment').focus();
        var sel = document.selection.createRange();
        sel.text = tagStr + str ;
        return;
    } else if (window.getSelection) { // for Safari
        comment = document.getElementById('comment');
        var str = window.getSelection();
        comment.value += tagStr + str ;
        comment.focus();
    } else {
        comment = document.getElementById('comment');
        comment.value += tagStr;
    }
}

function imgSet(num) {
	var fid = document.getElementById('img' + num);
	fid.src = 'file:///' + document.getElementById('myfile' + num).value;
}

function Attach(num) {

	// Get image size
    var fid = document.getElementById('img' + num);
    var fw  = fid.width;
    var fh  = fid.height;

    if (fw == 0) { fw = ''; }
    if (fh == 0) { fh = ''; }
    
    var comment = document.getElementById('comment');
    var fv      = document.getElementById('myfile' + num).value;    
    var fp      = fv.replace(/\\/g, '/').split('/');
    var fn      = fp.length - 1;
    var f       = fp[fn];

	if ( fw ){
		var img_src = '<img src="/Cocona/resources/' + f + '" width="' + fw + '"  height="' + fh + '" alt="" />';
	} else {
		var img_src = '<img src="/Cocona/resources/' + f + '" />';
	}
	if (fv != '') { // If file value is not empty...
        if (document.getSelection) { // for Mozilla
            var selLength = comment.textLength;
            var selStart  = comment.selectionStart;
            var selEnd    = comment.selectionEnd;
            if (selEnd == 1 || selEnd == 2) { selEnd = selLength; }
            var str1 = (comment.value).substring(0, selStart);
            var str2 = (comment.value).substring(selStart, selEnd);
            var str3 = (comment.value).substring(selEnd, selLength);
            comment.value = str1 + img_src + str3;
            comment.focus();
        } else if (document.selection) { // for WinIE
            var str = document.selection.createRange().text;
            document.getElementById('comment').focus();
            var sel = document.selection.createRange();
            sel.text = img_src;
            return;
        } else if (window.getSelection) { // for Safari
            var str = window.getSelection();
            comment.value += img_src;
            comment.focus();
        } else {
            comment.value += img_src;
        }
    } else { // If file is not selected...
//      comment.value += '<img src="./resources/" width="" height="" alt="" />';
    }
}

/*
==============================================================
clickable cite attribute DOM
==============================================================
original source written by Simon Willson
: http://simon.incutio.com/
*/
function clickableCite() {
    q = document.getElementsByTagName('blockquote');
    for (i=0; i<q.length; i++) {
        cite = q[i].getAttribute('cite')
        if (cite) {
            newlink = document.createElement('a');
            newlink.setAttribute('href', cite);
            newlink.setAttribute('title', cite);
            newlink.appendChild(document.createTextNode('→ Source'));
            newdiv = document.createElement('div');
            newdiv.className = 'citesource';
            newdiv.appendChild(newlink);
            q[i].appendChild(newdiv);
        }
    }
}
window.onload = clickableCite;


/*
==============================================================
Smiley
==============================================================
*/
function smiley(icon) {
    comment = document.getElementById("comment");
	icon = ' ' + icon + ' ';
	comment.value += icon;
}



