/********************************************

        -- Bildungslounge Signup JS --

    @author     Kulikov Alexey
    @since      01.12.2009

 ********************************************/

/***
    @purpose    Submit a SignUp form part checking all the mandatory inputs and such
    @param      string  -   the name of the submitted part
 ***/
function submitPart1(part){
    var inputs  = $$('.inputMandatory');
    var clean   = true;
    var pwerror = false;
    var owner   = false;

    //see if I have description here
    /*
    if(part == ''){
        var myEditor = nicEditors.findEditor('area1');
        $('area1').value = myEditor.getContent();
    }
    */

    for(var i = 0; i < inputs.length; i++){
        if(inputs[i].type != 'file'){
            inputs[i].value = inputs[i].value.strip();
        }

        if((inputs[i].type == 'checkbox' && !inputs[i].checked) || (!inputs[i].value && inputs[i].offsetHeight)){ //check only visible objects
            inputs[i].addClassName('inputError');
            clean = false;
        }else{
            inputs[i].removeClassName('inputError');
        }
    }

    if(part == 'confirm'){
        if($('pw1')){
            if($('pw1').value != $('pw2').value){
                pwerror = true;
            }

            if($('badOwner').style.display != 'none'){
                owner = true;
            }
        }
    }

    //so, it a mandatoru field is not filled or the passwords are not identical or the email login is
    //occupied by someone else then ring the bells of fury
    if(!clean || pwerror || owner){
        if(owner){
            $('errorMessage3').blindDown();
            setTimeout(function(){ $('errorMessage3').blindUp(); }, 3000);
            $('ownerMail').addClassName('inputError');
        }

        if(pwerror){
            $('errorMessage2').blindDown();
            setTimeout(function(){ $('errorMessage2').blindUp(); }, 3000);
            $('pw1').addClassName('inputError');
            $('pw2').addClassName('inputError');
        }

        if(!clean){
            $('errorMessage').blindDown();
            setTimeout(function(){ $('errorMessage').blindUp(); }, 3000);
        }

        Effect.Shake('button');
        return false;
    }



    $('signupPart1').submit();
    return true;
}

function cloneAttachments(){
    cloneRow('bilder');
}


/***
    @purpose    Clone a DOM element
 ***/
function cloneDiv(id){
    var root    = $(id).parentNode;
    var clone   = $(id).cloneNode(true);
    clone.id    = 'trainer_'+trainer;
    //clone.style.display = 'none';
    root.appendChild(clone);
    $('trainer_'+trainer).show();
    trainer++;

    var dfields = $$('#'+clone.id+' a.admin');
    for(var i = 0; i<dfields.length; i++){
        dfields[i].href = 'javascript:killDiv(\''+clone.id+'\')';
    }
    //window.location.hash = 'trainer_'+trainer;

    //var objDiv = document.getElementById("divExample");
    //objDiv.scrollTop = objDiv.scrollHeight;
    scrollTo(0,99999999999);
}




/***
    @purpose    Pretty remove an element
 ***/
function killDiv(id){
    if(confirm('Sind Sie sicher?')){
        $(id).switchOff();
        dropElement.delay(0.7,id);
    }
    //console.log(obj);
}


function dropImage(id, number, trainer, baseid){
    if(confirm('Sind Sie sicher?')){
        $(id).switchOff();
        dropElement.delay(0.7,id);
        showElement.delay(0.7,id+'add');

        (new AjaxRequest).send({
            url     : "/ajax/index.php",
            data    : {
                action: 'dropPremImage',
                key: number,
                tid: trainer,
                image_id: baseid
            },

            onSuccess  : function(data, info)
            {

            }
          });
    }
}

function showElement(id){
    $(id).show();
}

/***
    @purpose    To remove a DOM element
 ***/
function dropElement(id){
    if(document.getElementById(id)){
        var em=document.getElementById(id);
        em.parentNode.removeChild(em);
    }
}



/***
    @purpose    Check that an owner email is already taken
                or not
 ***/
var timer = null;
function checkOwner(obj){
    if (timer) {
        window.clearTimeout(timer);
    }
    timer = window.setTimeout( function() {
                                                timer = null;
                                                checkOwnerCallback(obj);
                                            }, 400 );

}

function checkOwnerCallback(obj){
    (new AjaxRequest).send({
        url     : "/ajax/index.php",
        data    : {
            action: 'checkOwner',
            email: obj.value
        },

        onSuccess  : function(data, info)
        {
            if(data.uid){
                $('badOwner').show();
                $('ownerMail').addClassName('inputError');
            }else{
                $('badOwner').hide();
                $('ownerMail').removeClassName('inputError');
            }
        }
      });
}


function checkSubCats(obj){
    if(obj.value){
        $('category_id').hide();
    }else{
        $('category_id').show();
    }
}


function checkSubmit(part){
    alert(1);
    return false;
}
