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

    $("dd").each(function (i){
          var $match = $("dt").eq(i);
         var re = new RegExp('\\b' + 'Please' + '\\b');
          $(this).children('input').focus( function () {
                $match.children('strong').hide();
                if ($(this).val().match(re)) {
                    $(this).css('color','#333');
                    $(this).val('');
                }

          });
          $(this).children('input').blur( function () {
              if ($(this).attr('class') == 'error' && $(this).val() == "") {
                       $(this).val('Please fill in required field');
                       $(this).css('color','red');

              }
          });
    });

  $("form#domain_login").submit( function () {
  var fields_required = {'user_id':'UserID','url':'Domain','Domain_password':'Password'};

    var error_fields = [];
    error_fields['Domain'] = 'Please fill in required field';
    error_fields['UserID'] = 'Please fill in required field';
    error_fields['Password'] = 'xxxx';

    var error_messages = 0;
    $.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() == "") {
                   error_messages++;
                        if (temp_var == val) {
                            var existing_class = $("input#"+key).attr('class');
                            $("input#"+key).addClass('error');
                            $("input#"+key).val(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;
    }
  });

});
