$(document).ready(function(){
	$('li.page-item-3 a:first').click(function(){return false;});
	$('li.page-item-5 a:first').click(function(){return false;});
	$('li.page-item-50 a:first').click(function(){return false;});
	
	$("a.lightbox-link").fancybox({
		'hideOnContentClick': true
		});
});

function checkEmail(email)
{
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/;
	return email.match(regex)	?	true	:	false;
}

function sendContactForm(theme_path)
{
	var errors = new Array();
	
	var name	= $('#name').val();
	var phone	= $('#phone').val();
	var email	= $('#email').val();
	var message = $('#message').val();
	
	if (name.length == 0) {
		errors[errors.length] = 'Name is required field';
	}
	if (email.length == 0) {
		errors[errors.length] = 'Email address is required field';
	}
	else if (!checkEmail(email)) {
		errors[errors.length] = 'Email address is not in valid format';
	}
	
	if (message.length == 0) {
		errors[errors.length] = 'Message is required field';
	}
	
	if (errors.length != 0) {
		var message = '';
		for (var i =0; i < errors.length; i++)
		{
			message += errors[i] + '\n';
		}
		alert(message);
		return false;
	}
	
	/**
	 * All checkings are ok, proceed to the ajax call
	 */
	var ajax_options = {
			beforeSend: function()
			{
				$('#ajax-throbber').css('display', 'inline');
				$('#submit_button').attr('disabled', 'disabled');
			},
			complete: function()
			{
				$('#ajax-throbber').css('display', 'none');
				//$('#submit_button').attr('disabled', '');
				$('input[type=text]').val('');
                                $('#message').val('');
				$('li.buttons').css('display', 'none');
				$('li.required-fileds-text').css('display', 'none');
				$('li.status-message').slideDown();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert('There was an error durring request. Please try again later!');
			},
			success: function(html, textStatus)
			{
				return false;
			},
			timeout: '10000',
			type: 'POST',
			dataType: 'html',
			data: 	'form=contact' + 
					'&name=' + name + 
					'&email=' + email + 
					'&phone=' + phone +
					'&message='	+ message
			,
			url: theme_path + '/ajax-send-form.php'
		};
	$.ajax(ajax_options);
	return false;
}

function sendPrescriptionForm(theme_path)
{
	var errors = new Array();
	
	var name		= $('#name').val();
	var email		= $('#email').val();
	var birth		= $('#birth').val();
	var phone		= $('#phone').val();
	var pharmacy	= $('#pharmacy').val();
	var medication	= $('#medication').val();
	var message 	= $('#message').val();
	
	if (name.length == 0) {
		errors[errors.length] = 'Name is required field';
	}
	
	if (email.length == 0) {
		errors[errors.length] = 'Email address is required field';
	}
	else if (!checkEmail(email)) {
		errors[errors.length] = 'Email address is not in valid format';
	}
	
	if (birth.length == 0) {
		errors[errors.length] = 'Date of Birth is required field';
	}
	
	if (phone.length == 0) {
		errors[errors.length] = 'Phone number is required field';
	}
	
	if (pharmacy.length == 0) {
		errors[errors.length] = 'Pharmacy Information is required field';
	}
	
	if (medication.length == 0) {
		errors[errors.length] = 'Medication Requested is required field';
	}
	
	if (errors.length != 0) {
		var message = '';
		for (var i =0; i < errors.length; i++)
		{
			message += errors[i] + '\n';
		}
		alert(message);
		return false;
	}
	
	/**
	 * All checkings are ok, proceed to the ajax call
	 */
	var ajax_options = {
			beforeSend: function()
			{
				$('#ajax-throbber').css('display', 'inline');
				$('#submit_button').attr('disabled', 'disabled');
			},
			complete: function()
			{
				$('#ajax-throbber').css('display', 'none');
				//$('#submit_button').attr('disabled', '');
				
				$('li.buttons').css('display', 'none');
				$('li.required-fileds-text').css('display', 'none');
				$('li.status-message').slideDown();

                                $('input[type=text]').val('');
                                $('#pharmacy').val('');
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert('There was an error durring request. Please try again later!');
			},
			success: function(html, textStatus)
			{
				return false;
			},
			timeout: '10000',
			type: 'POST',
			dataType: 'html',
			data: 	'form=prescription-request' + 
					'&name=' + name + 
					'&email=' + email + 
					'&birth=' + birth + 
					'&phone=' + phone + 
					'&pharmacy=' + pharmacy + 
					'&medication=' + medication + 
					'&message=' + message 
			,
			url: theme_path + '/ajax-send-form.php'
		};
	$.ajax(ajax_options);
	return false;
}

function displayAppointmentForm()
{
	var new_patient = jQuery('#new_patient input:radio:checked').val();
	if (new_patient == 'yes') {
		$('#new-patient-no').slideUp();
		$('#new-patient-yes').slideDown();
	} else {
		$('#new-patient-yes').slideUp();
		$('#new-patient-no').slideDown();
	}
	$('li.buttons').css('display', 'block');
	$('li.required-fileds-text').css('display', 'block');
	$('li.status-message').css('display', 'none');
}
function sendAppointmentFormYes(theme_path)
{	
	var errors = new Array();
	
	var name		= $('#yes-name').val();
	var birth		= $('#yes-birth').val();
	var email		= $('#yes-email').val();
	var phone		= $('#yes-phone').val();
	var aphone		= $('#yes-aphone').val();
	var insurance	= $('#yes-insurance').val();
	
	if (name.length == 0) {
		errors[errors.length] = 'Name is required field';
	}
	
	if (email.length == 0) {
		errors[errors.length] = 'Email address is required field';
	}
	else if (!checkEmail(email)) {
		errors[errors.length] = 'Email address is not in valid format';
	}
	
	if (birth.length == 0) {
		errors[errors.length] = 'Date of Birth is required field';
	}
	
	if (phone.length == 0) {
		errors[errors.length] = 'Phone number is required field';
	}
	
	if (errors.length != 0) {
		var message = '';
		for (var i =0; i < errors.length; i++)
		{
			message += errors[i] + '\n';
		}
		alert(message);
		return false;
	}
	
	/**
	 * All checkings are ok, proceed to the ajax call
	 */
	var ajax_options = {
			beforeSend: function()
			{
				$('#ajax-throbber-yes').css('display', 'inline');
				$('#submit_button').attr('disabled', 'disabled');
			},
			complete: function()
			{
				$('#ajax-throbber-yes').css('display', 'none');
				//$('#submit_button').attr('disabled', '');
				
				$('li.buttons').css('display', 'none');
				$('li.required-fileds-text').css('display', 'none');
				$('li.status-message').slideDown();

                                $('input[type=text]').val('');
                                $('input[name=checkbox]').attr('checked', false);


			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert('There was an error durring request. Please try again later!');
			},
			success: function(html, textStatus)
			{
				return false;
			},
			timeout: '10000',
			type: 'POST',
			dataType: 'html',
			data: $('#trtmrt').serialize(),
			url: theme_path + '/ajax-send-form.php'
		};
	$.ajax(ajax_options);
	return false;
}

function sendAppointmentFormNo(theme_path)
{
	var errors = new Array();
	
	var name		= $('#no-name').val();
	var birth		= $('#no-birth').val();
	var email		= $('#no-email').val();
	var phone		= $('#no-phone').val();
	var aphone		= $('#no-aphone').val();
	var physician	= $('#no-physician').val();
	var reason		= $('#no-reason').val();
	
	var new_problem = $('input[name=new_problem]:checked').val();
	var follow_up 	= $('input[name=follow-up]:checked').val();
	
	if (name.length == 0) {
		errors[errors.length] = 'Name is required field';
	}
	if (birth.length == 0) {
		errors[errors.length] = 'Date of Birth is required field';
	}
	if (email.length == 0) {
		errors[errors.length] = 'Email address is required field';
	}
	else if (!checkEmail(email)) {
		errors[errors.length] = 'Email address is not in valid format';
	}
	
	if (phone.length == 0) {
		errors[errors.length] = 'Phone number is required field';
	}
	if (new_problem == 'yes' || new_problem == 'no') {
		
	} else {
		errors[errors.length] = 'If this is new problem please select Yes, otherwise select No';
	}
	if (follow_up == 'yes' || follow_up == 'no') {
		
	} else {
		errors[errors.length] = 'Is this for a follow-up appointment to a test please select Yes, otherwise select No';
	}
	
	
	if (errors.length != 0) {
		var message = '';
		for (var i =0; i < errors.length; i++)
		{
			message += errors[i] + '\n';
		}
		alert(message);
		return false;
	}
	
	/**
	 * All checkings are ok, proceed to the ajax call
	 */
	var ajax_options = {
			beforeSend: function()
			{
				$('#ajax-throbber-no').css('display', 'inline');
				$('#submit_button').attr('disabled', 'disabled');
			},
			complete: function()
			{
				$('#ajax-throbber-no').css('display', 'none');
				
				$('li.buttons').css('display', 'none');
				$('li.required-fileds-text').css('display', 'none');
				$('li.status-message').slideDown();

                                $('input[type=text]').val('');
                                $('input[name=radio]').attr('checked', false);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert('There was an error durring request. Please try again later!');
			},
			success: function(html, textStatus)
			{
				return false;
			},
			timeout: '10000',
			type: 'POST',
			dataType: 'html',
			data: 	'form=appointment-no' + 
					'&name=' + name + 
					'&email=' + email + 
					'&birth=' + birth + 
					'&phone=' + phone + 
					'&aphone=' + aphone + 
					'&physician=' + physician + 
					'&reason=' + reason + 
					'&new_problem=' + new_problem + 
					'&follow_up=' + follow_up 
			,
			url: theme_path + '/ajax-send-form.php'
		};
	$.ajax(ajax_options);
	return false;
}
