  /**
   *  email.forms.js  :: eHaus  :: 2009/10/22
   *  ------------------------------------------------------
   *  Validate a form that's going to be emailed to a client
   *  via the front end of their website
   *  
   *  Requires jQuery and reCAPTCHA        
   */
  
  /**
   *  General page stuff
   */     
      $(document).ready(function(){
      
        $("#recaptcha_reload").click(function(){
          $("#reCaptchaError").hide(0);
        });
      
      });  
   
  /**
   *  processForm will be called from the front end and handle all the cleansing    
   *  of the form before it is submitted
   */
   
      function processForm(thisForm,fields)
      {
        //  Validate form input
        var validForm = validate(thisForm,fields);
        if(!validForm)
        {
          return false;
        }
        
        //  Validate reCAPTCHA and make sure they're human
        var validReCAPTCHA  = validateReCAPTCHA(thisForm);
        if(!validReCAPTCHA)
        {
          $("#reCaptchaError").toggle("slow");
          return false;
        }
        
        var sendParams = $("#" + thisForm).serialize();
        
        $.ajax({
		      type: "POST",
		      url: "include/email.forms.relay.asp", 
		      dataType: "xml",
		      data: sendParams,
		      async: false,
		      success: function(xml){
		        var errCode = $("errorcode", xml).text();
		        var errMsg  = $("errormsg", xml).text();
		        
            $("#"+thisForm+"_mailsendError").html("<br /><br />" + errMsg);
            $("#"+thisForm).hide();
            $("#recaptcha_area").hide();
            $("#submitButton").hide();
            
            //  Return to top of the page
            //window.scrollTo(0,0);                           
          }
	      });
         
        return true;     
      }
      
  /**
   *  validateReCAPTCHA will make sure that the user has gotten the CAPTCHA right
   */
   
      function validateReCAPTCHA(thisForm)
      {
        var challange  =  $("#"+thisForm+" .captcha").val();
        
        if(challange == undefined)
        {
          alert("Please complete the Captcha Challange box");
          return false;
        }
        
        var validateRequest = "challange="+challange+"&form="+thisForm;
        
        $.ajax({
		      type: "GET",
		      url: "include/fancycaptcha/fancycaptcha.validate.asp", 
		      dataType: "xml",
		      data: validateRequest,
		      async: false,
		      success: function(xml){
		        var errCode = $("errorcode", xml).text();
		            $("#"+thisForm+"_validCaptcha").html(errCode);
          }
	      });
        
        var validCaptcha = $("#"+thisForm+"_validCaptcha").html(); 
        
        //$("#" + thisForm + " .captcha").remove();
        
        if(validCaptcha == 0){
          return true;
        }else{
          return false;
        }    
        
      }
      
  /**
   *  Validate function  
   */
      function validate(frm, fields)
      {
	       var DoValidate = true
	       if (DoValidate)
         {
		        validForm = true;
		        firstError = null;
		        errorstring = '';
		        var x = $(":input", '#'+frm);
		        var m = '';

		        // loop through everything and check it
		        for (var i=0;i<x.length;i++)
		        {
			         m += x[i].name  + '\n' ;
			         // Test the pipe separated list of fields submitted..
			         var reg = new RegExp(fields, 'i' );
			         if (reg.test(x[i].name))
               {
				          var ereg = /email/ig;
				          if (ereg.test(x[i].name))
                  {
					           var vreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					           if (vreg.test(x[i].value) == false)
                     {
						            alert('Invalid email address. ');
						            return false;
					           }
				          }
                  else
                  {
					           if (!x[i].value)
                     {
						            alert('Please fill in all fields marked by an asterisk (*) - These fields are required. ');
						            return false;
					           }
				          }	
			         }	
		        }
		        
		        if (validForm)
            {
			        return true;
            }
            else
            {
			        return false; 
            }
	       }
         else
         {
		      //document.frmEdit.submit();
	       }
      }         
