
var filmFrame = document.getElementById("todaysFilm");
filmFrame.style.verticalAlign = "top";
var salUrl = "/cgi-webaxy/sal/sal.pl?ID=839468_cinematheque&act=search2&dbResOptions=hideResTabTitle=1&getValueAJAX=dataid,f1,f2,f3,f4&dbid=shows2009&query=sort<D>f3<D>sort2<D>f4<D>f2<D>%2b0<D>f2<D>%2b0";

//-----------------------------
function get_cookie(Name) {
  		var search = Name + "=";
  		if (document.cookie.length > 0) { // if there are any cookies
			 offset = document.cookie.indexOf(search);
 			 if (offset != -1) { // if cookie exists 
    			offset += search.length ;  // set index of beginning of value
   				end = document.cookie.indexOf(";", offset); // set index of end of cookie value
    			if (end == -1) 
       					end = document.cookie.length;
    			return decodeURIComponent(document.cookie.substring(offset, end))
		 	}
  		}
}

//----------------------------------------------------------------------
function set_cookie(cname,cvalue,expire) {
	document.cookie  = cname+"="+encodeURIComponent(cvalue)+"; path=/"+((expire==null) ? "" : (";expires="+expire.toGMTString()));
}

//===================================================================
//  
//  setFilms: get array and create films table 
//  
//===================================================================
function setFilms(filmsString) {
	while (filmFrame.firstChild) {
		filmFrame.removeChild(filmFrame.firstChild);
	}
	set_cookie("todaysFilmsString",filmsString);
	var fieldsNum = 6;	// dataid,f1,f1_title,f2,f3,f4
	var films = filmsString.split("<D>");
	var today = "";
	if (films.length > fieldsNum + 3) {
		today = films[fieldsNum + 3];
		/*
		today.search(/(\d\d\d\d)\-?(\d\d)\-?(\d\d)/);
		today = (RegExp.$3 - 0)+"/"+(RegExp.$2 - 0)+"/"+RegExp.$1;
		*/
	}
	else {
		var d = new Date();
		today = d.getDate() + "/" + (d.getMonth() + 1) + "/"+d.getFullYear();
	}
	
	var table = document.createElement("table");
	table.className = "filmsTitleTable";
	var newRow = table.insertRow(-1);
	var newTd = newRow.insertCell(-1);
	newTd.style.textAlign = "right";
	var cellText = "סרטי היום";
	newTd.appendChild(document.createTextNode(cellText));
	newTd = newRow.insertCell(-1);
	newTd.style.textAlign = "left";
	newTd.appendChild(document.createTextNode(today));
	filmFrame.appendChild(table);
	
	if (films.length == 0) {
		alert('no movies');
		return;
	}
	table = document.createElement("table");
	table.className = "filmsTable";
	for (var f = 0; f < films.length; f+=fieldsNum) {
		filmDataid = films[f+1];
		filmTitle = films[f+2];
		filmDate =  films[f+3];
		filmHour =  films[f+4];
		filmHall =  films[f+4];
		newRow = table.insertRow(-1);
		newRow.style.verticalAlign = "top";
		newTd = newRow.insertCell(-1);
		newTd.className = "filmsHourTd";
		newTd.appendChild(document.createTextNode(filmHour));
		newTd = newRow.insertCell(-1);
		filmTitleLink = document.createElement("a");
		filmTitleLink.appendChild(document.createTextNode(filmTitle));
		//filmTitleLink.setAttribute("target","_blank");
		filmTitleLink.href = "/cgi-webaxy/sal/sal.pl?ID=839468_cinematheque&dbid=movies2009&act=show3&dataid="+filmDataid;
		filmTitleLink.className = "filmsLink";
		newTd.appendChild(filmTitleLink);
		
	}
	filmFrame.appendChild(table);
}

//getAJAX(salUrl,setFilms);  
if (get_cookie("todaysFilmsString")) {
	setFilms(get_cookie("todaysFilmsString"));
}
else {
	getAJAX(salUrl,setFilms);                                                                                                                                                                
}
