var httpSendComment  = getHTTPObject();
var httpSendPing = getHTTPObject();
var httpTags = getHTTPObject();
	
function pingTechnorati(){

}


//POST COMMENT
function postComment(articleID){
	
	if(httpSendComment.readyState == 4||httpSendComment.readyState == 0){
		
		var targetContainer = document.getElementById('commentform');		
		
		/* Parameters */
		var commentAuthor = document.getElementById('comment_form_author').value;
		var commentEmailAddress = escape(document.getElementById('comment_form_email').value);
		var commentTitle = escape(document.getElementById('comment_form_title').value);
		var commentContent = escape(document.getElementById('comment_form_comment').value);
		
		var sendComment = 1;
		
		// Validate entries
		if(commentAuthor=='' && sendComment == 1){alert('Please enter a name.'); document.getElementById('comment_form_author').focus(); sendComment = 0; }
		if(commentEmailAddress=='' && sendComment == 1){alert('Please enter an email address.'); document.getElementById('comment_form_email').focus(); sendComment = 0; }
		if(commentContent=='' && sendComment == 1){alert('Please enter a comment.'); document.getElementById('comment_form_comment').focus(); sendComment = 0; }
		
		if(sendComment == 1){
			targetContainer.innerHTML = '<span class="message_alert">Saving Comments, Please Wait</span>';
			
			var url = '/sendComment.php?articleID='+articleID; 
			var params = "cAuthor=" + commentAuthor + "&cEmail=" + commentEmailAddress + "&cTitle=" + commentTitle + "&cContent=" + commentContent; 
		
			httpSendComment.open('POST', url, false);
	
			httpSendComment.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			httpSendComment.onreadystatechange = function() 
			{
				if(httpSendComment.readyState == 4)
				{	
					targetContainer.innerHTML = '<p>Your comments have been added. We\'ll email you when other comments are added.</p>';
				}
			};
			httpSendComment.send(params);
		}
	}	
}



function hConfirmSaveComment(){
	var targetContainer = document.getElementById('commentform');
	if(httpSendComment.readyState == 4){	
		targetContainer.innerHTML = '<p>Your comments have been saved. You will receive an email shortly with an activation link. Non-activated comments will be deleted after 48 hours.</p>';
	}
}

		
function publishArticle()
{
		
	var checkboxes = document.getElementsByName("catChk");
	var catList = '';
	var formValid = 1;
	
	for(i = 0; i < checkboxes.length; i++){
		if(checkboxes[i].checked == true) {
			catList += '{' + checkboxes[i].value + '}';
		}
	}
	
	if (document.getElementById('articleTitle').value == '') { formValid = 0; }
	if (catList == '') { formValid = 0; }
	if (formValid == 1) {
		document.getElementById('articleCategories').value = catList;
		document.fNewArticle.action = '/admin_functions.php?fn=publish&articleID='+articleID;
		document.fNewArticle.submit();
	}else{
		alert('Please complete all fields before submitting.');
	}
}


function saveArticle()
{
		
	var checkboxes = document.getElementsByName("catChk");
	var catList = '';
	var formValid = 1;
	
	for(i = 0; i < checkboxes.length; i++){
		if(checkboxes[i].checked == true) {
			catList += '{' + checkboxes[i].value + '}';
		}
	}
	
	if (document.getElementById('articleTitle').value == '') { formValid = 0; }

	if (formValid == 1) {
		document.getElementById('articleCategories').value = catList;
		document.fNewArticle.action = '/admin_functions.php?fn=save&articleID='+articleID;
		document.fNewArticle.submit();
	}else{
		alert('Please add a title before saving.');
	}
}

function deleteArticle(artID)
{
	var http = getHTTPObject();
	if (http.readyState == 4 || http.readyState == 0) 
	{
		if(confirm('Really delete?'))
		{
			http.open("GET",'/admin_functions.php?fn=delete_article&article_id=' + artID, true);
			http.onreadystatechange = function() 
			{
				if (http.readyState == 4) { 
					location.href = 'admin.php';
					//alert(http.responseText);
				}
			}; 
			http.send(null);
		}
	}
}


function sendTags()
{

	var tagField = document.getElementById('articleTags');
	var tagString = tagField.value;
	
	if (httpTags.readyState == 4 || httpTags.readyState == 0) {
		if(tagString != ''){
			var target = document.getElementById('article_tag_container');
			target.innerHTML = '<p>Saving...</p>';
			
			tagField.value = ''; //clear tag field
			httpTags.open("GET",'/admin_functions.php?fn=addTags&articleID=' + articleID + '&tag_string=' + escape(tagString), true);
			httpTags.onreadystatechange = function() 
			{
				if (httpTags.readyState == 4) {
					var response = httpTags.responseText;
					
					if (response == ''){
						target.innerHTML = '<div class="form_check_item">No Tags Added</div>';
					}else{
						target.innerHTML = response;
					}
				}
			}; 
			httpTags.send(null);
		
		}	
	}
}
		
function title_type_change(opt)
{
	var target = document.getElementById('image_header_details');
	if(opt == "1")
	{
		target.style.display = "none";
	}
	else
	{
		target.style.display = "block";
	}
}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

