	var xmlhttpDuplicateEmail;
	var xmlDoc;
	var Parameter = "";
	var vUserName = false;
	
	function SaveAccountSetup()
	{
		//Get input hidden object for save temp value Account Setup
		var hAccountSetup = document.getElementById("hAccountSetup");
		
		var txtUserName = document.getElementById("txtUserName");
		var txtPassword = document.getElementById("txtPassword");		
		
		hAccountSetup.value = txtUserName.value + "#" + txtPassword.value;		
	}

	function ShowAccountSetup()
	{
		//Get input hidden object for show temp value Account Setup
		var hAccountSetup = document.getElementById("hAccountSetup");
		
		var txtUserName = document.getElementById("txtUserName");
		var txtPassword = document.getElementById("txtPassword");
		var txtReTypePassword = document.getElementById("txtReTypePassword");
		var sAccountSetup = document.getElementById("saccountsetup");
		
		//Check to The Last Control if null it's mean the all control not yet Rendered otherwise the control is rendered and bind data to that control
		//Content Not yet Rendered
		if(sAccountSetup == null)
		{
			setTimeout("ShowAccountSetup();", 300);
		}
		//Content Rendered
		else
		{
			if(hAccountSetup.value != "" && hAccountSetup.value != null)
			{
				//Get Company Id, default to 0, it's mean a new registrar
				var companyId = RegOrEditAccount();
				var sTitleAccount = document.getElementById("titleaccount");
		
				//New Registration change title to "Create Account" otherwise "Update Account"
				if(companyId == 0)
					sTitleAccount.innerHTML = "Create Account";
				else
					sTitleAccount.innerHTML = "Update Account";
		
				//Split value from input hidden
				var AllValue = hAccountSetup.value.split("#");
				//Bind value to control
				txtUserName.value = AllValue[0];
				txtPassword.value = AllValue[1];
				txtReTypePassword.value = AllValue[1];
			}
		}
	}
	
	function AccountValidation()
	{
		//~ var ValidationAccount = false;
		//~ var vUserName = false;
		//~ var vPassword = false;
		//~ var vReTypePassword = false;
		
		//~ var PatternEmail =  /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		
		var sIndicates = document.getElementById("sIndicates");
		//Object to Confirmation
		var txtUserName = document.getElementById("txtUserName");
		//~ var txtPassword = document.getElementById("txtPassword");
		//~ var txtReTypePassword = document.getElementById("txtReTypePassword");
		//Confirmation
		var vtxtUserName = document.getElementById("vtxtUserName");
		//~ var vtxtPassword = document.getElementById("vtxtPassword");
		//~ var vtxtReTypePassword = document.getElementById("vtxtReTypePassword");
		
		//Email True format
		if(PatternEmail.test(txtUserName.value))
		{	
			CheckDuplicateEmail();
			
			//~ vUserName = true;
			//~ sIndicates.style.display = "none";
			//~ vtxtUserName.style.display = "none";			
		}
		//Email Wrong Format
		else
		{
			vUserName = false;
			sIndicates.style.display = "";
			vtxtUserName.style.display = "";
			vtxtUserName.innerHTML = "* Invalid email format";
			
			ValidatePassword(vUserName);
		}		
		
		//~ return ValidationAccount;
	}
	
	function ValidatePassword(vUserName2)
	{
		var sIndicates = document.getElementById("sIndicates");
		var ValidationAccount = false;		
		var vPassword = false;
		var vReTypePassword = false;
		//Object to Confirmation
		var txtPassword = document.getElementById("txtPassword");
		var txtReTypePassword = document.getElementById("txtReTypePassword");
		//Confirmation
		var vtxtPassword = document.getElementById("vtxtPassword");
		var vtxtReTypePassword = document.getElementById("vtxtReTypePassword");
		
		//There is a value on Password Text
		//Hide Validation Star
		if(txtPassword.value != "" && txtPassword.value != null)
		{
			vtxtPassword.style.display = "none";
			vPassword = true;
		}
		//Show Validation Star
		else
		{
			sIndicates.style.display = "";
			vtxtPassword.value = "*";
			vtxtPassword.style.display = "";
			vPassword = false;
		}
		
		//There is a value on ReType sPassword Text
		//Hide Validation Star
		if(txtReTypePassword.value != "" && txtReTypePassword.value != null)
		{			
			vtxtReTypePassword.style.display = "none";
			vReTypePassword = true;
		}
		//Show Validation Star
		else
		{
			sIndicates.style.display = "";
			vtxtReTypePassword.style.display = "";
			vReTypePassword = false;
		}
		
		//Check Password and ReType Password
		if(vPassword == true && vReTypePassword == true)
		{			
			//Password and ReType Password Match
			if(txtPassword.value == txtReTypePassword.value)
			{	
				if(txtPassword.value.length < 6)
				{
					vtxtPassword.style.display = "";
					vtxtReTypePassword.style.display = "";
					vtxtPassword.innerHTML = "* At least 6 character.";
					ValidationAccount = false;
				}
				else
				{
					var ValidPassword = IsValidPassword(txtPassword.value)
					if(ValidPassword == false)
					{
						vtxtPassword.style.display = "";
						vtxtReTypePassword.style.display = "";
						vtxtPassword.innerHTML = "* Must contains 1 number and 1 character.";
						ValidationAccount = false;						
					}
					else
					{
						//Password Not Contain &
						if(txtPassword.value.indexOf("&") == -1)
						{
							sIndicates.style.display = "none";
							vtxtPassword.style.display = "none";
							vtxtReTypePassword.style.display = "none";	
							ValidationAccount = true;
						}
						//Password Contain &
						else
						{
							sIndicates.style.display = "";
							vtxtPassword.style.display = "";							
							vtxtPassword.innerHTML = "* Password cannot contain &.";
							ValidationAccount = false;
						}
					}
				}
			}
			//password and ReType Password do not Match
			else
			{
				vtxtPassword.style.display = "";
				vtxtReTypePassword.style.display = "";
				vtxtPassword.innerHTML = "* Password do not match";
				ValidationAccount = false;
			}			
		}
		
		//UserName, Password and ReType Password is correct
		if(vUserName2 == true && ValidationAccount == true)
		{
			ValidationAccount = true;
			//Save Value To Temp Input Hidden
			SaveValue();
			
			if(AccountSetupToConfirmation == true)
				IndexPage = 7;		//GoToConfirmation
			else	
				IndexPage += 1;
				
			//Request Next Page
			RequestPage(MyPages[IndexPage]);
			
			if(AccountSetupToConfirmation == true)
			{
				//Get Button Continue Id
				var btnContinue = document.getElementById("btnContinue");
				//Change Button Continue value
				btnContinue.value = "Submit";
				btnContinue.src = "Images/submit_but.gif";
				//hide click here
				var sGoToConfirmation = document.getElementById("sGoToConfirmation");
				sGoToConfirmation.style.display = "none";
				//Set to false back
				AccountSetupToConfirmation = false;
			}
			
			//Show Back Button
			if(IndexPage != 1)
			{				
				var btnBack = document.getElementById("btnBack");	
				btnBack.style.display = "";
			}
			
			//Set Time Out to 300 milisecond and Check Value Hidden  Input, if not null than show the value
			setTimeout("CheckValueInputHidden();", 300);
		}
		//UserName, Password and ReType Password InCorrect
		else
		{
			ValidationAccount = false;
		}
		
		//validateAccount = ValidationAccount;
	}
	
	function CheckDuplicateEmail()
	{		
		//Clear vUserName		
		var txtUserName = document.getElementById("txtUserName");
		
		//Get Company Id, default to 0, it's mean a new registrar
		var companyId = RegOrEditAccount();		
		
		//build parameter
		var parameter = "companyId=" + companyId + "&email=" + txtUserName.value;
		
		//Request Check Duplicate Email
		RequestPageCheckDuplicateEmail("checkduplicateemail.php", parameter);
	}
	
	function RequestPageCheckDuplicateEmail(url, parameter)
	{
		xmlhttpDuplicateEmail=null;
		// code for Mozilla, etc.
		if (window.XMLHttpRequest)
		{
			xmlhttpDuplicateEmail=new XMLHttpRequest();
		}
		// code for IE
		else if (window.ActiveXObject)
		{
			xmlhttpDuplicateEmail=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (xmlhttpDuplicateEmail!=null)
		{
			xmlhttpDuplicateEmail.open("POST", url, true);			  
			xmlhttpDuplicateEmail.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttpDuplicateEmail.setRequestHeader("Content-length", parameter.length);
			xmlhttpDuplicateEmail.setRequestHeader("Connection", "close");
			  
			xmlhttpDuplicateEmail.onreadystatechange=StateChangeCheckDuplicateEmail;
			xmlhttpDuplicateEmail.send(parameter);			
		}
		else
		{
			alert("Your browser does not support XMLHTTP.");
		}
	}
	
	function StateChangeCheckDuplicateEmail()
	{				
		//readyState, 0 = uninitialized
		//		1 = loading
		//		2 = loaded
		//		3 = interactive
		//		4 = complete
		
		//Completed Request
		if (xmlhttpDuplicateEmail.readyState==4)
		  {
			// Status value just 404 and 200, 404 = "Not Found" & 200 = "Ok"
			if (xmlhttpDuplicateEmail.status==200)
			{				
				//Get Response from next page
				var resp = xmlhttpDuplicateEmail.responseText;	// response	
				//~ alert(resp);
				
				var sIndicates = document.getElementById("sIndicates");
				var vtxtUserName = document.getElementById("vtxtUserName");
				//Email doesn't exist on database and we can use that email for username
				if(resp == "true")
				{
					vUserName = true;
					//Hide error message
					sIndicates.style.display = "none";
					vtxtUserName.style.display = "none";					
				}
				//Email Exist on databse and give warning message
				else
				{					
					vUserName = false;
					sIndicates.style.display = "";
					vtxtUserName.innerHTML = "* Username already exist.";
					vtxtUserName.style.display = "";
				}
				
				ValidatePassword(vUserName);
			}
			else
			{			
				alert("Problem retrieving data");
			}
		  }
	}
	
	function RegOrEditAccount()
	{	
		var fullURL = parent.document.URL;
		var qs = fullURL.split('?');
		//Default to 0, it's mean new Registrar
		var cid = "0";
		
		if(qs.length > 1)
		{
			queryStr = qs[1].split('&');
			for(var i=0; i<queryStr.length; i++)
			{
				key = queryStr[i].split('=');
				if(key[0] =="cid")
					cid = key[1];
			}			
		}

		return cid;
	}
	
	function IsValidPassword(password)
	{
		var Valid = false;
		var LetterValue = OneIsLetter(password);
		var NumericValue = OneIsNumeric(password);
		
		if(LetterValue == true && NumericValue == true)
			Valid = true
		else
			Valid = false
			
		return Valid
	}
	
	function OneIsLetter(sText)
	{
		var ValidChars = "abcdefghijklmnopqrstuvwxyz.";
		var Char;
		
		for (i = 0; i < sText.length; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) != -1) 
				return true;
		}
		
		return false;
	}
	
	function OneIsNumeric(sText)
	{
		var ValidChars = "0123456789.";
		var Char;
	 
		for (i = 0; i < sText.length; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) != -1) 
				return true;
		}
		
		return false;
	}
	
	function AllIsLetter(sText)
	{
		var ValidChars = "abcdefghijklmnopqrstuvwxyz.";
		var IsChar = true;
		var Char;
		
		
		for (i = 0; i < sText.length && IsChar == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsChar = false;
			}
		}
		
		return IsChar;	
	}
	
	function AllIsNumeric(sText)
	{
		var ValidChars = "0123456789.";
		var IsNumber=true;
		var Char;
	 
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
			}
		}
		
		return IsNumber;	   
	}