$(document).ready(function(){
   $("#poro").click(function(event){
     	var code = $(this).attr('id');
     	var toggle='N'              ;
        var trolo ='';         
        var mosca ='';
        var soip=  $('#soip').attr("value")
        var baseid=$(this).attr('id');
        $("input").each(function (i) {
                var kpa = $(this).attr("value");
                var id1 =   $(this).attr("name");
                var kpc = $(this).attr("kpc");

                if ( kpa == '' && kpc != 'pi' && id1 !='vari1' &&  id1 !='vari' &&  id1 !='vari2'  && id1 !='company' &&  id1 !='address2' 
&&  id1 !='type=country_select' &&  id1 !='type=state_select' ) 
                    {trolo = trolo + '' + id1 +' ' };
                if ( kpa == '' && kpc == 'pi' && soip == '1' &&  id1 !='b_company' &&  id1 !='b_address2' &&  id1 !='country_select' &&  id1 !='state_select')  
		   {trolo = trolo + '  ' + id1 +' ' };

                                  
        });
	if ( trolo != '' ) {  alert ('Errors -check ' +trolo );
                              event.preventDefault();
                          }
       
   });
   $("#teta").click(function(event){
        var code = $(this).attr('id');
        var toggle=''              ;
        var mosca =0;
        var mosca1 =0;
        var baseid=$(this).attr('id');
        $("input").each(function (i) {
                var kpa = $(this).attr("value");
                var kpb = $(this).attr("kpb");
                var id1 =   $(this).attr("name");
                if ( kpa == ''   ) kpa =0;
                if ( isNaN(kpa)  ) { kpa =0;
                    toggle ='';
                } 
                if (  id1 == 'vari' ||  id1 == 'vari2' ||  id1 == 'vari1'   )
                {mosca = mosca + parseInt(kpa) };
                if (  kpb =='pi'  )
                {mosca1 = mosca1 + parseInt(kpa) };

        });
        $('#pija').attr("value",mosca + mosca1);
        $('#teto').attr("value", mosca1 );
         alert ( 'Recalculated '+toggle); 
        event.preventDefault();
         //}

   });
//

$('input').each(function(index) {
    //alert(index + ': ' + $(this).attr("value") );
     if  ($(this).attr("value") == 'Cancel')  {
        $(this).remove();


   }
   if  ($(this).attr("name") == 'mv_click')  {
        $(this).hide(678);


   }

    if  ($(this).attr("type") == 'file')  {
        $(this).attr("id","file");


   }
});
// check if file exists when user selects a new file
        $('#file').change(function () {
            var filePath = $('#file').val();
            alert($('#file').val());
            $.ajax({
                 url: "test.html?me="+filePath,
                 success :function( data ) {
                        var ipo = data.indexOf("ZZZC");
                        console.log( 'data:', ipo );

                        if (ipo > 1) {
                                var overwrite = confirm('Warning :: File  has already been uploaded.'
                                               + 'The existing data will be overwritten on submission. Continue?');
                                if (!overwrite) {
                                            $('#file').replaceWith($('#file').clone(true));
                                }
                                else {
                                                        $('input').each(function(index) {
                                                         //alert(index + ': ' + $(this).attr("value") );
                                                                if  ($(this).attr("name") == 'mv_click')  {
                                                                        $(this).show(678);
                                                        }

                                                        });

                                }
                        }
                        else { alert('File OK in Host U can send OK');
                                        $('input').each(function(index) {
                                                         //alert(index + ': ' + $(this).attr("value") );
                                                        if  ($(this).attr("name") == 'mv_click')  {
                                                                $(this).show(678);


                                                        }

                                        });
                        }
                 }
            })

        })




//
 });


