var logHeb = HasLogicalHebrew();

function HasLogicalHebrew() {
	var bName = navigator.appName.toLowerCase();
	var bAgent = navigator.userAgent.toLowerCase();
	
	var ret = (bName.indexOf("microsoft") != -1) && (bAgent.indexOf("win") != -1);
	return ret;
}

var logos = new Array();
logos["qa"] = "<IMG SRC=\"../icondir/banners/banner-qa.jpg\" ALT=\"[Q&A]\" WIDTH=400 HEIGHT=100 BORDER=0>";
logos["mara"] = "<img src=\"../icondir/banners/banner-mara.jpg\" ALT=\"[Site Rabbi]\" width=400 height=106 border=0>";
logos["korn"] = '<img src="../icondir/banners/banner-qa_kornfein.jpg" alt="[Itsik Kornfein Q&amp;A page]" width=400 height=100>';

function writeAskHeader(param) {
	var str = logos[param];
	if (str)
		document.write('<CENTER>'+str+'</CENTER><BR>');
}

function Reverse(str) {
	var ret = "";
	for (i = str.length - 1; i >= 0; i--) {
		ret += str.charAt(i);
	}
	return ret;
}

var qTypes = new Array();
var qAuthors = new Array();
var gSeparator = "---";

var theIconDir = "../icondir/qa/";
var dimstr = " width=50 height=50 border=0 align=right";

function makeType(inName, inCode, inImage) {
	this.iName = inName;
	this.iImage = inImage;
	this.iCode = inCode;
	return this;
}

function makeAuthor(inName, inCode, startCat, endCat) {
	this.iName = inName;
	this.iCode = inCode;
	this.iStartCat = startCat;
	this.iEndCat = endCat;
	return this;
}

function addAuthor(inName, inCode, startCat, endCat) {
	var src = new makeAuthor(inName, inCode, startCat, endCat);
	qAuthors[qAuthors.length] = src;
}

function addType(inName, inCode, inImage) {
	var src = new makeType(inName, inCode, inImage);
	qTypes[qTypes.length] = src;
}

addType("יללכ", 0, "qa_btn.gif,50,50");
addType("היווירט",1,"trivia_btn.gif,50,50");
addType("הירוטסה",2, "history_btn.gif,50,50");
addType("היגולואידיא",3,"ideology_btn.gif,50,50");
addType("הקיטקט",4,"tactics_btn.gif,50,50");
addType("חאסכ",5,"ksh_btn.gif,50,50");
addType("הלהנה", 6, "qa_btn.gif,50,50");
addType("םידהוא", 7, "qa_btn.gif,50,50");
addType("הנטק הצע", 7, "qa_btn.gif,50,50");
addType(gSeparator, -1,"");

addType("יללכ",50,"lamp.gif,56,67");
addType("הנומא",51,"hamsa.gif, 81,100");
addType("השעת לא/השע",52,"reaper.gif, 85,85");

addType("יעוצקמ",60,"glove.gif,50,70");
addType("ישיא",61,"glasses.gif,70,57");
addType("יללכ",62,"tiger.gif,100,67");

addAuthor("רתאה תווצ", 1, 0, 10000);
addAuthor("הטוש דיסח", 2, 0, 9);
addAuthor("ךורב",3,0, 0);
addAuthor("יחצ",4, 0, 0);
addAuthor("טרמלוא לואש",5, 0, 9);
addAuthor("רתאה בר",80, 50, 59);
addAuthor("ןייפנרוק קיציא",100, 60, 69);


function findRecordByCategory(theCat) {
	var theRec = null;
	for (i in qTypes) {
		theRec = qTypes[i];
		if (theRec && (theRec.iCode == theCat))
			return theRec;		
	}
//	alert("could not find category "+theCat);
	return qTypes[0];
}

function findRecordByAuthor(theAuthor) {
	var theRec;
	for (i in qAuthors) {
		if (qAuthors[i].iCode == theAuthor)
			return qAuthors[i];
	}
	return qAuthors[0];
}

function writeTypes(theOption, startCat, endCat) {
	var theRec;
	for (i in qTypes) {	
		theRec = qTypes[i];
		if (theRec.iCode >= startCat && theRec.iCode <= endCat) {
			document.write("<option value="+theRec.iCode);
			if (theRec.iCode == theOption)
				document.write(" selected");
			document.write(">");
			if (logHeb)
				document.write(Reverse(theRec.iName));
			else
				document.write(theRec.iName);
		}
	}
}

function writeAuthors(theOption, startCat, endCat) {
//	alert("start cat is "+startCat+" and endcat is "+endCat+" author is "+theOption);
	var theAuth;
	theOption = parseInt(""+theOption);
	for (i in qAuthors) {
		theAuth = qAuthors[i];
		if (startCat >= theAuth.iStartCat && endCat <= theAuth.iEndCat) {
			document.write("<option value="+theAuth.iCode);
			if (theAuth.iCode == theOption)
				document.write(" selected");
			document.write(">");
			if (logHeb)
				document.write(Reverse(theAuth.iName));
			else
				document.write(theAuth.iName);
		}
	}
}

function IsHebrew(inString) {
	if (! inString)
		return false;

	var len = inString.length;
	if (len > 7)
		len = 7;
	for (i = 0; i < len; i++) {
		if (inString.charCodeAt(i) > 185)
			return true;
	}
	return false;
}

function writeQuestion( theDay, theMonth, theYear, theCategory, theTitle, theQuestion, theAnswer, theFrom, theAuthor, theGreet) {
	var catRec = findRecordByCategory(theCategory);
	var authRec = findRecordByAuthor(theAuthor);
	var theDate = makeStringFromDate(theDay, theMonth, theYear);
	var hebQ = IsHebrew(theQuestion);
	var hebA = IsHebrew(theAnswer);
	var hebFrom = IsHebrew(theFrom);
	var theImage, theWidth, theHeight;
	var theArr;
	
	if (theGreet == null || theGreet.length == 0) {
		theGreet = 'ר"תיב הללאי תכרבב';
	}
	
	with (document) {
		theArr = catRec.iImage.split(",");
		theImage = theArr[0];
		theWidth = parseInt(theArr[1]);
		theHeight = parseInt(theArr[2]);

		write ("<table border=1 cellspacing=0 cellpadding=2>");
		write("<tr valign=top><td align=left bgcolor=black><font color=white>");
		write(theDate);
		write ("<BR><font class=smallFont><B>"+catRec.iName+"</B> :הירוגטק</font></font>");
		write("</td><td align=right bgcolor=#ffff99 width="+(theWidth+10)+"><IMG align=right SRC=");
		write (theIconDir+theImage); // 
		write (" WIDTH="+theWidth+" HEIGHT="+theHeight+" BORDER=0 hspace=5></td></tr>");
		write ("<tr><td align=");
		if (hebQ)
			write("right nowrap");
		else
			write("left width=500");
		write(" colspan=2 bgcolor=white>");
		if (theFrom && theFrom.length) {
			if (hebFrom)
				write(":");
			write("<B>"+theFrom+"</B><BR>");
			if (! hebFrom)
				write(":");
		}
		write (theQuestion);
		write("<tr><td align=right colspan=2 nowrap background=\"../icondir/backgrounds/grad-yaa.gif\"><table><tr><td align=");
		if (hebA)
			write("right nowrap>");
		else
			write("left width=500>");
		write (theAnswer);
		write ("<BR></td></tr><tr><td align=left>&nbsp;&nbsp;&nbsp;<table><tr><td align=right nowrap>"+theGreet+"<BR><B>"+authRec.iName+"</B></td></tr></table></td></tr></table></td></tr></table>");
	}
}

function writeQANavBar(prevName, nextName, submitURL) {
	with (document) {
		write ("<CENTER><table cellpadding=0 cellspacing=0 border=0><tr valign=middle>");
		write ('<td><IMG SRC=../icondir/backgrounds/navbar_left.gif WIDTH=11 HEIGHT=30 BORDER=0></td>');
		write('<td background=../icondir/backgrounds/navbar_bg.gif><table cellpadding=0 cellspacing=3 border=0><tr valign=middle>');

		if (nextName && nextName.length > 0) {
			write("<td><a href="+nextName+"><IMG SRC=../icondir/buttons/phand_left.gif WIDTH=41 HEIGHT=22 BORDER=0></a></td>");
			write("<td> <a href="+nextName+" class=smallLink>םדוקה דומעל</a>&nbsp;&nbsp;</td>");
		}

		if (submitURL && submitURL.length > 0)
			write('<td align=center bgcolor=#ffffdd><a href="'+submitURL+'">הלאש תשגה</a></td>');
		if (prevName && prevName.length > 0) {
			write("<td>&nbsp;&nbsp;<a href="+prevName+" class=smallLink>אבה דומעל</a></td>");
			write("<td> <a href="+prevName+"><IMG SRC=../icondir/buttons/phand_right.gif WIDTH=41 HEIGHT=22 BORDER=0></a></td>");
		}
		write ('</tr></table></td>');
		write("<td><IMG SRC=../icondir/backgrounds/navbar_right.gif WIDTH=11 HEIGHT=30 BORDER=0></td></tr></table></CENTER><BR>");
	}
}

