var ULBox=false;  //view div
var cursorTimer,CursorDiv = false;
var PopupFullScreenSelfX,PopupFullScreenSelfY;
var DOMAIN="http://"+document.domain+"/";
var CONFIG = new Object;
var USER = new Object;
var GLOBALS = new Object;
var CONTAINERS = new Object;

//--------------events---------------
var pageX, pageY;
var browsemode = false;                                  // crossbrowsers
if(navigator.appName=="Netscape"){browsemode=true;} // Mozilla
if(navigator.appName=="Microsoft Internet Explorer"){var IE=true;} // IE must die =)

//---- browsers
if (window.navigator.userAgent.indexOf ("MSIE") >= 0)
{
	ua = 'Explorer';
	uaVers=window.navigator.userAgent.substr(window.navigator.userAgent.indexOf("MSIE")+5,1);
	if(parseInt(uaVers)==6){var IE6 = true;}
}

function isObject(v)
{	if(typeof(v)=='object') {return true;}else{return false;}}

function isNumber(x)
{
  return ( (typeof x === typeof 1) && (null !== x) && isFinite(x) );
}


//-----------------------PxToInt()--------------
function PxToInt(v)
{
   v=v.replace("px","");
   v=parseInt(v);
   return v;
}
//-----------------------MyDOMGetChilds()--------------
function MyDOMGetChilds(el)
{
	var retArr = new Array;
	var j = 0;
	var i = 0;
	for(var i = 0;i<el.childNodes.length;i++)
	{
		if(el.childNodes[i].nodeType==1){retArr[j] = el.childNodes[i];j++};
	}
	return retArr;
}
// --- trim()------------------
function trim (str)
{
    if(!str || str==undefined){return ;}
	str = str.replace(/^\s+/, '');
	for (var i = str.length; --i >= 0;) {
	        if (/\S/.test(str.charAt(i))) {
	                str = str.substring(0, i + 1);
	                break;
	        }
	}
	return str;
}

//-------------StatHelp()---------------
function StatHelp(from,w)
{
     if(!w){w=200;} // default width
     div=ge('stathelp');
     from=ge(from);
     str=from.innerHTML;
     div.innerHTML=str;
     div.style.display="block";
     if(!browsemode && navigator.appName!='Opera'){div.style.position = 'fixed';}
     div.style.width=w+'px';
     div.style.top=pageY+4+'px';
     div.style.left=pageX+4+'px';
}
//--------------StatHelpDivOff()---------------
function StatHelpDivOff()
{
   d=ge("stathelp");
   d.style.display="none";
   d.innerHTML="";
}


function MyBoxMove(elId, toY)
{
	var e = ge(elId);
	var dY, curY = Ext.get(elId).getTop(true);
	var delay;  // ms
	var step = 8; //px

	dY = Math.abs(toY-curY);
	delay = 100/dY;
	//if(delay<100){step = 2;}
	if(delay>100){delay = 100;step = 1}

	if(curY<toY)
	{
		Ext.get(elId).setTop((curY+step));
	}else{return ;}

	setTimeout("MyBoxMove('"+elId+"',"+toY+")", delay);
}

function ToCursor(str,opt)
{
    var anime = {
    	duration: 2
    };
    if(!str){return ;}
    if(!opt){opt = new Object; opt.autoHide = true; opt.title = 'Информация';}
    var box = Ext.get('userLogBox'),dataBox = ge('userLogData');
    if(opt.title){ge('userLogTitle').innerHTML = opt.title;}else{ge('userLogTitle').innerHTML = 'Информация';}
    dataBox.innerHTML += str;
    if(ULBox)
    {
    	clearTimeout(cursorTimer);
    	cursorTimer=setTimeout('CursorDivOff()',5000);
    	return ;
    }
    if(IE){box.show();}else{box.show(anime);}
    //box.show();
    //box.moveTo(20,100,anime);
    MyBoxMove('userLogBox',0);
    ULBox = true;
   //ge('userLogBox').style.position = 'fixed';

    if(opt.autoHide){cursorTimer=setTimeout('CursorDivOff()',5000);}
}


function CursorDivOff()
{
   var anime = {
    	duration: 2,
    	callback: CursorAfterShow
    };
    var box = Ext.get('userLogBox'),dataBox = ge('userLogData');
    box.hide(anime);

    ULBox = false;
    //Ext.get('userLogBox').setTop(-200);
}
function CursorAfterShow()
{
    var box = Ext.get('userLogBox'),dataBox = ge('userLogData');
    dataBox.innerHTML = '';
    box.setTop(-200);
}


function HideDiv(id)
{
        document.getElementById(id).style.height="1px";
        document.getElementById(id).style.visibility="hidden";
        document.getElementById(id).style.overflow="hidden";
}


function UploaderLog()
{
  log=document.getElementById('uploader').innerHTML;
  //alert(log);
}

var zoomId=null;
var zoomMode=null;

function sh(id)
{
   div=ge(id);
   if(id=='popup2'){PopupFullScreenState = false;}
   if(!div){alert("not "+id);return ;}
   if (div.style.display!="none" && div.style.display){div.style.display="none";}else{div.style.display="block";}
   return false;
}

function ge(str)
{
    return document.getElementById(str);
}

function onsf(id)
{   if(ge('htmlEdField_'+id))
   {
   	    ge('htmlEdField_'+id).value = htmlEd.getValue();
   }

   if(ge('cityListXX'))                        // for city list
   {
   	  var cityV = MyCityList.exportValues();
   	  if(cityV.city){ge('cityListXX').value = cityV.city;}else{ge('cityListXX').value = 0;}
   }
   return true;}

function sf(id)
{
   onsf(id);
   //alert(ge(id).innerHTML);
   ge(id).submit();
}

var smilediv;
function csd(id)
{
   smilediv=id;
}

function pst(str)
{
   div=document.getElementById(smilediv);
   if(div){div.value+=" "+str;}
  // sh(smileblock);
}
var smileblock;                //block with smile img
function SetSmileDiv(id)
{
 smileblock=id;
}

function rand(a,b)
{
   return a+(b-a)*Math.random();
}

function print_r(ob)
{
	var str = '';	for(var a in ob)
	{		str = str + a + '  ';	}
	alert(str);
	return str;}

function gd(id)
{
	var data2,data=document.getElementById(id);
	data2 = data.innerHTML;
	data.innerHTML = '';
	return data2;
}

var popupFlag = false,winPopUp;
function newPopup(fromId,name,opt)
{
 var defW = 400;
 var defOpt =  {width:400,height:'auto',modal:false,closable:true,maximizable:false,collapsible:false,resizable:true}

 if(!isObject(opt)){var opt = new Object();}for(var a in defOpt)
{	if(typeof(opt[a])=='undefined'){opt[a] = defOpt[a];}}

 var winData = gd(fromId);
 if(!winData){return ;}

 if(popupFlag){winPopUp.my_hide();}

 winPopUp = new Ext.Window({
            title    : name,
            width    : opt.width,
            height : opt.height,
            maximizable : opt.maximizable,
            collapsible: opt.collapsible,
            plain    : true,
            closable: opt.closable,
            resizable:opt.resizable,
            draggable : true,
            modal:opt.modal,
            closeAction :'my_hide',
            fromId: fromId,
            html: winData,
            my_html:winData
        });

   winPopUp.my_hide = function()
   {
   	   var d = ge(this.fromId);
   	   var tid = this.id;
   	   tid = ''+tid+'';
   	   if(d){d.innerHTML = this.my_html;}
   	   this.close();
   	   popupFlag = false;               //!!!!!!!!
   }
        winPopUp.show();
        popupFlag = true;

        return winPopUp;
}


function setCookie (name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + (expires) );
	//alert(expires_date);

      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires_date : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


function CityChoose()
{
	if(ge('city_list').value==0)
	{
		Ext.get('otherCityBox').show();
		Ext.get('otherCityField').focus();
	}else{
		Ext.get('otherCityBox').setDisplayed('none');
		ge('otherCityField').value = '';
	}
}

function RefreshCaptchaImg()
{
  ge("regimg").src="../regimg.php?r="+Math.random();
}

function CheckCaptcha()
{
	var s = ge('captchaStr').value;
    if(s.length!=5)
    {
    	 MyAlert('Неверное количество символов кода! Пожалуйста, введите <b>5</b> символов, изображенных на картинке.');
    	 return ;
    }
    goajax('captchaAjaxAnswer','pd_act=captchaCheck&captchaStr='+s);
}

function MyAlert(str)
{
	Ext.MessageBox.show({
           msg: str,
           title: 'Информация',
           width:300,
           draggable:false,
           buttons: Ext.MessageBox.OK,
           icon: Ext.MessageBox.INFO
           //wait:true,
           //waitConfig: {interval:200},
           //icon:'ext-mb-download', //custom class in msg-box.html
           //animEl: 'mb7'
       });

}

function MyError(str)
{
	Ext.MessageBox.show({
           msg: str,
           title: 'Информация',
           width:300,
           buttons: Ext.MessageBox.OK,
           icon: Ext.MessageBox.ERROR
           //wait:true,
           //waitConfig: {interval:200},
           //icon:'ext-mb-download', //custom class in msg-box.html
           //animEl: 'mb7'
       });

}

function MyHide(id)
{	Ext.get(id).hide({duration:1.5,callback:MyHideCallBack});}

function MyHideCallBack(e)
{	ge(e.id).style.display = 'none';}


function ctrlEnter(event, formElem)
{
    if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
    {
           sf(formElem.id);
    }
}

function myCtrlEnter(event,toEval)
{
    if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
    {
           eval(toEval);
    }
}

function objectEqual(a1,a2)
{	if(!isObject(a1) || !isObject(a2)){return ;}

	for(var a in a1)
	{		if(a1[a]!=a2[a]){return ;}	}

	return true;}

function UserLogLocal(elId,str)
{	var el = ge(elId);
	if(!el){return ;}
	el.style.display = 'none';
	el.innerHTML = str;
	Ext.get(elId).show({duration: 2});}