// JavaScript Document
//add faq to the patient
$(document).ready(function() {
var loginError = "";

//load countries
$('#loadingBarCountries').show();
$.post('../../../ajax/load/countries.php'
		,{
		
		  'id':null
		}
		,function(returnData)
		{
		
			if(returnData)
			{
				$('#loadingBarCountries').hide();
				$('#countries').html(returnData);
			}
			else
			{	
			
				
			}
			
		}
		,'text');	
		
function valAllowEmpty(value,title)
{
	//doesnt allow empty	
	if(value == '')
	{
		loginError += "<li>- " + title + "</li>";
	}
}
function addErrorMessage(value)
{
	//doesnt allow empty	
	loginError += "<li>- " + value + "</li>";
	
}
function valEmail(value)
{
	if(value != "")
	{
		AtPos = value.indexOf("@");
		StopPos = value.lastIndexOf(".");
		
		if (AtPos == -1 || StopPos == -1) {
			loginError += "<li>- Not a valid email address</li>";
		}
	}
}
function minValue(value,minVal,title)
{
	if(value.length < minVal)
	{
		loginError += "<li>- "+ title + " cannot be less than "+ minVal +" characters</li>";
	}
}
$("input[name='submit']").click(function() {	
	loginError = "";
	$('.errorLabel').empty();
	
	var interestTrainingArray = Array();
	$("input[name='trainingInterest[]']:checked").each(function()
	  {
		  interestTrainingArray.push($(this).val());
		  
	  });
	var interest = interestTrainingArray.join('|');
	
	
		
	valAllowEmpty($('#fname').val(),$('#fname').attr('title'));
	valAllowEmpty($('#lname').val(),$('#lname').attr('title'));
	valAllowEmpty($('#email').val(),$('#email').attr('title'));
	valAllowEmpty($('#pass').val(),$('#pass').attr('title'));
	valAllowEmpty($('#tel').val(),$('#tel').attr('title'));
	valAllowEmpty($('#address').val(),$('#address').attr('title'));
	valEmail($('#email').val());
	
	

	if($('#pass').val() != "")
	{
		
		minValue($('#pass').val(),8,"Password");
		
			if($('#cpass').val() == "")
			{
				valAllowEmpty($('#cpass').val(),$('#cpass').attr('title'));
			}
			else
			{
				if($('#pass').val() != $('#cpass').val())
				{
					addErrorMessage("Passwords didn't match");
				}
			}
	}
	
	if($("select[@id='country'] option:selected").attr('id') == 0)
	{
		addErrorMessage("Please select a Country of Practice");		
	}

	
	if(loginError != "")
	{
		$('.errorLabel').append(loginError);
		$('.errorLabel').show();
	}
	else
	{
		$('#loadingBar').show();
		$('.errorLabel').hide();
		
		var receiveInfo = 0;
		var trainingInterest = 0;
	
		if($("input[name='receiveInfo']:checked").val() != undefined)
		{
			receiveInfo = 1;
		}
/*		if($("input[name='trainingInterest']:checked").val() != undefined)
		{
			trainingInterest = 1;
		}
	*/
		
		$.post('../../../ajax/register/surgeons.php'
		,{
		
		  'fname':$('#fname').val()
		  ,'lname':$('#lname').val()
		  ,'email':$('#email').val()
		  ,'password':$('#pass').val()
		 , 'country_practiceID':$("select[@id='country'] option:selected").attr('id')
		 , 'practiceAddress':$('#address').val()
		 , 'telephone':$('#tel').val()
		 , 'receiveInfo':receiveInfo
		  ,'trainingInterest':interest.toString()
		}
		,function(returnData)
		{
		
			if(returnData)
			{
				$('.errorLabel').show();
				
				$('.errorLabel').append('<p>Once your application has been received, we will contact you for further information. <br/><br/>Your application will be then processed by Orteq and you will be able to access to the surgical area of this site.  In the surgical area you will find detailed information on Actifit&trade;, the procedure, results, and be able to join the discussion forums with your colleagues and peers.</p>');
				
				
				
				$('#loadingBar').hide();
				$('#continueLogin').show();
				$('#formFields').hide();
				
			}
			else
			{	$('.errorLabel').show();
				$('.errorLabel').append(returnData);
			
				$('#loadingBar').hide();
				$('#continueLogin').hide();
				$('#formFields').show();
				
			}
			
		}
		,'text');	
	}
	});
});