$(document).ready(function() {
    
    //setup topsy for header
    $('a[rel=top-menu]').tipsy({gravity: 'n'});
    
})


function markAffiliateClick(aff_link_id){
    strOutput = $.ajax({
    url: "ajax/ajax.proxy.php?action=mark-affiliate-click&id=" + aff_link_id,
    async: false
    }).responseText;    
}


//adding products to cart
function addProductToCart(pcs_object, product_id, return_url){
    product_pcs = $('#'+pcs_object).val();
    url2open = 'http://www.kido.ro/cart_action.php?action=add_product&product_id='+product_id+'&pcs='+product_pcs+'&return_url='+return_url;
    //window.location.href = url2open;
    window.open(url2open, '_self');
}


//contul meu
function checkRegisterUserForm(){
    retVal = true;
    err_text = "";
    
    if($('#reg_prenume').val()==""){retVal = false; err_text = err_text + "- Campul 'Prenume' trebuie completat" + "<br />";}
    if($('#reg_nume').val()==""){retVal = false; err_text = err_text + "- Campul 'Nume' trebuie completat" + "<br />";}
    if($('#reg_email').val()==""){retVal = false; err_text = err_text + "- Campul 'Email' trebuie completat" + "<br />";}
    if($('#reg_passwd').val()==""){retVal = false; err_text = err_text + "- Campul 'Parola' trebuie completat" + "<br />";}
    if($('#reg_passwd_again').val()==""){retVal = false; err_text = err_text + "- Campul 'Repeta Parola' trebuie completat" + "<br />";}
    if($('#reg_passwd').val()!="" && $('#reg_passwd_again').val()!=""){
        if($('#reg_passwd').val()!=$('#reg_passwd_again').val()){
            retVal = false; 
            err_text = err_text + "- Campurile 'Parola' si 'Repeta Parola' trebuie sa fie identice" + "<br />";
        }
    }    
    
    if($('#reg_email').val()!=""){
        strOutput = $.ajax({
        url: "ajax/ajax.proxy.php?action=register-check-unique&email=" + $('#reg_email').val(),
        async: false
        }).responseText;    
        
        if(strOutput=="error"){
            retVal = false; 
            err_text = err_text + "- <b>Adresa de email a mai fost folosita pentru un alt cont pe kido.ro</b>" + "<br />";            
        }
    }
    
    if(retVal==false){
        err_text = "<strong>Contul nu a putut fi creat pentru ca am intampinat urmatoarele erori</strong><br />" + err_text;
        $('#error_container').html(err_text);
        $('#error_container').fadeIn('normal');
    }
    
    return retVal
}


//confirmare cont
function confirmAccount(secureCode){
    c_email = $('#reg_check_email').val();
    c_code = $('#reg_check_code').val();
    
    if(c_email=='' || c_code==''){
        $('#confirm_response').hide();
        $('#confirm_response').html('Completati ambele campuri pentru a valida contul!');
        $('#confirm_response').slideDown('normal');
    }else{
        strOutput = $.ajax({
        url: "ajax/ajax.proxy.php?action=register-confirm-account&email=" + c_email + "&code=" + c_code + "&secure_code=" + secureCode,
        async: false
        }).responseText;
        
        $('#confirm_response').hide();
        $('#confirm_response').html(strOutput);
        $('#confirm_response').slideDown('normal');                
    }
}

//login
function loginUser(secureCode, rType){
    l_user = $('#auth_email').val();
    l_pass = $('#auth_pass').val();        
    
    if(l_user=="" || l_pass==""){
        $('#error_container').hide();
        $('#error_container').html("<strong>Eroare</strong><br />Completeaza ambele campuri pentru autentificare");
        $('#error_container').slideDown('normal');         
    }else{        
        strOutput = $.ajax({
        url: "ajax/ajax.proxy.php?action=check-login&user=" + l_user + "&pass=" + l_pass + "&secure_code=" + secureCode,
        async: false
        }).responseText;        
        
        if(strOutput=="ok"){
            if(rType==1){window.open("http://www.kido.ro/cont/", '_self');}
            if(rType==2){window.open("http://www.kido.ro/cos/alege-adresa.html", '_self');}
            
        }else{
            $('#error_container').hide();
            $('#error_container').html("<strong>Eroare</strong><br />"+strOutput);
            $('#error_container').slideDown('normal');            
        }
    }
}


//trimitere sugestii din myaccount
function sendSuggest(){
    var regX = /\n/gi ;
    msg = $('#txt-suggest').val();
    msg = msg.replace(regX, "<br /> \n");
    strOutput = $.ajax({
    url: "ajax/ajax.proxy.php?action=add-suggest&msg=" + msg,
    async: false
    }).responseText;            
    
    $('#txt-suggest').val('')
    $('#form-suggest').hide();
    $('#form-suggest-response').html(strOutput);
    $('#form-suggest-response').slideDown('normal');
}


//adauga copil
function addKidToClient(){
    x_nume = $('#kid-name').val();
    x_what = $('#kid-what').val();
    x_day = $('#kid-birth_day').val();
    x_month = $('#kid-birth_month').val();
    x_year = $('#kid-birth_year').val();
    
    if(x_nume==""){
        $('#add-kid-response').html("<b>Eroare: trebuie completat numele!</b>");
        $('#add-kid-response').slideDown('normal');        
    } else {
        strOutput = $.ajax({
        url: "ajax/ajax.proxy.php?action=add-kid&nume=" + x_nume + "&what=" + x_what + "&day=" + x_day + "&month=" + x_month + "&year=" + x_year,
        async: false
        }).responseText;                    
        window.open("http://www.kido.ro/cont/", '_self');
    }
}


//my-account editare parola
function editAccount_editPasswd_check(){
    retVal = true;
    $('#error-container-passwd').hide();
    if($('#old_passwd').val()=="" || $('#new_passwd').val()==""){
        $('#error-container-passwd').html('<strong>Eroare!</strong><br />Ambele campuri trebuie completate');
        $('#error-container-passwd').slideDown('normal');
        retVal = false;
    }
    
    return retVal
}


//my-account editare cont
function editAccount_editDetails_check(){
    retVal = true;
    strErr = "";
    $('#error-container-account').hide();
    
    if($('#edit_prenume').val()==""){
        retVal = false;
        strErr = strErr + "- Campul 'Prenume' este obligatoriu<br />";
    }
    
    if($('#edit_nume').val()==""){
        retVal = false;
        strErr = strErr + "- Campul 'Nume' este obligatoriu<br />";
    }    
    
    if(retVal==false){
        $('#error-container-account').html('<strong>Eroare!</strong><br />'+strErr);
        $('#error-container-account').slideDown('normal');        
    }
    
    return retVal    
}


//server-msg closing
function ServerMsg_Ticker(second_remaining){
    second_remaining = second_remaining - 1;
    if(second_remaining>0){        
        $('#server-msg-closing').html('notificarea va disparea in '+second_remaining+' secunde');
        window.setTimeout(ServerMsg_Ticker, 1000, second_remaining);
    }else{
        $('#server-msg').slideUp('normal');
    }
}

//validate adauga adresa
function editAccount_checkAddAdresa(){
    retVal = true;
    strErr = "";
    $('#error-container-add-adresa').hide();        
    
    if($('#adr_strada').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi numele strazii<br />";
    }
    if($('#adr_nr').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi numarul strazii<br />";
    }    
    if($('#adr_city').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi orasul<br />";
    }    
    
    if(retVal==false){
        $('#error-container-add-adresa').html('<strong>Eroare!</strong><br />'+strErr);
        $('#error-container-add-adresa').slideDown('normal');        
    }
    
    return retVal
}

//validate edit adresa
function editAccount_checkEditAdresa(fid){
    retVal = true;
    strErr = "";
    $('#error-container-edit-adresa-'+fid).hide();        
    
    if($('#adr_strada-'+fid).val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi numele strazii<br />";
    }
    if($('#adr_nr-'+fid).val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi numarul strazii<br />";
    }    
    if($('#adr_city-'+fid).val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi orasul<br />";
    }    
    
    if(retVal==false){
        $('#error-container-edit-adresa-'+fid).html('<strong>Eroare!</strong><br />'+strErr);
        $('#error-container-edit-adresa-'+fid).slideDown('normal');        
    }
    
    return retVal
}


//confirm delete
function ConfirmDelete2Link (sUrl) {
    var answer = confirm ("Sigur vrei sa stergi?\n\rApasa OK pentru DA");
    if (answer) {window.open (sUrl, '_self');}
} 



//cart - modificare transport
function cart_alterTransport(cartSubtotal, transName, transPrice, extraValue){
    totalPrice = cartSubtotal + transPrice - extraValue;
    $('#calculation-transport').html("<span>"+transPrice+" Lei</span> Transport ("+transName+"): <div class='clear'></div>");
    $('#calculation-total').html("<span>"+totalPrice+" Lei</span> Total : <div class='clear'></div>");
    $('#transport-name').val(transName);
    $('#transport-value').val(transPrice);
}


//cart - goto adresa
function cart_gotoAdresa(){
    transName = $('#transport-name').val();
    transPrice = $('#transport-value').val();        
    reducere = $('#reducere-cart').val();        
    window.open('http://www.kido.ro/cart_action.php?action=set-transport&name='+transName+'&value='+transPrice+'&reducere='+reducere,'_self');
}


//cart finalizare
function cart_FinOrderCheckAddr(){
    retVal = true;
    strErr = "";
    $('#error-container-add-adresa').hide();        
    
    if($('#adr_strada').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi numele strazii<br />";
    }
    if($('#adr_nr').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi numarul strazii<br />";
    }    
    if($('#adr_city').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa completezi orasul<br />";
    }    
    
    if(retVal==false){
        $('#error-container-add-adresa').html('<strong>Eroare!</strong><br />'+strErr);
        $('#error-container-add-adresa').slideDown('normal');        
    }
    
    return retVal    
}


//validare formular contact
function checkContactForm(){
    retVal = true;
    strErr = "";
    $('#error_container').hide();     

    if($('#nume').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa ne spui numele tau<br />";
    }    
    
    if($('#email').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa stim adresa ta de email pentru a te putea contacta<br />";
    }        
    
    if($('#msg').val()==""){
        retVal = false;
        strErr = strErr + "- Sigur n-ai uitat ceva? Completeaza si campul 'Mesajul tau'<br />";
    }            

    if(retVal==false){
        $('#error_container').html('<strong>Pentru a ne trimite mesajul tau corecteaza urmatoarele erori:</strong><br />'+strErr);
        $('#error_container').slideDown('normal');        
    }        
    
    return retVal
}


function prepareSearch(sString){
    window.open('http://www.kido.ro/prepare_search.php?q='+encodeURIComponent(sString),'_self');
}

function indexCarousel_Switch_Proxy(ic_item, ic_container){
    $('#run_auto_carousel').val('0');
    indexCarousel_Switch(ic_item, ic_container);
}

function indexCarousel_Switch(ic_item, ic_container){        
    //hiding old selected item + showing new selected item
    $('#'+$('#selected_carousel_container').val()).fadeOut('fast', function(){
        $('#'+$('#selected_carousel_item').val()).removeClass('selected');        
        $('#selected_carousel_container').val(ic_container);
        $('#'+$('#selected_carousel_container').val()).fadeIn('fast', function(){
            $('#selected_carousel_item').val(ic_item);        
            $('#'+$('#selected_carousel_item').val()).addClass('selected');            
        });
    });
}

function indexCarousel_Auto(){
    if($('#run_auto_carousel').val()=="1"){
        cindex = $('#carousel_index').val();
        if(cindex==4){
            cindex=1;
        }else{
            cindex=parseInt(cindex)+1;
        }
        indexCarousel_Switch('item'+cindex,'icontainer'+cindex);
        $('#carousel_index').val(cindex);
        window.setTimeout(indexCarousel_Auto,5000);
    }
}

function fastCheckout_checkForm(){
    retVal = true;
    strErr = "";
    $('#error-container-fast-checkout').hide();     

    if($('#client_name').val()==""){
        retVal = false;
        strErr = strErr + "- Lipsa <b>NUME</b>! Trebuie sa ne spui numele tau<br />";
        $('#client_name').css('border-color','#CD180D');
    } else {
        $('#client_name').css('border-color','#008DDA');
    }
    
    if($('#client_prename').val()==""){
        retVal = false;
        strErr = strErr + "- Lipsa <b>PRENUME</b>! Trebuie sa ne spui prenumele tau<br />";
        $('#client_prename').css('border-color','#CD180D');
    } else {
        $('#client_prename').css('border-color','#008DDA');
    }        
    
    if($('#client_email').val()==""){
        retVal = false;
        strErr = strErr + "- Lipsa <b>EMAIL</b>! Avem nevoie de adresa ta de email pentru a comunica<br />";
        $('#client_email').css('border-color','#CD180D');
    } else {
        if(checkEmail($('#client_email').val())==false){
            retVal = false;
            strErr = strErr + "- <b>EMAIL</b> invalid! Adresa de email nu este corecta<br />";
            $('#client_email').css('border-color','#CD180D');            
        }else{
            $('#client_email').css('border-color','#008DDA');
        }        
    }        
    
    if($('#client_phone').val()==""){
        retVal = false;
        strErr = strErr + "- Lipsa <b>TELEFON</b>! Spune-ne un numar de telefon de contact<br />";
        $('#client_phone').css('border-color','#CD180D');
    } else {
        $('#client_phone').css('border-color','#008DDA');
    }                
    
    if($('#adr_order').val()==""){
        retVal = false;
        strErr = strErr + "- Lipsa <b>ADRESA</b>! Trebui sa stim adresa la care sa-ti livram coletul<br />";
        $('#adr_order').css('border-color','#CD180D');
    } else {
        $('#adr_order').css('border-color','#008DDA');
    }                
    
    if($('#adr_city').val()==""){
        retVal = false;
        strErr = strErr + "- Lipsa <b>ORAS</b>! In ce oras trimitem comanda?<br />";
        $('#adr_city').css('border-color','#CD180D');
    } else {
        $('#adr_city').css('border-color','#008DDA');
    }                        
    
    if(retVal==false){
        $('#error-container-fast-checkout').html('<strong>Pentru a trimite comanda corecteaza erorile:</strong><br />'+strErr);
        $('#error-container-fast-checkout').slideDown('normal');        
    }        
    
    return retVal    
}



function checkEmail(myEmail) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myEmail)){
        return (true)
    }
    return (false)
}


function showSidebarCartContent(){    
    if($('#cos-sidebar-linkchanger').text()=="vezi produsele"){
        $('#cos-sidebar-linkchanger').text('ascunde');
        $('#cos-sidebar-content-product-container').slideDown();
    }else{
        $('#cos-sidebar-linkchanger').text('vezi produsele');
        $('#cos-sidebar-content-product-container').hide();
    }    
}


function checkAddTestimonialForm(){
    retVal = true;
    strErr = "";
    $('#error_container').hide();     

    if($('#nume').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa ne spui numele tau<br />";
    }    
    
    if($('#email').val()==""){
        retVal = false;
        strErr = strErr + "- Trebuie sa stim adresa ta de email pentru a te putea contacta sa-ti multumim<br />";
    } else {
        if(checkEmail($('#email').val())==false){
            retVal = false;
            strErr = strErr + "- <b>EMAIL</b> invalid! Adresa de email nu este corecta<br />";            
        }
    }        
    
    if($('#opinie').val()==""){
        retVal = false;
        strErr = strErr + "- Sigur n-ai uitat ceva? Completeaza si campul 'Parerea ta'<br />";
    }            

    if(retVal==false){
        $('#error_container').html('<strong>Pentru a ne trimite parerea ta corecteaza urmatoarele erori:</strong><br />'+strErr);
        $('#error_container').slideDown('normal');        
    }        
    
    return retVal    
}


function requestAQuote(){        
    //sending content to ajax proxy
    formdetails = $('#raq-details').val();
    formdetails = formdetails.replace ( /\n/gm, '<br />' );
    strOutput = $.ajax({
    url: "ajax/ajax.proxy.php?action=request-a-quote&name=" + $('#raq-name').val() + "&email=" + $('#raq-email').val() + "&phone=" + $('#raq-phone').val() + "&who=" + $('#raq-who').val() + "&age=" + $('#raq-age').val() + "&details=" + formdetails,
    async: false
    }).responseText;    
    
    //set timed closing of modal window
    setTimeout("closeRequestAQuoteForm()",5000);
    
    //show response 
    $('#raq-response').slideDown('normal');
    
    return false
}

function closeRequestAQuoteForm(){
    //reset fields
    $('#raq-response').hide();
    $('#raq-name').val('');
    $('#raq-email').val('');
    $('#raq-phone').val('');
    $('#raq-age').val('');
    $('#raq-details').val('');
    
    
    //close window
    $('#lean_overlay').hide(); 
    $('#raq').hide();
}
