// JavaScript Document
$(document).ready(function(){
	main.init();
})


//-------------------------------------------------------------------[ main
	var main = {
		
		init:function(){
			user.init();
		//	form.validateInit();
		//	form.registrationEvents();
			form.sweepsInit();
		//	form.sweepsLoginVerifier();
		}
		
	}
	
//..........................................................................[ form

	var form = {
		
		mandatoryAuth:null,
		
		setMandatoryAuth:function(value){this.mandatoryAuth = value;},
		getMandatoryAuth:function(){return this.mandatoryAuth;},
		

		sweepsInit:function(){
			
			var _this = this;
			
			$(".sweepsSubmitButton").click(function () { user.startRegistrationProcess("sweepstakes"); });
			

			_this.fieldEventsInit();
			_this.capitalizeEvent();
			_this.lowercaseEvent();
			_this.phoneFormatEvent();
		},
		

		
		validateInit:function(){
			//Adding special methods to validate
			jQuery.validator.addMethod(
				"specialChars", 
				function(value, element, params) { 
					return (element.value.indexOf(' ') == -1) && (element.value.match(/([a-zA-Z0-9~!@#%&_"';:\.*\-\.?\.\\.{\.}\.[\]\.(\.)\.^\.$\./])$/)) ; 
				}, 
				"This field contains not allowed chars"
			);
		},
		
		capitalizeEvent:function(){
            $(".noCapsLock").keyup(function (){
            	var str = this.value;
            	var newstr=str.charAt(0).toUpperCase();
            	
            	for(var i=1 ; i<str.length ;i++){
            		if(str.charAt(i-1)==" "){
            			aux=str.charAt(i).toUpperCase();
            		}
            		else{
            			aux=str.charAt(i).toLowerCase();
            		}
                    newstr = newstr+aux;
            	}
            	this.value=newstr;
        	});
        },
        
        lowercaseEvent:function(){
            $(".lowercase").keyup(function (){
            	this.value = this.value.toLowerCase();
        	});
        },
        
        phoneFormatEvent:function(){
            $(".phone").keyup(function(event){
                str = this.value;
                newstr = "";
                for(var i=0; i<str.length; i++){
                    switch(i){
                        case 0:{
                            if((str.charAt(i)!="(") && (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57)){
                                newstr += "("+str.charAt(i);
                            }
                            else {newstr += "(";}
                        }break;
                        case 4:{
                            if((str.charAt(i)!=")") && (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57)){
                                newstr += ")"+str.charAt(i);}
                            else {newstr += ")";}
                        }break;
                        case 8:{
                            if((str.charAt(i)!="-") && (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57)){
                                newstr += "-"+str.charAt(i);}
                            else {newstr += "-";}
                        }break;
                        case 13:{
                            if((str.charAt(i)!="x") && (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57)){
                                newstr += "x"+str.charAt(i);}
                            else {newstr += "x";}
                        }break;
                        default:{
                            if(str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57){
                                newstr += str.charAt(i);
                            }
                        }
                    }
                }
                this.value = newstr;
            });
        },
fieldEventsInit:function(){
			//Hides and shows the zipcode according to the selected country
			//Hides and shows the state according to the selected country
			if($("#country").val()=="65"){
				$("#zipcodeWrapper").slideDown();
				$("#stateWrapper").slideDown();
			}
			else {
				$("#zipcodeWrapper").hide();
				$("#stateWrapper").slideUp();
			}
			
			$("#country").change(function () {
				if($("#country").val()==65){
					$("#zipcodeWrapper").slideDown();
					$("#stateWrapper").slideDown();
				}
				else {
					$("#zipcodeWrapper").hide();
					$("#stateWrapper").slideUp();
				}
			});	

	}
      
		
	}

//..........................................................................[ user

	var user = {
		
		regLevel:null,
		hasDemographics:0,
		linkLevel:0,
		methodStatus:null,
		
		init:function(){
			
			var _this = this;
			
			nav = user.getNavigator();

		},
		

		setLinkLevel:function(value){this.linkLevel = value;},
		getLinkLevel:function(){return this.linkLevel;},
		

		
		startRegistrationProcess:function(linkLev){
			
			var _this = this;
			_this.setLinkLevel(linkLev);
			
			switch(_this.getLinkLevel()){
				case "sweepstakes":{
                    
                    //$(".error").width("290px");
                    //$(".error").css("margin-bottom", "5px");
                    $("input .error").css("width", "160px");

					form.setMandatoryAuth(false);
					//var _data1 = null;
					
					$("#sweepsRegForm").validate({

						rules: {
							salutation: {
								required: true
							},
							firstname: {
								required: true
							},
							lastname: {
								required: true
							},
							retypepassword: {
								required: true,
								rangelength: [6, 16],
								equalTo: "#password"
							},
							email: {
								required: true,
								email: true/*,
								remote: uFormsRoot+"/registration/validEmail/email/"+$("#email").val()*/
							},
							confirmemail: {
								required: true,
								email: true,
								equalTo: "#email"
							},
							country: {
								required: true
							},
							terms: {
								required: true
							},
							authLev: {
								required: function() { return form.getMandatoryAuth() ? true: false;}
							},
							zipcode: {
								required: function() { return $("#country").val()==65 ? true: false;},
								number: function() { return $("#country").val()==65 ? true: false;},
								rangelength: [5, 5]
							},
							homephone: {
								required: true,
								rangelength: [13, 24]
							},
							mobilephone: {
								rangelength: [13, 24]
							},
							photoCountry: {
								required: true
							},
							photoDescription: {
								required: true
							},
							photoTitle: {
								required: true
							},
							imagefile: {
								required: true
							},
							nickname: {
								required: true
							},
							city: {
								required: true
							},
							state: {
								required: function() { return $("#country").val()==65 ? true: false;}
							}
						},
						messages: {
							salutation: {
								required: "Debes indicar un saludo."
							},
							firstname: {
								required: "Escribe tu nombre."
							},
							lastname: {
								required: "Escribe tu apellido."
							},
							email: {
								required: "Ingresa tu direcci&oacute;n de email.",
								email: "El email debe ser verdadero."/*,
								remote: "Esta direcci&oacute;n de correo ya existe"*/
							},
							confirmemail: {
								required: "Ingresa de nuevo tu direcci&oacute;n de email.",
								email: "El email debe ser verdadero.",
								equalTo: "La direcci&oacute;n de email debe ser igual a la anterior."
							},
							country: {
								required: "Escribe el nombre del pa&iacute;s donde vives."
							},
							terms: {
								required: "Debes estar de acuerdo con los T&eacute;rminos y Condiciones."
							},
							photoCountry: {
								required: "Escribe pa&iacute;s."
							},
							photoDescription: {
								required: "Escribe descriptci&oacute;n."
							},
							photoTitle: {
								required: "Escribe titulo de foto."
							},
							imagefile: {
								required: "Sube la imagen."
							},
							authLev: {
								required: "Debes seleccionar al menos una opcion"
							},
							zipcode: {
								required: "Escribe tu c&oacute;digo postal.",
								number: "El c&oacute;digo postal debe ser v&aacute;lido.",
								rangelength: "El c&oacute;digo postal debe contener 5 d&iacute;gitos."
							},
							homephone: {
								required: "Ingresa tu n&uacute;mero telef&oacute;nico.",
								rangelength: "El n&uacute;mero telef&oacute;nico debe contener de 10 a 20 d&iacute;gitos."
							},
							mobilephone: {
								rangelength: "El n&uacute;mero telef&oacute;nico debe contener de 10 a 20 d&iacute;gitos."
							},
							nickname: {
								required: "Escribe el apodo."
							},
							city: {
								required: "Escribe la ciudad en donde vives."
							},
							state: {
								required: "Escribe el nombre del estado donde vives."
							}
						},
						debug:false
					});
					
				}break;
				default:{
					cookie.set("linkAction", "");
				}break;
			}
		},
		
		getNavigator:function(){
			//returns the navigator's name
			if (navigator.appName.indexOf("Microsoft Internet Explorer") != -1) {
				if (navigator.appVersion.indexOf("MSIE 6.0") > -1){
						return "MSIE6";
				}
				else {
					return "MSIE7";
				}
			}
			else{
				if (navigator.userAgent.indexOf("Safari") == -1){
					return "Firefox";
				}
				else{
					return "Safari";
				}
			}
		}
		
	}