﻿// keep session alive
window.setInterval(function()
{

	new Ajax.Request('antiidle.ashx', {
        method: 'post',
        parameters: {
            ajaxaction: 'antiidle'
        },
        onSuccess: function(transport) {
            //alert(transport.responseText);
		},
        onFailure: function(transport) {
            alert('AJAX error, please relogin');
        }
	});
}, 60000);

/*
function feedbacksubmit(f)
{
    var data = $(f).serialize(true);
    
    var hasdata = false;
    $H(data).each(function(i) { if (i.value != '') hasdata = true; });
    
    if (!hasdata)
        return false;
    
    var msg = '';
    var tmp = $$('div.feedback_message');
    if (tmp && typeof(tmp[0]) != 'undefined')
        msg = tmp[0].innerHTML;
    
    new Ajax.Request('feedback.ashx', {
        method: 'post',
        parameters: data,
        onSuccess: function(transport) {
            f.innerHTML = msg;
        },
        onFailure: function(transport) {
            alert('AJAX error');
        }
    });
    return false;
}
*/
function ajaxaction(control, act, form, p1, p2, p3, p4, p5, p6, errhandler)
{
    var params;
    if (form)
        params = $(form).serialize(true);
    else
        params = {};
    params['_ajaxcontrol'] = control;
    params['_ajaxact'] = act;
    params['_p1'] = p1;
    params['_p2'] = p2;
    params['_p3'] = p3;
    params['_p4'] = p4;
    params['_p5'] = p5;
    params['_p6'] = p6;
    
    new Ajax.Request(location.href, {
        method: 'post',
        parameters: params,
        onSuccess: function(transport) {

            var ct = transport.getResponseHeader('Content-Type');
            var res = transport.responseText;
            if (ct.match(/text\/errormsg-data/))
            {
                eval('res = ' + res + ';');
                if (errhandler)
                    errhandler(res);
                else
                    alert('unhandled: ' + res);
            }
            else if (ct.match(/text\/errormsg/))
            {
                alert(res);
            }
            else if (ct.match(/application\/x-redirect/))
            {
                location.href = res;
            }
            else if (ct.match(/text\/html/))
            {
                $(control).update(res);
            }
            else if (ct.match(/application\/json/))
            {
                return transport.responseJSON;
            }
            else if (ct.match(/application\/x-htmlpart/))
            {
try {
                var re = /<htmlpart[^>]*id="([^">]+)"[^>]*>((?:(?!<\/htmlpart)[\s\S])*)<\/htmlpart>/mig;
                var ms;
                while ((ms = re.exec(transport.responseText)))
                {
//alert(ms[0]);
                    var o = $(ms[1]);
                    var h = ms[2];
                    if (o)
                        o.innerHTML = '' + h;
                    else
                        alert('htmlpart missing: ' + ms[1]);
                }
} catch (e) { alert('ERR:'+ e.message); }
            }
        },
        onFailure: function(transport) {
            alert('AJAX error\r\n' + transport.responseText);
        }
    });
}

function hilite(o, v)
{
    if (v)
        $(o).addClassName('hover');
    else
        $(o).removeClassName('hover');
}

function popup(url, rd, scrollbars)
{
	//var width = _popupwidth;
	//var height = _popupheight;
	var width = screen.width - 50;
	var height = screen.height - 150;
	
	var off = 0;
	/*
	try {
        if (window.opener) {
	        off += 30;
	        if (window.opener.opener)
		        off += 30;
        }
	} catch (e) { }
	*/
	//window.child = window.open(url, '', "width="+width+",height="+height+",scrollbars=no,dependent=yes,resizable=no,status=no,top="+((window.screen.height-height)/2+off).toString()+",left="+((window.screen.width-width)/2+off).toString()+"");

	res = "yes";
    if (typeof(scrollbars) == 'undefined')
        scrollbars = 'no';
    if (scrollbars != '')
        scrollbars = 'scrollbars=' + scrollbars + ',';

//alert("width="+width+",height="+height+"," + scrollbars+"dependent=yes,resizable="+res+",status=no,top="+((window.screen.height-height)/2+off).toString()+",left="+((window.screen.width-width)/2+off).toString()+"");

	window.child = window.open(url, '', "width="+width+",height="+height+"," + scrollbars+"dependent=yes,resizable="+res+",status=no,top="+((window.screen.height-height)/2+off).toString()+",left="+((window.screen.width-width)/2+off).toString()+"");
	window.child.focus();
	window.child.rd = rd;
}

function spopup(url, width, height, scrollbars)
{
	var off = 0;
	/*
	try {
        if (window.opener) {
	        off += 30;
	        if (window.opener.opener)
		        off += 30;
        }
	} catch (e) { }
	*/

	res = "yes";
	if (typeof(scrollbars) == 'undefined')
        scrollbars = 'yes';
    scrollbars = 'scrollbars=' + scrollbars + ',';

	window.child = window.open(url, '', "width="+width+",height="+height+"," + scrollbars+"dependent=yes,resizable="+res+",status=no,top="+((window.screen.height-height)/2+off).toString()+",left="+((window.screen.width-width)/2+off).toString()+"");
	window.child.focus();
}

function chatpopup(params)
{
    var width = 780;
	var height = 500;
	var off = 0;
	window.chatwin = window.open('../chat.ashx/Default.html?' + params, 'dexchattrickywin', "width="+width+",height="+height+",scrollbars=no,dependent=yes,resizable=yes,status=no,top="+((window.screen.height-height)/2+off).toString()+",left="+((window.screen.width-width)/2+off).toString()+"");
}

function input_number(i, min, max)
{
	if (i.value == '')
		return true;
	var n = new Number(i.value);
	if (isNaN(n)) {
		alert('Hibás szám!');
		i.focus();
		return false;
	}
	n = n + 0;
	if ((typeof(min) != 'undefined' && n < min) || (typeof(max) != 'undefined' && n > max))
	{
	    alert('A szám nem a megfelelő tartományban van!');
		i.focus();
		return false;
	}
	i.value = n.toString();
	return true;
}

function input_re(i, re)
{
	if (i.value == '')
		return true;
    if (!re.match(i.value))
    {
        alert('Hibás formátum!');
        i.focus();
        return false;
    }
    return true;
}

function input_phonenumber(o, fmt)
{
    var s = o.value;
    if (s == '')
        return;
    if (typeof(fmt) == 'undefined' || fmt == '')
        fmt = '+2-2-3-4';
        
    s = s.replace(/ /g, '');
        
    // +xxxxxx or 00xxxxxx
    var hasplus = s.charAt(0) == '+';
    if (!hasplus && s.match(/^00/))
    {
        s = s.replace(/^00/, '');
        hasplus = true;
    }
    
    s = s.replace(/[^0-9]/g, '');
    
    // convert hungarian internal prefix to international
    if (s.match(/^06/))
    {
        s = s.replace(/^06/, '36');
        hasplus = true;
    }
    
    // add country prefix (default: 36)
    if (!hasplus)
        s = '36' + s;

    fmt = fmt.split('');
    var cmd, i, k
    var j = 0;
    var res = '';
    for (i = 0; i < fmt.length; i++)
    {
        cmd = fmt[i];
        if (cmd.match(/^[0-9]+$/))
        {
            cmd = new Number(cmd);
            for (k = 0; k < cmd; k++)
                res += s.charAt(j++);
        }
        else
            res += cmd;
    }
    o.value = res;
}

function input_date(i, re)
{
	if (i.value == '')
		return true;
		
    var s = new String(i.value);
    var a = s.split(/\. *|-|T|:| +/);
    var d = new Date(Number(a[0]), Number(a[1]) - 1, Number(a[2]));
    if (isNaN(d))
    {
        alert('Hibás dátum formátum! (ÉÉÉÉ.HH.NN)');
        i.focus();
        return false;
    }
    i.value = d.getFullYear()+'.'+(10 > d.getMonth()+1 ? '0' : '')+(d.getMonth()+1)+'.'+(10 > d.getDate() ? '0' : '')+d.getDate();
    return true;
}

function re_escape(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}

function addhtml2table(parent, s)
{
    var o = document.createElement('div');
    o.innerHTML = '<table>' + s + '</table>';
    var t = o.getElementsByTagName('table')[0];
    for (var r = 0; r < t.rows.length; r++)
    {
        var nr = t.rows[r].cloneNode(true);
        parent.appendChild(nr);
    }
}

function delnode(n)
{
    n = $(n);
    n.parentNode.removeChild(n);
}

