/***********************************************************
* gUncle Development
* http://www.guncle.com
* 
* ajax.js
* Page that contains all ajax javascript functions 
***********************************************************/

var hmajax = createRequest();

/***********************************************************
* ajax function to "mark as read" visitor-sent contact
* -variables-
* id = id of entry
***********************************************************/
function contactViewed(id) {
	hmajax.open('get','../ajax.php?action=contactViewed&id='+id);
	hmajax.onreadystatechange = contactViewedResponse;
	hmajax.send(null);
}
function contactViewedResponse() {
	if(hmajax.readyState == 4){
       	window.location.reload();
    }
}


/***********************************************************
* ajax function to change font size
* -variables-
* size = amount to change
***********************************************************/
function fontSize(size) {
	sendRequest( "theme_set.php",size );
}


/***********************************************************
* generic ajax functions to remove item with id
* -variables-
* type = type of item to remove
* id = id of item
***********************************************************/
function itemRemove(type,id) {
	check = confirm("Do you really want to remove this "+type+" item?");
	if( check ) {
		hmajax.open('get','/ajax_remove.php?type='+type+'&id='+id);
		hmajax.onreadystatechange = itemRemoveResponse;
		hmajax.send(null);
	}
}
function itemRemoveResponse() {
	if(hmajax.readyState == 4){
       	window.location.reload();
    }
}


/***********************************************************
* ajax functions to post blog comment
***********************************************************/
function postBlogComment() {
	var cAuthor = document.getElementById('frm_name').value;
	var cText = document.getElementById('frm_comments').value;
	var cBlog = document.getElementById('frm_blog').value;
	if( cAuthor == '' || cText == '' ) {
		alert('Please fill out both the name and comments fields to post your comment.');
	}
	else {
		hmajax.open('get','ajax_comments.php?action=blog&blog_id='+cBlog+'&comment='+cText+'&name='+cAuthor);
		hmajax.onreadystatechange = postBlogCommentResponse;
		hmajax.send(null);
	}
}
function postBlogCommentResponse() {
	if(hmajax.readyState == 4){
        var response = hmajax.responseText;
        
       	if( response ) {
	       	if( response == 'incomplete' ) {
		       	alert('Please fill out both fields prior to posting your comments.');
	       	}
	       	else if( response == 'error' ) {
		       	alert('There was an error posting your comments. Please try again.');
	       	}
	       	else if( response == 'filtered' ) {
		       	alert('The comment you attempted to post included inappropriate material.');
	       	}
	       	else {
		       	var comments = document.getElementById('comments').innerHTML;
		       	comments = response + comments;
		       	document.getElementById('frm_name').value='';
		       	document.getElementById('frm_comments').value='';
		       	commentBox();
		       	document.getElementById('comments').innerHTML = comments;
	       	}
       	}
    }
}


/***********************************************************
* ajax functions for visitors to request full-res copy of
* photo
***********************************************************/
function photoRequest() {
	var pFile = document.getElementById('frm_file').value;
	var pEmail = document.getElementById('frm_email').value;
	hmajax.open('get','/ajax_photo_request.php?file='+pFile+'&email='+pEmail);
	hmajax.onreadystatechange = photoRequestResponse;
	hmajax.send(null);
}
function photoRequestResponse() {
	if(hmajax.readyState == 4){
        var response = hmajax.responseText;
        
       	if( response ) {
	       	//var request = response.split("|");
	       	document.getElementById('request-form').innerHTML = response;
       	}
    }
}


/***********************************************************
* function to change theme on theme.php page
* -variables-
* page = url of current page
* selectbox = name of select box
* variable = name of url variable
***********************************************************/
function theme_refresh() {
	theme = document.getElementById('style').value;
	sendRequest( "theme_set.php",theme );
}


/***********************************************************
* ajax processing functions
***********************************************************/
function createRequest() {
	var obj;
	var browser = navigator.appName;
	if( browser == "Microsoft Internet Explorer" ) {
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		obj = new XMLHttpRequest();
	}
	return obj;
}
function sendRequest(page,id) {
	hmajax.open('get',page+'?id='+id);
	hmajax.onreadystatechange =  handleResponse;
	hmajax.send(null);
}
function handleResponse() {
	if( hmajax.readyState == 4 ) {
		var response = hmajax.responseText;
		var update = new Array();
		window.location.reload();
	}
}