// JavaScript Document
$(document).ready(function() {
    function me(objID) {
        $(objID).css('border','2px dashed red').click(function() {
            $(this).css('border','1px inset gray');
        }
                                                     );
    }

    function stopRKey(evt) {
        var evt = (evt) ? evt : ((event) ? event : null);
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if ((evt.keyCode == 13) && (node.type=="text"))  {
            return false;
        }
    }
    document.onkeypress = stopRKey;
    $('#f1').show();
    $('#f2').hide();
    $('#f3').hide();
    $('#st1').show();
    $('#step1text').css('text-decoration','underline');
    $('#st1').click(function() {
        var er=new Array();
        if ( (!$('#visa_1').attr("checked")) && (!$('#visa_0').attr("checked")) ) {
            er.push('Please check the EU VISA field ');
            me('#euvisa');
        }
        if ( (!$('#visausa_1').attr("checked")) && (!$('#visausa_0').attr("checked")) ) {
            er.push('Please check the US VISA field ');
            me('#usvisa');
        }
        if ( $.trim( $('#first').val() ).length==0 ) {
            er.push('First name is empty');
            me('#first');
        }
        if ( $.trim( $('#family').val() ).length==0 ) {
            me('#family');
            er.push('Family name is empty');
        }

        var eml=$('#e-mail').val();
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if( ($.trim( eml ).length==0) || (reg.test(eml) == false)  ) {
            me('#e-mail');
            er.push('E-mail address is empty,incorrect or has intervals.');
        }
        if (er.length >0) {
            a=' - '+er.join("\n\n - ");
            alert(a);
            $('html, body').animate( {scrollTop:0}, 'slow');
            return false;
        } else {
            //Personal Info
            //document.forms[0].submit();
            $(this).hide();
            $('#f1').hide();
            $('#step2text').css('text-decoration','underline');
            $('#f2').show();
            $('#st2').show();
            $('html, body').animate( {scrollTop:0}, 'slow');
        }
    }
                   );
    //
    $('#st2').click(function() {
        $(this).hide();
        $('#f1').hide();
        $('#f2').hide();
        $('#f3').show();
        $('#st3').show();
        $('#step3text').css('text-decoration','underline');
        $('html, body').animate( {scrollTop:0}, 'slow');
    }
                   );

    $('#st3').click(function() {
      if($.trim( $('#cap').val() ).length==0){
        me('#cap');
        alert('Please enter the "" Verification code ""   as seen on the image.\n\nIf the image is not readable, click on it, to get new code.');
        return false;
      }
      $(this).hide();
      $('#f1').show();
      $('#f2').show();
      $('#f3').show();
      $('#st4').show();
      $('#step4text').css('text-decoration','underline');
      $('html, body').animate( {scrollTop:0}, 'slow');
    }
                   );
    $('#st4').click(function() {
      if($.trim( $('#cap').val() ).length==0){
        me('#cap');
        alert('Please enter the "" Verification code ""   as seen on the image.\n\nIf the image is not readable, click on it, to get new code.');
        return false;
      }


      $('html').animate( {scrollTop:0});
      document.forms[0].submit();
    }
                   );
    String.prototype.repeat = function(n) {
        return new Array(n + 1).join(this);
    };
    $.leading_zeros = function(n, total_digits) {
        n = n.toString();
        return '0'.repeat(total_digits - n.length) + n;
    };
    //
    var counterexp = $("label:contains('Job title')").size();
    $("#capim").click(function() {
        $.get('?newc=1');
        this.src=this.src+(Math.random()*1000000000000000);
    }
                     );
    $('#expsadd').click(function() {
        var padded_counter = $.leading_zeros(++counterexp, 1);
        var extrafield =  '<fieldset class="fs"><legend>Experience '+ padded_counter +'<span style="float:right;font-size:9px;" onclick="$(this).parent().parent().remove()">[remove]</span></legend><label>Job title</label>';
        extrafield +=  '<input type="text" name="e['+ padded_counter +'][title]"  class="itxt" value=""><br>';
        extrafield +=  '<label>Department</label><input type="text" name="e['+ padded_counter +'][dep]"   class="itxt" value=""><br>';
        extrafield +=  '<label>Employer/Company</label><input type="text" name="e['+ padded_counter +'][company]"   class="itxt" value=""><br>';
        extrafield +=  '<label>Period</label><input type="text" name="e['+ padded_counter +'][period]"   class="itxt" value=""><br>';
        extrafield +=  '<label>Description</label><input type="text" name="e['+ padded_counter +'][description]"   class="itxt" value=""><br></fieldset>';
        $('#ee').append(extrafield);
    }
                       );
    //edu
    var ceds = $("fieldset[id^='edu']").size();
    $('#edusadd').click(function() {
        var ednr = $.leading_zeros(++ceds, 1);
        var nedu = '<fieldset class="fs"><legend>Education '+ ednr;
        nedu +='<span style="float:right;font-size:9px;" onclick="$(this).parent().parent().remove()">[remove]</span>';
        nedu +='</legend><label>School / University</label><input type="text" name="edu['+ ednr +'][school]"  class="itxt" value=""><br clear="all" />';
        nedu +='<label>Period</label><input type="text" name="edu[' + ednr + '][dates]"  value=""><span class="fnote">e.g Sep 2001 / Jun 2006</span><br clear="all" />';
        nedu +='<label>Description</label><textarea name="edu[' + ednr + '][description]"  class="itxt"></textarea><br /></fieldset>';
        $('#eduplace').append(nedu);
    }
                       );
    //
    //edu
}
                 );
