$(function(){
	jQuery(".customSelect").customSelect({mainClass:'newSearchDrop'});
})


function getModels(id,selected) {
	jQuery("#Models").html('<option value="-1">... Loading ...</option>');	
	jQuery("#Models").customSelect('refresh');
	
	jQuery.ajax({
		url: 'search_ajax.php',
		data: {id:id},
		type: 'post',
		dataType: "json",
		success: function(data){
			jQuery("#Models").html('<option value="-1">Select</option>');
			jQuery.each(data,function(index,d){
				jQuery("#Models").append('<option '+(d.id==selected?'selected="selected"':'')+' value="'+d.id+'">'+d.text+'</option>');
			});
			jQuery("#Models").customSelect('refresh');
		}
	})
}

function Simple_ajaxcall( process, div, method, form )
{
	myFormData = '';
	if( !method )
	{
		method = 'get';
	}
	if( !form )
	{
		form = false;
	}
	else
	{
		myFormData = $(form).serialize(true);
	}

	if( !process ) return false;
	if( !div ) div = 'false_div';
	
	ajaxRequest = new Ajax.Request(
		process,
		{
			method: method,
			parameters: myFormData,
			onComplete: function(request) 
			{
				var myMesArr = request.responseText.split("##");
				
				// alert(request.responseText);
				if( myMesArr[0] == 'ok' )
				{
					if( $(div) )
					{
						$(div).innerHTML = myMesArr[1];
					}
					else
					{
						var index = 1;
						if( !$(myMesArr[index]) && $('resp') && !myMesArr[index].include('jscode') )
						{
							// alert(myMesArr[index++]);
							$('resp').innerHTML = myMesArr[index++];
							$('resp').style.display='';
							position_layer('resp', 1, 0, -200);
						}
						
						if( myMesArr[index].include('jscode') )
						{
							code = myMesArr[index].split('jscode');
							// alert(code[1]);
							if( code[1].include('js#') )
							{
								codes = code[1].split('js#');
								// alert(codes.length);
								for( var k=0; k<codes.length; k++ )
								{
									eval( codes[k] );
								}
							}
							else
							{
								eval( code[1] );
							}
							index++;
						}
						
						while( true )
						{
							if( $(myMesArr[index]) )
							{
								$(myMesArr[index++]).innerHTML = myMesArr[index++];
							}
							else
							{
								break;
							}
						}
						
						if( myMesArr[index].include('jscode') )
						{
							code = myMesArr[index].split('jscode')
							if( code[1].include('js#') )
							{
								codes = code[1].split('js#');
								// alert(codes.length);
								for( var k=0; k<codes.length; k++ )
								{
									eval( codes[k] );
								}
							}
							else
							{
								eval( code[1] );
							}
							index++;
						}
					}					
				}
				else { alert(request.responseText);return false; }
			}
		}
	) 
}
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 = parseInt( (dim.width-mywidth)/2 );
	if(myheight > dim.height)newtop = AktTop + 10;
  else newtop = AktTop + parseInt( (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";
}

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.png" border="0" alt="(New)" /></a>');
			jQuery("#" + captcha_plus_ID).css("display","block");
		}
	);
}
