var current_obj;

function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function showErrorLayer(msg)  {
	
	$('errorLayer').show();
	
	$('errorLayer').innerHTML = msg;
	
}

function index_changeFormAction(form, value) {

	form.action = '/search/' + value + '?search=' + decodeURIComponent(trim(form.search.value));
	
	form.submit();
	
	return(false);
}

function default_changeFormAction(form, value) {
			
	var search = trim(form.search.value);	
	var loc = '/search/' + value + '?search=' + search;
	
	parent.document.location.href = loc;
		
}

function useAdvancedSearch(obj, layer) {
	
	if($('searchLayer_1')) $('searchLayer_1').hide();
	if($('searchLayer_2')) $('searchLayer_2').hide();
	
	if($('errorLayer')) $('errorLayer').hide();
	
	var id = obj.options[obj.options.selectedIndex].value;
	
	if(id == 2) {
		document.forms['sForm'].search.disabled = true;
	}
	else {
		document.forms['sForm'].search.disabled = false;
	}
	
	if($(layer + id)) $(layer + id).show();
}

function users_checkForm(obj) {

	var search = trim(obj.search.value);
	var picture = obj.picture.value;
	
	var advanced = obj.advanced.value;	
			
	var sb_error = false;
	var is_special = false;
	
	switch(advanced) {
	
		case '0':
		default:
			if(search.length < 3) {
				showErrorLayer(ERRORS[1]);
				sb_error = 1;
			}
		break;
		
		case '1':
		
			var town = trim(obj.town.value);
			var country = trim(obj.country.value);
			var plz = trim(obj.plz.value);
			
			var age_from = obj.age_from.value;
			var age_to = obj.age_to.value;
			
			if(plz.length >= 1) {
				is_special = true;
				if(plz.length < 2 && plz.length != 0 || isNaN(plz)) {
					showErrorLayer(ERRORS[3]);
					sb_error = true;
				}
			}
		
			if(age_to >= 1) {
				is_special = true;
				if(age_to < age_from) {
					showErrorLayer(ERRORS[4]);
					sb_error = true;
				}
				if((age_to - age_from) > 10) {
					showErrorLayer(ERRORS[8]);
					sb_error = true;
				}
			}
			
			if(town.length > 1) {
				is_special = true;
				if(town.length < 3) {
					showErrorLayer(ERRORS[5]);
					sb_error = true;
				}
			}
			
			if(search.length >= 1 || is_special == false) {
				if(search.length < 3) {
					showErrorLayer(ERRORS[1]);
					sb_error = true;
				}
			}		
		break;
		
		case '2':
		
			var realm_id = obj.realm_id.value;
			var faction = obj.faction.value;
			
			var max_level = obj.max_level.value;
			var min_level = obj.min_level.value;			
		
			if(max_level > 0 || min_level > 0) {
			
				if(min_level > max_level) {
					showErrorLayer(ERRORS[6]);
					sb_error = true;
				}
				else {
					if(max_level - min_level > 10) {
						showErrorLayer(ERRORS[7]);
						sb_error = true;
					}
				}
			}
			
		break;
	
	}
	
	if(sb_error == true) {
		return(false);
	}
	
	$('errorLayer').hide();
	
	document.getElementById('searchButton').innerHTML = '<img src="images/loading.gif" width="16" height="16" />';
	
	return(true);
}

function blogs_checkForm(obj)  {
	
	var sb_error = false;
	var valid_search = false;
	
	var search = trim(obj.search.value);
	var text = trim(obj.text.value);
	var username = trim(obj.username.value);
	var blog_from = trim(obj.blog_from.value);
	var blog_to = trim(obj.blog_to.value);
	
	if(search.length > 0) {
		if(search.length < 3) {
			showErrorLayer(ERRORS[1]);
			sb_error = true;
		}
		else {
			valid_search = true;
		}
	}		
	
	if(username.length > 0) {
		if(username.length < 3) {
			showErrorLayer(ERRORS[2]);
			sb_error = true;
		}
		else {
			valid_search = true;
		}		
	}		
	
	if(text.length > 0) {
		if(text.length < 3) {
			showErrorLayer(ERRORS[3]);
			sb_error = true;
		}
		else {
			valid_search = true;
		}
	}		
		
	if(blog_from.length > 0) {
		if(blog_to.length > 0) {
						
			if(blog_to < blog_from) {
				showErrorLayer(ERRORS[5]);
				sb_error = true;
			}			
		}
	}
	
	if(valid_search == false) {
		showErrorLayer(ERRORS[4]);
		sb_error = true;
	}
	
	if(sb_error == true) {
		return(false);
	}
	
	$('errorLayer').hide();

	document.getElementById('searchButton').innerHTML = '<img src="/images/loading.gif" width="16" height="16" />';
	
	return(true);
}

function groups_checkForm(obj)  {
	
	var sb_error = false;
	
	var search = trim(obj.search.value);
	var category = obj.category.value;


	if(category <= -1) {	
		if(search.length < 3) {
			showErrorLayer(ERRORS[1]);
			sb_error = true;	
		}
	}
	else {

		if(search.length>0 && search.length < 3) {
			showErrorLayer(ERRORS[1]);
			sb_error = true;	
		}		
	}

	if(sb_error == true) {
		return(false);
	}
	
	$('errorLayer').hide();

	document.getElementById('searchButton').innerHTML = '<img src="images/loading.gif" width="16" height="16" />';
	
	return(true);
}