onDomReady(function () {
	// init
	registration.initialize();
});


var registration = {
	newsLetter:true,
    initialize: function(){
		//initialize the light box - enter code
		if($("codePanel"))
			registration.codeLB = new Lightbox($("codePanel"),{opacity:0.5});

		//initialize the light box - send reminder
		if($("sendCodePanel"))
			registration.sendLB = new Lightbox($("sendCodePanel"),{opacity:0.5});


		//if registration form was submitted - show the code light box
		if(flix.getItemFromQueryString("showLB"))
			registration.codeLB.show();

		//initialize the light box - movie details
		if($("formValidationErrors"))
			registration.validationErrorsLB=new Lightbox($("formValidationErrorsReg"),{opacity:0.4});

        //initialize the light box - movie details
        if($("formEmailSent"))
            registration.emailSentLB=new Lightbox($("formEmailSent"),{opacity:0.4});

		// select USA as default
		var options = $("Country").options;
		for (var i=0;i<options.length;i++) {
			if (options[i].value == 219) {
				$("Country").selectedIndex = i;
				return;
			}
		}
         

	},
	showHide: function(a,b){
		if(a)	a.hide();
		if(b)	b.show();
	},
	checkAvailability: function() {
		this.lastCheckedNickname = $("nick").value;
		if ($("nick").value.length < 4) {
			$("availableIcon").src = "/images/registration/invalid_icon.gif";
			$("availableText").innerHTML = "Not available";
			$("availableIcon").style.display = "";
			return;
		}
		flix.showReloader('Loading...','load');
		var updater = new Ajax("/registration/CheckForUserName?userName="+$("nick").value, {method: 'get', onComplete:function(){
			flix.hideReloader();
			if (this.transport.responseXML.documentElement.getAttribute("available") == "false") {
				$("availableIcon").src = "/images/registration/invalid_icon.gif";
				$("availableText").innerHTML = "Not available";
			} else {
				$("availableIcon").src = "/images/registration/valid_icon.gif";
				$("availableText").innerHTML = "Available";
			}
			$("availableIcon").style.display = "";
		}}).request();
	},
	CheckRegStep1: function(){
		var nick = new String(document.forms["RegisterStep1"].nick.value);
		var Email = new String(document.forms["RegisterStep1"].Email.value);
		var Password = new String(document.forms["RegisterStep1"].Password.value);
		var rePassword = new String(document.forms["RegisterStep1"].rePassword.value);
		var Country = new String(document.forms["RegisterStep1"].Country.selectedValue);
		var Captcha = new String(document.forms["RegisterStep1"].Captcha.value);
		var errorsList = "<ul>";
		
		if(nick.length < 6 || nick.length > 20)
		{
			errorsList += "<li>Username should be 6-20 characters.</li>";
        	//errorsList += "</ul>";
            //$("errors").innerHTML = errorsList;
            //registration.validationErrorsLB.show();
            //return false;
		}

		if(Password.length < 6 || Password.length > 10)
		{
			errorsList += "<li>Password should be 6-10 characters.</li>";
            //errorsList += "</ul>";
            //$("errors").innerHTML = errorsList;
            //registration.validationErrorsLB.show();
            //return false;
		}   

		if(!flix.ValidEmail(Email.toLowerCase()))
		{
			errorsList += "<li>The e-mail is not valid.</li>";
			//errorsList += "</ul>";
			//$("errors").innerHTML = errorsList;
			//registration.validationErrorsLB.show();
			//return false;
		}

		if(Password.toLowerCase()!=rePassword.toLowerCase() )
		{
			errorsList += "<li>Please re-type the password.</li>";
            //errorsList += "</ul>";
			document.forms["RegisterStep1"].rePassword.value="";
            //$("errors").innerHTML = errorsList;
            //registration.validationErrorsLB.show();
            //return false;
		}
		if(Captcha.length == 0 )
		{
			errorsList += "<li>Please type the verification code.</li>";
            //errorsList += "</ul>";
            //$("errors").innerHTML = errorsList;
            //registration.validationErrorsLB.show();
            //return false;
		}

		if(errorsList != "<ul>")
        {
			$("errorsReg").innerHTML = errorsList;
			registration.validationErrorsLB.show();
			return false;
		}

		flix.showReloader('Loading...','load');
		var newsLetter = (registration.newsLetter == true)?"true":"false"; 
        var poster = new Ajax("/registration/Register?userName="+nick+"&email="+Email+"&password="+Password+"&countryId="+$("Country").options[$("Country").selectedIndex].value+"&captchaCode="+Captcha+"&newsLetter="+newsLetter, {method:'get', onComplete:function() {
			flix.hideReloader();
            
            
            
            try
            {
               var success = this.transport.responseXML.documentElement.getElementsByTagName("Success")[0].firstChild.nodeValue;
                if (success == "false")
                {
                    var reason = this.transport.responseXML.documentElement.getElementsByTagName("FailText")[0].firstChild.nodeValue;
                    $("errorsReg").innerHTML = "<ul><li>"+reason+"</li></ul>";
                    registration.validationErrorsLB.show();
                }
                else
                {
                    //var verification = this.transport.responseXML.documentElement.getElementsByTagName("Verification")[0].firstChild.nodeValue;
                    //if (verification == "true") document.location.replace("ConfirmationWasSent");
                    //else
                     document.location.replace("/registration/EmailSent");
                     //document.location.replace("/pickphone/index");
                }
            
            }
            catch(err)
            {
                 document.location.replace("/registration/EmailSent");
            
            }
            
            }}).request();

	},
	revertToDefault: function(input) {
		if (input.value.replace(/ /g, "") == "" || input.value == input.defaultValue) {
			input.value = input.defaultValue;
			input.className = "defaultEmail";
		}
	},
	changeEmailValue: function(input) {
		if (input.value.replace(/ /g, "") == "" || input.value == input.defaultValue) {
			input.className = "defaultEmail";
		} else {
			input.className = "";
		}
	},
    next: function() {
      document.location.replace("/pickphone/index");  
    },
    checkBoxClick: function()
    {
       registration.newsLetter = !registration.newsLetter; 
       //debugger;
    
    },
	lastCheckedNickname:"",
	resetAvailability: function() {
		if ($("nick").value != registration.lastCheckedNickname) {
			$("availableText").innerHTML = "Check availability";
			$("availableIcon").style.display = "none";
		} else if (registration.lastCheckedNickname != "") {
			$("availableText").innerHTML = $("availableIcon").src.match(/invalid_icon/i) ? "Not available" : "Available";
			$("availableIcon").style.display = "";
		}
	}
};
