// JavaScript to support mithras.org
// mithrasorg.js - release 005
//
// Original material (c) Mithras.org 2003
//
// Helped along the way, thanks to:
//   Weblog engine:       Movabletype.org
//   Farsi text handler:  Farsitools (Sourceforge) / Persianblog 
//   Calendar functions:  John Walker, http://www.fourmilab.ch/ 
//   Expandable sections: M.C. Matti, http://www.webreference.com/programming/css_content/index.html
//   Cookie functions:    Tomer Shiran, copyright (c) 1996-1997 Athenia Associates, http://www.webreference.com/js/


// Email and web page icon routines

function iconemail(sTxt) {
	if (sTxt != "") {
		document.write('<a href="mailto:' + sTxt + '"><img src="/images/envelope.gif"  border="0"></a>&nbsp;');
	}
}

function iconwebpage(sTxt) {
	sOutput = "";
	if (sTxt != "") {
		sOutput += '<a href="javascript:pop_under(\'';
		sOutput += sTxt;
		sOutput += '\');"><img src="/images/house.gif" border="0"></a>&nbsp;';
	}
	document.write ( sOutput );
}

function iconauthor(sAuthor, sURL) {
	sOutput = "";
	if (sAuthor != "") {
		sOutput += 'نويسنده:&nbsp;';
		if (sURL != "") {
			sOutput += '<a target="_blank" href="' + sURL + '">' + sAuthor + '</a>';
		} else {
			sOutput += sAuthor;
		}
		sOutput += '&nbsp;&nbsp;&nbsp;'
	}
	document.write ( sOutput );

}

function scrambler(sTxt) {
	var sOutput = "";

	for (i = 0 ; i < sTxt.length ; i++) {
		switch (sTxt.charAt(i)) {
			case '.' :
				sOutput += ' نقطه ';
				break;
			case '@' :
				sOutput += ' در ';
				break;
			default :
				sOutput += sTxt.charAt(i);
		}
	}
	alert ('Input was: '+sTxt+', output is: '+sOutput);
	return sOutput;
}


var PERSIAN_DAYS = new Array(
	"شنبه","يکشنبه","دوشنبه","سه شنبه","چهار شنبه","پنجشنبه","جمعه");

var PERSIAN_DIGITS = new Array(
	"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹");

var PERSIAN_MONTHS = new Array(
	"فروردين","ارديبهشت","خرداد","تير","مرداد","شهريور","مهر","آبان","آذر","دی","بهمن","اسفند");

var BUTTON_LANG = new Array(
	"انگليسی",
	"فارسی");

var BUTTON_KEYB = new Array(
	"منطقی",
	"ايرانی");


var langFarsi;		
var keybFarsi;		


function farsinum(sTxt) {
	var sOutput = "";

	for (i = 0 ; i < sTxt.length ; i++) {
		if (sTxt.charCodeAt(i) >= 48 && sTxt.charCodeAt(i) <= 57)
			sOutput += PERSIAN_DIGITS[sTxt.charCodeAt(i) - 48];
	}
	
	return sOutput;
}

function farsidate(sInput)
{

	var dd, mm, yy, gd, nFirstcomma, nSecondcomma;
	var sDelimiter = "/";

	nFirstcomma = sInput.indexOf(sDelimiter, 1);
	nSecondcomma = sInput.indexOf(sDelimiter, nFirstcomma + 1);
	
// Even though mithras.org uses dd/mm/yy throughout, as a concession to our American friends we'll default to "Great Satan" format

// Strip off leading zeroes from the parsed date components - this squiffs the function
	mm = sInput.substring(0, nFirstcomma);
	dd = sInput.substring(nFirstcomma + 1, nSecondcomma);
	yy = sInput.substring(nSecondcomma + 1);

	if (mm.charAt(0) == "0") {mm = mm.charAt(1)};
	if (dd.charAt(0) == "0") {dd = dd.charAt(1)};
//	No need to process yy - this is passed as a four digit year

	gd = gregorian_to_jd(parseInt(yy), parseInt(mm), parseInt(dd));

	return jd_to_persian(gd);

}

// Pop-under routine - to open comment author's web pages underneath

function pop_under(url) {
	wintmp=window.open(url);
//	wintmp.blur();
	window.focus();
}
// Expandable / Collapsable sections

function dsp(loc){
   if(document.getElementById){
      var foc=loc.firstChild;
      foc=loc.firstChild.innerHTML?
         loc.firstChild:
         loc.firstChild.nextSibling;
      foc.innerHTML=foc.innerHTML=='+'?'-':'+';
      foc=loc.parentNode.nextSibling.style?
         loc.parentNode.nextSibling:
         loc.parentNode.nextSibling.nextSibling;
      foc.style.display=foc.style.display=='block'?'none':'block';}
}  


// Multibrowser text handler

function farsitext (keyCode, key) {

var farsikey = [	// Farsi keyboard map based on ASCII characters 
	0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x066A, 0x0026, 0x0027, 
	0x0029, 0x0028, 0x002A, 0x002B, 0x060C, 0x002D, 0x002E, 0x002F, 
	0x06F0, 0x06F1, 0x06F2, 0x06F3, 0x06F4, 0x06F5, 0x06F6, 0x06F7, 
	0x06F8, 0x0
