function selectText( id ){
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function addToOnload(object,func) {  
  var oldonload = object.onload;
  if (typeof object.onload != 'function') {
    object.onload = func;
  } else {
    object.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


// Utility function to add an event listener
function wpAddEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}


function validEmail( email, nincsUzenet, hibasUzenet ) {
	if (email=="" || email== null) {
		if ( ! nincsUzenet ) nincsUzenet='Az email cím nincs megadva!';
		alert(nincsUzenet);
		return false;
	} else {
		var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; // valid
		if (!(!emailReg1.test(email) && emailReg2.test(email))) { // if syntax is valid
			if ( ! hibasUzenet ) hibasUzenet='Az email cím formailag hibás!';
			alert(hibasUzenet);
			return false;
		}
		return true;
	}
}

function validDatum( strValue ) {

		// nullát nem ellenőrzi
	if ( strValue == "0000-00-00" || strValue == "" ) return true;


		// év ellenőrzése
	var intYear = strValue.substring(0,4);
	if ( ! isNumeric(intYear) || strValue.length == 0 ) {
		alert("Hibás év, kérjük az 'éééé-hh-nn' dátum formát használja ");
		return false;
	}

		// hónap ellenőrzése
	var intMonth = strValue.substring(5,7);
	if ( ! isNumeric(intMonth) || parseInt( intMonth, 10 ) > 12 ) {
		alert("Hibás hónap, kérjük az 'éééé-hh-nn' dátum formát használja ");
		return false;
	}

		// nap ellenőrzése
	var intDay = strValue.substring(8,10);
	if ( ! isNumeric(intDay) || parseInt( intDay, 10 ) > 31 ) {
		alert("Hibás nap, kérjük az 'éééé-hh-nn' dátum formát használja ");
		return false;
	}

   		//create a lookup for months not equal to Feb.
    var honapNap = {'01' : 31,
					'03' : 31,
                    '04' : 30,
					'05' : 31,
                    '06' : 30,
					'07' : 31,
                    '08' : 31,
					'09' : 30,
                    '10' : 31,
					'11' : 30,
					'12' : 31};



	var intNap = parseInt(intDay,10);
	var intHo = parseInt(intMonth,10);
	var intEv = parseInt(intYear,10);

    //alert (intNap + " <=  " + honapNap[intMonth]);

    	//check if month value and day value agree
    if( honapNap[intMonth] != null ) {
      	if( intNap <= honapNap[intMonth] && intNap != 0 ) {
	  		return true; //found in lookup table, good date
		}
    }


    if (intHo == 2) {
       if (intNap > 0 && intNap < 29) {
           return true;

	   }else if (intNap == 29) {

	   			// year div by 4 and ((not div by 100) or div by 400) ->ok
			if ((intEv % 4 == 0) && (intEv % 100 != 0) ||
				 (intEv % 400 == 0)) {

				 return true;
			}
       }
    }


	alert("A dátum nem helyes" + strValue);

	return false;
}

function checkTimeKeyPressed( e, id ){
    if (e.keyCode) keycode=e.keyCode;
    else keycode=e.which;
    ch=String.fromCharCode(keycode);

    var textfield = document.getElementById(id);
    var txt = textfield.value;

    if ( txt == '00:00' ) txt = '';
    if ( txt.length == 2 ) txt = txt + ':';
    if ( txt.length > 5 ) txt = txt.substr(0, 5);

    var doIt = true;
    while ( txt.length > 0 && doIt ){
        doIt = false;
        switch( txt.length ){
            case 1:
                if ( ! txt.match(/^[0-2]$/) ) doIt = true;
                break;
            case 2:
                if ( ! txt.match(/^[0-2][0-9]$/) ) doIt = true;
                if ( txt > '23' ) doIt = true;
                break;
            case 3:
                if ( ! txt.match(/^[0-2][0-9]:$/) ) doIt = true;
                if ( txt > '23:' ) doIt = true;
                break;
            case 4:
                if ( ! txt.match(/^[0-2][0-9]:[0-5]$/) ) doIt = true;
                if ( txt > '23:5' ) doIt = true;
                break;
            case 5:
                if ( ! txt.match(/^[0-2][0-9]:[0-5][0-9]$/) ) doIt = true;
                if ( txt > '23:59' ) doIt = true;
                break;
        }

        if ( doIt ) txt = txt.substr(0, txt.length-1);
    }

        // ha közben törölni kellett rossz adat miatt
    if ( txt.length == 2 ) txt = txt + ':';

    textfield.value = txt;
}

function validTime( strValue ) {
	var hibas = false;

		// számok átkonvertálása
	if ( isNumeric( strValue ) ) {
		if ( strValue.length == 1 ) strValue = "0" + strValue + ":00";
		if ( strValue.length == 2 && strValue < 25 ) strValue = strValue + ":00";
		if ( strValue.length == 3 ) strValue = "0" + strValue.substring(0,1) + ':' + strValue.substring(1,3);
		if ( strValue.length == 4 ) strValue = strValue.substring(0,2) + ':' + strValue.substring(2,4);
	}


		// érvénytelen hosszú stringeket visszadobja
	if ( strValue.length > 5 || strValue.length < 3 ) {
		hibas = true;
	}else{
			// óra ellenőrzése
		var intHour = strValue.substring(0,2);
		if ( ! isNumeric(intHour) || parseInt( intHour, 10 ) > 24 ) {
			hibas = true;
		}

			// perc ellenőrzése
		var intMinute = strValue.substring(3,5);
		if ( ! isNumeric(intMinute) || parseInt( intMinute, 10 ) > 59 ) {
			hibas = true;
		}
	}

	if ( hibas ) {
		alert("Hibás idő, kérjük az 'óó:pp' formát használja ");
		strValue = '00:00';
	}

	return strValue;
}

var numbers="0123456789";
function isNumeric(x) {
    // is x a String or a character?
    if(x.length>1) {
      // remove negative sign
      x=Math.abs(x)+"";
      for(j=0;j<x.length;j++) {
        // call isNumeric recursively for each character
        number=isNumeric(x.substring(j,j+1));
        if(!number) return number;
      }
      return number;
    }
    else {
      // if x is number return true
      if(numbers.indexOf(x)>=0) return true;
      return false;
    }
}


function validInteger( strValue ){
	if ( isNumeric( strValue ) ) return strValue;

	alert("Nem egész szám: " + strValue);

	return 0;
}

function validDouble( strValue ){

	strValue = replaceChars( ",", ".", strValue );

	var numberParts = strValue.split(".", 1 );

	if ( isNumeric( numberParts[0] ) && numberParts[1] == null ) return strValue;
	if ( isNumeric( numberParts[0] ) && isNumeric( numberParts[1] ) ) return strValue;

	alert("Nem szám: " + strValue);

	return 0;
}

function replaceChars( replaceThis, withThis, source ) {
	temp = "" + source;

	while ( temp.indexOf( replaceThis  ) >-1 ) {
		pos= temp.indexOf( replaceThis );
		temp = "" + ( temp.substring(0, pos) + withThis + temp.substring( (pos + replaceThis.length), temp.length) );
	}

	return temp;
}

/*  ellenőrzi az oldal választás joghoz kötött submitolhatóságát */
function checkChangePage( oldalRight, userRight, requiredPageName ){
	if ( oldalRight == 0 || userRight/oldalRight == Math.round( userRight/oldalRight ) ) {
		document.page.pageChooser.value = requiredPageName;
		document.page.submit();
	}
}

/* sbmitol egy control gomb formot */
function controlSubmitButton( submitValue, form, button ){
	button.value = submitValue;
	form.submit();
}

function writeIntoElement( id, text ){
	if (document.getElementById){
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}else if (document.all){
		x = document.all[id];
		x.innerHTML = text;
	}else if (document.layers){
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

function printText(elem) {
    popup = window.open('','popup','toolbar=no,menubar=no,width=200,height=150');
    popup.document.open();
    popup.document.write("<html><head></head><body onload='print()'>");
    popup.document.write(elem);
    popup.document.write("</body></html>");
    popup.document.close();
} 

function getElementPosition(obj){
    var topValue= 0,leftValue= 0;
    while(obj){
        leftValue+= obj.offsetLeft;
        topValue+= obj.offsetTop;
        obj= obj.offsetParent;
    }

    return [leftValue, topValue];
}

function createDiv( id, html, width, height, left, top, background, border ){
   var newdiv = document.createElement('div');

   newdiv.setAttribute('id', id);
   newdiv.style.width = width;
   newdiv.style.height = height;
   newdiv.style.position = 'absolute';
   newdiv.style.left = left;
   newdiv.style.top = top;
   newdiv.style.background = background;
   newdiv.style.border = border;
   newdiv.style.filter = 'alpha(opacity=100)';
   newdiv.style.zIndex = 1;

   if (html) {
       newdiv.innerHTML = html;
   } else {
       newdiv.innerHTML = "nothing";
   }

   document.body.appendChild(newdiv);
}

 function changeOpacity( obj, opacity ){
    if (navigator.appName.indexOf("Netscape")!=-1&&parseInt(navigator.appVersion)>=5){
        obj.style.MozOpacity=opacity/100
    }else if (navigator.appName.indexOf("Microsoft")!=-1&&parseInt(navigator.appVersion)>=4){
        obj.filter = 'alpha(opacity=' + opacity + ')';
    }
}

/*
 *  animált mozgással és átméretezéssel átrak egy element-et
 *  obj: az element
 *  var animPropsDiv = {
    left: {start: 5, end: 100},
    top: {start: 55, end: 20},
    height: {start: 100, end: 400},
    width: {start: 300, end: 800}
};
    anim: true, false - kell-e animálni
 */

function moveElement( obj, animProps, anim ){
    
    if ( ! anim ){
            // Az eredeti finkció futtatása
        obj.style.top = animProps.left.end + "px";
        obj.style.left = animProps.top.end + "px";
    }else{
        //Az animáció futtatása
        var moveArgs = {
            node: obj,
            easing: dojo.fx.easing.linear,
            duration: 300,
            properties:{}
        };

        //Az egyes mozgások beállítása
        if (animProps.left!=null){
            moveArgs.properties.left = {start: animProps.left.start, end: animProps.left.end, unit: "px"};
        }

        if (animProps.top!=null){
            moveArgs.properties.top = {start: animProps.top.start, end: animProps.top.end, unit: "px"};
        }

        if (animProps.width!=null){
            moveArgs.properties.width = {start: animProps.width.start, end: animProps.width.end, unit: "px"};
        }

        if (animProps.height!=null){
            moveArgs.properties.height = {start: animProps.height.start, end: animProps.height.end, unit: "px"};
        }

        dojo.fx.chain([dojo.animateProperty(moveArgs)]).play();
    }
}

function resizeElement( obj, width, height ){
    obj.style.width = width + "px";
    obj.style.height = height + "px";
}

function setInProgressAndJumpToPage( oldal, aloldal, inProgressIdName, inProgressID, extraParameters ){
	var url = oldal + '/control.php?aloldal=' + aloldal + '&' + inProgressIdName + '=' + inProgressID;
	if ( extraParameters.length > 0 ) url = url + '&' + extraParameters;

	window.location = url;
}

function openWindow( windowWidth, windowHeight, url, windowName, passedParameters ) {

	var OpenSubX = (screen.width/2)-(windowWidth/2);
	var OpenSubY = (screen.height/2)-(windowHeight/2);
	var pos = ', left='+OpenSubX+', top='+OpenSubY;
	var features = 'toolbar=no, menubar=yes, resizable=no, scrollbars=yes, status=no, location=no, width=' + windowWidth + ', height=' + windowHeight + pos;

	window.open( url + passedParameters, windowName, features);
}

function printWindow( nyomtatGombNeve ) {
	document.getElementById( nyomtatGombNeve ).style.visibility = 'hidden';

	window.print();
	window.onAfterPrint();
}

function showDolgozom( left, top ){
	document.getElementById('dolgozom_div').style.visibility = 'visible';
	document.getElementById('dolgozom_div').style.left = left + 'px';
	document.getElementById('dolgozom_div').style.top = top + 'px';
}

function hideDolgozom(){
	document.getElementById('dolgozom_div').style.visibility = 'hidden';
}

function setSelectedOption( select, selectedValue ){
    for (var i = 0; i < select.options.length; i++) {
        if ( select.options[i].value == selectedValue ){
            select.options[i].selected = true;
            break;
        }
    }
}

function getAjaxObject(){
    var xmlhttp;

        // browser based object setting
    if (window.XMLHttpRequest){
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }else if (window.ActiveXObject){
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }else{
      alert("Your browser does not support XMLHTTP!");
    }

    return xmlhttp;
}