$(function(){
	$("#product-brief-registration #other-services").click(function(){
		if($("[name='other-details']").attr("disabled")){
			$("[name='other-details']").removeAttr("disabled");
		}else{
			$("[name='other-details']").attr("disabled","disabled").val("");
		}
	});
	
	// Validation formulaire d'enregistrement pour accès aux product briefs
	$("#product-brief-registration form").not("#update-account, #modify-email, #modify-phone, #modify-password").submit(function(){
		$(".error").remove();
		$(".errorField").removeClass("errorField");
		var errorsString = new Array();
		var errorsElems = new Array();
		
		if($("[name='email']").val().trim() == ''){
			errorsString.push('Please enter your email.');
			errorsElems.push($("[name='email']"));
		}else if(!testEmail($("[name='email']").val().trim())){
			errorsString.push('Invalid email format.');
			errorsElems.push($("[name='email']"));
		}
		if($("[name='phone']").val().replace(" ", "").trim() == ''){
			errorsString.push('Please enter your phone number.');
			errorsElems.push($("[name='phone']"));
		}else if(!testPhone($("[name='phone']").val().replace(" ", "").trim())){
			errorsString.push('Invalid phone number format.');
			errorsElems.push($("[name='phone']"));
		}
		if($("[name='first-name']").val().replace(" ", "").trim().length < 3){
			errorsString.push('Please enter your first name.');
			errorsElems.push($("[name='first-name']"));
		}
		if($("[name='last-name']").val().replace(" ", "").trim().length < 3){
			errorsString.push('Please enter your last name.');
			errorsElems.push($("[name='last-name']"));
		}
		if($("[name='company-name']").val().replace(" ", "").trim().length < 2){
			errorsString.push('Please enter company name.');
			errorsElems.push($("[name='company-name']"));
		}
		if($("[name='company-url']").val().replace(" ", "").trim().length < 12){
			errorsString.push('Please enter company website URL.');
			errorsElems.push($("[name='company-url']"));
		}
		if($("[name='position-in-company']").val().replace(" ", "").trim().length == 0){
			errorsString.push('Please enter position in company.');
			errorsElems.push($("[name='position-in-company']"));
		}
		if($("#other-services").attr("checked") == true && $("[name='other-details']").val().replace(" ", "").trim().length == 0){
			errorsString.push('Please enter area of interest.');
			errorsElems.push($("[name='other-details']"));
		}
		if($(".checkboxContainer :checkbox:checked").length == 0){
			errorsString.push('Please specify other area of interest.');
			errorsElems.push($(".checkboxContainer"));
		}
		if($("[name='how-info']").val().replace(" ", "").trim().length < 3){
			errorsString.push('Please specify how you heard about us.');
			errorsElems.push($("[name='how-info']"));
		}
		if($("[name='access-conditions']").attr("checked") != true){
			errorsString.push('You must accept the terms of use in order to register.');
			errorsElems.push($("[name='access-conditions']"));
		}
		for (x in errorsString){
			errorsElems[x].closest('td').addClass("errorField");
			errorsElems[x].parent().append('<span class="error">' + errorsString[x] + '</span>');
		}

		return errorsString.length == 0;
	});
	
	$("#product-brief-registration #update-account").submit(function(){
		$(".error").remove();
		$(".errorField").removeClass("errorField");
		var errorsString = new Array();
		var errorsElems = new Array();
		var updateAccount = $(this);
		
		if($("[name='first-name']", updateAccount).val().replace(" ", "").trim().length < 3){
			errorsString.push('You forgot to specify your first name.');
			errorsElems.push($("[name='first-name']"));
		}
		if($("[name='last-name']", updateAccount).val().replace(" ", "").trim().length < 3){
			errorsString.push('You forgot to specify your last name.');
			errorsElems.push($("[name='last-name']"));
		}
		if($("[name='company-name']", updateAccount).val().replace(" ", "").trim().length < 2){
			errorsString.push('You forgot to specify the company name.');
			errorsElems.push($("[name='company-name']"));
		}
		if($("[name='company-url']", updateAccount).val().replace(" ", "").trim().length < 12){
			errorsString.push('You forgot to specify your company website URL.');
			errorsElems.push($("[name='company-url']"));
		}
		if($("[name='position-in-company']", updateAccount).val().replace(" ", "").trim().length == 0){
			errorsString.push('You forgot to specify your position in the company.');
			errorsElems.push($("[name='position-in-company']"));
		}
		if($("#other-services", updateAccount).attr("checked") == true && $("[name='other-details']", updateAccount).val().replace(" ", "").trim().length == 0){
			errorsString.push('You forgot to specify what other Buzzinbees services you are interested in.');
			errorsElems.push($("[name='other-details']"));
		}
		if($(":checkbox:checked", updateAccount).length == 0){
			errorsString.push('You have to choose at least one area you are most interested in.');
			errorsElems.push($(".checkboxContainer"));
		}
		if($("[name='how-info']", updateAccount).val().replace(" ", "").trim().length < 3){
			errorsString.push('You forgot to specify how you heard about Buzzinbees.');
			errorsElems.push($("[name='how-info']"));
		}
		for (x in errorsString){
			errorsElems[x].closest('td').addClass("errorField");
			errorsElems[x].parent().append('<span class="error">' + errorsString[x] + '</span>');
		}

		return errorsString.length == 0;
	});
	
	$("#product-brief-registration #modify-email").submit(function(){
		$(".error").remove();
		$(".errorField").removeClass("errorField");
		var errorsString = new Array();
		var errorsElems = new Array();
		
		if(!testEmail($("[name='new-email']").val().trim())){
			errorsString.push('You forgot to specify your email or it was not well formated.');
			errorsElems.push($("[name='new-email']"));
		}
		for (x in errorsString){
			errorsElems[x].closest('td').addClass("errorField");
			errorsElems[x].parent().append('<span class="error">' + errorsString[x] + '</span>');
		}

		return errorsString.length == 0;
	});
	
	$("#product-brief-registration #modify-phone").submit(function(){
		$(".error").remove();
		$(".errorField").removeClass("errorField");
		var errorsString = new Array();
		var errorsElems = new Array();
		
		if(!testPhone($("[name='new-phone']").val().replace(" ", "").trim())){
			errorsString.push('You forgot to specify your phone number or it was not well formated.');
			errorsElems.push($("[name='new-phone']"));
		}
		for (x in errorsString){
			errorsElems[x].closest('td').addClass("errorField");
			errorsElems[x].parent().append('<span class="error">' + errorsString[x] + '</span>');
		}

		return errorsString.length == 0;
	});
	
	$("#product-brief-registration #modify-password").submit(function(){
		$(".error").remove();
		$(".errorField").removeClass("errorField");
		var errorsString = new Array();
		var errorsElems = new Array();
		
		if($("[name='password']").val().replace(" ", "").trim().length == 0){
			errorsString.push('Please enter your current password.');
			errorsElems.push($("[name='password']"));
		}else if($("[name='password']").val().replace(" ", "").trim().length < 6){
			errorsString.push('Password must be at least 6 characters long.');
			errorsElems.push($("[name='password']"));
		}
		
		if($("[name='new-password']").val().replace(" ", "").trim().length == 0){
			errorsString.push('Please enter your new password.');
			errorsElems.push($("[name='new-password']"));
		}else if($("[name='new-password']").val().replace(" ", "").trim().length < 6){
			errorsString.push('Password must be at least 6 characters long.');
			errorsElems.push($("[name='new-password']"));
		}
		
		if($("[name='new-password-2']").val().replace(" ", "").trim().length == 0){
			errorsString.push('Please enter your new password verification.');
			errorsElems.push($("[name='new-password-2']"));
		}else if($("[name='new-password-2']").val().replace(" ", "").trim().length < 6){
			errorsString.push('Password must be at least 6 characters long.');
			errorsElems.push($("[name='new-password-2']"));
		}
		
		if($("[name='new-password']").val() != $("[name='new-password-2']").val()){
			errorsString.push('New password and verification don\'t match.');
			errorsElems.push($("[name='new-password-2']"));
		}
		
		for (x in errorsString){
			errorsElems[x].closest('td').addClass("errorField");
			errorsElems[x].parent().append('<span class="error">' + errorsString[x] + '</span>');
		}

		return errorsString.length == 0;
	});
	
	$(".form-information span").hide();
	$("div.form-information").click(function(){
		$(this).children().toggle();
	});
	
	$("#lost-password-switch").click(function(){
		$(".error").remove();
		$(".errorField").removeClass("errorField");
		$(".lost-password-for").slideToggle("fast");
	});
	
	$("#lost-password").submit(function(){
		$("[name='product-brief_lost']").val($("[name='product-brief_lost']").val().split(' ').join('').split('-').join('').split('(').join('').split(')').join(''));
		if(testEmail($("[name='product-brief_lost']").val())){
			$("[name='lost-type']").val("email");
		}else if(testPhone($("[name='product-brief_lost']").val())){
			$("[name='lost-type']").val("phone");
		}else{
			alert("The input field doesn't seems to match a correct email or phone format. Please try again.");
			return false;
		}
	});
	
	$(".new-page").attr("target","_blank");
	$("#left-nav  li:only-child").removeClass("top-border");
});

function testEmail(email){
	// Expression régulière sur la norme RFC2822 utilisée dans le module Perl Mail::RFC822::Address
	var emailFilter = /^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/;
	return emailFilter.test(email);
}

function testPhone(phone){
	// Expression régulière sur la norme E164 inspirée du post http://stackoverflow.com/questions/2113908/what-regular-expression-will-match-valid-international-phone-numbers
	var phoneFilter = /^\+(999|998|997|996|995|994|993|992|991|990|979|978|977|976|975|974|973|972|971|970|969|968|967|966|965|964|963|962|961|960|899|898|897|896|895|894|893|892|891|890|889|888|887|886|885|884|883|882|881|880|879|878|877|876|875|874|873|872|871|870|859|858|857|856|855|854|853|852|851|850|839|838|837|836|835|834|833|832|831|830|809|808|807|806|805|804|803|802|801|800|699|698|697|696|695|694|693|692|691|690|689|688|687|686|685|684|683|682|681|680|679|678|677|676|675|674|673|672|671|670|599|598|597|596|595|594|593|592|591|590|509|508|507|506|505|504|503|502|501|500|429|428|427|426|425|424|423|422|421|420|389|388|387|386|385|384|383|382|381|380|379|378|377|376|375|374|373|372|371|370|359|358|357|356|355|354|353|352|351|350|299|298|297|296|295|294|293|292|291|290|289|288|287|286|285|284|283|282|281|280|269|268|267|266|265|264|263|262|261|260|259|258|257|256|255|254|253|252|251|250|249|248|247|246|245|244|243|242|241|240|239|238|237|236|235|234|233|232|231|230|229|228|227|226|225|224|223|222|221|220|219|218|217|216|215|214|213|212|211|210|98|95|94|93|92|91|90|86|84|82|81|66|65|64|63|62|61|60|58|57|56|55|54|53|52|51|49|48|47|46|45|44|43|41|40|39|36|34|33|32|31|30|27|20|7|1)([0-9]{0,14})$/;
	return phoneFilter.test(phone);
}

function showError(elem){
	
}

function a(){
	alert('a');
}
