function updateStateBox(country, stateSelectId)
{
	document.getElementById('wrap' + stateSelectId).innerHTML = "<p>Loading...</p>";
	replacePanel('wrap' + stateSelectId, 'ajaxPages/getStateBox.php?country=' + encodeURIComponent(country) + '&boxName=' + stateSelectId);
}

/* main ajax engine. Get the ajax object. Support crappy browsers. etc */
function getAjaxObj()
{
	var ajaxObj = null;
	var strName;
	
	try{
		ajaxObj=new XMLHttpRequest();
	} catch (stupidBrowserException) {
		if (navigator.userAgent.indexOf("MSIE")>=0)
		{ 
			strName="Msxml2.XMLHTTP";
			if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
				strName="MSXML2.ServerXMLHTTP.4.0"

			try { 
				ajaxObj =new ActiveXObject(strName);
			} catch(e) { 
				alert("Error. Scripting for ActiveX might be disabled"); 
			} 
		} else {
			alert("You need to use an AJAX supported browser to use this application.");
		}
	}
	return ajaxObj;
}

function replacePanel(tagId, theUrl)
{
	var ao = getAjaxObj();
    ao.onreadystatechange = function() { handleChange(tagId, ao); };
    ao.open("GET", wwwDocRoot + theUrl , true)
    ao.send(null)
}

/* extra content function */
function constructUrl(fields)
{
	var i;
	var theUrl = "";
	for (i = 0; i < fields.length; i++)
	{
		if (document.getElementById(fields[i]))
			theUrl += fields[i] + "=" + encodeURIComponent(document.getElementById(fields[i]).value) + '&';
	}
	
	return theUrl.substr(0, theUrl.length - 1);
}

/* ajax base for extra content */
function getExtraContent(urlToGo)
{
	getExtraContentReal("GET", urlToGo);
}

function getExtraContentByPost(urlToGo)
{
	getExtraContentReal("POST", urlToGo);
}

function getExtraContentReal(sendType, urlToGo)
{
        document.getElementById('extraContent').className = 'extraContentShow';
        document.getElementById('ajaxLoadingSpace').innerHTML = '<p>Loading...</p>';
        document.getElementById('ajaxLoadingSpace').className = 'ajaxLoadingSpaceShow';

        var ao = getAjaxObj();
        ao.onreadystatechange = function() { handleExtraContent('extraContent', ao); };
        ao.open(sendType, wwwDocRoot + urlToGo , true)
        ao.send(null)
}	

/* return handling functions */
function handleChange(tagid, ao)
{
	if ((ao.readyState == 4) && (ao.status == 200))
		document.getElementById(tagid).innerHTML = ao.responseText;
}

function handleExtraContent(tag, ao)
{
	if ((ao.readyState == 4) && (ao.status == 200))
	{
		document.getElementById('ajaxLoadingSpace').innerHTML = '';
		document.getElementById('ajaxLoadingSpace').className = 'ajaxLoadingSpaceHide';

		document.getElementById(tag).innerHTML = ao.responseText;
	}
}

function addToRegistry(pid)
{ replacePanel('buyStatusTag', 'giftRegistry/add/' + pid); }

/* closing function */
function clearExtraContent()
{
	document.getElementById('extraContent').className = 'extraContent';	
	document.getElementById('extraContent').innerHTML = "";
}

/* email functions */
function emailFriend(pid)
{ getExtraContent('includes/incMailer.php?mode=mail&pid=' + pid); }

function sendAjaxMail()
{ getExtraContent('includes/incMailer.php?mode=domail&' + constructUrl(['myname', 'email', 'friendsemail', 'answer', 'pid'])); }

/* extra images functions */
function openExtraImages(pid)
{ getExtraContent('extraImages.php?pid=' + pid); }

/* comments functions */
function openComments(theType, iid)
{ getExtraContent('comments.php?mode=add&type=' + theType + '+&iid=' + iid); }

function addUserComment()
{ getExtraContent('comments.php?' + constructUrl(['mode', 'postName', 'email', 'iid', 'cid', 'type', 'title', 'body', 'answer'])); }

function reloadComments(theType, iid) 
{ replacePanel(theType + 'Comments', "comments.php?mode=showall&type=" + theType + "&iid=" + iid); }
