function doPrint(){
	//alert('Article: <?=$_article?>, Issue: <?=$_issue?>');
	window.location = window.location + "&print=1";
}

function doEmail(){
	//alert('Article: <?=$_article?>, Issue: <?=$_issue?>');
	window.location = '/email.php?url=' + window.location;
}


var countTabs 	 = 0;
var allowRmvTabs = 0;
var isSelected	 = false;
var popupWin 	 = false;

function HandleKeyDown(obj, event) {
   var tabKeyCode = 9;
   if (event.keyCode == tabKeyCode && event.srcElement == obj) {
      obj.selection = document.selection.createRange();
      obj.selection.text = String.fromCharCode(tabKeyCode);
      event.returnValue = false;
      countTabs++;
   }
}


function cntTabs(){
	txtObj = document.getElementById('bodyCode');
	if (txtObj.createTextRange) {
    	txtObj.caretPos = document.selection.createRange().duplicate();
    	isSelected = true;
	}
}

function doTabs(e){
	if(!e) {
		var e = window.event;
	}
	
	var letter = 0;
	var ctrl = (e.ctrlKey)?true:false;
	/*
	if ((e.keyCode<16)||(e.keyCode>18)) {
		letter=e.keyCode;
	}
	*/
	if(e.keyCode){
		letter = e.keyCode;
	} else if(e.which) {
		letter = e.which;
	}
	
	if(letter=='13' && !ctrl){
		setTimeout('doTabs2()',10);
	}
	if((letter=='8') && allowRmvTabs){
		countTabs--;
	}
	else{
		allowRmvTabs = 0;
	}
	if(countTabs>6) countTabs=0;
}

function doTabs2(){
	document.getElementById('bodyCode').selection = document.selection.createRange();
	for(i=0;i<countTabs;i++){
		document.getElementById('bodyCode').selection.text = String.fromCharCode(9);
	}
	allowRmvTabs=1;
}

// DETECTS SELECTION IN TEXTAREA OBJECTS


function markSelection (txtObj) {
	if (txtObj.createTextRange) {
    	txtObj.caretPos = document.selection.createRange().duplicate();
    	isSelected = true;
	}
}

// NESTS HTML TAGS AROUND A SELECTION

function insertTag (txtName, tagn1, tagn2) {
	var txtObj = document.getElementById('bodyCode');
	
	
	/*
	for( var i in txtObj ){
		document.writeln('txtObj[\' '+i+' \'] \t= ' + txtObj[i] + '\n<br />');
	}*/
	
	if (txtObj.selectionEnd && txtObj.selectionStart) {
		//alert('veritaserum');
		var startPos = txtObj.selectionStart;
		var endPos 	 = txtObj.selectionEnd;
		var txtLength = txtObj.textLength;
		txtObj.value = txtObj.value.substring(0, startPos) + tagn1 + txtObj.value.substring(startPos, endPos) + tagn2 + txtObj.value.substring(endPos, txtLength);
		txtObj.focus();
	} else if (isSelected) {
		// handle for firefox
		if (txtObj.createTextRange && txtObj.caretPos) {
		// handle for internet explorer
	    	var caretPos = txtObj.caretPos;
		  	caretPos.text = tagn1+caretPos.text+tagn2;
	      	markSelection (txtObj);
	      	if (txtObj.caretPos.text=='') {
	        	isSelected=false;
	     		txtObj.focus();
	      	}
		}
	}
	/*
	if (txtObj.selectionEnd) {
		// handle for firefox
		var startPos = txtObj.selectionStart;
		var endPos 	 = txtObj.selectionEnd;
		var txtLength = txtObj.textLength;
		txtObj.value = txtObj.value.substring(0, startPos) + footnote + txtObj.value.substring(endPos, txtLength);
		
  	} else if(txtObj.textLength) {
		var txtLength = txtObj.textLength;
		txtObj.value = txtObj.value.substring(0, textLength) + footnote 
		
	}  else {
		txtObj.focus();
		txtObj.value = txtObj.value + footnote;
		
	}
	
	} else {
		var txtObj = document.getElementById('bodyCode');
		// if nothing is selected in the bodyCode
		// insert something at the end of the bodyCode
		txtObj.focus();
		txtObj.value = txtObj.value + tagn1 + tagn2;
		
	}*/
} 


function insertFootnote(txtName, footnote) {
	if (isSelected) {
		var txtObj = eval ("document.forms[0]." + txtName);
		if (txtObj.createTextRange && txtObj.caretPos) {
			var caretPos = txtObj.caretPos;
			caretPos.text = footnote + caretPos.text;
      		markSelection(txtObj);
      		if (txtObj.caretPos.text == '') {
        		isSelected=false;
     			txtObj.focus();
      		}
    	}
  	} 
	
	
}


function callFootnote(article_id, democratiya){
	
	txtObj = document.getElementById('bodyCode');
	
	/*
	var text = false;
	if (txtObj.selectionEnd != undefined) {
		// handle for firefox
		var startPos 	= txtObj.selectionStart;
		var endPos 	 	= txtObj.selectionEnd;
		text 		= txtObj.value.substring(startPos, endPos);
			
	} else  {
		text = document.selection;
	}

	popupWin = window.open('/cms/footnote/add.php?article=' + article_id ,'useless','width=600,height=200,resizeable=yes,status=yes');
	if( text ){
		//alert( text );
		popupWin.document.getElementById('text').value = text;
	}
	*/
	
	if (democratiya)
	{
		openWindow('/cms/democratiya/footnote/add.php?article=' + article_id ,'useless','width=600,height=200,resizeable=yes,status=yes');
	}
	else
	{
		openWindow('/cms/footnote/add.php?article=' + article_id ,'useless','width=600,height=200,resizeable=yes,status=yes');
	}
}

/*
function editFootnote(footnote_id){
	openWindow('/cms/footnote/edit.php?footnote=' + footnote_id ,'footnote', 'width=800,height=400,resizeable=yes,status=yes');
}
*/

function openWindow(url, name, params) {
  	if(params == undefined) {
		params = 'width=520,height=500,scrollbars=yes,resizeable=yes';
	}
	//alert( 'open: ' + url + ', \n name: ' + name + ', \n params: ' + params );
  	popupWin = window.open(url, name, params);
  	if(popupWin) {
		popupWin.focus();
	} else {
		alert("Your browser has blocked this feature. Please allow popups to use this feature.");
	}
	return false;
}
	
/*
function eventExt(id, val) {
	//Accepts params from external page (all_galleries.php) and adds them to the resources list.
	insertFootnote(
		'data[body]',
		'<footnote id="' + id + '" number="' + val + '" />');
}
*/

function onKeyDownH(e){
	if(!e) var e = window.event;
	var ctrl = (e.ctrlKey)?true:false;
	var letter = 0;
	
	if(e.keyCode){
		letter = e.keyCode;
	} else if(e.which) {
		letter = e.which;
	}
	
	if((letter=='32') && ctrl){
		//alert('letter 32 plus ctrl');
		// ctrl + space
		var article_id = document.getElementById('article').value;
		//alert("opening article " + article_id);
		callFootnote(article_id);
		//editField('f');
	}
	
	if((letter=='13') && ctrl){
		insertAtCursor(document.forms[0].bodyCode,"<br>");
	}
	
	if((letter=='66') && ctrl){
		// ctrl + b 
		editField('b');
		return false;
	}
	if((letter=='73') && ctrl){
		// ctrl + i
		editField('i');
		return false;
	}
	if((letter=='85') && ctrl){
		// ctrl + u 
		editField('u');
		return false;
	}
	if((letter=='68') && ctrl){
		editField('d');
		return false;
	}
	if((letter=='72') && ctrl){
		editField('h');
		lock=0;
		return false;
	}
	if((letter=='49') && ctrl){
		editField('l');
		return false;
	}
	if((letter=='50') && ctrl){
		editField('c');
		return false;
	}
	if((letter=='51') && ctrl){
		editField('r');
		return false;
	}
	if((letter=='119') && ctrl){
		launchFindAndReplace();
		return false;
	}
	if((letter=='83') && ctrl){
		submitForm();
		return false;
	}
	letter=0;
	ctrl=0;
}

function editField(x) {
	
	
	if(x=='f'){
		insert1='<footnote>';
		insert2='</footnote>';
	}

	if(x=='b'){
		insert1='<b>';
		insert2='</b>';
	}
	if(x=='i'){
		insert1='<i>';
		insert2='</i>';
	}
	if(x=='u'){
		insert1='<u>';
		insert2='</u>';
	}
	if(x=='l'){
		insert1='<div style="text-align:left">';
		insert2='</div>';
	}
	if(x=='c'){
		insert1='<div style="text-align:center">';
		insert2='</div>';
	}
	if(x=='r'){
		insert1='<div style="text-align:right">';
		insert2='</div>';
	}
	if(x=='d'){
		insert1='<span class="text">';
		insert2='</span>';
	}
	if(x=='h'){
		href=prompt("Insert a Hyperlink URL","http://");
		insert1='<a href="'+href+'">';
		insert2='</a>';
	}
	insertTag('bodyCode',insert1,insert2);
}


// KEYBOARD LISTENER

function onloadH(e){
   emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+" : "unknown";
   if (emod == "NN4") {
      document.captureEvents(Event.KEYDOWN);
    }
   document.onkeydown = onKeyDownH;
   return true;
}

function selectRange(startOffset,length){
var obj = document.getElementById("bodyCode");
if( obj.createTextRange ){
	obj.focus( obj.caretPos );
	var caretPos =document.selection.createRange().duplicate();
	caretPos.moveToElementText(obj);caretPos.collapse();
	caretPos.moveStart( "character", startOffset );
	caretPos.moveEnd( "character", length );
	caretPos.select();
	}
}

function findNext(){
	selectRange(document.getElementById('bodyCode').value.indexOf(document.findreplace.find.value), document.findreplace.find.value.length);
}	

function findAndReplace(x){
	brk='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-&';
	fnd=document.findreplace.find.value;
	rpl=document.findreplace.replace.value;
	str=document.getElementById('bodyCode').value;
	stp=new Array();
	cnt=0;
	nStr=str;
	if(!document.findreplace.checkcase.checked){
		fnd=fnd.toLowerCase();
		nStr=nStr.toLowerCase();
	}
	for(i=0;i<str.length;i++){
		nFnd=nStr.indexOf(fnd);
		nRpl='';
		if(document.findreplace.wholeword.checked) rLen=fnd.length;
		else rLen=rpl.length;
		if(nFnd>=0){
			for(r=1;r<=rLen;r++){
				nRpl=nRpl+'~';
			}
			nStr=nStr.substring(0,nFnd)+nRpl+nStr.substring(nFnd+fnd.length,nStr.length);
		}
		if(nFnd<0) break;
		stp[nFnd]=1;
		ch1=nStr.charAt(nFnd-1);
		ch2=nStr.charAt(nFnd+fnd.length);
		if(document.findreplace.wholeword.checked){
			if(brk.indexOf(ch1)>-1||brk.indexOf(ch2)>-1) stp[nFnd]=0;
		}
	}
	nRpl='';
	for(j=0;j<str.length;j++){
		if(x&&cnt>0) break;
		if(stp[j]){
			str=str.substring(0,j)+rpl+str.substring(j+fnd.length,str.length);
			cnt++;
		}
	}
	document.getElementById('bodyCode').value=str;
	ifs='';
	if(cnt!=1)ifs='s';
	alert('Replaced '+cnt+' instance'+ifs);
}

function launchFindAndReplace(){
	frDiv.style.display='block';
	selectedText = document.selection;
	if (selectedText.type == 'Text'){
		newRange=selectedText.createRange();
		document.findreplace.find.value=newRange.text;
	}
}

killTime = 0;
function keepAlive()
{
 if(killTime) 
 {
 	clearTimeout(killTime);
 	killTime = 0;
 }
}
function killMenu()
{
 killTime = setTimeout("closeMenu()",120);
}
function openMenu()
{
	document.getElementById('subSearchTypeMenu').style.display = 'block';
}
function closeMenu()
{
	document.getElementById('subSearchTypeMenu').style.display = 'none';
}
function changeSelection(x,y)
{
	document.forms['subSearch'].searchType.value = x;
	//document.getElementById('searchType').value = x;
	document.getElementById('subSearchType').innerHTML = y;
	closeMenu();
}

function footnote(n)
{
	fnElem = document.getElementById('fn'+n);
	fnElem.style.display = (fnElem.style.display == 'block') ? 'none' : 'block';
}
