var ajaxInProcess=false;
var state=new Array();
state['txtName']=0;
state['txtSurname']=0;
state['selGender']=0;
state['date']=0;
state['txtZip']=0;
state['txtTown']=0;
state['selCountry']=0;
state['txtUsername']=0;
state['txtPassword']=0;
state['txtPasswordConf']=0;
state['txtEmail']=0;
state['txtCaptcha']=0;


function validate(field, value)
{
        if (value.trim()=='') return;
        var jSonRequest = new Request.JSON
        (
                {
                        url: 'index.php/ajax/ajaxcheck',
                        data: {'field': field, 'value': value},
                        method: 'post',
                        onComplete: function(json)
                        {
                                ajaxInProcess=false;
                                highlightError(json.status,json.field);
                        },
                        onRequest: function()
                        {
                                ajaxInProcess=true;
                        },
                        onError: function()
                        {
                                ajaxInProcess=true;
                                alert('Server Error!');
                        }
                }
        );

        jSonRequest.send();
}

function validateJS(field, value)
{
        if (value.trim()=='') return;
        switch (field)
        {
                case 'txtPassword':
                        var re=/^[A-z0-9a-z]{6,}$/;
                        if (re.test(value.trim())) highlightError(1,field);
                        else highlightError(0,field);
                        break;
                case 'txtPasswordConf':
                        if (value==$('txtPassword').value) highlightError(1,field);
                        else highlightError(0,field);
                        break;
                case 'txtName':case 'txtSurname':case 'txtTown':
                        var re=/^[a-zA-Z\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u03B2\s\.]+$/;
                        if (re.test(value.trim())) highlightError(1,field);
                        else highlightError(0,field);
                        break;
                case 'selCountry':
                                $('selCountrybund1').disabled=true;
                                $('selCountrybund2').disabled=true;
                                $('selCountrybund3').disabled=true;

                        if (value=='0')
                        {
                                $('txtCountry').disabled=true;
                                highlightError(0,field);

                        }
                        else if (value=='input')
                        {
                                state['selCountry']=0;
                                $('txtCountry').disabled=false;
                                $('txtCountry').focus();
                                $('txtCountry').select();
                                $('txtCountry').set('style','background: #FFFFFF');
                        }
                        else
                        {
                                highlightError(1,field);
                                $('txtCountry').disabled=true;
                                $('txtCountry').set('style','background: #4E3224; border: 0;');

                                if (value==_lang_register_country_de)
                                {
                                $('selCountrybund1').disabled=false;
                                $('selCountrybund2').disabled=true;
                                $('selCountrybund3').disabled=true;
                                }
                                if (value==_lang_register_country_au)
                                {
                                $('selCountrybund1').disabled=true;
                                $('selCountrybund2').disabled=false;
                                $('selCountrybund3').disabled=true;
                                }
                                if (value==_lang_register_country_sz)
                                {
                                $('selCountrybund1').disabled=true;
                                $('selCountrybund2').disabled=true;
                                $('selCountrybund3').disabled=false;
                                }
                        }
                        break;
                case 'txtCountry':
                        var re=/^[a-zA-Z\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u03B2\s\.]+$/;
                        if (re.test(value.trim())) highlightError(1,'selCountry');
                        else highlightError(0,'selCountry');
                        break;
                case 'selGender':
                        if (value=='0') highlightError(0,field);
                        else highlightError(1,field);
                        break;
                case 'selSearch':
                        if (value=='0') highlightError(0,field);
                        else highlightError(1,field);
                        break;
                case 'txtZip':
                        var re=/^[0-9]{4,5}$/;
                        if (re.test(value.trim())) highlightError(1,field);
                        else highlightError(0,field);
                        break;
                default:
                        break;
        }
}

function validateADM(field, value)
{
        if (value.trim()=='') return;
        switch (field)
        {
                case 'txtPassword':
                        var re=/^[A-z0-9a-z]{6,}$/;
                        if (re.test(value.trim())) highlightErrorAdm(1,field);
                        else highlightError(0,field);
                        break;
                case 'txtPasswordConf':
                        if (value==$('txtPassword').value) highlightErrorAdm(1,field);
                        else highlightError(0,field);
                        break;
                case 'txtName':case 'txtSurname':case 'txtTown':
                        var re=/^[a-zA-Z\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u03B2\s\.]+$/;
                        if (re.test(value.trim())) highlightError(1,field);
                        else highlightErrorAdm(0,field);
                        break;

        }
}

function validateDate()
{
        var f=document.frmRegister;
        var d=f.selDay.value;
        var m=f.selMonth.value;
        var y=f.selYear.value;

        if (d*m*y!=0)
        {
                m--;
                var date=new Date();
    date.setFullYear(y,m,d);
                 if (date.getMonth() == m) highlightError(1,'date');
                 else highlightError(0,'date');
        }
}

function highlightError(status,field)
{
        var statusBar=$(field+'Status');
        if (status!='ERROR')
        {
                if (status=='1')
                {
                        statusBar.getParent().set('style','background: #4E3224; border: 0;');
                        statusBar.set('text','OK');
                        statusBar.fade(0);
                }
                else
                {
                        statusBar.setStyles
                        (
                                {
                                        display:'inline',
                                        opacity: 0
                                }
                        );
                        statusBar.set('text','Error');
                        statusBar.fade(1);
                        statusBar.getParent().set('style','background: #C91A21');
                }
                state[field]=status;
        }
}
function highlightErrorAdm(status,field)
{
        var statusBar=$(field+'Status');
        if (status!='ERROR')
        {
                if (status=='1')
                {
                        statusBar.getParent().set('style','background:#F1F1ED;');
                        statusBar.set('text','OK');
                        statusBar.fade(0);
                }
                else
                {
                        statusBar.setStyles
                        (
                                {
                                        display:'inline',
                                        opacity: 0
                                }
                        );
                        statusBar.set('text','Error');
                        statusBar.fade(1);
                        statusBar.getParent().set('style','background: #C91A21');
                }
                state[field]=status;
        }
}
function submitForm()
{
        var f=document.frmRegister;
        var flag=true;
        for (key in state)
        if (state[key]==0)
        {
                flag=false;
                break;
        }
        if (!f.chkAgb.checked)
        {
                $('chkAgbStatus').set('text','You must check this field');
                $('chkAgbStatus').getParent().set('style','background: #ff7777');
                flag=false;
        }
        else
        {
                $('chkAgbStatus').empty();
                $('chkAgbStatus').getParent().set('style','background: #fff');
        }

        if (flag && !ajaxInProcess)
        {
                f.submit();
        }
        else
        {
                for (key in state)
                {
                        if (state[key]==0)
                        {
                                highlightError(0,key);
                        }
                }
        }
}

function refreshCaptcha()
{
        area=$('captchaArea');
        area.empty();
        var jSonRequest = new Request.JSON
        (
                {
                        url: 'index.php/ajax/refresh_captcha',
                        data: {'act': 'refresh'},
                        method: 'post',
                        onComplete: function(json)
                        {
                                area.empty();
                                area.set('html',json.image);
                        },
                        onRequest: function()
                        {
                                area.set('text','Loading....');
                        },
                        onError: function()
                        {
                                area.set('text','Error! Try Letter!');
                        }
                }
        );

        jSonRequest.send();
}
function selectLands(field, value)
{
        switch (field)
        {
        			 case 'selCountry':
                                $('selCountrybund1').disabled=true;
                                $('selCountrybund2').disabled=true;
                                $('selCountrybund3').disabled=true;


                        if (value=='input')
                        {
                                state['selCountry']=0;
                                $('txtCountry').disabled=false;
                                $('txtCountry').focus();
                                $('txtCountry').select();
                                $('txtCountry').set('style','background: #FFFFFF');
                        }
                        else
                        {
                                $('txtCountry').disabled=true;
                                $('txtCountry').set('style','background: #4E3224; border: 0;');
                                if (value==_lang_register_country_de)
                                {
                                $('selCountrybund1').disabled=false;
                                $('selCountrybund2').disabled=true;
                                $('selCountrybund3').disabled=true;
                                }
                                if (value==_lang_register_country_au)
                                {
                                $('selCountrybund1').disabled=true;
                                $('selCountrybund2').disabled=false;
                                $('selCountrybund3').disabled=true;
                                }
                                if (value==_lang_register_country_sz)
                                {
                                $('selCountrybund1').disabled=true;
                                $('selCountrybund2').disabled=true;
                                $('selCountrybund3').disabled=false;
                                }
                        }
                        break;


                default:
                        break;
        }
}
function mustRegister()
{
        alert (_lang_register_must_register);
        return;
}