function strstr (haystack, needle, bool) {
    // Finds first occurrence of a string within another  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/strstr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr('Kevin van Zonneveld', 'van');
    // *     returns 1: 'van Zonneveld'    // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
    // *     returns 2: 'Kevin '
    // *     example 3: strstr('name@example.com', '@');
    // *     returns 3: '@example.com'
    // *     example 4: strstr('name@example.com', '@', true);    // *     returns 4: 'name'
    var pos = 0;
    
    haystack += '';
    pos = haystack.indexOf( needle );    if (pos == -1) {
        return false;
    } else{
        if (bool){
            return haystack.substr( 0, pos );        } else{
            return haystack.slice( pos );
        }
    }
}

// prepare the form when the DOM is ready 
$(document).ready(function() { 		
    var options = { 
    	// target element(s) to be updated with server response	
        target:        '#logowanieKontener',
        // pre-submit callback
        beforeSubmit:  showRequest,
        // post-submit callback
        success:       showResponse,
 
        // other available options:
        	
        //url:       url         // override for form's 'action' attribute
        
        type:      'post'        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    };    
    
    var optionsPH = { 
        	// target element(s) to be updated with server response	
            target:        '#logowanieKontenerPH',
            // pre-submit callback
            beforeSubmit:  showRequestPH,
            // post-submit callback
            success:       showResponsePH,
     
            // other available options:
            	
            //url:       url         // override for form's 'action' attribute
            
            type:      'post'        // 'get' or 'post', override for form's 'method' attribute 
            //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
            //clearForm: true        // clear all form fields after successful submit 
            //resetForm: true        // reset the form after successful submit 
     
            // $.ajax options can be used here too, for example: 
            //timeout:   3000 
        };
        
    var optionsNL = { 
        	// target element(s) to be updated with server response	
            target:        '#subskrypcjaKontenerNL',
            // pre-submit callback
            beforeSubmit:  showRequestNL,
            // post-submit callback
            success:       showResponseNL,
     
            // other available options:
            	
            //url:       url         // override for form's 'action' attribute
            
            type:      'post'        // 'get' or 'post', override for form's 'method' attribute 
            //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
            //clearForm: true        // clear all form fields after successful submit 
            //resetForm: true        // reset the form after successful submit 
     
            // $.ajax options can be used here too, for example: 
            //timeout:   3000 
        };
    
    // bind to the form's 
    //$('#logowanieForm').ajaxForm(options); 
    
    // bind to the form's submit event 
    $('#logowanieForm').submit(function() {
    	
        // inside event callbacks 'this' is the DOM element so we first
        
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
                        
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    });        
    
    $('#przypomnienieHaslaForm').submit(function() {
    	
        // inside event callbacks 'this' is the DOM element so we first
        
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(optionsPH); 
                        
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    });
    
    $('#subskrypcjaForm').submit(function() {
    	
        // inside event callbacks 'this' is the DOM element so we first
        
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(optionsNL); 
                        
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
 
}); 
 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString);
    //$('input.zaloguj').val('Trwa logowanie...');
    $('#loader').html('&nbsp;<img src="/szablon/perceptus.pl/mod/layout/img/al.gif" width="16" height="16" />');
    $('#logowanieKontener').html('');
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 

//pre-submit callback 
function showRequestPH(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString);
    //$('input.zaloguj').val('Trwa logowanie...');
    $('#loaderPH').html('&nbsp;<img src="/szablon/perceptus.pl/mod/layout/img/al.gif" width="16" height="16" />');
    $('#logowanieKontenerPH').html('');
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
}

function showRequestNL(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString);
    //$('input.zaloguj').val('Trwa logowanie...');
    $('#subskrypcjaKontenerNL').html('&nbsp;<img src="/szablon/perceptus.pl/mod/layout/img/al.gif" width="16" height="16" />');
    //$('#logowanieKontenerPH').html('');
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
}

// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.');
    
    // sprawdzam czy w odpowiedzi znajduja sie bledy
    var odpowiedz; 
    blad = (strstr(responseText, 'Nieprawidłow') || strstr(responseText, 'zablokowany')?1:0);    
    if ( blad == 0 ) {
    	//window.location.reload();
    	window.location.href = 'https://perceptus.pl/PL/twoje_konto/historia_zamowien/';
    }
    $('#loader').html('');
}

function showResponsePH(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.');
    
    $('#loaderPH').html('');
    $('input[name=email]').attr('value','');
    $('#logowanieKontenerPH').html(responseText);
} 


function showResponseNL(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.');
    
    //$('#loaderPH').html('');
    //$('input[name=email]').attr('value','');
    //$('#logowanieKontenerPH').html(responseText);
} 
