// JavaScript Document
var validColor='#EFEBDE';
var invalidColor='#FFCCCC';
function highlightMandatoryAcct(formid){
	var form=document.getElementById(formid);
	if(form.txtEmail!=null && isEmpty(form.txtEmail.value))
	{
		form.txtEmail.style.background=invalidColor;
	}
	if(form.txtPassWord1!=null && isEmpty(form.txtPassWord1.value))
	{		
		form.txtPassWord1.style.background=invalidColor;		
	}	
	if(form.txtPassWord2!=null && isEmpty(form.txtPassWord2.value))
	{
		form.txtPassWord2.style.background=invalidColor;		
	}
	if(form.file1!=null&&isEmpty(form.file1.value))
	{
		form.file1.style.background=invalidColor;		
	}
	if(form.txtFirstName!=null&&isEmpty(form.txtFirstName.value))
	{
		form.txtFirstName.style.background=invalidColor;		
	}
	alert('Please Enter All Required Fields!');
}

function highlightInValidAcct(formid)
{
	var form=document.getElementById(formid);
	if(form.txtHint!=null)
		form.txtHint.style.background=validColor;
	if(form.selGender!=null)
		form.selGender.style.background=validColor;
	if(form.txtLastName!=null)
		form.txtLastName.style.background=validColor;
	if(form.txtHomePhone!=null)
		form.txtHomePhone.style.background=validColor;
	if(form.txtWorkPhone!=null)
		form.txtWorkPhone.style.background=validColor;
	if(form.txtMobilePhone!=null)
		form.txtMobilePhone.style.background=validColor;
	if(form.txtStreet!=null)
		form.txtStreet.style.background=validColor;
	if(form.txtSuburb!=null)
		form.txtSuburb.style.background=validColor;
	if(form.selState!=null)
		form.selState.style.background=validColor;
	if(form.txtState!=null)
		form.txtState.style.background=validColor;
	if(form.txtPostcode!=null)
		form.txtPostcode.style.background=validColor;
	if(form.selCountry!=null)
		form.selCountry.style.background=validColor;
	if(form.chkNotify!=null)
		form.chkNotify.style.background=validColor;
}

function chkAddCust(formid)
{
	var form=document.getElementById(formid);
	if(form.txtEmail!=null)
	{
		if(isEmpty(form.txtEmail.value))
		{
			form.txtEmail.focus();
			highlightMandatoryAcct(formid);		
			return false;
		}
		else
		{
			form.txtEmail.style.background=validColor;
		}
		if(!isCorrectEmail(form.txtEmail.value))
		{
			alert('The email address is invalid!');
			form.txtEmail.focus();
			form.txtEmail.style.background=invalidColor;
			return false;
		}
		else
		{
			form.txtEmail.style.background=validColor;
		}
	}
	if(form.txtPassWord1!=null)
	{
		if(isEmpty(form.txtPassWord1.value))
		{
			form.txtPassWord1.focus();
			highlightMandatoryAcct(formid);	
			return false;
		}else
		{
			form.txtPassWord1.style.background=validColor;
		}
	}
	if(form.txtPassWord2!=null)
	{
		if(isEmpty(form.txtPassWord2.value))
		{
			form.txtPassWord2.focus();
			highlightMandatoryAcct(formid);	
			return false;
		}else
		{
			form.txtPassWord2.style.background=validColor;
		}
		if(chkHaveGaps(form.txtPassWord1))
		{
			return false;
		}
	}
	if(form.txtPassWord1!=null&& form.txtPassWord2!=null)
	{
		if(!chkLen(form.txtPassWord1,6,12,'Password must be 6-12 characters'))
		{
			return false;
		}
		if(!isEqual(form.txtPassWord1.value,form.txtPassWord2.value))
		{
			alert('The password and confirm password you entered are different!');
			form.txtPassWord2.focus();
			form.txtPassWord2.style.background=invalidColor;
			return false;
		}
		else
		{
			form.txtPassWord2.style.background=validColor;
		}
	}
	
	if(form.file1!=null && isEmpty(form.file1.value))
	{
		alert('Please choose a photo to upload!');
		form.file1.style.background=invalidColor;
		form.file1.focus();
		return false;
	}
	else
	{
		form.file1.style.background=validColor;
	}
}

function chkAddCust1(formid)
{
	var form=document.getElementById(formid);
	highlightInValidAcct(formid);
	if(isEmpty(form.txtEmail.value))
	{
		form.txtEmail.focus();
		highlightMandatoryAcct(formid);
		return false;
	}
	else
	{
		form.txtEmail.style.background=validColor;
	}
	if(!isCorrectEmail(form.txtEmail.value))
	{
		alert('The email address is not valid!');	
		form.txtEmail.style.background=invalidColor;	
		form.txtEmail.focus();
		return false;
	}
	else
	{
		form.txtEmail.style.background=validColor;
	}
	if(chkHaveGaps(form.txtPassWord1))
	{
		return false;
	}
	
	if(form.txtPassWord1.disabled=='')
	{
		if(isEmpty(form.txtPassWord1.value))
		{	
			form.txtPassWord1.focus();
			highlightMandatoryAcct(formid);
			return false;
		}
		else
		{
			form.txtPassWord1.style.background=validColor;
		}
		if(!isEqual(form.txtPassWord1.value,form.txtPassWord2.value))
		{
			alert('The password and confirm password you entered are different!');
			form.txtPassWord2.focus();
			form.txtPassWord2.style.background=invalidColor;
			return false;
		}
		else
		{
			form.txtPassWord2.style.background=validColor;
		}
		if(!chkLen(form.txtPassWord1,6,12,'Password must be 6-12 characters'))
		{
			return false;
		}
	}
	
	if(isEmpty(form.txtFirstName.value))
	{
		form.txtFirstName.focus();
		form.txtFirstName.style.background=invalidColor;
		highlightMandatoryAcct(formid);
		return false;
	}
	else
	{
		form.txtFirstName.style.background=validColor;
	}
}
//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;
}
//set a field unable
function setUnableField(formid)
{
	var form=document.getElementById(formid);
	/*if(form.txtEmail!=null)
	{
		form.txtEmail.disabled='disabled';
	}*/
	if(form.txtPassWord1!=null)
	{
		form.txtPassWord1.disabled='disabled';
		//alert(form.txtPassWord1.readonly);
	}
	if(form.txtPassWord2!=null)
	{
		form.txtPassWord2.disabled='disabled';
	}
}
//set input value
function setInputValue(inputId,label)
{
	var input=document.getElementById(inputId);
	if(input!=null)
		input.value=label;
}
//set div value
function setDivValue(divId,divValue)
{
	var div=document.getElementById(divId);
	div.innerText=divValue;	
}

function setTDValue(tdId,tdValue)
{
	var td=document.getElementById(tdId);	
	td.innerHTML=tdValue;
}
//set form action
function setAction(formid,url)
{
	var form=document.getElementById(formid);
	form.action=url;
}

//modify password
function modifyPassword()
{
	location.href='login.pge?action=modifyPassword';
	return;
}