// valid ADSL
function CheckADSL(hrefADSL,AdslStatus) {  
  if(AdslStatus == null || AdslStatus == '-1')
		{
			inlineMsg(hrefADSL,'Bạn chưa đăng ký dịch vụ ADSL!',2);
		
		return false;
		}    
  if(AdslStatus == '0')
  {
	  inlineMsg(hrefADSL,'Tài khoản ADSL của bạn chưa được kích hoạt!',2);
		return false;		 
  }
  return true;
}
// valid ADSL
function CheckMobile(hrefMobile,MobileStatus) {  	  
  if(MobileStatus == null || MobileStatus == '-1')
		{
			inlineMsg(hrefMobile,'Bạn chưa đăng ký dịch vụ Mobile!',2);
		
		return false;
		}    
  if(MobileStatus == '0')
  {
	  inlineMsg(hrefMobile,'Tài khoản mobile của bạn chưa được kích hoạt!',2);
		return false;		 
  }
  
}
function CheckBillMobile(hrefMobile,MobileStatus,MobileType) {  	  
  if(MobileStatus == null || MobileStatus == '-1')
		{
			inlineMsg(hrefMobile,'Bạn chưa đăng ký dịch vụ Mobile!',2);
		
		return false;
		}    
  if(MobileStatus == '0')
  {
	  inlineMsg(hrefMobile,'Tài khoản mobile của bạn chưa được kích hoạt!',2);
		return false;		 
  }
  if(MobileType == 'C')
  {
	  inlineMsg(hrefMobile,'Hiện tại hệ thống chỉ hỗ trợ tra cước cho thuê bao trả sau!',2);
		return false;		 
  }
  
}
function CheckRegFrontMobile(hrefMobile,MobileStatus,MobileType) {  	  
  if(MobileStatus == null || MobileStatus == '-1')
		{
			inlineMsg(hrefMobile,'Bạn chưa đăng ký dịch vụ Mobile!',2);
		
		return false;
		}    
  if(MobileStatus == '0')
  {
	  inlineMsg(hrefMobile,'Tài khoản mobile của bạn chưa được kích hoạt!',2);
		return false;		 
  }
  if(MobileType != 'C')
  {
	  inlineMsg(hrefMobile,'Bạn phải đang là thuê bao trả trước thì mới có thể sử dụng dịch vụ này!',2);
		return false;		 
  }
  
}

function CheckMobileLock(hrefMobile,MobileStatus) {  	  
  if(MobileStatus == null || MobileStatus == '-1')
		{
			inlineMsg(hrefMobile,'Bạn chưa đăng ký dịch vụ Mobile!',2);
		
		return false;
		}    
  if(MobileStatus == '0')
  {
	  inlineMsg(hrefMobile,'Tài khoản mobile của bạn chưa được kích hoạt!',2);
		return false;		 
  }
  // TK mobile bi khoa 1 chieu
  if(MobileStatus == '01' || MobileStatus == '10' || MobileStatus == '11')
  {
	  inlineMsg(hrefMobile,'Tài khoản mobile của bạn đã bị khóa một chiều! Bạn không thể sử dụng dịch vụ này',2);
		return false;		 
  }
  // TK mobile bi khoa 1 chieu
  if(MobileStatus == '02' || MobileStatus == '20' || MobileStatus == '22')
  {
	  inlineMsg(hrefMobile,'Tài khoản mobile của bạn đã bị khóa hai chiều! Bạn không thể sử dụng dịch vụ này',2);
		return false;		 
  }
  if(MobileStatus == '03' || MobileStatus == '30' || MobileStatus == '33')
  {
	  inlineMsg(hrefMobile,'Tài khoản mobile của chưa được kích hoạt! Bạn không thể sử dụng dịch vụ này',2);
		return false;		 
  }
  return true;
}


function CheckPstnLock(hrefMobile,MobileStatus) {  	  
  if(MobileStatus == null || MobileStatus == '-1')
		{
			inlineMsg(hrefMobile,'Bạn chưa đăng ký dịch vụ PSTN!',2);
		
		return false;
		}    
  if(MobileStatus == '0')
  {
	  inlineMsg(hrefMobile,'Tài khoản PSTN của bạn chưa được kích hoạt!',2);
		return false;		 
  }
  // TK mobile bi khoa 1 chieu
  if(MobileStatus == '01' || MobileStatus == '10' || MobileStatus == '11')
  {
	  inlineMsg(hrefMobile,'Tài khoản PSTN của bạn đã bị khóa một chiều! Bạn không thể sử dụng dịch vụ này',2);
		return false;		 
  }
  // TK mobile bi khoa 1 chieu
  if(MobileStatus == '02' || MobileStatus == '20' || MobileStatus == '22')
  {
	  inlineMsg(hrefMobile,'Tài khoản PSTN của bạn đã bị khóa hai chiều! Bạn không thể sử dụng dịch vụ này',2);
		return false;		 
  }
  if(MobileStatus == '03' || MobileStatus == '30' || MobileStatus == '33')
  {
	  inlineMsg(hrefMobile,'Tài khoản PSTN của chưa được kích hoạt! Bạn không thể sử dụng dịch vụ này',2);
		return false;		 
  }
  return true;
}
// form validation function //
var url = "checkuser.jsp?user="; // The server-side script
var http = getHTTPObject(); // We create the XMLHTTPRequest Object
var httpEmail = getHTTPObject();
var httpADSL = getHTTPObject();
var httpPSTN = getHTTPObject();

function getHTTPObject() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}
function handleHttpResponse() 
{
    if (http.readyState == 4) 
	{
        if (http.status == 200) {
            //alert("handleHTTPResponse");            
                    
            // Split the comma delimited response into an array.

            //For plain text response (not XML formatted),
            //results = http.responseText.split(",");                    
                    
            // Or for XML formatted response text:
            var message = http.responseText;
			if (message.indexOf("empty") >= 0)
			{
				//alert(message);
				inlineMsg('p_Username_1','B&#7841;n ch&#432;a nh&#7853;p t&ecirc;n truy c&#7853;p !',2);
				return false;
			}
			if (message.indexOf("ok") >= 0)
			{
				//alert(message);
				inlineMsg('p_Username_1','T&ecirc;n truy  c&#7853;p n&agrave;y ch&#432;a t&#7891;n t&#7841;i, b&#7841;n c&oacute; th&#7875; s&#7917; d&#7909;ng t&ecirc;n n&agrave;y !</strong> ',2);
				return true;
			}
			else
			{
				inlineMsg('p_Username_1','T&ecirc;n truy c&#7853;p b&#7841;n ch&#7885;n &#273;&atilde; &#273;&#432;&#7907;c s&#7917; d&#7909;ng, xin vui l&ograve;ng  ch&#7885;n t&ecirc;n truy c&#7853;p kh&aacute;c!</strong>',2);
    		return false;
			}
        } else {
			inlineMsg('p_Username_1','Not able to check user name',2);
			return false;
            //alert ( "Not able to check user name" );
        }
    }    
}
function handleHttpResponseBT() 
{
    if (http.readyState == 4) 
	{
        if (http.status == 200) {
            //alert("handleHTTPResponse");            
                    
            // Split the comma delimited response into an array.

            //For plain text response (not XML formatted),
            //results = http.responseText.split(",");                    
                    
            // Or for XML formatted response text:
            var message = http.responseText;
			if (message.indexOf("empty") >= 0)
			{
				//alert(message);
				inlineMsg('p_Username_1','B&#7841;n ch&#432;a nh&#7853;p t&ecirc;n truy c&#7853;p !',2);
				return false;
			}
			if (message.indexOf("ok") >= 0)
			{
				//alert(message);				
				return true;
			}
			else
			{
				inlineMsg('p_Username_1','T&ecirc;n truy c&#7853;p b&#7841;n ch&#7885;n &#273;&atilde; &#273;&#432;&#7907;c s&#7917; d&#7909;ng, xin vui l&ograve;ng  ch&#7885;n t&ecirc;n truy c&#7853;p kh&aacute;c!</strong>',2);
    		return false;
			}
        } else {
			//inlineMsg('p_Username_1','Not able to check user name',2);
			//return false;
            //alert ( "Not able to check user name" );
        }
    }    
}
function handleHttpResponseMb() 
{
    if (http.readyState == 4) 
	{
        if (http.status == 200) {            
            var message = http.responseText;
			//alert(message);
			if (message.indexOf("Used") >= 0)
			{
				//alert(message);
				inlineMsg('p_ISDN_4','Số điện thoại di động Bạn đăng ký đang được sử dụng !',2);
				return false;
			}
			if (message.indexOf("none") >= 0)
			{
				//alert(message);
				inlineMsg('p_ISDN_4','Số điện thoại di động Bạn đăng ký không có !',2);
				return false;
			}
			if (message.indexOf("error") >= 0)
			{
				//alert(message);
				inlineMsg('p_ISDN_4','Error: ' + message,2);
				return false;
			}
			if (message.indexOf("F1") >= 0  || message.indexOf("C1") >= 0)
			{
				
				return true;
			}
        } else {
			//inlineMsg('p_ISDN_4','Không thể kết nối đến CSDL để kiểm tra số mobile',2);
			//return false;
            //alert ( "Not able to check user name" );
        }
    }    
}
function handleHttpResponseSe() 
{
    if (http.readyState == 4) 
	{
        if (http.status == 200) {            
            var message = http.responseText;
			//alert(message);
			if (message.indexOf("none") >= 0)
			{
				//alert(message);
				inlineMsg('secureletter','Bạn nhập sai chuỗi ký tự bảo mật !',2);
				return false;
			}
			return true;
			
        } else {
			inlineMsg('secureletter','Không thể kiểm tra tính hợp lệ của chuỗi ký tự bảo mật',2);
			return false;
            //alert ( "Not able to check user name" );
        }
    }    
}
function processRequestPSTN() {	
  if (httpPSTN.readyState == 4) {
	if (httpPSTN.status == 200) {
	   var message = httpPSTN.responseText;	   
		alert(message);

		if (message.indexOf("noreg") >= 0)
		{
			//alert(message);
			inlineMsg('p_HomePhone_4',' Không tồn tại số PSTN này trên hệ thống  !',2);
			return false;
		}
		if (message.indexOf("Used") >= 0)
		{
			//alert(message);
			inlineMsg('p_HomePhone_4',' Số PSTN này đang được sử dụng trên hệ thống  !',2);
			return false;
		}
		if (message.indexOf("error") >= 0)
		{
			//alert(message);
			inlineMsg('p_HomePhone_4',' error !',2);
			return false;
		}
		return true;
	}
	else
	{
		inlineMsg('p_HomePhone_4','Không thể kiểm tra tính hợp lệ của account PSTN',2);
		return false;
            
	}
	return true;
  }
}
function isExistPSTN(form) {    
	
    var p_HomePhone_4 = document.getElementById("p_HomePhone_4").value;
	if (p_HomePhone_4.substring(0,1) == "0")
            p_HomePhone_4 = p_HomePhone_4.substring(1);
	var p_Cert_No_5 = document.getElementById("p_Cert_No_5").value;
	
	//alert(p_HomePhone_4 + "<<<>>>" + p_Cert_No_5);
	var url1 = "checkPSTN.jsp?pstn="; // The server-side script
    httpPSTN.open("GET", url1 + escape(p_HomePhone_4) + "&contact_no=" + p_Cert_No_5, true);
    httpPSTN.onreadystatechange = processRequestPSTN;
    httpPSTN.send(null);
}
/* Bat dau phan kiem tra thong tin ADSL */
function processRequestADSL() {	
  if (httpADSL.readyState == 4) {
	if (httpADSL.status == 200) {
	   var message = httpADSL.responseText;	   
		//alert(message);
		if (message.indexOf("Used") >= 0)
		{
			inlineMsg('p_ADSL_4','Tên truy cập ADSL đang được dùng trên hệ thống !',2);
			return false;
		}
		if (message.indexOf("none") >= 0)
		{
			//alert(message);
			inlineMsg('p_ADSL_4',' Không tồn tại tên truy cập ADSL trên hệ thống  !',2);
			return false;
		}
		if (message.indexOf("error") >= 0)
		{
			//alert(message);
			inlineMsg('p_ADSL_4',' error !',2);
			return false;
		}
		return true;
	}
	else
	{
		//inlineMsg('p_ADSL_4','Không thể kiểm tra tính hợp lệ của account ADSL',2);
		return false;
            
	}
	return true;
  }
}
function isExistADSL(form) {
    
	
    var p_ISDN_4 = document.getElementById("p_ADSL_4").value;	
	//alert(p_ISDN_4);
	var url1 = "checkADSL.jsp?adsl="; // The server-side script
    httpADSL.open("GET", url1 + escape(p_ISDN_4), true);
    httpADSL.onreadystatechange = processRequestADSL;
    httpADSL.send(null);
}
/* Ket thuc phan kiem tra thong tin ADSL */
/* Bat dau phan kiem tra thong tin email */
function processRequestEmail() {	
  if (httpEmail.readyState == 4) {
	if (httpEmail.status == 200) {
	   var message = httpEmail.responseText;	   
		//alert(message);
		if (message.indexOf("empty") >= 0)
		{
			inlineMsg('p_Email_1','B&#7841;n ch&#432;a nh&#7853;p địa chỉ Email !',2);
			return false;
		}
		if (message.indexOf("none") >= 0)
		{
			//alert(message);
			inlineMsg('p_Email_1','Địa chỉ Email bạn nhập đã được sử dụng trên hệ thống !',2);
			return false;
		}
		return true;
	}
	else
	{
		//inlineMsg('p_Email_1','Không thể kiểm tra sự tồn tại của email',2);
		return false;
            
	}
	return true;
  }
}
function CheckEmail(p_Email) {
    //alert(p_Email);
	//var p_Email_1 = document.getElementById("p_Email_1").value; //form.p_Username_1.value;
	var url1 = 'checkEmail.jsp?email=' + escape(p_Email);
	httpEmail.open("GET", url1, true);
    httpEmail.onreadystatechange = processRequestEmail;
    httpEmail.send(null);
  
}
function isExistMobile(form) {
    
	//var UserNameRegex = /^[a-z0-9]*$/i;
    var p_ISDN_4 = document.getElementById("p_ISDN_4").value; //form.p_Username_1.value;
	if (p_ISDN_4.substring(0,1) == "0")
            p_ISDN_4 = p_ISDN_4.substring(1);
	//alert(p_ISDN_4);
	//return false;
	var url1 = "checkMobile.jsp?isdn="; // The server-side script
    http.open("GET", url1 + escape(p_ISDN_4), true);
    http.onreadystatechange = handleHttpResponseMb;
    http.send(null);
}
//var chkMobi = false;
//var chkAdsl = false;
//var chkPstn = false;

function isExistMobileNew()
{
  var p_ISDN_4 = document.getElementById("p_ISDN_4").value; //form.p_Username_1.value;
  if (p_ISDN_4.substring(0,1) == "0")
        p_ISDN_4 = p_ISDN_4.substring(1);
  var aoCP = new AjaxObject({
      url: 'checkMobile.jsp',
      params:
      {
        isdn: p_ISDN_4
      },
      success: function()
      {
        //alert(this.responseText);
        var msgContent = this.responseText;
        
      }
    });
   aoCP.request();

  return false;
}
function isExistUserName(form) {
    
	var UserNameRegex = /^[a-z0-9]*$/i;
    var p_Username_1 = document.getElementById("p_Username_1").value; //form.p_Username_1.value;
	//alert(p_Username_1);
	
    http.open("GET", url + escape(p_Username_1), true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
}
function isExistUserNameBT(form) {
    
	var UserNameRegex = /^[a-z0-9]*$/i;
    var p_Username_1 = document.getElementById("p_Username_1").value; //form.p_Username_1.value;
	//alert(p_Username_1);
	
    http.open("GET", url + escape(p_Username_1), true);
    http.onreadystatechange = handleHttpResponseBT;
    http.send(null);
}
function CheckSecurityCode(SecurityCodeInput) {
    
	var secureletter = document.getElementById("secureletter").value; //form.p_Username_1.value;
	//alert(secureletter);
	
    http.open("GET", 'checkSecurityCode.jsp?secureletter=' + escape(secureletter), true);
    http.onreadystatechange = handleHttpResponseSe;
    http.send(null);
}
function SubmitFormReg() {
	var frm = document.getElementById("frmRegister");
	if (frm)
	{
		alert("sfsfsdfs");
		frm.method = "post";
		frm.action = "register_acc.jsp";
		frm.submit();
	}
	else
		alert("Error");
    
	//form.action = "register_acc.jsp";
	//form.method = "post";
	//document.frmRegister.submit();
}
function ValidDataRegPage1(form) {
  //var nameRegex = /^[a-z ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữ]*$/i;
  var nameRegex = /^[a-z ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữàáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵđÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴĐ]*$/i;
  //var addressRegex = /^[a-z0-9 ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữ]*$/i;
  var addressRegex = /^[a-z0-9 ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữàáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵđÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴĐ]*$/i;
  var mobiRegex = /^(097|098|0164|0165|0166|0167|0168|0169)\d{7}$/i;
  var homePhoneRegex = /^(097|098|0164|0165|0166|0167|0168|0169)\d{7}$/i;
  var UserNameRegex = /^[a-z0-9]*$/i;
  var PassRegex = /(([\d].*[a-z])|([a-z].*[\d]))+/i;
  //var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  //var nameRegex = /[a-zA-Z?â?êô??áà??ã???????????????ò???????????????]?[a-zA-Z ]?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  var firstName = form.p_FamilyName_1.value;
  var lastName = form.p_FirstName_1.value;
  var midleName = form.p_MiddleName_1.value;
 
  var BirthDate = form.p_Day.value;
  var BirthMonth = form.p_Month.value;
  var BirthYear = form.p_Year.value;

  var Gender = form.p_Gender.value;
  var Country = form.p_Country_cmb.value;
  var p_Province = form.p_Province_cmb.value;
  var p_Nationality = form.p_Nationality_cmb.value;
  var p_Address_1 = form.p_Address_1.value;
  var p_Email = form.p_Email_1.value;
  var p_Job = form.p_Job_cmb.value;

  var p_Username_1 = form.p_Username_1.value;
  var p_Password_1 = form.p_Password_1.value;
  var p_ConfirmPassword_1= form.p_ConfirmPassword_1.value;
  
  var p_SecretQuestion_cmb = form.p_SecretQuestion_cmb.value;
  var p_SecretAnswer_1 = form.p_SecretAnswer_1.value;
  // Check ho
  if(firstName == "") {    
    inlineMsg('p_FamilyName_1','B&#7841;n ch&#432;a nh&#7853;p h&#7885; t&ecirc;n !',2);
    return false;
  }
  if(!firstName.match(nameRegex)) {
    inlineMsg('p_FamilyName_1','H&#7885; t&ecirc;n kh&ocirc;ng &#273;&#432;&#7907;c c&oacute; c&aacute;c c&aacute;c k&yacute; t&#7921; s&#7889; !',2);
    return false;
  }
  //Check ten dem
  if(!(midleName == "") && (!midleName.match(nameRegex))) {
    inlineMsg('p_MiddleName_1','H&#7885; t&ecirc;n kh&ocirc;ng &#273;&#432;&#7907;c c&oacute; c&aacute;c c&aacute;c k&yacute; t&#7921; s&#7889; !',2);
    return false;
  }
    // Check ten
  if(lastName == "") {    
    inlineMsg('p_FirstName_1','B&#7841;n ch&#432;a nh&#7853;p t&ecirc;n!',2);
    return false;
  }
  if(!lastName.match(nameRegex)) {
    inlineMsg('p_FirstName_1','H&#7885; t&ecirc;n kh&ocirc;ng &#273;&#432;&#7907;c c&oacute; c&aacute;c c&aacute;c k&yacute; t&#7921; s&#7889; !',2);
    return false;
  }
  // Check Ngay sinh
   if(BirthDate == "-") {    
    inlineMsg('p_Day','B&#7841;n ch&#432;a nh&#7853;p ng&agrave;y sinh !',2);
    return false;
  }
  // Check tháng sinh
   if(BirthMonth == "-") {    
    inlineMsg('p_Month','B&#7841;n ch&#432;a nh&#7853;p th&aacute;ng sinh !',2);
    return false;
  }
  // Check nam sinh
   if(BirthYear == "-") {    
    inlineMsg('p_Year','B&#7841;n ch&#432;a nh&#7853;p năm sinh !',2);
    return false;
  }
    // Check gioi tinh
   if(Gender == "-") {    
    inlineMsg('p_Gender','B&#7841;n ch&#432;a ch&#7885;n gi&#7899;i t&iacute;nh !',2);
    return false;
  }
   // ??a ch?
 if(p_Address_1 == "") {    
    inlineMsg('p_Address_1','B&#7841;n ch&#432;a nh&#7853;p &#273;&#7883;a ch&#7881; !',2);
    return false;
  }
 //if(!p_Address_1.match(addressRegex)){
 //   inlineMsg('p_Address_1','B&#7841;n nh&#7853;p ch&#432;a &#273;&uacute;ng &#273;&#7883;nh d&#7841;ng email.',2);
 //   return false;
 // }
   // Check quoc gia
   if(Country == "-") {    
    inlineMsg('p_Country_cmb','B&#7841;n ch&#432;a ch&#7885;n qu&#7889;c gia !',2);
    return false;
  }

 // Check tinh thanh
   if(p_Province == "-" && Country == "84") {    
    inlineMsg('p_Province_cmb','B&#7841;n ch&#432;a ch&#7885;n t&#7881;nh th&agrave;nh !',2);
    return false;
  }
// Check quoc tich
   if(p_Nationality == "-") {    
    inlineMsg('p_Nationality_cmb','B&#7841;n ch&#432;a ch&#7885;n t&#7881;nh th&agrave;nh !',2);
    return false;
  }
 
// Check email
 if(p_Email == "") {    
    inlineMsg('p_Email_1','B&#7841;n ch&#432;a ch&#7885;n email !',2);
    return false;
  }
 if(!p_Email.match(emailRegex)){
    inlineMsg('p_Email_1','B&#7841;n nh&#7853;p ch&#432;a &#273;&uacute;ng &#273;&#7883;nh d&#7841;ng email.',2);
    return false;
  }
  CheckEmail(p_Email);

// Check nghe nghiep
   if(p_Job == "-") {    
    inlineMsg('p_Job_cmb','B&#7841;n ch&#432;a ch&#7885;n ngh&#7873; nghi&#7879;p !',2);
    return false;
  }
 // Check ten truy cap
 if(p_Username_1=="") {
    inlineMsg('p_Username_1','B&#7841;n ch&#432;a nh&#7853;p t&ecirc;n truy c&#7853;p !',2);
    return false;
  }
if(!p_Username_1.match(UserNameRegex)) {
    inlineMsg('p_Username_1','H&#7885; t&ecirc;n kh&ocirc;ng &#273;&#432;&#7907;c c&oacute; c&aacute;c c&aacute;c k&yacute; t&#7921; s&#7889; !',2);
    return false;
  }
  //alert(p_Username_1);
  // Khi KH bam luon nut tiep tuc, neu User chua ton tai trong hthong thi ko bao gi
 isExistUserNameBT(p_Username_1);

// Check pass
 if(p_Password_1 == "") {    
    inlineMsg('p_Password_1','B&#7841;n ch&#432;a nh&#7853;p m&#7853;t kh&#7849;u !',2);
    return false;
  }
  if(p_Password_1.length < 6 && p_Password_1 !=  "") {    
    inlineMsg('p_Password_1','M&#7853;t kh&#7849;u ph&#7843;i t&#7889;i thi&#7875;u 6 k&yacute; t&#7921; !',2);
    return false;
  }
  if(!p_Password_1.match(PassRegex)) {    
    inlineMsg('p_Password_1','M&#7853;t kh&#7849;u ph&#7843;i bao g&#7891;m c&#7843; k&yacute; t&#7921; s&#7889; v&agrave; chu&#7895;i !',2);
    return false;
  }
  if(p_ConfirmPassword_1 !="" && p_Password_1 !=  p_ConfirmPassword_1) {    
    inlineMsg('p_Password_1','M&#7853;t kh&#7849;u kh&ocirc;ng tr&ugrave;ng v&#7899;i m&#7853;t kh&#7849;u nh&#7853;p l&#7841;i  !',2);
    return false;
  }
  // Check retype pass
  
 if(p_ConfirmPassword_1 == "") {    
    inlineMsg('p_ConfirmPassword_1','B&#7841;n ch&#432;a nh&#7853;p m&#7853;t kh&#7849;u nh&#7853;p l&#7841;i !',2);
    return false;
  }
  if(p_ConfirmPassword_1.length < 6 && p_Password_1 !=  "") {    
    inlineMsg('p_ConfirmPassword_1','M&#7853;t kh&#7849;u nh&#7853;p l&#7841;i ph&#7843;i t&#7889;i thi&#7875;u 6 k&yacute; t&#7921; !',2);
    return false;
  }
  if(!p_ConfirmPassword_1.match(PassRegex)) {    
    inlineMsg('p_ConfirmPassword_1','M&#7853;t kh&#7849;u nh&#7853;p l&#7841;i ph&#7843;i bao g&#7891;m c&#7843; k&yacute; t&#7921; s&#7889; v&agrave; chu&#7895;i !',2);
  }
   if(p_Password_1 !=  p_ConfirmPassword_1) {    
    inlineMsg('p_ConfirmPassword_1','M&#7853;t kh&#7849;u nh&#7853;p l&#7841;i kh&ocirc;ng tr&ugrave;ng v&#7899;i m&#7853;t kh&#7849;u !',2);
    return false;
  }
// Check cau hoi bi mat

   if(p_SecretQuestion_cmb == "-") {    
    inlineMsg('p_SecretQuestion_cmb','B&#7841;n ch&#432;a ch&#7885;n c&acirc;u h&#7887;i b&iacute; m&#7853;t !',2);
    return false;
  }

  // Check cau tra loi cau hoi bi mat
 if(p_SecretAnswer_1 == "") {    
    inlineMsg('p_SecretAnswer_1','B&#7841;n ch&#432;a tr&#7843; l&#7901;i c&acirc;u h&#7887;i b&iacute; m&#7853;t !',2);
    return false;
  }
  		form.p_FirstName.value = escape(form.p_FirstName_1.value);
		form.p_MiddleName.value = escape(form.p_MiddleName_1.value);
		form.p_FamilyName.value = escape(form.p_FamilyName_1.value);
                
        form.p_Day.value = escape(form.p_Day.value);
		form.p_Month.value = escape(form.p_Month.value);
		form.p_Year.value = escape(form.p_Year.value);
                
		form.p_Gender.value = escape(form.p_Gender.value);
		
		form.p_Address.value = escape(form.p_Address_1.value);
		//form.p_Cert_No.value = escape(form.p_Cert_No_1.value);
		
		form.p_Country.value = escape(form.p_Country_cmb.value);
		if (Country != "84")
			form.p_Province.value = "";
		else		
			form.p_Province.value = escape(form.p_Province_cmb.value);
			
		form.p_Nationality.value = escape(form.p_Nationality_cmb.value);
		
		form.p_ContactPhone.value = escape(form.p_ContactPhone_1.value);
		form.p_Email.value = escape(form.p_Email_1.value);
		form.p_Company.value = escape(form.p_Company_1.value);
		form.p_CompanyAddress.value = escape(form.p_CompanyAddress_1.value);
		form.p_Username.value = escape(form.p_Username_1.value);
		form.p_Password.value = escape(form.p_Password_1.value);                
		form.p_SecretQuestion.value = escape(form.p_SecretQuestion_cmb.value);
		form.p_SecretAnswer.value = escape(form.p_SecretAnswer_1.value);
		form.p_Job.value =  escape(form.p_Job_cmb.value);
                
  return true;
}
function ValidDataRegPage2(form,SeCode) {
  //var nameRegex = /^[a-z ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữ]*$/i;
  var nameRegex = /^[a-z ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữàáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵđÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴĐ]*$/i;
  //var addressRegex = /^[a-z0-9 ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữ]*$/i;
  var addressRegex = /^[a-z0-9 ăâđêôơưáàảạãắằẳặẵấầẩậẫếềểệễốồổộỗớờởợỡứừửựữàáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵđÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴĐ]*$/i;
  var mobiRegex = /^(097|098|0164|0165|0166|0167|0168|0169)\d{7}$/i;
  var UserNameRegex = /^[a-z0-9]*$/i;
  var PassRegex = /(([\d].*[a-z])|([a-z].*[\d]))+/i;
  //var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  //var nameRegex = /[a-zA-Z?â?êô??áà??ã???????????????ò???????????????]?[a-zA-Z ]?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  var p_ISDN_4 = form.p_ISDN_4.value;
  var p_ADSL_4 = form.p_ADSL_4.value;
  var secureletter = form.secureletter.value;
  
  if(!form.p_isMobileAccount.checked && !form.p_isADSL.checked && !form.p_isFixPhone.checked && !form.p_isSuperMarket.checked)
		{
			inlineMsg('p_Continue','Qu&yacute; kh&aacute;ch c&#7847;n ch&#7885;n &iacute;t nh&#7845;t m&#7897;t trong c&aacute;c d&#7883;ch v&#7909; &#273;&#432;&#7907;c cung c&#7845;p b&#7903;i Viettel !',2);
		
		return false;
		}    
  if(form.p_isMobileAccount.checked)
  {
	  // Check so mobile
	  if(p_ISDN_4 == "") {    
		inlineMsg('p_ISDN_4','B&#7841;n ch&#432;a nh&#7853;p s&#7889; di &#273;&#7897;ng !',2);
		return false;
	  }
	  //Ktra xem so mobile da duoc su dung chua
	  if(!p_ISDN_4.match(mobiRegex)) {
		inlineMsg('p_ISDN_4','S&#7889; di &#273;&#7897;ng ph&#7843;i b&#7855;t &#273;&#7847;u b&#7857;ng m&#7897;t trong c&aacute;c d&atilde;y s&#7889; sau: 097,098,0164,0165,0166,0167,0168,0169 ',2);
		return false;
	  }
	  //isExistMobileNew();
      if( ((p_ISDN_4.indexOf("097") == 0) || (p_ISDN_4.indexOf("098") == 0)) && (p_ISDN_4.length < 10 || p_ISDN_4.length > 10) ) {
            inlineMsg('p_ISDN_4','S&#7889; di &#273;&#7897;ng ph&#7843;i c&oacute; &#273;&#7911; 10 ch&#7919; s&#7889; v&#7899;i c&aacute;c &#273;&#7847;u 097,098 !',2);
            return false;
        }
        if( ((p_ISDN_4.indexOf("0164") == 0) || (p_ISDN_4.indexOf("0165") == 0) || (p_ISDN_4.indexOf("0166") == 0) || (p_ISDN_4.indexOf("0167") == 0) || (p_ISDN_4.indexOf("0168") == 0) || (p_ISDN_4.indexOf("0169") == 0)) && (p_ISDN_4.length < 11 || p_ISDN_4.length > 11) ) {
            inlineMsg('p_ISDN_4','S&#7889; di &#273;&#7897;ng ph&#7843;i c&oacute; &#273;&#7911; 11 ch&#7919; s&#7889; v&#7899;i c&aacute;c &#273;&#7847;u 0164,0165,0166,0167,0168,0169 !',2);
            return false;
        }
  }
  if(form.p_isADSL.checked) {
	 var p_Password_5= form.p_Password_5.value;
	   // Check ten truy cap
	 if(p_ADSL_4=="") {
		inlineMsg('p_ADSL_4','B&#7841;n ch&#432;a nh&#7853;p t&ecirc;n truy c&#7853;p ADSL!',2);
		return false;
	  }
          /*
	 if(!p_ADSL_4.match(UserNameRegex)) {
		inlineMsg('p_ADSL_4','T&ecirc;n truy c&#7853;p ph&#7843;i g&#7891;m c&aacute;c k&yacute; t&#7921; ch&#7919; c&aacute;i ho&#7863;c s&#7889; !',2);
		return false;
	  }
	  if(p_Password_5=="") {
		inlineMsg('p_Password_5','B&#7841;n ch&#432;a nh&#7853;p m&#7853;t kh&#7849;u ADSL!',2);
		return false;
	  }
        */
	  isExistADSL(p_ADSL_4);
	  
  }
  if(form.p_isFixPhone.checked) {
	 var p_Province_5= form.p_Province_5.value;
	 var p_HomePhone_4=form.p_HomePhone_4.value;
	 var p_Cert_No_5 = form.p_Cert_No_5.value;
	 
	   // Check ten truy cap
	 if(p_Province_5=="-") {
		inlineMsg('p_Province_5','B&#7841;n ch&#432;a ch&#7885;n t&#7881;nh/th&agrave;nh!',2);
		return false;
	  }
	  if( p_HomePhone_4 == "" ) {
		inlineMsg('p_HomePhone_4','B&#7841;n ch&#432;a nh&#7853;p s&#7889; &#273;i&#7879;n tho&#7841;i c&#7889; &#273;&#7883;nh !',2);
		return false;
	  }
	  	  
	  //Lay so va bo ma tinh thanh di
	  var str= p_HomePhone_4.substring(p_Province_5.length + 1);
	  //alert(str.substring(0,2));
      /*
	  if(str.substring(0,2) < 25 || str.substring(0,2) > 29)
		{
			inlineMsg('p_HomePhone_4','S&#7889; &#273;i&#7879;n tho&#7841;i c&#7889; &#273;&#7883;nh ph&#7843;i c&oacute; d&#7841;ng: M&atilde; v&ugrave;ng + 25xxxxx &#273;&#7871;n 29xxxxx !',2);                       
			return false;
		}
		if(str.length<6||str.length>7)
		{
			inlineMsg('p_HomePhone_4','S&#7889; &#273;i&#7879;n tho&#7841;i c&#7889; &#273;&#7883;nh ph&#7843;i &#273;&#7911; 6 ho&#7863;c b&#7843;y k&yacute; t&#7921; s&#7889;',2);  
			
			return false;
		}
       */

	  if( p_Cert_No_5 == "" )  {
		inlineMsg('p_Cert_No_5','B&#7841;n ch&#432;a nh&#7853;p s&#7889; h&#7907;p &#273;&#7891;ng !',2);
		return false;
	 }
	 //isExistPSTN(p_Cert_No_5);
  } 
	if( secureletter == "" )  {
		inlineMsg('secureletter','Bạn chưa nhập ký tự kiểm tra !',2);
		return false;
	 }	
	 /*
	if( secureletter != SeCode )  {
		inlineMsg('secureletter','Ký tự kiểm tra bạn nhập không đúng !',2);
		return false;
	 }	
	 */
   //alert(strSecureLetter);
   //return CheckSecurityCode(secureletter);
  
  	form.p_Command.value = 'insert';
	
	form.p_FirstName.value = escape(form.p_FirstName.value);
	form.p_MiddleName.value = escape(form.p_MiddleName.value);
	form.p_FamilyName.value = escape(form.p_FamilyName.value);
			//alert(form.p_FamilyName.value);
			//alert(escape(form.p_FirstName.value));
			//alert(frmRegister.p_FirstName.value);
			//alert(frmRegister.p_FamilyName.value);
			
	form.p_Day.value = escape(form.p_Day.value);
	form.p_Month.value = escape(form.p_Month.value);
	form.p_Year.value = escape(form.p_Year.value);
			
	form.p_Gender.value = escape(form.p_Gender.value);
	
	form.p_Address.value = escape(form.p_Address.value);
	form.p_Cert_No.value = escape(form.p_Cert_No.value);
	
	form.p_Country.value = escape(form.p_Country.value);
	form.p_Province.value = escape(form.p_Province.value);
	form.p_Nationality.value = escape(form.p_Nationality.value);
	
	form.p_Job.value =  escape(form.p_Job.value);
	form.p_ContactPhone.value = escape(form.p_ContactPhone.value);
	form.p_Email.value = escape(form.p_Email.value);
	form.p_Company.value = escape(form.p_Company.value);
	form.p_CompanyAddress.value = escape(form.p_CompanyAddress.value);
	form.p_Username.value = escape(form.p_Username.value);
	form.p_Password.value = escape(form.p_Password.value);                            
	form.p_SecretQuestion.value = escape(form.p_SecretQuestion.value);
	form.p_SecretAnswer.value = escape(form.p_SecretAnswer.value);
	
	form.p_ISDN.value = escape(form.p_ISDN_4.value);                                            
	form.p_ADSL.value = escape(form.p_ADSL_4.value);
	form.p_Password_2.value = escape(form.p_Password_5.value);
	//form.p_ConfirmPassword_2.value = escape(form.p_ConfirmPassword_5.value);
	form.p_HomePhone.value = escape(form.p_HomePhone_4.value.substring(1));
	form.p_Province_2.value = escape(form.p_Province_5.value);
	form.p_Cert_No_2.value = escape(form.p_Cert_No_5.value);
	form.p_Username_3.value = escape(form.p_Username_6.value);
	form.p_Password_3.value = escape(form.p_Password_6.value);

	//form.p_ConfirmPassword_3.value = escape(form.p_ConfirmPassword_6.value);
	//SubmitFormReg();
	
  return true;
}


// START OF MESSAGE SCRIPT //

var MSGTIMER = 50;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 5;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 5000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "Images/msg_arrow.gif"; 
}
/*
Check Email 
*/
var reqEmail = init();
reqEmail.onreadystatechange = processRequest;
function init() {
  if (window.XMLHttpRequest) {
	return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
	return new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function processRequest () {	
  if (reqEmail.readyState == 4) {
	if (reqEmail.status == 200) {
	   var message = http.responseText;
	   
		if (message.indexOf("empty") >= 0)
		{
			//alert(message);
			inlineMsg('p_Email_1','B&#7841;n ch&#432;a nh&#7853;p t&ecirc;n truy c&#7853;p !',2);
			return false;
		}
		if (message.indexOf("none") >= 0)
		{
			//alert(message);
			inlineMsg('p_Email_1','Địa chỉ Email bạn nhập đã được sử dụng trên hệ thống !',2);
			return false;
		}
		return true;
	}
  }
}

function doGet(url) {  
  reqEmail.open("GET", url, true);
  reqEmail.send(null);
}

function doPost(body) {
  reqEmail.open("POST", url, true);
  reqEmail.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  reqEmail.send(body);
}
