Array.prototype.inArray = function(value) 
{
	for (var i in this) 
	{ 
		if (this[i] === value) 
		{
			return i; 	
		}
	}
	
	return false;
}

function display_element(id, cmd)
{
	if (document.getElementById('div_'+id)) 
	{	
		document.getElementById('div_'+id).style.display = cmd;
		
		if ( document.getElementById(id+'_month') )
		{	if ( parseInt(document.getElementById(id+'_month').style.width) < 100 )
			{	document.getElementById(id+'_month').style.width = '100px';
			}
		}
		if ( document.getElementById(id+'_cityname') )
		{	if ( parseInt(document.getElementById(id+'_cityname').style.width) < 100 )
			{	document.getElementById(id+'_cityname').style.width = '100px';
			}			
		}

		if ( document.getElementById(id) )
		{	
			if ( document.getElementById(id).type && document.getElementById(id).type.substring(0,6) == 'select' &&  parseInt(document.getElementById(id).style.width) < 100 )
			{	document.getElementById(id).style.width = '100px';
			}
		}	
	}
	else 
	{	
		if (document.getElementById(id))
		{
			document.getElementById(id).style.display = cmd;
			
			if ( document.getElementById(id) )
			{	if ( document.getElementById(id).type && document.getElementById(id).type.substring(0,6) == 'select' &&  parseInt(document.getElementById(id).style.width) < 100 )
				{	document.getElementById(id).style.width = '100px';
			}
			}
		}	
	}
}	

function str_pad( input, pad_length, pad_string, pad_type ) 
{
    var half = '', pad_to_go;
 
    var str_pad_repeater = function(s, len) {
        var collect = '', i;
 
        while(collect.length < len) collect += s;
        collect = collect.substr(0,len);
 
        return collect;
    };
 
    input += '';
 
    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }
 
    return input;
}

function select_value (select_name, value)
{
	if ( document.getElementById(select_name) )
	{	for(i=0; i < document.getElementById(select_name).length; i++)
		{	if (document.getElementById(select_name).options[i].value == value)
			{	document.getElementById(select_name).selectedIndex=i
				break;
			}
		}
	}
}

function select_multi_value (select_name, value)
{
	if ( document.getElementById(select_name) )
	{
		for(i=0; i < document.getElementById(select_name).length; i++)
		{	option_value = document.getElementById(select_name).options[i].value;
			pattern = new RegExp("(^"+option_value+"$)|(^"+option_value+",)|(,"+option_value+"$)|(,"+option_value+",)");
			if ( value.match(pattern) )
			{	document.getElementById(select_name).options[i].selected = true;
			}
			else
			{	document.getElementById(select_name).options[i].selected = false;
			}
		}
	}
}			


function open_close_container( action, container_id )  
{  
	container = document.getElementById(container_id);  

	if( action == 'close' )
	{	container.style.display = 'none';}
	else
	{  
		try
		{	container.style.display = ''; }  
		catch(ex) 
		{	container.style.display = 'block'; }  
	}   
}

function trim(str, chars) 
{
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function disable_select( base_select, managed_select )
{
	disablers = new Array();
	disablers = eval(base_select+'_disablers');

	if ( disablers.inArray(	document.getElementById(base_select).selectedIndex ) )
	{
		document.getElementById(managed_select).disabled = true;
	}
	else
	{
		document.getElementById(managed_select).disabled = false;
	}
}


function getWindowsDimensions( _location )
{
	var myWidth = 0;
	var myHeight = 0;
	var dimensions = new Array();

	if ( !_location )
	{ _location = '';
	}

	myWidth = top.document.body.clientWidth;
	myHeight = top.document.body.clientHeight;

	dimensions[0] = myWidth;
	dimensions[1] = myHeight;

	return dimensions;
}

function replaceall(str, what, whereof)
{
	var tmp = str.toString();
	var index = tmp.indexOf(what);
	while (index != -1)
	{
		tmp = tmp.replace(what, whereof)
		index = tmp.indexOf(what);
	}
	return tmp;
}

function cleartext(str)
{

	var tmp = str.toString();
	var index = tmp.indexOf(String.fromCharCode(34));
	while (index != -1)
	{
		tmp = tmp.replace(String.fromCharCode(34), '`')
		index = tmp.indexOf(String.fromCharCode(34));
	}

	index = tmp.indexOf(String.fromCharCode(39));
	while (index != -1)
	{
		tmp = tmp.replace(String.fromCharCode(39), '`')
		index = tmp.indexOf(String.fromCharCode(39));
	}
	return tmp;
}

function setfocus(next, aEvent)
{
	var myEvent = aEvent ? aEvent : window.event; 
	
	if (myEvent && myEvent.keyCode == 13)
	{
		document.getElementById(next).focus();
	}
}

function check_number(number)
{
	var numbers = "0123456789,.-";
	var tmp = number+' ';
	var ret_str ="";
	var nextchar = "";

	if ( tmp.length > 0 )
	{
		tmp = tmp.replace(/[,]/g, '.', tmp);		
		tmp = tmp.replace(/[ ]/g, '', tmp);		

		decimal_pos = tmp.indexOf('.')

		if	(tmp != "")
		{	for (i = 0; (i < tmp.length); i++)
			{	nextchar = tmp.charAt(tmp.length-1-i);
				if (numbers.indexOf(nextchar) == -1)
				{
					alert('Hibás számformátum!');
				}

				ret_str = nextchar+''+ret_str;

				if ( decimal_pos == -1 && tmp.length > 4 )
				{	if ( i%3 == 2)
					{	ret_str = ' '+ret_str;} 
				}
			}
		}	
	}

	ret_str = trim(ret_str, ' ');

	return ret_str;
}

function check_adoszam ( adoszam, badcell )
{
	var tmp;

	if ( adoszam.substr(0,2).toUpperCase() != 'HU' )
	{
		var check_nums = new Array(9,7,3,1,9,7,3);
		var checksum = 0;

		tmp = adoszam.replace(/[^0-9]/g, '', adoszam);
	
		if (tmp.length != 0)
		{
			if (tmp.length != 11)
			{	alert('Hibás adószám! A helyes formátum: 123456789-1-12');
				tmp = '';
			}
			else
			{/*	for (i = 0; i < 7; i++)
				{	checksum += parseInt(tmp.charAt(i)) * check_nums[i];
				}
	
				checksum = 10-(checksum%10);
	
				if ( checksum != parseInt(tmp.charAt(i)) )
				{	alert("Hibás az adószám ellenőrző összege (ettől még lehet valós, csak szólok!)");
				}*/
				tmp = tmp.substr(0,8)+"-"+tmp.substr(8,1)+"-"+tmp.substr(9,2);
			}
		}
	}
	else
	{	tmp = adoszam.replace(/[^0-9]/g, '', adoszam);

		if (tmp.length != 8)
		{	alert('Hibás közösségi adószám! A helyes formátum: HU12345678');
			tmp = '';
		}
		else
		{	tmp = 'HU'+tmp;
		}
	}
	
	if ( tmp == '' && document.getElementById(badcell) )
	{	document.getElementById(badcell).focus();
	} 
	
	return tmp;
}

function check_email (email, badcell, recall)
{
	var tmp = email;
	var emails = new Array();

	tmp = tmp.replace(/\s+/g, ',', tmp);

	if ( !recall )
	{	recall=0		
	}

	if ( tmp.length > 0 )
	{
		if ( recall != 1)
		{	tmp = email.toLowerCase();
			
			tmp = tmp.replace(/;/g, ',', tmp);
			tmp = tmp.replace(/,+/g, ',', tmp);
			tmp = tmp.replace(/,$|^,/g, '', tmp);
		
			tmp = tmp.replace(/'á'/g, 'a', tmp);
			tmp = tmp.replace(/'é'/g, 'e', tmp);
			tmp = tmp.replace(/'í'/g, 'i', tmp);
			tmp = tmp.replace(/'ó'/g, 'o', tmp);
			tmp = tmp.replace(/'ö'/g, 'o', tmp);
			tmp = tmp.replace(/'ő'/g, 'o', tmp);
			tmp = tmp.replace(/'ú'/g, 'u', tmp);
			tmp = tmp.replace(/'ü'/g, 'u', tmp);
			tmp = tmp.replace(/'ű'/g, 'u', tmp);
	
			emails = tmp.split(',');
			tmp = '';

			for( i=0; i<emails.length; i++ )
			{	
				if ( emails[i].indexOf('mial') != -1 )
				{	if ( confirm(emails[i]+': biztosan mial, nem inkább mail?') )
					{	emails[i] = emails[i].replace(/mial/g, 'mail', emails[i]);
					}
				}
			
				emails[i] = emails[i].replace(/\.*$|^\.*/g, '', emails[i]);

				emails[i] = check_email(emails[i], badcell, 1);		
				tmp += emails[i]+',';
			}
	
			tmp = tmp.replace(/,$/g, '', tmp);
		}
		else
		{	tmp_2 = tmp.replace('@', '');
			_delete = false;

			if ( tmp_2.indexOf('@') > 1 )
			{	alert(tmp+': minden email-címben csak egy "@" lehet');
				_delete = true;
			}
	
			if ( tmp.indexOf('@') < 0 )
			{	alert(tmp+': minden email-címben kell legyen egy "@"');
				_delete = true;
			}
			
			if ( tmp.indexOf('.') < 0 )
			{	alert(tmp+': minden email-címben kell legyen legalább egy "."');
				_delete = true;
			}

			if ( tmp.length < 9 )
			{	alert(tmp+': ez az emailcím rövidnek tűnik!');
			}

			if ( _delete )
			{	tmp = '';				
			}
		}
	
		tmp = tmp.replace(/,$|^,/g, '', tmp);
	}

	return tmp;
}

function check_date(str_date, badcell, format)
{		
	var tmp = '';
	var year, month, day;
	var parts = new Array();
	var today = new Date();
	var to_year 	= today.getFullYear();
	var to_month 	= today.getMonth()+1;
	var to_day 		= today.getDate();
	var is_time 	= false;
	
	var date_ok = true;

	if (str_date.length != 0)
	{	
		tmp = str_date.replace(/[\s+]/g, " ", str_date); //dupla szóközök ki
		tmp = tmp.replace(/[-:\/\s]/g, ".", tmp);		 //elválasztó karakterek egységesen pontra			
		tmp = tmp.replace(/[^.\d]/g, "", tmp);			 //nem numerikus illetve nem pont ki			

		if( (tmp.indexOf('.') == -1 && tmp.length > 8) || (tmp.length > 10) ) 
		{	is_time = true;}

		if( format == 'M-d')
		{	tmp = '2000' + str_date.replace(/[-.:\/\s]/g, "", tmp).substr(-4,4);
		}

		parts = tmp.split('.');

		if ( tmp.indexOf('.') == -1)
		{	year 	= parseInt(tmp.substring(0,4),10);
			month 	= parseInt(tmp.substring(4,6),10);
			day 	= parseInt(tmp.substring(6,8),10);
			hour 	= parseInt(tmp.substring(8,10),10);
			minute 	= parseInt(tmp.substring(10,12),10);
			sec 	= parseInt(tmp.substring(12,14),10);
		}
		else
		{	year 	= parseInt(parts[0],10);
			month 	= parseInt(parts[1],10);
			day 	= parseInt(parts[2],10);
			hour 	= parseInt(parts[3],10);
			minute 	= parseInt(parts[4],10);
			sec 	= parseInt(parts[5],10);
		}

		if ( isNaN(year) )
		{	year = to_year;			
		}
		if ( isNaN(month) )
		{	month = 1;			
		}
		if ( isNaN(day) )
		{	day = 1;			
		}
		if ( isNaN(hour) )
		{	hour = 0;			
		}
		if ( isNaN(minute) )
		{	minute = 0;			
		}
		if ( isNaN(sec) )
		{	sec = 0;			
		}

		if ( (format == 'today_limit') && (	
				(to_year < year) ||
				(to_year == year && to_month < month) || 
				(to_year == year && to_month == month && to_day < day)))
		{	date_ok = false;
		}

		if ( date_ok )
		{	
			if ( (month == 2 && year%100 == 0 && year%400 != 0 && day > 28) || (month == 2 && year%4 != 0 && day > 28) ) 
			{	day = 28;}
			else if ( month == 2 && day > 29 )
			{	day = 29;}
			else if ( (month == 4 || month == 6 || month == 9 || month == 11) && day > 30 )
			{	day = 30;}
			else if	( day > 31 )
			{	day = 31;}
	
			if (tmp.length == 4)
			{	tmp = year;}
			else if (
				year < 1900 || month < 1 || month > 12 || day < 1 || 
				hour > 24  || minute > 60 || sec > 60 )
			{	
				alert("Hibás dátum! A helyes formátum: éééé(1900-).hh.nn");
				tmp = '';
				document.getElementById(badcell).focus();
			}
			else
			{	month 	= str_pad( month, 2, '0', 'STR_PAD_LEFT');
				day 	= str_pad( day, 2, '0', 'STR_PAD_LEFT');
				hour 	= str_pad( hour, 2, '0', 'STR_PAD_LEFT');
				minute 	= str_pad( minute, 2, '0', 'STR_PAD_LEFT');
				sec 	= str_pad( sec, 2, '0', 'STR_PAD_LEFT');

				if (is_time)
				{ tmp = year+'.'+month+'.'+day+' '+hour+':'+minute+':'+sec;
				}
				else
				{ tmp = year+'.'+month+'.'+day;
				}
			}
	
			if( format == 'M-d')
			{	month 	= str_pad( month, 2, '0', 'STR_PAD_LEFT');
				day 	= str_pad( day, 2, '0', 'STR_PAD_LEFT');

				tmp = month+'.'+day;	
			}
		}//endif (date_ok)
		else
		{
			if( format == 'today_limit')
			{	month 	= str_pad( to_month, 2, '0', 'STR_PAD_LEFT');
				day 	= str_pad( to_day, 2, '0', 'STR_PAD_LEFT');

				alert('A mai napnál nagyobb dátum nem adható meg!');								

				tmp = to_year+'.'+month+'.'+day;

				document.getElementById(badcell).focus();
			}			
		}
	}
	
	return tmp;
}

function check_telefonszam (number)
{
	var countrycode, statecode, phonenumber, rest;
	var tmp = number;
	
	if (tmp.length >= 8)
	{
		tmp = replaceall(tmp," ","");
		tmp = replaceall(tmp,"/","");
		tmp = replaceall(tmp,"(","");
		tmp = replaceall(tmp,")","");
		tmp = replaceall(tmp,"-","");
		
		if (tmp.charAt(0) == "+")
		{	
			countrycode = tmp.substring(0,3);
			tmp = tmp.substring(3);
		}
		else if ( tmp.substring(0,2) == "06" || (tmp.substring(0,2) == "36" && tmp.length >= 10) )
		{	
			countrycode = "+36";
			tmp = tmp.substring(2);
		}
		else
		{	
			countrycode = "+36";
		}	
		
		if (tmp.charAt(0) == "1")
		{
			statecode = "1";
			tmp = tmp.substring(1);
		}
		else
		{
			statecode = tmp.substring(0,2);
			tmp = tmp.substring(2);
		}
		
		tmp = countrycode + " (" + statecode + ") " + tmp.substring(0,3)+" "+tmp.substring(3);
	}	
	
	return tmp;
}

function check_bankszamlaszam( ba )
{
	var numbers = "0123456789", tmp;
	var multipliers = "9731";

	tmp = replaceall(ba,"-","");		
	
	for (i = 0; (i < tmp.length); i++)
	{
		nextchar = tmp.charAt(tmp.length-1-i);
		if (numbers.indexOf(nextchar) == -1)
		{
			alert('Hibás bankszámlaszám formátum!');
			tmp = '';
		}
	}

	if (tmp.length != 0)
	{
		i = 0;
		sum = 0;
		checksum = 	0;

		for (groups = 0; groups <= 1;  groups++)
		{			
			if (groups == 0)
			{	group_length = 8;}
			else
			{	group_length = tmp.length-8;}

			group_start = groups*8;
			
			for (i = group_start; i < group_start+group_length; i++)
			{
				sum += parseInt(tmp.charAt(i)) * parseInt(multipliers.charAt((i % 4)));
			}
			checksum = sum % 10;
		}

		if ( checksum != 0 )
		{	alert("Hibás a bankszámlaszám ellenőrző összege!");
		}

	}	
	
	if ( tmp.length > 8 )
	{	tmp = tmp.substring(0,8)+'-'+tmp.substring(8);		
	}
	if ( tmp.length > 17 )
	{	tmp = tmp.substring(0,17)+'-'+tmp.substring(17);		
	}
	
	return tmp;
}   

function elrejt_tabla( modul )
{
	if ( modul == 'uzletkotok' )
	{
		parent.document.getElementById('cr_uzletkotok').style.display = 'none';
		parent.document.getElementById('cr_telephelyek').style.display = 'none';
		parent.document.getElementById('cr_szemelyek').style.display = 'none';
	}	
	else if ( modul == 'partnerek' )
	{
		parent.document.getElementById('cr_e_partnerek').style.display = 'none';
		parent.document.getElementById('cr_e_telephelyek').style.display = 'none';
		parent.document.getElementById('cr_e_szemelyek').style.display = 'none';
	}

}

function load_table( table, file )
{
	parent.document.getElementById(table).src = file;
	parent.document.getElementById(table).style.display = '';
}

function betolt_tabla( _location, sessionid, tabla, kod, parent_id, hiv_nev, called_menu, called_field, trans_id )
{
	if ( tabla.indexOf('_e_') >= 0 )
	{	elrejt_tabla('partnerek');}
	else
	{	elrejt_tabla('uzletkotok');}

	parent.document.getElementById(tabla).style.display = '';
	parent.document.getElementById(tabla).src = _location+'/'+tabla+'.php?trans_id='+trans_id+'&sessionid='+sessionid+'&kod='+kod+'&parent_id='+parent_id+'&hiv_nev='+hiv_nev+'&called_menu='+called_menu+'&called_field='+called_field;
}

function get_telepules( postalcode, _location, sessionid )
{
	postalcode = parseInt(postalcode);

	if ( postalcode > 1000 && postalcode < 9999 )
	{	document.getElementById('get_telepules').src = _location+'/get_telepules.php?sessionid='+sessionid+'&postalcode='+postalcode;}
}

function load_file( _location, file_name )
{
	window.open(_location + file_name);
}

function upload( _location, sessionid, mezo_id, upload_dir )
{
	document.getElementById('empty_frame').style.display = 'block';
	document.getElementById('empty_frame').src = _location+'/uploader.php?sessionid='+sessionid+'&trans_id='+( Math.floor(Math.random()*10000) )+'&mezo_id='+mezo_id+'&upload_dir='+upload_dir;
}

function submit_table( _location, sessionid, table_name )
{
	document.getElementById('form_'+table_name).target = 'nezo';
	document.getElementById('form_'+table_name).action = _location+'/'+'nezo.php?sessionid='+sessionid;
	document.getElementById('form_'+table_name).submit();
	parent.document.getElementById(table_name).style.display='none';
}

function submit_to_new_file(sessionid, form, new_file, new_target)
{
	save_action = document.getElementById(form).action; 
	save_target = document.getElementById(form).target; 

	document.getElementById(form).action = new_file+'?sessionid='+sessionid+'&trans_id='+Math.floor(Math.random()*10000); 
	document.getElementById(form).target = new_target; 
	document.getElementById(form).submit();

	document.getElementById(form).action = save_action; 
	document.getElementById(form).target = save_target; 
}

function refresh_container ( _location, sessionid, session_table, session_id, linked_table, parent_iframe, title, form, count )
{//sessionid = PHPSESSID; session_id = [session][crm][pir][cr_table][id]

	save_action = document.getElementById(form).action;
	save_target = document.getElementById(form).target;
	document.getElementById(form).target = 'empty_frame';

	document.getElementById(form).action = _location+'/'+'refresh_container.php?sessionid='+sessionid+'&session_table='+session_table+'&session_id='+session_id+'&linked_table='+linked_table+'&parent_iframe='+parent_iframe+'&title='+title+'&form='+form+'&count='+count;
	document.getElementById(form).submit();

	document.getElementById(form).action = save_action;
	document.getElementById(form).target = save_target;
}

function delete_item(linked_table, index)
{
	if( confirm('Biztosan törölni szeretné a tételt?') )
	{	if(document.getElementById(linked_table+'_deleted_'+index))
		{	document.getElementById(linked_table+'_deleted_'+index).value = 1;
		}
		if(document.getElementById('container_'+linked_table+'_'+index))
		{	document.getElementById('container_'+linked_table+'_'+index).style.display = 'none';
		}
	}	
}