// COPYRIGHT
// ~~~~~~~~~
// Original idea of Orfo system belongs to Dmitry Koteroff.
// If you want to modify this script, please communicate 
// with author first: http://forum.dklab.ru/other/orphus/.
//
// You may use this script "as is" with no restrictions.
// Please do not remove these comments.
//
var orfo = new Object;

// Следующие параметры можно изменять вручную.
orfo.img        = "http://molbiol.ru";
orfo.img_x      = "55";
orfo.img_y      = "13";

orfo.bgcolor    = 0; // цвет, пока картинка не догрузилась (0 - прозрачный)

// Следующие параметры можно не изменять.
orfo.hq         = ""; 
orfo.contlen    = 10;
orfo.contunit   = "word";
orfo.seltag1    = "-->>";
orfo.seltag2    = "<<--";
orfo.seltag3    = "<font color=#FF0000><em>";
orfo.seltag4    = "</em></font>";
orfo.language = "ru";

if (orfo.language == "ru") {
orfo.alt        = "Выделите орфографическую ошибку мышью и нажмите Ctrl+Enter.\nСделаем язык чище!";
orfo.badbrowser = "Не выделен текст с ошибкой или ваш браузер не поддерживает перехват выделенного текста. Возможно, слишком стара\я верси\я, а возможно, еще кака\я-нибудь ошибка.";
orfo.toobig     = "Вы выбрали слишком большой объем текста!";
orfo.subject    = "Орфографическая ошибка";
orfo.docmsg     = "Документ:";
orfo.intextmsg  = "Орфографическая ошибка в тексте:";
orfo.ifsendmsg  = "Послать сообщение об ошибке?\nВаш браузер останется на той же странице.";
orfo.author     = "Система Orfo || Автор: Дмитрий Котеров || http://orphus.dklab.ru";
send_it         = "Отправить!";
do_not_send     = "Не надо";
height_orpho     = 300;
author_orpho     = "Гость";
} else {
orfo.alt        = "Select spelling error with mouse and press Ctrl+Enter.\nLet's make the language pure!";
orfo.badbrowser = "Either you did not select the text with mistake, or your browser dows not support selection handling of IFRAMEs.";
orfo.toobig     = "You have selected too large text block!";
orfo.subject    = "Spelling error";
orfo.docmsg     = "Document:";
orfo.intextmsg  = "Spelling error in text:";
orfo.ifsendmsg  = "Do you want to send a notice to webmaster?\nYour browser will NOT be redirected.";
orfo.author     = "Orfo system || Dmitry Koteroff || http://orphus.dklab.ru";
send_it         = "Send it!";
do_not_send     = "Cancel";
height_orpho     = 500;
author_orpho     = "Guest";
}


if(navigator.appName.indexOf("Netscape")!=-1 && eval(navigator.appVersion.substring(0,1))<5) {
	} else {
document.writeln('<img src="'+orfo.img+'/izo/orpho.gif" alt="'+orfo.alt+'" title="'+orfo.alt+'" width="'+orfo.img_x+'" height="'+orfo.img_y+'" border=0 onClick="return orfo_imgclick()">');
}

function BODY_onkeypress(e)
{	var pressed=0;
	if(!orfo.ready) return;
	
	var we=null;
	if(window.event) we=window.event;
	else if(parent && parent.event) we=parent.event;

	if(we) {
		// IE
		pressed=we.keyCode==10;
	} else if(e) {
		// NN
		pressed = 
			(e.which==10 && e.modifiers==2) || // NN4
			(e.keyCode==0 && e.charCode==106 && e.ctrlKey) ||
			(e.keyCode==13 && e.ctrlKey) // Mozilla
	}
	if(pressed) orfo_do();
}

function orfo_strip_tags(text) {
	for(var s=0; s<text.length; s++) {
		if(text.charAt(s)=='<') {
			var e=text.indexOf('>',s); if(e<=0 || e==false) continue;	
			text=text.substring(0,s)+text.substring(e+1); s--;
		}
	}
	return text;
}

function orfo_strip_slashn(text) {
	for(var s=0; s<text.length; s++) {
		if(text.charAt(s)=='\n' || text.charAt(s)=='\r') {
			text=text.substring(0,s)+" "+text.substring(s+1);
			s--;
		}
	}
	return text;
}

function getCookie(name) {
	var cname = name + "=";
	var dc = document.cookie;

	if (dc.length > 0) {
		begin = dc.indexOf(cname);
		if (begin != -1) {
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1)  {
				end = dc.length;
			} // end if
			return (dc.substring(begin, end));
		} // end if
	} // end if
	return null;
}

var user_cookie = getCookie('member_name');
if(''==user_cookie || null==user_cookie) { 
} else {
author_orpho=user_cookie;
}

function orfo_do() {
	var text=null, context=null;
	if(navigator.appName.indexOf("Netscape")!=-1 && eval(navigator.appVersion.substring(0,1))<5) {
		alert(orfo.badbrowser);
		return;
	}

	var w = parent? parent : window;

	var selection = null;
	if(w.getSelection) {
		context=text=w.getSelection();
	} else if(w.document.getSelection) {
		context=text=orfo_strip_tags(w.document.getSelection());
	} else {
		selection = w.document.selection;
	}
	if(selection) {
		var sel = text = selection.createRange().text;
		var s=0; while(text.charAt(s)==" " || text.charAt(s)=="\n") s++;
		var e=0; while(text.charAt(text.length-e-1)==" " || text.charAt(text.length-e-1)=="\n") e++;
		var rngA=selection.createRange();
		rngA.moveStart(orfo.contunit,-orfo.contlen);
		rngA.moveEnd("character",-text.length+s);
		var rngB=selection.createRange();
		rngB.moveEnd(orfo.contunit,orfo.contlen);
		rngB.moveStart("character",text.length-e);
		text    = text.substring(s,text.length-e);
		context = rngA.text+orfo.seltag1+text+orfo.seltag2+rngB.text;
		context_show = rngA.text+orfo.seltag3+text+orfo.seltag4+rngB.text;
	}
	if(text=="") { alert(orfo.badbrowser); return; }
	if(context.length>1024) {
		alert(orfo.toobig);
		return;
	}
	var url = w.document.location;


// открываем окно 
id = window.open('','orpho', config='height='+height_orpho+',width=500,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');
id.document.write("<head><title>&nbsp;&nbsp;&nbsp;"+orfo.subject +"&nbsp;&nbsp;&nbsp;</title></head><BODY BGCOLOR='white'>");
id.focus();
id.document.write("<br>--"+author_orpho+"--<br>"+orfo.intextmsg+"<br><font color=#0000FF><hr noshade>"+orfo_strip_slashn(context_show)+"<hr noshade></font><br>");
id.document.write("<form name='orpho' id='orpho' action='"+orfo.img+"/orpho.php' method=post>");
id.document.write("<input type='hidden' name='orpho_lng' value='"+orfo.language+"' />");
id.document.write("<input type='hidden' name='text' value='"+text+"' />");
id.document.write("<input type='hidden' name='author_orpho' value='"+author_orpho+"' />");
id.document.write("<input type='hidden' name='url' value='"+url+"' />");
id.document.write("<input type='hidden' name='context' value='"+context+"' />");
if(orfo.language=="en") id.document.write("Correct variant:<br><textarea cols='50' rows='10' name='altern'>"+context+"</textarea><br>");
id.document.write(orfo.ifsendmsg+"<br><div align='center'><input type='submit' value='"+send_it+"'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='"+do_not_send+"' onClick='self.close()'>");
id.document.write("</form></div>");

// закрываем окно 
id.document.write("</body></html>");
id.document.close();
}

function orfo_imgclick() {
	return alert(orfo.author+"\n\n"+orfo.alt+"\n");
}

orfo.ready = true;
document.onkeypress = BODY_onkeypress;
if(parent) parent.document.onkeypress = BODY_onkeypress;

