﻿// StringToArray13(s) - возвращает массив строк разделённых по #13 ("\r")
function StringToArray13(s){
	var r=new Array(), i=0, b=0, z;
	while(true){
		z=s.indexOf("\r",b);
		if(z<0) {
			r[i]=s.substr(b);
			break;
		}
		else{
			if(z<=b) r[i]='';
			else r[i]=s.substring(b,z);
			b=z+1;
			i++;
		}
	}
	return r;
}

function nl2br(s){
	var r=StringToArray13(s);
	s='';
	if(r.length>0){
		for(i=0;i<r.length;i++){
			if(i>0) s=s+'<br/>';
			s=s+r[i];
		}
	}
	return s;
}

function FindCSSRule(name){
	var r=null;
	if(document.styleSheets.length>0){
		var j;
		for(var i=0;(i<document.styleSheets.length)&&(!r);i++){
			if(document.styleSheets[i].cssRules) {
				if(document.styleSheets[i].cssRules.length>0){
					for(j=0;(j<document.styleSheets[i].cssRules.length)&&(!r);j++){
						if(document.styleSheets[i].cssRules[j].selectorText==name){
							r=document.styleSheets[i].cssRules[j];
						}
					}
				}
			}
			else if(document.styleSheets[i].rules) {
				if(document.styleSheets[i].rules.length>0){
					for(j=0;(j<document.styleSheets[i].rules.length)&&(!r);j++){
						if(document.styleSheets[i].rules[j].selectorText==name){
							r=document.styleSheets[i].rules[j];
						}
					}
				}
			}
		}
	}
	return r;
}

function findPositionWithScrolling( oElement ) {
  function getNextAncestor( oElement ) {
    var actualStyle;
    if( window.getComputedStyle ) {
      actualStyle = getComputedStyle(oElement,null).position;
    } else if( oElement.currentStyle ) {
      actualStyle = oElement.currentStyle.position;
    } else {
      //fallback for browsers with low support - only reliable for inline styles
      actualStyle = oElement.style.position;
    }
    if( actualStyle == 'absolute' || actualStyle == 'fixed' ) {
      //the offsetParent of a fixed position element is null so it will stop
      return oElement.offsetParent;
    }
    return oElement.parentNode;
  }
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    var originalElement = oElement;
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    if( !originalElement.parentNode || !originalElement.style || typeof( originalElement.scrollTop ) == 'undefined' ) {
      //older browsers cannot check element scrolling
      return [ posX, posY ];
    }
    oElement = getNextAncestor(originalElement);
    while( oElement && oElement != document.body && oElement != document.documentElement ) {
      posX -= oElement.scrollLeft;
      posY -= oElement.scrollTop;
      oElement = getNextAncestor(oElement);
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function OffsetInParent(element,parent){
//	dbg('lkjlkjlkjlkj');
//	var P;
	var pos=new Object();
	pos.x=0;
	pos.y=0;
	pos.parentFound=false;
  if(is_ie) {
		do{
			//dbg(P);
			pos.x+=element.offsetLeft;
			pos.y+=element.offsetTop;
			P=element.offsetParent;
			if(P){
	//			dbg(P.id); 
				element=P;
			} 
		}while((P!=parent)&&(P!=null)); 
		if((P!=null)&&(parent!=null)) pos.parentFound=true; 
	}
	else { //http://www.howtocreate.co.uk/tutorials/javascript/browserspecific
//		dbg('xernia');
	  xy=findPosition(element);
	  xy1=findPosition(parent);
		pos.parentFound=true;
		pos.x=xy[0]-xy1[0];
		pos.y=xy[1]-xy1[1];
//		dbg(element.offsetTop+' '+parent.offsetTop+'*'+element.y+' '+parent.y);
//		pos.x=element.offsetLeft-parent.offsetLeft;
//		pos.y=element.offsetTop-parent.offsetTop;
//		pos.x=element.x;
//		pos.y=element.y;
	} 
	return pos; 
}

function WindowHeight(){
	return (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
}

function WindowWidth(){
	return (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
}

//возвращает число, на которое заканчивается строка id
function GetLastNumber(id){
	var s=new String(id);
	var i=s.length-1;
	while((i>=0)&&(s.charAt(i)>='0')&&(s.charAt(i)<='9')) i--;
	i++;
	s=s.substr(i);
	if(s) i=new Number(s)
	else i=-1;
	return(i);
}

//возвращает имя файла из URL
function GetFileName(url){
	var s=new String(url);
	var i=s.lastIndexOf('/');
	return(s.substr(i+1));
}

function GetURLParam(url){
	var s=new String(url);
	var i=s.lastIndexOf('?');
	return(s.substr(i+1));
}

