	function blink (element, newClass)
    {
    	element.setAttribute('class',newClass);               
		element.className = newClass;               
	}   
	function unBlink (element, newClass)
	{
    	element.setAttribute('class',newClass);               
		element.className = newClass;               
	}
	function show (id)
	{
    	document.getElementById (id).removeAttribute('class');               
    	document.getElementById (id).className = "";
	}
	function hide (id)
	{
    	document.getElementById (id).setAttribute('class','menuHidden');               
    	document.getElementById (id).className = "menuHidden";               
	}

	function showOrders (prefix, start)
	{
		var index = 0;
		var ordObject;
		while ((ordObject = document.getElementById (prefix + index)) != null)
		{
			if (index < start)
			{
				hide(prefix + index);
			}
			else if (index > start +2)
			{
				hide(prefix + index);
			}
			else
			{
				show(prefix + index);
			}
			index ++;
		}
		return false;
	}

	function post (url, params, postFunction)
	{
		try
		{
		   var http = null
		
		   if (window.XMLHttpRequest) {
		      http = new XMLHttpRequest()
		   } else if (window.ActiveXObject) {
		      http = new ActiveXObject("Microsoft.XMLHTTP")
		   }

			http.open("POST", url, true);
	
			http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Content-length", params.length);
			http.setRequestHeader("Connection", "close");
			
			http.onreadystatechange = function() {//Call a function when the state changes.
			    if(http.readyState == 4 && http.status == 200) {
			    	eval(postFunction);
	    		}
			}
			http.send(params);
		}
		catch (ex){alert('error '+ex);return true;}
		return false;
	}
	
	function encode (param)
	{
		return encodeURIComponent(param);
	}
	
	function getParams (formNode)
	{
		var params = '';
		for (i = 0 ; i < formNode.getElementsByTagName('input').length; i++)
		{
			params = params + formNode.getElementsByTagName('input')[i].name + '=' + encode(formNode.getElementsByTagName('input')[i].value)+ '&';
		}
		for (i = 0 ; i < formNode.getElementsByTagName('textarea').length; i++)
		{
			params = params + formNode.getElementsByTagName('textarea')[i].name + '=' + encode(formNode.getElementsByTagName('textarea')[i].value)+ '&';
		}
		return params;
	}
	
	function funzioneUpdates(page) {
	try
	{
	   var http_request = null
	   var jsonUpdates = null;
	
	   if (window.XMLHttpRequest) {
	      http_request = new XMLHttpRequest()
	   } else if (window.ActiveXObject) {
	      http_request = new ActiveXObject("Microsoft.XMLHTTP")
	   }
	
	   http_request.onreadystatechange = function() {
	      if ( http_request.readyState == 4 ) {
	         if ( http_request.status == 200 ) {
	
	            jsonUpdates = eval( "(" + http_request.responseText + ")" );

	            var pageNumber = document.getElementById('pageNumber');
	            var pageNumberList = pageNumber.childNodes;
		        for (i = pageNumberList.length-1 ; i > -1; i--)
		        {
					pageNumber.removeChild(pageNumberList[i]);
				}
				pageNumber.appendChild (document.createTextNode(jsonUpdates.page));
			
	        for (i = 0 ; i < jsonUpdates.videos.length; i++)
	        {
	        	show('videoPreview_'+i);
	            var videoPos = document.getElementById('videoPreview_'+i);
	            var rows = videoPos.getElementsByTagName('div');
	            rows[0].firstChild.firstChild.setAttribute('src',jsonUpdates.videos[i].Preview);
	            rows[0].firstChild.setAttribute('href',jsonUpdates.videos[i].Link);
	            rows[2].firstChild.firstChild.nodeValue=jsonUpdates.videos[i].Title;
	            rows[2].firstChild.setAttribute('href',jsonUpdates.videos[i].Link);
	            rows[3].firstChild.nodeValue = jsonUpdates.videos[i].Description;
	            rows[4].firstChild.nodeValue = jsonUpdates.videos[i].Views;
	            rows[5].firstChild.nodeValue = jsonUpdates.videos[i].Durata;
	            rows[6].getElementsByTagName('a')[0].setAttribute('href','../view/start.action?inserzionista='+jsonUpdates.videos[i].Owner);
	            rows[6].getElementsByTagName('a')[0].firstChild.nodeValue=jsonUpdates.videos[i].Owner;

	            var menuPos = document.getElementById('authorMenu_'+i);
	            var menuRows = menuPos.getElementsByTagName('div');
	            if (jsonUpdates.videos[i].UserImage == '')
	            {
	            	menuRows[0].getElementsByTagName('img')[0].removeAttribute('class');               
	            	menuRows[0].getElementsByTagName('img')[0].className = "";
	            }
	            else
	            {
	            	menuRows[0].getElementsByTagName('img')[0].setAttribute('class','menuHidden');               
	            	menuRows[0].getElementsByTagName('img')[0].className = "menuHidden";               
	            }
	            menuRows[0].getElementsByTagName('img')[0].setAttribute('src',jsonUpdates.videos[i].UserImage);
	            menuRows[1].getElementsByTagName('a')[0].setAttribute('href','../view/start.action?inserzionista='+jsonUpdates.videos[i].Owner);
	            menuRows[2].getElementsByTagName('a')[0].setAttribute('href','../view/showAccount.action?userId='+jsonUpdates.videos[i].OwnerId);

	        }
	        for (i = jsonUpdates.videos.length ; i < 4; i++)
	        {
	        	try
	        	{
	        		hide('videoPreview_'+i);
	        	}
	        	catch (ex){}
	        }
	        
	            http_request = null;
	         }
	      }
	   };
	
	   http_request.open( 'POST', './'+page+'.action', true );
	   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   http_request.setRequestHeader("Content-length", 0);
	   http_request.send(null);
	   }catch (ex)
	   {alert('error '+ex);return true;}
	   return false;
	}




//	function updateCommenti(videoId) {
//	try
//	{
//	   var http_request = null
//	   var jsonUpdates = null;
//	
//	   if (window.XMLHttpRequest) {
//	      http_request = new XMLHttpRequest()
//	   } else if (window.ActiveXObject) {
//	      http_request = new ActiveXObject("Microsoft.XMLHTTP")
//	   }
//	
//	   http_request.onreadystatechange = function() {
//	  
//	      if ( http_request.readyState == 4 ) {
//	         if ( http_request.status == 200 ) {
//	
//	            jsonUpdates = eval( "(" + http_request.responseText + ")" );
//	            var commentiPos = document.getElementById('commenti');
//	            var commentiList = commentiPos.childNodes;
//		        for (i = commentiList.length-1 ; i > -1; i--)
//		        {
//					commentiPos.removeChild(commentiList[i]);
//				}
//		        for (i = 0 ; i < jsonUpdates.commenti.length; i++)
//		        {
//		        	
//		        	var commento = document.createElement('div');
//		        	commento.setAttribute('class','commento');
//		        	var inserzionista = document.createElement('div');
//		        	inserzionista.setAttribute('class','inserzionista');
//		        	inserzionista.appendChild (document.createTextNode('Il '+jsonUpdates.commenti[i].Inserimento+' '));
//		        	var inserzionistaLink = document.createElement('a');
//		        	inserzionistaLink.setAttribute('href','../view/start.action?inserzionista='+jsonUpdates.commenti[i].Inserzionista);
//		       		inserzionistaLink.appendChild(document.createTextNode(jsonUpdates.commenti[i].Inserzionista));
//		        	inserzionista.appendChild (inserzionistaLink);
//		        	inserzionista.appendChild (document.createTextNode(' ha detto:'));
//		        	
//					var testoCommento = document.createElement('div');
//					testoCommento.appendChild (document.createTextNode(jsonUpdates.commenti[i].Commento));
//		        	
//		        	commento.appendChild(inserzionista);
//		        	commento.appendChild(testoCommento);
//
//		        	commentiPos.appendChild(commento);
//		        }
//				
//	        
//	            http_request = null;
//	         }
//	      }
//	   };
//	
//	   http_request.open( 'POST', '../noteManagement/updNoteView.action?videoId='+videoId, true );
//	   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//	   http_request.setRequestHeader("Content-length", 0);
//	   http_request.send(null);
//	   }catch (ex)
//	   {alert('error '+ex);return true;}
//	   return false;
//	}
//	
//	var obj = null;
//	function setMouseMoveElement (element)
//	{
//		obj = document.getElementById (element);
//	}
	
//	var starPosition;
//	function mosso (e)
//	{
//		var frame = 24;
//		starPosition = parseInt((e.pageX-findPosX(obj)+frame/2)/frame);
//		obj.setAttribute('width',starPosition*frame+'px');
//		obj.style.width = starPosition*frame+'px';
//	}		
	
//	function selectedStar (videoId , id )
//	{
//		var frame = 24;
//		post ('../voteManagement/addVote.action','videoId='+videoId+'&votazioneId='+id+'&voto='+starPosition,'changeVote('+videoId+','+id+')');
//	}
	
//	function changeVote (videoId,id)
//	{
//		hide('stars_'+id);
//		updateVote(videoId);
//	}

	function updateVote(videoId) {
	try
	{
	   var http_request = null
	   var jsonUpdates = null;
	
	   if (window.XMLHttpRequest) {
	      http_request = new XMLHttpRequest()
	   } else if (window.ActiveXObject) {
	      http_request = new ActiveXObject("Microsoft.XMLHTTP")
	   }
	
	   http_request.onreadystatechange = function() {
	  
	      if ( http_request.readyState == 4 ) {
	         if ( http_request.status == 200 ) {
	
	            jsonUpdates = eval( "(" + http_request.responseText + ")" );
		        for (i = 0 ; i < jsonUpdates.Votes.length; i++)
		        {
		        	var votazione = document.getElementById('votazione_'+jsonUpdates.Votes[i].Id);
		        	
		        	var votazioneList = votazione.childNodes;
			        for (l = votazioneList.length-1 ; l > -1; l--)
			        {
						votazione.removeChild(votazioneList[l]);
					}		        	
		        	votazione.appendChild (document.createTextNode(jsonUpdates.Votes[i].Nome+' '+jsonUpdates.Votes[i].VotoMedio+' - votato in '+jsonUpdates.Votes[i].Votanti));
		        	if (jsonUpdates.Votes[i].Votanti != '')
		        	{
		        		show ('votazione_'+jsonUpdates.Votes[i].Id);
		        	}
		        }
	        
	            http_request = null;
	         }
	      }
	   };
	
	   http_request.open( 'POST', '../voteManagement/addVoteAjax.action?videoId='+videoId, true );
	   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   http_request.setRequestHeader("Content-length", 0);
	   http_request.send(null);
	   }catch (ex)
	   {alert('error '+ex);return true;}
	   return false;
	}

		
	function findPosX(obj)
	{
		var curleft = 0;
		if(obj.offsetParent)
		while(1) 
		{
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
		else if(obj.x)
			curleft += obj.x;
		return curleft;
	}

	function findPosY(obj)
	{
		var curTop = 0;
		if(obj.offsetParent)
		while(1) 
		{
			curTop += obj.offsetTop;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
		else if(obj.y)
			curTop += obj.y;
		return curTop;
	}

	
	function addAutorePreferito()
	{
		hide('addAutorePreferito');
		show('remAutorePreferito');
		return true;
	}		

	function remAutorePreferito()
	{
		hide('remAutorePreferito');
		show('addAutorePreferito');
		return true;
	}		

	function remVideoPreferito()
	{
		hide('remVideoPreferito');
		show('addVideoPreferito');
		return true;
	}
	
	function showAuthorMenu(obj, menuId)
	{
		show (menuId);
		var authorMenu = document.getElementById (menuId);
		authorMenu.setAttribute('left',(findPosX(obj)+50)+'px');
		authorMenu.style.left  = (findPosX(obj)+50)+'px';
		authorMenu.setAttribute('top',(findPosY(obj)+15)+'px');
		authorMenu.style.top  = (findPosY(obj)+15)+'px';
	}

	function updateView(videoId,time,completed) {
	try
	{
	   var http_request = null
	   var jsonUpdates = null;
	
	   if (window.XMLHttpRequest) {
	      http_request = new XMLHttpRequest()
	   } else if (window.ActiveXObject) {
	      http_request = new ActiveXObject("Microsoft.XMLHTTP")
	   }
	
	   http_request.onreadystatechange = function() {};
	
	   http_request.open( 'POST', '../videoManagement/updVideoCounter.action', true );
	   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   http_request.setRequestHeader("Content-length", 0);
	   http_request.send('videoId='+videoId+'&time='+time+'&completed='+completed);
	   }catch (ex)
	   {alert('error '+ex);return true;}
	   return false;
	}
	
	function changeImage(videoId,time) {
	try
	{
	   var http_request = null
	   var jsonUpdates = null;
	
	   if (window.XMLHttpRequest) {
	      http_request = new XMLHttpRequest()
	   } else if (window.ActiveXObject) {
	      http_request = new ActiveXObject("Microsoft.XMLHTTP")
	   }
	
	   http_request.onreadystatechange = function() {};
	
	   http_request.open( 'POST', '../videoManagement/updVideoImageConfirm.action', true );
	   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   http_request.setRequestHeader("Content-length", 0);
	   http_request.send('id='+videoId+'&timePosition='+time);
	   }catch (ex)
	   {alert('error '+ex);return true;}
	   return false;
	}

	function refreshSession() {
		try
		{
		   var http_request = null
		   var jsonUpdates = null;
		
		   if (window.XMLHttpRequest) {
		      http_request = new XMLHttpRequest()
		   } else if (window.ActiveXObject) {
		      http_request = new ActiveXObject("Microsoft.XMLHTTP")
		   }
		
		   http_request.onreadystatechange = function() {};
		
		   http_request.open( 'POST', '../accountManagement/refreshSession.action', true );
		   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		   http_request.setRequestHeader("Content-length", 0);
		   http_request.send('');
		   }catch (ex)
		   {alert('error '+ex);return true;}
		   
		   setTimeout("refreshSession();",60000);			   
		   return false;
	}

