var d = document;
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isNs = (navigator.appName.indexOf("Netscape") != -1);

function getE(id){
	return document.getElementById(id);
}

function defined(x){
	return typeof(x) != 'undefined' && x != null;
}

function addListener(element, event, func){
	if(element.attachEvent)
		element.attachEvent('on' + event, function(){ func(window.event) });
	else if(element.addEventListener)
		element.addEventListener(event, func, false);
}

function ShowWin(url,x,y,name,isscrollbars) {
	cx=screen.width / 2 - (x / 2);
	cy=(screen.height/2-(y/2));
    
    isscrollbars=(isscrollbars=="no")?"no":"yes";
	window.open(url,name,"toolbar=no,status=no,directories=no,menubar=no,resizable=yes,width="+x+",height="+y+",scrollbars="+isscrollbars+",top="+cy+",left="+cx);
}


function ShowWinMod(options) {
	if (!defined(options.url)) { alert('Не указана сылка на страницу!'); return false; }
	if (!defined(options.name)) { alert('Не указано название окна!'); return false; }
	if (defined(options.prx) && defined(options.pry)) {
		var x,y;
		y=screen.availHeight
		if(!y) y=screen.height
		x=screen.availWidth
		if(!x) x=screen.width
		x = parseInt(options.prx*x/100);
		y = parseInt(options.pry*y/100);
		cx=screen.width / 2 - (x / 2);
		cy=(screen.height/2-(y/2));
	} else if (defined(options.x) && defined(options.y)) {
		var x,y;
		x = options.x;
		y = options.y;
		cx=screen.width / 2 - (x / 2);
		cy=(screen.height/2-(y/2));
	} else { alert('Не заданы размеры окна!'); return false; }
	
	options.isscrollbars=(defined(options.isscrollbars))?((options.isscrollbars=="no")?"no":"yes"):"yes";
	options.toolbar=(defined(options.toolbar))?((options.toolbar=="no")?"no":"yes"):"no";
	options.status=(defined(options.status))?((options.status=="no")?"no":"yes"):"no";
	options.directories=(defined(options.directories))?((options.directories=="no")?"no":"yes"):"no";
	options.menubar=(defined(options.menubar))?((options.menubar=="no")?"no":"yes"):"no";
	options.resizable=(defined(options.resizable))?((options.resizable=="no")?"no":"yes"):"no";
	options.wlocation=(defined(options.wlocation))?((options.wlocation=="no")?"no":"yes"):"no";
	window.open(options.url,options.name,"toolbar="+options.toolbar+",location="+options.wlocation+",status="+options.status+",directories="+options.directories+",menubar="+options.menubar+",resizable="+options.resizable+",width="+x+",height="+y+",scrollbars="+options.isscrollbars+",top="+cy+",left="+cx);
}


function MOver(MySrc,MyColor) { MySrc.style.cursor="auto"; MySrc.bgColor=MyColor; }
function MOut (MySrc,MyColor) { MySrc.style.cursor="auto"; MySrc.bgColor=MyColor; }



function Selecter(Form, EName, S) {
    var f=Form;
    for ( i=0; i<f.length; i++ ) {
        if (f.elements[i].name==EName) {
            f.elements[i].checked=(S==1)?true:false;
        }
   }        
}



function ShowDivWindow(Name, Text, Action) {
    leerId=Name;
    LoadingHTML=Text;

	cx=(document.body.clientWidth/2)-(200/2);
	cy=(document.body.clientHeight/2)-(100/2);


/*    
	cx=screen.width/2;
	cy=screen.height/2;
*/
    
    if ((navigator.userAgent.indexOf("MSIE 5.5")==-1) && (navigator.userAgent.indexOf("MSIE 6")==-1)) {
//      cx += 90;
      cy-= 117;
    }

   
    if (document.all) {
        var leerElem = document.all[leerId];
        leerElem.innerHTML = LoadingHTML;
        leerElem.style.left = cx;
        leerElem.style.top = cy;
        leerElem.style.visibility = ((Action=="show")?"visible":"hidden");
    }
    else if (document.getElementById) {
        var leerElem = document.getElementById(leerId);
        leerElem.innerHTML = LoadingHTML;
        leerElem.style.left = cx;
        leerElem.style.top = cy;
        leerElem.style.visibility = ((Action=="show")?"visible":"hidden");
    }
    else if (document.layers) {
        document.layers[leerId].left = cx;
        document.layers[leerId].top = cy;
        document.layers[leerId].document.open();
        document.layers[leerId].document.write(LoadingHTML);
        document.layers[leerId].document.close();
        document.layers[leerId].visibility = ((Action=="show")?"show":"hide");
    }
}



function prepare_submit() {
	var form = document.getElementById('f_install');
	var inputs = form['informer']; 
	var values = '';
	var action = form.action;
	
	if (inputs != null){
		if (inputs.length != null) {				
			for (i = 0; i<inputs.length; i++){	
				//if (inputs[i].checked) {
					values += inputs[i].value+'_';
				//}								
			}		
			values = values.substr(0,values.length-1);						
		} else {
			//if (inputs.checked) {	
				values = inputs.value;
			//}	
		}	
	}
	
	if (values != '')
	{
		form.action = action + '&id=' + values;
	}
	
	return true;	
}


function get_qs(no_name) {
	var qs = '?';
	var form = document.getElementById(form_name);
	//var selects = form.getElementsByTagName('select');
	//var inputs = form.getElementsByTagName('input');
	

	var element = form.elements;


	if (element != null) {
		if (element.length != null) {
			for (i=0; i<element.length; i++){
				if (element[i].name != no_name)
				{
					qs += element[i].name+'='+element[i].value+'&';	
				}
			}		
		} else {
			if (element[i].name != no_name)
			{
				qs += element.name+'='+element.value+'&';
			}
		}
	}



	/*if (selects != null) {
		if (selects.length != null) {
			for (i=0; i<selects.length; i++){
				
					qs += selects[i].name+'='+selects[i].value+'&';
				}
			}		
		} else {
			qs += selects.name+'='+selects.value+'&';
		}
	}

	if (inputs != null) {
		if (inputs.length != null) {
			for (i = 0; i<inputs.length; i++){
				if (inputs[i].name != no_name)
				{
					qs += inputs[i].name+'='+inputs[i].value+'&';
				}
							
			}		
		} else {
			if (inputs.name != no_name)
			{
				qs += inputs.name+'='+inputs.value+'&';
			}
		}
	}*/

	return qs;
}



//-----------------------------------------------
function load_frame() {
	var qs = get_qs();
	var frame = window.parent.document.getElementById('frame_js');
	//window.opener

	frame.src = 'http://'+http+'/informers/'+controller_name+qs;	//	'/view'+
}


function fill_code() {						
	var HTML = '';
	
	var qs = get_qs('html');
	var area = window.parent.document.getElementById('code'+controller_id);
	//window.opener

	if (scripts != null){
		for (i = 0; i<scripts.length; i++){
			HTML = HTML + '<script language="javascript" src="http://'+http+scripts[i]+'"></script>';
		}
	}
	
	HTML = HTML + '<script language="javascript" src="http://'+http+'/informers/'+controller_name+qs+'"></script>';
	area.value = HTML;
}

function fill_a() {						
	var HTML;
	var qs = get_qs();
	var a = window.parent.document.getElementById('a'+controller_id);
	//window.opener
	a.href = '/informers/'+controller_name+'/edit'+qs;
}


function fill_div() {
	var qs = get_qs();
	var div = window.parent.document.getElementById('div'+controller_id);
	//window.opener
	var sUrl = "http://"+http+"/informers/"+controller_name+qs;

	div.innerHTML = loadHTML(sUrl);
}

function LoadCSS(name,n) {
	window.parent.document.getElementById(name).href = n;
	//window.opener
}
//-----------------------------------------------


function loadHTML(sURL)

{

  var request=null;



  // пытаемся создать объект для MSXML 2 и старше

  if(!request) try {

    request=new ActiveXObject('Msxml2.XMLHTTP');

  } catch (e){}



  // не вышло... попробуем для MSXML 1

  if(!request) try {

    request=new ActiveXObject('Microsoft.XMLHTTP');

  } catch (e){}



  // не вышло... попробуем для Mozilla

  if(!request) try {

    request=new XMLHttpRequest();

  } catch (e){}



  if(!request)

    // ничего не получилось...

    return "";

 

  // делаем запрос

  request.open('GET', sURL, false);

  request.send(null);



  // возвращаем текст

  return request.responseText;

}

function get_div(id) {
	var div = window.opener.document.getElementById(id);	
	if (!div) { alert('Ошибка фильтра: Отсутствует div с id = div_'+filter) } 
	return div.innerHTML;	
}


function set_div(id,content) {
	var div = window.opener.document.getElementById(id);	
	div.innerHTML = content;
	
}


/*function LoadCSS(Filehref) {
	var link = window.opener.document.createElement('link');
	window.opener.document.getElementsByTagName("body")[0].appendChild(link);
	link.rel = 'stylesheet';
	link.type = 'text/css';
    link.href = Filehref;
}
*/

function relcal() {

	Calendar.setup({
	inputField : "DaF", // ID of the input field
	ifFormat : "%d-%m-%Y", // the date format
	showsTime : false,
	button : "trigger_DaF" // ID of the button
	});

	Calendar.setup({
	inputField : "DaT", // ID of the input field
	ifFormat : "%d-%m-%Y", // the date format
	showsTime : false,
	button : "trigger_DaT" // ID of the button
	});

}




//----------------------------------------------------------------------------------------------------
// Global functions
//----------------------------------------------------------------------------------------------------
function getScrX() {
  var offset = 0;
  if(window.pageXOffset)
    offset = window.pageXOffset;
  else if(document.documentElement && document.documentElement.scrollLeft)
    offset = document.documentElement.scrollLeft;
  else if(document.body && document.body.scrollLeft)
    offset = document.body.scrollLeft;
  return offset;
}

function getScrY() {
  var offset = 0;
  if(window.pageYOffset)
    offset = window.pageYOffset;
  else if(document.documentElement && document.documentElement.scrollTop)
    offset = document.documentElement.scrollTop;
  else if(document.body && document.body.scrollTop)
    offset = document.body.scrollTop;
  return offset;
}

function getWinX() {
  var size = 0;
  if(window.innerWidth)
    size = window.innerWidth;
  else if(document.documentElement && document.documentElement.clientWidth)
    size = document.documentElement.clientWidth;
  else if(document.body && document.body.clientWidth)
    size = document.body.clientWidth;
  else size = screen.width;
  return size;
}

function getWinY() {
  var size = 0;
  if(window.innerHeight)
    size = window.innerHeight;
  else if(document.documentElement && document.documentElement.clientHeight)
    size = document.documentElement.clientHeight;
  else if(document.body && document.body.clientHeight)
    size = document.body.clientHeight;
  else size = screen.height;
  return size;
}

function absPosition(obj) {
    this.x = 0;
    this.y = 0;
    while (obj) {
        this.x += obj.offsetLeft;
        this.y += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return {x:this.x, y:this.y};
}


// перемещение div по экрану за полосой прокрутки
function JSFX_FloatTopDiv() {

	this.timer = null;
	this.ftlObj = {};
	this.startDiv = null;

	this.DeLayer = function(id) {
		var placeX = (d.body.clientWidth - d.body.clientWidth / 2),
		placeY = (d.body.clientHeight - d.body.clientHeight / 2) + 100;
		var GetElements = d.getElementById ? d.getElementById(id) : d.all ? d.all[id] : d.layers[id];
		if (d.layers)
			GetElements.style = GetElements;
		GetElements.sP = function(x,y) {
			this.style.right = x;
			this.style.top = y;
		};
		GetElements.x = placeX;
		GetElements.y = isNs ? pageYOffset + innerHeight : d.body.scrollTop + d.body.clientHeight;
		GetElements.y -= placeY;
		return GetElements;
	}

	this.add = function(divid) {
		this.ftlObj[divid] = this.DeLayer(divid);
	}
	
	this.stop = function() {
		clearTimeout(this.timer);
		clearInterval(this.timer);
		if (defined(this.startDiv)) {
			this.startDiv.style.display = 'none';
		}
		this.startDiv = null;
	}

	this.stayTopright = function() {
		placeY = (d.body.clientHeight - d.body.clientHeight / 2) + 100;
		var pY = isNs ? pageYOffset + innerHeight : d.body.scrollTop + d.body.clientHeight;
		this.startDiv.y += (pY - placeY - this.startDiv.y) / 15;
		this.startDiv.sP(this.startDiv.x, this.startDiv.y);
		var obj = this;
		this.timer = setTimeout(function() { obj.stayTopright(); }, 10);
	}

	this.start = function(divid) {
		this.stop();
		if (defined(this.ftlObj[divid])) {
			this.startDiv = this.ftlObj[divid];
			this.stayTopright();
		}
	}
}

JSFX = new JSFX_FloatTopDiv();
