// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function link_to_detail(id,code)
{
    var val=document.getElementById('record_versions').value; 
    return document.location.href='/products/show_product/'+id+'?code='+ code +'&versions='+val;
}


function validate_form_load(){
    var file = document.getElementById("data_csv_file").value;
    if (file == ""){alert('Select a csv File'); return false;}
    
}
function validate_contact_form(lang){
    var name = document.getElementById("record_name").value;
    var company = document.getElementById("record_company").value;
    var address = document.getElementById("record_address").value;
    var city = document.getElementById("record_city").value;
    var state = document.getElementById("record_state").value;
    var country = document.getElementById("record_country").value;
    var phone = document.getElementById("record_phone").value;
    var email = document.getElementById("record_email").value;
    
    if(name == ""){ 
        if(lang == "eng"){alert("Name is mandatory");}else{alert("Nome è obbligatoria");}
        return false;
    }
    if(company == ""){ 
        if(lang == "eng"){alert("Company is mandatory");}else{alert("Azienda è obbligatoria");}
        return false;
    }
    if(address == ""){ 
        if(lang == "eng"){alert("Address is mandatory");}else{alert("Indirizzo è obbligatoria");}
        return false;
    }
    if(city == ""){ 
        if(lang == "eng"){alert("City is mandatory");}else{alert("Città è obbligatoria");}
        return false;
    }
    if(state == ""){ 
        if(lang == "eng"){alert("State is mandatory");}else{alert("Stato è obbligatoria");}
        return false;
    }
    if(country == ""){ 
        if(lang == "eng"){alert("Country is mandatory");}else{alert("Nazione è obbligatoria");}
        return false;
    }
    if(phone == ""){ 
        if(lang == "eng"){alert("Phone is mandatory");}else{alert("Telefono è obbligatoria");}
        return false;
    }
    if(email == ""){ 
        if(lang == "eng"){alert("E-mail is mandatory");}else{alert("E-mail è obbligatoria");}
        return false;
    }
    //new Ajax.Request('/dashboard/send_mail',{asynchronous:true, evalScripts:true,parameters:Form.serialize('contact_form')});return false;    
}

function send_register(lang,action){
    var login = document.getElementById('record_login').value;
    var region =  document.getElementById('record_region').value;
    var name =  document.getElementById('record_name').value;
    var address = document.getElementById('record_address').value;
    var city = document.getElementById('record_city').value;
    var province = document.getElementById('record_province').value;
    var cap  = document.getElementById('record_cap').value;
    var phone = document.getElementById('record_phone').value;
    var piva = document.getElementById('record_p_iva').value;
    var fcode = document.getElementById('record_fcode').value;
    var tax_code = document.getElementById('record_tax_code').value;
    var accept =document.getElementById('accept');

    var company = document.getElementsByName('record[type_company]');
    var citizens = document.getElementsByName('record[citizen]');

    if(login == ""){ 
        if(lang == "eng"){alert("E-mail is mandatory");}else{alert("E-mail è obbligatoria");}
        return false;
    }
    if(action == 'create'){
       var password = document.getElementById('record_password').value;
        if(password == ""){ 
            if(lang == "eng"){alert("Password is mandatory");}else{alert("Password è obbligatoria");}
            return false;
        }
    }
    else{
        var new_pwd = document.getElementById('new_password').value;
        var confirm_pwd = document.getElementById('new_con_password').value;
       if(new_pwd != ""){
         if(new_pwd != confirm_pwd){
             if(lang == "eng"){alert("Password are different");}else{alert("Password non corrisponde");}
             return false;
         }
        }
    }
    if(company[0].checked == true && region == ""){ 
        if(lang == "eng"){alert("Region is mandatory");}else{alert("Region Sociale è obbligatoria");}
        return false;
    }
    if(company[1].checked == true && name == ""){ 
        if(lang == "eng"){alert("Name is mandatory");}else{alert("Nome è obbligatoria");}
        return false;
    }
    if(address == ""){ 
        if(lang == "eng"){alert("Address is mandatory");}else{alert("Indirizzo è obbligatoria");}
        return false;
    }
    if(city == ""){ 
        if(lang == "eng"){alert("City is mandatory");}else{alert("Citá Sociale è obbligatoria");}
        return false;
    }
    if(citizens[0].checked == true  && province == ""){ 
        if(lang == "eng"){alert("Province is mandatory");}else{alert("Provincia è obbligatoria");}
        return false;
    }
    if(cap == ""){ 
        if(lang == "eng"){alert("CAP is mandatory");}else{alert("CAP è obbligatoria");}
        return false;
    }
    if(phone == ""){ 
        if(lang == "eng"){alert("Phone is mandatory");}else{alert("Telefono è obbligatoria");}
        return false;
    }
    if(citizens[0].checked == true && company[0].checked == true && piva == ""){ 
        if(lang == "eng"){alert("Tax is mandatory");}else{alert("P.IVA è obbligatoria");}
        return false;
    }
    if(citizens[0].checked == true && fcode == ""){ 
        if(lang == "eng"){alert("Fiscal Code is mandatory");}else{alert("Codice Fiscale è obbligatoria");}
        return false;
    }
    if( (citizens[1].checked == true || citizens[2].checked == true) && company[0].checked == true && tax_code == ""){ 
        if(lang == "eng"){alert("Tax Code is mandatory");}else{alert("Tax Code è obbligatoria");}
        return false;
    }
    
    
    if (accept.checked == false){
        if(lang == "eng"){alert("Accept the conditions");}else{alert("Accetto di condizioni");}
        return false;
    }
    //new Ajax.Request('/user/create',{asynchronous:true, evalScripts:true,parameters:Form.serialize('new_register')});return false;
    
}

function send_to_forgot_pwd(lang){
    
    var login = document.getElementById('login_account').value;
    if(login == ""){ 
        if(lang == "eng"){alert("E-mail is mandatory");}else{alert("E-mail è obbligatoria");}
        return false;
    }
    
}

function calculate_total(){
    new Ajax.Request('/products/calculate_total',{asynchronous:true, evalScripts:true,parameters:Form.serialize('cart_form')});return false;
}

function ajax_request(lang){
    var i;
    var ban = false;
    var prods = document.getElementsByName("product");
    var quantity = document.getElementById("select_quantity").value;
    for (i = 0; i < prods.length; i++)
        {
            if(prods[i].checked == true)
                ban= true;
        }
        
        if(ban == false){ 
            if(lang == "eng"){alert("Select a product");}else{alert("Va selezionato un prodotti");}
            return false;
        }
        if(quantity == ""){ 
            if(lang == "eng"){alert("Quantity is mandatory");}else{alert("Quantità è obbligatoria");}
            return false;
        }
        new Ajax.Request('/products/add_cart',{asynchronous:true, evalScripts:true,parameters:Form.serialize('products_form')});return false;
    }
    
    function send_order_ajax(){
        new Ajax.Request('/products/send_order',{asynchronous:true, evalScripts:true,parameters:Form.serialize('order_confirm')});return false;
    }
    
    function MM_swapImgRestore() { //v3.0
        var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
    }
    function MM_preloadImages() { //v3.0
        var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
            var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
        
    }
    
    function MM_findObj(n, d) { //v4.01
        var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
            d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
        if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
        for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
        if(!x && d.getElementById) x=d.getElementById(n); return x;
    }
    
    function MM_swapImage() { //v3.0
        var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
        if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
    }
    function load_google_maps(){
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            var lat = 45.453197000000003;
            var lon=  9.2005420000000004;
            var html ='<div class="font7"><b> Buttura Mario srl</b><br>Corso di Porta Romana 121, 20122 Milano <br>';
            html = html+ 'Telephone +39 02 5456353<br>Telephone +39 02 5456353<br> buttura@buttura.it</div>';
            map.setCenter(new GLatLng(lat, lon), 16);
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.addOverlay(new GMarker(new GPoint(lon,lat)));
            map.openInfoWindowHtml(map.getCenter(),html);
        }
    }
    function show_offert(id){
        var offer = document.getElementById('offer_'+ id);
        var tbl_h = $('products_table').getHeight();
        var cbl_h = $('colors_table').getHeight();
        var wscreen  = screen.width;
        $('offer_'+id).setStyle({top: (cbl_h+(tbl_h/2)+200) + "px"});
        $('offer_'+id).setStyle({left: ((wscreen/2)-171) + "px"});
        new Effect.Grow('offer_'+id);
    }
    
    function handleResponse(request){
        window.location.reload(false);
    }
    
    function popup(mylink, windowname)
    {
        if (! window.focus)return true;
        var href;
        if (typeof(mylink) == 'string')
            href=mylink;
        else
            href=mylink.href;
        window.open(href, windowname, 'width=600,height=400,scrollbars=yes');
        return false;
    }
    
    function check_products( products){
        var total = 0;
        var selected = false;
        for(var i=0; i< products.length; i++){
            var element = $(products[i]);
            if(element.checked == true){
             selected = true;
             total+= parseFloat(element.value);
            }
        }
        if(selected == false){
            alert("Va selezionato un prodotti");return false;
        }
            
        if(total < 30){
            alert("L'importo minimo richiesto per l'invio della merce è 30 €. Vi preghiamo di contattarci direttamente e di verificare le nostre condizioni di fornitura");
            return false;
         }
    }
    
    function change_register_form(){
        var fields = document.getElementsByClassName('register_item');
        var elements = document.getElementsByClassName('province');
        var company = document.getElementsByName('record[type_company]');
        var citizens = document.getElementsByName('record[citizen]');
        var el = document.getElementById('div_lbl_cap');
        var form_rows = new Array();
        for(var i=0; i<fields.length; i++){
           var element = fields[i];
            element.style.display = 'none';
        }
        for(i=0; i< elements.length; i++){
            elements[i].style.display = 'none';
        }
        
        if(citizens[0].checked == true && company[0].checked == true){
            form_rows= ['region','name','address','city','prov_cap','lbl_province','province','lbl_cap','cap','phone','fax','piv','fiscal_code'];
            el.style.width= '33px';
            Element.show('name_lbl');
            Element.hide('name_lbl_mandatory');
        }
        if(citizens[0].checked == true && company[1].checked == true){
            form_rows= ['name','address','city','prov_cap','lbl_province', 'province','lbl_cap','cap','phone','fax','fiscal_code'];
            el.style.width= '33px';
            Element.hide('name_lbl');
            Element.show('name_lbl_mandatory');
        }
        if((citizens[1].checked == true || citizens[2].checked == true ) && company[1].checked == true){
            form_rows= ['name','address','city','prov_cap','lbl_cap','cap','state','phone','fax'];
            el.style.width= '143px';
            Element.hide('name_lbl');
            Element.show('name_lbl_mandatory');
        }    
        if((citizens[1].checked == true || citizens[2].checked == true ) && company[0].checked == true){
            form_rows= ['region','name','address','city','prov_cap','lbl_cap','cap','state','phone','fax','tax_code'];
            el.style.width= '143px';
            Element.show('name_lbl');
            Element.hide('name_lbl_mandatory');
        }
        for(i=0; i< form_rows.length; i++){
            Element.show(form_rows[i]);
        }
    }