/*Function to validate the search form*/
function init()
{
	initMessages();
	//If any information to show, show this, not the errors
	if($("div.information").length > 0 && $("div.information").html() !="")
		showInformation()
	else
		showErrors();
}

function validateLoginForm(form)
{
	var errorText ="";

	if($.trim(form.username.value).length<=0)
	{
		errorText=errorText + message_with_args("errors.required", messages["com.username"]) +"<br/><br/>";
	}

	if($.trim(form.password.value).length<=0)
	{
		errorText=errorText + message_with_args("errors.required", messages["com.password"]) +"<br/><br/>";
	}

	if(errorText!="")
	{
		showErrorDlg(errorText);
		return false;
	}
	else
	{
		return true;
	}
}

function validateChangePasswordForm(form)
{
	var errorText ="";

	if($.trim(form.oldPassword.value).length<=0)
	{
		errorText=errorText + message_with_args("errors.required", messages["com.oldPassword"]) +"<br/><br/>";
	}

	if($.trim(form.newPassword.value).length<6)
	{
		$("#newPassword").val("");
		$("#reenterPassword").val("");
		errorText=errorText + message_with_args("errors.minLength", messages["com.newPassword"],6) +"<br/><br/>";
	}
	else if($.trim(form.newPassword.value) != $.trim(form.reenterPassword.value))
	{
		$("#newPassword").val("");
		$("#reenterPassword").val("");
		errorText=messages["errors.passwordsDoNotMatch"];
	}

	if(errorText!="")
	{
		showErrorDlg(errorText);
		return false;
	}
	else
	{
		return true;
	}
}

function validateRegisterForm(form)
{
	var errorText ="";
	
	if($.trim(form.username.value).length<6)
	{
		errorText=message_with_args("errors.minLength", messages["com.username"],6) +"<br/><br/>";
	}

	if(!isAlphaNumericSpace($.trim(form.username.value)))
	{
		errorText=message_with_args("errors.notAlphaNumeric", messages["com.username"]) +"<br/><br/>";
	}
	
	if($.trim(form.forenames.value).length<2)
	{
		errorText=errorText + message_with_args("errors.minLength", messages["com.forenames"],3) +"<br/><br/>";
	}

	if($.trim(form.surname.value).length<2)
	{
		errorText=errorText + message_with_args("errors.minLength", messages["com.surname"],3) +"<br/><br/>";
	}

	//Only check if exists
	if(form.password!=null)
	{
		if($.trim(form.password.value).length<6)
		{
			$("#password").val("");
			$("#reenterPassword").val("");
			errorText=errorText + message_with_args("errors.minLength", messages["com.password"],6) +"<br/><br/>";
		}
		else if($.trim(form.password.value) != $.trim(form.reenterPassword.value))
		{
			$("#password").val("");
			$("#reenterPassword").val("");
			errorText=messages["errors.passwordsDoNotMatch"] +"<br/><br/>";
		}
	}
	
	var email = $.trim(form.email.value);
	if(email=="")
	{
		errorText=errorText + message_with_args("errors.required", messages["com.email"]) +"<br/><br/>";
	}

	if(!isEmail(email))
	{
		errorText=errorText + message_with_args("errors.email", email) +"<br/><br/>";
	}

	if(form.con_id.value=="")
	{
		errorText=errorText + message_with_args("errors.required", messages["com.nationality"]) +"<br/><br/>";
	}

	//Only check if exists
	if($("#acceptTerms").length!=0 && $("#acceptTerms").attr("checked")!=true)
	{
		errorText=errorText + messages["errors.mustAcceptTerms"] +"<br/><br/>";
	}

	//Only check if exists
	if(form.update_comment!=null)
	{
		if($.trim(form.update_comment.value).length==0)
		{
			errorText=errorText + "Reason for updating account required" +"<br/><br/>";
		}
	}
	
	if(errorText!="")
	{
		$("#acceptTerms").attr("checked","");
		showErrorDlg(errorText);
		return false;
	}
	else
	{
		return true;
	}
}

function validateForgottenPasswordForm(form)
{
	var errorText ="";

	if($.trim(form.username.value).length<=0 && $.trim(form.email.value).length<=0)
	{
		errorText=messages["errors.requiredUsernameOrEmail"];
	}

	if(errorText!="")
	{
		showErrorDlg(errorText);
		return false;
	}
	else
	{
		return true;
	}
}

function changeUserIcon(ref,url)
{
	$("#currentIcon img").attr("src",url);
	$("#icon_ref").val(ref);
	return false;
}

function addDeleteUserRole(action,usr_id,role_name)
{
	if($('#addDeleteRoleComment').val()=="")
	{
		showErrorDlg("Enter a reason below");
	}
	else
	{
		var url="UserAdmin.do"
		url+="?usr_id="+usr_id;
		url+="&action="+action;
		url+="&role_name="+role_name;
		url+="&comment="+escape($("#addDeleteRoleComment").val());
		url+="#roles";
		showConfirmNavigate('Are you sure you want to amend this role?',url); return false;
	}
}


function blockDevice(usr_id,dev_id)
{
	if($('#actionComment').val()=="")
	{
		showErrorDlg("Enter a reason below");
	}
	else
	{
		var url="UserAdmin.do"
		url+="?usr_id="+usr_id;
		url+="&action=blockDevice";
		url+="&dev_id="+dev_id;
		url+="&comment="+escape($("#actionComment").val());
		url+="#roles";
		showConfirmNavigate('Are you sure you want to block this device?',url); return false;
	}
}

//Admin functionality to unassign a set of avatars
var avatarNo;
var unassignWld_id;
function unassignSelectedAvatars(wld_id)
{
		var avatars = $("#worldPanel table input:checkbox:checked");
		var count = $(avatars).size();
		avatarNo = 0; //initial values
		unassignWld_id = wld_id;
		showConfirmDlg("Are you sure you want to unassign "+count+" avatars?",doUnassignSelectedAvatars); return false;
		return false;
}

function doUnassignSelectedAvatars()
{
	var avatars = $("#worldPanel table input:checkbox:checked");
	var count = $(avatars).size();
	
	if(avatarNo>=count)
	{
		updateWaitDlg("Done - reloading page.");
		document.location = "ShowWorld.do?wld_id="+unassignWld_id;
		return false;
	}
	
	var ava_id = extractIndexUS($("#worldPanel table input:checkbox:checked").eq(avatarNo).attr("id"))
	if(avatarNo==0)
		showWaitDlg("");
	updateWaitDlg("Unassigning "+(avatarNo+1)+" of " + count +": ava_id="+ava_id);
	unassignAvatar(unassignWld_id,ava_id);
	avatarNo++;
}

function unassignAvatar(wld_id,ava_id)
{

	$.ajaxSetup({cache: false,error:callbackAjaxDlgError});
	//Invoke the AJAX call
	var url;
	url="UnassignAvatarAjax.do";
	var data = {wld_id:wld_id, ava_id: ava_id};
	$.post(url,data,doUnassignSelectedAvatars,"html");
	return false;
}



function createSubscription(usr_id, wld_id, ava_id)
{
	if($('#adjustSubscriptionComments').val()=="")
	{
		showErrorDlg("Enter a reason below");
	}
	else
	{
		var newExpiry=getDateFromPicker($("#newExpiry"));
		var newForceRenew=$("#newForceRenew").val();
		var url="UserAdmin.do"
		url+="?usr_id="+usr_id;
		url+="&action=createSubscription";
		url+="&wld_id="+wld_id;
		url+="&ava_id="+ava_id;
		url+="&expiry_date="+newExpiry.getTime();
		url+="&force_renew="+newForceRenew;
		url+="&comment="+escape($("#adjustSubscriptionComments").val());
		url+="#subscription";
		showConfirmNavigate("Are you sure?<br/>Subscription expiry: " + formatDate(newExpiry,"dd-MM-yyyy") + "<br/>Force renew: "+newForceRenew,url); 
	}
	return false;
}