function __xmlhttp_request()
{
	var oXMLHttp = null;
	
	try
	{
		oXMLHttp = new XMLHttpRequest();	
	}
	catch(e)
	{
		var aHttpVersions = new Array
		(
		 	"MSXML2.XMLHTTP.6.0",
			"MSXML2.XMLHTTP.5.0",
			"MSXML2.XMLHTTP.4.0",
			"MSXML2.XMLHTTP.3.0",
			"MSXML2.XMLHTTP",
			"Microsoft.XMLHTTP"
		);
		
		for(i=0 ; i<aHttpVersions.length ; i++)
		{
			try
			{
				oXMLHttp = new ActiveXObject(aHttpVersions[i]);	
			}
			catch(e) 
			{
				//
			}
		}
	}
	
	if(oXMLHttp != null)
	{
		return oXMLHttp;	
	}
	else
	{
		return false;
	}
}

/*new code*/
Array.prototype.inArray = function(v)
{
	for(var i in this)
	{
	    if(this[i] == v)
		{
		      return true;
		}
	}  
	return false;
}

function __remove_checkboxes(divID,relVal)
{
	var divOB = document.getElementById(divID);
	var checked = new Array();
	var form = document.forms['theForm'];
	
	for(i=j=0; i<form.length; ++i)
	{
		if(form[i].type == "checkbox" && form[i].checked)
		{
			checked[j] = form[i].value; 
			++j;
		}
	}
	
	if(divOB)
	{
		divOB.innerHTML = '';
	}
	
	return checked;
}

function __get_selected_value(element)
{
	var oForm = document.forms['theForm'];
	
	if(oForm)
	{
		if(oForm[element])
		{
			return oForm[element].options[oForm[element].options.selectedIndex].value;	
		}
	}
}

function __select_default_option(element,variable)
{
	var oForm = document.forms['theForm'];
	
	if(oForm)
	{
		if(oForm[element])
		{
			for(i=0 ; i<oForm[element].options.length ; i++)
			{
				if(oForm[element].options[i].value == variable)
				{
					oForm[element].options[i].selected = true;
					
					break;
				}
			}
		}
	}
}

function __remove_select_options(element)
{
	var oForm = document.forms['theForm'];
	
	if(oForm)
	{
		if(oForm[element])
		{
			for(i=oForm[element].options.length ; i>=0 ; i--)
			{
				oForm[element].options[i] = null;	
			}
		}
	}
}

function __add_default_option(element)
{
	var oForm = document.forms['theForm'];
	
	if(oForm)
	{
		if(oForm[element])
		{
			oForm[element].options[0] = new Option('','');
		}
	}
}

function __add_tag()
{
	var oForm = document.forms['theForm'];
	
	if(oForm)
	{
		if(oForm['tag_new'])
		{
			if(oForm['tag_new'].value.replace(/\s+/g).length == 0)
			{
				alert('Please enter tag first');
				oForm['tag_new'].focus();
				return false;
			}
			else
			{
				jQuery.ajax
				(
					{
						type: 		'POST',
						url: 		'rpc.php?mode=add_tag&xml=true',
						data: 		'mode=add_tag&tag_new=' + oForm['tag_new'].value,
						dataType:	'xml',
						success: 	function(xml)
						{	
							id = 0;
							
							var checked = __remove_checkboxes('tags','tag');
							var html = '';
							
							if(jQuery(xml).find('tag').length > 0)
							{
								
								jQuery(xml).find('tag').each
								(
									function()
									{
						            	html += '<a href="" onclick="__delete_tag('+jQuery(this).attr('id')+'); return false;"><img src="images/b_trash.gif" border="0" align="absbottom" alt="delete category" style="margin-bottom: 3px;" /></a>';
										html += '<input type="checkbox" name="tags['+jQuery(this).attr('id')+']" id="tag'+jQuery(this).attr('id')+'"';
										if(jQuery(this).attr('checked') == 'true' || checked.inArray(jQuery(this).attr('id')))
										{
											html += ' checked ';
										}
						                html += 'value="'+jQuery(this).attr('id')+'" rel="tag" /> <label for="tag'+jQuery(this).attr('id')+'">'+jQuery(this).text()+'</label>';
										html += '<br/>';
									}
								);
							}
							
							document.getElementById('tags').innerHTML = html;
							oForm['tag_new'].value = '';
						}
					}
				);
			}
		}
	}
}

function __delete_tag(subID)
{
	var oForm = document.forms['theForm'];
	
	if(oForm)
	{
		var id = subID;
		
		if(id != '')
		{
			if(confirm('Are you sure you want to DELETE this tag ? '))
			{
				jQuery.ajax
				(
					{
						type: 		'POST',
						url: 		'rpc.php?mode=delete_tag&xml=true',
						data: 		'mode=delete_tag&id=' + id,
						dataType:	'xml',
						
						success: 	function(xml)
						{	
							var checked = __remove_checkboxes('tags','tag');
							var html = '';
							
							if(jQuery(xml).find('tag').length > 0)
							{
								
								jQuery(xml).find('tag').each
								(
									function()
									{
						            	html += '<a href="" onclick="__delete_tag('+jQuery(this).attr('id')+'); return false;"><img src="images/b_trash.gif" border="0" align="absbottom" alt="delete category" style="margin-bottom: 3px;" /></a>';
										html += '<input type="checkbox" name="tags['+jQuery(this).attr('id')+']" id="tag'+jQuery(this).attr('id')+'"';
										if(jQuery(this).attr('checked') == 'true' || checked.inArray(jQuery(this).attr('id')))
										{
											html += ' checked ';
										}
						                html += 'value="'+jQuery(this).attr('id')+'" rel="tag" /> <label for="tag'+jQuery(this).attr('id')+'">'+jQuery(this).text()+'</label>';
										html += '<br/>';
									}
								);
							}
							
							document.getElementById('tags').innerHTML = html;
							oForm['tag_new'].value = '';
						}
					}
				);	
			}
		}
		else
		{
			oForm['position'].focus();
			
			return false;
		}
	}
}
