/* グローバル変数 */
var vPath = "http://aiship.jp/o/"; //imgディレクトリまでのパス
var vTo = ""; // データ転送先ID
var vFrom = ""; // データ転送元ID

function getElementsByClass(searchClass) {
    var classElements = new Array();
    var allElements = document.getElementsByTagName("*");
    for (x = 0, y = 0; x < allElements.length; x++) {
	    if (allElements[x].className == searchClass) {
	        classElements[y] = allElements[x];
	        y++;
	    }
    }
    return classElements;
}

/* IDから編集対象のフレームを取得 */
function getRichTextElementById(o){
	var element;
	if(document.all){
		element = frames[o].document;
	}else{
		element = document.getElementById(o).contentDocument;
	}
	return element;
}

/* 指定されたIDのフレームをリッチテキスト編集対象にする */
function RTE_init(o){
//	window.alert(o);
	getRichTextElementById(o).designMode = "on";
	win = document.getElementById(o).contentWindow;
	win.focus();
}

function init(){
	RTE_init("imode");
	RTE_init("ezweb");
	RTE_init("softbank");
//	a = document.getElementById('templateKeitaiBody').value;
//	window.alert(document.getElementById('templateKeitaiBody').value);
//	window.alert(a);
//	setRichTextValue("imode",a);
	if (document.all) {
	  // IEでのonload代用処理
	  document.getElementById("imode").onreadystatechange = function () {
	    if (this.readyState == "complete") {
	      setRichTextValue("imode",document.getElementById('templateKeitaiBody').value);
	      this.onreadystatechange = null;
	    }
	  }
	  document.getElementById("ezweb").onreadystatechange = function () {
	    if (this.readyState == "complete") {
	      setRichTextValue("ezweb",document.getElementById('templateKeitaiBodyEz').value);
	      this.onreadystatechange = null;
	    }
	  }
	  document.getElementById("softbank").onreadystatechange = function () {
	    if (this.readyState == "complete") {
	      setRichTextValue("softbank",document.getElementById('templateKeitaiBodyYahoo').value);
	      this.onreadystatechange = null;
	    }
	  }
	}else{
		setRichTextValue("imode",document.getElementById('templateKeitaiBody').value);
		setRichTextValue("ezweb",document.getElementById('templateKeitaiBodyEz').value);
		setRichTextValue("softbank",document.getElementById('templateKeitaiBodyYahoo').value);
	}
//	setRichTextValue("imode",document.getElementById('templateKeitaiBody').value);
//	setRichTextValue("ezweb",document.getElementById('templateKeitaiBodyEz').value);
//	setRichTextValue("softbank",document.getElementById('templateKeitaiBodyYahoo').value);

	if(document.getElementById("newArticleWrite")){
		document.getElementById("newArticleWrite").onclick = RTEsubmit;
	}
	if(document.getElementById("selectTpl")){
		document.getElementById("selectTpl").onclick = RTEsubmit;
	}
	if(document.getElementById("uploadImg")){
		document.getElementById("uploadImg").onclick = RTEsubmit;
	}
	if(document.getElementById("mailSendTest")){
		document.getElementById("mailSendTest").onclick = RTEsubmitAlert;
	}
	if(getElementsByClass("deleteImg")){
		deleteImg = getElementsByClass("deleteImg");
		for(var i = 0;i < deleteImg.length;i++){
			deleteImg[i].onclick = RTEsubmit;
		}
	}
}

/* 指定されたIDのフレームに画像を挿入 */
function setRichImage(o,e){
//	window.alert("a");
	
	palette_left = document.getElementById('palette').offsetLeft;
	palette_top = document.getElementById('palette').offsetTop;
	
	pictchar = "pictchar-" + o;
	pictchar_left = document.getElementById(pictchar).offsetLeft;
	pictchar_top = document.getElementById(pictchar).offsetTop;
//	window.alert(palette_left + ":" + palette_top);
//	window.alert(pictchar_left + ":" + pictchar_top);
	if(document.all){
		if(document.compatMode=='CSS1Compat'){
			click_x = window.event.clientX + document.documentElement.scrollLeft;
			click_y = window.event.clientY + document.documentElement.scrollTop;
		}else{
			click_x = window.event.clientX + document.body.scrollLeft;
			click_y = window.event.clientY + document.body.scrollTop;
		}
	}else{
		click_x = e.clientX + window.pageXOffset;
		click_y = e.clientY + window.pageYOffset;
	}
	
	click_x = click_x - palette_left - pictchar_left;
	click_y = click_y - palette_top - pictchar_top;
//	window.alert(click_x + ":" + click_y);
	if(o == "imode"){
		
		unit_y = unit_x = 224 / 16;
		x = Math.floor(click_x / unit_x) -(-1); 
		y = Math.floor(click_y / unit_y);
//		window.alert(unit_x + ":" + unit_y);
//		window.alert(x + ":" + y);
		no = parseInt(y * 16) + parseInt(x);//座標から画像の番号を取得
		image = "common_img/" + o + "/" + no + ".gif";
	//	window.alert(image);
	
	}else if(o.substring(0,5) == "ezweb"){
		unit_y = unit_x = 240 / 16;
		x = Math.floor(click_x / unit_x) -(-1); 
		y = Math.floor(click_y / unit_y);
		no = parseInt(y * 16) + parseInt(x);//座標から画像の番号を取得
		if(o == "ezweb2"){
			no = no - (-334);
		}
//		window.alert(String(no).length);
		if(String(no).length == 2){
//			window.alert("a");
			no = String("0").concat(String(no));
		}else if(String(no).length == 1){
//			window.alert("b");
			no = String("00").concat(String(no));
		}
//		window.alert(no);
		image = "common_img/" + o + "/" + String(no) + ".gif";
		o = "ezweb"
//		window.alert(image);
	}else{
		unit_y = unit_x = 272 / 16;
		x = Math.floor(click_x / unit_x) -(-1); 
		y = Math.floor(click_y / unit_y);
		no = parseInt(y * 16) + parseInt(x);//座標から画像の番号を取得
		if(o == "softbank2"){
			no = no - (-270);
		}
		image = "common_img/" + o + "/" + no + ".gif";
		o = "softbank";
	}
		
	if(getRichTextElementById(o).body.innerHTML == ""){
		getRichTextElementById(o).execCommand("selectall");
	}
	win = document.getElementById(o).contentWindow;
	win.focus();
	//window.alert(o);
//	window.alert(image);
	getRichTextElementById(o).execCommand("InsertImage",false,vPath + image);
//	window.alert(getRichTextValue(o));
	closePalette();

}

/*
function setRichImage(o,image){
//	getRichTextElementById(o).body.innerHTML += "<img src=" + image.src + " />";
	if(getRichTextElementById(o).body.innerHTML == ""){
		getRichTextElementById(o).execCommand("selectall");
	}
	getRichTextElementById(o).execCommand("InsertImage",false,image.src);

	closePalette();

}
*/

function getRichTextValue(o){
	value = getRichTextElementById(o).body.innerHTML;
	return value;
}
function setRichTextValue(o,value){
	
	if(!document.all){
		getRichTextElementById(o).designMode = "off";
	}
//	window.alert(1);
	getRichTextElementById(o).body.innerHTML = value;
//	getRichTextElementById(o).write(value);

	getRichTextElementById(o).designMode = "on";

	
}
/* パレットを開く */
function openPalette(name,e){
	
	pictchar_id = "pictchar-" + name;
	
	/* 全ての絵文字を不可視にする */
	document.getElementById("pictchar-imode").style.visibility = "hidden";
	document.getElementById("pictchar-ezweb1").style.visibility = "hidden";
	document.getElementById("pictchar-ezweb2").style.visibility = "hidden";
	document.getElementById("pictchar-softbank1").style.visibility = "hidden";
	document.getElementById("pictchar-softbank2").style.visibility = "hidden";
	
	/* 表示したい絵文字を可視にする */
	document.getElementById(pictchar_id).style.visibility = "visible";
	//window.alert(document.documentElement.scrollTop);
	//window.alert(event.clientY);
	
	/* クリックされた座標を取得してパレットの表示位置を確定 */
	if(document.all){
		if(document.compatMode=='CSS1Compat'){
			document.getElementById("palette").style.top = (document.documentElement.scrollTop + event.clientY) + "px";
			document.getElementById("palette").style.left = (document.documentElement.scrollLeft + event.clientX) + "px";
		}else{
			document.getElementById("palette").style.top = (document.body.scrollTop + event.clientY) + "px";
			document.getElementById("palette").style.left = (document.body.scrollLeft + event.clientX) + "px";
		}
	}else{
		document.getElementById("palette").style.top = window.pageYOffset + e.clientY + "px";
		document.getElementById("palette").style.left = window.pageXOffset + e.clientX + "px";
	}
	document.getElementById("palette").style.display = "block";

}

function openTextPalette(e){
	
	/* クリックされた座標を取得してパレットの表示位置を確定 */
	if(document.all){
		if(document.compatMode=='CSS1Compat'){
			document.getElementById("replaceText").style.top = (document.documentElement.scrollTop + event.clientY) + "px";
			document.getElementById("replaceText").style.left = (document.documentElement.scrollLeft + event.clientX) + "px";
		}else{
			document.getElementById("replaceText").style.top = (document.body.scrollTop + event.clientY) + "px";
			document.getElementById("replaceText").style.left = (document.body.scrollLeft + event.clientX) + "px";
		}
	}else{
		document.getElementById("replaceText").style.top = window.pageYOffset + e.clientY + "px";
		document.getElementById("replaceText").style.left = window.pageXOffset + e.clientX + "px";
	}
	document.getElementById("replaceText").style.display = "block";

}

/* パレットを閉じる */
function closeTextPalette(){
	
	document.getElementById("replaceText").style.display = "none";

}

/* パレットを閉じる */
function closePalette(){
	
	document.getElementById("palette").style.display = "none";

}

function convertEmoji(from,to){
//	window.alert(getRichTextValue(from));
	data = "&value=" + encodeURIComponent(getRichTextValue(from));
	data+= "&to=" + to;
	data+= "&from=" + from;
	data+= "&action_MailDemo_Rte=true";
	vTo = to;
	vFrom = from;
//	sendRequest(recieveEmoji,data,"POST","js/rte.php",true,true);
	sendRequest(recieveEmoji,data,"POST","index.php",true,true);

}

function recieveEmoji(o){
//	window.alert(o.responseText);
	setRichTextValue(vTo,o.responseText);
}

/*
function preview(){
	document.getElementById("preview-imode").innerHTML = getRichTextValue("textfield");
	document.getElementById("preview-ezweb").innerHTML = getRichTextValue("textfield");
	document.getElementById("preview-softbank").innerHTML = getRichTextValue("textfield");
}*/

function RTEsubmit(){
	if(document.all){
		getRichTextElementById("imode").execCommand("selectall");
		getRichTextElementById("imode").execCommand("unlink");
		getRichTextElementById("ezweb").execCommand("selectall");
		getRichTextElementById("ezweb").execCommand("unlink");
		getRichTextElementById("softbank").execCommand("selectall");
		getRichTextElementById("softbank").execCommand("unlink");
	}
	document.getElementById("templateKeitaiBody").value = getRichTextValue("imode");
	document.getElementById("templateKeitaiBodyEz").value = getRichTextValue("ezweb");
	document.getElementById("templateKeitaiBodyYahoo").value = getRichTextValue("softbank");
//	window.alert(document.getElementById("action_sale_mm_newArticleWrite").value + ":" + document.getElementById("action_sale_mm_uploadImg").value + ":" + document.getElementById("action_sale_mm_deleteImg").value);
//	document.form2.submit();
}

function RTEsubmitAlert(){
	if(document.all){
		getRichTextElementById("imode").execCommand("selectall");
		getRichTextElementById("imode").execCommand("unlink");
		getRichTextElementById("ezweb").execCommand("selectall");
		getRichTextElementById("ezweb").execCommand("unlink");
		getRichTextElementById("softbank").execCommand("selectall");
		getRichTextElementById("softbank").execCommand("unlink");
	}
	document.getElementById("templateKeitaiBody").value = getRichTextValue("imode");
	document.getElementById("templateKeitaiBodyEz").value = getRichTextValue("ezweb");
	document.getElementById("templateKeitaiBodyYahoo").value = getRichTextValue("softbank");
	submit_alert();	
//	window.alert(document.getElementById("action_sale_mm_newArticleWrite").value + ":" + document.getElementById("action_sale_mm_uploadImg").value + ":" + document.getElementById("action_sale_mm_deleteImg").value);
//	document.form2.submit();
}

function submit_alert(){
window.alert("デコメ等のHTMLメールは、機種によっては表示されない\nもしくは受信できない可能性がありますのでご注意ください。\n");
}

