
$(document).ready(function(){
	
	var transitionTime = 300;
	var options = {
			
		     errorContainer : "#msg-box",
		     errorLabelContainer : "#msg-box ul",
		     wrapper : "li",
		     submitHandler : function(form){

				return false;
			
		     },
		     showErrors: function(errorMap, errorList) {
		     	 if(errorList.length) {
					$('#msg-box').show(transitionTime);
					$('.register-form').removeClass('center').addClass('right');
				 }
			     this.defaultShowErrors();
				 $('.error').each(function(index) {
						$wrapper = $(this).parents('.inputwrapper');
						if($wrapper.size() > 0) {
							$wrapper.addClass('error');
							$(this).removeClass('error');
						}
				 });
		     },
			 unhighlight: function( element, errorClass, validClass ) {
				 $(element).removeClass(errorClass).addClass(validClass);
		     	 $wrapper = $(element).parents('.inputwrapper');
				 if($wrapper.size() > 0) {
					$wrapper.removeClass(errorClass).addClass(validClass);
				 }
			 },
		     messages :{
		    	 required: "",
		    	 nome : "name",
		    	 cognome : "surname",
		    	 mail : "email",
		    	 sesso: "gender",
		    	 statoCod: "country",
		    	 provinciaCod : "state",
		    	 accettoPrivacy: "terms of agreement"
		     }
		}
	
	//$('#registerform').validate(options);
	
	$('#registerform').submit(function(e){
		e.preventDefault();
		var data = $(this).serialize();
		var action = $(this).attr('action');

		
		if($(this).validate(options).form()){
			$.ajax({
				url : action,
				type: "POST",
				data: data,
				success: function(msg){
					var resp = $.xml2json(msg);
					if('OK' == resp.result){
						var message = '<p>Thank you.<br />Your registration request has been sent successfully.</p><p>Shortly you will recieve an e-mail. Follow the link on the e-mail to complete the registration process</p><p><a href="http://www.hoganrebel.com"> go to the homepage &raquo;</a></p>';
					} else if('KO' == resp.result && '04' == resp.error) {
						var message = '<p>Registration Failed.</p><p>This email address has already been registered.</p>';					
					} else {
						var message = '<p>Registration Failed.</p><p>An unknown system error occured.</p><p>Please try again later.</p>';
					}
					$('#msg-box div').empty();
					$('#msg-box div').prepend(message);
					$('#msg-box').show(transitionTime);
					$('.register-form').removeClass('center').addClass('right');
				}
			});
			
		}
	});
	
	
	$('#close-errors').click(function(){
		$('#msg-box').hide(transitionTime);
		$('.register-form').removeClass('right').addClass('center');
	})
	
	var currentValue = "";
	$('#countrySelect').blur(function(){
		var val= $('#countrySelect').val();
		if(currentValue != val){
			currentValue = val;
			$.ajax({
				type: "GET",
				url: "../countries/index.php",
				data :"country="+escape(currentValue),
				success : function(html){
					$('#stateSelect').empty();
					$('#stateSelect').prepend(html);
				}
			});
		}
	});
});









