  /**
   *  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)	//only applies to 
      {
        //  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;
        } else {
			//set the destination / submit via js only...
			//form.destination = subscribe.asp --
			$('#'+thisForm).attr("action", "subscribe.asp");
			$("#" + thisForm).submit();
			return true;	//needed?
		}

        //var sendParams = $("#" + thisForm).serialize();
        
		//not ajax, just go submit!
		// $('#frmsubcribe').submit();	// overwrite that function as this... so clicking the button
		// calls subscribe, or does nothing if no javascript. (also add a hidden field via Javascript... if not present don't go further.)

		/*
        $.ajax({
		      type: "POST",
		      //url: "include/email.forms.relay.asp",
			  url: "subscribe.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 Challenge 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();	//why not just set a variable?
        
        //$("#" + 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;
					           }
					           
                     var preg = /password/ig;
					           if (preg.test(x[i].name))
                     {
						            var p_len = (x[i].value).length;
						            if(p_len < 8)
						            {
                          alert("Your password must be at least 8 characters long.");
                          return false  
                        }
                        if(($("#password_conf").val() == "")||($("#password_conf").val() != $("#password").val()))
                        {
                          alert("Please ensure your password and confirmation password match!");
                          return false;
                        }
                        
                        var pass    = $("#password").val();
                        var remind  = $("#reminder").val();
                        
                        if(pass.match(remind) == pass)
                        {
                          alert("Your reminder cannot contain your password");
                          return false;
                        }
					           }
				          }	
			         }	
		        }
		        
		        if (validForm)
            {
			        return true;
            }
            else
            {
			        return false; 
            }
	       }
         else
         {
		      //document.frmEdit.submit();
	       }
      }         
