

counter=0;
var ajaxuid;
var asset_vid;
var asset_vtype;
var tab_vname;

function setNoteData() {
		//alert("IN SET NOTE DATA ");
		//pass ajax data for note - top,left,width,height, body

		callMetrics('edit');

		asset_vid = assetId;
		asset_vtype = assettype;
		if(tabname!=null)
		tab_vname = tabname;
		else
		tab_vname="";

		var currId=DL_oLayer.id.substring(12);
		if(currId!="" && currId!=null){
			
		var noteBody = DL_oLayer.getElementsByTagName('textarea')[0].value;

		if(noteBody!=null && noteBody!=""){
		var left = DL_oLayer.style.left;
		tempyTop = 1; 
		var top = parseFloat(DL_oLayer.style.top) * 1 + tempyTop*1;
		var top = top + "px";
		DL_oLayer.getElementsByTagName('input')[1].value=top;
		var width = DL_oLayer.style.width;
		var url=location.href;
		var height = DL_oLayer.style.height;
		var actiontype="update"
		var webid=0;
		var noteid=DL_oLayer.id.substring(12);
		var i=0;
		webid=noteid;
		// Commented to change request to post start on 3 Nov 08
		//var ajaxurl="../../Webnote_Servlet?web_id="+webid+"&asset_id="+escape(assetId)+"&asset_type="+escape(assettype)+"&tab_name="+escape(tabname)+"&note_txt="+escape(noteBody)+"&title=TITLE&screen_x="+left+"&screen_y="+top+"&height="+height+"&width="+width+"&display_flag=Y&active=Y&action="+actiontype";
		// Commented to change request to post start on 3 Nov 08
		var ajaxurl="../../Webnote_Servlet";
		var params="web_id="+webid+"&asset_id="+escape(assetId)+"&asset_type="+escape(assettype)+"&tab_name="+escape(tabname)+"&note_txt="+escape(noteBody)+"&title=TITLE&screen_x="+left+"&screen_y="+top+"&height="+height+"&width="+width+"&display_flag=Y&active=Y&action="+actiontype;
	
		XMLRequest.open("POST",ajaxurl,true);
		XMLRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XMLRequest.onreadystatechange = updateInfo;
		XMLRequest.send(params);
		}
	}
}					


function insertNoteData() {
			
		asset_vid = assetId;
		asset_vtype = assettype;
		if(tabname!=null)
		tab_vname = tabname;
		else
		tab_vname="";
		
		var noteBody = DL_oLayer.getElementsByTagName('textarea')[0].value;
		if(noteBody!=null && noteBody!=""){
		var left = DL_oLayer.style.left;
		tempyTop = 1; 
		var top = parseFloat(DL_oLayer.style.top) * 1 + tempyTop*1;
		var top = top + "px";
		DL_oLayer.getElementsByTagName('input')[1].value=top;
		var width = DL_oLayer.style.width;
		var url=location.href;
		var height = DL_oLayer.style.height;
		var actiontype="insert";
		var webid=0;
		var statusflag='';
		var noteid="";
		var actiontype="insert";
		noteid=DL_oLayer.id.substring(12);
		if(noteid!=null && noteid!="")
			{
				actiontype="update";
				webid=noteid;
			}
		
		if (actiontype=="update"){
			callMetrics('edit');
		}else{
			callMetrics('add');
		}

		//var ajaxurl="../../Webnote_Servlet?web_id="+webid+"&asset_id="+escape(assetId)+"&asset_type="+escape(assettype)+"&tab_name="+escape(tabname)+"&note_txt="+escape(noteBody)+"&title=TITLE&screen_x="+left+"&screen_y="+top+"&height="+height+"&width="+width+"&display_flag=Y&active=Y&action="+actiontype;
		var ajaxurl="../../Webnote_Servlet";
		var params="web_id="+webid+"&asset_id="+escape(assetId)+"&asset_type="+escape(assettype)+"&tab_name="+escape(tabname)+"&note_txt="+escape(noteBody)+"&title=TITLE&screen_x="+left+"&screen_y="+top+"&height="+height+"&width="+width+"&display_flag=Y&active=Y&action="+actiontype;
		//XMLRequest.open("GET",ajaxurl,true);
		XMLRequest.open("POST",ajaxurl,true);
		XMLRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		if(actiontype == "insert" ){
			XMLRequest.onreadystatechange = insertInfo;
		}else if(actiontype == "update" ){
			XMLRequest.onreadystatechange = updateInfo;
		}
		XMLRequest.send(params);
		}
					
}




var struid=0;
function insertInfo()
{
		var flag1= true;
		if (XMLRequest.readyState  == 4)
	            {
					    if (XMLRequest.status  == 200)
					{
						//Added on 1 Nov incase of any exception while inserting notes start
						if(!(XMLRequest.responseText == "INSERTEXCEPTION")){		
							user_count=XMLRequest.responseText.split('FSLROW');
						//Added on 1 Nov incase of any exception while inserting notes end
							struid=user_count[0];
							appendDivId();
                            // Modified for Search History Start Jan 13 ,2009
							document.getElementById('usernotecount').innerHTML="<a href=\"javascript:call_pbar_metrics('pb_show_notes');viewAll("+ flag1 +", '"+ asset_vid +"', '"+ asset_vtype +"','" +tab_vname+"' );\"><img src=\"/shared/images/annotate_view.gif\"/>Show notes on this Page ("+ user_count[1] +")</a>" ;
						    // Modified for Search History  End Jan 13 ,2009
                        }
						//Added on 1 Nov incase of any exception while inserting notes start
						else{
							alert("Your note was not saved due to some System Error.Inconvenience Regretted");						
						}
						//Added on 1 Nov incase of any exception while inserting notes end
										
					}
	          	}
}



function updateInfo()
{
	if (XMLRequest.readyState  == 4)
		{
				if(XMLRequest.status  == 200){
			 	//Added on 1 Nov incase of any exception while updating notes start
						if(XMLRequest.responseText == "UPDATEEXCEPTION"){
							alert("Your note was not saved due to some System Error.Inconvenience Regretted");						
						}
					//Added on 1 Nov incase of any exception while updating notes end
				}
		}
}


var deleteNote_asset_id='';
var deleteNote_asset_type='';
var deleteNote_tab_name='';
function deleteNote(deletedNoteId,asset_id,asset_type,tab_name) {
	callMetrics('delete');
	deleteNote_asset_id = asset_id;
	deleteNote_asset_type = asset_type;
	deleteNote_tab_name = tab_name;
	subnoteid=deletedNoteId.substring(12);
	
	if(subnoteid!="" && subnoteid!=null){
	var url = "../../Webnote_Servlet?web_id="+subnoteid+"&asset_id="+escape(asset_id)+"&asset_type="+escape(asset_type)+"&tab_name="+escape(tab_name)+"&active=N&action=delete";
	XMLRequestDeleteNote.open("GET", url , true);
	XMLRequestDeleteNote.onreadystatechange = deletedNote;	
	XMLRequestDeleteNote.send(null);  
	}
}

function deletedNote()
{
		var flag1=true;
		if (XMLRequestDeleteNote.readyState  == 4)
	    {
			if (XMLRequestDeleteNote.status  == 200)
			{
					//Added on 1 Nov incase of any exception while Deleting notes start
						if(!(XMLRequestDeleteNote.responseText == "DELETEEXCEPTION")){
					//Added on 1 Nov incase of any exception while Deleting notes end
							notecount = XMLRequestDeleteNote.responseText;
							if( notecount != 0 ){
							  // Modified for Search History Start Jan 29 ,2009
							document.getElementById('usernotecount').innerHTML="<a href=\"javascript:call_pbar_metrics('pb_show_notes');viewAll("+ flag1 +", '"+ deleteNote_asset_id +"', '"+ deleteNote_asset_type +"','" +deleteNote_tab_name+"' );\"><img src=\"/shared/images/annotate_view.gif\"/>Show notes on this Page("+ notecount +")</a>" ;
							  // Modified for Search History  Jan 29,2009
							}else{
							  // Modified for Search History Start Jan 29 ,2009
							document.getElementById('usernotecount').innerHTML="<img src=\"/shared/images/annotate_view.gif\"/>Show notes on this Page("+ notecount +")" ;
							  // Modified for Search History  End Jan 29,2009
							}
						}

					//Added on 1 Nov incase of any exception while Deleting notes start
						else{							
							alert("Your note was not deleted due to some System Error.Inconvenience Regretted");						
						}
					//Added on 1 Nov incase of any exception while Deleting notes end
			}
	    }
}

function appendDivId()
{
	
		var noteTemplateNode = document.getElementById('noteTemplate');
		if(noteTemplateNode!=null){
		divIdb4changing=noteTemplateNode.id
		divId=divIdb4changing+struid;
		noteTemplateNode.id=divId;
		chkdiv = document.getElementById(divId);
		}
}

function hidedisplayflag(hiddenNoteId)
{
	callMetrics('hide');
	//alert(hiddenNoteId);

	var sub_nt_hide=hiddenNoteId.substring(12);
	//alert("ID = "+sub_nt_hide);
	if(sub_nt_hide!=null && sub_nt_hide!=""){
	var hideurl = "../../Webnote_Servlet?web_id="+sub_nt_hide+"&display_flag=N&action=hide";
	XMLRequestHideNoteflag.open("GET", hideurl , true);
	XMLRequestHideNoteflag.onreadystatechange = noteHidden;	
	XMLRequestHideNoteflag.send(null);  }
}

function noteHidden(){
		if (XMLRequestHideNoteflag.readyState  == 4){
			if(XMLRequestHideNoteflag.status  == 200){
			//Added on 1 Nov incase of any exception while hiding note start
			if(XMLRequestHideNoteflag.responseText == "HIDEEXCEPTION"){
					alert("Your note cannot be hidden due to some System Error.Inconvenience Regretted");						
			}		
			//Added on 1 Nov incase of any exception while hiding note end
			}
	    }
}


function viewAll(flag1,id,type,tab)
{
	callMetrics('show');

	if(flag1==false)	
	{
		var url_currpage=location.href;
		document.getElementById('url_page').value=url_currpage+"&viewnt=true";
		document.usersessionchk.url_page.value=url_currpage+"&viewnt=true";
		document.usersessionchk.submit();
	}
	else{

			var viewurl="../../Webnote_Servlet?asset_type="+type+"&asset_id="+id+"&tab_name="+tab+"&action=viewall";
			XMLRequestViewNote.open("GET", viewurl , true);
			XMLRequestViewNote.onreadystatechange = viewNotesData;	
			XMLRequestViewNote.send(null);  
	}
}


function viewNotesData()
{
	if (XMLRequestViewNote.readyState  == 4)
	{
		if( XMLRequestViewNote.status  == 200){
		//Added on 1 Nov incase of any exception while View All notes start

		if(!(XMLRequestViewNote.responseText == "VIEWEXCEPTION")){
		//Added on 1 Nov incase of any exception while View All notes end
			//allNotes, create a list of all disaplyed notes, so that the one currently dispalyed will not be re-displayed
			allDivs =document.getElementsByTagName('div');
			numOfDivs = allDivs.length;
			var dsplayedNotes = "";
			// get all notes DIVS
			for (var i = 0; i < numOfDivs; i++) {
				if (allDivs[i].className=="notesLayer") {
					dsplayedNotes = dsplayedNotes +"$$"+allDivs[i].id.substring(12)+"$$";
				}
			}

						 rowdata=XMLRequestViewNote.responseText.split('FSLROW');
						 var notescount=rowdata.length;
						 colfield=0;
						 loopcounter=notescount-1;
						 for(var i=0; i<loopcounter;i++)
						{
								columndata=rowdata[i].split("FSLCOL");	
								
								if(dsplayedNotes.indexOf("$$"+columndata[colfield+6]+"$$") == -1){
									insertNote("no");
									textContainer = newNote.getElementsByTagName('textarea')[0];
									dragBar=newNote.getElementsByTagName('div')[0];
									iframeBuffer=newNote.getElementsByTagName('iframe')[0];
									textContainer.value=unescape(columndata[colfield]);
									newNote.id=newNote.id+columndata[colfield+6];
									newNote.style.left=columndata[colfield+1];
									newNote.style.top=columndata[colfield+2];
									newNote.style.width=columndata[colfield+4];
									newNote.style.height=columndata[colfield+5] ;
									dragBar.style.width=columndata[colfield+4];
									iframeBuffer.style.width=columndata[colfield+4];
									iframeBuffer.style.height=columndata[colfield+5];

									newNote.style.height=columndata[colfield+5];
									fullNoteHeight=parseInt(columndata[colfield+5] ) + 22+ "px";
									iframeBuffer.style.height=fullNoteHeight;
									textContainer.style.height=columndata[colfield+5]; 

						}							
						}
			}
			//Added on 1 Nov incase of any exception while View All notes start
			else{
				alert("Your notes cannot be viewed due to some System Error.Inconvenience Regretted");
			
			}
			//Added on 1 Nov incase of any exception while View All notes end
	  }
	}
}

var XMLRequest=getAjaxRequest();
var XMLRequestDeleteNote=getAjaxRequest();
var XMLRequestHideNoteflag=getAjaxRequest();
var XMLRequestViewNote=getAjaxRequest();

function getAjaxRequest()
{
	var oXMLRequest;
	if (window.ActiveXObject)
			{
				oXMLRequest = new ActiveXObject("Microsoft.XMLHTTP");  
			}
		else
			{
				oXMLRequest = new XMLHttpRequest();
			}    

	return oXMLRequest;
}

function callMetrics(action){
	if (window.webpage_note_metrics && action!= 'undefined' ){
		webpage_note_metrics(action);
	}
}