jQuery(document).ready(
      function () {
      
         var tAllowToSubmit = false;
         fIds=["your-name",  "your-zip", "your-phone","your-email", "insurance-provider", "hear-about"];
         
	jQuery('.thankyou').empty().hide();
         setTimeout(function () { tAllowToSubmit = true; }, 1000);

         var tCheckSubmitPromotionalFormTimeout = false;
         function lkCheckSubmitPromotionalForm(fForm) {
	         jQuery('#ffsub').hide();
            jQuery('form.js-contact-us-form .preloader').stop(false, true).fadeIn(500);
            
            if (tCheckSubmitPromotionalFormTimeout)
               clearTimeout(tCheckSubmitPromotionalFormTimeout);
            if (tAllowToSubmit == false)
               tCheckSubmitPromotionalFormTimeout = setTimeout(function () { lkCheckSubmitPromotionalForm(fForm) }, 1000);
            else
               lkSubmitPromotionalForm(fForm);
            return false;
         }
         function lkSubmitPromotionalForm(fForm) {
         okflg=true;
         for (i=0; i<fIds.length; i++)
         {
         	myid=fIds[i];
         	v = jQuery("#"+myid).val();
         	//alert(myid+' -> '+jQuery("#"+myid).val());
         	if (v.length<1)
         	{
         		okflg=false;
         		jQuery("#"+myid).css('border','1px red dotted');
         	}
         	else
         	{
         		jQuery("#"+myid).css('border','');
         	}
         }
         
	    
	if (okflg == false) 
	{
		jQuery('form.js-contact-us-form .preloader').stop(false, true).fadeOut(500).hide();
		jQuery('#ffsub').show();
		
		return false;
	}
	
        jQuery.ajax({
               url: fForm.action,
               cache: false,
               dataType: 'json',
               data: jQuery('form.js-contact-us-form').serialize(),
               type: 'POST',
               beforeSend: function () { },
               complete: function () { jQuery('form.js-contact-us-form .preloader').stop(false, true).fadeOut(500); },
               success: function (fData) {
                  jQuery('form.js-contact-us-form .field-validation-error').text('');
//alert(fData);
                  if (fData.success === true) {
//		    jQuery("#hiddenframe").load("/thankyou.html");
                     //jQuery('#thank-you iframe').attr('src', '/thank-you/');
                     //showPopupBySelector('#thank-you');
      //      alert('OK');         

                     jQuery('.thankyou').empty().append('Thank you for contacting Dallas Bariatric Center.<br><br> A representative from our office will contact you soon.');
                     //document.write('<div style="display:none;visibility:hidden"><scr'+'ipt>document.write("<ifr'+'ame src=/thankyou.html name=thankifr></if'+'rame>");</scr'+'ipt></div>');
                     
                     jQuery('form.js-contact-us-form').children().hide();
		    jQuery('.thankyou').show();
//		     _gaq.push(['_trackEvent', 'Form submit', 'Submit Clicked', '']);


		    var ts=setTimeout("jQuery('form.js-contact-us-form').each(function(){this.reset();}); jQuery('form.js-contact-us-form').children().show();jQuery('.thankyou').empty().hide();jQuery(document).ready();jQuery('form.js-contact-us-form .preloader').stop(false, true).fadeOut(500);",10000);
                  } else if (fData.errors) {
   // alert('errors');
                     for (var tC in fData.errors) {
                        jQuery('#validation-message-' + fData.errors[tC].PropertyName).text(fData.errors[tC].ErrorMessage);
                     }
                  } else if (fData.fatalerrors) {
//alert('fatalerrors');
                     var tErr = '';
                     for (var tC in fData.fatalerrors) {
                        tErr += fData.fatalerrors[tC] + '\n';
                     }
                     alert(tErr);
                  }
               },
               error: function(jqXHR, textStatus, errorThrown){ alert(textStatus) }
            });

            return false;
         }

        jQuery('form.js-contact-us-form').submit(function () 
	{ 
//	    _gaq.push(['_trackEvent', 'Form submit', 'Submit Clicked', '']);
//	    document.getElementById("psm").innerHTML = '<iframe src="/thankyou.html" style="border:none;width:1px;height:1px;" marginheight="0" marginwidth="0" frameborder="0"></iframe>';
	    lkCheckSubmitPromotionalForm(this); return false; 
	});

	jQuery("#ffsub").click(function()
	{
	    lkCheckSubmitPromotionalForm(document.getElementById('fform'));
	    return false;
	}
	);


      }
   );

