function isEmpty(str)
{
	if (str != "")
		while (str.charAt(0) == " ")
			str = str.substr(1, str.length);
	return (str == "")? true : false;
}

function Trim(str)
{
	if (str != "")
		while (str.charAt(0) == " ")
			str = str.substr(1, str.length);
	return str;
}

function inputNumber(number)
{
	var pattern = "0123456789 -().";
	
	if (len != 0)
	{
		var index = 0;
		var len = number.value.length;
		
		while ((index < len) && (len != 0))
			if (pattern.indexOf(number.value.charAt(index)) == -1)
			{
				if (index == len-1)
					number.value = number.value.substring(0, len-1);
				else if (index == 0)
					 	number.value = number.value.substring(1, len);
					 else number.value = number.value.substring(0, index)+number.value.substring(index+1, len);
				index = 0;
				len = number.value.length;
			}
			else index++;
	}
}

function doCheck(check, state, n)
{
	if (n == 1)
		check.checked = state;
	else
	{
		for (var i = 0; i < n; i++)
			check[i].checked = state;
	}
}

function isDelete(frm, n)
{
	var flag = false;

	if (n == 1)
		flag = frm.checkbox.checked;
	else
	{	
		var i = 0;		
		
		while ((i < n) && (!flag))
		{
			if (frm.checkbox[i].checked)
				flag = true;
			else i++;	
		}
	}	
	if(!flag)
	{
		alert("Please select items that you want to delete!");
		return;
	}
	frm.submit();
}

function isEmail(email)
{
	if (email == "")
		return false;
	if (email.indexOf(" ") > 0)	
		return false;
	if (email.indexOf("@") == -1)	
		return false;
	if (email.indexOf(".") == -1)	
		return false;
	if (email.indexOf("..") != -1)	
		return false;
	if (email.indexOf("@") != email.lastIndexOf("@"))		
		return false;
		
	var len = email.length;
	
	if (email.lastIndexOf(".") == len-1)
		return false;
	
	var str = "0123456789abcdefghikjlmnopqrstuvwxyz-@._";
	
	for (var index = 0; index < len; index++)
		if (str.indexOf(email.charAt(index)) == -1)
			return false;
			
	var indexDot = 	email.indexOf(".");
	var indexAcsign = email.indexOf("@");
	
	if ((indexDot == indexAcsign-1) || (indexDot-1 == indexAcsign))
		return false;
					
	return true;
}

function chkConfirm(pwd, confirmpwd)
{
	if (pwd.value == "")
	{
		alert("Please choose a password.");
		pwd.focus();
		return false;
	}
	
	if (pwd.value.length < 6)
	{
		alert("Your new password must be at least 6 character.");
		pwd.focus();
		return false;
	}
	
	if ((confirmpwd.value == "") || (pwd.value != confirmpwd.value))
	{
		alert("Your password entries did not math.");
		confirmpwd.focus();
		return false;
	}	
	return true;
}

function chkcommonInfo(name, address, city, zip, country, phone, fax, email, isUser)
{
	if (isEmpty(name.value))
	{
		alert("Please enter your name!");
		name.focus();
		return false;
	}
	
	if (isEmpty(address.value))
	{
		alert("Please enter address!");
		address.focus();
		return false;
	}
	
	if (isEmpty(city.value))
	{
		alert("Please enter city!");
		city.focus();
		return false;
	}
	
	if (isEmpty(zip.value))
	{
		alert("Please enter zip/postal!");
		zip.focus();
		return false;
	}
	
	if (country.selectedIndex == 0)
	{
		alert("Please choose a country!");
		return false;
	}	
	
	if (isUser)
	{
		if (phone.value.length < 7)
		{
			alert("Invalid phone number, please try again");
			phone.focus();
			return false;
		}
		if (!isEmail(email.value))
		{
			alert("Invalid e-mail address, please try again");
			email.focus();
			return false;
		}		
	}
	else
	{
		if ((phone.value != "") && (phone.value.length < 7))
		{
			alert("Invalid phone number, please try again");
			phone.focus();
			return false;
		}
		if ((email.value != "") && (!isEmail(email.value)))
		{
			alert("Invalid e-mail address, please try again");
			email.focus();
			return false;
		}		
	}
	if ((fax.value != "") && (fax.value.length < 7))
	{
		alert("Invalid fax number, please try again");
		fax.focus();
		return false;
	}
	return true;
}

function chkCusInfo(frm, isChk, submitType)
{
	if (!chkcommonInfo(frm.customer, frm.address, frm.city, frm.zip, frm.country, frm.phone, frm.fax, frm.email, true))
	{
		if (submitType)
			return false;
		return;	
	}
	if (isChk)
	{
		if (submitType)
			return chkConfirm(frm.password, frm.confirmpassword);
		if (!chkConfirm(frm.password, frm.confirmpassword))
			return;
	}
	if (submitType)
		return true;
	frm.submit();	
}

function chkUserInfo(frm)
{
	if (!chkcommonInfo(frm.name, frm.address, frm.city, frm.zip, frm.country, frm.phone, frm.fax, frm.email, false))
	return;
	if (isEmpty(frm.username.value))
	{
		alert("Invalid user, please try again");
		frm.username.focus();
		return;
	}
	if (!chkConfirm(frm.password, frm.confirmpassword))
		return;
	frm.submit();	
}
