function setCookie(name, value) {
	var expire = new Date();
	expire.setTime(expire.getTime() + 30*24*60*60*1000);

	document.cookie = name + "=" + escape(value) + "; expires=" + expire.toGMTString() + "; path=/";
}
	
function removeCookie(name) {
	var expire = new Date();
	expire.setTime(expire.getTime() - 30*24*60*60*1000);
		
	document.cookie = name + "=" + escape('remove') + "; expires=" + expire.toGMTString() + "; path=/";
}
	
function getCookie(name, value) {
	var search = name + "=";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";", offset); // set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;

			return unescape(document.cookie.substring(offset, end));
		}
	}

	return value;
}

function isIdentifier(s) {
	if (s.length < 2)
		return false;
		
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (('\\' == c) || ('/' == c) || (':' == c) || ('*' == c) || ('?' == c) || ('"' == c) || ('<' == c) || ('>' == c) || ('|' == c) || (',' == c) || ('%' == c) || ('&' == c) || ('=' == c) || ('+' == c)) 
			return false;
		else 
			continue;
	} 

	return true;
}
	
function isEmail(s) {
	if (s.length < 4 || s.length > 40)
		return false;

	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (('_' == c) || ('-' == c) ||
			('@' == c) || ('.' == c) ||
			(('A' <= c) && (c <= 'Z')) ||
			(('a' <= c) && (c <= 'z')) ||
			(('0' <= c) && (c <= '9')))
			continue;
		else 
			return false;
	} 
		
	if ((i = s.lastIndexOf('@')) == -1)
		return false;
		
	if (s.charAt(0) == '@' || s.charAt(s.length-1) == '@')
		return false;
		
	var strTail = s.substring(i+1, s.length);
	if (strTail.length < 3)
		return false;
	if ((i = strTail.lastIndexOf('.')) == -1)
		return false;
			 
	var strTailEnd = strTail.substring(i+1, strTail.length);
	if (strTailEnd.length == 0)
		return false;
		
	return true;
}
	
function submitEmail(text, alerttext) {
	if (!isEmail(text.value)) {
		alert(alerttext);
		text.focus();
		return false;
	}
		
	return true;
}

function submitIdentifier(text, alerttext) {
	if(!isIdentifier(text.value)) {
		alert(alerttext);
		text.focus();
		return false;
	}
		
	return true;
}
	
function submitEqual(text1, text2, alerttext) {
	if(text1.value != text2.value) {
		alert(alerttext);
		text2.focus();
		return false;
	}
		
	return true;
}
	
function submitText(text, alerttext) {
	if (text.value.length == 0) {
		alert(alerttext);
		text.focus();
		return false;
	}
		
	return true;
}
	
function submitChoice(radio, alerttext) {
	for (var i=0; i<radio.length; i++) {
		if (radio[i].checked == "1") 
			return true;
	}
		
	alert(alerttext);
	return false;
}

function submitFloat(text, alerttext) {
	if (isNaN(parseFloat(text.value))) {
		alert(alerttext);
		text.focus();
		return false;
	}
		
	return true;
}
	
function submitInteger(text, alerttext) {
	if (isNaN(parseInt(text.value))) {
		alert(alerttext);
		text.focus();
		return false;
	}
		
	return true;
}
	
function submitQuantity(nMin, nMax, nDelta, Quantity, alerttext) {
	var nQuantity = parseInt(Quantity.value);
	if (nQuantity == Math.NaN || nQuantity < nMin || nQuantity > nMax || ((nQuantity - nMin) % nDelta) != 0) {
		alert(alerttext);
		Quantity.focus();
		return false;
	}
		
	return true;
}


	
var form;
var strElapsed;
var nProgress;
	
function startProgress(f, s) {
	form = f;
	strElapsed = s;
	nProgress = 0;
	showProgress();
}
	
function showProgress() {
	form.Progress.value = nProgress + strElapsed;
	nProgress ++;
	setTimeout("showProgress()", 1000);
}


function findMember(options, member) {
	for (var i=0; i<options.length; i++) {
		if (options[i].value == member) { 
			return i; 
		}
	}
	return -1;
}
 
function enterMember(member) {
	var options = parent.frChatMembers.document.foChatMembers.Member.options;
	var n = findMember(options, member);
	if (n == -1) {
		var tmp = new Option(member, member);
		options[options.length].value=tmp;
		options.length++;
	}
}

function leaveMember(member) {
	var options = parent.frChatMembers.document.foChatMembers.Member.options;
	var n = findMember(options, member);
	if (n != -1) {
		for (var i=n+1;i<options.length;i++) {
			options[i-1].text=options[i].text;
			options[i-1].value=options[i].value;
		}
		options[options.length-1]=null;
		options.length--;
		
		var choice = parent.frChatMembers.document.foChatMembers.Member;
		if (n==choice.selectedIndex) choice.selectedIndex=0;
		if (n<choice.selectedIndex) choice.selectedIndex=n-1;
		if (n>choice.selectedIndex) choice.selectedIndex=n;
	}
}




function HideShow(obj) 
{
	if(document.getElementById(obj).style.display=="none")
	    document.getElementById(obj).style.display="inline";
	else
	    document.getElementById(obj).style.display="none";
}

function Hide(obj) 
{
	document.getElementById(obj).style.display="none";
}

function Show(obj) 
{
	document.getElementById(obj).style.display="inline";
}


function toggleZOOM() 
{
	if( ZOOM == "+" )
	    ZOOM = "-";
	else
	    ZOOM = "+";
}

function zoomPicture(obj) 
{
    	if( ZOOM == "+" ) 
        	document.getElementById(obj).width  = document.getElementById(obj).width*1.1;
    	else 
        	document.getElementById(obj).setAttribute("width",  document.getElementById(obj).getAttribute("width")*0.9);
}

function adjustPicture(obj1, obj2, obj3, factor) 
{
	if( document.getElementById(obj3) )
		document.getElementById(obj3).style.display = 'none';

	if( document.getElementById(obj1) && document.getElementById(obj2) && document.getElementById(obj3) )
	{
		var wdth = document.getElementById(obj1).width;
		var clientwidth = document.getElementById(obj2).clientWidth;
		if( wdth <= 0 && clientwidth <= 0 )
		{
			setTimeout(function() {adjustPicture(obj1, obj2, obj3, factor);}, 10);
		} else
		{	
        		document.getElementById(obj1).width =  clientwidth * factor;
			document.getElementById(obj3).style.display = 'inline';
		}
	} else
	{
		setTimeout(function() {adjustPicture(obj1, obj2, obj3, factor);}, 10);
	}
}



function openUrl(url, target, timeout)
{
	var cmd = "window.open('" + url + "', '" + target + "')";
	setTimeout(cmd, timeout);
}


var ajax = {

	doCommand: function(url) {			//this function just send out a request, does not expect any response and do not intend to handle any response
		this._doCommand(url);
	},

	_doCommand: function(url) {
		var req;
		if( window.XMLHttpRequest ) 
		{
			req = new XMLHttpRequest();
		} else 
		if( window.ActiveXObject ) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		req.onreadystatechange = function() {;}	//do nothing
		req.open("GET", url, true);
		req.send(null);
		return;
	},



	doGet: function(url, XMLOrText, callbackfunction, passthrough) {
		this._doGet(url, XMLOrText, callbackfunction, passthrough);
	},

	_doGet: function(url, XMLOrText, callbackfunction, passthrough) {
		var req;
		if( window.XMLHttpRequest ) 
		{
			req = new XMLHttpRequest();
		} else 
		if( window.ActiveXObject ) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}


		var ctx = {req: req,
			   url: url,
			   XMLOrText: XMLOrText,
			   callbackfunction: callbackfunction,
			   passthrough: passthrough};

		req.onreadystatechange = function() {
			ajax._processResponse(ctx);
		}

		req.open("GET", url, true);
		req.send(null);
		return;
	},

	_processResponse: function(ctx) {
		var req = ctx.req;
		if( req.readyState == 4 )
		{
			if( req.status == 200 )
			{
				var callbackfunction = ctx.callbackfunction;
				if( !callbackfunction )							//the callbackfunction might not be available yet, suc as loading from an external .js file
				{
					setTimeout(function() {ajax._processResponse(ctx);}, 100);	//the script will keep looping until a callbackfunction is available, it might run into a infinite looping if can not get the callbackfunction for whatever the reasons
				}

				if( callbackfunction )
				{
					if( ctx.XMLOrText )
					{
						callbackfunction(req.responseXML,  ctx.passthrough);
					} else
					{
						callbackfunction(req.responseText, ctx.passthrough);
					}
				}
			} else
			{
				//alert("There was a problem retrieving the XML data: " + req.statusText);
			}
		}
		return;
	}
};


function toggleElementDisplay(obj)
{
	var href = document.location.href;
	if( href.charAt(0) == 'h' && href.charAt(1) == 't' && href.charAt(2) == 't' && href.charAt(3) == 'p' )
	{
		var value = document.getElementById(obj).style.display;
		if( value == "inline" )
		{
	    		document.getElementById(obj).style.display="none";
			value = "none";
		} else
		{
	    		document.getElementById(obj).style.display="inline";
			value = "inline";
		}


		var url1 = "/servlet/Page?ElementId=" + obj + "&display=" + value;
		ajax.doCommand(url1);
		var url2 = "/servlet/GetAds?ElementId=" + obj + "&display=" + value;
		ajax.doCommand(url2);

		//setTimeout("javascript:document.location.reload(true);", 150);
		//setTimeout("javascript:document.frames('GlobalIFrame').document.location='/servlet/RefreshSessionCss';", 150);
  	} else
	{
		HideShow(obj);
	}
}


function isLocalAccess(responsetext, context) 
{
	if( responsetext != '1' && context && context.elementid )
		document.getElementById(context.elementid).style.display = "none";
}

