/**
 * AJAX Javascript
 *
 * Page to initiate all AJAX functions
 *
 * @author gUncle Development [http://www.guncle.com]
 * @version 2.1 3/12/2008
 */

var hmajax = createRequest();


/**
 * Ajax function to "mark as read" visitor-sent contact
 * @param integer $id = key of array
 */
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
 * @param integer size = amount to change
 */
function fontSize(size) {
	sendRequest( "theme_set.php",size );
}


/**
 * Generic ajax functions to remove item with id
 * @param string $type = type of item to remove
 * @param string $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){
		var response = splitResponse(hmajax.responseText);
		if( response ) {
			var r = document.getElementById(response['type']+'Row_'+response['id']);
			if( r ) { r.parentNode.deleteRow(r.rowIndex); }
			colorAltRows(response['type']+'Table');
		}
		else {
			window.location.reload();
		}
	}
}


/**
* Ajax functions to post blog comment
 */
function postBlogComment() {
	var cAuthor = document.getElementById('frm_name').value;
	var cText = encodeURIComponent(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;
       	}
    }
}


/**
 * Changes theme for site
 * @param string $page = url of current page
 * @param string $selectbox = name of select box
 * @param string $variable = name of url variable
 */
function theme_refresh() {
	theme = document.getElementById('style').value;
	sendRequest( "theme_set.php",theme );
}


/**
 * OLD ajax processing functions
 */
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();
	}
}

/**
 * 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 splitResponse(response) {
	if( response ) {
		var responseArray = response.split('|~');
		var newArray = new Array();
		for( var i=0; i<responseArray.length; i++ ) {
			tempArray = responseArray[i].split(':~');
			newArray[tempArray[0]] = tempArray[1];
		}
		return newArray;
	}
	else {
		return false;
	}
}
