function fetchAllChildNodes( elem )
{
	var fill = new Array();
	fetchAllChildNodesImpl(elem, fill);
	return fill;
}
	
function fetchAllChildNodesImpl( elem, fill )
{
	var childNodes = elem.childNodes;
	if(childNodes==null)
	{
		return;
	}
	for(var i=0; i<childNodes.length; i++)
	{
		var childNode = childNodes[i];
		fill.push(childNode);
		fetchAllChildNodesImpl(childNode, fill);
	}
}

function formValuesToMap( formElem )
{
	var data = new Array();

	var childNodes = fetchAllChildNodes(formElem);
	for(var i=0; i<childNodes.length; i++)
	{
		var childNode = childNodes[i];
		
		if(childNode.tagName == 'SELECT')
		{
			var val = childNode.options[childNode.selectedIndex].value;
			if((''+val) == '') // MSIE SUCKS !!!!
				val = childNode.options[childNode.selectedIndex].innerText;
			data[childNode.name] = val;
		}
		else if(childNode.tagName == 'TEXTAREA')
		{
			data[childNode.name] = childNode.value;
		}
		else if(childNode.tagName == 'INPUT')
		{
			if(childNode.type == 'checkbox')
  		{
  			// NOT W3C standard - but easier to handle
  			data[childNode.name] = childNode.checked ? 'true' : 'false';
  		}
  		else if(childNode.type == 'radio')
  		{
  			if(childNode.checked)
  			{
  				data[childNode.name] = childNode.value;
  			}
  		}
  		else
  		{
  			data[childNode.name] = childNode.value;
  		}
		}
		else
		{
			//window.alert('unsupported form element type: '+childNode.tagName);
		}
	}
	
	return data;
}

function setRadioChecked(formElem, name, value, disable)
{
	var childNodes = fetchAllChildNodes(formElem);
	for(var i=0; i<childNodes.length; i++)
	{
		var childNode = childNodes[i];
		
		if(childNode.tagName != 'INPUT') continue;
		if(childNode.type != 'radio')    continue;
		
		if(childNode.value == value)
			childNode.setAttribute('checked', 'yes');
		else
			childNode.removeAttribute('checked');
		
		if(disable)
			childNode.setAttribute('disabled', 'yes');
		else
			childNode.removeAttribute('disabled');
	}
}


function escapeArgs( argsArray )
{
	var result = '';
	for(var prop in argsArray)
		result += escape(prop)+'='+escape(argsArray[prop])+'&';
	if(result.length > 0)
		result = result.substring(0, result.length-1); // remove trailing '&'
	return result;
}

function checkTooShort( input, minlen, errorMessage )
{
	if(input == null || input.length < minlen)
	{
		window.alert(errorMessage);
		return true;
	}
	return false;
}

function checkEmpty( input, errorMessage )
{
	return checkTooShort(input, 1, errorMessage);
}

function checkEmail( input, errorMessage )
{
	var dotIndex = input.lastIndexOf('.');
	var atIndex1 = input.indexOf('@');
	var atIndex2 = input.lastIndexOf('@');
	if(atIndex1 != -1) // at least one @
	{
		if(atIndex1 == atIndex2) // exactly one @
		{
			if(dotIndex > atIndex1) // ~~~@~~~.~~~
			{
				return false;
			}
		}
	}
	
	window.alert(errorMessage);
	return true;
}

function checkDomain( input, errorMessage )
{
	var dotIndex = input.lastIndexOf('.');
	if(dotIndex != -1) // at least one .
	{
		return false;
	}
	
	window.alert(errorMessage);
	return true;
}

function checkEqual( op1, op2, errorMessage )
{
	if(op1 != op2)
	{
		window.alert(errorMessage);
		return true;
	}
	return false;
}

function replaceElement( from, to )
{
	var holder = from.parentNode;
	holder.insertBefore(to, from);
	holder.removeChild(from);
}


function ajax( method, action, post, responseEval, use_ajax_tunnel )
{
	var ms = new Date().getTime();
	action += ((action.indexOf('?')==-1)?'?':'&')+'random_seed='+ms;

	

	var httpAccess = false;

 	try
	{	
		httpAccess = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{	
		try
		{
			httpAccess = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			httpAccess = false;
		}
	}
	
	if (!httpAccess && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			httpAccess = new XMLHttpRequest();
		}
		catch (e)
		{
			httpAccess = false;
		}
	}
	
	if (!httpAccess && window.createRequest)
	{
		try
		{
			httpAccess = window.createRequest();
		}
		catch (e)
		{
			httpAccess = false;
		}
	}
	
	if(!httpAccess)
	{
		window.alert('ajax init error');
		return;
	}
	
	httpAccess.onreadystatechange = function()
	{
		if (httpAccess.readyState != 4) // response received
		{
			return;
		}
			
		if(httpAccess.status != 200)
		{
			window.alert('ajax status not 200: '+httpAccess.status);
		}
		else
		{
			if(responseEval != null)
			{
				responseEval(httpAccess.responseText);
			}
		}
	}
	
	if((typeof use_ajax_tunnel != 'undefined') && use_ajax_tunnel==true)
	{
		if(action.indexOf("http://") == 0)
		{
			action = action.substring(("http://").length);
			action = action.substring(action.indexOf('/'));
		}
		
		var getArgs = '';
		getArgs += 'ajax_tunnel_method='+method+'&';
		getArgs += 'ajax_tunnel_action='+escape(action);		
		action = 'http://www.trackconsole.com/ajax_tunnel.php?'+getArgs;
	}
	
	try
	{	
		httpAccess.open(method, action, true);
	}
	catch(e)
	{
		window.alert('ajax open error='+e.message+', action='+action);
		return;
	}
		
	try
	{
		if(post != null)
		{
			httpAccess.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			httpAccess.send(post);
		}
		else
		{
			httpAccess.send(null);
		}
	}
	catch(e)
	{
		return;
	}
}

function trim(value)
{
	value = value.replace(/^\s+/,''); 
	value = value.replace(/\s+$/,'');
	return value;
}


function createCookie( name, value, millisToLive )
{
	var date = new Date();
	date.setTime(date.getTime()+millisToLive);
	
	var aaa = escape(name) + "=" + escape(value);
	var bbb = "expires="+ date.toGMTString();
	var ccc = "path=/";
	
	document.cookie = aaa + "; " + bbb + "; " + ccc;
}

function readCookie(name)
{
	var parts = document.cookie.split(';');
	
	for(var i=0; i<parts.length; i++)
	{
		var part = trim(parts[i]);
		if (part.indexOf(name + "=") == 0)
			return part.substring(name.length + 1, part.length);
	}
	
	return null;
}

function eraseCookie(name)
{
	createCookie(name, "", -1);
}