
function showForm(country) {
	$('step1').style.display = 'none';
	$('step2').style.display = 'block';
}

/* validate */
window.addEvent("domready", function() {
	
	// required
	$$('.req').each(function(item, index){
		item.addEvent("blur", function() {
			var currLabel 	= item.getParent("fieldset").getElement("label[for='"+$(this).id+"']");
			if(item.value==null || item.value=="") {
				currLabel.addClass("labelerror");
			} else {
				currLabel.removeClass("labelerror");				
			}
		});
	});
	
	// firstname
	if('user_firstname') {
		$('user_firstname').addEvent("blur", function() {
			checkRequired($(this));
		});		
	}
	
	// lastname
	if('user_lastname') {
		$('user_lastname').addEvent("blur", function() {
			checkRequired($(this));
		});		
	}	
	
	// email
	if ($('user_email')) {
		$('user_email').addEvent("blur", function() {
			var currLabel = $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");
			if ( !validateEmail( $(this) ) ) {			
				$(currLabel).addClass("labelerror");
				$('information_user_email').addClass('error');
				$('information_user_email').innerHTML = "Ogiltig epostadress";
			} else {
				$(currLabel).removeClass("labelerror");
				$('information_user_email').removeClass('error');
				$('information_user_email').innerHTML = '<img src="img/gfx_load_pink.gif" alt="loading" width="12" height="12" />';
				(function(){doAjaxCallback("register", "ajax_task=email&email="+$('user_email').value, checkEmailPost)}).delay(1000);
			}
		});

		$('user_email').addEvent("click", function() {
			$('information_user_email').innerHTML = '';
		});
	}
	
	// email again
	if( $('user_email_again') ) {
		$('user_email_again').addEvent("blur", function() {
			var currLabel = $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");
			if( $(this).value != $('user_email').value || $(this).value == null || $(this).value == "" ) {
				currLabel.addClass("labelerror");
				$('information_user_email_again').addClass('error');
				$('information_user_email_again').innerHTML = "Upprepat email stämmer ej";
			} else {
				currLabel.removeClass("labelerror");
				$('information_user_email_again').removeClass('error');
				$('information_user_email_again').innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
			}			
		});
	}
	
	
	// username
	if( $('user_username')) {
		$('user_username').addEvent("blur", function() {
			var illegalChars = /\W/; // allow letters, numbers, and underscores
			var currLabel = $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");
			if( $(this).value.length < 5 ) {
				currLabel.addClass("labelerror");
				$('information_user_username').addClass('error');
				$('information_user_username').innerHTML = 'Måste vara minst 5 tecken.';
			} else if(illegalChars.test($(this).value)) {
				currLabel.addClass("labelerror");
				$('information_user_username').addClass('error');
				$('information_user_username').innerHTML = 'Only letters, numbers and underscore.';				
			} else {
				currLabel.removeClass("labelerror");
				$('information_user_username').innerHTML = '<img src="img/gfx_load_pink.gif" alt="loading" width="12" height="12" />';			
				(function(){doAjaxCallback("register", "ajax_task=username&username="+$('user_username').value, checkUsernamePost)}).delay(1000);		
			}
		});

		$('user_username').addEvent("click", function() {
			$('information_user_username').removeClass('error');
			$('information_user_username').innerHTML = 'Måste vara minst 5 tecken.';		
		});
	}
	
	// password
	if($('user_password'))
	$('user_password').addEvent("blur", function() {
		var currLabel = $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");
		if( $(this).value.length < 5 ) {
			currLabel.addClass("labelerror");
			$('information_user_password').addClass('error');
			$('information_user_password').innerHTML = "Måste vara minst 5 tecken.";
		} else {
			$('information_user_password').removeClass('error');
			currLabel.removeClass("labelerror");
			$('information_user_password').innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
		}
	});
	
	// password again
	if( $('user_password_again'))
	$('user_password_again').addEvent("blur", function() {
		var currLabel = $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");
		if( $(this).value != $('user_password').value || $(this).value.length < 5 || $(this).value == null || $(this).value == "" ) {
			currLabel.addClass("labelerror");
			$('information_user_password_again').addClass('error');
			$('information_user_password_again').innerHTML = "Upprepat lösenord stämmer ej";
		} else {
			currLabel.removeClass("labelerror");
			$('information_user_password_again').removeClass('error');
			$('information_user_password_again').innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
		}
	});
	
	// mobile
	if( $('user_mobile') ) {				
		$('user_mobile').addEvent("blur",function() {
			var currLabel = $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");			
			var legalChars = /^[\d\+\- ]+$/; // allow digits, +, - and 'space'
			if( !$(this).value) {
				currLabel.addClass("labelerror");
				$('information_user_mobile').addClass('error');
				$('information_user_mobile').innerHTML = "Måste fyllas i.";
			} else if(legalChars.test($(this).value)) {
				currLabel.removeClass("labelerror");
				$('information_user_mobile').removeClass('error');
				$('information_user_mobile').innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
			} else {
				currLabel.addClass("labelerror");
				$('information_user_mobile').addClass('error');
				$('information_user_mobile').innerHTML = "Får bara innehålla 0-9, +, - och mellanslag";
			}
		});
	}
	
	// blog name
	if( $('blog_name') ) {
		$('blog_name').addEvent("blur",function() {
			var currLabel 	= $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");	
			var currInfo	= $(this).getNext('div.information');
			if( !$(this).value ) {
				currLabel.addClass("labelerror");
				currInfo.addClass('error');
				currInfo.innerHTML = 'Måste fyllas i.';
			} else {
				currLabel.removeClass("labelerror");
				currInfo.removeClass('error');
				currInfo.innerHTML = '<img src="img/gfx_load_pink.gif" alt="loading" width="12" height="12" />';
				(function(){doAjaxCallback("register", "ajax_task=blog_name&blog_name="+$('blog_name').value, checkBlogNamePost)}).delay(500);
			}
		});
	}
	
	// blog_url
	if( $('blog_url') ) {
		$('blog_url').addEvent("blur", function() {
			var currLabel 	= $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");	
			var currInfo	= $(this).getNext('div.information');
			if( !$(this).value || $(this).value =='') {
				currLabel.addClass("labelerror");
				currInfo.addClass('error');
				currInfo.innerHTML = 'Måste fyllas i.';
			} else {				
				$(this).value = $(this).value.replace(/^\s*http:\/\//, '');	// no http:// in the beginning			
				currLabel.removeClass("labelerror");
				currInfo.removeClass('error');
				currInfo.innerHTML = '<img src="img/gfx_load_pink.gif" alt="loading" width="12" height="12" />';
				(function(){doAjaxCallback("register", "ajax_task=blog_url&blog_url="+$('blog_url').value, checkBlogUrlPost)}).delay(500);
			}
		});
	}
	// videofyme address
	if( $('blog_videofyme_address')) {
		// check that videofyme address is free
		$('blog_videofyme_address').addEvent("blur", function() {
			var currLabel 	= $(this).getParent("fieldset").getElement("label[for='"+$(this).id+"']");	
			var currInfo	= $(this).getNext('div.information');
			var legalChars = /^[\w\d\+\-]+$/; // allow chars, _, digits, + and -'
			if( !$(this).value || $(this).value == '' ) {
				currLabel.addClass("labelerror");
				currInfo.addClass('error');
				currInfo.innerHTML = 'Måste fyllas i.';
			} else if(!legalChars.test($(this).value)) {
				currLabel.addClass("labelerror");
				currInfo.addClass('error');
				currInfo.innerHTML = "Får bara innehålla a-z, 0-9, +, och -";
			} else {
				currLabel.removeClass("labelerror");
				currInfo.removeClass('error');
				currInfo.innerHTML = '<img src="img/gfx_load_pink.gif" alt="loading" width="12" height="12" />';
				(function(){doAjaxCallback("register", "ajax_task=videofyme_address&videofyme_address="+$('blog_videofyme_address').value, checkVideofymeAddressPost)}).delay(1000);							
			}
		});
	}
	
	// blog description
	if( $('blog_description') ) {
		$('blog_description').addEvent("blur", function() {
			var currLabel 	= $($(this)).getParent("fieldset").getElement("label[for='"+$(this).id+"']");	
			var currInfo	= $($(this)).getNext('div.information');
			
			if( !$(this) || $(this).value.length == 0 ) {
				currLabel.addClass("labelerror");
				currInfo.addClass('error');
				currInfo.innerHTML = 'Måste fyllas i.';
			} else {
				currLabel.removeClass("labelerror");
				currInfo.removeClass('error');
				currInfo.innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
			}			
		});
		$('blog_description').addEvent("keydown", function() {
			limitText($(this), 120);				
		});			
		$('blog_description').addEvent("keyup", function() {
			limitText($(this), 120);				
		});		
	}
	
	// blog categories
	// check them on load and add check event	
	countBlogCategories(true);	
	$$('.blog_categories').each(function(el){
		el.addEvent('click',function(){			
			countBlogCategories( false );
		});
	});
	
	// blog sex
	countBlogSex(true);	
	$$('.blog_sex').each(function(el){
		el.addEvent('click',function(){			
			countBlogAgeAndSex( false );
		});
	});
	
	// blog age
	countBlogAge(true);	
	$$('.blog_age').each(function(el){
		el.addEvent('click',function(){			
			countBlogAgeAndSex( false );
		});
	});
	
	// blog ad proposal
	if( $('blog_ad_proposal') ) {
		$('blog_ad_proposal').addEvent("keydown", function() {
			limitText($(this), 120);				
		});			
		$('blog_ad_proposal').addEvent("keyup", function() {
			limitText($(this), 120);				
		});		
	}
	
	if( $('submitregister') ) {
		$('submitregister').addEvent("click", function() {
			submitRegisterForm();
		});
	}
});

/*
function getCurrInfo( el ) {
	return el.getNext('div.information');
}

function getCurrLabel( el ) {	
	var currLabel =  el.getParent("fieldset").getElement("label[for='"+this.id+"']");
	return currLabel;
}
*/

function checkRequired( current ) {
	var currInfo 	= current.getNext('div.information');
	if( current.value == null || current.value == "" ) {
		currInfo.addClass("error");
		currInfo.innerHTML = "Måste fyllas i.";
	} else {
		currInfo.removeClass("error");
		currInfo.innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
	}
}

function countBlogCategories( init ) {
	var checked_blog_categories_total 	= 0;
	var max_checked 					= 3;
	$('information_blog_category').innerHTML = '';
	
	$$('.blog_categories').each(function(el){
		if( el.checked == true ) {
			checked_blog_categories_total++;			
		}
		
		// if more then max somehow are checked, uncheck them
		if(checked_blog_categories_total > max_checked) {
			el.checked = false;
		}
	});
	
	if(checked_blog_categories_total >= max_checked) {
		disableCheckBoxes( '.blog_categories' );
	} else if( checked_blog_categories_total < max_checked ) {
		enableCheckBoxes( '.blog_categories' );
	}
	
	// check that we have at least one check, skippet at onloadevent
	if( !init && !checked_blog_categories_total ) {
		$('label_blog_category').addClass('labelerror');
	} else {
		$('label_blog_category').removeClass('labelerror');
	}
}

function countBlogSex( init ) {
	var checked_blog_sex_total 	= 0;

	$$('.blog_sex').each(function(el){
		if( el.checked == true ) {
			checked_blog_sex_total++;			
		}
	});
	
	// check that we have at least one sex, skippet at onloadevent
	if( !init && !checked_blog_sex_total ) {
		return false;
	}
	return true;
}

function countBlogAge( init ) {
	var checked_blog_age_total 	= 0;
	var max_checked 			= 3;	
	
	$$('.blog_age').each(function(el){
		if( el.checked == true ) {
			checked_blog_age_total++;			
		}
		
		// if more then max somehow are checked, uncheck them
		if(checked_blog_age_total > max_checked) {
			el.checked = false;
		}
	});
	
	if(checked_blog_age_total >= max_checked) {
		disableCheckBoxes( '.blog_age');
	} else if( checked_blog_age_total < max_checked ) {
		enableCheckBoxes( '.blog_age' );
	}
	
	if( !init && !checked_blog_age_total ) {
		return false;
	}
	return true;
}
function countBlogAgeAndSex( init ) {
	var sexOk = countBlogSex( init );
	var ageOk = countBlogAge( init );
	
	if( !sexOk ) {
		$('label_blog_sex').addClass('labelerror');
		$('information_blog_sex').addClass('error');
		$('information_blog_sex').innerHTML = "Välj minst ett kön";
	} else if( !ageOk ) {
		$('label_blog_sex').addClass('labelerror');
		$('information_blog_sex').addClass('error');
		$('information_blog_sex').innerHTML = "Välj en eller flera åldersgrupper";
	} else {
		$('label_blog_sex').removeClass('labelerror');
		$('information_blog_sex').removeClass('error');
		$('information_blog_sex').innerHTML = '';
	}
}

function disableCheckBoxes( cb_class ) {
	$$(cb_class).each(function(el){
		if( el.checked == false ) {
			el.disabled = 'disabled';
		} else {
			el.disabled = '';
		}
	});	
}

function enableCheckBoxes( cb_class ) {
	$$(cb_class).each(function(el){		
			el.disabled = '';
	});	
}

function checkEmailPost( result ) {
	if( result.result > 0 ){
		$('information_user_email').removeClass('error');
		$('information_user_email').innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
	} else {
		$('information_user_email').addClass('error');
		$('information_user_email').innerHTML = result.msg;
	}
}

function checkUsernamePost( result ) {
	if( result.result > 0 ){
		$('label_user_username').removeClass('labelerror');
		$('information_user_username').removeClass('error');
		$('information_user_username').innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
	} else {
		$('label_user_username').addClass('labelerror');
		$('information_user_username').addClass('error');
		$('information_user_username').innerHTML = result.msg;
	}
}

function checkBlogNamePost( result ) {
	var currLabel 	= $('label_blog_name');
	var currInfo	= $('information_blog_name');
	
	if( result.result > 0 ){		
		currLabel.removeClass('labelerror');
		currInfo.removeClass('error');		
		currInfo.innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
		
	} else {
		currLabel.addClass('labelerror');
		currInfo.addClass('error');
		currInfo.innerHTML = result.msg;
	}	
}

function checkBlogUrlPost( result ) {
	var currLabel 	= $('label_blog_url');
	var currInfo	= $('information_blog_url');
	
	if( result.result > 0 ){		
		currLabel.removeClass('labelerror');
		currInfo.removeClass('error');		
		currInfo.innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
		
	} else {
		currLabel.addClass('labelerror');
		currInfo.addClass('error');
		currInfo.innerHTML = result.msg;
	}	
}

function checkVideofymeAddressPost( result ) {
	var currLabel 	= $('label_blog_videofyme_address');
	var currInfo	= $('information_blog_videofyme_address');
	
	if( result.result > 0 ){
		currLabel.removeClass('error');
		currInfo.removeClass('error');
		currInfo.innerHTML = '<img src="img/img_check.png" alt="loading" width="12" height="12" />';
	} else {
		currLabel.addClass('error');
		currInfo.addClass('error');
		currInfo.innerHTML = result.msg;
	}
}

function submitRegisterForm() {

	$('sent').value = 1;
	
	$('registerform').set('send', {
		onComplete: function(response) {
			var r = JSON.decode(response);
	
			
			if( r.result == 1 ) {
	
				$('registration').innerHTML = r.msg;
		
			} else {
			 
				var headline = 'Problem med ans&ouml;kan';
				var textmass = 'Du har inte fyllt i alla f&auml;lt korrekt.';
				
				/* var headline = 'Problem med ans&ouml;kan';
				var textmass = 'Du har inte fyllt i alla f&auml;lt korrekt.'; */
				var buttons = '<a class="printbutton" onclick="$(\'popin\').dispose();" href="javascript:;"><span>Ok</span></a>';
				doPopin(headline, textmass, buttons);
				
				for( var er in r.error ) {					
					$('label_'+er).addClass('labelerror');
					if( er == 'blog_age' ) {						
						$('information_blog_sex').addClass('error');					
						$('information_blog_sex').innerHTML = r.error[er];
					} else if( er != 'blog_category') {
						$('information_'+er).addClass('error');					
						$('information_'+er).innerHTML = r.error[er];
					}
				}
			}
		},
		onFailure: function(response) {
			alert('Fel i formul&auml;ret');
			//alert('Fel i formul&auml;ret');
		}
	});
		//Send the form.
	$('registerform').send();
	$('sent').value = 0;
}
