function JQS_ajaxcall( process, div, method, form )     // jQuery verion
{
    var pre = '#';
    if( !process ) return false;
    if( !div ) div = 'false_div';
    
	myFormData = '';
	if( !method )
	{
		method = 'GET';
	}
	
	if( !form )
	{
		form = false;
	}
	else if( !jQuery(pre+form) )
	{
		form = false;
		method = 'GET';
	}
	else
	{
		myFormData = jQuery(pre+form).serialize(true);
	}
    
    jQuery.ajax({
        url: process,
        // global: false,
        type: method,
        data: myFormData,
        dataType: "html",
        async:false,
        success: function( msg )
        {
            JQProc_ajaxcall( msg, div );
        }
    });
}

function JQProc_ajaxcall( msg, div )    // jQuery verion
{
    var pre = '#';
    if( !div ) div = 'false_div';
    var myMesArr = msg.split("##");
    // alert(myMesArr[0]);
    
    if( myMesArr[0] == 'ok' )
    {
        if( div != 'false_div' && jQuery(pre+div) )
        {
            jQuery(pre+div).innerHTML = myMesArr[1];
        }
        else
        {
            var index = 1;
            if( !document.getElementById(myMesArr[index]) && document.getElementById('resp') && !/jscode/.test(myMesArr[index]) )
            {
                jQuery(pre+'resp').html( myMesArr[index++] );
                jQuery(pre+'resp').show();
            }
            
            var start = 0;
            while( index > start )
            {
                start = index;
                if( /jscode/.test(myMesArr[index]) )
                {
                    code = myMesArr[index].split('jscode');
                    if( /js#/.test(code[1]) )
                    {
                        codes = code[1].split('js#');
                        for( var k=0; k<codes.length; k++ )
                        {
                            if( /notcode/.test(codes[k]) )
                            {
                                codes[k] = codes[k].substr(7);
                                continue;
                            }
                            eval( codes[k] );
                        }
                    }
                    else
                    {
                        eval( code[1] );
                    }
                    index++;
                }
                
                while( true )
                {
                    if( myMesArr[index] && document.getElementById(myMesArr[index]) )
                    {
                        jQuery(pre+myMesArr[index]).show();
                        jQuery(pre+myMesArr[index++]).html( myMesArr[index++] );
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }					
    }
    else { alert(msg);return false; }
}

function PTS_ajaxcall( process, div, method, form ) // prototype verion
{
    if( !process ) return false;
    if( !div ) div = 'false_div';
    
	myFormData = '';
	if( !method )
	{
		method = 'get';
	}
	if( !form )
	{
		form = false;
	}
	else
	{
		myFormData = $(form).serialize(true);
	}
	
	// alert('ajax fv.: ' + process + ' :::: myFormData: ' + myFormData + ' :::: method: ' + method);
	ajaxRequest = new Ajax.Request(
		process,
		{
			method: method,
			parameters: myFormData,
			onComplete: function(request) 
			{ 
				PTProc_ajaxcall( request.responseText, div );
			}
		}
	) 
}

function PTProc_ajaxcall( msg, div )    // prototype verion
{
    var pre = '';
    if( !div ) div = 'false_div';
    var myMesArr = msg.split("##");
    // alert(myMesArr[0]);
    
    if( myMesArr[0] == 'ok' )
    {
        if( div != 'false_div' && $(div) )
        {
            $(div).innerHTML = myMesArr[1];
        }
        else
        {
            var index = 1;
            if( !$(myMesArr[index]) && $('resp') && !myMesArr[index].include('jscode') )
            {
                $('resp').innerHTML = myMesArr[index++];
                $('resp').style.display='';
            }
            
            var start = 0;
            while( index > start )
            {
                start = index;
                if( /jscode/.test(myMesArr[index]) )
                {
                    code = myMesArr[index].split('jscode');
                    if( /js#/.test(code[1]) )
                    {
                        codes = code[1].split('js#');
                        for( var k=0; k<codes.length; k++ )
                        {
                            if( /notcode/.test(codes[k]) )
                            {
                                codes[k] = codes[k].substr(7);
                                continue;
                            }
                            eval( codes[k] );
                        }
                    }
                    else
                    {
                        eval( code[1] );
                    }
                    index++;
                }
                
                while( true )
                {
                    if( $(myMesArr[index]) )
                    {
                        $(myMesArr[index]).show();
                        $(myMesArr[index++]).innerHTML = myMesArr[index++];
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }					
    }
    else { alert(msg);return false; }
}

function Set_EmptyOption( div )
{
	var arrDiv = div.split(';');	
	arrDiv.each( function( s )
	{
		var id = 'ct_' + s;
		if( $(id) )
		{
			$(id).innerHTML = '<select name=""><option value="">-</option></select>';
		}
	});
}

function position_layer(div, type, korx, kory )
{
	if( !type ) type = 1;
	if( !korx ) korx = 0;
	if( !kory ) kory = 0;
	
  var dim = document.viewport.getDimensions();
  var myLayer = $(div);
  AktTop = document.viewport.getScrollOffsets().top;
  mywidth = myLayer.getWidth();
  myheight = myLayer.getHeight();
  // myheight += (myheight<50 ? plusheight : 0);
	
	// alert( dim.width + ' - ' + AktTop + ' :: ' + mywidth + ' - ' + myheight );
  
  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;

	if( type == 1 )
	{
		myLayer.setStyle({ left : newLeft + korx + "px", top : newtop + kory + "px" });
	}
	else if( type == 2 )
	{
		myLayer.setStyle({ left : newLeft + korx + "px" });
	}
	else if( type == 3 )
	{
		myLayer.setStyle({ top : newtop + kory + "px" });
	}
  // alert(myLayer.style.left + ' - ' + myLayer.style.top);
  // myLayer.style.left = newLeft + "px";
  // myLayer.style.top = newtop + "px";
}

function setCheckedValue(radioID, radioLength) 
{
	if(radioLength == undefined) {
		return;
	}
	
	for(var i = 0; i < radioLength; i++) 
	{
		// $(radioID + '_' + i).checked = false;
		setCheckedValueItem(radioID + '_' + i);
	}
}

function setCheckedValueItem(radioID) 
{
    // alert( jQuery('#'+radioID).attr( 'checked' ) );
	jQuery('#'+radioID).attr( 'checked', false )
}

var captcha_here_ID = 'captcha_here';
var captcha_plus_ID = 'captcha_plus';
function Init_Captcha( num )
{
	if( !num )num = '';
	
	var spamsource_ID = 'spamsource' + num;
	captcha_here_ID = 'captcha_here' + num;
	captcha_plus_ID = 'captcha_plus' + num;
	
	jQuery('#' + spamsource_ID).html( '<div class="ctextarea" style="background:none; height:auto;">'+
		'<div id="' + captcha_here_ID + '" style="float:left; width:140px; height:45px; font-size:10px !important;"></div>'+	
		'<div style="float:left;" id="' + captcha_plus_ID + '"><a href="javascript:loadCaptcha_ajax( \''+captcha_here_ID+'\', \''+captcha_plus_ID+'\' ) "><img src="captcha_files/refresh.png" border="0" alt="(New)" /></a></div>'+
	'</div>');
	loadCaptcha_ajax( captcha_here_ID, captcha_plus_ID );
}

function loadCaptcha_ajax( captcha_here_ID, captcha_plus_ID ) 
{
	jQuery("#" + captcha_plus_ID).css("display","none");
	jQuery("#" + captcha_here_ID).html('Loading code...<br />Please wait...');
	jQuery.get("captcha_files/captcha_table.php", { },
		function(data)
		{	
			jQuery("#" + captcha_here_ID).html(data);
			jQuery("#" + captcha_plus_ID).html('<a href="javascript:loadCaptcha_ajax( \''+captcha_here_ID+'\', \''+captcha_plus_ID+'\' )"><img src="captcha_files/refresh.gif" border="0" alt="(New)" /></a>');
			jQuery("#" + captcha_plus_ID).css("display","block");
		}
	);
}
