/*/////////////////////////////////////////////////////////////////////////////////////////
|	
|	interact.js - A place to put ajax and popup related functions
|	
|	null	 	sendForm(object, string, string) //prepares for elements for submission
|	null		makeRequest(string, string, string) //makes ajax request in POST format
|	null		handleContents(object, string) //executes code or changes text from response
|	bool		ajaxHandler(string, string/bool, string) //code to be executed based from response
|	
|	
|
/////////////////////////////////////////////////////////////////////////////////////////*/


	
//this function prepares the form info for sending
function sendForm(docForm, destination, container_id) {

	var strSubmit       = '';
	var formElem;
	var strLastElemName = '';
	
	for (i = 0; i < docForm.elements.length; i++) {
			formElem = docForm.elements[i];
			if (formElem.type != 'button' && formElem.type != 'radio' && formElem.type != 'checkbox') {
				strSubmit += formElem.name + '=' + escape(formElem.value)  + '&';
			}	
			else if ((formElem.type == 'radio' || formElem.type == 'checkbox') && formElem.checked) {			
				strSubmit += formElem.name + '=' + escape(formElem.value) + '&';
			}
	}
	//alert(strSubmit);
	makeRequest(destination, strSubmit, container_id);

}

//this function tries to send the info
var busy = false;
function makeRequest(url, qStr, container_id) {
	//document.getElementsByTagName("body")[0].style.cursor = 'progress';
	//alert(gevent);
	if (busy) return false;
	//busy = true;
	loading(gevent);
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { handleContents(http_request, container_id); };
	http_request.open('POST', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	http_request.send(qStr);

}

function handleContents(http_request, container_id) {

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//if () {
				
				code = new String(http_request.responseText);
				if (code.indexOf("<code>") != -1) {
					code = code.replace("<code>", "");
					code = code.replace("</code>", "");
					//alert(code);
					eval(code);
				}
				else if (code.indexOf("<text>") != -1 && container_id != "") {
					code = code.replace("<text>", "");
					code = code.replace("</text>", "");
					//alert(code);
					document.getElementById(container_id).innerHTML = code;
				}
				else {
					alert('Non XML format');
					//document.getElementById(container_id).innerHTML = code;
				}
			//}
		} else {
			alert('There was a problem with the request... ' + http_request.status);
		}
		loading(gevent);
		//busy = false;
	}

	//document.getElementsByTagName("body")[0].style.cursor = 'default';

}

var myemail = "";
var email;
var password;
var new_user = false;
function ajaxHandler (what, value) {
	
	switch (what) {
		case "login":
			document.login_form.submit();
			break;
		case "photovideo_commentLeft":
		
			document.getElementById("comment_error_"+value['photovideo_id']).innerHTML = "";


			var e = "<div class='photo_commentsbox_img'><a href='view_profile.php?id=" + value['from_user_id'] + "'><img src='pi/t_" + value['filename'] + "' alt='' width='75' height='75' border='0' class='photo_imgborder' /></a></div>";
			e += "<div class='photo_commentinfo'><strong><a href='view_profile.php?id=" + value['from_user_id'] + "'>" + value['username'] + "</a> . " + value['commentdt'] + " </strong><br />";
			e += value['comment'];
			e += "</div><br /><br /><br /><br /><br /><br /><br />";

			p = document.getElementById("photo_comment_container_"+value['photovideo_id']);

			var ih = p.innerHTML;

			if (ih.replace(/^\s+|\s+$/g, '') == 'No Comments') p.innerHTML = e;
			else p.innerHTML = e + ih;
			//increase comment counts
			c1 = document.getElementById("photovideo_comment_count_1_"+value['photovideo_id']);
			c2 = document.getElementById("photovideo_comment_count_2_"+value['photovideo_id']);
			c1.innerHTML = (parseInt(c1.innerHTML)+1);
			c2.innerHTML = (parseInt(c2.innerHTML)+1);

			showPhotovideoCommentBox(value['photovideo_id']);

			document.forms["photo_comment_form_"+value['photovideo_id']]._comment.value = "";

			break;
		case "commentLeft":
			
			document.getElementById("leave_comment_error").innerHTML = "";
			var d = "<div class='commentsbox_img'><a href='view_profile.php?id=" + value['from_user_id'] + "'><img src='pi/t_" + value['filename'] + "' alt='' width='75' height='75' border='0' class='imgborder' /></a></div>";
			d += "<div class='commentinfo'><strong><a href='view_profile.php?id=" + value['from_user_id'] + "'>" + value['username'] + "</a> . " + value['commentdt'] + " </strong><br />";
			d += value['comment'];
			d += "</div><br /><br /><br /><br /><br /><br />";
			d += "<img src='layout_images/comments_spacerbar.gif' width='520' height='7' alt='' />";

			p = document.getElementById("comment_box");
			
			var ih = p.innerHTML;
			p.innerHTML = d + ih;
			
			upper_profile("comments");
			
			break;
	}
	
	return true;
	
}


