$(function(){

    $('.forgot').click(function(){
        if($('input[name="email2"]').val() != '' ){
            $('form[name="forgotpass"]').submit();
        }else{
            alert('Bitte tragen Sie eine E-Mail Adresse ein!');
        }    
    });
    
    $('.showForgotContainer').click(function(){
        $('.col2_1,.col2_2').hide();
        $('.col1').show();    
    });
    
    
    $('.couponSubmit').click(function(){
        if($('input[name="coupon"]').val() != '' && $('input[name="coupon"]').val() != ' '){
            $('form[name="couponForm"]').submit();
        }else{
            alert("Bitte tragen Sie ein Gutscheincode ein!");
        }        
    });

    
    $('.addToCart').click(function()
    {
        $amount = $(this).parent().find('input').val();
        $artnr = $(this).attr('rel');
        if($amount != 0 && $artnr != '')
        {
            window.location.href = 'addToCart.php?artnr='+ $artnr +'&amount='+ $amount;
        }else{
            alert('Bitte geben Sie eine Menge an!');
        }    
    });
    
    
    
    $('.login').click(function() {
        checkLoginForm();
    });
    
    $('input[name="kdnr"],input[name="pass"]').keypress(function(event){
       if(event.which == 13){
        checkLoginForm(); 
       }
    })
    
    function checkLoginForm() {
        $kdnr = $('input[name="kdnr"]'); 
        $pass = $('input[name="pass"]'); 
        alertMSG = '';
        
        if( $kdnr.val() == '' ){
            alertMSG += 'Bitte tragen Sie eine korrekte E-Mail Adresse ein.'+"\n";                
        }
        
        if($pass.val() == ''){
            alertMSG += 'Bitte tragen Sie ein Passwort ein.';
        }
        
        
        if(alertMSG != ''){
            alert(alertMSG);
        }else{
           $('form[name="loginForm"]').submit(); 
        }    
    }
    
    
    
    
    $('.refresh').click(function(){
        $('form').submit();    
    });
    
    
    $('.regist').click(function(){
        window.location.href = 'register.php';    
    });
    
    
    
    $('.showDetails').click(function(){
        
        error = false;
        $radio = 0;
        
        if( validateForm('form[name="formdata"]') != true){ 
            error = true; 
        }
        
        //Zahlung
        if($('.vorkasse').attr('checked') == 'checked') $radio += 1;
        if($('.nachname').attr('checked') == 'checked') $radio += 1;
        if($('.rechnung').attr('checked') == 'checked') $radio += 1;
        if($('.lastschrift').attr('checked') == 'checked') $radio += 1;
        
        
        if($radio == 0){
            if( $('.paymentBox').parent().hasClass('errorVali') == false ){
                $('.paymentBox').wrap('<div class="errorVali"></div>');
            }
            error = true;
        }else{
            if( $('.paymentBox').parent().hasClass('errorVali') ){
                $('.paymentBox').unwrap();
            }
        }

        if(error == false){
            $('form').submit();
        }
                        
    });
    
    
    
    $('.setOrder').click(function(){
        if($('input[name="agbAccept"]').attr('checked') == 'checked'){
            $('form').submit();    
        }else{
            alert('Bitte akzeptieren Sie unsere AGB um mit der Bestellung fortzufahren.');
        }
           
    });
    
    $('.amountChange').blur(function(){
        $('form[name="warenkorbForm"]').submit();    
    });
    
})



function is_email(email){
    if(typeof email == 'undefined'){
        return false;    
    }
    
    var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
	
    if(result > -1){ 
	   return true; 
    } else { 
        return false; 
    }
        
}

