document.onkeydown = cancel_tab;

/* Tab禁止 */
function cancel_tab(e) {
	if(e == undefined){
		if(event.keyCode==9){
			event.returnValue=false;
			InEmoji('\t','text');
			return false;
		}
	} else {
		if(e.which==9){
			InEmoji('\t','text');
			return false;
		}
	}
}

function WindowOpen(PageName,imgwidth,imgheight){
	if(imgwidth > screen.width*0.8) imgwidth = screen.width*0.8;
	if(imgheight > screen.height*0.8) imgheight = screen.height*0.8;
	imgwidth = imgwidth + 50;
	imgheight = imgheight + 50;
	NewWindow = window.open(PageName,'image','scrollbars=1,resizable=0,width=' + imgwidth + ',height=' + imgheight + '\'');
}

function kakunin(mode){
	if (window.confirm('エディットモードを切り替えてよろしいですか？')){
		$('richtextflag').value = 1;
		if(mode=='rich'){
			$('edit_mode').value = 'rich';
		}else{
			$('edit_mode').value = '';
 			$('tore1').value = getIFrameDocument('editorWindow1').body.innerHTML;
		}
		
		document.t1.submit();
		return false;
	}else{
		return false;
	}
}

function colwin(colid,a){
	var win_name = 'colortext'+a;
	document.getElementById(win_name).value=colid;
}
	
function showlayer(a,num){
	//document.all('editorWindow'+num).contentWindow.blur();
	document.getElementById(a).style.visibility = 'visible';
}
		
function hiddenlayer(a){
	document.getElementById(a).style.visibility = 'hidden';
}
		
function pos_text_set(a,b){
	if (document.getElementById('editorWindow'+a).contentDocument){  
		document.getElementById('editorWindow'+a).contentDocument.write(b);
		document.getElementById('editorWindow'+a).contentDocument.close();
	} else {
		document.frames['editorWindow'+a].document.write(b);
		document.frames['editorWindow'+a].document.close();
	}
}
		
function load(ab){
	var win_name = 'editorWindow'+ab;
	getIFrameDocument(win_name).designMode = 'On';       
	//frames[0].document.designMode = 'On';
}
    
function getIFrameDocument(aID){
	if (document.getElementById(aID).contentDocument){  
		return document.getElementById(aID).contentDocument;
	} else {
		return document.frames[aID].document;
	}
}

function doRichEditCommand(aName,num, aArg){
	hiddenlayer('colorbox'+num);
	//document.all('editorWindow'+num).contentWindow.focus();
	getIFrameDocument('editorWindow'+num).execCommand(aName,false, aArg);
	document.all('editorWindow'+num).contentWindow.focus();
	//document.getElementById('editorWindow'+num).focus();
	write();
} 
        
function addLink(){
	var myUrl = prompt('Please enter a URL:', '');
	getIFrameDocument('editorWindow').execCommand('createLink', false, myUrl);
}    

function addimage(num){
	var myUrl = prompt('画像のURLを入力してください:', 'http://');
	if(myUrl){
		getIFrameDocument('editorWindow'+num).execCommand('insertimage', false, myUrl);
	}
}    

// view soruce
    
function getSource(num){
	//document.getElementById('sourceView\").innerHTML = replace(getIFrameDocument('editorWindow').body.innerHTML,'<','&lt;');
	//document.t1.tore1.value = (replace(getIFrameDocument('editorWindow1').body.innerHTML,'<','&lt;'));
	for(i=1;i<num+1;i++){
		document.getElementById('tore'+i).value = getIFrameDocument('editorWindow'+i).body.innerHTML;
	}

	return true;
}
        
    function replace(string,text,by) {
      var stringLength = string.length
      var textLength = text.length;
      if ((stringLength == 0) || (textLength == 0)) return string;

      var i = string.indexOf(text);
      if ((!i) && (text != string.substring(0,textLength))) return string;
      if (i == -1) return string;

      var newstr = string.substring(0,i) + by;

      if ( (i+textLength) < stringLength)
        newstr += replace(string.substring(i+textLength,stringLength),text,by);
      return newstr;
    }       

function timestamp(val,image_size_big){
	var date = new Date();
	var timestamp = date.getTime();
	ajaxReq("image.php","image.php?image=" + val + "&cont=5&status=ajax&t=" + timestamp,image_size_big);
}

// 絵文字挿入

function fade2(){

	$("popwindow").style.top = sy+'px';
	$("popwindow").style.left = sx+'px';
	$("popwindow").style.display = 'block';
}

function colwin(colid,a){
	var win_name = 'colortext'+a;
	document.getElementById(win_name).value=colid;
	$('csample').style.color = colid;
}

function displayXY(event){
	sx = Event.pointerX(event);
	sy = Event.pointerY(event);
}

function ajaxReq3(url,cid){

	new Ajax.Request(url,{
		method:"get",
		parameters:"cid="+cid,
		onSuccess:function(httpObj){
			$("popwindow").innerHTML = decodeURIComponent(httpObj.responseText);
		},
		onFailure:function(httpObj){
			$("popwindow").innerHTML = "エラーで読み込めませんでした";
		},
		onComplete:fade2()
	});
	
	
}

function doRichEditCommand1(a){

	InEmoji(a+",",'text')
	$("popwindow").style.display = 'none';
	$("popwindow").focus();

}

function doRichEditCommand2(a){

	InEmoji(a,'text')
	$("popwindow").style.display = 'none';
	$("popwindow").focus();

}

function doRichEditCommand3(a){

	InEmoji(a,'text')

}

function closePalette(){
	$("popwindow").style.display = 'none';
	$("popwindow").focus('');
}

function InEmoji(InTexts,elements){
	var Txtarea = $(elements);
	InTexts = '' + InTexts + '';
	if(navigator.appName.indexOf("Microsoft") > -1){
			Txtarea.focus();
		if (!document.selection) return;
				var range = document.selection.createRange();
				range.text=InTexts;
				Txtarea.focus();
		} else {
			if (Txtarea.createTextRange && Txtarea.caretPos) {
			var caretPos = Txtarea.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? InTexts + '' : InTexts;
			Txtarea.focus();
		} else {
			var length = Txtarea.textLength;
			var start = Txtarea.selectionStart;
			var end = Txtarea.selectionEnd;
			var Txtarea1 = Txtarea.value.substring(0, start);
			var Txtarea2 = Txtarea.value.substr(end, length);
			Txtarea.value = Txtarea1 + InTexts + Txtarea2;
			Txtarea.focus();
		}
	}
}

function popwin(val){
	var date = new Date();
	var timestamp = date.getTime();
	ajaxReq3("../../bbs-system/manage2/colorPalette_user.php",0);
}

function popwinTags(val){
	var date = new Date();
	var timestamp = date.getTime();
	ajaxReq3("../../bbs-system/manage2/htmlTags.php",0);
}

function popwinEmoji(val){
	var date = new Date();
	var timestamp = date.getTime();
	ajaxReq3("../../bbs-system/manage2/emojiPalette.php",0);
}
