// general use functions
String.prototype.trim = function ()
{
	return this.replace(/^\s*|\s*$/,"");
}

String.prototype.hasContent=function ()
{
	var x=/[a-zA-Z0-9]+/;
	return x.test(this);
}



// XML functions

function makeRequest(url,cmd)
{
	var httpRequest;
	var myCmd = cmd;

	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType)
		{
			httpRequest.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject)
	{ // IE
		try
		{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
				try
				{
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {}
		}
	}

	if (!httpRequest)
	{
		myErrorAlert('server');
		return false;
	}

	httpRequest.onreadystatechange = function() { parseResponse(httpRequest,myCmd); };
	// alert(url);
	httpRequest.open('GET', url, true);
	httpRequest.send('');

}

function parseResponse(httpRequest,cmd)
{
	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200)
		{
			// alert(cmd);
			if(cmd=='captcha_refresh')
			{
				// see if we have a valid state click
				parseCaptcha(httpRequest);
			}
		}
		else
		{
			alert(httpRequest.status);
			myErrorAlert('server');
		}
	}
}

function parseCaptcha(httpRequest)
{
	var captcha = httpRequest.responseXML.getElementsByTagName('captcha');
	var ident=getNodeValue(captcha[0],'ident');
	// alert(ident);
	var captcha_image=document.getElementById('captcha_image');
	var captcha_ident=document.getElementById('captcha_ident');
	if(ident.length>0)
	{
		captcha_image.src='captcha.php?captcha_ident=' + ident;
		captcha_ident.value=ident;
		// alert(captcha_ident_value);
	}
}

function getNodeValue(obj,tag)
{
	//return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
	if(obj)
	{
		var myObj = obj.getElementsByTagName(tag)[0];
		if(typeof(myObj)!='undefined')
		{
			if(myObj.firstChild)
			{
				if('nodeValue' in myObj.firstChild) return myObj.firstChild.nodeValue;
			}
		}
	}
	return '';
}

// grab value of cookie
function readCookie(cookieName)
{
	var myCookies = document.cookie;

	var pairs=myCookies.split(';');
	if(pairs.length>0)
	{
		for(x=0; x<pairs.length; x++)
		{
			p=pairs[x].split('=');
			// alert('"' + p[0] + '" : "' + cookieName + '"');
			if(p[0].charAt(0)==' ') p[0]=p[0].substr(1,p[0].length-1);
			if(p[0]==cookieName) return p[1];
		}
	}
	return false;
}
