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

    $("input[type='reset']").click( function () {
      $("p#agree").children('strong').text('');    
    });

    $("form#reseller_signup").submit( function () {

      var checkDomain = $("input#checkDomain").is(":checked");
      var checkHosting = $("input#checkHosting").is(":checked");
      var checkSSL = $("input#checkSSL").is(":checked");
      var checkBroadcast = $("input#checkBroadcast").is(":checked");
      var agreement = $("input#agreement").is(":checked");
      var error_messages = 0;

var fields_required = {'AccountName':'CompanyName','Address':'Address','Suburb':'Suburb/Town','State':'State','Postcode':'PostCode','Country':'Country','ContactName':'ContactPerson','PhoneCountryCode':'CountryCode','PhoneAreaCode': 'AreaCode','PhoneNumber':'PhoneNumber','Email':'Email'};

      var error_fields = [];

      error_fields['CompanyName'] = 'Please fill in required field.';
      error_fields['Address'] = 'Please fill in required field.';
      error_fields['Suburb/Town'] = 'Please fill in required field.';
      error_fields['State'] = 'Required field.';
      error_fields['PostCode'] = 'Required field.';
      error_fields['Country'] = 'Please fill in required field.';
      error_fields['ContactPerson'] = 'Please fill in required field.';
      error_fields['CountryCode'] = 'Required field.';
      error_fields['AreaCode'] = 'Required field.'; 
      error_fields['PhoneNumber'] = 'Please fill in required field.';
      error_fields['Email'] = 'Please fill in required field.';


      if($("input#agreement").is(":checked") == false) {
        error_messages++;
        $("p#agree").children('strong').text('You must agreee to the terms above.');
      }

      if($("input#agreement").is(":checked") == true) {
        $("p#agree").children('strong').text('');
      }

      if($("input[id^=check]:checked").length > 0) {
        $("p#products").children('strong').text('');
      }

      if($("input[id^=check]:checked").length == 0) {
        error_messages++;   
        $("p#products").children('strong').text('Please select a product/s to signup to.');
      }

      $.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).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." || $("input#"+key).val() == "Required field.") {
                        var existing_class = $("input#"+key).attr('class');
                        $("input#"+key).addClass('error');
                        $("input#"+key).val(error_fields[val]);
                        $("input#"+key).css('color','#cc0000');   
                        error_messages++;
                        }
                  }
                      
             }
             
             if ($("input#"+key).val() != "" && ($("input#"+key).val() == 'Please fill in required field.' || $("input#"+key).val() == 'Required field.')) {
                      if(key == 'Email') {
                     }
             } 
         });
    });

     if (error_messages > 0) { 
          location.href='signup.html#top';
          $("p.backend-error").html("We have encountered errors processing your request, <br> please see details below.");
          $("p.backend-error").css('visibility','visible');
          $("p.backend-error").css('color','#cc0000');
          $("p.backend-error").css('font-weight', 'bold');
          return false;
     }
});
});
