/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
* @param An alternative field name
*/
function showpic(kep, width, height, text, alt)
{
	if(!text)text='Bezáráshoz kattintson a képre';
	if(!alt)alt='';
	var marginBottom=0;
	if(/MSIE (6|5|4)/.test(navigator.userAgent))marginBottom=10;
  $('seged').innerHTML ='<div style="position: relative"><img src="'+kep+'" alt="' + alt + '" onclick="$(\'seged\').style.display = \'none\'" style="cursor: pointer" onmouseover="new Effect.Opacity(\'close\', {from: 0.0, to: 0.7, duration: 0.2 }); return false;" onmouseout="new Effect.Opacity(\'close\', {from: 0.7, to: 0.0, duration: 0.2 }); return false;" /><div id="close" align="right" style="display: block; width: 100%; margin: 0px; margin-bottom:' + marginBottom + 'px;padding: 3px 0px 3px 0px; position: absolute; bottom: 0px; left: 0px; background: #000; opacity:  0.0; filter:Alpha(style=0,opacity=0);color:#fff;">' + text + '!&nbsp;</div></div>';
  position_layer2('seged', width, height);
  //Effect.Grow('seged', {direction: 'center'}); return false;
  $('seged').appear({ duration: 0.4 });
  
}
function EditForm_direct(form, task, data1){
	myForm = $(form);
	if(task)myForm.task.value = task;
	if(data1)myForm.actid.value = data1;
	if(task=='delete'){
		myForm.boxchecked.value = 1;
		checked_alert_confirm(task);
	}
	else myForm.submit();
}
function EditForm_direct2(form, data1){
	myForm = $(form);
	if(data1)myForm.search1.value = data1;
	myForm.submit();
}

function position_layer2(div, width, height){
  var dim = document.viewport.getDimensions();
  var myLayer = $(div);
  AktTop = document.viewport.getScrollOffsets().top;
  mywidth = width;
  myheight = height;
  // myheight += (myheight<50 ? plusheight : 0);
  
  //mywidth = document.viewport.getWidth();
  //myheight = document.viewport.getHeight();
  newLeft = (dim.width-mywidth)/2;
  if(myheight > dim.height)newtop = AktTop + 10;
  else newtop = AktTop + (dim.height-myheight)/2;
  if(newLeft < 10)newLeft = 10;
  if(newtop < 10)newtop = 10;

  //alert(myLayer.style.left + ' - ' + myLayer.style.top);
  myLayer.style.left = newLeft + "px";
  myLayer.style.top = newtop + "px";
}

function checkAll( n, fldName ) {
  if (!fldName) {
     fldName = 'cb';
  }
  
  var f = document.adminForm;
  var n2 = 0;
  toggle='toggle_'+fldName;
  tgl=eval('f.' + toggle);
  if (fldName=='cb') c = f.toggle.checked; else c = tgl.checked;
  for (i=0; i < n; i++) {
    cb = eval( 'f.' + fldName + '' + i );
    if (cb) {
      cb.checked = c;
      n2++;
    }
  }
  if (c) {
    document.adminForm.boxchecked.value = n2;
  } else {
    document.adminForm.boxchecked.value = 0;
  }
}

function checkAllDin( n, fldName, toggleName ) {
 	// alert('teszt');
 if (!fldName) {
     fldName = 'cb';
  }
	var f = document.adminForm;
	var n2 = 0;
	tgl = eval('f.' + toggleName);
	c = tgl.checked;
	for (i=0; i < n; i++) {
		cb = eval( 'f.' + fldName + '' + i );
		if (cb) {
			cb.checked = c;
			n2++;
		}
	}
  if (c) {
    document.adminForm.boxchecked.value = n2;
  } else {
    document.adminForm.boxchecked.value = 0;
  }
}

function checkAny( n, fldName ) {
  if (!fldName) {
     fldName = 'cb';
  }
  
  var f = document.adminForm;
  var n2 = 0;
  toggle='toggle_'+fldName;
  tgl=eval('f.' + toggle);
  if (fldName=='cb') c = f.toggle.checked; else c = tgl.checked;
  for (i=0; i < n; i++) {
    cb = eval( 'f.' + fldName + '' + i );
    if (cb) {
      cb.checked = c;
      n2++;
    }
  }
  if (c) {
    document.adminForm.boxchecked.value = n2;
  } else {
    document.adminForm.boxchecked.value = 0;
  }
}


function isChecked(isitchecked){
  if (isitchecked == true){
    document.adminForm.boxchecked.value++;
  }
  else {
    document.adminForm.boxchecked.value--;
  }
}

/**
* Default function.  Usually would be overriden by the component
*/
function submitbutton(pressbutton) {
  submitform(pressbutton);
}

function submitThis(form){
	$(form).submit();
}
/**
* Submit the admin form
*/
function submitform(pressbutton){
  document.adminForm.task.value=pressbutton;
  try {
    document.adminForm.onsubmit();
    }
  catch(e){}
  document.adminForm.submit();
}

function submitbutton_controll(pressbutton, id){
  document.adminForm.task.value=pressbutton;
  document.adminForm.action = document.adminForm.action + "?id=" + id + "&id2=5";
  //alert(document.adminForm.action);
  try {
    document.adminForm.onsubmit();
    }
  catch(e){}
  document.adminForm.submit();
}

function submitbutton_controll_url(pressbutton, id, url){
  document.adminForm.task.value=pressbutton;
  document.adminForm.action = document.adminForm.action + "?id=" + id + "&id2=5" + url;
  //alert(document.adminForm.action);
  try {
    document.adminForm.onsubmit();
    }
  catch(e){}
  document.adminForm.submit();
}

function submitbutton_controll_full(pressbutton, url){
  document.adminForm.task.value=pressbutton;
  document.adminForm.action = document.adminForm.action;// + "?" + url;
  //alert(document.adminForm.action);
  try {
    document.adminForm.onsubmit();
    }
  catch(e){}
  document.adminForm.submit();
}

function submitbutton_user(pressbutton) {
  submitform_user(pressbutton);
}

/**
* Submit the admin form
*/
function submitform_user(pressbutton){
  document.urlap.task.value=pressbutton;
  try {
    document.urlap.onsubmit();
    }
  catch(e){}
  document.urlap.submit();
}
// Hirlevel selecthez
function submitformhirlevel(){
  try {
    document.adminHirlevel.onsubmit();
    }
  catch(e){}
  document.adminHirlevel.submit();
}

function checked_alert(submiting)
{
  if (document.adminForm.boxchecked.value == 0) 
  {
    alert('Jelöljön ki egy elemet!'); 
  }
  else 
  {
    submitbutton(submiting);
  }
}

function checked_alert_confirm(submiting)
{
  if (document.adminForm.boxchecked.value == 0) 
  {
    alert('Jelöljön ki egy elemet!'); 
  }
  else 
  {
    if (confirm('Biztos, hogy törli?')) submitbutton(submiting);
    else return false;
  }
}

function checked_alert_confirm_hir(submiting)
{
  if (document.adminForm.boxchecked.value == 0) 
  {
    alert('Jelöljön ki egy elemet!'); 
  }
  else 
  {
    if (confirm('Biztos, hogy el szeretné küldeni?')) submitbutton(submiting);
    else return false;
  }
}

function listItemTask( id, task ) {
    var f = document.adminForm;
    cb = eval( 'f.' + id );
    if (cb) {
        for (i = 0; true; i++) {
            cbx = eval('f.cb'+i);
            if (!cbx) break;
            cbx.checked = false;
        } // for
        cb.checked = true;
        f.boxchecked.value = 1;
        submitbutton(task);
    }
    return false;
}

function checkclick(id) {
var elem = document.getElementById(id);
if (elem.checked == '') elem.checked='checked'; else elem.checked='';
}

function txt_num_suffix() {
  if (document.getElementById('type_sel').value == 'text_num')
  document.getElementById('suffix').style.display ='';
  else 
  document.getElementById('suffix').style.display ='none';

}


function open_window_img(what, pwidth, pheight, bezartxt) {

  myleft=(screen.width)?(screen.width-pwidth)/2:100;
  mytop=(screen.height)?(screen.height-pheight)/2:100;
  
  f_popup = open("","cartridge_popup","width="+pwidth+",height="+pheight+",left="+myleft+", top="+mytop+",status=no,toolbar=no,menubar=no,resizable=no");
  f_popup.document.open();
  f_popup.document.write("<HTML><HEAD><TITLE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TITLE></HEAD>");
  f_popup.document.write("<BODY style='margin: 0px 0px 0px 0px; padding: 0px;' bgcolor='#000000'>");
  f_popup.document.write("<a href='javascript:;' onClick='javascript:window.close();'><img src='"+what+"' title='"+bezartxt+"' border='0'></a>");
  f_popup.document.write("</BODY></HTML>");
  f_popup.document.close();
}
function open_window_data(url, left, top, width, height)
{
  //if(f_popup && !f_popup.closed) f_popup.close();
  f_popup = open(url, 'ing_popup', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+width+', height='+height+', left='+left+', top='+top+', screenX='+left+', screenY='+top+'');
  
}


function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   try {
    document.urlap.onsubmit();
    }
    catch(e){}
    document.urlap.submit();
    return false;
   }
else
   return true;
}

var d = document;
function getObj(id)
{
  if (d.getElementById) return d.getElementById(id);
  else if (d.all) return d.all(id);
  else if (d.layers) return d.layers[id];
  else return null;
}

function hint_show(id)
{
  var hint = getObj(id);
  if (hint != null) hint.style.display = 'block';
}

function hint_hide(id)
{
  var hint = getObj(id);
  if (hint != null) hint.style.display = 'none';
}

function Is_Email(addr){
  if(addr.length == 0)return false;
  reg = /^(([a-z,0-9])*(\-|\.))*([a-z,0-9])*\@(([a-z,0-9])*(\-|\.))*([a-z,0-9])*\.[a-z]{2,3}$/;
  result = reg.test(addr);
  if(!result){
    return true;  
  }
  return false;
}

function validateBlogKuld(url, serverurl)
{
  var error = 0;
  if($('nev1').value=='') 
  {
    error++;
    $('err_nev1').innerHTML = "Kitöltése kötelező!"; 
  } 
  else $('err_nev1').innerHTML = ""; 
  
  if($('nev2').value=='')  
  {
    error++;
    $('err_nev2').innerHTML = "Kitöltése kötelező!"; 
  }  
  else $('err_nev2').innerHTML = "";      
  if($('email1').value=='') 
  {
    error++;              
    $('err_email1').innerHTML = "Kitöltése kötelező!"; 
  } 
  else if(Is_Email($('email1').value)) 
  {
    error++;
    
    $('err_email1').innerHTML = "Hibás emailcím!"; 
  } 
  else $('err_email1').innerHTML = "";
  
  if($('email2').value=='') 
  {
    error++;              
    $('err_email2').innerHTML = "Kitöltése kötelező!"; 
  } 
  else if(Is_Email($('email2').value)) 
  {
    error++;
    $('err_email2').innerHTML = "Hibás emailcím!"; 
  }
  else $('err_email2').innerHTML = "";
  
  if(error==0) sendBlogInMail($('nev1').value, $('nev2').value, $('email1').value, $('email2').value, url, serverurl);
}

function clearKuldForm()
{
  $('nev1').value='';
  $('nev2').value='';
  $('email1').value='';
  $('email2').value='';
  $('err_nev1').innerHTML='';
  $('err_nev2').innerHTML='';
  $('err_email1').innerHTML='';
  $('err_email2').innerHTML='';
  hint_hide('kuldform');
}

function position_layer(div){
  var dim = document.viewport.getDimensions();
  var myLayer = $(div);
  AktTop = document.viewport.getScrollOffsets().top;
  mywidth = myLayer.getWidth();
  myheight = myLayer.getHeight();
  // myheight += (myheight<50 ? plusheight : 0);
  
  newLeft = (dim.width-mywidth)/2;
	if(myheight > dim.height)newtop = AktTop + 10;
  else newtop = AktTop + (dim.height-myheight)/2;
	if(newLeft < 10)newLeft = 10;
	if(newtop < 10)newtop = 10;

  // alert(myLayer.style.left + ' - ' + myLayer.style.top);
  myLayer.style.left = newLeft + "px";
  myLayer.style.top = newtop + "px";
}
function position_layer_x(div){
  var dim = document.viewport.getDimensions();
  var myLayer = $(div);
  mywidth = myLayer.getWidth();
  
  newLeft = (dim.width-mywidth)/2;
	if(newLeft < 10)newLeft = 10;
  myLayer.style.left = newLeft + "px";
}
function position_layer_y(div){
  var dim = document.viewport.getDimensions();
  var myLayer = $(div);
  myheight = myLayer.getHeight();
  
  newTop = (dim.height-myheight)/2;
	if(newTop < 10)newTop = 10;
  myLayer.style.top = newTop + "px";
}

function Change_Display(dives, div, ful, data, myclass){
	// alert(a.checked);
	var myDivArr = dives.split(";");
	for(i=0;i < myDivArr.length;i++){
		if(myDivArr[i] == div){
			$(data + myDivArr[i]).style.display = '';
			$(ful + myDivArr[i]).className = myclass + '_on';
			var body_dimensions = $('body').getDimensions();  
			Width = body_dimensions.width + 'px';  
			Height = body_dimensions.height;  
			cntHeight = $('ws_content_prod').getHeight();
			// alert($('ws_content_prod').getHeight() + ' - ' + Height);
			if(cntHeight > Height){
				Height = (cntHeight+20) + 'px';
				$('ws_cover').setStyle({width: Width,height: Height,display: 'block'});
			}
			else{
				Height = (Height) + 'px';
				$('ws_cover').setStyle({width: Width,height: Height,display: 'block'});
			}
		}		
		else{
			$(data + myDivArr[i]).style.display = 'none';
			$(ful + myDivArr[i]).className = myclass;
		}
	}
	position_layer('ws_content_prod');
}

var HelpList_All = new Array();
var HelpList_in_Action = false;
var HelpList_Type = 1;
var HelpList_Akt = 0;
var HelpList_Active = false;
var HelpList_found = new Array();

function Load_HelpList(val, e, k){
	HelpList_Type = $('searchType').value;
	ajaxRequest = new Ajax.Request(
		serverurl + '/functions/ajaxfunc.php',
		{
			method: 'get',
			parameters: 'funcid=search&type=' + $('searchType').value,
			onComplete: function(request) 
			{ 
				HelpList_All.clear();
				HelpList_All = request.responseText.split(";");
				Help_List(val, e, k);
				// $('testID').innerHTML = request.responseText;
			}
		}
	)		
}

function Help_List(othis, e, k){
	if(!k)k=1;
	if(HelpList_Active==true){
		HelpList_Active = false;
	}
	else{
	// alert(HelpList_All.length);
	if((HelpList_All.length < 1 && !HelpList_in_Action) || HelpList_Type != $('searchType').value){
		// alert(HelpList_Active);
		HelpList_in_Action = true;
		Load_HelpList(othis, e, k);
		var a = new Element('div', { id:'helper', 'class': 'helper', 'style':'text-align:left;display:none;right:65px;top:-65px;' }).update("");
		new Insertion.After('tbl_spl_search', a);
	}
	else if(HelpList_All.length > 0){
		HelpList_Active = true;		
		var founded = false;
		var teszt = '';
		
		if(othis.value.length > 0){
			counter=1;
			if(HelpList_Type==1){
				pattern1 = new RegExp('(#&&#)', "gi");
				pattern2 = new RegExp('(#&#)', "gi");
				pattern = new RegExp('(' + othis.value + ')', "gi");				
				var matched = ''
				var aktmatched = '';
				for(m=0;m<HelpList_All.length;m++)
				{				
					if(aktmatched = HelpList_All[m].match( pattern )){
						founded = true;
						HelpList_found[counter-1] = HelpList_All[m];
						last = '';
						matched = HelpList_All[m];
						for(b=0;b<aktmatched.length;b++){
							if(!last.include(aktmatched[b])){
								last = last + ';' + aktmatched[b];
								matched = matched.replace(aktmatched[b], '#&&#' + aktmatched[b] + '#&#');
							}
						}
						matched = matched.replace(pattern1, '<font class="HelpList_found">');
						matched = matched.replace(pattern2, '</font>');
						if(counter < 11)
						teszt = teszt + '<div id="_' + counter + '" class="hl_off" onmouseover="Help_List_Which(this);"><a class="help_list" href="javascript:void(0)" onclick="Set_Value_HelpList(\'spl_search\', \'' + (counter-1) + '\', \'helper\');" style="text-decoration:none;">' + matched + '</a></div>';
						counter++;				
					}				
				}
				
				// HelpList_All.grep('(' + othis.value + ')').each(function(s) {
					// if(counter < 11){
						// founded = true;
						// found[counter-1] = s;
						// counter++;
					// }
				// });
				// counter=1;
				// found.each(function(s){
					
					// pattern = new RegExp('(' + othis.value + ')', "gi");
					// teszt = teszt + '<div id="_' + counter + '" class="hl_off" onmouseover="Help_List_Which(this);"><a class="help_list" href="javascript:void(0)" onclick="Set_Value_HelpList(\'spl_search\', \'' + s + '\', \'helper\');" style="text-decoration:none;">' + s.replace(pattern, '<font class="HelpList_found">' + othis.value + '</font>') + '</a></div>';
					// counter++;
				// });
			}
			else if(HelpList_Type==2){
				// HelpList_All.grep('(' + othis.value + ')').each(function(s) {
				HelpList_All.grep('(' + othis.value + ')').each(function(s) {
					founded = true;
					var Alls = s.split(" ");
					var FullAlls = new Array();
					var RemoveCounter = new Array();
					// FullAlls = Alls;
					// for(m=0;m<Alls.length;m++){
						// if(Alls[m].include('\n')){
							// RemoveCounter[] = m;
							// Afterfirst = Alls[m].split('\r\n');
							// Afterfirst.slice
						// }
					// }
					// for(m=0;m<Alls.length;m++){
						// if(Alls[m].include('.')){
							// RemoveCounter[] = m;
							// Afterfirst = Alls[m].split('.');
						// }
					// }
					// pattern = new RegExp('(\\s' + othis.value + ')', "gi");
					pattern = new RegExp('(^' + othis.value + ')', "gi");
					
					// matched = s.match( pattern );
					// if(matched.length > 0){
						// lastmatched = '';
					var matched = ''
					var aktmatched = '';
					for(m=0;m<Alls.length;m++)
					{		
						
						if(aktmatched = Alls[m].match( pattern )){
							conti = true;
							if(Alls[m].include('\n')){
								first = Alls[m].split('\r\n');
								Alls[m] = first[0];
								conti = false;
							}
							matched = Alls[m].replace(aktmatched[0], '<font class="HelpList_found">' + aktmatched[0] + '</font>');
							
							if(conti && Alls[m+1] && !Alls[m+1].include('\r\n'))
								matched = matched + ' ' + Alls[m+1] + (Alls[m+2] ? '...' : '');
							else conti = false;
							
							HelpList_found[counter-1] = (Alls[m]+((Alls[m+1] && conti) ? ' '+Alls[m+1] : ''));
							if(counter < 11)
								teszt = teszt + '<div id="_' + counter + '" class="hl_off" onmouseover="Help_List_Which(this);"><a class="help_list" href="javascript:void(0)" onclick="Set_Value_HelpList(\'spl_search\', \'' + (counter-1) + '\', \'helper\');" style="text-decoration:none;">' + matched + '</a></div>';
							counter++;
						}						
					}
								
					// teszt = teszt + '<div id="_' + counter + '" class="hl_off" onmouseover="Help_List_Which(this);"><a class="help_list" href="javascript:void(0)" onclick="Set_Value_HelpList(\'spl_search\', \'' + s + '\', \'helper\');" style="text-decoration:none;">' + s.replace(pattern, '<font style="background-color:#ff0;">' + (othis.value==' '?'&nbsp;':othis.value) + '</font>') + '</a></div>';
					// // alert(s);
					// counter++;
				});
			}
		}
		
		if(founded){	
			if($('helper')){
				// alert(teszt);
				$('helper').innerHTML = teszt + '<div class="HelpList_close"><a href="javascript:void(0)" onclick="$(\'helper\').style.display = \'none\';">' + HelpList_close + '</a></div>';
				// $('helper').innerHTML = '<div class="HelpList_close"><a href="javascript:void(0)" onclick="$(\'helper\').style.display = \'none\';">' + HelpList_close + '</a></div>';
				$('helper').style.display = '';
			}
			if(k==1 || k==2){
				var ktst = 2;
				if(/Opera/gi.test(navigator.userAgent)){
					ktst = 1;
				}

				mKcode = (e.keyCode ? e.keyCode : e.which);

				counter=11;
				if(mKcode == 38 && HelpList_Akt>0){
					// if($('_' + HelpList_Akt))$('_' + HelpList_Akt).className = 'hl_off';
					if(k==1)HelpList_Akt = parseInt(HelpList_Akt-1);
					if($('_' + HelpList_Akt))$('_' + HelpList_Akt).className = 'hl_on';
					// alert(HelpList_Akt + ' - ' + mKcode);
				}
				else if(mKcode == 40 && HelpList_Akt<counter-1 && HelpList_Akt>=0){
					// if($('_' + HelpList_Akt))$('_' + HelpList_Akt).className = 'hl_off';
					if(k==ktst)HelpList_Akt = parseInt(HelpList_Akt+1);
					if($('_' + HelpList_Akt))$('_' + HelpList_Akt).className = 'hl_on';
				}
				else if(mKcode == 13 && HelpList_Akt<=counter && HelpList_Akt>0){
					Set_Value_HelpList('spl_search', (HelpList_Akt-1), 'helper');	
					return false;
				}
				else{HelpList_Akt=0;}
				// alert(HelpList_Akt + ' - ' + k);
			}	
			// if($('testID'))$('testID').innerHTML = teszt + ("teszt, teszt, teszt").replace(/es/g, "F");

		}
		else{
			if($('helper'))$('helper').style.display = 'none';
		}
		HelpList_Active = false;
		// HelpList_All.grep(/othis.value/);
	}
	}
}
function Help_List_Which(othis){
	if(HelpList_Akt > 0)$('_' + HelpList_Akt).className = 'hl_off';
	HelpList_Akt = parseInt(othis.id.substring(1));
}
function Set_Value_HelpList(id, m, hide){
	HelpList_Akt = 0;
	HelpList_Active = false;
	if($(id))$(id).value = (HelpList_found[m]?HelpList_found[m]:'');
	if($(hide))$(hide).style.display = 'none';
	submitThis('searchSimple');
}

function myFunc(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
	if (evt) {
		return true;
	}
}
