{include file="$gentemplates/$header_tpl"}
<h2>{$lang.section.a_search}</h2>
{if $err eq 1}<p><font class="error_msg">*&nbsp;{$lang.err.no_searchname}</font></p>{/if}
{if $err eq 2}<p><font class="error_msg">*&nbsp;{$lang.err.exist_searchname}</font></p>{/if}
{if $err eq 3}<p><font class="error_msg">*&nbsp;{$lang.search.empty_result}</font></p>{/if}
  <table width="100%" border="0" cellpadding="0" cellspacing="0">
  <!-- search form -->
  <tr>
    <td {if $banner.right}width="85%" valign="top"{/if}>
    
    <form action="{$form.search_action}" method="post" name="search_form" id="adv_search_form" style="margin: 0px;">
    {$form.search_hiddens}
      <div class="content content_adult">
        <div class="advanced_search">
        <table cellpadding="0" cellspacing="0" border="0" width="100%" class="qs_form">
			<tr>
				<td colspan="2" style="padding-bottom: 15px; padding-top: 5px;" width="100%">
					<table cellpadding="0" cellspacing="0" width="100%">
					  <tr>
							<td width="33%">
								<input type="checkbox" name="who_look_me" value="1" {if $data.who_look_me eq 1}checked{/if}>
								&nbsp;{$lang.home_page.who_look_me}
							</td>
							<td width="33%">
								<input type="checkbox" name="foto_only" id="foto_only" value="1" {if $data.foto_only eq 1}checked{/if}>
								&nbsp;{$lang.search.foto}
							</td>
							<td width="33%">
								<input type="checkbox" name="online_only" id="online_only" value="1" {if $data.online_only eq 1}checked{/if}>
								&nbsp;{$lang.search.online}
							</td>
					  </tr>
					</table>
				</td>
			</tr>
			{*<tr>
              <td style="padding-bottom: 15px; padding-top: 5px;" width="50%">
                <table cellpadding="0" cellspacing="0">
                  <tr>
                  	<td style="padding-top: 3px; width:130px;">{$lang.search.foto}:</td>
                    <td><input type="checkbox" name="foto_only" id="foto_only" value="1" {if $data.foto_only eq 1}checked{/if}></td>
                  </tr>
                </table>
              </td>
              <td style="padding-bottom: 15px; padding-top: 5px;" width="50%">
                <table cellpadding="0" cellspacing="0">
					<tr>
						<td style="padding-top: 3px;width:130px;">{$lang.search.online}:</td>
						<td><input type="checkbox" name="online_only" id="online_only" value="1" {if $data.online_only eq 1}checked{/if}></td>
                  </tr>
                </table>
              </td>
            </tr>*}
        {assign var="count" value=0}
        {foreach item=item from=$elements name=elements}
            {assign var="count" value=$count+1}
            {if $smarty.foreach.elements.iteration is not div by 2 || $smarty.foreach.elements.first}
            <tr>
            {/if}
                <td width="50%" valign="top" style="padding-top: 5px;">
                    {if $item.position_text == 'inline'}
                        {$item.elem_html}&nbsp;
                        {if $lang.profile[$item.name_for_lang] ne ''}
                            {$lang.profile[$item.name_for_lang]}
                        {else}
                            {$item.name_for_lang}
                        {/if}
                    {else}
                        {include file="$site_path/$template_root/registration_edit_element.tpl"}
                    {/if}
                </td>
            {if $smarty.foreach.elements.iteration is div by 2 || $smarty.foreach.elements.last}
                {if $smarty.foreach.elements.last}
                    {if $count is not even}
                        <td></td>
                    {/if}
                {/if}
            </tr>
            {/if}
        {/foreach}
		
            <tr>
              <td colspan=2 style="padding: 0px 0px;" align="{$default_right}"><input type="button" class="big_button" onclick="javascript: document.search_form.submit();" value="{$lang.button.search}"></td>
            </tr>
        </table>
        </div>
      </div>
      {strip}<!-- save\load form-->
      <div style="padding-top: 20px;{if $user eq 1}display: none;{/if}">
      	<table width="100%" cellpadding="0" cellspacing="0">
	      <tr><td align="{$default_left}" class="advanced_search" style="width: 50%;" valign="top">
	        <!--save criteria form-->
	        <div class="content content_adult">
	          <h3>{$lang.section.a_searchsave_header}</h3>
	          <div class="qs_form" style="padding-top:15px;">
	            <table border="0" cellspacing="0" cellpadding="0">
	            <tr>
	              <td valign="top" align="{$default_left}"><input type="text" name="search_name" id="search_name" value="{if !$loadname}{$lang.advanced_search.my_custom_search}{else}{$loadname}{/if}" style="width: 165px" maxlength="100" class="index_input"></td>
	            </tr>
				<tr>
				  <td valign="top" style="padding-top: 10px;"><input type="button" class="index_btn" onclick="javascript:SaveSearchForm();" value="{$lang.button.save}"><div style="display:inline; padding: 10px;" id="save_result"></div></td>
	            </tr>
	            </table>
	          </div>
	        </div>
	        <!-- /save criteria form-->
	      </td>
		  <td style="padding: 0 10px; font-size:0;">&nbsp;</td>
	      <td align="{$default_left}" class="advanced_search" style="width: 50%; " valign="top">
	        <!--load criteria form-->
	        <div class="content content_adult" style="padding-bottom: 10px;">
	          <h3>{$lang.section.a_searchload_header}</h3>
	          <div class="qs_form" style="padding-top:15px;">
	            <table border="0" cellspacing="0" cellpadding="0">
	            <tr>
	              <td align="{$default_left}" valign="top" colspan="2">
	                <select class="index_select" name="load_id" id="load_id" style="width: 165px;">
	                  <option value="0">{$lang.home_page.select_default}</option>
	                  {html_options values=$load_id output=$load_name}
	                </select>
	              </td>
				</tr>
				<tr>
	              <td valign="top" style="padding-top: 10px;"><input type="button" class="index_btn" onclick="javascript: LoadSaved();" value="{$lang.button.load}"></td>
	              <td valign="top" style="padding: 10px 10px 0 10px;"><input type="button" class="index_btn" onclick="javascript: RemoveSaved();" value="{$lang.button.delete}"><div style="display:inline; padding: 10px;" id="delete_result"></div></td>
	            </tr>
	            </table>
	          </div>
	        </div>
	        <!-- /load criteria form-->
	      </td></tr>
	      </table>
      </div>
      <!-- //save\load form-->
      {/strip}
  </form>
  </td>
 		{if $banner.right}
		<td width="1%"></td><td width="14%" style="text-align:center;vertical-align:top;">
		<div style="float: left;">
		{$banner.right}
		</div>
		</td>
		{/if}	  							  
  </tr>
  <!-- /search form -->
  </table>

{$load_script}

<script>

{literal}
function Clear(){};

function RemoveSaved(){
    $.ajax({
        type: 'POST',
        url: SITE_ROOT+'/{/literal}{$seo.advanced_search}{literal}?sel=delete',
        data: {'load_id': $('#load_id').val()},
        success: function(data){
            //$('#delete_result').html(data);
			ShowAjaxError('', data);
        }
            });
        }
function LoadSaved(){
	if (document.getElementById('load_id').value > 0) {
		document.location.href=SITE_ROOT+'/{/literal}{$seo.advanced_search}{literal}?sel=load&load_id='+document.getElementById('load_id').value;
	} else {
		return false;
	}
}

function SaveSearchForm(){
    var massive = new Object();
    $('div .div_name_for_lang').each(function(){
        var div = $(this);
        var div_id = $(this).attr('id');

        div.find('input').each(function(){
            if($(this).attr('type')=='checkbox' || $(this).attr('type')=='radio'){
                var inval = $(this).val();
                if(typeof(massive[div_id]) == 'undefined' && $(this).attr('checked')){
                    massive[div_id] = new Object();
                }

                if(typeof($(this).attr('id')) !== 'undefined' && $(this).attr('id').indexOf('id_looking_user_subtypes') != -1){ //name
                    if(typeof(massive[div_id]['id_looking_user_subtypes']) == 'undefined'){
                        massive[div_id]['id_looking_user_subtypes'] = new Object();
                    }
                    if($(this).attr('checked')){
                        massive[div_id]['id_looking_user_subtypes'][inval] = 1;
                    }
                }
                else{
                    if($(this).attr('checked')){
                        massive[div_id][inval] = 1;
                    }
                }
            }
        });
        div.find('select').each(function(){
            var select = $(this);
            if($(this).attr('name') == 'looking_for[]'){
                if(typeof(massive[div_id]['looking_for']) == 'undefined'){
                    massive[div_id]['looking_for'] = new Object();
                }
                select.find('option').each(function(){
                    var inval = $(this).val();
                    if($(this).attr('selected')){
                        massive[div_id]['looking_for'][inval] = 2;
                    }
                });
            }
        	else{		
                select.find('option').each(function(){
                    var inval = $(this).val();
                    if(typeof(massive[div_id]) == 'undefined' && $(this).attr('selected')){
                        massive[div_id] = new Object();
            		}
                    if(/[0-9]/.test(inval) && $(this).attr('selected')){
                        massive[div_id][inval] = 2;
        			}
                });
    		}
		});
    });

    if(typeof(massive['foto_only']) == 'undefined' && $('#foto_only').attr('checked')){
        massive['foto_only'] = 1;
    }
    if(typeof(massive['online_only']) == 'undefined' && $('#online_only').attr('checked')){
        massive['online_only'] = 1;
    }

    $.ajax({
        type: 'POST',
        url: SITE_ROOT+'/{/literal}{$seo.advanced_search}{literal}?sel=save',
        data: 'str=' + serialize(massive) + '&name='+$('#search_name').val(),
        success: function(data){
			ShowAjaxError('', data);
    	}
	});
}

function serialize (mixed_value) {
        var _utf8Size = function (str) {
            var size = 0, i = 0, l = str.length, code = '';
            for (i = 0; i < l; i++) {
                code = str[i].charCodeAt(0);
                if (code < 0x0080) {
                    size += 1;
                } else if (code < 0x0800) {
                    size += 2;
                } else {
                        size += 3;
                        }
            }
            return size;
        };
    var _getType = function (inp) {
        var type = typeof inp, match;
        var key;

        if (type === 'object' && !inp) {
            return 'null';
        }
        if (type === "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';

    switch (type) {
        case "function":
            val = "";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
                        val = "s:" + _utf8Size(mixed_value) + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                            if (mixed_value.hasOwnProperty(key)) {
                   ktype = _getType(mixed_value[key]);
                       if (ktype === "function") {
                           continue;
                       }

                       okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                       vals += this.serialize(okey) +
                               this.serialize(mixed_value[key]);
                       count++;
                        }
            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;
    }
    if (type !== "object" && type !== "array") {
        val += ";";
    }
    return val;
}
$(document).ready(function() {
	$('#div_id_relationship').find('.all').click(function(){
		if($('#div_id_relationship').find('.all').is(':checked')){
			$('#div_id_relationship').find('input[type="checkbox"]').attr('checked', 'checked');
		}else{
			$('#div_id_relationship').find('input[type="checkbox"]').removeAttr('checked');
		}
	});
	$('#div_id_relationship').find('.id_relationship_uniq').change(function(){
		if(!$(this).is(':checked')){
			$('#div_id_relationship').find('.all').removeAttr('checked');
		}
	});
	
	if($('#div_id_relationship').find('.all').is(':checked')){
		$('#div_id_relationship').find('input[type="checkbox"]').attr('checked', 'checked');
	}
});

    $('#adv_search_form').find('select[name=id_language_1]').attr('name', 'id_language_1[]');

    $('#adv_search_form').find('input[type=radio]').each(function(){
        if($(this).attr('name') != 'looking_for[]'  && $(this).attr('name') != 'im'){
            name = $(this).attr('name');
            value = $(this).val();
            var html = '<input type="checkbox" checked name="'+name+'[]" value="'+value+'" />';
            $(this).after(html).remove();
        }
    });
$('select[name=id_language_2]').remove();
$('select[name=id_language_3]').remove();
var resize=0;
</script>

{/literal}
{include file="$site_path$index_theme_path/like.tpl"}

{include file="$gentemplates/$footer_tpl"}