var REGISTER_CAPTCHA="_nc";
var REGISTER_TOKEN="";
var REGISTER_CHARGE="_cg";

function Login(isConsole)
{
	var m_isConsole=true;
	if (isConsole==false)
		m_isConsole=false;
	
	this.getUrl = function(url)
		{
			if (m_isConsole)
			{
				return url;
			}
			else if(url=="console")
			{
				return g_consoleURL;
			}
			else
			{
				return "../" + url;
			}
		}
		
	this.getSecureUrl = function(url)
		{
			if (m_isConsole)
			{
				return "secure/"+url;
			}
			else
				return url;
		}

	this.displayTables=new Array();
	this.displayTables[this.displayTables.length]="login_loginTbl";
	this.displayTables[this.displayTables.length]="login_successTbl";     
	this.displayTables[this.displayTables.length]="login_loginFailed";
	this.displayTables[this.displayTables.length]="login_registerTbl";
	this.displayTables[this.displayTables.length]="login_registeringUserTbl";
	this.displayTables[this.displayTables.length]="login_registerConfirmTbl";
	this.displayTables[this.displayTables.length]="login_registerForWaitlist";
	this.displayTables[this.displayTables.length]="login_logoutConfirmTbl";
	this.displayTables[this.displayTables.length]="login_registerTbl_customer";
	this.displayTables[this.displayTables.length]="login_registerTbl_nocustomer";
	this.displayTables[this.displayTables.length]="login_registerTbl_charge";

	this.hideDisplayTables = function()
		{
			for (var i=0; i<this.displayTables.length; i++)
			{
				var tbl=document.getElementById(this.displayTables[i]);
				if (tbl!=null)
					tbl.style.display="none";
			}
		}

	this.display = function()
		{
			this.loginReset();
		}
		
	this.loginCallback = function(xml)
		{
			var btn=document.getElementById("btnRegister");
			if (btn)
				btn.disabled=false;
			btn=document.getElementById("btnSubscribe");
			if (btn)
				btn.disabled=false;
			btn=document.getElementById("btnBillingNext");
			if (btn)
				btn.disabled=false;

			var result = "";
			try
			{
				result = _util.selectSingleNodeText(xml, "/result/code");
				if (result==null)
				{ alert("LoginCallback:  Invalid returned xml value:  "+xml.xml);
				  return;
				}
			}
			catch (e)
			{
				alert("LoginCallback:  Invalid returned xml value: '"+xml+"': "+e.message);
				return;
			}

			_login.hideDisplayTables();

			switch(result)
			{
			case "loginsuccess":   				// login success
				if (_console.bIsLockedDown==true)
				{
					var strURL=_login.getUrl("console");
					
					var oParams=_util.getURLParams();
					if (oParams && oParams.product!=null)
					    strURL += "?product="+oParams.product;

					window.location.href = strURL;
					return;
				}
				var strRefresh=_util.selectSingleNodeText(xml, "/result/data/refresh");
				if(strRefresh!=null && strRefresh.length>0)
				{
					window.location.href = _login.getUrl("console");
					return;
				}
				document.getElementById("login_successTbl").style.display = "";
				var strCurrentUser=_util.selectSingleNodeText(xml, "/result/data/name");
				if (strCurrentUser!=null && strCurrentUser!="")
				   document.getElementById("login_currentUser").innerHTML=getLocalizedString("welcome")+", "+strCurrentUser+"!";
				document.getElementById("console_btnLogin").style.display="none";
				document.getElementById("console_btnLogout").style.display="";
				
				__initComplete();
				break;
			case "loginexpired":	  // Trial period has expired
				_console.showComponent('Account');
				_account.hideDisplayTables();
				var tbl=document.getElementById("account_newSignupTbl");
				if (tbl!=null)
					tbl.style.display = "";
				else
				{	// Expired account, but inside the main console where there is no signup table--redirect to main loginform.jsp
					window.location.href = _login.getUrl("console");
				}
				break;
			case "loginfailed":					// login failure
				_console.showComponent("Login");
				document.getElementById("login_loginFailedMsg").innerHTML=getLocalizedString("invalid_password");
				document.getElementById("login_loginFailed").style.display = "";
			    _login.setFocusToLoginCtrls();
				break;
			case "logindisabled":
				_console.showComponent("Login");
				document.getElementById("login_loginFailedMsg").innerHTML=getLocalizedString("you_have_not_completed_registration")+" <a class='medium color1 bold' href='javascript:_account.sendActivation();_console.showComponent(\"Login\");_login.setFocusToLoginCtrls();_util.cancelEvt(window.event)'>"+getLocalizedString("click_here")+"</a> "+getLocalizedString("to_resend");
				document.getElementById("login_loginFailed").style.display = "";
			    _login.setFocusToLoginCtrls();
				break;
			case "loginlocked":
				_console.showComponent("Login");
				document.getElementById("login_loginFailedMsg").innerHTML=getLocalizedString("this_account_is_locked")+" <a class='medium color1 bold' href='javascript:_account.showUnlockAccount();_util.cancelEvt(window.event)'>"+getLocalizedString("click_here")+"</a> "+getLocalizedString("to_unlock");
				document.getElementById("login_loginFailed").style.display = "";
			    _login.setFocusToLoginCtrls();
				break;
			case "loginnotfound":
			case "loginnotfoundnoemail":
				_console.showComponent("Login");
				document.getElementById("login_loginFailedMsg").innerHTML=getLocalizedString("username_not_registered");
				document.getElementById("login_loginFailed").style.display = "";
			    _login.setFocusToLoginCtrls();
				break;
			case "logout":						// logout
				document.getElementById("login_currentUser").innerHTML="";
				document.getElementById("console_btnLogin").style.display="";
				document.getElementById("console_btnLogout").style.display="none";
				_console.showComponent('Login');
				document.getElementById("login_logoutConfirmTbl").style.display = "";			
				_login.setFocusToLoginCtrls();
				break;
			case "alreadyexists":			// Invalid user when creating
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				//if (g_CustomerType==false)
				//	validationinfo=document.getElementById("divLoginValidationInfo_nc");

				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("username_already_exists")+" <a class='medium color1 bold' href='javascript:void(0)' onclick=\"_account.showForgotPassword();_util.cancelEvt(event)\">"+getLocalizedString("click_here")+"</a> "+getLocalizedString("to_receive_an_email");
				validationinfo.style.display="";
				document.getElementById("txtUsername"+g_CustomerType).className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedaddressstate":      // This only occurs for non-customers
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("state_country_combination_invalid");
				validationinfo.style.display="";
				document.getElementById("txtState"+g_CustomerType).className="validation_error";
				document.getElementById("txtCountry"+g_CustomerType).className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedbillingaddressstate":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("state_country_combination_invalid");
				validationinfo.style.display="";
				document.getElementById("billing_txtState_cg").className="validation_error";
				document.getElementById("billing_txtCountry_cg").className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "cardinvalid":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("credit_card_invalid");
				validationinfo.style.display="";
				document.getElementById("billing_txtCCNumber_cg").className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "cardexpired":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("credit_card_expired");
				validationinfo.style.display="";
				document.getElementById("billing_txtCCNumber_cg").className="validation_error";
				document.getElementById("billing_txtCCExpMonth_cg").className="validation_error";
				document.getElementById("billing_txtCCExpYear_cg").className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedcaptcha":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				//if (g_CustomerType==false)
				//	validationinfo=document.getElementById("divLoginValidationInfo_nc");
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("please_type_the_characters");
				validationinfo.style.display="";
				document.getElementById("txtCaptcha_nc").className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedtoken":
				var validationinfo=document.getElementById("divLoginValidationInfo");
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("please_type_auth_code");
				validationinfo.style.display="";
				document.getElementById("txtToken1").className="validation_error";
				document.getElementById("txtToken2").className="validation_error";
				document.getElementById("txtToken3").className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedemail":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				//if (g_CustomerType==false)
				//	validationinfo=document.getElementById("divLoginValidationInfo_nc");

				validationinfo.innerHTML="The account activation email cannot be sent to this address.  Please verify it is a valid address, if you still have problems, contact support at: <a class='medium color1 bold' href='mailto:"+getLocalizedString("support_email_address")+"'>"+getLocalizedString("support_email_address")+"</a>.";
				validationinfo.style.display="";
				document.getElementById("txtEmail"+g_CustomerType).className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedage":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				//if (g_CustomerType==false)
				//	validationinfo=document.getElementById("divLoginValidationInfo_nc");

				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("you_must_be_of_age");
				validationinfo.style.display="";
				document.getElementById("selBirthMonth").className="validation_error";
				document.getElementById("selBirthDay").className="validation_error";
				document.getElementById("selBirthYear").className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "registered":					// registered new user
				var tbl=document.getElementById("login_registerTbl_MAIN");
				if (tbl)
					tbl.style.display="none";
				document.getElementById("login_registerEmail").innerHTML = document.getElementById("txtEmail"+g_CustomerType).value;
				document.getElementById("login_registerConfirmTbl").style.display = "";
				trackRegistration();
				break;
			case "subscribedsuccess":					// registered new user
				var tbl=document.getElementById("login_registerTbl_MAIN");
				if (tbl)
					tbl.style.display="none";
				// document.getElementById("login_subscribedEmail").innerHTML = document.getElementById("txtEmail"+g_CustomerType).value;
				document.getElementById("login_subscribedConfirmTbl").style.display = "";
				trackSubscription();
				break;
			case "failedpayment":
			case "faildedorder":		// CC or Paypal transaction failed
				var tbl=document.getElementById("login_registerTbl_MAIN");
				if (tbl)
					tbl.style.display="none";

				if (window.g_isLoggedIn==true)
					document.getElementById("confirmRegisteredAdditionalInfo").innerHTML="<b>"+getLocalizedString("payment_failed_msg_logged_in")+"</b>";
				else
					document.getElementById("confirmRegisteredAdditionalInfo").innerHTML="<b>"+getLocalizedString("payment_failed_msg")+"</b>";
				
				document.getElementById("confirmRegisteredMessage").style.display="none";

				var strEmail=document.getElementById("txtEmail"+g_CustomerType).value;
				if (strEmail=="")
					document.getElementById("confirmRegisteredMessage2").style.display="none";
				else
					document.getElementById("login_registerEmail").innerHTML = strEmail;

				document.getElementById("login_registerConfirmTbl").style.display = "";
				break;
			case "passwordsent":				// sent password info
				document.getElementById("login_forgotConfirmTbl").style.display = "";
				break;
			case "updatesuccess":				// changed password info
				document.getElementById("login_passwordConfirmTbl").style.display = "";
				break;
			case "revalidate":
				alert("Error:  Please validate that all values are correct before you continue.");
				// var args=_login.validateRegistrationArgs('');
				break;
			case "failedproductrequired":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+"ERROR:  failedproductrequired";
				validationinfo.style.display="";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedpaypalset":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+"ERROR:  failedpaypalset";
				validationinfo.style.display="";
				__validationComplete();
				_login.showRegistration();
				break;
			case "failedip":
				var validationinfo=document.getElementById("divLoginValidationInfo"+g_CustomerType);
				validationinfo.innerHTML="<b>"+getLocalizedString("validation_error")+"</b><br/>"+getLocalizedString("you_must_be_located_at_ip");
				validationinfo.style.display="";
				document.getElementById("billing_txtCity_cg").className="validation_error";
				document.getElementById("billing_txtState_cg").className="validation_error";
				__validationComplete();
				_login.showRegistration();
				break;
			case "paypalset":
				var btn=document.getElementById("btnBillingNext");
				if (btn)
					btn.disabled=true;

				var strURL=_util.selectSingleNodeText(xml, "/result/data/paypalurl");
				if (strURL!=null && strURL!="")
					window.location.href=strURL;
				break;
			case "tosno":
				document.getElementById("pane_TOS").style.display="";
				document.getElementById("tbl_tosNo").style.display="";
				break;
			case "tosshow":
				document.getElementById("pane_TOS").style.display="";
				document.getElementById("tbl_tosShow").style.display="";
				trackTOS();
				break;
			default:
				alert("Invalid Login Return Code:  '"+result+"'");
				break;
			}
		}

	this.login = function(bAgreeTOS)
		{
			var username = document.getElementById("login_username").value;
			var password = document.getElementById("login_password").value;
			// TODO: apply client side validation rules...
			
			_login.setCookieInfo();
			
			var args = new Object();
			args.username = _util.trim(username);
			args.password = password;

			if (bAgreeTOS!=null)
			{			
				if (bAgreeTOS==true)
					args.tos="yes";
				else if (bAgreeTOS==false)
					args.tos="no";
			}

			_console.getComponentContent(this.getSecureUrl("login"), args, this.loginCallback, true);
		}
		
	this.logout = function()
		{
			_console.getComponentContent(this.getUrl("servlets/logout"), null, this.loginCallback, true);
		}
		
	this.loginReset = function()
		{
			this.hideDisplayTables();
			document.getElementById("login_password").value="";
			document.getElementById("login_loginTbl").style.display = "";
		}

	this.showWaitlist = function()
		{
			this.hideDisplayTables();
			document.getElementById("login_registerForWaitlist").style.display = "";			
		}
		
	this.addToWaitlist = function()
		{
			var email = document.getElementById("login_waitlistEmail").value;
			email=_util.trim(email);
			var objRegExp  = /(^[a-zA-Z0-9]([a-zA-Z0-9_\-\$\~\.]*)@([a-zA-Z0-9_\-\.]*)([.][a-zA-Z]{3})$)|(^[a-zA-Z0-9]([a-zA-Z0-9_\.]*)@([a-zA-Z0-9_\-\.]*)(\.[a-zA-Z]{2,3})(\.[a-zA_Z]{2})*$)/i;
			var bValid=objRegExp.test(email);
			if (!bValid)
			{
				alert("Invalid email address.  Please enter a valid email address, such as:  user@mydomain.com");
				return;
			}
			var firstname = document.getElementById("login_waitlistFirstName").value;
			if (firstname=="")
			{
				alert("First Name is required.");
				return;
			}

			var lastname = document.getElementById("login_waitlistLastName").value;
			if (lastname=="")
			{
				alert("Last Name is required.");
				return;
			}

			var args = new Object();
			args.email = email;
			args.firstname = firstname;
			args.lastname = lastname;

			_console.getComponentContent(this.getUrl("servlets/waitlist"), args, this.addToWaitlistCallback, true);
		}
	
	this.addToWaitlistCallback = function()
		{
			alert("Thank you for your interest in Islanders TV.  A confirmation email will be sent to you shortly.");
			_console.showComponent("Login");
		}
		
	g_CustomerType=REGISTER_TOKEN;
	g_SHOWWAITLIST=false;
	this.showRegistration = function(custType)
		{
			if (custType==null)
				custType=g_CustomerType;
			else
				g_CustomerType=custType;

			this.hideDisplayTables();

			if (custType==REGISTER_TOKEN)
			{
				document.body.className="loginform";
				document.getElementById("login_registerTbl_customer").style.display="";
				if (document.getElementById("login_registerTbl_TD")!=null)
				{
					document.getElementById("login_registerTbl_TD").style.paddingLeft="200px";
					document.getElementById("login_registerTbl_TD").style.paddingTop="150px";
				}
			}
			else if (custType==REGISTER_CAPTCHA)
			{
				// Don't show CC section yet
				if (g_SHOWWAITLIST==true)
				{
					this.showWaitlist();
					return;
				}

				document.body.className="loginform_freetrial";
				document.getElementById("login_registerTbl_nocustomer").style.display="";
				if (document.getElementById("login_registerTbl_TD")!=null)
				{
					document.getElementById("login_registerTbl_TD").style.paddingLeft="80px";
					document.getElementById("login_registerTbl_TD").style.paddingTop="180px";
				}

				this.refreshCaptcha();
			}
			else  // if (custType==REGISTER_CHARGE)
			{
				document.body.className="loginform_freetrial";
				document.getElementById("login_registerTbl_charge").style.display="";
				if (document.getElementById("login_registerTbl_TD")!=null)
				{
					document.getElementById("login_registerTbl_TD").style.paddingTop="75px";
					document.getElementById("login_registerTbl_TD").style.paddingLeft="200px";
				}
				
			}
			
			document.getElementById("login_registerTbl").style.display = "";			
		}

	this.refreshCaptcha = function()
		{
			var img = document.getElementById("imgCaptcha");
			if(img!=null)
			{
				var d=new Date();
				img.src=this.getUrl("jcaptcha")+"?t="+d.getTime();
				document.getElementById("txtCaptcha_nc").value="";
			}
		}

	this.registerUser = function(tokenRequired)
		{
			var args=this.validateLoginArgs(tokenRequired);
			
			if (args==null)
			   return;

			switch(g_CustomerType)
			{
				case REGISTER_CAPTCHA:
					_console.getComponentContent(this.getSecureUrl("captcharegister"), args, this.loginCallback, true);
					break;
				case REGISTER_TOKEN:
					_console.getComponentContent(this.getSecureUrl("tokenregister"), args, this.loginCallback, true);
					break;
				case REGISTER_CHARGE:
					_console.getComponentContent(this.getSecureUrl("chargeregister"), args, this.loginCallback, true);
					break;
				default:
					alert("Error:  Unknown register type:  '"+g_CustomerType+"'");
			}
			
			document.getElementById("login_registeringUserTbl").style.display="";
		}

	this.onChangeCountry = function(listbox, stateRow)
	{
		if (stateRow==null)
			stateRow="trState";

		var phoneExampleUS=document.getElementById("phoneExampleUS");
		var phoneExampleNonUS=document.getElementById("phoneExampleNonUS");
			
		if (listbox.value=="US" || listbox.value=="CA")
		{
			document.getElementById(stateRow).style.display="";
			if (phoneExampleUS!=null)
			{
				phoneExampleUS.style.display="";
				phoneExampleNonUS.style.display="none";
			}
		}
		else
		{
			document.getElementById(stateRow).style.display="none";
			if (phoneExampleUS!=null)
			{
				phoneExampleUS.style.display="none";
				phoneExampleNonUS.style.display="";
			}
		}
	}
	this.validateLoginArgs = function(tokenRequired)	
		{
			// The validation fields for the items end with _nc if it is not a customer
			//var strCust="";
			//if (g_CustomerType==false)
			//	strCust="_nc";
			strCust=g_CustomerType;
				
			var username=document.getElementById("txtUsername"+strCust);
			var email=document.getElementById("txtEmail"+strCust);
			var firstname=document.getElementById("txtFirstName"+strCust);
			var lastname=document.getElementById("txtLastName"+strCust);
			var password=document.getElementById("txtPassword"+strCust);
			var verify=document.getElementById("txtVerify"+strCust);
			var captcha=document.getElementById("txtCaptcha"+strCust);
			var token1=document.getElementById("txtToken1"+strCust);
			var token2=document.getElementById("txtToken2"+strCust);
			var token3=document.getElementById("txtToken3"+strCust);
			var cbTermsOfUse=document.getElementById("cbTermsOfUse"+strCust);
			// Only if Customer is false
			var address1=document.getElementById("txtAddressLine1"+strCust);
			var address2=document.getElementById("txtAddressLine2"+strCust);
			var city=document.getElementById("txtCity"+strCust);
			var state=document.getElementById("txtState"+strCust);
			var zip=document.getElementById("txtZip"+strCust);
			var country=document.getElementById("txtCountry"+strCust);
			var phone=document.getElementById("txtPhone"+strCust);
			var locale=document.getElementById("txtLocale"+strCust);

			var validationinfo=document.getElementById("divLoginValidationInfo"+strCust);
			var arrErrors=new Array();

			var bRequiredFieldMissing=false;
						
			username.className="input";
			email.className="input";
			firstname.className="input";
			lastname.className="input";
			password.className="input";
			verify.className="input";
			if(captcha!=null)
				captcha.className="input";
			if(token1!=null)
			{
				token1.className="input";
				token2.className="input";
				token3.className="input";
			}

			if ((g_CustomerType==REGISTER_CAPTCHA || g_CustomerType==REGISTER_CHARGE) && address1!=null)
			{
				address1.className="input";
				address2.className="input";
				city.className="input";
				state.className="input";
				zip.className="input";
				country.className="input";
				phone.className="input";
				if (locale!=null)
					locale.className="input";
			}

			validationinfo.style.display="none";
			
			if (username.value=="")
			{
				// arrErrors[arrErrors.length]="* Username is required.";
				bRequiredFieldMissing=true;
				username.className="validation_error";
			}
			else if (user.name.value.length<4 || username.length>32)
			{
				arrErrors[arrErrors.length]=getLocalizedString("username_min_max");;
				username.className="validation_error";
			}
			else
			{
				var objRegExp  = /[a-zA-Z0-9]/i;
				var bValid=objRegExp.test(username.value);
				if (!bValid)
				{
					arrErrors[arrErrors.length]=getLocalizedString("enter_valid_username");
					username.className="validation_error";
				}
			}
			if (email.value=="")
			{
				// arrErrors[arrErrors.length]="* Email Address is required.";
				bRequiredFieldMissing=true;
				email.className="validation_error";
			}
			else if (email.value.indexOf("@")<0 || email.value.indexOf(".")<0)
			{
				arrErrors[arrErrors.length]=getLocalizedString("enter_valid_email");
				email.className="validation_error";
			}
			else if (email.value.length>128)
			{
				arrErrors[arrErrors.length]=getLocalizedString("email_max_len");
				email.className="validation_error";
			}
			else
			{
				var objRegExp  = /(^[a-zA-Z0-9]([a-zA-Z0-9_\.]*)@([a-zA-Z0-9_\-\.]*)([.][a-zA-Z]{3})$)|(^[a-zA-Z0-9]([a-zA-Z0-9_\.]*)@([a-zA-Z0-9_\-\.]*)(\.[a-zA-Z]{2,3})(\.[a-zA_Z]{2})*$)/i;
				var bValid=objRegExp.test(email.value);
				if (!bValid)
				{
					arrErrors[arrErrors.length]=getLocalizedString("enter_valid_email");
					email.className="validation_error";
				}
			}
			if (firstname.value=="")
			{
				// arrErrors[arrErrors.length]="* First Name is required.";
				bRequiredFieldMissing=true;
				firstname.className="validation_error";
			}
			if (lastname.value=="")
			{
				// arrErrors[arrErrors.length]="* Last Name is required.";
				bRequiredFieldMissing=true;
				lastname.className="validation_error";
			}
			if (password.value=="")
			{
				// arrErrors[arrErrors.length]="* Password is required.";
				bRequiredFieldMissing=true;
				password.className="validation_error";
				verify.className="validation_error";
			}
			else if (password.value!=verify.value)
			{
				arrErrors[arrErrors.length]=getLocalizedString("password_confirm_do_not_match");
				password.className="validation_error";
				verify.className="validation_error";
			}

			if (g_CustomerType==REGISTER_CAPTCHA && captcha!=null && captcha.value=="")
			{
				arrErrors[arrErrors.length]=getLocalizedString("please_type_the_characters");
				captcha.className="validation_error";
			}

			if (cbTermsOfUse!=null)
			{
				if (cbTermsOfUse.checked==false)
					arrErrors[arrErrors.length]=getLocalizedString("you_must_accept_terms");
			}
			if(tokenRequired && token1!=null)
			{
				if (token1.value.length!=4 || token2.value.length!=4 || token3.value.length!=4)
				{
					arrErrors[arrErrors.length]="* Please enter a valid authorization code (XXXX-XXXX-XXXX).";
					token1.className="validation_error";
					token2.className="validation_error";
					token3.className="validation_error";
				}
			}

			if ((g_CustomerType==REGISTER_CAPTCHA || g_CustomerType==REGISTER_CHARGE) && address1!=null)
			{
				if (address1.value=="")
				{
					// arrErrors[arrErrors.length]="* Address is required.";
					bRequiredFieldMissing=true;
					address1.className="validation_error";
				}
				if (city.value=="")
				{
					// arrErrors[arrErrors.length]="* City is required.";
					bRequiredFieldMissing=true;
					city.className="validation_error";
				}
				if (country.value=="US" || country.value=="CA")
				{
					if (state.value=="")
					{
						// arrErrors[arrErrors.length]="* State is required.";
						bRequiredFieldMissing=true;
						state.className="validation_error";
					}
				}
				if (zip.value=="")
				{
					// arrErrors[arrErrors.length]="* Zip is required.";
					bRequiredFieldMissing=true;
					zip.className="validation_error";
				}
				if (country.value=="")
				{
					// arrErrors[arrErrors.length]="* Country is required.";
					bRequiredFieldMissing=true;
					country.className="validation_error";
				}

				phone.value=_util.trim(phone.value);
				var bValid=objRegExp.test(phone.value);
				if (bValid)
					phone.value=phone.value.substring(0,3)+"-"+phone.value.substring(3,6)+"-"+phone.value.substring(6);

				if (phone.value=="")
				{
					// arrErrors[arrErrors.length]="* Phone Number is required.";
					bRequiredFieldMissing=true;
					phone.className="validation_error";
				}
				else if (country.value=="US" || country.value=="CA")
				{
					var objRegExp = /^[a-zA-Z0-9]{3}-[a-zA-Z0-9]{3}-[a-zA-Z0-9]{4}$/i;
					var bValid=objRegExp.test(phone.value);
					if (!bValid)
					{
						arrErrors[arrErrors.length]=getLocalizedString("enter_valid_phone");
						phone.className="validation_error";
					}
				}
				else // Non-US Phone Number
				{
					var objRegExp = /(^[0-9]{1,3}-[0-9]{3,5}-[a-zA-Z0-9]{1,10}$)|(^[0-9]{1,3}-[a-zA-Z0-9]{1,10}$)/i;
					var bValid=objRegExp.test(phone.value);
					if (!bValid)
					{
						arrErrors[arrErrors.length]=getLocalizedString("enter_valid_phone_non_us");
						phone.className="validation_error";
					}
				}
			}

			var billing_txtFullName=document.getElementById("billing_txtFullName_cg");
			var billing_txtAddressLine1=document.getElementById("billing_txtAddressLine1_cg");
			var billing_txtAddressLine2=document.getElementById("billing_txtAddressLine2_cg");
			var billing_txtCity=document.getElementById("billing_txtCity_cg");
			var billing_txtState=document.getElementById("billing_txtState_cg");
			var billing_txtZip=document.getElementById("billing_txtZip_cg");
			var billing_txtCountry=document.getElementById("billing_txtCountry_cg");
			
			var billing_txtCCType=document.getElementById("billing_txtCCType_cg");
			var billing_txtCCNumber=document.getElementById("billing_txtCCNumber_cg");
			var billing_txtCCExpMonth=document.getElementById("billing_txtCCExpMonth_cg");
			var billing_txtCCExpYear=document.getElementById("billing_txtCCExpYear_cg");

			var rbs=this.getChargeTypeRadios();
			for (var i=0; i<rbs.length; i++)
				rbs[i].parentNode.style.backgroundColor="";

			if (g_CustomerType==REGISTER_CHARGE)
			{
				billing_txtFullName.className="input";
				billing_txtAddressLine1.className="input";
				billing_txtAddressLine2.className="input";
				billing_txtCity.className="input";
				billing_txtState.className="input";
				billing_txtZip.className="input";

				billing_txtCCType.className="input";
				billing_txtCCNumber.className="input";
				billing_txtCCExpMonth.className="input";
				billing_txtCCExpYear.className="input";

				if (this.getCurrentChargeType()==null)
				{
					arrErrors[arrErrors.length]=getLocalizedString("you_must_choose_a_charge_type");
					var rbs=this.getChargeTypeRadios();
					for (var i=0; i<rbs.length; i++)
						rbs[i].parentNode.style.backgroundColor="red";
				}
				
				if (this.getCurrentChargeType()==1)   // Monthly requires CC
				{
					if (billing_txtFullName.value=="")
					{
						// arrErrors[arrErrors.length]=getLocalizedString("full_name_required");
						bRequiredFieldMissing=true;
						billing_txtFullName.className="validation_error";
					}
					if (billing_txtAddressLine1.value=="")
					{
						// arrErrors[arrErrors.length]=getLocalizedString("address1_required");
						bRequiredFieldMissing=true;
						billing_txtAddressLine1.className="validation_error";
					}
					if (billing_txtCity.value=="")
					{
						// arrErrors[arrErrors.length]=getLocalizedString("city_required");
						bRequiredFieldMissing=true;
						billing_txtCity.className="validation_error";
					}
					if (billing_txtCountry.value=="US" || billing_txtCountry.value=="CA")
					{
						if (billing_txtState.value=="")
						{
							// arrErrors[arrErrors.length]=getLocalizedString("state_required");
							bRequiredFieldMissing=true;
							billing_txtState.className="validation_error";
						}
					}
					if (billing_txtZip.value=="")
					{
						// arrErrors[arrErrors.length]=getLocalizedString("zip_required");
						bRequiredFieldMissing=true;
						billing_txtZip.className="validation_error";
					}
					if (billing_txtCCNumber.value=="")
					{
						// arrErrors[arrErrors.length]=getLocalizedString("cc_number_required");
						bRequiredFieldMissing=true;
						billing_txtCCNumber.className="validation_error";
					}
					if (billing_txtCCExpMonth.value=="")
					{
						// arrErrors[arrErrors.length]=getLocalizedString("cc_expmonth_required");
						bRequiredFieldMissing=true;
						billing_txtCCExpMonth.className="validation_error";
					}
					if (billing_txtCCExpYear.value=="")
					{
						// arrErrors[arrErrors.length]=getLocalizedString("cc_expyear_required");
						bRequiredFieldMissing=true;
						billing_txtCCExpYear.className="validation_error";
					}
				}			
			}
			
			if (arrErrors.length>0 || bRequiredFieldMissing==true)
			{
				var strMsg="<b>"+getLocalizedString("validation_error")+"</b><br/>";
				if (bRequiredFieldMissing==true)       // No room for all messages at once...
				   strMsg+=getLocalizedString("all_fields_required_validation")+"<br/>";
				strMsg+=arrErrors.join("<br>");
				validationinfo.innerHTML=strMsg;
				validationinfo.style.display="";
				__validationComplete();
				return null;
			}
			else
			{
				var args=new Object();
				args.username=username.value;
				args.email=email.value;
				args.firstname=firstname.value;
				args.lastname=lastname.value;
				args.password=password.value;
				if(captcha!=null)
				{
					captcha.value=captcha.value.toUpperCase();
					args.captcha=captcha.value;
				}
				if(token1!=null)
				{
					token1.value=token1.value.toUpperCase();
					token2.value=token2.value.toUpperCase();
					token3.value=token3.value.toUpperCase();
					args.token=token1.value+"-"+token2.value+"-"+token3.value;
				}

				if ((g_CustomerType==REGISTER_CAPTCHA || g_CustomerType==REGISTER_CHARGE) && address1!=null)
				{
					args.address1=address1.value;
					args.address2=address2.value;
					args.city=city.value;
					if (country.value=="US" || country.value=="CA")
						args.state=state.value;
					else
						args.state="";
					args.zip=zip.value;
					args.country=country.value;
					args.phone=phone.value;
				}

				if (g_CustomerType==REGISTER_CHARGE)
				{  
					args.chargetype=this.getCurrentChargeType();
					
					args.cardholder=billing_txtFullName.value;
					args.billing_address1=billing_txtAddressLine1.value;
					args.billing_address2=billing_txtAddressLine2.value;
					args.billing_city=billing_txtCity.value;
					args.billing_state=billing_txtState.value;
					args.billing_zip=billing_txtZip.value;
					args.billing_country=billing_txtCountry.value;
		
					args.cardtype=billing_txtCCType.value;
					args.cardnumber=billing_txtCCNumber.value;
					args.cardexpmonth=billing_txtCCExpMonth.value;
					args.cardexpyear=billing_txtCCExpYear.value;
				}

				if (locale!=null)
					args.locale=locale.value;

				return args;
			}

		}

	this.getChargeTypeRadios = function()
		{
			var rbs=new Array();
			
			var i=0;
			var rb=document.getElementById("rbChargeType_"+i);
			while (rb!=null)
			{
				rbs[rbs.length]=rb;
				i++;
				rb=document.getElementById("rbChargeType_"+i);
			}
			
			return rbs;
		}

	this.getCurrentChargeType = function()
		{
			var iChargeType=null;
			var rbs=this.getChargeTypeRadios();
			for (var i=0; i<rbs.length; i++)
			{
				if (rbs[i].checked==true)
				{
					iChargeType=i;
					break;
				}
			}
			return iChargeType;
		}
		
	this.setFocusToLoginCtrls = function()
		{
		    if (document.getElementById("login_username").value.length==0)
		       _util.setFocusToCtrl("login_username");
		    else
		       _util.setFocusToCtrl("login_password");		
		}
		
	this.getCookieInfo = function()
		{
		   if (document.cookie!=null && document.cookie!='')
		   {
		      var the_cookie = document.cookie;
		      var crumbs = the_cookie.split(";");
		      for(var i=0; i<crumbs.length;i++)
		      {
		         curCrumb = crumbs[i].split("=");
		         if (curCrumb[0].indexOf("UserName") != -1)   // Cookie was found
		         { 
		            if (unescape(curCrumb[1])!="undefined")			
		            {
		               document.getElementById("login_username").value = unescape(curCrumb[1]);
		               document.getElementById("cbUseCookie").checked=true;
		               break;
		            }
		         }
		      }
		    }
		
		    _login.setFocusToLoginCtrls();
		}
		
	this.setCookieInfo = function()
		{
			if(document.getElementById("cbUseCookie").checked)
			{
				var oDate = new Date();
				oDate.setDate(oDate.getDate() + 90);		
				oDate = oDate.toGMTString();

				try		// Set the cookie with the current user name 
				{
					document.cookie = "UserName=" + escape(document.getElementById("login_username").value) + ";expires=" + oDate;
				}
				catch (e)
				{
				}
			  }
			  else
			  { 
				try		// Delete the cookie's user name
			    {
					document.cookie = "UserName=" + escape(document.getElementById("login_username").value) + ";expires=Fri, 13-Apr-1970 00:00:00 GMT";
				}
				catch (e)
				{
				}
			}
		}
	
	this.showWhatsThis = function()
		{
			alert(getLocalizedString("whats_this_alert"));
		}
}
