/*/////////////////////////////////////////////////////////////////////////////////////////
|	
|	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.');
		}
		loading(gevent);
		//busy = false;
	}

	//document.getElementsByTagName("body")[0].style.cursor = 'default';

}

var myemail = "";
var email;
var password;
var new_user = false;
function ajaxHandler (what, flag, value) {
	
	switch (what) {
		case "confirmed":
			if (flag) {
				document.getElementById('before').style.display = 'none';
				document.getElementById('after').style.display = 'block';
			}
			break;
		case "agreeToTerms":
			if (!flag) {
				document.getElementById('confirm_agree_text').innerHTML = "<b style=\"color: #FF0000;\">Please check the box</b> stating that you agree to the <a href=\"tos.php\" target=\"_blank\">Terms and Conditions</a>";
			}
			break;
		case "submitPhoto":
			if (document.getElementById('confirm_password')) document.getElementById('confirm_password').style.display=value;
			if (flag != "") {
				submitPhotoError(flag);
			}
			else if (value == 'none' || new_user) document.submit_photo.submit();
			else new_user = true;
			break;
		case "toggleLogin":
			if (document.getElementById('login_box')) document.getElementById('login_box').innerHTML = value;
			if (document.getElementById('join_top1')) document.getElementById('join_top1').innerHTML = "";
			if (document.getElementById('join_top2')) document.getElementById('join_top2').innerHTML = "";
			if (document.getElementById('mymenu')) {
				if (!flag) {
					document.getElementById('mymenu').style.visibility = 'visible';
					hidePopup('login');
				}
				else document.getElementById('mymenu').style.visibility = 'hidden';
			}
			else if (login_forward != null) location = login_forward;
			else location = 'myphotos.php';
			break;
		case "wrongPassword":
			if (document.getElementById('login_password_help')) {
					document.getElementById('login_password_help').style.color = '#FF0000';
					document.getElementById('login_password_help').innerHTML = value;
			}
			break;
		case "myLogout":
			document.location = 'index.php';			
			break;
		case "deletePosting":
			myposting = document.getElementById("posting_" + flag);
			myposting_options = document.getElementById("posting_options_" + flag);
			
			myposting.innerHTML = value;
			myposting.style.textDecoration = 'line-through';
			myposting_options.innerHTML = "(Removed)";
			myposting_options.style.textDecoration = 'line-through';
			
			break;
		case "contactSeller":
			if (!flag) document.getElementById("contact_error").innerHTML = value;
			else document.getElementById("inner_contact").innerHTML = "<div id=\"contact_sent\">Your message has been sent.<br /><br /><a href=\"javascript:void(0);\" onclick=\"hidePopup('contact')\">Close</a></div>";
			
			break;
		case "toggleNWS":
			//document.getElementById("city").innerHTML = value;
			
			window.location.reload(true);
			break;
		case "changeInfo":
			if (!document.getElementById('topBar_changeinfo_success')) {
					document.getElementById("changeinfo_success").innerHTML = addTopBar('changeinfo_success', document.getElementById("changeinfo_success"), false, 'changeinfo_success');
			}
			document.getElementById("changeinfo").innerHTML = document.getElementById("changeinfo_success").innerHTML;
			break;
		case "adminFlagDown":
			if (document.getElementById('flagphoto').style.display=='block') { 
				hidePopup('flagphoto');
			}
			if (document.getElementById('flagcomment')) { 
				if (document.getElementById('flagcomment').style.display=='block') { 
					hidePopup('flagcomment'); 
				}
			}
			document.getElementById("loading").style.display = 'none';
			
			document.getElementById(value).innerHTML = "Flagged down: " + flag;
			
			break;
		case "commentLeft":
			document.getElementById("comment_error").innerHTML = "";
			var d = "<div class='comment'><b>Posted by <i>" + value['firstname'] + "</i> | " + value['commentdt'] + "</b> <p>" + value['comment'] + "</p></div>";
			p = document.getElementById("comments");
			
			var ih = p.innerHTML;
			p.innerHTML = d + ih;
			
			var comment_count = document.getElementById("comment_count");
			comment_count.innerHTML = parseInt(comment_count.innerHTML) + 1;
			
			setTimeout("hideCommentBox()", 1500);			
			
			break;
	}
	
	return true;
	
}


