$(document).ready(function(){
     
    if ($("span.error").text() != "" && $("span.error").text() != "((ErrorMessage))") {
      $("p.backend-error").text($("span.error").text());
      $("p.backend-error").css('visibility','visible');
      $("p.backend-error").css('color','#cc0000');
      $("span.error").text('');
    }

   var re = new RegExp('\\b' + 'Please' + '\\b');
    $("dd").each(function (i){
          var $match = $("dt").eq(i);
          $(this).children('input').focus( function () {
                $match.children('strong').hide();
                if ($(this).val().match(re)) {
                    $(this).css('color','#333333');
                    $(this).val('');
                }
          });
          $(this).children('input').blur( function () {
              if ($(this).attr('class') == 'error' && $(this).val() == "") {
                       $(this).val('Please fill in required field.');
                       $(this).css('color','#cc0000');

              }
          });
    });

$("form#domain_password").submit( function () {
var fields_required = {'Domain':'DomainName','ImageKey':'Typeinthecharactersyouseeabove'};
var error_fields = [];
var error_messages = 0;
error_fields['DomainName'] = 'Please fill in required field.';
error_fields['Typeinthecharactersyouseeabove'] = 'Please fill in required field.';

     $.each(fields_required,function(key,val) {
              $("dt").each( function (i) {
                            var $match = $("dd").eq(i);
                            var temp_var = $(this).children('label').text().replace(/ /g,'');
                            var temp_var = temp_var.replace(/:/g,'');
                            var temp_var = temp_var.replace(/\*/g,'');
                        if ($("input#"+key).val() == "") {
                            if (temp_var == val) {
                                  error_messages++;
                                  var existing_class = $("input#"+key).attr('class');
                                  $("input#"+key).removeClass(existing_class);
                                  $("input#"+key).addClass('error');
                                  $("input#"+key).val(error_fields[val]);
                                  //$(this).children('strong').show();
                                  //$(this).children('strong').text(error_fields[val]);
                            }
                       }

                        if ($("input#"+key).val() != "") {
                            if (temp_var == val) {
                              if ($("input#"+key).val() == "Please fill in required field.") {
                                  error_messages++;
                              }
                            }
                        }
              });
      });
     if (error_messages > 0 ) {
        return false;
     }
   });
});
