
//=============================================================================
// DOUBLE CHECK FIELDS, BEFORE CONTINUNG...
//=============================================================================
subdomain_ok = false;
email_status = false;
simage_valid = false;

function log_reg_step(step)
{
  var reg_log = createRequestObject();
    if(reg_log!=null)
    {
        var url="iowners/update_reg_log.php?";
        url=url+"step="+step+"&id="+id_reg_log;
        reg_log.open( "GET", url );
        reg_log.send( null );
    }
}

function check_step(step){

    if( step == 'legal' )
    {
       log_reg_step('1');
       var result = LiveValidation.massValidate( [ over_18, tsandcs ] );
       if( result ){ return true;} else{ return false;}
    }

    else if( step == 'details' )
    {
       log_reg_step('2');
       check_email(document.getElementById('email').value);
       check_simage(document.getElementById('simage').value);

       var result = LiveValidation.massValidate( [ name1, name2, phone, email, email_c,
                                          address1, city, region, postcode,
                                          country, sex, age, password, password_c, simage
                                        ] );
       if( result == true )
       {
            if( email_status == false || simage_valid == false )
            {result = false;}
       }

       if( result ){ step2 = true; return true;} else{ return false;}
    }

    else if( step == 'property_info' )
    {
        log_reg_step('3');
        var result = LiveValidation.massValidate( [ prop_country, prop_region ] );
        if( result ){ step3 = true; return true;} else{ return false;}
    }

    else if( step == 'subdomain' )
    {
        log_reg_step('4');
        check_subdomain(document.getElementById('subdomain').value);

        var result = LiveValidation.massValidate( [ subdomain ] );
        if( result == true )
       {
            if( subdomain_ok == false )
            result = false;
       }

       if( result ){ step4 = true; return true;} else{ return false;}
    }

    else if( step == 'marketing' )
    {
        log_reg_step('5');
        var result = LiveValidation.massValidate( [ survey ] );
        if( result ){ step5 = true; return true;} else{ return false;}
    }

    else if( step == 'all' )
    {
        log_reg_step('6');
        var allok = true;
        var errors = 'Please fix these errors before continuying registration:\n\n';

        if( LiveValidation.massValidate( [ over_18, tsandcs ] ) == false )
        {
            errors += "Step 1: You must agree term and conditions and you must be over 18.\n";
            allok = false;
        }

        if( !LiveValidation.massValidate( [ name1, name2, phone, email, email_c,
                                          address1, city, region, postcode,
                                          country, sex, age, password, password_c, simage
                                        ] ) )
        {
            errors += "Step 2: Please doublechck the data you entered in details page.\n";
            allok = false;
        }

        if( !LiveValidation.massValidate( [ prop_country, prop_region ] ) )
        {
            errors += "Step 3: Please check your property info.\n";
            allok = false;
        }

        if( !LiveValidation.massValidate( [ subdomain ] ) )
        {
            errors += 'Step 4: Please select another domain name for your site.\n';
            allok = false;
        }

        if( !allok )
        {
            document.getElementById('reg_status').innerHTML = errors;
            alert( errors + '\nYou can do it by pressing its number at the top of registration form and fix occured errors before continuying.');
        }
        else
        {

          var reg_fin_log = createRequestObject();
            if(reg_fin_log!=null)
            {
                var url="iowners/update_reg_log.php?";
                url=url+"fin=true"+"&id="+id_reg_log;
                reg_fin_log.open( "GET", url );
                reg_fin_log.send( null );
            }
        }
        return allok;
    }         
}
//=============================================================================



//=============================================================================
// INITIALIZE MAIN CHECK OBJECTS
//=============================================================================

//=============================================================================
// LEGAL REQ STEP
    var over_18 = new LiveValidation('over18', { validMessage: '+'});
    over_18.add( Validate.Acceptance, { failureMessage: "Please confirm that you are over 18 years old" } );

    var tsandcs = new LiveValidation('tsandcs', { validMessage: '+'});
    tsandcs.add( Validate.Acceptance, { failureMessage: "Please accept Terms & Conditions before continuing" } );
//=============================================================================

//=============================================================================
// DETAILS STEP
    var name1 = new LiveValidation('name1', { wait: 1500 } );
    name1.add(Validate.Presence, { failureMessage: "Please enter your first name" });

    var name2 = new LiveValidation('name2', { wait: 1500 });
    name2.add(Validate.Presence, { failureMessage: "Please enter your family name" });

    var phone = new LiveValidation('phone', { wait: 1500 } );
    phone.add( Validate.Presence,  { failureMessage: "Please enter your phone number" });
    phone.add( Validate.Format, { pattern: /^[+]?[ 0-9]*$/i, failureMessage: "Please enter a valid phone number" } );

    var email = new LiveValidation('email', { wait: 1500 } );
    email.add(Validate.Presence, { failureMessage: "Please enter your email" });
    email.add(Validate.Email, {validMessage: "?", failureMessage: "Please enter a valid email address" });

    var requesting = false;

    function validate_email()
    {

      var Valid = LiveValidation.massValidate( [ email ] );

      if( Valid == true )
      {
        // if( !requesting )
            check_email(document.getElementById('email').value);
      }
      else
      {
        document.getElementById('status').innerHTML = '';
      }
    }



    var email_c = new LiveValidation('email_c', { wait: 1500 } );
    email_c.add(Validate.Confirmation, { match: 'email', failureMessage: "Fields do not match"});
    email_c.add(Validate.Presence, { failureMessage: "Please confirm your email" });

    var address1 = new LiveValidation('address1', { wait: 1500 } );
    address1.add(Validate.Presence, { failureMessage: "Please enter your address" });

    var city = new LiveValidation('city', { wait: 1500 } );
    city.add(Validate.Presence, { failureMessage: "Please enter your city" });

    var region = new LiveValidation('region', { wait: 1500 } );
    region.add(Validate.Presence, { failureMessage: "Please enter your region" });

    var postcode = new LiveValidation('postcode', { wait: 1500 } );
    postcode.add(Validate.Presence, { failureMessage: "Please enter your postcode" });

    var country = new LiveValidation('country1', {onlyOnSubmit: true } );
    country.add( Validate.Presence, { failureMessage: "Please select your country"});
    country.add( Validate.Exclusion, { within: [ 'Select' , '', 'select' ] , failureMessage: "Please select your country" }  );


    var sex = new LiveValidation('sex1');
    sex.add( Validate.Radio, { failureMessage: "Please select your gender" });
    function revalidate_sex()
    {
        var areAllValid = LiveValidation.massValidate( [ sex ] );
    }

    var age = new LiveValidation('age', { onlyOnSubmit: true });
    age.add( Validate.Exclusion, { within: [ 'Select' , '', 'select' ] , failureMessage: "Please select your age" }  );


    var password = new LiveValidation('password', { wait: 1500 } );
    password.add(Validate.Presence, { failureMessage: "Please enter your password" });

    var password_c = new LiveValidation('password_c', { wait: 1500 } );
    password_c.add(Validate.Confirmation, { match: 'password', failureMessage: "Fields do not match" });
    password_c.add(Validate.Presence, { failureMessage: "Please enter your password" });

    var simage = new LiveValidation('simage', { wait: 1500 } );
    simage.add(Validate.Presence, { failureMessage: "Please enter the security image letters" });
    simage.add(Validate.simage, {validMessage: "?", failureMessage: "Please enter a correct security image" });

    //var requesting_simage = false;

    function validate_simage()
    {
      var Valid = LiveValidation.massValidate( [ simage ] );

      if( Valid == true )
      {
         //if( !requesting_simage )
            check_simage(document.getElementById('simage').value);
      }
      else
      {
        document.getElementById('simage_status').innerHTML = '';
      }
    }
//=============================================================================

//=============================================================================
// PROPERTY INFO STEP
    var prop_country = new LiveValidation('prop_country', { onlyOnSubmit: true } );
    prop_country.add( Validate.Exclusion, { within: [ 'Select' , '', 'select', "any", "Any" ] , failureMessage: "Please select your property country" }  );
    prop_country.add(Validate.Presence, { failureMessage: "Please select your property country" });

    var prop_region = new LiveValidation('prop_region', { onlyOnSubmit: true } );
    prop_region.add( Validate.Exclusion, { within: [ 'Select' , '', 'select' ] , failureMessage: "Please select your property region" }  );
    prop_region.add( Validate.Presence, { failureMessage: "Please select your property region" });
//=============================================================================

//=============================================================================
// SUBDOMAIN STEP
    var subdomain = new LiveValidation('subdomain', { wait: 1500 });
    subdomain.add( Validate.Presence, { failureMessage: "Please enter domain name" });
    subdomain.add( Validate.Format, { pattern: /^[a-z0-9-_]+$/i, failureMessage: "Invalid domain name!" } );

    var requesting_subdomain = false;

    function validate_subdomain()
    {
      var Valid = LiveValidation.massValidate( [ subdomain ] );

      if( Valid == true )
      {
         //if( !requesting_subdomain )
            check_subdomain(document.getElementById('subdomain').value);
      }
      else
      {
        //document.getElementById('domain_status').innerHTML = '';
      }
    }
//=============================================================================

//=============================================================================
// MARKETING STEP
   var survey = new LiveValidation('survey', { onlyOnSubmit: true } );
   survey.add(Validate.Exclusion, { within: [ 'Select' , '', 'select', "any", "Any" ] , failureMessage: "Please select from where you heard about us." }  );
   survey.add(Validate.Other, { field: "survey" , other_field: "surveyother" });

   function validate_other_survey()
    {
      var Valid = LiveValidation.massValidate( [ survey ] );
    }
//=============================================================================

