User:Eukesh/Common.js

/* */

/*

This script is a modified version of Alex benenson's cyrillic translitarator and this version was created by Tatari for Amharic which was modified by Peringz for Malayalam and transliterated by Eukesh for Devnagari.

var consonants = {"क":"क","ख":"ख","ग":"ग","घ":"घ","ङ":"ङ","च":"च","छ":"छ","ज":"ज","झ":"झ","ञ":"ञ","ट":"ट","ठ":"ठ","ड":"ड","ढ":"ढ","ण":"ण","त":"त","थ":"थ","द":"द","ध":"ध","न":"न","प":"प","फ":"फ","ब":"ब","भ":"भ","म":"म","य":"य","र":"र","ल":"ल","व":"व","श":"श","ष":"ष","स":"स","ह":"ह","क्ष":"क्ष","त्र":"त्र","ज्ञ":"ज्ञ"}; var chillaksharam = {"്\\u200D":""};

var vowels = '"्a":"","a":"ा","्i":"ि","्I":"ी","िi":"ी","ेe":"ी","्u":"ु","्U":"ू","ोo":"ू","ुu":"ू","्e":"े","i":"ै","्o":"ो","u":"ौ","्:":"ः",":":"ः","्O":"ॉ","ॉM":"ाँ","्E":"ॅ","ंM":"ँ","्R":"ृ","्q":"॑","ृu":"ृ","ृU":"ॄ","ृओo":"ृऊ","ृl":"ॢ","ॢu":"ॢ","ॢU":"ॣ","ॢओo":"ॢऊ","्M":"ं",'; var roman = '"k":"क्","क्h":"ख्","g":"ग्","ग्h":"घ्","G":"ङ्","c":"च्","च्‌h":"छ्","j":"ज्","ज्h":"झ्","Y":"ञ्","T":"ट्","ट्h":"ठ्","D":"ड्","ड्h":"ढ्","N":"ण्","t":"त्","त्h":"थ्","d":"द्","द्h":"ध्","n":"न्","p":"प्","प्h":"फ्","f":"फ्","b":"ब्","ब्h":"भ्","v":"भ्","m":"म्","y":"य्","r":"र्","l":"ल्","L":"ळ्","w":"व्","z":"श्","S":"श्","श्h":"ष्","s":"स्","h":"ह्","स्h":"श्","x":"क्ष्","त्r":"त्र्","ङ्y":"ज्ञ्",'; var chill = '"M":"ं",'; var swaram = '"a":"अ","अa":"आ","A":"आ","e":"ए","E":"ऍ","ऍe":"ऎ","i":"इ","इi":"ई","इe":"ई","अi":"ऐ","I":"ई","o":"ओ","ओo":"औ","O":"ऑ","अu":"औ","ऑM":"ॐ","u":"उ","उu":"ऊ","U":"ऊ","H":"ः","R":"ऋ","ऋI":"ॠ","ऋl":"ॡ",'; var numerals = '"1":"१","2":"२","3":"३","4":"४","5":"५","6":"६","7":"७","8":"८","9":"९","0":"०",'; var conjuncts = '"र्r":"र्‍",'; var others = '"z":"।",'; var caps = '"B":"ब्","C":"॰","F":"फ्","G":"ङ्","J":"़","K":"़","M":"ं","P":"प्","Q":"॓","V":"व्","W":"व्","X":"क्ष्","Y":"ञ्","Z":"झ्",'; var ZWNJ = '"ःH":"\\u200C"';

// for compatibility with bookmarklets function cyr_translit(src) { return to_cyrillic(src); }

var conversionHash = undefined; var maxcyrlength = 0;

function getConversionHash { if (conversionHash == undefined) { // TODO var opr = "{" + vowels + roman + chill + swaram + numerals + conjuncts + caps + others; for (var consonant in consonants) { opr += '"' + consonant + 'a":"' + consonant + 'ा",'; opr += '"' + consonant + 'e":"' + consonant + 'े",'; opr += '"' + consonant + 'i":"' + consonant + 'ै",'; opr += '"' + consonant + 'o":"' + consonant + 'ो",'; opr += '"' + consonant + 'u":"' + consonant + 'ौ",'; }       for (var chk in chillaksharam) { opr += '"' + chk + 'a":"' + chillaksharam[chk] + '",'; opr += '"' + chk + 'q":"' + chillaksharam[chk] + '॑",'; opr += '"' + chk + 'e":"' + chillaksharam[chk] + 'े",'; opr += '"' + chk + 'i":"' + chillaksharam[chk] + 'ै",'; opr += '"' + chk + 'o":"' + chillaksharam[chk] + 'ो",'; opr += '"' + chk + 'u":"' + chillaksharam[chk] + 'ु",'; opr += '"' + chk + 'A":"' + chillaksharam[chk] + 'ा",'; opr += '"' + chk + 'E":"' + chillaksharam[chk] + 'ॅ",'; opr += '"' + chk + 'I":"' + chillaksharam[chk] + 'ी",'; opr += '"' + chk + 'O":"' + chillaksharam[chk] + 'ॉ",'; opr += '"' + chk + 'U":"' + chillaksharam[chk] + 'ू",'; }       opr += ZWNJ + "}"; // var tb = document.getElementById('wpTextbox1'); // tb.value = opr; conversionHash = eval("("+opr+")"); maxcyrlength=6; }

return conversionHash; }

function to_cyrillic(src, output, chunks) { if (src == undefined || src == "" || src == null) return src; if (output == undefined) output = new String;

var hash = getConversionHash; var location = 0; while (location < src.length) { var len = Math.min(maxcyrlength, src.length - location); var arr = undefined; var sub; while (len > 0) { sub = src.substr(location, len); arr = hash[sub]; if (arr != undefined) break; else len--; }       // need this for translit on the fly if (chunks != undefined) chunks[chunks.length] = sub; if (arr == undefined) { output += sub; location ++; }       else {

// case analysis var newChar = arr; if (sub.toLowerCase == sub.toUpperCase && arr.length > 1 && arr[1] && (newChar.toUpperCase != newChar.toLowerCase)) { // need translit hash to determine if previous character (and possibly the one before it) // were converted and are in upper case // set prevDud to true previous is not a translated character or simply a blank // set prevCap to true if previous was translated and was upper case

var prevCh = output.length == 0 ? null : output.substr(output.length - 1, 1); var prevDud = !prevCh || !getTranslitString(prevCh); var prevCap = (!prevDud && prevCh == prevCh.toUpperCase);

// sub is caseless but result isn't. case will depend on lookbehind and lookahead if (prevDud || !prevCap) { output += newChar.toLowerCase; prevCap = true; }		       else { var next = " "; if (location + len < src.length) next = src.substr(location + len, 1);

if (next != next.toUpperCase && next == next.toLowerCase ) { //next is lowercase (and not caseless) output += newChar.toLowerCase; }			       else if (next == next.toUpperCase && next != next.toLowerCase ) { // next is uppercase (and not caseless) output += newChar.toUpperCase; }			       else { // next is caseless. output case determined by the case of output[length - 2] var pprevCh = output.length == 1 ? null : output.substr(output.length - 2, 1); var pprevDud = !pprevCh || !getTranslitString(pprevCh); if (!pprevDud && (pprevCh == pprevCh.toUpperCase)) { //pre-prev is in upper case. output is also uppercase output += newChar.toUpperCase; }				       else { output += newChar.toLowerCase; }			       }		        }	        }	        else if ((sub.toLowerCase == sub.toUpperCase) && (arr.length < 2 || !arr[1])) { // literal treatment of newChar output += newChar;

}	       else if (sub != sub.toLowerCase) { // sub not all-lowercase output += newChar.toUpperCase; }	       else { // sub is lowercase output += newChar.toLowerCase; }	       location += len; }   }    return output; }

function convertIt(src,converter){ var resultbuffer=""; for(var i=0;i<src.length;i++){ resultbuffer=converter(resultbuffer+src[i]); }       return converter(resultbuffer);

}

var translitHash = undefined;

function initTranslit { if (translitHash == undefined) { translitHash = new Array;

for (var i = 0; i < conversionHash.length; i++) { var ch = conversionHash[i][1]; // if the translit string is not caseless, convert cyr string to upper case // otherwise maintain its case if (conversionHash[i][0].toUpperCase != conversionHash[i][0].toLowerCase) ch = ch.toUpperCase; if (translitHash[ch] == undefined) translitHash[ch] = conversionHash[i][0]; }   } }

//-- translit on-the-fly --

function replaceValue(node, value, stepback) { if (stepback == undefined) stepback = 0; if (isExplorer) { var range = document.selection.createRange; range.moveStart("character", -stepback); range.text = value; range.collapse(false); range.select; }   else { var scrollTop = node.scrollTop; var cursorLoc = node.selectionStart; node.value = node.value.substring(0, node.selectionStart - stepback) + value + node.value.substring(node.selectionEnd, node.value.length); node.scrollTop = scrollTop; node.selectionStart = cursorLoc + value.length - stepback; node.selectionEnd = cursorLoc + value.length - stepback; } }

// compare positions function positionIsEqual(other) { if (isExplorer) return this.position.isEqual(other.position); else return this.position == other.position; }

function Position(node) { if (node.selectionStart != undefined) this.position = node.selectionStart; else if (document.selection && document.selection.createRange) this.position = document.selection.createRange; this.isEqual = positionIsEqual; }

function resetState { this.position = new Position(this.node); this.transBuffer = ""; this.cyrBuffer = ""; }

function StateObject(node) { this.node = node; this.reset = resetState; this.cyrBuffer = ""; this.transBuffer = ""; this.position = new Position(node); }

var stateHash = new Array;

function isExplorer { return (document.selection != undefined && document.selection.createRange.isEqual != undefined); }

function pressedKey(event) { if (isExplorer) return event.keyCode; else return event.which; }

function transliterateKey(event) { /*   if ((event.keyCode == 27)     {        return;    }    */    if (event == undefined)        event = window.event;    var node = null;    if (event.target) {        node = event.target;        }    else if (event.srcElement) {        node = event.srcElement;        }    // initialize state    var state = stateHash[node];    if (state == null) {        state = new StateObject(node);        stateHash[node] = state;    }    if ( (pressedKey(event) > 20) && !event.ctrlKey && !event.altKey && !event.metaKey) {

var c = String.fromCharCode(pressedKey(event));

// process input var result = process_translit(state, c); // finish up       if (c != result.out || result.replace != 0) { if (isExplorer) event.returnValue = false; else event.preventDefault; replaceValue(node, result.out, result.replace); state.position = new Position(node);

}   } }

function TranslitResult { this.out = ""; this.replace = 0; }

function process_translit(state, c) { // reset state if position changed if (!state.position.isEqual(new Position(state.node))) state.reset; var result = new TranslitResult; // initial backbuffer. Add to it as characters are converted var backbuffer = getBackBuffer(state.node, state.cyrBuffer.length, 2); var chunks = new Array; state.transBuffer = state.transBuffer+ c

var str = to_cyrillic(state.cyrBuffer+c, backbuffer, chunks);

// remove backbuffer from output str = str.substr(backbuffer.length); result.out = str; /* str is now left alone - it has the output matching contents of chunks and will be used to reinitialize backbuffers, along with chunks and state.transBuffer */   // get the difference between state.cyrBuffer and output for (var i = 0; i < Math.min(state.cyrBuffer.length, result.out.length); i++) { if (state.cyrBuffer.substr(i, 1) != result.out.substr(i, 1)) { result.replace = state.cyrBuffer.length - i;	       result.out = result.out.substr(i); break; }   }    if (result.replace == 0) { if(result.out.length 0 && chunks[chunks.length - 1] == result.out.substr(result.out.length - 1)) { // no convertion took place, reset state state.reset; }   else { while (state.transBuffer.length > maxcyrlength) { state.transBuffer = state.transBuffer.substr(chunks[0].length); chunks.shift; str = str.substr(1); }       state.cyrBuffer = str; }   return result; }

function getBackBuffer(node, offset, count) { if (isExplorer) { //.tagName.toUpperCase == "EDITOR") {       var range = document.selection.createRange;        range.moveStart("character", -offset);        var result = range.text.substr(-count);        if (!result)	        result = "";        return result;

} else { return node.value.substring(0, node.selectionStart - offset).substr(-count); } }

// need this for bookmarklets function getSelectedNode { if (document.activeElement) return document.activeElement; else if (window.getSelection && window.getSelection && window.getSelection.rangeCount > 0) { var range = window.getSelection.getRangeAt(0); if (range.startContainer && range.startContainer.childNodes && range.startContainer.childNodes.length > range.startOffset) return range.startContainer.childNodes[range.startOffset] } return null; }

function toggleCyrMode { var node = getSelectedNode; if (node) { if (stateHash[node]) { if (removeKeyEventListener(node)) delete stateHash[node]; }       else { if (addKeyEventListener(node)) stateHash[node] = new StateObject(node); }   } }

function addKeyEventListener(node) { if (node.addEventListener) node.addEventListener("keypress", transliterateKey, false); else if (node.attachEvent) node.attachEvent("onkeypress", transliterateKey); else return false; return true; } function removeKeyEventListener(node) { if (node.removeEventListener) node.removeEventListener("keypress", transliterateKey, false); else if (node.detachEvent) node.detachEvent("onkeypress", transliterateKey); else return false; return true; }

function getSelectedText { if (isExplorer) { return document.selection.createRange.text; }   else { var node = getSelectedNode; if (node && node.value && node.selectionStart != undefined && node.selectionEnd != undefined) return node.value.substring(node.selectionStart, node.selectionEnd); }   return ""; }

/* calling functions */

function addLoadEvent(func) { if (window.addEventListener) window.addEventListener("load", func, false); else if (window.attachEvent) window.attachEvent("onload", func); }

function addTranslit(editForm,textBox) { checkrt(editForm); // check for translit support

if (textBox.addEventListener) textBox.addEventListener("keypress", processKeys, false); else if (textBox.attachEvent) textBox.attachEvent("onkeypress", processKeys); }

function addTextEvent {

///////			if(lija_getCookie('mlWikiTranslit')!=null && lija_getCookie('mlWikiTranslit')!="") realTime=(lija_getCookie('mlWikiTranslit')=="true")?true:false; //////

var editForm =document.getElementById('editform'); if(editForm != null) { var textBox=document.getElementById('wpTextbox1'); var textSummary = document.getElementById('wpSummary'); addTranslit(editForm,textBox); addCheckbox(editForm,textBox ); if(textSummary) { addTranslit(editForm,textSummary); } }

// add transliteration feature to search form also var searchForm = document.getElementById('searchform'); if( searchForm != null ) { try { var searchBox = document.getElementById('searchInput'); // add IME checkbox control to searchForm if (searchBox != null || searchForm != null) { var element = document.createElement("input"); element.setAttribute("type","checkbox"); element.setAttribute("id","sbrealtime"); if (element.addEventListener) element.addEventListener("click", rtClick, false); else if (element.attachEvent) element.attachEvent("onclick", rtClick); // var labelcheckBox = document.createTextNode('देवनागरी'); var chkSpan = document.createElement("span"); chkSpan.setAttribute("id","spnRealtime"); var searchBody = document.getElementById('searchBody'); searchBody.appendChild(element); searchBody.appendChild(chkSpan); // searchForm.insertBefore(element,searchBox); document.getElementById("sbrealtime").checked = realTime; document.getElementById('spnRealtime').innerHTML = 'देवनागरी'; // searchForm.insertBefore(chkSpan,searchBox); // var p = document.createElement("p"); // p.setAttribute("style","width:100%;height:1px;"); // searchForm.insertBefore(p,searchBox); }				 }				 catch(ex) {				 //ऎऱऱिनॆ ओटिक्कुक. }				 if(searchBox) { addTranslit(searchForm,searchBox); } }            			// add transliteration feature to power search.

var powerSearchForm = document.getElementById('search'); if( powerSearchForm != null) { var powerSearchBox = document.getElementById('lsearchbox'); if( powerSearchBox ) { addTranslit(powerSearchForm, powerSearchBox );} } // add transliteration feature to move page.

var movePageForm = document.getElementById('movepage'); if( movePageForm != null ) { var newTitleBox = document.getElementById('wpNewTitle'); if( newTitleBox ) {addTranslit(movePageForm, newTitleBox);} var reasonBox = document.getElementById('wpReason'); if( reasonBox ) {addTranslit(movePageForm, reasonBox ); } }

//विक्किपीडिय:कळरि var kalariForm = document.getElementById('createbox'); if( kalariForm != null ) {

var kalariText = document.getElementsByName('title')[0]; if( kalariText ) {addTranslit(kalariForm, kalariText);} }

//अप्‌लोड् ताळ्‍ //********************           var upForm=document.getElementById('upload'); if(upForm)//ई फों इल्लॆङ्किल्‍ पिन्नॆ मुन्नोट्ट् पोकण्ट. {             var upName=document.getElementById('wpDestFile'); var upComment=document.getElementById('wpUploadDescription'); if(upName) addTranslit(upForm,upName); if(upComment) addTranslit(upForm,upComment); }          //**************			//पवर्‍ सेर्‍च्च् //**************			/*  var nsSearchForm=document.getElementById('powersearch'); if(nsSearchForm) {                    addTranslit(nsSearchForm,document.getElementsByName('search')[1]); }*/			//**************   }

function addCheckbox(editform,textBox) {

if(editform==null||textBox==null) return;

try {           var element = document.createElement("input"); element.setAttribute("type","checkbox"); element.setAttribute("id","realtime");

if (element.addEventListener) element.addEventListener("click", rtClick, false); else if (element.attachEvent) element.attachEvent("onclick", rtClick);

var labelcheckBox = document.createTextNode(' देवनागरी परिक्षण - Use Ctrl + M to Toggle.'); editform.insertBefore(element,textBox); document.getElementById("realtime").checked = realTime; editform.insertBefore(labelcheckBox,textBox); var p = document.createElement("p"); p.setAttribute("style","width:100%;height:1px;"); editform.insertBefore(p,textBox); }            catch(ex) {             //ऎऱऱिनॆ ओटिक्कुक. }       }

var imeStatus = false; function processKeys(event) { if (rtsupported) { e = event || window.event;

var myKeyCode = 0; var myShiftKey = false; if( document.all ) { //IE myKeycode = e.keyCode; myShiftKey = e.shiftKey; } else if ( document.getElementById || document.layers ) { myKeyCode = e.which; myShiftKey = (myKeyCode == 16)?true:false; }

if ((e.keyCode == 14 && e.ctrlKey) || (e.which == 109 && e.ctrlKey)) {		           realTime = !realTime; lija_setCookie('mlWikiTranslit',realTime,1);//save in cookie var chk = document.getElementById('realtime'); var chkSb = document.getElementById('sbrealtime'); if (chk) { chk.checked = realTime; } if (chkSb) { chkSb.checked = realTime; } return false; }	           else if ((e.keyCode >= 3328 && e.keyCode <= 3455) || (e.which >= 3328 && e.which <= 3455)) { var chk = document.getElementById('realtime'); var spanSb = document.getElementById('spnRealtime'); var chkSb = document.getElementById('sbrealtime'); if (imeStatus == false || realTime) { realTime = false; if (chk) { chk.checked = realTime; chkSb.checked = realTime; spanSb.innerText = chkSb.checked } imeStatus = true; //alert('A Malayalam input tool was detected. Disabling in-built transliteration. To turn it On again use Ctrl+M'); window.status='A Malayalam input tool was detected. Disabling in-built transliteration. To turn it On again use Ctrl+M';//FF failure // return false; }	           }                else if (realTime) { imeStatus = false; transliterateKey(event); // call transliteration function }           }         }        var realTime=false;

function rtClick(event) { realTime = !realTime; lija_setCookie('mlWikiTranslit',realTime,1);//save in cookie }

var rtsupported = false; var error;

function checkrt(editform) { try { /*           var nav = navigator.userAgent.toUpperCase; rtsupported = (nav.indexOf("GECKO") >= 0 || nav.indexOf("OPERA") >= 0 || nav.indexOf("SAFARI") >= 0); */               rtsupported = (document.selection != undefined) if (!rtsupported) { var element = document.createElement("TEXTAREA"); editform.appendChild(element); if (element.selectionStart != undefined) rtsupported = true; editform.removeChild(element); }           } catch (error) {} }

//************************************		// ट्रान्‍स्‌लिऱ्ऱऱेषन्‍ स्क्रिप्ऱ्ऱिल्‍ कूक्कि सप्पोर्‍ट्ट् चेर्‍क्कानुळ्ळ श्रमं

function lija_getCookie(cookieName) {		if (document.cookie.length>0) { 		cookieStart=document.cookie.indexOf(cookieName + "="); if (cookieStart!=-1) { 		cookieStart=cookieStart + cookieName.length+1; cookieEnd=document.cookie.indexOf(";",cookieStart); if (cookieEnd==-1) cookieEnd=document.cookie.length; return unescape(document.cookie.substring(cookieStart,cookieEnd)); } 		}		return ""; }

function lija_setCookie(cookieName,value,expiredays) {		var exdate=new Date; exdate.setDate(exdate.getDate+expiredays); document.cookie=cookieName+ "=" +escape(value)+"; path=/"+ ((expiredays==null) ? "" : "; expires="+exdate.toGMTString); }

//************************************

// add this line to your monobook.js to disable in-built transliteration - thanks to Sadik Khalid for this quick tip // var userIMEEnabled = false;  // paste this line to user monobook.js and remove // from the begining of this line

// check for user preference and load in-built transliteration tool if(typeof(window.userIMEEnabled) == 'undefined') { addLoadEvent(addTextEvent); } /*

/*

addLoadEvent
*/ function addLoadEvent(func) { addOnloadHook( func ); }

/* */

/*