function addNote(){ 
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	 	return;
	}
	var body = escape(document.getElementById('notebody').value);
	var subject = escape(document.getElementById('notesubject').value);
	var notelink = escape(document.getElementById('notelink').value);
	var linknote = document.getElementById('linknote').value;
	
	if ( document.getElementById('newnote_id') ){
		var note_id = document.getElementById('newnote_id').value;		
	} else {
		var note_id = document.getElementById('note_id').value;
	}
	
	var url="notehandler.php";
	url=url+'?subject='+subject+'&body='+body+'&link='+notelink+'&linknote='+linknote+'&note_id='+note_id+'&rand=' +Math.random();
	
	
	xmlHttp.onreadystatechange=function(){ stateChanged('notestatus'); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function addHomeNote(){ 
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	 	return;
	}
	var body = escape(document.getElementById('homenotebody').value);
	var subject = escape(document.getElementById('homenotesubject').value);
	var url="notehandler.php";
	url=url+'?subject='+subject+'&body='+body+'&action=homenote&rand='+Math.random();
	
	//alert(url);
	
	
	xmlHttp.onreadystatechange=function () { stateChanged('notestatus','noteprogress','home'); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function editHomeNote(note_id){ 
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	 	return;
	}
	var body = escape(document.getElementById('notebody_'+note_id).value);
	var subject = escape(document.getElementById('notesubject_'+note_id).value);
	var notelink = escape(document.getElementById('notelink_'+note_id).value);
	var linknote = document.getElementById('linknote_'+note_id).value;
	var url="notehandler.php";
	url=url+'?subject='+subject+'&body='+body+'&link='+notelink+'&linknote='+linknote+'&note_id='+note_id+'&action=homenote&rand=' +Math.random();
	
	
	xmlHttp.onreadystatechange=function() { editStateChanged(note_id,'notestatus'); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function deleteNote(note_id){ 
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	 	return;
	}
	
	var url="notehandler.php";
	url=url+'?action=delete&note_id='+note_id+'&rand=' +Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=function(){ deleteStateChanged(note_id,'note'); }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function deleteStateChanged(note_id,deltype){ 

	if ( deltype == 'bookmark' ){
		var thisObj = document.getElementById('bm_'+note_id);
	} else if ( deltype == 'notification'){
		var thisObj = document.getElementById('nt_'+note_id);
	} else {
		var thisObj = document.getElementById('note_'+note_id);
	}
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		remove_node(thisObj);
	} else {
		thisObj.innerHTML = '<img src="images/ajax-loader.gif">';	
	}
}


function stateChanged(status_target,progress_target,callfrom){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		if ( callfrom != 'home' ){
	
			document.getElementById(status_target).innerHTML=xmlHttp.responseText;
			if ( document.getElementById('note_img') && status_target != 'bookmarklink' ){
				document.getElementById('note_img').src = 'images/pg_noted.gif';	
				
			}
		} else {
			var curr = document.getElementById(status_target).innerHTML;
			document.getElementById(progress_target).innerHTML = '';
			document.getElementById(status_target).innerHTML=xmlHttp.responseText+curr;
			document.getElementById('homenotebody').value = '';
			document.getElementById('homenotesubject').value = '';
			document.getElementById('newnotes').style.display = 'none';
			
		}
	} else {
		if ( document.getElementById(progress_target) ){
			document.getElementById(progress_target).innerHTML = '<img src="images/ajax-loader.gif">';	
		}
	}
		
}

function editStateChanged(note_id,content_target){ 

	var table = document.getElementById('note_'+note_id);
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		remove_node(table);
		var curr = document.getElementById(content_target).innerHTML
		document.getElementById(content_target).innerHTML=xmlHttp.responseText+curr;
	} else {
		table.innerHTML = '<img src="images/ajax-loader.gif">';	
	}
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	}catch (e){
		 //Internet Explorer
		 try{
		 	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e){
		 	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		 }
	}
	return xmlHttp;
}

function addBookmark(lnk){
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	 	return;
	}
	var url="notehandler.php";
	url=url+'?action=bookmark&link='+escape(lnk)+'&rand=' +Math.random();
	xmlHttp.onreadystatechange=function(){ stateChanged('bookmarklink','bookmarklink'); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function remove_node(node){
	if(node.removeNode){		node.removeNode(true);
	}else{
		for(var i=node.childNodes.length-1;i>=0;i--){			remove_node(node.childNodes[i]);
		    node.parentNode.removeChild(node);
		}
	}return null;
}

function deleteBookmark(note_id){
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	 	return;
	}
	
	var url="notehandler.php";
	url=url+'?action=delete&note_id='+note_id+'&rand=' +Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=function(){ deleteStateChanged(note_id,'bookmark'); }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function deleteNotification(note_id){
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
	 	return;
	}
	
	var url="notehandler.php";
	url=url+'?action=delete&note_id='+note_id+'&rand=' +Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=function(){ deleteStateChanged(note_id,'notification'); }
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function progress_button(tgt,ptype){
	btn = document.getElementById(tgt);
	btn.disabled = true;
	if ( ptype == 'update' ){
		btn.value = 'Updating';
	} else {
		btn.value = 'Saving';
	}	
}