// Check the form before submitting
function CheckSearchForm(myForm)
{
	//Check for a word to search
	if (myForm.search.value == "")
	{
		alert("אנא הקלד לפחות מילה אחת לחיפוש");
		myForm.search.focus();
		return false;
	}
	return true;
}

$.fn.reverseOrder = function() {
	return this.each(function() {
		$(this).prependTo( $(this).parent() );
	});
};

function fixLtr(text){
	reg = /(\s|^)(\.?[a-zA-Z][a-zA-Z\-+_\'\"#;:0-9\.=]+)/g;
	return text.replace(reg, '$1<span dir=ltr>$2</span>');
}

function getMenuOffset(e){
	var offset = -7;
	offset -= parseInt($(e).css("paddingRight"));
	offset -= Math.max(0, $(document).width()-$(window).width());
	return offset;
}

var ziC = 10000;
$(document).ready(function(){
	$("#searchfrm .srchquery").height(16 + ($.browser.webkit ? 2 : 0));
	$("a.menuHeader").hover(function(){
		$(this).next().css('zIndex', ziC++).show();
		var offset = getMenuOffset(this);
		$(this).next().css('right', ($(document).width() - $(this).offset().left - $(this).width() + offset) + "px");
		$(this).next().hover(function(){
			$(this).show();
			$(this).parent().addClass("selected2");
			
		}, function(){
			$(this).hide().delay(200);
			$(this).parent().removeClass("selected2");
		});
	}, function(){
		$(this).next().hide().delay(200);
	});
	$(".menucategories > li > ul > li > div > a").add("ul.catTOC > li > a").add(".taglist a").add(".fixRTL").each(function(i, e){
		e=$(e);
		e.html(fixLtr(e.text()));
		e.removeClass('fixRTL');
	});
	if ($(".headerlogin").length > 0)
		$.ajax({ url: "/forum/uw_loginbox.asp", success: function(data){
			$(".headerlogin").html(data);
		  }});
	if ($("div.counter").length > 0)
		$.ajax({ url: "/counter.asp", success: function(data){
			$("div.counter").html(data);
		  }});
	$("table.XSS_PPCV_tblMain tr td a").click(function(){
		pageTracker._trackEvent("jobs", "click", "time:" + new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds());
	});
	if ($.browser.msie && $.browser.version < 8)
	{
		getMenuOffset = function(e){
			$(e).next().css("top", ($(e).offset().top + 26) + "px");
			return -13 - ($(e).parent().hasClass("first") ? 11 : 0);
		}
		if ($.browser.version == "6.0"){
			$("a.menuHeader").each(function(){sum=0;$(this).children().each(function(){sum+=$(this).width()});$(this).width(sum+2)});
		}
		$("ul.menucategories li ul li").css("float", "none").css('width', '100%');
		$("ul.menucategories li ul li div").each(function(){
			if($(this).css("float")!='none')
				$(this).css("float", "left").reverseOrder();
		});
		$("ul.menucategories li ul li strong").reverseOrder();
	}
});
jQuery.ie6Alert = function(mainText) {
    if($.browser.msie && $.browser.version=="6.0") {
        var links =
            '<div>התקינו: ' // install
            + '<a href="http://www.microsoft.com/ie8/">IE8</a> או '
			+ '<a href="http://www.mozilla.com/">Firefox</a> או '
            + '<a href="http://www.google.com/chrome/">Chrome</a>';
        var html = '<div class="ie6alert">' + mainText + links + '</div>'
        $("body").prepend(html)
        // add css to div.ie6alert
        $('.ie6alert')
            .css("color", "#D8000C")
            .css("border", "0")
            .css("border-bottom", "1px solid")
            .css("margin", "0")
            .css("padding", "1em 5em")
            .css("font", "normal 2em Helvetica, Arial, sans-serif")
            .css("text-align", "center")
            .css("background-color", '#FFBABA');
        // add css to div with links
        $('.ie6alert div')
            .css('margin-top', '10px')
            .css("font-size", "0.5em");
        // add css to link
        $('.ie6alert div a')
            .css('color', '#D8000C')
            .css('font-weight', 'bold')
            .css('letter-spacing', '0.1ex');
		$('.ie6alert')
			.hide()
			.slideDown();
    }
}

// start with

$(document).ready(function(){
    $.ie6Alert('IE6 הינו דפדפן מיושן משנת 2001...');
});
$(document).ready(function(){
	try{
		$(".documentHTML table").each(function(){
				var t=$(this);
				if (t.attr('width') && 1*t.attr('width') && 1*t.attr('width')>580)
					t.css('width', '100%')}
		);
		$(".document-left ul.doctoc").first().css('max-height', '250px').css('overflow-y', 'auto').css('padding-right', '4px').css('margin-right', '1px').scrollTop($("ul.doctoc strong").parent().offset().top - $(".document-left ul.doctoc").offset().top - 50);
	}catch(e){}
});

function recordEvent(category, action, label) {
  try {
    pageTracker._trackEvent(category, action, label);
  }catch(err){setTimeout('recordEvent("'+category+'","'+action+'","'+label+'")', 100);}
}

var fails = 0;

function recordLink(href, category, action){
	try{
		pageTracker._trackEvent(category, action);
		setTimeout('location.href = "' + href + '"', 200);
	}catch(err){
		if (fails < 8){
			++fails;
			setTimeout('recordLink("'+href+'","'+category+'","'+action+'")', 100);
		}else{
			location.href = href;
		}
	}
}

$(document).ready(function(){
	$("#homeTabs a").each(function(i,e){
		e.index=i;
		e.selectMe = function(){
			if ($(this).hasClass('selected'))
				return;
			$("#homeMainList").animate({'margin-right':(-544*this.index) + "px"}, 350);
			$("#homeTabs a").removeClass('selected');
			$(this).addClass('selected');
			g=$("a[name="+this.href.split('#')[1]+"]");
			g.css('position','absolute').css('top', $(document).scrollTop()+'px').css('left', '0');
			if (!$.browser.msie || $.browser.version > 8)
			{
				location.href=this.href;
			} else {
				ignoreLocation = true;
			}
		};
		$(e).click(function(){
			this.selectMe();
			return false;
		});
	});
	setInterval(function(){
		if (!$.browser.webkit && (!$.browser.msie || $.browser.version > 7))
		{
			$("#homeMainList").scrollLeft(0);
			$("#homeMainContainer").scrollLeft(0);
		} else {
			$("#homeMainContainer").scrollLeft($("#homeMainContainer")[0].scrollWidth-524);
		}
		if (!window.ignoreLocation)
		{
			var t = location.href.split('#')[1];
			t = t ? t : 'students';
			$("a." + t + "Tab").each(function(i,e){e.selectMe()});
		}
	}, 50);
});

function updateCourses(){
	if (!document.frmCourses)
		return;
	var faculty = document.frmCourses.faculty;
	var course  = document.frmCourses.course;
	if (typeof(courses) == 'undefined')
	{
		courses = new Array();
		for (var i = 0; i < course.options.length; ++i)
		{
			var o = course.options.item(i);
			courses.push({value: o.value, text: o.text, faculty: $(o).attr('faculty')});
		}
	}
	var oldvalue = course.value;
	if (!faculty.value)
	{
		while (course.options.length)
			course.remove(0);
		for (var i = 0; i < courses.length; ++i)
			course.options.add(new Option(courses[i].text, courses[i].value));
	} else {
		while (course.options.length)
			course.remove(0);
		for (var i = 0; i < courses.length; ++i)
			if ((!courses[i].faculty) || (courses[i].faculty == faculty.value))
				course.options.add(new Option(courses[i].text, courses[i].value));
	}
	course.value = oldvalue;
}

$(document).ready(function(){
	updateCourses();
});