var Logon_System = function(name){
	this.name = name;
	this.Menu_Array = new Array();
	this.Menu_Edit_mode = Menu_Edit_mode;
}

Logon_System.prototype = Tool_Element.prototype;
Logon_System.prototype = Elements.prototype;  
Logon_System.prototype = Tool_Form_Functions.prototype;  

Logon_System.prototype.BlivOprettet = function(Obj)
{
  Obj.deleteElements(Obj.Logon_Container);
	Obj.InsertTextLine(Obj.Logon_Container,TXT.Text_0157 + " " + SERVER_ADDR,'Title');
	Obj.Br(Obj.Logon_Container);

	Obj.InsertTextLine(Obj.Logon_Container,TXT.Text_0158 ,'Body');
	Obj.Br(Obj.Logon_Container);

	Obj.NameLine = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0159+": ", 'Admin_FunctionFrame_Line',"text",'tab_10')
	Obj.UsernameLine = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0160+": ", 'Admin_FunctionFrame_Line',"text",'tab_10')
	Obj.EmailLine = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0161+": ", 'Admin_FunctionFrame_Line',"text",'tab_10')
	Obj.PasswordLine1 = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0162+": ", 'Admin_FunctionFrame_Line',"password",'tab_10')
	Obj.PasswordLine2 = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0163+": ", 'Admin_FunctionFrame_Line',"password",'tab_10')
	Obj.Br(Obj.Logon_Container);
	Obj.InsertActionButton(Obj.Logon_Container,TXT.Text_0164,"Admin_Function_Save",Obj.BlivOprettetScript,Obj.name);
	Obj.Br(Obj.Logon_Container);
}


Logon_System.prototype.BlivOprettetScript = function(Obj)
{
	if (Obj.NameLine.InputField.value.length<4) {
		Obj.NameLine.InputField.style.backgroundColor ="red";
		return false;
	}
	else Obj.NameLine.InputField.style.backgroundColor ="";
	
	if (Obj.UsernameLine.InputField.value.length<3) {
		Obj.UsernameLine.InputField.style.backgroundColor ="red";
		return false;
	}
	else Obj.UsernameLine.InputField.style.backgroundColor ="";
	
	var mailFejl = Check_Mail_Address(Obj.EmailLine.InputField.value)
	if (mailFejl<0) {
		if (mailFejl==-1)	alert("@ "+TXT.Text_0092);
		if (mailFejl==-2)	alert("@ "+TXT.Text_0093);
		if (mailFejl==-3)	alert(". "+TXT.Text_0092);
		if (mailFejl==-4)	alert(". "+TXT.Text_0093);
		if (mailFejl==-5)	alert(TXT.Text_0094);
		Obj.EmailLine.InputField.style.backgroundColor ="red";
		return false;
	}
	else Obj.EmailLine.InputField.style.backgroundColor ="";
	
	if (Obj.PasswordLine1.InputField.value.length<4 || Obj.PasswordLine1.InputField.value.length>50) {
		Obj.PasswordLine1.InputField.style.backgroundColor ="red";
		return false;
	}
	else Obj.PasswordLine1.InputField.style.backgroundColor ="";
	
	if (Obj.PasswordLine2.InputField.value.length!=Obj.PasswordLine1.InputField.value.length) {
		Obj.PasswordLine2.InputField.style.backgroundColor ="red";
		return false;
	}
	else Obj.PasswordLine2.InputField.style.backgroundColor ="";
	
	CON.Server_Connector(SERVER_ADDR + "/SHARE/connect/logon.php","class.login.php","login","Create_User",Obj.name,Obj.name+".CallBackBlivOprettet","&crname="+Obj.NameLine.InputField.value+"&crusername="+Obj.UsernameLine.InputField.value+"&cremail="+Obj.EmailLine.InputField.value+"&crpassword="+Obj.PasswordLine1.InputField.value)
}

Logon_System.prototype.CallBackBlivOprettet = function(Obj)
{
}

Logon_System.prototype.Log_Off_System = function()
{
 	CON.Sql_Logon(this.name+".PasswordArray","","logon_sql",this.name+".PasswordArray",this.name+".CheckLogon("+this.name+")",USERNAME,"","LOGOFF");
}

Logon_System.prototype.CheckLogon = function(Obj)
{
  var returnValue = false;
  if (Obj.UsernameLine.InputField.value.length < 2) {
	  Obj.UsernameLine.style.backgroundColor ="red";
	  returnValue = true;
	}
	else 	  Obj.UsernameLine.style.backgroundColor ="";
	
  if (Obj.PasswordLine.InputField.value == "") {
	  Obj.PasswordLine.style.backgroundColor ="red";
	  returnValue = true;
	}
	else 	  Obj.PasswordLine.style.backgroundColor ="";
	if (returnValue) 
	{
	 	return false; 
	}
	else
	{
		if (Obj.ConnectDone)
  	{
			if (Obj.PasswordArray.result)
			{
			  location.href = SERVER_SELF;
			}
			else
			{
			  if (Obj.PasswordArray.UserName) {
				 	alert(TXT.Text_0112); 
	  			Obj.PasswordLine.style.backgroundColor ="red";
				}
				else
				{
				 alert(TXT.Text_0111);
				 Obj.UsernameLine.style.backgroundColor ="red";
				}
			}
			Obj.ConnectDone=false;
		}
		else
		{
		 	Obj.ConnectDone=true;
		 	CON.Sql_Logon(Obj.name+".PasswordArray","","logon_sql",Obj.name+".PasswordArray",Obj.name+".CheckLogon("+Obj.name+")",Obj.UsernameLine.InputField.value,hex_md5(Obj.PasswordLine.InputField.value),Obj.SaveInfoLine.InputField.checked);
		}
	}
}

Logon_System.prototype.SendPassword = function(Obj)
{
	var mailFejl = Check_Mail_Address(Obj.EmailLine.InputField.value)
	switch (mailFejl)
	{
		case -1: 
			alert("@ "+TXT.Text_0092);
		break;
		case -2: 
			alert("@ "+TXT.Text_0093);
		break;
		case -3: 
			alert(". "+TXT.Text_0092);
		break;
		case -4: 
			alert(". "+TXT.Text_0093);
		break;
		case -5: 
			alert(TXT.Text_0094);
		break;
	}
		
  if (mailFejl===true) {
    Obj.EmailLine.InputField.style.backgroundColor ="";
	  var returnValue = false;
	}
	else
	{
	  Obj.EmailLine.InputField.style.backgroundColor ="red";
	  var returnValue = true;
	}
	if (returnValue) return false;
	else{
		CON.Server_Connector(SERVER_ADDR + "/SHARE/connect/logon.php","class.login.php","login","Send_Password","MAIL",this.name+"Change_Password","&email="+Obj.EmailLine.InputField.value)
	}
}

Logon_System.prototype.LogonForm = function(Obj)
{
  this.ConnectDone = false;
  this.deleteElements(Obj);
  this.ConnectDone = false;
  this.Logon_Container = this.Form(Obj,"class='LogonContainer'");
	this.InsertTextLine(this.Logon_Container,TXT.Text_0181,'Body');
	this.InsertTextLine(this.Logon_Container,"<a href='JavaScript: "+this.name+".BlivOprettet("+this.name+")'>" + TXT.Text_0156 + "</a>",'Body');
	this.Hr(this.Logon_Container);
	this.InsertTextLine(this.Logon_Container,TXT.Text_0106 + " " + SERVER_ADDR,'Title');

	this.Br(this.Logon_Container);

	this.UsernameLine = this.InsertInput(this.Logon_Container,"", TXT.Text_0087+": ", 'Admin_FunctionFrame_Line',"text",'tab_6')
	this.PasswordLine = this.InsertInput(this.Logon_Container,"", TXT.Text_0100+": ", 'Admin_FunctionFrame_Line',"password",'tab_6')
	this.SaveInfoLine = this.InsertInput(this.Logon_Container,"", TXT.Text_0108+": ", 'Admin_FunctionFrame_Line',"checkbox",'tab_6')
	this.Br(this.Logon_Container);
	this.InsertActionButton(this.Logon_Container,TXT.Text_0106,"Admin_Function_Save",this.CheckLogon);
	this.Br(this.Logon_Container);
	this.Hr(this.Logon_Container);
	this.Br(this.Logon_Container);
	this.InsertTextLine(this.Logon_Container,TXT.Text_0107,'Admin_Menu_Overskrift');
	this.EmailLine = this.InsertInput(this.Logon_Container,"", TXT.Text_0109+": ", 'email',"text",'tab_6')
	this.Br(this.Logon_Container);
	this.InsertActionButton(this.Logon_Container,TXT.Text_0110,"Admin_Function_Save",this.SendPassword);
}

Logon_System.prototype.UserInfo = function(Obj)
{
  this.ConnectDone = false;
  this.deleteElements(Obj);
  this.ConnectDone = false;
  this.Logon_Container = this.Form(Obj,"class='LogonContainer'");

	this.InsertActionButton(this.Logon_Container,TXT.Text_0178,"Admin_Function_Save",Login.Log_Off_System);
	this.Br(this.Logon_Container);
	this.InsertTextLine(this.Logon_Container,TXT.Text_0173,'Title');
	
	CON.Server_Connector(SERVER_ADDR + "/SHARE/connect/logon.php","class.login.php","login","Get_User_Info",this.name,this.name+".UserInfoRetur")
}

Logon_System.prototype.UserInfoRetur = function(Obj)
{
	this.InsertTextLine(this.Logon_Container,TXT.Text_0087 + "=" + Obj.UserArray.username,'Body');
	this.InsertTextLine(this.Logon_Container,TXT.Text_0060 + "=" + Obj.UserArray.navn,'Body');
	this.InsertTextLine(this.Logon_Container,TXT.Text_0088 + "=" + Obj.UserArray.email,'Body');
	this.Br(this.Logon_Container);
	this.InsertTextLine(this.Logon_Container,TXT.Text_0174,'Body');
	
	var ShowAdminLink=false;
	
	if (Obj.UserArray.Roller.length == 0)
	{
		this.InsertTextLine(this.Logon_Container,TXT.Text_0175,'Body');
	}
	else
	{
		for (i=0; i<Obj.UserArray.Roller.length; i++)
		{
			this.InsertTextLine(this.Logon_Container,Obj.UserArray.Roller[i].rolle + " (" + Obj.UserArray.Roller[i].beskrivelse + ")",'Body');
			if (Obj.UserArray.Roller[i].rolle!="Medlem" || Obj.UserArray.Roller[i].rolle!="Everyone")	ShowAdminLink = true;		
		}
	}
	if (ShowAdminLink) {
		this.Br(this.Logon_Container);
		this.InsertTextLine(this.Logon_Container,TXT.Text_0177,'Body');	
	}
	
	this.Br(this.Logon_Container);
	this.InsertTextLine(this.Logon_Container,TXT.Text_0176,'Body');

	this.Br(this.Logon_Container);
	this.InsertActionButton(this.Logon_Container,TXT.Text_0105,"Admin_Function_Save",Login.Change_Password,Obj);
}

Logon_System.prototype.Change_Password = function(Obj)
{
  Obj.deleteElements(Obj.Logon_Container);
  PassWordOk=false;
	Obj.InsertTextLine(Obj.Logon_Container,TXT.Text_0105,'MainHeadline');
	Obj.Br(Obj.Logon_Container);
	Obj.PasswordLineold = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0138+": ", 'Admin_FunctionFrame_Line',"password",'tab_8')
	Obj.PasswordLine1 = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0115+": ", 'Admin_FunctionFrame_Line',"password",'tab_8')
	Obj.PasswordLine2 = Obj.InsertInput(Obj.Logon_Container,"", TXT.Text_0116+": ", 'Admin_FunctionFrame_Line',"password",'tab_8')
	Obj.InsertActionButton(Obj.Logon_Container,TXT.Text_0073,"Admin_Function_Save",Obj.Save_Change_Password,Obj);
	Obj.Br(Obj.Logon_Container);

}


Logon_System.prototype.Save_Change_Password = function(Obj)
{
  var PW = Obj.PasswordLine1.InputField.value;
  var PWold = hex_md5(Obj.PasswordLineold.InputField.value);
  var PW1 = hex_md5(PW);
  var PW2 = hex_md5(Obj.PasswordLine2.InputField.value);
  var PWch = Check_Password(""+Obj.PasswordLine1.InputField.value);
	
	if (PassWordOk==false)
	{
	  PassWordOk = true;
 		this.Sql = CMS_NAME+"bruger;password;username='%s'";
	  CON.Sql_Connect(this,this.Sql,"select_sql",Obj.name+".UserArray",Obj.name+".Save_Change_Password("+Obj.name+")",USERNAME);
	  return;
	}
	
	if (Obj.UserArray[0].password!=PWold)  {
	  alert(TXT.Text_0139);
	  return false
	}

	if (PW1!=PW2)  {
	  alert(TXT.Text_0120);
	  return false
	}
	if (PW1==PWold)  {
	  alert(TXT.Text_0117);
	  return false
	}
	
	if (PWch==-1)  {
	  alert(TXT.Text_0118);
	  return false
	}

	if (PWch==-2)  {
	  alert(TXT.Text_0119);
	  return false
	}
  	this.Sql = CMS_NAME+"bruger;password='%s',pw='%s';id=%d";
	  CON.Sql_Connect(Obj,this.Sql,"update_sql","Save_User_Change_Return_Value",Obj.name+".Save_User_Change_Return("+Obj.name+")",PW1,PW,USER_ID);
	
}

Logon_System.prototype.Save_User_Change_Return = function(Obj)
{
  if (Save_User_Change_Return_Value!="UPDATED") {
	  alert(TXT.Text_0141);
	  return false
	}
  alert(TXT.Text_0140)
	MemberInfoScript()
}



