var error_fields = new Array();
var error_elements = new Array();
var error_popup_obj = null;
var parent_popup_obj = null;

function getRandomNum(lbound, ubound)
{
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getRandomChar()
{
	var numberChars = "0123456789";
	var lowerChars = "abcdefghijklmnopqrstuvwxyz";
	var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
	var charSet = numberChars;
	charSet += lowerChars;
	charSet += upperChars;

	return charSet.charAt(getRandomNum(0, charSet.length));
}

function getPassword(length)
{
	var rc = "";
	if (length > 0)
		rc = rc + getRandomChar();

	for (var idx = 1; idx < length; ++idx)
	{
		rc = rc + getRandomChar();
	}
	return rc;
}

function checkLimit(len, divid)
{
	var maxlen = 250;
	var status = len>maxlen ? (len-maxlen)+" characters over limit" : "&nbsp;";
	var oDiv = document.getElementById(divid);
	if(!oDiv) return '';
	if(oDiv.innerHTML != status) oDiv.innerHTML = status;
	return status;
}

function generatePassword( destin_ids )
{
	var length = 8;
	var pwd = getPassword( length );
	for ( var i = 0; i < destin_ids.length; i++ )
		document.getElementById(destin_ids[i]).value = pwd;
}

function my_field_error(element, errorMessage)
{
	if ( errorMessage )
	{
		element.style.backgroundColor = "#FFE0A5";
		top.error_fields.push(errorMessage);
	}
	else
	{
		element.style.backgroundColor = "#FFF";
	}
}

function validate_shutdown()
{
	if ( top.error_fields.length )
	{
		if ( top.error_popup_obj )
		{
			if ( top.parent_popup_obj )
			{
				top.parent_popup_obj.hide();
			}
			top.document.getElementById(error_popup_obj.divId + "_error_content").innerHTML = top.error_fields.join("<br>");
			//top.popup_obj_popup_error.divContent.innerHTML = top.error_fields.join("<br>");
			top.error_popup_obj.show();
		}
		top.error_fields = new Array();
	}
}

function spinControl(name, step, min, max)
{
	var elem = document.getElementsByName(name)[0];
	if ( elem )
	{
		var nextVal = parseInt(elem.value) + step;
		if ( nextVal < min )
		{
			elem.value = min;
			return;
		}
		if ( nextVal > max )
		{
			elem.value = max;
			return;
		}
		elem.value = nextVal;
	}
}

function showPopup(str, x, y)
{
	var str3 = 'location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,modal=yes,alwaysRaised=yes,resizable=yes,status=no,';
	if (y < (screen.availHeight - 20))
		str3 += 'scrollbars=no,width=' + x + ',height=' + y + ',left=' + ((screen.availWidth-x)/2) + ',top=' + (screen.availHeight-y)/2;
	else
		str3 += 'scrollbars=yes,width=' + (x + 20) + ',height=' + (screen.availHeight - 50) + ',left=' + ((screen.availWidth-x)/2) + ',top=10';
	var newWindow = parent.window.open(str, getPassword(10), str3);
	newWindow.focus();
}

function visibleElement(current, all)
{

}

// *********  CWorldTime ***********************
function CWorldTime(divName){
	this.days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	this.months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	this.divName = divName;
	this.showTime = true;
	this.showDate = true;
	this.interval = 1000; // refresh time each second;
	this.localzone =0; //0=GMT, >0 west, <0 east
	this.timezone = ''; //
	this.delta = 0; // shift time in miliseconds from local time
	this.delta_server = 0; // shift time in miliseconds from server time
	this.AMPM = true; // show time in 12 or 24 hrs format
	this.IsDaylightSavingTime = false; // is daylight saving time now (summer) //not used now

	this.setTimezone = function(tz) {
		this.timezone = parseFloat(tz);
		this.delta = isNaN(this.timezone) ? 0 : (this.timezone - this.localzone) * 3600000;
	}

	this.stop = function() { var id=this.timerID; this.timerID = null; if(null!=id)window.clearTimeout(id);};
	this.start = function() {if(null!=this.timerID)stop(); var thisObj = this; this.tick();};
//	this.stop = function() { var id=this.timerID; this.timerID = null; if(null!=id)window.clearInterval(id);};
//	this.start = function() {if(null!=this.timerID)stop(); var thisObj = this; this.tick(); this.timerID = window.setInterval( function(){thisObj.tick();},this.interval);};
	this.tick = function() {
		var time = new Date();
		time.setTime(time.getTime()+this.delta + this.delta_server);
		this.display(time);
		var thisObj = this;
		this.timerID = window.setTimeout(function(){thisObj.tick();},this.interval);
	}
	this.display = function(time){
		var stime = '';
		if(this.AMPM)
		{
			stime = (time.getHours()>12) ?
					   this.leaderzero(time.getHours()-12) + ':' + this.leaderzero( time.getMinutes() ) + ' PM' :
					   this.leaderzero(time.getHours()) + ':' + this.leaderzero( time.getMinutes() ) + ' AM';
		}
		else
		  stime = this.leaderzero(time.getHours()) + ':' + this.leaderzero( time.getMinutes() ) + ':' + this.leaderzero( time.getSeconds() );
		var sdate = this.months[time.getMonth()] + ' ' + time.getDate();
		var oTime = document.getElementById(this.divName);
		s = "";
		if(this.showTime) s += "<strong>"+stime+"</strong>";
		if(this.showDate && this.showTime) s +=", ";
		if(this.showDate) s += sdate;
		if(oTime) oTime.innerHTML = s;
	}
	this.leaderzero = function(n){return n<=9 ? '0'+n : n;}
	this.setDiff = function( server_time )
	{
		var time = new Date();
		this.delta_server = server_time*1000 - time.getTime();


	}

	this.init = function(){
		var tz = new Date();
		//zonenow = tz.getTimezoneOffset()/60;
		this.localzone = -tz.getTimezoneOffset()/60;
		this.timezone = this.localzone;
		tz = new Date((new Date()).getFullYear(),0,1); //calculate localzone by winter time
		winterzone = -tz.getTimezoneOffset()/60;
		this.IsDaylightSavingTime = (winterzone != this.localzone);
	}
	this.init();
	return this;
}


function setNewHref( item, value )
{
	var re = new RegExp(item+'=\\\w+&', 'gi');
	var re2 = new RegExp('&' + item+'=\\\w+', 'gi');
	var re3 = new RegExp('\\?' + item+'=\\\w+', 'gi');
	var href = document.location.href;
	href = href.replace(re, '');
	href = href.replace(re2, '');
	href = href.replace(re3, '');
	href += href.indexOf('?')==-1 ? '?' : '&';
	href += item+'='+value;
	document.location.href = href;

}