$(document).ready(function(){
	var searchType = $('.selZipCity').val();
	
	$('form.frmSearchVet').submit(function(event){
		if (searchType === 'zip')
		{
			if (isValidZipCode($('.txtMainFindVet').val()) === false)
			{
				alert('You entered an invalid zipcode');
				return false;
			}
		}
	});
	
	$('.selZipCity').change(function(event){
		searchType = $(this).val();
	})
	
	$('.txtMainFindVet').DefaultValue('Enter ZIP code');
	
	
	/* CLICK TO CALL */
	$('.noticeFeedback').hide();
	var contactOptions = {
			title: 'Click To Call',
			autoOpen: false,
			modal: true,
			width:350,
			height:400,
			draggable: false,
			resizable: false,
			show: 'drop',
			hide: 'drop',
			buttons: {
					'Send': sendCall,
					'Close': closeCall
				}
		}
	function sendCall()
	{
		/* VALIDATING CLICK TO CALL FORM */
		if (validate() === 1)
		{
			var name = $('#txtYourName').val();
			var email = $('#txtYourEmail').val();
			var number = $('#txtYourNumber').val();
			$.get(base_url+'index.php/welcome/generate_token/', function(str){
				$.ajax({
					type: 'POST',
					url: base_url+'index.php/welcome/check_token/',
					data: {str:str, name: name, email: email, number: number},
					success: function(data) 
					{
						if (data > 0)
						{
							var firstButton=$('.ui-dialog-buttonpane button:first').fadeOut();
							$('.noticeFeedback').fadeIn();
							setTimeout('$(\'#fldContactForm\').dialog(\'close\');$(\'#frmClickToCall input[type="text"]\').each(function(){$(this).val(\'\');});$(\'.noticeFeedback\').hide();$(\'.ui-dialog-buttonpane button:first\').fadeIn()', 5000);
						}
					},
					failure: function()
					{
						alert('unable to complete the security form');
					}
				})
			});
		}
	}
	function closeCall()
	{
		$('#fldContactForm').dialog('close');
		$('#frmClickToCall .inlineErrorMessage').each(function(){
			$(this).text('');
		});
	}
	
	function validate()
	{
		var valid = 1;
		$('#frmClickToCall input[type="text"]').each(function(){
			if ($(this).attr('class') === 'required') { // check required fields
				if ($(this).val() === '')
				{
					$(this).prev('p').attr('class','inlineErrorMessage').text('field is required');
					valid = 0;
				}
				else
					$(this).prev('p').removeClass().text('');
			}
			if ($(this).attr('class') === 'valid_email') {
				if (echeck($(this).val()) === false)
				{
					$(this).prev('p').attr('class','inlineErrorMessage').text('invalid email format');
					valid = 0;
				}
				else
					$(this).prev('p').removeClass().text('');
			}
		});
		return valid;
	}
	
	$('#fldContactForm').dialog(contactOptions);
	
	$('.btnClickToTalk').click(function(event){
		$('#fldContactForm').dialog('open');
	})
})
