//-----------------------------------------
// http://zso.lt Common JavaScript library
//-----------------------------------------

//-----------------------------------------
function elementDimension(element,which) {
//-----------------------------------------
    var x,y;
    var test1 = element.scrollHeight;
    var test2 = element.offsetHeight;
    if (test1 > test2) // all but Explorer Mac
    {
	    x = element.scrollWidth;
        y = element.scrollHeight;
    }
    else // Explorer Mac;
         //would also work in Explorer 6 Strict, Mozilla and Safari
    {
	    x = element.offsetWidth;
        y = element.offsetHeight;
    }
    if (which == "x") return x;
    else return y;
}

//---------------------------------------
function innerDimension(element,which) {
//---------------------------------------
    var x,y;
    if (self.innerHeight) // all except Explorer
    {
	    x = self.innerWidth;
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
    {
	    x = document.documentElement.clientWidth;
        y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
    	x = document.body.clientWidth;
        y = document.body.clientHeight;
    }
    if (which == "x") return x;
    else return y;
}

// get the true offset of anything on NS4, IE4/5 & NS6, even if it's in a table!
function getAbsX(elt) { 
    return (elt.x) ? elt.x : getAbsPos(elt,"Left"); 
}

function getAbsY(elt) { 
    return (elt.y) ? elt.y : getAbsPos(elt,"Top"); 
}

function getAbsPos(elt,which) {
 iPos = 0;
 while (elt != null) {
    iPos += elt["offset" + which];
    elt = elt.offsetParent;
 }
 return iPos;
}

//------------------------------------------------------------------------------------
function kep_ablak(img,imgw,imgh)
//------------------------------------------------------------------------------------
{
 imghx = imgh+20; // a kepek "latszolagos meretet" kicsit megnoveljuk, hogy az ablaknak legyen kerete
 imgwx = imgw+20;
  
 sh = screen.height;
 sw = screen.width;
    
 if ((sh>imghx) && (sw>imgwx)) 
 {
    oszto = 1; // minden OK, nem kell a kepen varialni
 }
 if ((sh>imghx) && (sw<imgwx)) // a magassag OK, de a szelesseg nem
 {
    oszto = imgwx/(sw-50);//korrekcios tenyezo az 50, mivel nem szeretnenk,
    //hogy az ablak pont a kepernyo szeleig erjen, kicsit jobban leosztjuk a mereteket
 }
 if ((sh<imghx) && (sw>imgwx)) // a szelesseg OK, de a magassag nem
 {
    oszto = imghx/(sh-50);//korrekcios tenyezo az 50
 }
 if ((sh<imghx) && (sw<imgwx)) // egyik sem OK
 {
    oszto1 = imgwx/(sw-50);
    oszto2 = imghx/(sh-50);
    if (oszto1>oszto2) oszto = oszto1;// a szelesseg jobban eltert e kepernyo szelessegetol, mint a magassag
    else oszto = oszto2;
 }

 imgh2 = imgh/oszto;//aranyosan csokkentem a kep mereteit
 imgw2 = imgw/oszto;

 imghx = imgh2+20;//ablakmeret korrekcio
 imgwx = imgw2+20;

 prop = "height="+imghx+",width="+imgwx;
 ujablak = open('','pic_window',prop);
 ujablak.document.open();
 ujablak.document.write("<html><body onblur='close();'><img src=\""+img+"\" height=\""+imgh2+"\" width=\""+imgw2+"\"\></body></html>");
 ujablak.document.close();
}

//--------------------------------------------------------------------------
function setCookie(name, value, expire) {
//--------------------------------------------------------------------------
  document.cookie = name + "=" + escape(value)
  + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

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

//--------------------------------
function externalLinks() {
//--------------------------------
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") &&
	anchor.getAttribute("rel") == "external")
    anchor.target = "_blank";
  }
} 

//--------------------------
function focusField(name) {
//--------------------------
    for(i=0; i < document.forms.length; ++i) {
        var obj = document.forms[i].elements[name];
        if (obj && obj.focus) {obj.focus();}
    }
}

//--------------------------------------------------------
function textCounter( field, countfield, maxlimit, lang ) {
//--------------------------------------------------------
    //source: http://inmyexperience.com/archives/000327.shtml
    if ( field.value.length > maxlimit ){
        field.value = field.value.substring( 0, maxlimit );
        if (lang=="hu") {
            alert( 'A szövegmező maximum '+maxlimit+' karakter hosszú lehet.' );
        } else {
            alert( 'Textarea value can only be '+maxlimit+' characters in length.' );
        }
        return false;
    } else {
        countfield.value = maxlimit - field.value.length;
    }
}

//------------------------------------------
function insertAtCursor(myField, myValue) {
//-----------------------------------------
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos   = myField.selectionEnd;
        myField.value= myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
    // calling the function example: insertAtCursor(document.formName.fieldName, 'this value');
}

//------------------------------
function getSel(lang, textarea){
//------------------------------
    if (document.getSelection) {
        if (textarea!=""){
            var selLength = textarea.textLength;
            var selStart  = textarea.selectionStart;
            var selEnd    = textarea.selectionEnd;
            if (selEnd==1 || selEnd==2) selEnd=selLength;
            txt = (textarea.value).substring(selStart, selEnd);
        } else {
            txt = document.getSelection();
        }
    } else if (document.selection) {
        txt = document.selection.createRange().text;
    } else {
        if (lang=='hu') {
            alert("sajnos ez nem működik a böngésződdel.");
        } else {
            alert("Sorry, this is not possible with your browser.");
        }
        return;
    }        
    return txt.replace(new RegExp('([\\f\\n\\r\\t\\v ])+', 'g')," ");
}

//-----------------------------
function isComplete(img) {
//-----------------------------
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete)
        return false;

     // However, they do have two very useful properties: naturalWidth and
     // naturalHeight. These give the true size of the image. If it failed
     // to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        return false;

    // No other way of checking: assume it's ok.
    return true;
}

//----------------------------------
function newClass(obj, new_style) {
//----------------------------------
    obj.className = new_style;
}

//-----------------------------------------
function getParameter ( parameterName ) {
//-----------------------------------------
    var queryString = window.location.search;
    // Add "=" to the parameter name (i.e. parameterName=value)
    var parameterName = parameterName + "=";
    if ( queryString.length > 0 ) {
        // Find the beginning of the string
        begin = queryString.indexOf ( parameterName );
        // If the parameter name is not found, skip it, otherwise return the value
        if ( begin != -1 ) {
            // Add the length (integer) to the beginning
            begin += parameterName.length;
            // Multiple parameters are separated by the "&" sign
            end = queryString.indexOf ( "&" , begin );
            if ( end == -1 ) {
                end = queryString.length
            }
            // Return the string
            return unescape ( queryString.substring ( begin, end ) );
        }
        // Return "null" if no parameter has been found
        return "null";
    }
}

//---------------------------------------------------------------------
// http://www.hypergeneric.com/corpus/javascript-inner-viewport-resize/
//---------------------------------------------------------------------
function GetInnerSize () {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}

function ResizeToInner (w, h, x, y) {
	// make sure we have a final x/y value
	// pick one or the other windows value, not both
	if (x==undefined) x = window.screenLeft || window.screenX;
	if (y==undefined) y = window.screenTop || window.screenY;
	// for now, move the window to the top left
	// then resize to the maximum viewable dimension possible
	window.moveTo(0,0);
	window.resizeTo(screen.availWidth,screen.availHeight);
	// now that we have set the browser to it's biggest possible size
	// get the inner dimensions.  the offset is the difference.
	var inner = GetInnerSize();
	var ox = screen.availWidth-inner[0];
	var oy = screen.availHeight-inner[1];
	// now that we have an offset value, size the browser
	// and position it
	window.resizeTo(w+ox, h+oy);
	window.moveTo(x,y);
} 
