
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 3.2
 *	LISENSE: FL0HXF6DKASG - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: smilesdelivery.com
 *	FILE: INDEX.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2010
 *	http://www.flynax.com/
 *
 ******************************************************************************/

/**
*
* var imt pressed key code
*
**/
var pressKey = false;

/**
*
* autocomplete plugin
*
* @package jQuery
* 
**/
document.onkeydown = detectKey;

function detectKey(e){
	if( !e ){
	    if( window.event )
	    {
			e = window.event;
		}
		else
		{
			return;
		}
	}

  	if( typeof( e.keyCode ) == 'number'  )
  	{
		e = e.keyCode;
	}
	else if( typeof( e.which ) == 'number' )
	{
		e = e.which;
	}
	else if( typeof( e.charCode ) == 'number'  )
	{
		e = e.charCode;
	}
	else
	{
		return false;
	}
	
	pressKey = e;
}

/**
*
* alert the message and focus current field
*
* @param srting field - jQuery format field 
* @param string message - alert message text
* 
**/
function fail_alert( field, message )
{
	Ext.MessageBox.alert(lang['alert'], message, function(){
		$(field).addClass('field_error');
		$(field).focus();
	});
}

/* adaptation for IE6 */
if(!Array.indexOf)
{
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

/**
*
* hide or show the menu blocks (via jQuery effect) by ID
*
* @param srting id - field id
* 
**/
function block_action( id )
{
	if ( $( '#lblock_'+id ).css('display') == 'block' )
	{
		$( '#lblock_'+id ).slideUp('normal');
		$( '#lb_status_'+id ).removeClass('lb_head_rpart_max');
		$( '#lb_status_'+id ).addClass('lb_head_rpart_min');
		
		createCookie('adMenu_'+id, 'hide', 30);
	}
	else
	{
		$( '#lblock_'+id ).slideDown('slow');
		$( '#lb_status_'+id ).removeClass('lb_head_rpart_min');
		$( '#lb_status_'+id ).addClass('lb_head_rpart_max');
		
		var tab_cookie = readCookie('adMenu_'+id);
		
		if ( tab_cookie == 'hide' )
		{
			createCookie('adMenu_'+id, 'show', 1);
		}
	}
}

/**
*
* hide or show the fieldset blocks (via jQuery effect) by ID
*
* @param srting id - field id
* 
**/
function fieldset_action( id )
{
	if ( $( '#'+id ).css('display') == 'block' )
	{
		$( '#'+id ).slideUp('normal');
		$( '#legend_'+id ).removeClass('up');
		$( '#legend_'+id ).addClass('down');
	}
	else
	{
		$( '#'+id ).slideDown('slow');
		$( '#legend_'+id ).removeClass('down');
		$( '#legend_'+id ).addClass('up');
	}
}

/**
*
* hide or show the object (via jQuery effect) by ID, and hide all objects by html path
*
* @param srting id - field id
* @param srting path - html path
* 
**/
function show( id, path )
{
	if (path != '')
	{
		$(path+'.hide').slideUp('normal');
	}
	
	if ( !id )
		return false;

	if ( $( '#'+id ).css('display') == 'block' )
	{
		$( '#'+id ).slideUp('slow');
	}
	else
	{
		$( '#'+id ).slideDown('slow');
	}
}

/**
*
* hide or show the plan types (via jQuery effect) by ID, and hide all objects by html path
*
* @param srting id - field id
* @param srting path - html path
* 
**/
function show_plan_types( id, path )
{
	if ( id == 'featured' )
	{
		$( '#package' ).slideUp('normal');
		$( '#'+id ).slideUp('normal');	
	}
	else
	{
		if (path != '')
		{
			$(path+'.hide').slideUp('fast');
			$( '#featured' ).slideDown('fast');
		}
		
		if ( id == 'package' )
		{
			$( '#using_limit' ).slideUp('fast');
		}
		else
		{
			$( '#using_limit' ).slideDown('normal');
		}

		if ( $( '#'+id ).css('display') == 'block' )
		{
			$( '#'+id ).slideUp('normal');
		}
		else
		{
			$( '#'+id ).slideDown('slow');
		}
	}
}

/**
*
* hide or show the object on the block page (via jQuery effect) by ID, and hide all objects by html path
*
* @param srting id - field id
* @param srting path - html path
* 
**/
function block_banner( id, path )
{
	if ( id == '')
	{
		id = 'btype_other';
	}

	if( id != 'btype_banner' )
	{
		if( id == 'btype_html' )
		{
			$( '#btype_html' ).slideDown('normal');
			$( '#btype_banner' ).slideUp('fast');
			$( '#btype_other' ).slideUp('fast');
		}
		else
		{
			$( '#btype_other' ).slideDown('normal');
			$( '#btype_banner' ).slideUp('fast');
			$( '#btype_html' ).slideUp('fast');
		}
	}
	else
	{
		$( '#btype_banner' ).slideDown('normal');
		$( '#btype_other' ).slideUp('fast');
		$( '#btype_html' ).slideUp('fast');
	}
	
}

/* jQuery block manager | auto size controller */
$(document).ready(function() {
	$('.size').click(function(){
		if ($(this).attr('id') == 'original' || $(this).attr('id') == 'block')
		{
			$('#resW').attr('readonly', true);
			$('#resH').attr('readonly', true);
			$('#resW').val('');
			$('#resH').val('');
		}	
		else if ($(this).attr('id') == 'flash')
		{
			$('#resW').attr('readonly', false);
			$('#resH').attr('readonly', false);
			$('#original').attr('disabled', true);
			$('#block').attr('disabled', true);
			$('#custom').attr('checked', true);
		}
		else if ($(this).attr('id') == 'image')
		{
			$('#original').attr('disabled', false);
			$('#block').attr('disabled', false);
		}
		else
		{
			$('#resW').attr('readonly', false);
			$('#resH').attr('readonly', false);
		}
	});
});

/* jQuery textarea resizer plugin usage */
$(document).ready(function() {
	$('textarea.resizable:not(.processed)').TextAreaResizer();
});

/**
*
* prompt alert
*
* @param string message - prompt message text
* @param srting method  - javascript method (function)
* @param Array  params  - method (function) params
* @param string load_object  - load object ID
* @param string mod  - confirm mod
* 
**/
function rlConfirm( message, method, params, load_object, mod )
{
	Ext.MessageBox.confirm('Confirm', message, function(btn){
		if ( btn == 'yes' )
		{
			if ( mod == 'smarty')
		{
			var func = method+'('+params+')';
		}
		else
		{
			var func = method+'(\"'+params+'\")';
		}
		
		eval(func);
		
		$('#'+load_object).fadeIn('normal');
		}
	});
}

/**
*
* listing fields manager
*
* @param string type - field type
* 
**/
function field_types( type )
{
	$('#additional_options div.hide').slideUp('fast');
	$('.data_format').attr('disabled', true);
	$('#field_'+type).slideDown('normal');
	$('#field_'+type+' select').attr('disabled', false);
}

/**
*
* data formats manager
*
* @param string id - selector id
* 
**/
function format( id )
{
	$('.data_format').each(function(){
		if ($(this).attr('disabled') != true)
		{
			if ( $(this).val() == '0' )
			{
				$('#'+id).slideDown('normal');
			}
			else
			{
				$('#'+id).slideUp('fast');
			}
		}
	});
	
}

/**
*
* image resize fieds action
*
* @param string type - resize type
* 
**/
function resize_action( type )
{
	$('#resW').attr('readonly', false);
	$('#resH').attr('readonly', false);
	
	if (type == 'W')
	{
		$('#resH').attr('readonly', true);
	}
	else if (type == 'H')
	{
		$('#resW').attr('readonly', true);
	}
	else if (type == 'C')
	{
		$('#resW').attr('readonly', false);
		$('#resH').attr('readonly', false);
	}
	else
	{
		$('#resW').attr('readonly', true);
		$('#resH').attr('readonly', true);
	}
}

/**
*
* @var string (step) - item step
*
**/
var step = null;

/**
*
* listing fields builder
*
* @param string type  - field type
* @param string langs - system langs
* 
**/
function field_build( type, lg )
{
	var data = '';
	
	// set default field type
	if ( type == 'checkbox' )
	{
		input_d_type = 'checkbox';
	}
	else
	{
		input_d_type = 'radio';
	}
	
	eval("step = "+type+"_step");
	
	data += '<tr id="'+type+'_'+step+'"><td>';
	
	for (var i = 0; i <= lg.length-1; i++)
	{
		var item = lg[i].split('|');
		data += '<div style="height: 29px;"><input type="text" class="text" name="'+type+'['+step+']['+item[0]+']" />&nbsp;&nbsp;&nbsp;'+lang['ext_item_value']+' <small>(<b>'+item[1]+'</b>)</small>&nbsp;&nbsp;&nbsp;</div>';
	}
	
	// default button build
	data += '</td><td class="add_f_item">&nbsp;<input id="'+type+'_def_'+step+'" type="'+input_d_type+'" name="'+type;
	if ( type == 'checkbox' )
	{
		data += '['+step+']';
	} 
	data += '[default]" value="'+step+'" />&nbsp;<label for="'+type+'_def_'+step+'">'+lang['ext_default']+'</label>&nbsp;&nbsp;&nbsp;</td>';
	
	// remove button build
	data += '<td>&nbsp;&nbsp;&nbsp;<span onclick="$(\'#'+type+'_'+step+'\').remove();" class="delete">'+lang['ext_remove']+'</span></td></tr>';

	eval(type+"_step++");
	
	$("#"+type).append(data);
}

/**
*
* check form
*
* @param array data - form data
* @param srting method  - javascript method (function)
* @param Array  params  - method (function) params
* @param string load_object  - load object ID
* 
**/
function rlCheck( data, method, load_object )
{
	var len = data.length;
	var criterion = null;
	var cond = null;
	
	var params = "Array( ";
	
	for (var i=0; i<len; i++)
	{
		var object = data[i][0].indexOf('.') == 0 ? data[i][0]+':checked' : '#'+data[i][0];
		
		$(object).removeClass('field_error');
		
		criterion = data[i][2] == 'undefined' ? null : data[i][2];
		params += "Array('"+data[i][0]+"', '"+quote($(object).val())+"', '"+criterion+"')";

		if ( i+1 != len)
		{
			params += ', ';
		}
		
		if (data[i][0] != '')
		{
			if (!data[i][2])
			{
				if ($(object).val() == '' || typeof($(object).val()) == 'undefined')
				{
					return fail_alert( object, data[i][1] );
				}
			}
			else
			{
				cond = data[i][2].split('^');
				
				if (cond[0] == 'f')
				{
					var query = "var res = "+cond[1]+"('"+$(object).val()+"') );";
				}
				else
				{
					var query = "var res = "+$(object).val().length+cond[0]+cond[1]+" ? true : false;";
				}
				
				eval(query);
				
				if (!res)
				{
					return fail_alert( object, data[i][1] );
				}
			}
		}
		
		criterion = null;
	}
	
	params += ")";

	var func = method+'('+params+')';

	eval(func);
	
	$('#'+load_object).fadeIn('normal');
}

/**
*
* additional checking form for add phrase
*
* @param array data - form data
* 
**/
function js_addPhrase(params)
{
	var vars = "Array( ";
	
	var len_v = params.length;

	for (var i = 0; i < params.length; i++)
	{
		vars += "Array( '"+quote(params[i][0])+"', '"+quote(params[i][1])+"' )";
		
		if ( len_v != i+1)
		{
			vars += ", ";
		}
	}
	
	vars += " )";

	var len = 0;
	
	$('#lang_add_phrase textarea').each(
		function(){
			len++;
		}
	);

	var values = "Array( ";
	
	var step = 1;
	
	$('#lang_add_phrase textarea').each(
		function(){
			values += "Array( '"+quote($(this).attr('name'))+"', '"+quote($(this).val(), true)+"' )";
			if (len != step)
			{
				values += ", ";
			}
			step++;
		}
	);
	
	values += ")";
	
	var func = 'xajax_addPhrase('+vars+', '+values+')';
	
	eval(func);
}

var form_submit = true;

/**
*
* forms submit handler
*
* @param array data - form data
* 
**/
function submitHandler()
{
	if ( form_submit )
		return true;
	else
		return false;
}

/**
*
* selectOptions
*
* @package jQuery
* 
**/
(function($) {
	$.fn.selectOptions = function(option){
		if ( false !== $(this).attr('class').indexOf('text') )
		{
			$(this).val(option);
		}
		else
		{
			$(this).children('option').each(function(){
				
				if ( $(this).val() == option )
				{
					$(this).attr('selected', true);
				}
			});
		}
	}
})(jQuery);

/**
*
* autocomplete plugin
*
* @package jQuery
* 
**/
(function($) {
	$.fn.rlAutoComplete = function(options){
		options = jQuery.extend({
			type: '*',
			add_id: false,
			id: false
		},options)

		var store = new Array;
		var query = '';
		var obj = this;
		var set = false;
		var set_hidden = false;
		var cur_item = 0;
		var total = 0;
		var interface = '<div id="ac_interface" class="autocomplete"></div>';
		var hidden_field = '<input type="hidden" name="" id="ac_hidden" value="'+options.id+'" />';
		var save_pos = 0;
		
		var key_enter = 13;
		var key_down = 40;
		var key_up = 38;
		var poss = 0;

		$('body').click(function(){
			if ( pressKey != key_enter )
			{
				cur_item = 0;
			}
		});
		
		if ( options.id )
		{
			$(obj).after(hidden_field);
			$('#ac_hidden').attr('name', $(obj).attr('name'));
			$(obj).attr('name', $(obj).attr('name')+'_tmp');
			set_hidden = true;
		}
		
		$(obj).attr('autocomplete', 'off');
		
		$(obj).keyup(function(){
			if ( pressKey == key_enter )
			{
				$(obj).val($('#ac_item_'+cur_item).html().replace(/<b>/i, '').replace(/<\/b>/i, ''));
				$('#ac_interface').hide();
				
				// clear save position
				save_pos = poss = 0;
				
				if ( options.add_id )
				{
					var index = $('#ac_item_'+cur_item).attr('id').split('_')[2];
					index--;
					$('#ac_hidden').attr('value', store[index]['ID']);
					
					/* allow forms submition */
					form_submit = true;
				}
			}
			else if ( pressKey == key_down )
			{
				if ( cur_item < total && $('#ac_interface').css('display') == 'block')
				{
					cur_item++;
					poss += 24;
					drow();
				}
			}
			else if ( pressKey == key_up && $('#ac_interface').css('display') == 'block' )
			{
				if (cur_item > 1)
				{
					cur_item--;
					poss -= 24;
					drow('up');
				}
			}

			if( (query != $(this).val() && pressKey != key_enter) || (pressKey == key_down && cur_item == 0) )
			{
				/* build interface */
				if ( !set )
				{
					$(obj).after(interface);
					var obj_margin = $(obj).css('margin-left');
					var obj_poss = $(obj).position();
					
					$('#ac_interface').css({left: obj_poss.left, top: obj_poss.top}).css('margin-left', obj_margin);
					if ( options.add_id && !set_hidden )
					{
						$(obj).after(hidden_field);
						$('#ac_hidden').attr('name', $(obj).attr('name'));
						$(obj).attr('name', $(obj).attr('name')+'_tmp');
						set_hidden = true;
					}
					set = true;
				}
				
				if ( pressKey != key_down )
				{
					cur_item = 0;
				}
				
				/* do complete */
				$(obj).addClass('autocomplete_load');
				$.getJSON("controllers/accounts_autocomplete.inc.php", {str: $(this).val(), type: options.type, add_id: options.add_id}, function(response){
					total = response.length;
					if(response != '')
					{
						store = response;
						var content = '';
						for (var i=0;i<response.length;i++)
						{
							var search = eval('/'+query+'/i');
							var out = response[i]['Username'].replace(search, '<b>'+query+'</b>' )
							var index = i+1;
							content +='<div id="ac_item_'+index+'" style="padding: 5px;color: #777;">'+out+'</div>';
						}
						$('#ac_interface').html(content);
						$('#ac_interface').show();
						
						if ( options.add_id )
						{
							/* prevent forms submition */
							form_submit = false;
							//$(obj).val($('#ac_item_'+cur_item).html().replace(/<b>/i, '').replace(/<\/b>/i, ''));
							//$('#ac_interface').hide();
						}
			
						$('#ac_interface div').click(function(){
							$(obj).val($(this).html().replace(/<b>/i, '').replace(/<\/b>/i, ''));
							$('#ac_interface').hide();
							
							// clear save position
							save_pos = poss = 0;
							
							if ( options.add_id )
							{
								form_submit = true;
								var index = $(this).attr('id').split('_')[2];
								index--;
								$('#ac_hidden').attr('value', response[index]['ID']);
							}
						});
		
						$('#ac_interface div').mouseover(function(){
							$('#ac_interface div').css('background-color', 'white');
							$(this).css('background-color', '#faefe9');
						});
		
						$('#ac_interface div').mouseout(function(){
							$(this).css('background-color', 'white');
						});
		
						$('body').click(function(){
							$('#ac_interface').hide();
							
							// clear save position
							save_pos = poss = 0;
						});
					}
					else
					{
						$('#ac_interface').hide();
					}
					$(obj).removeClass('autocomplete_load');
				});
				query = $(this).val();
			}
		});
		
		function drow(direction)
		{
			$('#ac_interface div').css('background-color', 'white');
			$('#ac_item_'+cur_item).css('background-color', '#faefe9');

			$('#ac_interface').animate({scrollTop:poss-48}, 100);
		}
	}
})(jQuery);

/**
*
* @paccage jQuery script
*
* account types description manageer
* 
**/
$(document).ready(
	function (){
		$('#type_selector').change(
			function(){				
				//reload additional fields block
				$('#reg_step2').slideUp('slow', function(){
					$('#additional_fields').html('');
				});

				//show next button
				$('#next1').slideDown('normal');
			}
		);
	}
);

/**
*
* escape or replace quotes
*
* @param string str - string for replacing
* @param bool to - replace if true and escape if false
* 
**/
function quote( str, to )
{
	if ( typeof(str) == 'undefined' )
	{
		return false;
	}
	
	if (!to)
	{
		return str.replace(/'/g, "").replace(/"/g, "");
	}
	else
	{
		var to_single = '&rsquo;';
		var to_double = '&quot;';
		
		return str.replace(/'/g, to_single).replace(/"/g, to_double).replace(/\n/g, '<br />' );
	}
}
