/*
Function for New Account form
*/
var alert_text = "";
var curlang = "EN";
/* 简体中文版返回信息 */
var msgEmailInvalid_CN = "无效的邮件地址。";
var msgUserNull_CN = "请输入您的用户名。";
var msgUserStartSpace_CN = "用户名不能以空格开始。";
var msgUserEndSpace_CN = "用户名不能以空格结束。";
var msgUserShort_CN = "用户名太短，长度为 3～32 个字符。";
var msgUserLong_CN = "用户名太长，长度为 3～32 个字符。";
var msgUserInvalid_CN = "无效的用户名。";
var msgUserInvalid2_CN = "用户名包含不合法的字符。";
var msgUserCantUse_CN = "很遗憾，该用户名已被注册。";
var msgUserCanUse_CN = "恭喜，该用户名可以使用。";
var msgMandatoryNull_CN = "必填项不能为空。";
/* English return messages */
var msgEmailInvalid_EN = "Invalid email format.";
var msgUserNull_EN = "Please type your user name.";
var msgUserStartSpace_EN = "User name cannot start with space character.";
var msgUserEndSpace_EN = "User name cannot end with space character.";
var msgUserShort_EN = "User name must be at least 3 characters long.";
var msgUserLong_EN = "User name must not be longer than 32 characters.";
var msgUserInvalid_EN = "Invalid user name format.";
var msgUserInvalid2_EN = "Your user name contains invalid character(s).";
var msgUserCantUse_EN = "This user name is already in use by anohter person.";
var msgUserCanUse_EN = "This user name can be used.";
var msgMandatoryNull_EN = "Mandatory field cannot be null.";
function setcurlang() {
	if (/CN$/.test(location.hostname.toUpperCase())) {
		curlang = "CN";
	}
}
function preloadimages() {
	if (document.images) {
		    img1 = new Image();
		    img1.src = "icon_success.gif";
		    img2 = new Image();
		    img2.src = "icon_error.gif";
	}
}
function getretmsg(id) {
	return(eval(id + "_" + curlang));
}
function checkrequired(form){
var continue_flag = true;
clearalltext();
continue_flag &= checkFullName("FullName");
if (continue_flag) {
	checkUserAvailability();
}
continue_flag &= checkboxchk("Product");
continue_flag &= combochk("IsReselling");
continue_flag &= combochk("HowManyFaxUser");
continue_flag &= radiochk("IsFax");
continue_flag &= radiochk("IsDemo");
continue_flag &= textboxchk("FirstName");
continue_flag &= textboxchk("LastName");
continue_flag &= textboxchk("Company");
continue_flag &= textboxchk("City");
continue_flag &= combochk("Country");
continue_flag &= combochk("Region");
continue_flag &= textboxchk("Phone");
continue_flag &= checkEmail("Email");
continue_flag &= combochk("Language");
continue_flag &= whereheardchk("WhereHeard");
continue_flag &= combochk("Industry");
continue_flag &= WithExistingResellerchk("WithExistingReseller");
continue_flag &= radiochk("IsExistingCustomer");
continue_flag &= radiochk("IsLBP");
continue_flag &= combochk("ExpectedPurchaseDate");
continue_flag &= faxhardwarechk("Faxhardware");
if (continue_flag == false){
	alert(alert_text);
	return false;
}
form.action=form.action+"&chk=mmocartxe";
return true;
}
function checkboxchk(element)
{
var i = 0;
var checkobj = document.getElementsByName(element);
var checkobjlen = checkobj.length;
var checked = false;
for (i = 0; i < checkobjlen; i++)
{
	if (checkobj[i].checked == true)
	{
		return true;
	}
}
alerttext(element);
return false;
}
function combochk(element){
var comboobj = document.getElementsByName(element);
if (comboobj[0].selectedIndex == 0)
{
	alerttext(element);
	return false;
}
return true;
}
function radiochk(element){
var radioobj = document.getElementsByName(element);
var i = 0;
for (i = 0;i < radioobj.length; i++){
	if (radioobj[i].checked == true)
	{
		return true;
	}
}
alerttext(element);
return false;
}
function textboxchk(element){
if(document.getElementsByName(element)[0].value == "")
{
	alerttext(element);
	return false;
}
return true;
}
function checkEmail(Email) {
var content = document.getElementsByName(Email)[0].value;
	if (textboxchk(Email))
	{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(content)) {
			return(true);
		}else
		{
			if (alert_text == ""){
				alert_text = getretmsg("msgEmailInvalid");
			}
			alerttext(Email);
			return(false);
		}
	}
	alerttext(Email);
	return false;
}
function checkFullName(FullName) {
	var user = document.getElementsByName(FullName)[0].value;
	if (textboxchk(FullName) == false) {
		alert_text = "";
		alerttext(FullName);
		dispAvailabilityMsg(getretmsg("msgUserNull"), 0);
		return (false);
	}
	if (/^[ ]/.test(user)) {
		alert_text = getretmsg("msgUserStartSpace");
		alerttext(FullName);
		dispAvailabilityMsg(getretmsg("msgUserStartSpace"), 0);
		return (false);
	}
	if (/[ ]$/.test(user)) {
		alert_text = getretmsg("msgUserEndSpace");
		alerttext(FullName);
		dispAvailabilityMsg(getretmsg("msgUserEndSpace"), 0);
		return (false);
	}
	if (user.length < 3) {
		alert_text = getretmsg("msgUserShort");
		alerttext(FullName);
		dispAvailabilityMsg(getretmsg("msgUserShort"), 0);
		return (false);
	}
	if (user.length > 32) {
		alert_text = getretmsg("msgUserLong");
		alerttext(FullName);
		dispAvailabilityMsg(getretmsg("msgUserLong"), 0);
		return (false);
	}
	if (/^[0-9a-zA-Z\.\-\_\@ ]+$/.test(user) == false) {
		alert_text = getretmsg("msgUserInvalid");
		alerttext(FullName);
		dispAvailabilityMsg(getretmsg("msgUserInvalid2"), 0);
		return (false);
	}
	return (true);
}
function whereheardchk(element){
var sindex = document.getElementsByName(element)[0].selectedIndex;
var tindex = document.getElementsByName(element)[0].options.length;
sindex++;
if (combochk(element))
{
	if (sindex == tindex)
	{
		return textboxchk("OtherWhereHeard");
	}
	return true;
}
alerttext(element);
return false;
}
function WithExistingResellerchk(element){
	if (radiochk(element)){
		if (document.getElementsByName(element)[0].checked == true){
			return textboxchk("ExistingReseller");
		}
		return true;
	}
	alerttext(element);
	return false;
}
function faxhardwarechk(element){
if (document.getElementById("Extrafax").style.display == 'none')
{
	return true;
}else
{
	return combochk(element);
}
}
function alerttext(id){
	if ( id == "OtherWhereHeard" ){
		id = "WhereHeard";
	}
	if ( id == "ExistingReseller"){
		id = "WithExistingReseller";
	}
	id = id + "div";
	if (document.getElementById(id))
	{
		document.getElementById(id).style.color="#FF0000";
	}
	if (alert_text == ""){
		alert_text = getretmsg("msgMandatoryNull");
	}
}
function clearalltext(){
	alert_text = "";
	cleartext("Product");
	cleartext("IsReselling");
	cleartext("HowManyFaxUser");
	cleartext("IsFax");
	cleartext("IsDemo");
	cleartext("FullName");
	cleartext("FirstName");
	cleartext("LastName");
	cleartext("Company");
	cleartext("City");
	cleartext("Country");
	cleartext("Region");
	cleartext("Phone");
	cleartext("Email");
	cleartext("Language");
	cleartext("WhereHeard");
	cleartext("Industry");
	cleartext("WithExistingReseller");
	cleartext("IsExistingCustomer");
	cleartext("IsLBP");
	cleartext("ExpectedPurchaseDate");
	cleartext("Faxhardware");
}
function cleartext(id){
	id = id + "div";
	if (document.getElementById(id)){
		document.getElementById(id).style.color="";
	}
}
/* Check username availability by using AJAX */
function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}
function dispAvailabilityMsg(msg, isSuccess) {
	if (isSuccess == 1) {
		document.getElementById('FullNamediv').style.color="";
		document.getElementById('UserAvailabilitydiv').style.color = "#27A414";
		document.getElementById('UserAvailabilitydiv').innerHTML = '<img src="./icon_success.gif" border="0" style="vertical-align:middle"> ' + msg + '</img>';
	}
	else {
		document.getElementById('FullNamediv').style.color="#FF0000";
		document.getElementById('UserAvailabilitydiv').style.color = "#E80000";
		document.getElementById('UserAvailabilitydiv').innerHTML = '<img src="./icon_error.gif" border="0" style="vertical-align:middle"> ' + msg + '</img>';
	}
}
function refreshUserAvailability(xmlResp) {
	var user = document.getElementsByName("FullName")[0].value;
	var userExisted = xmlResp.getElementsByTagName("HasUser")[0].childNodes[0].nodeValue;
	if (userExisted == "1") {
		alert_text = getretmsg("msgUserCantUse");
		dispAvailabilityMsg(getretmsg("msgUserCantUse"), 0);
	}	
	else if (userExisted == "0") {
		dispAvailabilityMsg(getretmsg("msgUserCanUse"), 1);
	}
}
function checkUserAvailability() {
	this.handleStateChange = function() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				refreshUserAvailability(xmlHttp.responseXML);
			}
		}
	}
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = this.handleStateChange;
	xmlHttp.open("GET", "./CheckUserAvailability?OpenAgent&USERNAME=" +
		document.getElementsByName("FullName")[0].value, true);
	xmlHttp.send(null);
}
