/**
 * JavaScript for Loggix
 *
 * @since   5.6.7
 * @version 9.2.9
 */


/**
 * Initialize
 */
function init() {
    var target1 = document.getElementById('user-name');
    var target2 = document.getElementById('title');
    if (target1) {
        target1.focus();
    } else if (target2) {
        target2.focus();
    }
}



/**
 * Toggle Button DOM
 */

// New way
function toggleElement(id) {
	var element = document.getElementById(id);
	element.style.display = (element.style.display != 'none' ? 'none' : 'block' );
	return false;
}

// Old way
/*
function toggleElement(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 Value Check
 *
 */
function checkInputValue(xmlLanguage) {
    var xmlLanguage;
    var message1;
    var message2;
    var message3;

    if (xmlLanguage == 'ja') {
        message1 = 'タイトルが入力されていません。';
        message2 = 'カテゴリーが入力されていません。';
        message3 = 'コメントが入力されていません。';
    } else {
        message1 = 'No Title.';
        message2 = 'No Category.';
        message3 = 'No Comment.';
    }

    if (document.getElementById('title').value == '') {
        alert(message1);
        document.getElementById('title').focus();
        return false;
    } else if (document.getElementById('category').value == '') {
        alert(message2);
        document.getElementById('category').focus();
        return false;
    } else if (document.getElementById('comment').value == '') {
        alert(message3);
        document.getElementById('comment').focus();
        return false;
    }
    return true;
}

function checkCommentInputValue(alert_lang) {
    var alert_lang;
    var message1;
    var message2;
    var message3;

    if (alert_lang == 'japanese') {
        message1 = '名前が入力されていません。';
        message2 = 'タイトルが入力されていません。';
        message3 = 'コメントが入力されていません。';
        message4 = 'パスワードが入力されていません。';
    } else {
        message1 = 'Please Enter Your Name.';
        message2 = 'Please Write Your Comment Title.';
        message3 = 'Please Write Something.';
        message4 = 'Please Set Your Password.';
    }
    if (document.getElementById('user_name').value == '') {
        alert(message1);
        document.getElementById('user_name').focus();
        return false;
    } else if (document.getElementById('comment-title').value == '') {
        alert(message2);
        document.getElementById('comment-title').focus();
        return false;
    } else if (document.getElementById('comment').value == '') {
        alert(message3);
        document.getElementById('comment').focus();
        return false;
    } else if (document.getElementById('user_pass').value == '') {
        alert(messsage4);
        document.getElementById('user_pass').focus();
        return false;
    }
    return true;
}

function checkContactInputValue(alert_lang) {
    var alert_lang;
    var message1;
    var message2;
    var message3;

    if (alert_lang == 'japanese') {
        message1 = 'お名前が入力されていません。';
        message2 = 'E-Mailが入力されていません。';
        message3 = 'お問い合わせ内容が入力されていません。';
    } else {
        message1 = 'Please Enter Your Name.';
        message2 = 'Please Enter Your E-Mail.';
        message3 = 'Please Write Something.';
    }
    if (document.getElementById('contact_name').value == '') {
        alert(message1);
        document.getElementById('contact_name').focus();
        return false;
    } else if (document.getElementById('contact_email').value == '') {
        alert(message2);
        document.getElementById('contact_email').focus();
        return false;
    } else if (document.getElementById('contact_comment').value == '') {
        alert(message3);
        document.getElementById('contact_comment').focus();
        return false;
    }
    return true;
}
/**
 * TAG BUTTON FUNCTIONS
 *
 * @author Gabriele Caniglia <www.musimac.it>
 * @author kaz
 */
/* --- 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;
    }
}


function setFile(num, xmlLanguage) {
    var targetFile   = document.getElementById('img' + num);
    var targetButton = document.getElementById('button' + num);
    var fileValue    = document.getElementById('myfile' + num).value;
    var filePointer  = fileValue.replace(/\\/g, '/').split('/');
    var fileNumber   = filePointer.length - 1;
    var file         = filePointer[fileNumber];

    if (file.match(/.jpg/i) || file.match(/.png/i) || file.match(/.gif/i)) {
        targetFile.src     = 'file:///' + fileValue;
        targetButton.value = (xmlLanguage == 'ja') ? '画像タグを挿入↑' : 'Insert Image Tag↑';
        document.getElementById('thumb-size' + num).style.display = 'block';
        document.getElementById('thumb_width' + num).value = document.getElementById('upload_thumbimg_max_width').value;
        document.getElementById('thumb_title' + num).value = document.getElementById('title').value;
        document.getElementsByName('thumb_float')[0].checked = true;
    } else if (file.match(/.mp3/i)) {
        targetFile.src     = '../theme/css/_shared/mp3.png';
        targetButton.value = (xmlLanguage == 'ja') ? 'ポッドキャストタグを挿入↑' : 'Insert Podcast Tag↑';
        document.getElementById('thumb-size' + num).style.display = 'none';
    } else if ((file.match(/.m4/i)) || file.match(/.mp4/i)) {
        targetFile.src     = '../theme/css/_shared/m4.png';
        targetButton.value = (xmlLanguage == 'ja') ? 'ポッドキャストタグを挿入↑' : 'Insert Podcast Tag↑';
        document.getElementById('thumb-size' + num).style.display = 'none';
    } else if (file.match(/.mov/i)) {
        targetFile.src     = '../theme/css/_shared/mov.png';
        targetButton.value = (xmlLanguage == 'ja') ? 'ポッドキャストタグを挿入↑' : 'Insert Podcast Tag↑';
        document.getElementById('thumb-size' + num).style.display = 'none';
    } else if (file.match(/.wav/i)) {
        targetFile.src     = '../theme/css/_shared/wav.png';
        targetButton.value = (xmlLanguage == 'ja') ? 'ポッドキャストタグを挿入↑' : 'Insert Podcast Tag↑';
        document.getElementById('thumb-size' + num).style.display = 'none';
    } else if (file.match(/.pdf/i)) {
        targetFile.src     = '../theme/css/_shared/pdf.png';
        targetButton.value = (xmlLanguage == 'ja') ? 'PDFへのリンクを挿入↑' : 'Insert a link to PDF↑';
        document.getElementById('thumb-size' + num).style.display = 'none';
    } else {
        targetFile.src     = '../theme/css/_shared/file-icon-large.png';
        targetButton.value = (xmlLanguage == 'ja') ? 'ファイルへのリンクを挿入↑' : 'Insert File Tag↑';
        document.getElementById('thumb-size' + num).style.display = 'none';
    }
}

function Attach(num) {

    // Get image size
    var targetFile = document.getElementById('img' + num);
    var fileWidth  = targetFile.width;
    var fileHeight = targetFile.height;

    if (fileWidth  == 0) { fileWidth  = ''; }
    if (fileHeight == 0) { fileHeight = ''; }

    var comment     = document.getElementById('comment');
    var fileValue   = document.getElementById('myfile' + num).value;
    var filePointer = fileValue.replace(/\\/g, '/').split('/');
    var fileNumber  = filePointer.length - 1;
    var file        = filePointer[fileNumber];
    var title       = document.getElementById('title').value;
    var thumbTitle  = document.getElementById('thumb_title' + num).value;
    if (thumbTitle != '') { title = thumbTitle; }
    if (title == '') { title = file; }
    var attachCode  = '<img src="./data/resources/'
                    + file + '" alt="' + title + '" />';
//    var attachCode  = '<img src="./data/resources/'
//                    + file + '" width="' + fileWidth + '" height="' + fileHeight
//                    + '" alt="" />';

    if (file.match(/.jpg/i) || file.match(/.png/i) || file.match(/.gif/i)) {
        var thumbMaxWidth  = parseInt(document.getElementById('upload_thumbimg_max_width').value);
        var thumbMaxHeight = parseInt(document.getElementById('upload_thumbimg_max_height').value);
        var thumbWidth     = parseInt(document.getElementById('thumb_width' + num).value);
        var thumbHeight    = parseInt(document.getElementById('thumb_height' + num).value);
        if (isNaN(thumbWidth)) {
            thumbWidth = '';
        }
        if (isNaN(thumbHeight)) {
            thumbHeight = '';
        }
        if (thumbWidth != '' && (thumbWidth < 1 || thumbMaxWidth < thumbWidth)) {
            thumbWidth = thumbMaxWidth;
        }
        if (thumbHeight != '' && (thumbHeight < 1 || thumbMaxHeight < thumbHeight)) {
            thumbHeight = thumbMaxHeight;
        }
        var thumbFloat = document.getElementsByName('thumb_float' + num);
        var thumbFloatType = '';
        for (var i = 0; i < thumbFloat.length; i++) {
            if (thumbFloat[i].checked) {
                thumbFloatType = thumbFloat[i].value;
                break;
            }
        }
        var thumbStyle = '';
        switch (thumbFloatType) {
          case 'left':
          case 'right':
            thumbStyle = ' style="float: ' + thumbFloatType + ';"';
            break;
          default:
            thumbStyle = '';
            break;
        }
        attachCode  = '<img src="./data/resources/' + file
                    + '" width="' + thumbWidth + '" height="' + thumbHeight
                    + '" alt="' + title + '"' + thumbStyle + ' />';
//        attachCode  = '<img src="./data/resources/'
//                    + file + '" width="' + fileWidth + '" height="' + fileHeight
//                    + '" alt="' + file + '" />';
    } else if (file.match(/.mp3/i) ||
               file.match(/.m4/i)  ||
               file.match(/.m4v/i) ||
               file.match(/.mp4/i) ||
               file.match(/.mov/i) ||
               file.match(/.wav/i)) {
        attachCode = '<!-- PODCAST=' + file + ' -->';
    } else {
        attachCode = '<a href="./data/resources/' + file + '">' + file + '</a>';
    }

    if (fileValue != '') { // If file value is not empty...
        // for Mozilla and Safari 1.3 or greater
        if ((comment.selectionStart) && (!window.opera)) {
            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 + attachCode + 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 = attachCode;
            return;
        } else if (window.getSelection) { // for Old Safari
            var str = window.getSelection();
            comment.value += attachCode;
            comment.focus();
        } else {
            comment.value += attachCode;
        }
    } else { // If file is not selected...
        var defaultImageTag = '<img src="./data/resources/" alt="" />';
        insertAtCursor(comment, defaultImageTag);
    }
}


/*
'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) {
    comment = document.getElementById("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>',
		'li' 		:   '<li></li>',
		'quote'		:	'<blockquote cite="http://" title="">\n<p></p>\n</blockquote>',
		'br'		:	'<br />',
		'clear'		:	'<br class="clear" />',
		'code'      :   '<code></code>',
		'abbr'      :   '<abbr title=""></abbr>',
		'tag'		:	'&lt;&gt;'
	};
    comment.focus();
    insertAtCursor(comment, TagLookup[tag_value]);
}

/**
 * Wrap selected text with XHTML Tag
 *
 * @author kaz
 * @author Hiro
 */
function wrapText(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
    comment = document.getElementById('comment');
    if ((comment.selectionStart) && (!window.opera)) { // for Mozilla and Safari 1.3 or grater (by Hiro)
        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;
        comment.focus();
        var sel = document.selection.createRange();
        sel.text = "<" + elem + cls + attr + ">" + str + "</" + elem + ">";
        return;
    } else if (window.getSelection) { // for old Safari
        var str = window.getSelection();
        comment.value += '<' + elem + cls + attr + '>' + str + '</' + elem + '>';
        comment.focus();
    } else {
        comment.value += '<' + elem + cls + attr + '></' + elem + '>';
    }
}



/**
 * Insert Smiley Icon Code
 *
 * @author kaz
 * @author Hiro
 */
function smiley(icon) {
    comment = document.getElementById("comment");
    icon = ' ' + icon + ' ';
    insertAtCursor(comment, icon);
    return false;
}

