var json = "";
var error_good = new Array();
var error_fields = new Array();
var error_ids = new Array();
var all_elements = 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 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 = "#e2a5ff";
        top.error_fields.push(errorMessage);
    }
    else
    {
        element.style.backgroundColor = "#FFF";
    }
}


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 writeBookmarkLink(url, title)
{
	try {
		if (window.sidebar) { // Mozilla Firefox Bookmark
			return document.write('<a href="javascript:createBookmarkLink(\'' + url + '\', \'' + title + '\')");">Bookmark this page</a>'); 
		} else if (window.external) { // IE Favorite
			return document.write('<a href="javascript:createBookmarkLink(\'' + url + '\', \'' + title + '\')");">Bookmark this page</a>'); 
		} else if (window.opera && window.print) { // Opera Hotlist
			return document.write('<a rel="sidebar" href="' + url + '" title="' + title + '">Bookmark this page</a>');
		} 
	} catch(err) {
		// do nothing
	}
}

function createBookmarkLink(url, title) {
	try {
		if (window.sidebar) { // Mozilla Firefox Bookmark
			return window.sidebar.addPanel(title, url, "");
		} else if( window.external ) { // IE Favorite
			return window.external.AddFavorite( url, title); 
		} else if(window.opera && window.print) { // Opera Hotlist
			return true; 
		}
	} catch(err) {
		// do nothing
	}
}

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;
}
