var theForm = null

function cbCancelNewWriter()
{
	window.location.href = "../index.php"
}

function cbSubmitNewWriter()
{
	dbgWrite('newwriter');
	
	if ( Verify() )
	{
		theForm.action = "newsign.php"
		theForm.submit()
	}
}

function initNewWriter()
{
	initUserForm()	
	dbgWrite('newsign','done');
	dbgPrint();
	
	setTimeout('Preload()',1)
	
	return true
}

function initUserForm( user )
{
	theForm = document.forms["SignForm"]
	if ( user )
	{
		with( user )
		{
			theForm.firstname.value = FirstName
			theForm.lastname.value = LastName
			theForm.email.value = Email
			theForm.answer.value = Answer

			with( theForm.question )
			{
				for ( var i=0; i<options.length; i++ )
				{
					if ( options[i].value == QuestionID )
						selectedIndex = i
				}
			}
		}
	}
}

function cbCancelChangeWriter()
{
	window.close()
}

function cbSubmitChangeWriter()
{
	// Form entry validation
	if( !VerifyFirstName() ) return false;
	if( !VerifyLastName() ) return false;
	if( !VerifyEmail() ) return false;
	if( !VerifyPassword(true) ) return false;
	if( !VerifyPasswordCopy() ) return false;
	if( !VerifyAnswer() ) return false;

	theForm.action = "submitchangewriter.php";
	theForm.submit();
	return true;
}

function cbSubmitDeleteWriter()
{
	if ( confirm( GetLocalized("ConfirmDeleteWriter") ) )
	{
		window.location.href = "submitdeletewriter.php"
	}
}

function cbCancelDeleteWriter()
{
	window.history.go(-1)
}

function Verify()
{
	if( !VerifyFirstName() ) return false;
	if( !VerifyLastName() ) return false;
	if( !VerifyEmail() ) return false;
	if( !VerifyLogin() ) return false;
	if( !VerifyPassword() ) return false;
	if( !VerifyPasswordCopy() ) return false;
	if( !VerifyQuestion() ) return false;
	if( !VerifyAnswer() ) return false;
	if( !VerifyAgreement() ) return false;
	if( !CheckCookie( "../admin/troublecookie.php" ) ) return false;
	return true;
}

function VerifyFirstName()
{
	if ( theForm.firstname.value == "" )
	{
		alert( GetLocalized("alert_firstname") )
		theForm.firstname.focus()
		return false;
	}
	
	return true;
}

function VerifyLastName()
{
	if ( theForm.lastname.value == "" )
	{
		alert( GetLocalized("alert_lastname") )
		theForm.lastname.focus()
		return false;
	}
	
	return true;
}

function VerifyEmail()
{
	var _m = theForm.email.value
	if ( _m == "" )
	{
		alert( GetLocalized("alert_email") )
		theForm.email.focus()
		return false;
	}
	_m = _m.replace( /^\s{1,}/, '' )
	_m = _m.replace( /\s{1,}$/, '' )
	if ( _m.match( /\s/g ) || ! _m.match( /\S+@\S+\.\S+/ ) )
	{
		alert( GetLocalized("alert_validemail") )
		theForm.email.focus()
		return false;
	}
	else
		theForm.email.value = _m
	return true;
}

function VerifyLogin()
{
	var login = theForm.login.value
	if ( login == "" )
	{
		alert( GetLocalized("alert_login") )
		theForm.login.focus()
		return false;
	}
	if ( login.length<login.length > 32 )
	{
		alert( GetLocalized("alert_loginsize") )
		theForm.login.focus()
		return false;
	}
	if ( login.match( /^keeboo/ ) || login.match( /^\-/ ) || login.match( /^\_/ ) )
	{
		alert( GetLocalized("alert_loginforbidden") )
		theForm.login.focus()
		return false;
	}
	if ( login.match( /[^a-zA-Z0-9\-\_]/ ) || !(login.toLowerCase()==login) )
	{
		alert( GetLocalized("alert_logincarforbidden") )
		theForm.login.focus()
		return false;
	}
	return true;
}

function VerifyTruePassword()
{
	var password = theForm.password
	if ( password.value.length < 5 )
	{
		alert( GetLocalized("alert_password") )
		password.focus()
		return false;
	}
	return true;
}

function VerifyPassword(empty)
{
	var password = eval( 'theForm.password' + dt )
	if ( empty && password.value == "" )
		return true
	if ( password.value.length < 5 )
	{
		alert( GetLocalized("alert_password") )
		password.focus()
		return false;
	}
	return true;
}

function VerifyPasswordCopy()
{
	var password = eval( 'theForm.password'+dt )
	var passwordcopy = eval( 'theForm.passwordcopy' + dt )
	
	if ( passwordcopy.value != password.value )
	{
		alert( GetLocalized("alert_passwordcopy") )
		passwordcopy.focus()
		theForm.password.value = ""
		return false;
	}
	theForm.password.value = password.value
	return true;
}

function VerifyQuestion()
{
	if ( theForm.question.value == "" )
	{
		alert( GetLocalized("alert_question") )
		theForm.question.focus()
		return false;
	}
	return true;
}

function VerifyAnswer()
{
	if ( theForm.answer.value == "" )
	{
		alert( GetLocalized("alert_answer") )
		theForm.answer.focus()
		return false;
	}
	return true;
}

function VerifyAgreement()
{
	if ( ! theForm.agreement[0].checked )
	{
		alert( GetLocalized("alert_agreement") )
		return false;
	}
	
	return true;
}

function cbTroubleSignCancel()
{
	window.history.go(-1)
}

function cbTroubleSignSubmit()
{
	theForm = document.forms["Identification"]

	// Form entry validation
	if( !VerifyLogin() ) return false;
	if( !VerifyQuestion() ) return false;
	if( !VerifyAnswer() ) return false;
	
	theForm.submit()
	
	return true;
}

function cbSignUp()
{
	document.location.href = "signing/newwriter.php"
	return true;
}

function cbSignIn()
{
	if( !VerifyLogin() ) return false;
	if( !VerifyTruePassword() ) return false;
	if( !CheckCookie( "../admin/troublecookie.php" ) ) 
		return false;

	dbgWrite('signin');
	theForm.submit()
	return true;
}

function cbSignBackIn()
{
	// Form entry validation
	if( !VerifyLogin() ) return false;
	if( !VerifyTruePassword() ) return false;
	
	theForm.submit()
	return true;
}

function cbSignOutClose()
{
	top.close()
}

EnterKeyCode = 13
function KeyUpHdl( e )
{
	if( (_ns&&e.which == EnterKeyCode) || (_ie && event.keyCode == EnterKeyCode) )
	{
		cbSign()
	}	
	return true
}

function GetDate()
{
	var today, day, number, month, year
	today = new Date();
	day = today.getDay();
	number = today.getDate();
	if (number<10)
		number = "0"+number;
	month = today.getMonth();
	if (_ns)
		year = today.getFullYear();
	else
		year = today.getYear();
	var days = GetLocalized("DateDays")
	var months = GetLocalized("DateMonths")
	var format = GetLocalized("DateFormat")
	format = format.replace( /__Day__/, convStr(days[day]) )
	format = format.replace( /__Number__/, number )
	format = format.replace( /__Month__/, convStr(months[month]) )
	format = format.replace( /__Year__/, year)
	return format
}
	
var cbSign = null
function InitSignIn(writer)
{
	var d = new Date()
	var y = new String(d.getYear())
	var s = d.toLocaleString()
	var str = '<table cellpadding="0" cellspacing="0" border="0"><tr><td class="DateText">'
	str += GetDate()
	str += '</td></tr></table>'
	updateLayer( str, "Date" )
	
	theForm = layerForm("SignIn","LoginTable")
	if( writer==null ) 
		theForm.login.focus()
	
	cbSign = cbSignIn
	
	if(_ns4)
	{
		document.captureEvents(Event.KEYPRESS);
		document.onkeypress = KeyUpHdl;
	}
	else
		document.onkeyup = KeyUpHdl;
		
	dbgWrite('homepage','done');
	dbgPrint();
	
	return false;
}

function InitSignOut()
{
	//theForm = layerForm("SignIn","LoginTable")
	theForm = document.forms["SignIn"]
	theForm.login.focus()
	
	cbSign = cbSignBackIn
	
	if(_ns4)
	{
		document.captureEvents(Event.KEYPRESS);
		document.onkeypress = KeyUpHdl;
	}
	else
		document.onkeyup = KeyUpHdl;
	return false;
}
