// $Id: functions.js,v 1.10 2007/11/01 15:58:26 jyu Exp $

function getPOSTParams(theForm)
{
	var params = '';
	for (var x = 0; x < theForm.elements.length; x++)
	{
		if (theForm.elements[x].type == 'radio' || theForm.elements[x].type == 'checkbox') {
			if (theForm.elements[x].checked) {
				params += theForm.elements[x].name+'='+encodeURIComponent(theForm.elements[x].value)+'&';
			}
		}
		else {
			params += theForm.elements[x].name+'='+encodeURIComponent(theForm.elements[x].value)+'&';
		}
	}
	params += 'ajax=1';
	return params;
}

function getCommentForm(rurl, iid)
{
	callACAC('/bd/comments/comments_form.php', 'ajax=1&rurl=' + rurl + '&iid=' + iid, 'commentFormBlock');
}

function refreshComments(rurl)
{
	callACAC('/bd/comments/comments_fetch.php', 'rurl=' + rurl, 'commentsBlock');
	
	//ScaleToFit('commentFormBlock');
	//window.setTimeout("ScaleToFit('commentsBlock')", 1000);
}


function submitComment(theForm, req, ajax)
{
	if (req == 1) {
		if ( theForm.name.value == '' ) {
			alert('Please enter your name.');
			theForm.name.focus();
			return false;
		}
		
		if ( theForm.comment.value == '' ) {
			alert('Please enter your comment.');
			theForm.comment.focus();
			return false;
		}

		if ( theForm.captcha.value == '' ) {
			alert('Please enter the letters or numbers you see in the image.');
			theForm.captcha.focus();
			return false;
		}

		apos=theForm.email.value.indexOf("@");
		dotpos=theForm.email.value.lastIndexOf(".");
		if (apos < 1 || dotpos-apos < 2){
			alert('Please enter a valid email address.');
			theForm.email.focus();
			return false;
		}
	}
	if (ajax == 1) {
		//storeCurrentSize('commentsBlock');
		//storeCurrentSize('commentFormBlock');
		callACAC('/bd/comments/comments_process.php', getPOSTParams(theForm), 'commentFormBlock', 'POST');
		return false;
	}
	return true;
}

function storeCurrentSize(id)
{
	var obj = document.getElementById(id);
	obj.style.width = obj.scrollWidth + 'px';
	obj.style.height = obj.scrollHeight + 'px';
	obj.style.overflow = 'hidden';
}


////////////////////////////////////////////////////////////
// Core Ajax Functions
////////////////////////////////////////////////////////////

function callACAC(url, params, pageElement, formAction, callMessage, errorMessage) {
	var method = formAction ? formAction : 'GET';
	if (callMessage)
		document.getElementById(pageElement).innerHTML = callMessage;
	try {
		req = new XMLHttpRequest(); /* e.g. Firefox */
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
			} catch (E) {
				req = false;
			} 
		} 
	}
	req.onreadystatechange = function() {responseACAC(pageElement, errorMessage);};
	url = method == 'GET' ? url+'?'+params : url;
	req.open(method,url,true);
	if (method == 'POST') {
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		req.send(params);
	} else {
		req.send(null);
	}
}

function responseACAC(pageElement, errorMessage) {
	var output = '';
	if(req.readyState == 4) {
		if(req.status == 200) {
			output = req.responseText;
			document.getElementById(pageElement).innerHTML = output;
		} else {
			if (errorMessage)
				document.getElementById(pageElement).innerHTML = errorMessage+"\n"+output;
		}
	}
}

function ScaleToFit(id) {
	var obj = document.getElementById(id);
	//alert('ID: ' + id + ' Old: ' + parseFloat(obj.style.height) + ' New: ' + obj.scrollHeight);
	resize(obj.scrollWidth, obj.scrollHeight, parseFloat(obj.style.width), parseFloat(obj.style.height), id);
}

var speed = 10; // Delay between increments.
var inc = 10; // Increment amount -- also changes speed

function resize(width, height, currentwidth, currentheight, id) {
	var obj = document.getElementById(id);
	w = currentwidth;
	h = currentheight;
	if (currentwidth <= width) {
		w += inc;
		obj.style.width = w + 'px';
	}
	if (currentheight <= height) {
		h += inc;
		obj.style.height = h + 'px';
	}
	if (currentwidth <= width || currentheight <= height) {
		var t = window.setTimeout("resize("+width+", "+height+", "+w+", "+h+", '"+id+"')", speed);
	} else { 
		obj.style.height = height + 'px';
		obj.style.width = width + 'px';
	}
}

