

	
PrivateNotes = function() {}

PrivateNotes.editPrivateNotesMode = false;
PrivateNotes.oldText = null;

PrivateNotes.toggleEditMode = function() {
	if (!PrivateNotes.editPrivateNotesMode) {
		edit = document.createElement("textarea");
		edit.setAttribute("name", "privateNotesEditField");
		edit.setAttribute("id", "privateNotesEditField");
		edit.setAttribute("style", "width: 100%; height: 200px; ");
		edit.setAttribute("cols", 50);
		edit.setAttribute("rows", 15);
		
		PrivateNotes.oldText = DWRUtil.getValue("privateNotesText");
		edit.appendChild(document.createTextNode(PrivateNotes.oldText));
		
		container = $("privateNotes");
		while (container.hasChildNodes()) {
	          container.removeChild(container.firstChild);
	    }
		container.appendChild(edit);
		$("editNotesButton").value = "Annuller";
		$("privateNotesEditorTools").style.display = "inline";
	} else {
		pre = document.createElement("pre");
		pre.setAttribute("id", "privateNotesText");
		
		pre.appendChild(document.createTextNode(PrivateNotes.oldText));

		container = $("privateNotes");
		while (container.hasChildNodes()) {
	          container.removeChild(container.firstChild);
	    }
		container.appendChild(pre);
		
		$("editNotesButton").value = "Rediger noter";
		$("privateNotesEditorTools").style.display = "none";
	}
	PrivateNotes.editPrivateNotesMode = !PrivateNotes.editPrivateNotesMode;
}

PrivateNotes.savePrivateNotes = function() {
	QuotationService.savePrivateNotes(DWRUtil.getValue("privateNotesEditField"), PrivateNotes.afterSavePrivateNotes);
}

PrivateNotes.afterSavePrivateNotes = function() {
	PrivateNotes.oldText = DWRUtil.getValue("privateNotesEditField");
	PrivateNotes.toggleEditMode();
}


PrivateNotes.complaint = function() {
	PrivateNotes.annotate("COMPLAINT");	
}

PrivateNotes.annotate = function(annotation) {
	formField = $("privateNotesEditField");
	var curDate = GetCurrDateTime2();
	var annotateString = "== " + annotation + " == ";
	annotateString += curDate;
	annotateString += " == ";
	annotateString += "    (Guest)";
	annotateString += " ==\n\n\n"
	if (formField.value == " ") {
		formField.value = annotateString;
	} else {
		formField.value = annotateString + formField.value; 
	}
	setSelectionRange(formField,annotateString.length-2,0);
}



function setSelectionRange(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	} else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function GetCurrDateTime2()	{
	    var now_month;
	    var now_day;
	    var now_year;
	    var now_hour;
	    var now_min;
	    var now_sec;
	    var now_daytime;
	    var now_datetime;
	    var use_hour = "00";
	    var use_min = "00";
	    var use_sec = "00";
	    var use_month = "00";
	    var use_day = "00";
	    now_datetime = new Date();    
	    now_month = now_datetime.getMonth() + 1;
	    now_day = now_datetime.getDate();
	    now_year = now_datetime.getYear(); 
	    now_hour = now_datetime.getHours();
	    now_min = now_datetime.getMinutes();
	    now_sec = now_datetime.getSeconds();
	    
	    
	    if (now_year < 100) {
	      
	      now_year = '19' + now_year; 
	    }
	    if ((now_year >= 100) && (now_year < 1000)) {
	      
	      now_year = parseInt(now_year) + 1900;
	      now_year = now_year.toString();
	    }
	    
	    if (now_month < 10) {
	      use_month = "0" + now_month;
	    } else {
	      use_month = now_month;
	    }
	    if (now_day < 10) {
	      use_day = "0" + now_day;
	    } else {
	      use_day = now_day;
	    }
	    if ("dd mm yyyy" == "mm dd yyyy") {
	      now_datetime = use_month + '/' + use_day + '/' + now_year + ' ';
	    }
	    if ("dd mm yyyy" == "dd mm yyyy") {
	      now_datetime = use_day + '/' + use_month + '/' + now_year + ' ';
	    }
	    if ("dd mm yyyy" == "yyyy mm dd") {
	      now_datetime = now_year + '/' + use_month + '/' + use_day + ' ';
	    }
	    
	    if (now_hour < 12) {
	      if (now_hour == 0) {
	        now_hour += 12;
	      }
	      use_ind = "AM";
	    } else {
	      if (now_hour != 12) {
	        now_hour %= 12;
	      }
	      use_ind = "PM";
	    }
	    
	    if (now_hour < 10) {
		      use_hour = "0" + now_hour;
	 } else {
      use_hour = now_hour;
	 }
	    if (now_min < 10) {
	      use_min = "0" + now_min;
	    } else {
	      use_min = now_min;
	    }
	    if (now_sec < 10) {
	      use_sec = "0" + now_sec;
	    } else {
	      use_sec = now_sec;
	    }
	    now_datetime = now_datetime + use_hour + ':' + use_min + ':' + use_sec + ' ' + use_ind;
	    
	    return (now_datetime);
}
