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_startup()
{
    top.error_fields = new Array();
}

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();
            if (top.popup_obj_popup_service) {
                top.popup_obj_popup_service.hideLoader();
            }
        }
        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 = 'scrollbars=1,location=0,menubar=0,toolbar=0,dependent=1,dialog=1,modal=1,alwaysRaised=1,resizable=0,status=0,';
    if (y < (screen.availHeight - 20))
        str3 += 'width=' + x + ',height=' + y + ',left=' + ((screen.availWidth-x)/2) + ',top=' + (screen.availHeight-y)/2;
    else
        str3 += '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</a>');
		} else if (window.external) { // IE Favorite
			return document.write('<a href="javascript:createBookmarkLink(\'' + url + '\', \'' + title + '\')");">Bookmark</a>');
		} else if (window.opera && window.print) { // Opera Hotlist
			return document.write('<a rel="sidebar" href="' + url + '" title="' + title + '">Bookmark</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;
}


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;

}

function quest(id){
	if($(id).style.display=='none'){
		$(id).style.display='block';
	}else{
		$(id).style.display='none';
	}
}

function getCookie ( cookie_name )
{
	var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  	if ( results )
  		return ( unescape ( results[1] ) );
  	else
    	return null;
}

function deleteCookie(cookie_name) {
	setCookie(cookie_name,"",-1);
}

function setCookie(cookie_name, cookie_value, cookie_expire, cookie_path, cookie_domain, cookie_secure) {
	var expire_string;
	var cookie_string = cookie_name + "=" + cookie_value;
	if (cookie_expire) {
		var expire_date = new Date();
		var ms_from_now = cookie_expire * 24 * 60 * 60 * 1000;
		expire_date.setTime(expire_date.getTime() + ms_from_now)
		expire_string = expire_date.toGMTString();

		cookie_string += "; expires=" + expire_string;
	}

	if (cookie_path) {
		cookie_string += "; path=" + cookie_path;
	}

	if (cookie_domain) {
		cookie_string += "; domain=" + cookie_domain;
	}

	if (cookie_secure) {
		cookie_string += "; true";
	}

	document.cookie = cookie_string;
}