// (c) 2004-2010 Ali Jahanshiri <alijsh@yahoo.com>

if(!String.prototype.trim){String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');};}
String.prototype.endsWith=function(exp){return(this.search(new RegExp(exp+'$'))!==-1);};String.prototype.beginsWith=function(exp){return(this.search(new RegExp('^'+exp))!==-1);};String.prototype.contains=function(string){return(' '+this+' ').indexOf(' '+string+' ')>-1;};var Elem={addClass:function(el,cl){if(!el.className.contains(cl)){el.className=(el.className+' '+cl).trim();}},removeClass:function(el,cl){el.className=((' '+el.className+' ').replace(' '+cl+' ','')).trim();},hasClass:function(el,cl){return el.className.contains(cl);},isAllWS:function(nod){return!(/[^\t\n\r ]/.test(nod.data));},isIgnorable:function(nod){return(nod.nodeType==8)||((nod.nodeType==3)&&this.isAllWS(nod));},sibBefore:function(sib){while((sib=sib.previousSibling)){if(!this.isIgnorable(sib)){return sib;}}
return null;},sibAfter:function(sib){while((sib=sib.nextSibling)){if(!this.isIgnorable(sib)){return sib;}}
return null;}};var Browser={info:navigator.userAgent.toLowerCase(),isIE:function(){return(this.info.indexOf('msie')!==-1);}};var $I=function(selector){return document.getElementById(selector);};var $T=function(selector,context){return context.getElementsByTagName(selector);};var $C=function(selector,context){var chunks=selector.split('.'),tagName=chunks[0],className=chunks[1],elems=$T(tagName,context),i=0,il=elems.length,ret=[];for(;i<il;i+=1){if(elems[i].className.contains(className)){ret.push(elems[i]);}}
return ret;};var $K=function(selector){return(selector.charAt(0)==='#')?'ID':(selector.split('.').length===2)?'CLASS':'TAG';};var $=function(selector,context){var parts=selector.split(' ');context=context||document;if(parts.length===1){switch($K(parts[0])){case'ID':return $I(selector.substr(1));case'TAG':return $T(parts[0],context);case'CLASS':return $C(parts[0],context);}}else if(parts.length===2){switch($K(parts[0])){case'ID':context=$I(parts[0].substr(1));switch($K(parts[1])){case'TAG':return $T(parts[1],context);case'CLASS':return $C(parts[1],context);}
break;case'TAG':case'CLASS':return null;}}
return null;};var TabView=function(args){if(args.premade){this.view=$('#'+args.id);this.bar=$('ul',this.view)[0];this.frame=$('div',this.view)[0];this.selIndex=args.active||0;}else{this.view=document.createElement('div');this.bar=document.createElement('ul');this.frame=document.createElement('div');this.view.id=args.id;Elem.addClass(this.view,'tabview');$('#'+args.parent).appendChild(this.view);this.view.appendChild(this.bar);this.view.appendChild(this.frame);this.selIndex=-1;}
Elem.addClass(this.bar,'tabbar');Elem.addClass(this.frame,'tabframe');this.index=-1;this.isCloseable=args.closeable||false;this.pfxTab=this.view.id+'-tab';this.pfxPage=this.view.id+'-page';this.tabs=$('li',this.bar);if(args.premade){this.populate();}
this.addEvent();};TabView.prototype={populate:function(index){var DIVs=$('div',this.frame),i=0,il=DIVs.length,n=-1;for(;i<il;i+=1){if(Elem.hasClass(DIVs[i].parentNode,'tabframe')){n+=1;Elem.addClass(DIVs[i],'tabpage');DIVs[i].id=this.pfxPage+n;this.tabs[n].id=this.pfxTab+n;}}
this.index=n;this.selectTab(this.selIndex);},getTab:function(index){return $('#'+this.pfxTab+index);},getTabIndex:function(tab){return parseInt(tab.id.replace(this.pfxTab,''),10);},getPage:function(index){return $('#'+this.pfxPage+index);},selectTab:function(index){var tab=this.getTab(index);if(!tab){return;}
Elem.addClass(tab,'active');Elem.addClass(this.getPage(index),'active');this.selIndex=index;},deselectTab:function(index){var tab=this.getTab(index);if(!tab){return;}
Elem.removeClass(tab,'active');Elem.removeClass(this.getPage(index),'active');},showTab:function(index){this.deselectTab(this.selIndex);this.selectTab(index);},addEvent:function(){var self=this;this.bar.onmouseup=function(e){var ev=window.event||e,tab=ev.target||ev.srcElement,btn=null;if(tab.nodeType===3||tab.nodeName.toLowerCase()!=='li'){tab=tab.parentNode;}
if('object'===typeof ev){if(Browser.isIE()){switch(ev.button){case 1:btn='l';break;case 4:btn='m';break;}}else{switch(ev.button){case 0:btn='l';break;case 1:btn='m';break;}}
switch(btn){case'l':self.showTab(self.getTabIndex(tab));break;case'm':if(self.isCloseable){self.removeTab(self.getTabIndex(tab));}
break;}}};},addTab:function(args){var tab=document.createElement('li'),page=document.createElement('div');this.index+=1;tab.id=this.pfxTab+this.index;tab.appendChild(document.createTextNode(args.caption));Elem.addClass(page,'tabpage');page.id=this.pfxPage+this.index;page.innerHTML=args.content;this.bar.appendChild(tab);this.frame.appendChild(page);this.showTab(this.index);},removeTab:function(index){this.switchTab(index);this.bar.removeChild(this.getTab(index));this.frame.removeChild(this.getPage(index));},switchTab:function(index){if(index!==this.selIndex){return;}
var tab=this.getTab(index),sw=Elem.sibBefore(tab);if(!sw){sw=Elem.sibAfter(tab);}
if(sw){this.showTab(this.getTabIndex(sw));}}};var Keyboard={init:function(){var i,il,j,jl,keyrows=['q w e r t y u i o p','a s d f g h j k l -','z x c v b n m - - -','â č ğ š ž ø - - - -'],keys=[],out='<table class="kb"><tr>'+'<td><input type="button" class="button" value="'+Res.of('clear')+'" onclick="Keyboard.clear()"/></td>'+'<td><input type="button" class="button" value="'+Res.of('space')+'" onclick="Keyboard.space()"/></td>'+'<td><input type="button" class="button" value="'+Res.of('backspace')+'" onclick="Keyboard.backspace()"/></td>'+'</tr></table><div id="plchars">';function makeKB(c){out+='<table class="'+c+'">';for(i=0,il=keyrows.length;i<il;i+=1){out+='<tr>';keys=keyrows[i].split(' ');for(j=0,jl=keys.length;j<jl;j+=1){out+='<td'+(keys[j]==='-'?' class="hidden"':'')+'><input type="button" class="button" value="'+keys[j]+'" onclick="Keyboard.insert(\''+keys[j]+'\')"/></td>';}
out+='</tr>';}
out+='</table>'}
makeKB('kb');keyrows=['âdan ândan idan ndan rdan udan dan','ftan stan štan xtan tan - -'];makeKB('kb');out+='</div><div id="pachars">';keyrows=['آ ا ب پ ت ث ج چ ح خ د','ذ ر ز ژ س ش ص ض ط ظ ع','غ ف ق ک گ ل م ن و ه ی'];makeKB('kb rtl');keyrows=['اندن ادن ردن ندن ودن یدن دن','ختن ستن شتن فتن تن - -'];makeKB('kb rtl');out+='</div>';$('#kb').innerHTML=out;},ed:$('#txtVerb'),insert:function(str){this.ed.value+=str;this.ed.focus();},space:function(){this.ed.value+=' ';this.ed.focus();},clear:function(){this.ed.value='';this.ed.focus();},backspace:function(){var str=this.ed.value;if(str.length>0){this.ed.value=str.substring(0,str.length-1);this.ed.focus();}}};var Res={problem:{en:'There was a problem. Please try again later.',es:'Hubo un problema. ¡Inténtalo otra vez!'},XMLHTTP:{en:'Giving up: your browser does not support the required functionality (XMLHTTP)',es:'Saliendo: tu navegador no sostiene la funcionalidad necesaria (XMLHTTP)'},NAV:{en:'You have not entered an infinitive.',es:'No has introducido un infinitivo.'},NV:{en:'Enter your verb!',es:'Enter your verb!'},check:{en:'Check your input! It includes invalid characters.',es:'Verifica tu introducido! Incluye los caracteres inválidos.'},space:{en:'Space',es:'Espacio'},backspace:{en:'Backspace',es:'Retroceso'},clear:{en:'Clear',es:'Borrar'},clickTo:{en:'Click to conjugate',es:'Haz clic para conjugar'},of:function(s){return this[s][Page.lang]||this[s][en];}};var AJAX={status:'ready',ready:function(){$('#statbox').style.display='none';this.status='ready';clearInterval(this.timeout);},busy:function(){this.status='busy';$('#statbox').style.display='block';},fail:function(){window.alert(Res.of('problem'));this.ready();},success:function(){Page.tvwContent.showTab(0);Page.tvwConjs.addTab({caption:Conjugator.verb,content:this.xhr.responseText});this.ready();},send:function(url){var self=this;this.xhr.onreadystatechange=function(){self.query();};this.xhr.open('GET',url,true);this.xhr.send(null);this.timeout=setTimeout(function(){self.xhr.abort();},10E3);},query:function(){try{if(this.xhr.readyState==4){if(this.xhr.status==200){this.success();}else{this.fail();}}else{this.busy();}}catch(e){window.alert(e.description);}},init:function(){var versions=['Msxml2.XMLHTTP','Microsoft.XMLHTTP'],i=0;this.xhr=null;if(window.XMLHttpRequest){this.xhr=new XMLHttpRequest();if(this.xhr.overrideMimeType){this.xhr.overrideMimeType('text/xml');}}else if(window.ActiveXObject){for(;i<2;i+=1){try{this.xhr=new ActiveXObject(versions[i]);break;}
catch(e){}}}
if(this.xhr===null){this.status='busy';window.alert(Res.of('XMLHTTP'));}}};var Conjugator={init:function(){AJAX.init();this.mode='pa';},err:function(s){window.alert(s);$('#txtVerb').focus();}};Conjugator.InputOK=function(mode){var str=$('#txtVerb').value.trim().toLowerCase(),erroneous=false,re='';if(str.length===0){this.err(Res.of('NV'));return false;}
switch(mode){case'pa':str=str.replace(/\u0643/g,'\u06A9');str=str.replace(/[\u0649\u064A]/g,'\u06CC');break;case'pl':str=str.replace(/\'/g,'ø').replace(/aa/g,'â').replace(/ch/g,'č').replace(/gh/g,'ğ').replace(/zh/g,'ž').replace(/sh/g,'š');break;default:return false;}
$('#txtVerb').value=str;switch(mode){case'pa':re='[^\u0622-\u0648\u067E\u0686\u0698\u06AF\u06A9\u06CC\u200C\u064E\u064F\u0650\u0651 ]';erroneous=($('#txtVerb').value.search(re)!=-1);break;case'pl':re="[^a-zâøğžš' ]";erroneous=($('#txtVerb').value.search(re)!=-1);break;}
if(erroneous){this.err(Res.of('check'));return false;}
switch(mode){case'pa':if((str.length<3)||(!str.endsWith('[\u062F\u062A]\u0646'))){this.err(Res.of('NAV'));return false;}
break;case'pl':if((str.length<5)||(!str.endsWith('[dt]an'))){this.err(Res.of('NAV'));return false;}
break;}
return true;};Conjugator.conjugate=function(verb,mode){if(AJAX.status!=='ready'){return;}
if(!mode){mode=this.mode;}
if(!verb){if(this.InputOK(mode)){verb=$('#txtVerb').value;}else{return;}}
this.verb=verb;AJAX.send(((mode=='pa')?'pvcpa.php':'pvcpl.php')+'?verb='+encodeURIComponent(verb.trim())+'&lang='+Page.lang);};$('#txtVerb').onkeydown=function(e){if(AJAX.status!=='ready'){return;}
var keynum=(window.event)?e.keyCode:e.which;if(keynum==13){Conjugator.conjugate();}};$('#cboScript').onchange=function(){Conjugator.mode=this.options[this.selectedIndex].value;switch(Conjugator.mode){case'pa':$('#txtVerb').className='rtl';$('#pachars').style.display='';$('#plchars').style.display='none';break;case'pl':$('#txtVerb').className='ltr';$('#pachars').style.display='none';$('#plchars').style.display='';break;}};$('#btnConj').onclick=function(){Conjugator.conjugate();};var Page={};Page.makeTopList=function(){function make(items,mode){var s='<ul class="m-list'+((mode==='pa')?' rtl':'')+'">';for(var i=0;i<20;i+=1){s+='<li><a herf ="#" title="'+Res.of('clickTo')+'" onclick="Conjugator.conjugate(\''+items[i]+'\',\''+mode+'\')">'+items[i]+'</a></li>';}
$('#top-'+mode).innerHTML+=s+'</ul>';}
make(['بودن','رَفتن','دادن','آمدن','داشتن','کردن','دیدن','نوِشتن','شدن','خواندن','خوردن','خواستن','گرفتن','گفتن','آوردن','دانستن','گشتن','زدن','شستن','شنیدن'],'pa');make(['raftan','budan','kardan','xordan','didan','xwâstan','dâštan','šodan','dâdan','goftan','neveštan','âmadan','bastan','šostan','bordan','gereftan','xwândan','šenidan','dânestan','šekastan'],'pl');};Page.init=function(){this.lang=$('html')[0].getAttribute('lang')||'en';this.lang=this.lang.split('-')[0];this.tvwContent=new TabView({premade:true,id:'tvwContent',active:2});this.tvwConjs=new TabView({id:'tvwConjs',parent:'output',closeable:true});this.makeTopList();Keyboard.init();Conjugator.init();$('#cboScript').selectedIndex=0;$('#cboScript').onchange();};Page.init();