/**
 * multi language tag check etc..
 *
 */

function checkNewsContentMultiLangTagCheck(){
 if (! checkMultiLangTag('表題', 'title')) {
  return false;
 }
 if (! checkMultiLangTag('本文', 'text')) {
  return false;
 }
 return true;
}

function checkMultiLangTag(checkObjName, textAreaObjId){
 var textArea = document.getElementById(textAreaObjId);
 var textAreaValue = textArea.value.toLowerCase();
// alert(textAreaValue);

 if (! openedTagCheck(checkObjName, textAreaValue, 'ja', '/ja', 'en', '/en')) {
  return false;
 }
 if (! openedTagCheck(checkObjName, textAreaValue, 'en', '/en', 'ja', '/ja')) {
  return false;
 }

 if (! closedTagCheck(checkObjName, textAreaValue, 'ja', '/ja', 'en', '/en')) {
  return false;
 }
 if (! closedTagCheck(checkObjName, textAreaValue, 'en', '/en', 'ja', '/ja')) {
  return false;
 }
 
 alert(checkObjName + " multi language tag (ja,en tag) check OK!");
 
 return true;
}

function openedTagCheck(checkObjName, content, openTag, closeTag, otherOpenTag, otherCloseTag) {

 openTag = "["+openTag+"]";
 closeTag = "["+closeTag+"]";
 otherOpenTag = "["+otherOpenTag+"]";
 otherCloseTag = "["+otherCloseTag+"]";

 //alert(openTag);
 //alert(closeTag);
 //alert(otherOpenTag);
 //alert(otherCloseTag);
 
 var arrayTags = new Array();
 arrayTags = content.split(openTag);
// split(content, openTag, arrayTags);
 //ja閉じタグチェック
 // i=0は[ja]タグの左側
 for (var i=1; i<arrayTags.length; i++) {
  var checkString = arrayTags[i];
  //alert(checkString);
  var posCloseTag = checkString.indexOf(closeTag);
  if (posCloseTag == -1) {
   alert(checkObjName + "の " + i+'個目の'+openTag+'開始タグに対応する'+closeTag+'閉じタグを記入してください。');
   return false;
  }else{
   var posOtherOpenTag = checkString.indexOf(otherOpenTag);
   var posOtherCloseTag = checkString.indexOf(otherCloseTag);
   if(posOtherOpenTag != -1 && posOtherOpenTag < posCloseTag){
    alert(checkObjName + "の " + openTag+'～'+closeTag+'の間に'+otherOpenTag+'タグを記入しないでください。');
    return false;
   }else if(posOtherCloseTag != -1 && posOtherCloseTag < posCloseTag) {
    alert(checkObjName + "の " + openTag+'～'+closeTag+'の間に'+otherCloseTag+'タグを記入しないでください。');
    return false;
   }
  }
  
 }
 return true;
}


function closedTagCheck(checkObjName, content, openTag, closeTag, otherOpenTag, otherCloseTag) {

 openTag = "["+openTag+"]";
 closeTag = "["+closeTag+"]";
 otherOpenTag = "["+otherOpenTag+"]";
 otherCloseTag = "["+otherCloseTag+"]";

 var arrayTags = new Array();
 arrayTags = content.split(closeTag);
// split(content, closeTag, arrayTags);
 //開始タグチェック
 // i=0は[/ja]タグの左側
 for (var i=0; i<arrayTags.length-1; i++) {
  var checkString = arrayTags[i];
  //alert(checkString);
  var posOpenTag = checkString.indexOf(openTag);
  if (posOpenTag == -1) {
   alert(checkObjName + "の " + (i+1)+'個目の'+closeTag+'閉じタグに対応する'+openTag+'開始タグを記入してください。');
   return false;
//  }else{
//   var posOtherOpenTag = checkString.indexOf(otherOpenTag);
//   var posOtherCloseTag = checkString.indexOf(otherCloseTag);
//   if(posOtherOpenTag != -1 && posOtherOpenTag seposCloseTag){
//    alert(openTag+'～'+closeTag+'の間に'+otherOpenTag+'タグを記入しないでください。');
//    return false;
//   }else if(posOtherCloseTag != -1 && posOtherCloseTag < posCloseTag) {
//    alert(openTag+'～'+closeTag+'の間に'+otherCloseTag+'タグを記入しないでください。');
//    return false;
//   }
  }
  
 }
 return true;
}
