String.prototype.equalsIgnoreCase = function(other) {return (this.toUpperCase() == other.toUpperCase());}
String.prototype.startsWith = function(text) {return (this.indexOf(text) == 0);}
String.prototype.contains = function(text) {return (this.indexOf(text) >= 0);}

$(document).ready(function() {
 	var content = $("#content");
	// make pretty round corners
	$(".round_border").corner("round 12px");
	// hide hidden things
	$('div.hidden').hide();
	// hide the error/success messages after a few seconds
	$('div.good,div.bad').each(function() {
		var div = $(this);
		if(div) window.setTimeout(function(){div.fadeOut('slow').slideUp('slow');},5000);
	});
	// change all external #content links to open in new windows
	content.find("a").each(function() {
		var href = $(this).attr("href");
		if(!href) return;
		if(href.startsWith("http://") && !href.contains("pmalu"))
			$(this).attr("target","_blank");
	});
	// make all greek span's into greek letters
	convertGreek(document);
	// enable all date inputs and set them to MYSQL dates
	if($("script[src*='datePicker']").attr("language") == "javascript") {
		Date.firstDayOfWeek = 7;
		Date.format = 'yyyy-mm-dd';		
		$("input.date_input").datePicker({startDate:'1970-01-01'});
	}
});

function convertGreek(location) {
	$(location).find("span.greek").each(function() {
	var span = $(this);
	var english = span.text();
	var letters = english.split(" ");
	var greek = "";
	for (var i = 0; i < letters.length; i++) {
		caps = letters[i].substring(0,1).toUpperCase();
    extra = letters[i].substring(1,letters[i].length);
    greek += "&" + caps + extra + ";";
	} span.html("<abbr title='"+english+"'>"+greek+"</abbr>");
	});
}

function convertLinks(location) {
	$(location).each(function() {
		var place = $(this);				
		place.html(place.html().replace(/\bhttp:\/\/[^ ]+\b/ig,"<a target='_blank' href='$&'>$&</a>"));
	});
}

function setCookie(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" +escape(value) +
	((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		} 
	} return "";
}
