// JavaScript Document
var validColor='#EFEBDE';
var invalidColor='#FFCCCC';
function highlightMandatoryAcct(formid){
	var form=document.getElementById(formid);
	if(isEmpty(form.txtPassWord.value))
	{
		form.txtPassWord.style.background=invalidColor;
	}
	if(isEmpty(form.txtNewPassword.value))
	{
		form.txtNewPassword.style.background=invalidColor;
	}
	if(isEmpty(form.txtConfirmPassword.value))
	{
		form.txtConfirmPassword.style.background=invalidColor;
	}
	alert('Please enter all required fields!');
}
//login
function signin(formId)
{
	
	var form =document.getElementById(formId);
	if(isEmpty(form.txtEmail.value))
	{
		alert('Please enter your Email!');
		form.txtEmail.focus();
		return false;
	}
	if(isEmpty(form.txtPassWord.value))
	{
		alert('Please enter your password!');
		form.txtPassWord.focus();
		return false;
	}
	if(isEmpty(form.txtValidateCode.value))
	{
		alert('Please enter your ValidateCode!');
		form.txtValidateCode.focus();
		return false;
	}
	if(isNaN(form.txtValidateCode.value))
	{
		alert('The Validate Code is not correct. Please enter the number beside the validation code box!');
		form.txtValidateCode.focus();
		return false;
	}
	form.action='loginAction.pge?action=signin';
	return true;
}

//logout
function register()
{
	var strurl=document.frmLogin.strurl.value
	window.location.href='accountDetail.pge?action=register&strurl='+strurl;
	return;
}

//getPassword
function getPassword()
{
	location.href='login.pge?action=getPassWord';
	return;
}

//confirm modify Password
function confirmPassword(formId)
{
	var form =document.getElementById(formId);
	if(isEmpty(form.txtPassWord.value))
	{
		highlightMandatoryAcct(formId);
		form.txtPassWord.focus();
		return false;
	}
	else
	{
		form.txtPassWord.style.background=validColor;
	}
	if(isEmpty(form.txtNewPassword.value))
	{
		highlightMandatoryAcct(formId);
		form.txtNewPassword.focus();
		return false;
	}
	else
	{
		form.txtNewPassword.style.background=validColor;
	}
	
	if(chkHaveGaps(form.txtNewPassword))
	{
		return false;
	}
	if(!chkLen(form.txtNewPassword,6,12,'Password must be 6-12 characters'))
	{
		return false;
	}
	
	if(isEmpty(form.txtConfirmPassword.value))
	{
		highlightMandatoryAcct(formId);
		form.txtConfirmPassword.focus();
		return false;
	}
	else
	{
		form.txtConfirmPassword.style.background=validColor;
	}
	
	if(!isEqual(form.txtNewPassword.value,form.txtConfirmPassword.value))
	{
		alert('The password and confirm password you entered are different!');
		form.txtConfirmPassword.focus();
		return false;
	}
	
	form.action='loginAction.pge?action=modifyPassword';
	return true;
}

//check field length
function chkLen(field,minLen,MaxLen,msg)
{
	if(field.value.length<minLen||field.value.length>MaxLen)
	{
		alert(msg);
		field.focus();
		field.style.background=invalidColor;
		//highlightMandatoryAcct(formid);
		return false;
	}
	return true;
}
//check have any gaps
function chkHaveGaps(field)
{
	if (field.value.lastIndexOf(" ") > -1)
	{
		alert("Password must not have any gaps");
		field.focus();
		field.style.background=invalidColor;
		//highlightMandatoryAcct(formid);
		return true;
	}
	return false;
}

function hightlight(formId)
{
	var form =document.getElementById(formId);
	form.txtEmail.style.background=invalidColor;
	form.txtHint.style.background=invalidColor;
}
//getNewPassword
function getNewPassword(formId)
{
	var form =document.getElementById(formId);
	if(isEmpty(form.txtEmail.value))
	{
		hightlight(formId);
		alert('Please enter email!');
		form.txtEmail.focus();
		return false;
	}
	else
	{
		form.txtEmail.style.background=validColor;
	}
	
/*	if(isEmpty(form.txtHint.value))
	{
		hightlight(formId);
		alert('Please enter password hint!');
		form.txtHint.focus();
		return false;
	}
	else
	{
		form.txtHint.style.background=validColor;
	}*/
	form.action='loginAction.pge?action=getNewPassword';
	form.submit();
}

//exit
function exit(url)
{
	location.href=url;
	return;
}