// JavaScript Document
//add faq to the patient
$(document).ready(function() {
var loginError = "";
		
function valAllowEmpty(value,title)
{
	//doesnt allow empty	
	if(trim(value) == '')
	{
			
		loginError += "<li>" + title + "</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 trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
$("input[@name='sub_enquiry']").click(function() {	
	loginError = "";
	$('.errorLabel').empty();
	$('#PatientEnquiryform *').each(function() {

		if($(this).attr('title'))
		{
			valAllowEmpty($(this).val(),$(this).attr('title'));
			if($(this).attr('id') == 'email')
			{
				valEmail($(this).val());
			}
		}
	});
	if(loginError != "")
	{
		$('.errorLabel').append(loginError);
		$('.errorLabel').show();
	}
	else
	{
		$('#loadingBar').show();
		$('.errorLabel').hide();
		
		$.post('../../../ajax/email/patientEnquiry.php'
		,{
		
		  'email':$('#email').val()
		  ,'name':$('#name').val()
		  ,'enquiry':$('#enquiry').val()
		}
		,function(returnData)
		{
			if(returnData == 'ok')
			{
				$('.errorLabel').show();
				$('.errorLabel').append('Enquiry Sent Successfully');
				$('#loadingBar').hide();
				//clear fields
				$('#email').val('');
				$('#name').val('');
				$('#enquiry').val('');
			}
			else
			{	$('.errorLabel').show();
				$('.errorLabel').append('An error occurred');
				$('#loadingBar').hide();
			}
		}
		,'text');	
	}
	});
});