function ajaxFunction(){
	var ajaxRequest = null;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch(e){
			// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					//alert("Your browser broke!");
				}
			}
		}
		return ajaxRequest;
	}

/*************************************/

function getXhrObject(method,scriptFileName,data,synch){
		var xhr_object = ajaxFunction();
		if(xhr_object == null){
			alert("Your browser don't support XMLHTTPRequest objects...");
		   return;
		}
		if(method == "POST" && data != null) {
			scriptFileName += "?"+data;
		}
		xhr_object.open(method, scriptFileName, synch);
		
		if(method == "POST") {
			   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			   xhr_object.setRequestHeader("Content-length", data.length);
			   xhr_object.setRequestHeader("Connection", "close");
		}
		return xhr_object;
}

/*************************************/
function editNews(newsId,page,currentPage,searchStr,searchBy){
 var urlStr = "news_editOneNews.jsp?newsid="+newsId+"&page="+page;
 if((page=='showall')||(page=='search'))
	urlStr+= "&p="+currentPage;
 if(page=='search')
	 urlStr+="&searchstr="+searchStr+"&searchby="+searchBy;
 openDialog(urlStr, "Edit News", 820, 461, null, false,false);
}
/*************************************/
function removeNews(newsId,page,currentPage,searchStr,searchBy){
	resp = window.confirm("this will delete definitively the news "+newsId+". Do you want to continue?");
 	if(resp){
 		var urlStr="news_removeNews.jsp";
 		var data= "newsid="+newsId;
 		var xhr_object = getXhrObject("POST",urlStr,data,true);
 		xhr_object.send(data);
 		xhr_object.onreadystatechange = function() {
 			if(xhr_object.readyState == 4){
 				if(xhr_object.status == 200){
 					if(page=="showall"){
 						hide("dlg");
 						var urlStr= "news_showAllNews.jsp?p="+currentPage;
 						loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
 					}
 					if(page=="search"){
 						hide("dlg");
 						var urlStr= "news_search_results.jsp?p="+currentPage+"&searchstr="+searchStr+"&searchby="+searchBy;
 						loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
 					}
 					if(page=="main")
 						window.location.reload();
 				} 
 		}
 	};
 	return true;
  } else
	  return false;
}
/*************************************/
function updateNews(newsId,page,currentPage,searchStr,searchBy){
	var urlStr="news_updateNews.jsp";
	var title = byId("newsedittitle").value.trim();
	var body = byId("newseditbody").value.trim();
	
	title = encodeURIComponent(title.unicodeEncode());
	body = encodeURIComponent(body.unicodeEncode());
	
	var data= "newsid="+newsId+"&title="+title+"&body="+body;
	Request.send(urlStr, "POST", data, function(response){
		if(page=="showall"){
			hide("dlg");
			var urlStr= "news_showAllNews.jsp?p="+currentPage;
			loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
		}
		if(page=="search"){
			hide("dlg");
			var urlStr= "news_search_results.jsp?p="+currentPage+"&searchstr="+searchStr+"&searchby="+searchBy;
			loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
		}
		if(page=="main")
			window.location.reload();
	});
}
/********************************/
function newsGotoPage(currentPage,nbPage,page,searchStr,searchBy){
	var cPage = byId("gotopage").value;
	var urlStr="";
	if(cPage>0 && cPage<=nbPage){
		if(page=="showall")
			urlStr= "news_showAllNews.jsp?p="+cPage;
		if(page=="search")
			urlStr= "news_search_results.jsp?p="+cPage+"&searchstr="+searchStr+"&searchby="+searchBy;;
		loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
	}
	else{
	 	alert("bad page number");
	 	byId("gotopage").value = currentPage;
	}
}
/*********************************************/
function newsPrevPage(currentPage,page,searchStr,searchBy){
	alert(currentPage+"--"+page+"--"+searchStr+"--"+searchBy);
	var urlStr="";
	if(page=="showall")
		urlStr= "news_showAllNews.jsp?p="+currentPage;
	if(page=="search")
		urlStr= "news_search_results.jsp?p="+currentPage+"&searchstr="+searchStr+"&searchby="+searchBy;;
	loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
}
function newsNextPage(currentPage,page,searchStr,searchBy){
	alert(currentPage+"--"+page+"--"+searchStr+"--"+searchBy);
	var urlStr="";
	if(page=="showall")
		urlStr= "news_showAllNews.jsp?p="+currentPage;
	if(page=="search")
		urlStr= "news_search_results.jsp?p="+currentPage+"&searchstr="+searchStr+"&searchby="+searchBy;;
	loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
}
function gotoPage(gotopage, page,searchStr,searchBy){
	byId("gotopage").value = gotopage;
	if(page=="showall")
		urlStr= "news_showAllNews.jsp?p="+gotopage;
	if(page=="search")
		urlStr= "news_search_results.jsp?p="+gotopage+"&searchstr="+searchStr+"&searchby="+searchBy;;
	loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
}
/*********************************************/
function addNews(page){
	var urlStr = "news_addANews.jsp?p="+page;
	 openDialog(urlStr, "Add News", 820, 461, null, false,false);
	
}
/***********************************/
function saveAddedNews(page){
	var title= byId("newsaddtitle").value.trim();
	if(title==""){
	   alert("Can't save. Title is empty...");
	   return false;
	}
	var body= byId("newsaddbody").value.trim();
	if(body==""){
	   alert("Can't save. Body is empty...");
	   return false;
	}
	
	title = encodeURIComponent(title.unicodeEncode());
	body = encodeURIComponent(body.unicodeEncode());
	
	var urlStr="news_addNews.jsp";
	var data= "title="+title+"&body="+body;
	
	Request.send(urlStr, "POST", data, function(response){
		if(page=='news'){
			hide("dlg");
			var urlStr= "news_showAllNews.jsp?p=1";
			window.close();
			loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
		}
		if(page=='main')
			window.location.reload();
	});
	return true;
}
/*****************************************************************/
function newsSearchResults(){
	var searchStr= byId("searchnews").value.trim();
	if(searchStr==""){
		alert("search string is empty!!!");
		return false;
	}
	
	var e = document.getElementById("select_search");
	var searchBy = e.options[e.selectedIndex].value;
	if(searchBy=='select'){
		alert("please, select an option to search by!!!");
		return false;
	}
	var urlStr= "news_search_results.jsp?searchstr="+searchStr+"&searchby="+searchBy;
	loadContent(urlStr,'all_news_and_seach_container','all_news_and_seach_content','GET','','');
	return true;
}
