function setItemSize(){
    $('input[id=id_first_name]').css('width','100px');
    $('input[id=id_last_name]').css('width','250px');
    $('input[id=id_father_name]').css('width','100px');
    $('input[id=id_birth_certificate_no]').css('width','150px');
    $('input[id=id_birth_certificate_no]').css('text-align','left');
    $('input[id=id_issuance_place]').css('width','150px');
    $('input[id=id_birth_place]').css('width','150px');
    $('input[id=id_national_code]').css('width','100px');
    $('input[id=id_national_code]').css('text-align','left');
    $('input[id=id_national_code]').css('direction','ltr');
    $('input[id=id_profession]').css('width','250px');
    $('input[id=id_profession_history]').css('width','50px');
    $('input[id=id_profession_history]').css('text-align','left');
    $('input[id=id_profession_history]').attr('maxlength', '2')
    $('input[id=id_profession_history]').css('direction','ltr');
    $('input[id=id_education]').css('width','250px');
    $('input[id=id_phone]').css('width','150px');
    $('input[id=id_phone]').css('text-align','left');
    $('input[id=id_phone]').css('direction','ltr');
    $('input[id=id_mobile]').css('width','150px');
    $('input[id=id_mobile]').css('text-align','left');
    $('input[id=id_mobile]').css('direction','ltr');
    $('input[id=id_email]').css('width','250px');
    $('input[id=id_email]').css('text-align','left');
    $('input[id=id_email]').css('direction','ltr');
    $('input[id=id_postal_code]').css('width','150px');
    $('input[id=id_postal_code]').css('text-align','left');
    $('input[id=id_postal_code]').css('direction','ltr');
    $('input[id=id_company_name]').css('width','300px');
    $('input[id=id_ceo_name]').css('width','200px');
    $('input[id=id_register_no]').css('width','150px');
    $('input[id=id_register_no]').css('text-align','left');
    $('input[id=id_register_no]').css('direction','ltr');
    $('input[id=id_national_id]').css('width','150px');
    $('input[id=id_national_id]').css('text-align','left');
    $('input[id=id_national_id]').css('direction','ltr');
    $('input[id=id_place_of_register]').css('width','150px');
    $('input[id=id_legal_type]').css('width','200px');
    $('input[id=id_legal_email]').css('width','250px');
    $('input[id=id_legal_email]').css('text-align','left');
    $('input[id=id_legal_email]').css('direction','ltr');
    $('input[id=id_legal_phone]').css('width','150px');
    $('input[id=id_legal_phone]').css('text-align','left');
    $('input[id=id_legal_phone]').css('direction','ltr');
    $('input[id=id_legal_fax]').css('width','150px');
    $('input[id=id_legal_fax]').css('text-align','left');
    $('input[id=id_legal_fax]').css('direction','ltr');
}
function changeRegisterType(){
    val = $(this).val();
    clearFormWarnings();
    if(val=='individual'){
        hideLegalInfo();
        showIndividualInfo();
        $('input[id=id_first_name]').focus();
    }else{
        hideIndividualInfo();
        showLegalInfo();
        $('input[id=id_company_name]').focus();
    }
}
function hideIndividualInfo(){
    $('input[id=id_first_name]').parent().parent().hide();
    $('input[id=id_last_name]').parent().parent().hide();
    $('input[id=id_father_name]').parent().parent().hide();
    $('input[id=id_birth_certificate_no]').parent().parent().hide();
    $('input[id=id_issuance_place]').parent().parent().hide();
    $('input[id=id_birth_place]').parent().parent().hide();
    $('#id_birth_date_0').parent().parent().hide();
    $('input[id=id_national_code]').parent().parent().hide();
    $('input[id=id_profession]').parent().parent().hide();
    $('input[id=id_profession_history]').parent().parent().hide();
    $('input[id=id_education]').parent().parent().hide();
    $('input[id=id_phone]').parent().parent().hide();
    $('input[id=id_mobile]').parent().parent().hide();
    $('input[id=id_email]').parent().parent().hide();
    $('input[id=id_postal_code]').parent().parent().hide();
}
function showIndividualInfo(){
    $('input[id=id_first_name]').parent().parent().show();
    $('input[id=id_last_name]').parent().parent().show();
    $('input[id=id_father_name]').parent().parent().show();
    $('input[id=id_birth_certificate_no]').parent().parent().show();
    $('input[id=id_issuance_place]').parent().parent().show();
    $('input[id=id_birth_place]').parent().parent().show();
    $('#id_birth_date_0').parent().parent().show();
    $('input[id=id_national_code]').parent().parent().show();
    $('input[id=id_profession]').parent().parent().show();
    $('input[id=id_profession_history]').parent().parent().show();
    $('input[id=id_education]').parent().parent().show();
    $('input[id=id_phone]').parent().parent().show();
    $('input[id=id_mobile]').parent().parent().show();
    $('input[id=id_email]').parent().parent().show();
    $('input[id=id_postal_code]').parent().parent().show();
}
function hideLegalInfo(){
    $('input[id=id_company_name]').parent().parent().hide();
    $('input[id=id_ceo_name]').parent().parent().hide();
    $('input[id=id_register_no]').parent().parent().hide();
    $('input[id=id_national_id]').parent().parent().hide();
    $('#id_date_of_register_0').parent().parent().hide();
    $('input[id=id_place_of_register]').parent().parent().hide();
    $('input[id=id_legal_type]').parent().parent().hide();
    $('input[id=id_legal_email]').parent().parent().hide();
    $('input[id=id_legal_phone]').parent().parent().hide();
    $('input[id=id_legal_fax]').parent().parent().hide();
}
function showLegalInfo(){
    $('input[id=id_company_name]').parent().parent().show();
    $('input[id=id_ceo_name]').parent().parent().show();
    $('input[id=id_register_no]').parent().parent().show();
    $('input[id=id_national_id]').parent().parent().show();
    $('#id_date_of_register_0').parent().parent().show();
    $('input[id=id_place_of_register]').parent().parent().show();
    $('input[id=id_legal_type]').parent().parent().show();
    $('input[id=id_legal_email]').parent().parent().show();
    $('input[id=id_legal_phone]').parent().parent().show();
    $('input[id=id_legal_fax]').parent().parent().show();
}
function startProcess(e){
    e.preventDefault();
    $('div#initform').fadeIn('slow', function(){
        $('input[id=id_first_name]').focus();
        $('div#introduction').hide();
        $('input[id=id_register_type]').live($.browser.msie ? 'click':'change', changeRegisterType)
    });
}

function scrollToElement(theElement){
    window.scroll(0, 400);
}

function validateFields(){
    err = false;
    $('table.jform').find('input[type=text]:visible').each(function(e){
        if($(this).val() == '' && $(this).attr('id') != 'id_education'){
            $(this).before('<i></i>');
            err = true;
        }
    });
    $('table.jform').find('select:visible').each(function(e){
        if($(this).val() == ''){
            if($(this).parent().find('i').attr('id') != '')
                $(this).before('<i></i>');
            err = true;
        }
    });
    if(err == true){
        $('div#err_empty').fadeIn('slow');
        scrollToElement('div#err_empty');
    }
    return err;
}
function clearFormWarnings(){
    $('div#initform').find('div.error_message').each(function(){
        $(this).hide();
    })
    $('table.jform').find('i').each(function(){
        $(this).remove();
    })
}

function submitForm(e){
    e.preventDefault();
    clearFormWarnings();
    if(validateFields() == false){
        frm = {'register_type': $('input[id=id_register_type]:checked').val()}
        $('table.jform').find('input[type=text]:visible').each(function(e){
            frm[$(this).attr('name')] = $(this).val();
        });
        $('table.jform').find('select:visible').each(function(e){
            frm[$(this).attr('name')] = $(this).val();
        });
        $('div.indicator').show();
        $.post(document.forms[0].action, frm, function(data){
            $('div.indicator').hide();
            if(data.indexOf('~') == 0){
                tracking_number = data.substr(1,8)
                hideLegalInfo();
                hideIndividualInfo();
                $('input[id=id_register_type]').parent().parent().parent().hide();
                $('#id_how_hear_us').parent().parent().hide();
                $('input[id=id_captcha_code]').parent().parent().hide();
                $('#id_submit_form').parent().parent().hide();
                $('div.success_message center').append('<b>'+tracking_number+'</b>');
                $('div.success_message').fadeIn('slow');
            }
            else
                if(data.indexOf(':') != -1){
                    $('div#err_custom').html('<center>'+data.split(':')[0]+'</center>').fadeIn('slow');
                    scrollToElement('div#err_custom');
                    $('table.jform').find('img.cap').attr('src', '/captcha/'+data.split(':')[1]);
                }else{
                    if(data == '1'){
                        $('div#err_date').fadeIn('slow');
                        scrollToElement('div#err_date');
                    }else if(data.indexOf('@') != -1){
                        d=data.split(',');
                        for(i=1;i<d.length;i++){
                            $('input[id=id_'+d[i]+']').before('<i></i>');
                        }
                        $('div#err_validate').fadeIn('slow');
                        scrollToElement('div#err_validate');
                    }else{
                        $('div#err_custom').html('<center>'+data+'</center>').fadeIn('slow');
                        scrollToElement('div#err_custom');
                    }
                }
        });
    }
}
$(document).ready(function(){
   $('input[id=id_captcha_code]').css('width','80px');
   $('input[id=id_captcha_code]').css('text-align','center');
   $('input[id=id_captcha_code]').css('text-transform','uppercase');
   setItemSize();
   if($('input[id=id_register_type]:checked').val() == 'individual'){
    showIndividualInfo();
    hideLegalInfo();
   }else{
    hideIndividualInfo();
    showLegalInfo();
   }
   $('a#start_process').live('click', startProcess);
   $('#id_submit_form').live('click', submitForm);
});