
  /*
   * Insert Script Plugin
   *
   * Copyright (c) 2008 Kevin Martin (http://synarchydesign.com/insert)
   * Licensed under the GPL license:
   * http://www.gnu.org/licenses/gpl.html
   *
   */
  jQuery.insert = function(file)
  {
    var data  = [];
    var data2 = [];
  
    if (typeof file == 'object')
    {
      data = file;
      file = data.src !== undefined ? data.src : false;
      file = file === false && data.href !== undefined ? data.href : file;
      file = file === false ? file2 : false;
    }
  
    if (typeof file == 'string' && file.length)
    {
      var index = file.lastIndexOf('.');
      var index2  = file.replace('\\', '/').lastIndexOf('/') + 1;
      var ext   = file.substring(index + 1, file.length);
    }
  
    switch(ext)
    {
      case 'js':
        data2 = {
          elm:  'script',
          type: 'text/javascript',
          src:  file
        };
      break;
  
      case 'css':
        data2 = {
          elm:  'link',
          rel:  'stylesheet',
          type: 'text/css',
          href: file
        };
      break;
  
      default:
        data2 = {elm: 'link'};
      break;
    }
  
    data2.id = 'script-' + (typeof file == 'string' && file.length ?
      file.substring(index2, index) : Math.round(Math.rand() * 100));
  
    for (var i in data)
    {
      data2[i] = data[i];
    }
  
    data  = data2;
    var tag = document.createElement(data.elm);
  
    delete data.elm;
  
    for (i in data)
    {
      tag.setAttribute(i, data[i]);
    }
  
    jQuery('head').append(tag);
  
    return jQuery('#' + data.id);
  };

  function Object_To_String(objectOrArray,printToPage,spaces,objectName)
  {
    var result = "";
    if ((typeof(objectOrArray) == "undefined"))
    {
      alert("function Object_To_String(...) -  Missing first argumnent!");
      return result;
    }    
    if ((typeof(printToPage) == "undefined"))
    {
      printToPage = false;
    }    
    if ((typeof(spaces) == "undefined"))
    {
      spaces = "";
    }    
    if ((typeof(objectName) == "undefined"))
    {
      objectName = "root";
    }
    if (printToPage)
    {
      result += (spaces + "<ul>\r\n");
    }    
    if (typeof(objectOrArray) == "object")
    { 
      if (printToPage)
      {
        result += (spaces + "<ul>\r\n");
      }
      else
      {
        if (objectOrArray != null &&
            (typeof(objectOrArray.length) != "undefined") &&
            objectOrArray.length > 0)
        {
          result += spaces + "";
        }
      }
      if (("" + objectOrArray) != "undefined")
      {
        for (var p in objectOrArray)
        {
          if (typeof(objectOrArray[p]) == "object")
          {
            if (printToPage)
            {
              result += (spaces + "  <li>[" + (objectName == "" ? "" : (objectName + ".")) + p + "] => " + typeof(objectOrArray[p]) + "</li>\r\n");
              result += (spaces + "    <ul>\r\n");
            }
            else
            {
              result += spaces + "  [" + (objectName == "" ? "" : (objectName + ".")) + p + "] => " + typeof(objectOrArray[p]) + "\r\n";
              result += spaces + "    \r\n";
            }
            result += Object_To_String(objectOrArray[p],printToPage,spaces + "      ",p);
            if (printToPage)
            {
              result += (spaces + "    </ul>\r\n");
            }
            else
            {
              result += spaces + "    \r\n";
            }
          }
          else
          {
            if (printToPage)
            {
              result += (spaces + "  <li>[" + (objectName == "" ? "" : (objectName + ".")) + p + "] => "+objectOrArray[p] + "</li>\r\n");
            }
            else
            {
              result += spaces + "  [" + (objectName == "" ? "" : (objectName + ".")) + p + "] => "+ objectOrArray[p] + "\r\n";
            }
          }
        }
      }
      if (printToPage)
      {
        result += spaces + "</ul>\r\n";
      }
      else
      {
        if (objectOrArray != null &&
            (typeof(objectOrArray.length) != "undefined") &&
            objectOrArray.length > 0)
        {
          result += "\r\n";
        }      
      }
    }
    return result;
  }
  
  // JavaScript Document
  $(document).ready(function()
                    {
                      var textFields = $("form[name='fastContactForm']").find("input[type='text']")
                      $(textFields).addClass("idleField");
                      $(textFields).focus(function()
                                          {
                                            $(this).removeClass("idleField").addClass("focusField");
                                            if (this.value == this.defaultValue)
                                            { 
                                              this.value = "";
                                            }
                                            if (this.value != this.defaultValue)
                                            {
                                              this.select();
                                            }
                                          }
                                         );                                                        
                      $(textFields).blur(function()
                                         {
                                           $(this).removeClass("focusField").addClass("idleField");
                                           if ($.trim(this.value) == "")
                                           {
                                             this.value = (this.defaultValue ? this.defaultValue : "");
                                           }
                                         }
                                        );                                                        
                    }
                   );

  function HtmlForm_ValidateEmailAddress(formName,eMail)
  {
    if (eMail == null ||
        eMail.length < 3 ||
        eMail.indexOf("@")  == -1 ||
        eMail.indexOf(".")  == -1 ||
        eMail.indexOf("*")  != -1 ||
        eMail.indexOf("'")  != -1 ||
        eMail.indexOf(",")  != -1 ||
        eMail.indexOf("\\") != -1 ||
        eMail.indexOf("/")  != -1)
    {
      alert("Email address is not valid! \n\n" +
            "Folowing rules are used for e-mail check:\n\n" +
            "1. Must have more than 2 char\n" +
            "2. Must have @ in it\n" +
            "3. Must have minimum one dot in it\n" +
            "4. Must *not* have following characters in it: *',/\\\n");
      return false;
    }
    return true;
  }
  
  function Fast_Contact_Us_Before_Submit(formData, jqForm, options)
  {
    var form = $("form[name='fastContactForm']");
    
    var textField = $(form).find("input[name='name']");
    if ($(textField).val() == "" ||
        $(textField).val() == $(textField).attr("title"))
    {
      alert("Please enter your Name!");
      $(textField).focus();
      return false;
    }

    textField = $(form).find("input[name='email']");
    if ($(textField).val() == "" ||
        $(textField).val() == $(textField).attr("title"))
    {
      alert("Please enter your E-mail!");
      $(textField).focus();
      return false;
    }

    textField = $(form).find("input[name='email']");
    if (HtmlForm_ValidateEmailAddress($(form).name,$(textField).val()) == false)
    {
      // alert("E-mail address is not valid!");
      $(textField).focus();
      return false;
    }

    textField = $(form).find("input[name='phone']");
    if ($(textField).val() == "" ||
        $(textField).val() == $(textField).attr("title"))
    {
      alert("Please enter your Phone number!");
      $(textField).focus();
      return false;
    }

    textField = $(form).find("input[name='question']");
    if ($(textField).val() == "" ||
        $(textField).val() == $(textField).attr("title"))
    {
      alert("Please enter Your question!");
      $(textField).focus();
      return false;
    }
    $("#submitingFormProgressImage").show();
    return true;
  }

  function Fast_Contact_Us_Submit()
  {
    // 
    $("#fastContactForm").ajaxSubmit({
                                       // target:        '#output1',   // target element(s) to be updated with server response 
                                       beforeSubmit: Fast_Contact_Us_Before_Submit,
                                       success: function(responseJsonObject, statusText, xhr, $form)
                                                {
                                                  $("#submitingFormProgressImage").hide();

                                                  // This part does not work
                                                  var responseCode = "";
                                                  responseCode += "<span id='WPMG_ROI_Tracking'>";
                                                  responseCode += "<!-- WPMG ROI Tracking Action Script: ShortContact BEGIN -->";
                                                  responseCode += "<script type='text/javascript'>";
                                                  responseCode += "";
                                                  responseCode += "  (function()";
                                                  responseCode += "   {";
                                                  responseCode += "    var d=document,u=((d.location.protocol=='https:')?'s':'')+'://www.conversionruler.com/bin/js.php?siteid=3615';";
                                                  responseCode += "    d.write(unescape('%3Cscript src=%22http'+u+'%22 type=%22text/javascript%22%3E%3C/script%3E'));";
                                                  responseCode += "   }";
                                                  responseCode += "  )();";
                                                  responseCode += "";
                                                  responseCode += "</script><script type='text/javascript'>";
                                                  responseCode += "";
                                                  responseCode += "cr_track('3');";
                                                  responseCode += "";
                                                  responseCode += "</script><noscript><div style='position: absolute; left: 0'><img src='https://www.conversionruler.com/bin/tracker.php?siteid=3615&amp;nojs=1&amp;actn=3' alt='' width='1' height='1' /></div></noscript>";
                                                  responseCode += "<!-- WPMG ROI Tracking Action Script: ShortContact END -->";
                                                  responseCode += "</span>";
                                                  
                                                  // Other way of that part, works:
                                                  // WPMG ROI Tracking Action Script: ShortContact BEGIN
                                                  var url = "http" + ((document.location.protocol=='https:')?'s':'') + "://www.conversionruler.com/bin/js.php?siteid=3615";
                                                  $.insert(url).ready(function()
                                                  {
                                                    cr_track("3");
                                                  });
                                                  // WPMG ROI Tracking Action Script: ShortContact END
                         
                                                  alert(responseJsonObject["reponse_alert_message"]);
                                                },
                                       error:   function(xhr, ajaxOptions, thrownError)
                                                { 
                                                  $("#submitingFormProgressImage").hide();
                                                  if (confirm("Contact Us submit failed. Do you want to see more info about bug ?"))
                                                  {
                                                    alert(xhr.responseText);
                                                  }                                                  
                                                },
                                       url: "fast-contact-us.php",       // override for form's 'action' attribute 
                                       type : "POST",        // 'get' or 'post', override for form's 'method' attribute 
                                       dataType: "json",        // 'xml', 'script', or 'json' (expected server response type) 
                                       semantic: true,
                                       // clearForm: true        // clear all form fields after successful submit 
                                       resetForm: true,    // reset the form after successful submit 
                                       // $.ajax options can be used here too, for example: 
                                       //timeout:   3000 
                                      }
                                     );
    return false;
  }

  var formFastContactUsHtml = "";
  formFastContactUsHtml += "    <form action=\"\"\r\n";
  formFastContactUsHtml += "          method=\"POST\"\r\n"; 
  formFastContactUsHtml += "          name=\"fastContactForm\"\r\n";
  formFastContactUsHtml += "          id=\"fastContactForm\"\r\n";
  formFastContactUsHtml += "          onsubmit=\"return Fast_Contact_Us_Submit();\">\r\n";
  formFastContactUsHtml += "      Fast contact us:\r\n";
  formFastContactUsHtml += "      <fieldset>\r\n";
  formFastContactUsHtml += "           <input name=\"command\" id=\"command\" value=\"send\" type=\"hidden\"/>\r\n";
  formFastContactUsHtml += "           <input name=\"name\" id=\"name\" class=\"status\" value=\"Name\" title=\"Name\" type=\"text\"/>\r\n";
  formFastContactUsHtml += "           <input name=\"email\" id=\"email\" class=\"status\" value=\"E-mail\" title=\"E-mail\" type=\"text\"/>\r\n";
  formFastContactUsHtml += "           <br />\r\n";
  formFastContactUsHtml += "           <input name=\"phone\" id=\"phone\" class=\"status\" value=\"Phone number\" title=\"Phone number\" type=\"text\"/>\r\n";
  formFastContactUsHtml += "           <input name=\"question\" id=\"question\" class=\"status\" value=\"Your question\" title=\"Your question\" type=\"text\"/>\r\n";
  formFastContactUsHtml += "           <br />\r\n";
  formFastContactUsHtml += "           <input value=\"Submit\" type=\"submit\" class=\"submitBtn\" /><span style=\"padding-left: 10px; padding-top: 8px;\"><img id=\"submitingFormProgressImage\" style=\"display: none;\" src=\"./images/loading.gif\" title=\"Submiting form\" /></span>\r\n";
  formFastContactUsHtml += "      </fieldset>\r\n";
  formFastContactUsHtml += "    </form>\r\n";
