
var email_ok = false;
var subdomain_ok = false;
var simage_ok = false;
// On document ready do these:
$(document).ready(function()
{
    $("#but_step2").click(function()
    {
       if( jcheck_step1() === false )
       return;

       if( $("#promo").attr("value") == 'CA111' || $("#promo").attr("value") == 'ca111' )
       {
            $("#buttons2").hide("slow", function(){  $("#loading2").show("slow");  });
            register();
       }
       else
       {
           $("#step1").hide();
           $("#step2").show();
       }
    })

    $("#register").click(function()
    {
       if( jcheck_step2() === false )
       return;

       $("#buttons").hide("slow", function(){  $("#loading").show("slow");  });

       register();
    })

    $("#back").click(function()
    {
       $("#step2").hide();
       $("#step1").show();
    })
});
//

//register
function register()
{
    var options = {};
    // crawl each input field and grab its value
    $(".boxwide-info").find("input, select").each(function()
      { options[$(this).attr("id")] = $(this).attr("value");}
    );
    options['last_visited_page'] = $("#last_visited_page").attr("value"); 
    options['referer'] = $("#referer").attr("value");
    options['referer_from_vp'] = $("#referer_from_vp").attr("value");

    $.post("../include/register.php", options, function(data){
        if( data == "1" )
        {   $("#step1").hide();
            $("#step2").hide();
            $("#step3").show();
        }
        else
        { $("#loading2").hide("slow");
          $("#buttons2").show("slow");
          $("#loading").hide("slow");
          $("#buttons").show("slow");
          alert("error: "+data);
        }
    });
}
//

// red border, error message in title
function errorit( id, error)
{
    $("#"+id).css(  "border", "solid red 2px" );
    $("#"+id).attr( "title",  error );
    $("#"+id).parent().find("span").remove();
    $("#"+id).parent().append('<span style="float:right;color:red;font-size:9px;">'+error+'</span>' );
}

// take off the red border and error message
function reset( id )
{
    $("#"+id).css(  "border", "" );
    $("#"+id).attr( "title",  "" );
    $("#"+id).parent().find("span").remove('');
}


// check if field is filled properly
function jcheck( id, type )
{
    var val = $("#"+id).attr("value");
    // types: text, email, phone, accept, simage, subdomain

    // Text check
    if( id != '' && type == "text" )
    {
      if( val == "" )
      {
        errorit( id, 'This field can not be empty...');
        return false;
      }
      else
      {
        reset( id );
        return true;
      }
    }

    // Pass check
    if( id != '' && type == "pass" )
    {
      if( val == "" )
      {
        errorit( id, 'This field can not be empty...');
        return false;
      }
      else if(  verifyPass(val) === false )
      {
        errorit( id, 'Please use only numbers and/or letters...');
        return false;
      }
      else
      {
        reset( id );
        return true;
      }
    }

    // Phone check
    if( id != '' && type == "phone" )
    {
      if( val == '' || verifyPhone(val) === false)
      {
         errorit( id, 'Please check the phone number...');
         return false;
      }
      else
      {
        reset( id );
        return true;
      }
    }


    // Email check
    if( id != '' && type == "email" )
    {
      if( val == '' || verifyEmail(val) === false )
      {
        errorit( id, 'Please check the email address...');
        return false;
      }
      else
      {
        $.get("include/check_email.php?email="+val, function(data){
           var result = handleres( id, data );
           if( data == "1" )
           email_ok = true;
           else
           email_ok = false;
        });
        return email_ok;
      }
    }

    // Confirm check
    if( id != '' && type == "confirm" )
    {
      if( val == '' || $('#'+id).attr("value") != $('#'+id+'_c').attr("value") )
      {
         errorit( id+'_c', 'Fields do not match...');
         return false;
      }
      else
      {
         reset( id+'_c' );
         return true;
      }
    }

    // Accept check
    if( id != '' && type == "accept" )
    {
      if( $("#"+id).attr("checked") == false )
      {
        errorit( id, 'You have to accept our terms and conditions to proceed...');
        return false;
      }
      else
      {
        reset( id );
        return true;
      }
    }

    // Simage check
    if( id != '' && type == "simage" )
    {
      if( val == '' )
      {
        errorit( id, 'Please enter the numbers from the security image...');
        simage_ok = false;
      }
      else
      {
        $.get("include/security_image_check.php?simage="+val, function(data){
           simage_ok = handleres( id, data );

        });
      }
      return simage_ok;
    }

    // Subdomain check
    if( id != '' && type == "subdomain" )
    {
      if( val == '' || val == 'www' || val == 'admin' || val == 'member' )
      {
        errorit( id, 'Please choose a diferent subdomain name...');
        return false;
      }
      else
      {
        $.get("include/check_subdomain.php?subdomain="+val, function(data){
           var result = handleres( id, data );
           if( data == "1" )
           subdomain_ok = true;
           else
           subdomain_ok = false;

        });
        return subdomain_ok;
      }
    }

    // Dropdown check
    if( id != '' && type == "dropdown" )
    {
      if( val == "" || val == "Select" || val == "select" )
      {
        errorit( id, 'Please choose a survey question...');
        return false;
      }
      else
      {
        reset( id );
        return true;
      }
    }
}

// doublecheck step1 fields before moving to next step
function jcheck_step1()
{            // check field    id         type
    var name1     =  jcheck( 'name1',    'text'    );
    var name2     =  jcheck( 'name2',    'text'    );
    var phone     =  jcheck( 'phone',    'phone'   );
    var email     =  jcheck( 'email',    'email'   );
    var email2    =  jcheck( 'email',    'confirm' );
    var password  =  jcheck( 'password', 'pass'    );
    var password2 =  jcheck( 'password', 'confirm' );
    var simage    =  jcheck( 'simage',   'simage'  );

    var accept    =  jcheck( 'accept',   'accept'  );
    if( name1     !== false &&
        name2     !== false &&
        phone     !== false &&
        email     !== false &&
        email2    !== false &&
        password  !== false &&
        password2 !== false &&
        simage    !== false &&
        accept    !== false
      )
    return true;
    else
    return false;
}
//

// doublecheck step2 fields before starting registration
function jcheck_step2()
{   // check field    id         type
    var subdomain = jcheck( 'subdomain', 'subdomain' );
    var survey    = jcheck( 'survey', 'dropdown'     );
    if( subdomain !== false && survey !== false )
    return true;
    else
    return false;
}
//

// handle returned result.
function handleres( id, res )
{
    if( res != "1" ) // should be 1 if all ok.
    {
      errorit( id, res);
      return false;
    }
    else            // else, print error
    {
      reset( id );
      return true;
    }
}
//

// regexp email verify
function verifyEmail(email){
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
var string = email;
     if (string.search(emailRegEx) == -1)
          return false;
     return true;
}
//

// regexp phone verify
function verifyPhone(phone){
var phoneRegEx = /^[\ \(\)0-9+-]{6,22}$/i;
var string = phone;
     if (string.search(phoneRegEx) == -1)
          return false;
     return true;
}
//

// regexp pass verify
function verifyPass(pass){
var passRegEx = /^[0-9a-zA-Z ]{1,100}$/i;
var string = pass;
     if (string.search(passRegEx) == -1)
          return false;
     return true;
}
//