if(!com) var com={};
if(!com.gogii) com.gogii={};
if(!com.gogii.registration) com.gogii.registration={};
com.gogii.Registration = function() {
	this.isFormValid = false;
	this.userNameAvailable = false;
	this.emailAvailable = false;
	this.passwordValid = false;
	this.cpasswordValid = false;
	this.zipValid = false;
	this.bdayValid = true;
	this.genderValid = true;
	this.unameValid = false;

	 this.setAllToFalse = function() {
       this.isFormValid = false;
		this.userNameAvailable = false;
		this.emailAvailable = false;
		this.passwordValid = false;
		this.cpasswordValid = false;
		this.zipValid = false;
		this.bdayValid = false;
		this.genderValid = false;
		this.unameValid = false;
        return;
    }

	this.valid = function() {
		if (this.userNameAvailable && this.emailAvailable && this.passwordValid && this.cpasswordValid && this.bdayValid && this.zipValid) {
			$('user.username').value = $('user.username').value.trim();
            return true;
		}

		if(!this.userNameAvailable){
			if($('user.username').value == ""){
				cgr.procValidationErr($('tipUsernameTxt'), "username not available.");
                $('user.username').value = $('user.username').value.trim();
				reg.userNameAvailable = false;
			}
		}

		if(!this.emailAvailable){
			cgr.isEmailAvailable('/gogii/application_CheckEmailAvailability.html?ajax=true', $('user.email'));
		}

		if(!this.passwordValid){
			if($('user.password').value == "" || $('user.password').value.length < 6 || $('user.password').value.length >12) {
				cgr.procValidationErr($('tipPasswordTxt'), "Invalid Password.");
				reg.passwordValid = false;
			}
		}

		if(!this.cpasswordValid){
			if($('user.confirmPassword').value == ""){
				cgr.procValidationErr($('tipPasswordTxt'), "Password do not match.");
				reg.cpasswordValid = false;
			}
		}
		if($('user.password').value == $('user.confirmPassword').value && $('user.password').value != "" && $('user.password').value.length >= 6 && $('user.password').value.length <= 12){
			reg.cpasswordValid = true;
			reg.passwordValid = true;
			$('tipPasswordTxt').className = "noErrNrmTxt";
			$('tipPasswordTxt').innerHTML = "Password confirmed";
		}else{
			cgr.procValidationErr($('tipPasswordTxt'), "Password do not match.");
			reg.cpasswordValid = false;
			reg.passwordValid = false;
		}

		if(!this.zipValid){
			if($('signUpDto.memberAddress.zip').value == "" || isValidZip($('signUpDto.memberAddress.zip'))){
				cgr.procValidationErr($('tipZipTxt'), "Invalid zip code");
	            reg.zipValid = false;
			}
		}
		return false;
	}
}
var reg = new com.gogii.Registration();
com.gogii.registration = {
	alertHello: function(){
		alert('hello');
	},
	unameMsgShow: function() {
		$('tipUsername').show();
		$('tipExtra').show();
	},
	txHide: function() {
		$('tipExtra').hide();
	},
	teShow: function() {
		$('tipExtra').hide();
		$('tipEmail').show();
	},
	tpShow: function() {
		$('tipExtra').hide();
		$('tipPassword').show();
	},
	tcpShow: function() {
		if (reg.cpasswordValid && reg.passwordValid) {
			return;
		}
		$('tipExtra').hide();
		$('tipPassword').show();
		if (reg.passwordValid) {
			$('tipPasswordTxt').className = "infoTxt nrmTxt";
			$('tipPasswordTxt').innerHTML = "Retype your password";
		} else {
			cgr.procValidationErr($('tipPasswordTxt'), "Invalid password");
			reg.cpasswordValid = false;
		}
	},
	tpZipShow: function() {
		$('tipZip').show();
	},
	tpBdayShow: function() {
		$('tipBday').show();
	},
	tpGenderShow: function() {
		$('tipGender').show();
	},
	insertLoading: function(elem, tip, className) {
		elem.innerHTML ="";
		var loadingSpan = document.createElement('span');
		loadingSpan.className = className;
		loadingSpan.innerHTML = tip;
		elem.appendChild(loadingSpan);
	},
	isValid: function() {
		return reg.valid();
	},
	setAllToFalse: function() {
        reg.setAllToFalse();
    },
	isUnameAvailable: function(url, elem) {
		if (elem.value.length < 4 || elem.value.length > 12) {
			reg.unameValid = false;
			cgr.procValidationErr ($('tipUsernameTxt'), "Username must be 4-12 chars");
			return false;
		}
		$('tipUsernameTxt').className = "infoTxt nrmTxt";
		cgr.insertLoading($('tipUsernameTxt'), "Checking availability...", "loading");
		var params = elem.name + "=" + elem.value;
		new Ajax.Request (url, {
			method : 'post',
			parameters : params,
			onSuccess: function(transport) {
				cgr.processInfo(transport.responseText, $('tipUsernameTxt'));
				var output = transport.responseText.evalJSON();
				if (output.result == "success") {
					reg.userNameAvailable = true;
                    $('user.username').value = $('user.username').value.trim();
				} else {
					reg.userNameAvailable = false;
                    $('user.username').value = $('user.username').value.trim();
				}
				cgr.showEmailConfirmation();
				return true;
			}
		});
	},
	showEmailConfirmation: function(){
		//this is to show available confirmation for email, if email is already populated.
		if($('user.email').readOnly){
			$('tipEmail').show();
		}
	},
	emailIsValid: function(){
		reg.emailAvailable = true;
		},
	isEmailAvailable: function(url, elem) {
		if(!validate_email(elem.value)) {
			cgr.procValidationErr($('tipEmailTxt'), "Invalid email address");
			reg.emailAvailable = false;
			return false;
		}
		$('tipEmailTxt').className = "infoTxt nrmTxt";
		cgr.insertLoading($('tipEmailTxt'), "Checking availability...", "loading");
		var params = elem.name + "=" + elem.value;
		new Ajax.Request (url, {
			method : 'post',
			parameters : params,
			onSuccess: function(transport) {
				cgr.processInfo(transport.responseText, $('tipEmailTxt'));
				var output = transport.responseText.evalJSON();
				if (output.result == "success") {
					reg.emailAvailable = true;
				} else {
					reg.emailAvailable = false;
				}
				return true;
			}
		});
	},
	isValidPassword: function(elem, elem1) {
		if (elem.value == "" || elem.value.length < 6 || elem.value.length >12) {
			cgr.procValidationErr($('tipPasswordTxt'), "Invalid password");
			reg.passwordValid = false;
		} else {
			reg.passwordValid = true;
		}

		if(elem1.value == ""){
			$('tipPasswordTxt').className = "infoTxt nrmTxt";
			$('tipPasswordTxt').innerHTML = "Retype your password";
			return;
		}

		if(elem.value == elem1.value){
			reg.cpasswordValid = true;
			reg.passwordValid = true;
			$('tipPasswordTxt').className = "noErrNrmTxt";
			$('tipPasswordTxt').innerHTML = "Password confirmed";
		}else{
			cgr.procValidationErr($('tipPasswordTxt'), "Password do not match.");
			reg.cpasswordValid = false;
			reg.passwordValid = false;
		}
	},
	isValidConfirmPassword: function(elem, elem1) {
		if (elem.value != elem1.value) {
			cgr.procValidationErr($('tipPasswordTxt'), "Passwords don't match");
			reg.cpasswordValid = false;
			reg.passwordValid = false;
		} else {
			reg.cpasswordValid = true;
			reg.passwordValid = true;
			$('tipPasswordTxt').className = "noErrNrmTxt";
			$('tipPasswordTxt').innerHTML = "Password confirmed";
		}
	},
	isValidZip: function(elem) {

		var zipCodeValue = elem.value;
        digits="0123456789"

        var invalidZip = false;
        if(zipCodeValue != null) {
            for(i=0; i<5; i++) {
                if (digits.indexOf(zipCodeValue.charAt(i)) < 0) {
                    invalidZip = true;
                    break;
                }
            }
        }
        if(elem == null || zipCodeValue == null || zipCodeValue.length != 5 || invalidZip) {
            cgr.procValidationErr($('tipZipTxt'), "Invalid zip code");
            reg.zipValid = false;
        } else {
            reg.zipValid = true;
			$('tipZipTxt').className = "noErrNrmTxt";
			$('tipZipTxt').innerHTML = "Zip code confirmed";
        }
	},
	isValidBday: function(elem) {
		/* the minumum age you want to allow in */
		var min_age = 13;

		var year = parseInt($('dob_year').value);
		var month = parseInt($('dob_month').value) - 1;
		var day = parseInt($('dob_day').value);

		var theirDate = new Date((year + min_age), month, day);
		var today = new Date();

		if ((today.getTime() - theirDate.getTime())< 0 ) {
			cgr.procValidationErr($('tipBdayTxt'),"You must be at least 13 to Join");
            reg.bdayValid = false;
		}
		else {
			reg.bdayValid = true;
            $('tipBdayTxt').className = "noErrNrmTxt";
            $('tipBdayTxt').innerHTML = "Birth date confirmed";
		}
	},
	isValidGender: function(elem) {
		$('tipGenderTxt').className = "noErrNrmTxt";
		$('tipGenderTxt').innerHTML = "Gender confirmed";
	},
	processInfo: function(response, elem) {
		var jsonResult = response.evalJSON();
		if (jsonResult.result == "success") {
			elem.innerHTML = jsonResult.message;
			elem.className = "noErrNrmTxt";
		} else {
			cgr.procValidationErr (elem, jsonResult.message);
		}
	},
	procValidationErr: function(targetElem, msg) {
		targetElem.innerHTML = msg;
		targetElem.className = "errNrmTxt";
	}
}
cgr = com.gogii.registration;
