function doCanned(){	
	var canned = document.getElementById('canned');
	var url = canned.options[canned.selectedIndex].value;
	var postStr = "type=fetchCanned&id=" + url;
	makePOSTRequest('/php/ajax.php',postStr,'fetchCanned');
}

function saveCanned(){	
	var cannedSub = document.getElementById('cannedSub');
	var cannedBody = document.getElementById('cannedBody');
	var postStr = "type=saveCanned&body=" + cannedBody.value+"&subject="+cannedSub.value;
	makePOSTRequest('/php/ajax.php',postStr,'saveCanned');
}

function discardCanned(){	
	var preview = document.getElementById('cannedBox');	
	var cannedBody = document.getElementById('cannedBody');
	if (preview && cannedBody) {			
		preview.style.display = 'none';
		cannedBody.value = '';
	}
}

function showPreview(isShow){	
	var preview = document.getElementById('PreviewBox');
	var previewDiv = document.getElementById('previewDiv');
	var field = document.getElementById('message');
	if (preview && previewDiv && field) {
		preview.style.display = isShow?'':'none';
		previewDiv.style.display = isShow?'none':'';
		//field.disabled = isShow;
	}
	if (isShow) {
		var message = document.getElementById('message');
		var sig = document.getElementById('includesig');
		var postStr = "type=parseMessage&text=" + message.value;
		if(sig){
			postStr = postStr+"&sig="+sig.checked;	
		}
		makePOSTRequest('/php/ajax.php', postStr, 'parseMessage');
	}else{
		var previewMsg = document.getElementById('PreviewMsg');
		previewMsg.innerHTML = '';
		field.focus();
	}
}

function showCanned(){	
	var preview = document.getElementById('cannedBox');	
	var cannedBody = document.getElementById('cannedBody');
	preview.style.display = '';
	var cannedText = getSelectionText();
	if(!cannedText){
		var message = document.getElementById('message');
		cannedText = message.value;
	}
	cannedBody.value = cannedText;
}

function addCannedReply(reqObj){	
	if (reqObj.readyState == 4){
   		if (reqObj.status==200 || window.location.href.indexOf("http")==-1){
			var data = eval('(' + reqObj.responseText + ')');
			if(data.status=="OK"){				
				tag('', data.text, 'canned')
			}else{
				alert(data.alert);
			}
		}else{
			alert('Server Error: Request Failed. Can\'t Fetch Canned Reply');
		}
   	}
}

function saveCannedReply(reqObj){	
	if (reqObj.readyState == 4){
   		if (reqObj.status==200 || window.location.href.indexOf("http")==-1){
			var data = eval('(' + reqObj.responseText + ')');
			if(data.status=="OK"){				
				alert("Canned reply saved succesfully.");
				var previewBtn = document.getElementById('cannedBtn');
				var preview = document.getElementById('cannedBox');
				preview.style.display = 'none';
				previewBtn.value = 'Save Canned Reply';	
			}else{
				alert(data.alert);
			}
		}else{
			alert('Server Error: Request Failed. Can\'t Fetch Canned Reply');
		}
   	}
}

function showParsedReply(reqObj){
	if (reqObj.readyState == 4){
   		if (reqObj.status==200 || window.location.href.indexOf("http")==-1){
			var data = eval('(' + reqObj.responseText + ')');
			if(data.status=="OK"){				
				var previewMsg = document.getElementById('PreviewMsg');
				previewMsg.innerHTML = data.text;
			}
		}else{
			alert('Server Error: Request Failed. Can\'t Fetch Canned Reply');
		}
   	}
}
