function createXMLHttp() {

    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
      ];

      for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {
            //Do nothing
        }
      }
    }
    throw new Error("XMLHttp object could be created.");
}

/**
 * updateStateField()
 *
 * This function will update a state select field & state label based off
 * a country select field.  The first 5 parameters are required, the sixth
 * is optional text to be appended to the state label.
 *
 * @param string e				The country select field element ID
 * @param string l				The current language
 * @param string label			The state label ID
 * @param string s				The state select field element ID
 * @param int in_smartway		1 if used in smartway, 0 otherwise
 * @param string add				Additional text to append to the state label
 *
 * Example: updateStateField('create_event_country','name_en','event_state_label','create_event_state',1,':')
 */
function updateStateField(e,l,label,s,in_smartway,add,all_flag)
{
	var xmlhttp = createXMLHttp();
	/* Testing */
	var pos = elementPosition(document.getElementById(s+"_container"));
	var nd = document.createElement('div');
	nd.setAttribute('id', 'states_loading_msg');
	nd.className = 'webbox_rollover';
	nd.style.height = '20px';
	nd.style.top = (pos[1] + 20) + 'px';
	nd.style.left = pos[0] + 'px';
	nd.innerHTML = 'Loading states... please wait';
	document.body.appendChild(nd);
	/* Testing End  */

	var c = document.getElementById(e).value;
	var server = 'ajax_server.php';
	if(in_smartway==0)
	{
		server = 'smartway/' + server;
	}

	if(all_flag != null && all_flag > 0)
	{
		all_flag = 1;
	}
	else
	{
		all_flag = 0;
	}

	xmlhttp.open('GET',server+"?action=state_label&ajax_param_country_id="+c+"&ajax_param_lang="+l+"&ajax_param_allflag="+all_flag,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4)
		{
			/* Testing */
			document.body.removeChild(document.getElementById('states_loading_msg'));
			/* Testing End */
			response = xmlhttp.responseText;
			var info = response.split("|");
			if(info[1] == 'disabled')
			{
				document.getElementById(s+"_container").innerHTML = '<select name="'+s+'" id="'+s+'"><option value="-">----------</option></select>';
				document.getElementById(s).disabled = true;
				document.getElementById(label).innerHTML = '';
				document.getElementById(label).innerHTML = info[2] + add;
			}
			else
			{
				document.getElementById(s).disabled = false;
				document.getElementById(s+"_container").innerHTML = '<select name="'+s+'" id="'+s+'">'+info[2]+'</select>';
				document.getElementById(label).innerHTML = info[1] + add;
			}

			/*if(info[0] == '0')
			{
				document.getElementById(zipcode).disabled = true;
			}
			else
			{
				document.getElementById(zipcode).disabled = false;
			}*/
		}
	}
	xmlhttp.send(null);
}

function openMapWindow(id)
{
	window.open('index.php?page=event_map&event_id='+id,'','width=450,height=450,resizeable=0,toolbar=0');
	return false;
}

function openWindow(url,w,h)
{
	window.open(url,'','width='+w+', height='+h+',resizeable=0, toolbar=0, statusbar=0')
}

function cancelEvent()
{
	document.cancel_event.submit();
	return false;
}

function elementPosition(param){
	var x=0, y=0;
	var obj = (typeof param == "string") ? document.getElementById(param) : param;
	if (obj) {
		x = obj.offsetLeft;
		y = obj.offsetTop;
		var body = document.getElementsByTagName('body')[0];
		while (obj.offsetParent && obj!=body){
			x += obj.offsetParent.offsetLeft;
			y += obj.offsetParent.offsetTop;
			obj = obj.offsetParent;
		}
	}
	coord = new Array(x,y);
	return coord;
}

function chat_open()
{
	var new_window = window.open('index.php?page=chat_rooms','','width=600,height=500,scrollbars=no,resizeable=no');
}

function statusEdit(div, edit)
{
	div = document.getElementById(div);
	div.style.display = 'inline';

	prefix = 'is ';
	edit = document.getElementById(edit);
	edit.value = prefix;
	edit.focus();
	set_caret_position(edit, prefix.length, edit.value.length);
}

function set_caret_position(obj,start,end)
{
	if(document.selection)
	{
		if (obj.tagName == 'TEXTAREA')
		{
			var i = obj.value.indexOf("\r", 0);
			while (i != -1 && i < end)
			{
				end--;
				if(i < start)
				{
					start--;
				}
        i = obj.value.indexOf("\r", i + 1);
			}
		}
		var range = obj.createTextRange();
		range.collapse(true);
		range.moveStart('character', start);
		if (end != undefined)
		{
			range.moveEnd('character', end - start);
		}
    range.select();
	}
	else
	{
		obj.selectionStart = start;
		var sel_end = end == undefined? start: end;
		obj.selectionEnd = Math.min(sel_end, obj.value.length);
		obj.focus();
	}
}

function statusCancel(div)
{
	div = document.getElementById(div);
	div.style.display = 'none';
}

function statusSet(div, edit, div_status, div_status_add, status_edit, status_default, name, update_home, hide_form)
{
	var update_home = (update_home == null) ? true : update_home;
	var hide_form = (hide_form == null) ? true : hide_form;
	var xmlhttp = createXMLHttp();

	if (input = document.getElementById(edit))
	{
		statusText = input.value;
	}
	else
	{
		statusText = '';
	}
    statusText = statusText.substring(0, 139);

	server = 'smartway/ajax_server.php';
	xmlhttp.open('GET', server + "?action=statusSet&ajax_param_member_id=" + member_id + "&ajax_param_statusText=" + statusText,true);
	if (update_home==true)
	{
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState == 4)
			{
				/* Testing */
				/* Testing End */
				response = xmlhttp.responseText;
				if (response > 0)
				{
					document.getElementById(div_status).innerHTML = name + ' ' + statusText;
					if (statusText.length > 0)
					{
						document.getElementById(div_status_add).innerHTML = 'a moment ago';
						document.getElementById(status_edit).innerHTML = 'edit';
					}
					else
					{
						document.getElementById(div_status).innerHTML = '';
						document.getElementById(div_status_add).innerHTML = '';
						document.getElementById(status_edit).innerHTML = status_default;
					}
				}
			}
		}
	}
	xmlhttp.send(null);
	div = document.getElementById(div);
	if (hide_form==true)
	{
		div.style.display = 'none';
	}
}