function $my(obj){
  return document.getElementById(obj);
}

function notversion(){alert("not working in this version!");return false;}

function dologin(){jspost("/login",{'dest':window.location.pathname});return false;}

allowpost=true;
function jspost(url,vals,newpage){
	if(newpage!=undefined) _target = ' target="_blank"';
	else _target='';
	if(allowpost){allowpost=false;var newform='<form id="newpost" action="'+url+'" method="post" '+_target+' >';for(v in vals){newform+='<input type="hidden" name="'+v+'" value="'+vals[v]+'" />';}
	newform+='</form>';$('layout').innerHTML+=newform;$('newpost').submit();}
}

function ajax(go_,id_,to_){

    var ajaxSuccess = function(json){
        if(!json) {alert('Wrong Ajax Output!');return false;}
        if(json.js || json.repl)
        {
            if(json.repl) {
                var repl = json.repl;
                for (var i in repl)
                   $(i).innerHTML = repl[i];
            }
            if (json.js){
               eval(json.js);
            }
        }
        else {
            if(json.errors){
                alert(json.errors);
            }else{
                if(json.OK)
                   alert(json.OK);
                if(json.url)
                   location.href = json.url;
            }
        }
    };

	$j.ajax({
		'dataType' : 'json',
		'async' : false,
		'url' : '/ajax/friendsmutual',
		'type' : 'POST',
		'data' : {'go':go_,'id':id_,'to':to_},
		'success': function(json){ajaxSuccess(json)}
	});

}

function checkLeng(that,checkLeng_id,maxLeng) {

	if ((typeof checkLeng_id=='undefined') || (typeof checkLeng_id!='undefined' && checkLeng_id.length==0))
		var checkLeng_id = 'checkLeng-span';
	if (maxLeng==undefined)
		var maxLeng = 150;

	txt = $j(that).val();
	txtLeng = txt.length;
	if ( txtLeng > maxLeng ) $j(that).val(txt.substring(0, maxLeng));
	$j('#'+checkLeng_id).html(parseInt(maxLeng - $j(that).val().length)+'');
	$j(that).focus();

	return true;
}

function selectImage(input) {
  
	SpawEngine.spaw_dir = "/inc/spaw/";
	
	SpawEngine.setPlatform("php");
	
	var spawArgs;
	var spawArguments;
	var spawEditor = new SpawEditor(input);
	
	SpawEngine.registerEditor(spawEditor);
	spawEditor.setTheme(SpawThemespaw2);
	spawEditor.setLang("en");
	spawEditor.setOutputCharset("");
	
	spawEditor.stylesheet = "";
	var content_page = new SpawEditorPage(input,input,"ltr");
	spawEditor.addPage(content_page);
	spawEditor.getTab(input).template = "";
	spawEditor.getTab(input).active_template = "";
	spawEditor.active_page =content_page;
	
	SpawEngine.openDialog('spawfm', 'spawfm', spawEditor, document.getElementById(input).value, 'type=flash', 'callbackSelectImage', null, null);
}

function callbackSelectImage(editor, result, tbi, sender) {
	document.getElementById(editor.name).value = result;
	window.focus();
	document.getElementById(editor.name).focus();
}
/**************************************************************************************************************************************/
/*												for form validate
/**************************************************************************************************************************************/
var site_name =  new Array ('','Taylors','Wakefield');

function checkForm() {
	setTimeout(function asd(){checkFormReal()},200);
	return false;
}
function checkFormReal(){
	var cleared = [];
	var all_cleared = [];
	for (s = 0; s < site_for_check.length; s++) {
		var j = site_for_check[s];
		var full_fields = 0;
		for (i = 0; i < form_fields_n.length; i++) {

			var fidx = form_fields_n[i].replace(/#/,j);

			if (fidx.match(/__$/)) { // это мы ищем правильный ID для SPAW
            	fidx = findIdForSPAW(fidx);
			}

			fidx = '#'+fidx;
//console.log(fidx);
			if ($j(fidx).attr('id') && $j(fidx).val().replace(/<br[ ]?[\/]?>/gi,'').replace(/<[\/]?p>/i,'').replace(/&nbsp;/,'').length)
				full_fields++;

		}

		if (full_fields!=form_fields_n.length && full_fields!=0)
			cleared[cleared.length] = site_name[j];

		if (full_fields==0)
			all_cleared[all_cleared.length] = j;

	}

	if (all_cleared.length == site_count) {
		alert('Please complete all required fields for '+(site_count==2?'any ':'')+'site');
		return false;
	}

	if (cleared.length!=0) {
		alert(cleared + '\n\nPlease complete all required fields for '+cleared+' to save');
		return false;
	}

    $j('#DATA').submit();
//	return false;
}

function findIdForSPAW(idx){
	for (k = 1; k < 20; k++) {
		if ($j('#'+idx+k).attr('id'))
			return idx+k;
	}
	return false;
}
/**************************************************************************************************************************************/

/*** RECIPES HIGHLIGHTING ***/
function hlWines(el) {
  jel = $j(el);
  if (jel.attr('recipeseveryday')) {
    att = 'rele';
    fid = jel.attr('recipeseveryday');
  } else {
    att = 'relc';
    fid = jel.attr('recipescelebrity');
  }
  jel.parent().parent().find("td[rel=wines] a").each(
    function(i,o) {
      if ($j(o).attr(att).indexOf(',' + fid + ',') >= 0 ) {
        $j(o).addClass("hlLink");
      } else {
        $j(o).removeClass("hlLink");
      }
    }
  );
}

function removeHlWines(el) {
  $j(el).parent().parent().find("td[rel=wines] a").each(function(i,o) {
        $j(o).removeClass("hlLink");
  });
}

function hlRecipes(el) {
  jel = $j(el);
  jel.parent().parent().find("td[rel=recipes] a").each(
    function(i,o) {
      addClass = false;
      if ($j(o).attr('recipeseveryday')) {
        if (jel.attr('rele').indexOf(',' + $j(o).attr('recipeseveryday') + ',') >= 0) {
          addClass = true;
        }
      } else {
        if (jel.attr('relc').indexOf(',' + $j(o).attr('recipescelebrity') + ',') >= 0) {
          addClass = true;				        
        }
      }
      if (addClass) {
        $j(o).addClass("hlLink");
      } else {
        $j(o).removeClass("hlLink");
      }
    }
  );
}

function removeHlRecipes(el) {
  $j(el).parent().parent().find("td[rel=recipes] a").each(function(i,o) {
        $j(o).removeClass("hlLink");
  });
}