function loteUserLoggedIn(json){
	if(!json.__user || !json.__user.user_id || !json.__user.user_id>0){	
		return false;
	}
	return true;
}

function isset(v){return (typeof(v)!='undefined');};

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
// set html into the error divs and display. image to be localised
function makeError(id, errorMessage) {
	var imgSrc = '/images/error_corner.gif';
	$("#"+id).html('<img src="'+imgSrc+'" /><div class="fieldErrorMsg">'+errorMessage+'</div>').fadeIn(300);
}

function makeInfoTip() {
	var imgSrc = '/images/tips_corner.gif';

	$('.infotip').each(function(counter,e){
		var subSection = $(e).attr("id").substring(0,16);
		if(subSection!="user_dob_infotip"){
			var html_str = $(this).html();
			$(this).html('<img src="'+imgSrc+'" /><div class="fieldTipsMsg">'+html_str+'</div>');
			
			var field_id = $(this).attr('id').replace('_infotip','');
			
			$('#'+field_id).focus(function(){
				$('#'+field_id+'_infotip').fadeIn(200);
			});
			$('#'+field_id).blur(function(){
				$('#'+field_id+'_infotip').fadeOut(200);
			});
		}
	});
}

$(document).ready(function(){
	// set height to advert height so that it can be shown properly
	if ($('#subcontent_area').attr('id') && $('#sidebar_advert').attr('id')){
		if ($('#sidebar_advert').height() > $('#subcontent_area').height()){
			$('#subcontent_area').height($('#sidebar_advert').height());
		}
		$('#sidebar_advert').css('right', '0px');//css('left', $('#subcontent_area').width());
		$('#subcontent_area').css('margin', '0px '+($('#sidebar_advert').width()+10)+'px 0px 0px');
	}
	// set the margin for home content with sidebar
	if ($('#sidebar_left').attr('id') && $('#content_area').attr('id')){
		$('#content_area').css('margin', '0px 0px 0px '+($('#sidebar_left').width()+10)+'px');
	}
	
	// create infotip content
	makeInfoTip();
	
	// set background of table in fieldset to #xxxxxx
	var $inputs = $('input, textarea, select, password');
	$inputs.each(function(i, el) {
		$(el).focus(function(){
			$(this).parents('table:first').css('background-color','#F0F5F8');
		});
		
		$(el).blur(function(){
			$(this).parents('table:first').css('background-color','');
		});
	});
});

function formProcessing(flag){
	if(flag==true){
		$("#form_button_div").hide();
		$("#form_processing_div").show();
	}
	else {
		$("#form_button_div").show();
		$("#form_processing_div").hide();
	}
}

function doRedirect(defaultUrl, baseUrl){
	var myRegExp1 = /servicelance.com/;
	var myRegExp2 = /logged_out/;
	var myRegExp3 = /logout/;
	var myRegExp4 = /login/;
	var myRegExp6 = /user\/activate/;

	var matchPos1 = document.referrer.search(myRegExp1);
	var matchPos2 = document.referrer.search(myRegExp2);
	var matchPos3 = document.referrer.search(myRegExp3);
	var matchPos4 = document.referrer.search(myRegExp4);
	var matchPos5 = document.referrer.indexOf(baseUrl);
	var matchPos6 = document.referrer.search(myRegExp6);
	
	if(matchPos1 != -1 && matchPos2== -1  && matchPos3== -1 && matchPos4== -1 && matchPos6== -1 && matchPos5 >=0 && baseUrl!=document.referrer) {
		window.location.href=document.referrer;
	} 
	else {
		window.location.href= defaultUrl;
	}
}
