﻿function search_form_init() {
        
        var search_form = $('#search_form');

        search_form.find('.submit > a').click(function(){
		var searched = search_form.find('.input input').val().replace(/^\s+/,'').replace(/\s+$/,'').replace(/\s/g,'+');
		if (searched.length < 3){
			alert('Wyszukiwany tekst musi mieć conajmniej 3 znaki ');
		} else {
			document.location.href = '/szukaj/'+searched;	
		}
		
           
        });
	 search_form.submit(function(){
		var searched = search_form.find('.input input').val().replace(/^\s+/,'').replace(/\s+$/,'').replace(/\s/g,'+');
		if (searched.length < 3){
			alert('Wyszukiwany tekst musi mieć conajmniej 3 znaki ');
		} else {
			document.location.href = '/szukaj/'+searched;	
		}
		return false;
	 });
        search_form.find('.input input').focus(function(){
           this.value = ''; 
        });
        
        search_form.find('.input input').blur(function(){
            if (this.value == '') this.value = 'wpisz szukane hasło'; 
        });
}
     
$(document).ready(function(){
        search_form_init();

});

function trySubmitApprise() {
    $( '#contact_message' ).hide();
    var fname = $( '#user_name' ).val();
    var femail = $( '#user_email' ).val();
    var friend = $( '#friend_email' ).val();
    
    var ajax = new myAjax( 'eczg' );
    ajax.action = 'trySubmitApprise';
    ajax.post( 'name='+fname+'&email='+femail+'&friend='+friend );
    ajax.onLoad = function() {
	if( this.response == 'ok' ) {
	    $( '#contact_message' ).html( 'Wiadomość została wysłana' );
	    $( '#contact_message' ).show();
	    $('#contact_form').hide();
	} else {
	    $( '#contact_message' ).html( this.response );
	    $( '#contact_message' ).show();
	}
    }
}

function trySubmitContact() {
        //alert('test'); return;
    $( '#contact_message' ).hide();
    var fname = $( '#name' ).val();
    var femail = $( '#email' ).val();
    var fp = $( '#phone' ).val();
    var fbody = $( '#question2' ).val();
    //var fq = $( '#question1' ).val();

    if(!$("input[name='question1']").is(":checked")){
        $( '#contact_message' ).html( 'Wybierz zagadnienie' );
        $( '#contact_message' ).css('color','#ff0000');
        $( '#contact_message' ).show();
    } else {
        var fco = $( '#company' ).val();
        var fci = $( '#city' ).val();
        //var fq = $("input[name='question1']").val();//.find(":checked")
        var fq = $("input[name='question1']:checked").val();
        //alert(fq);
        //return false;
        var ajax = new myAjax( 'eczg' );
        ajax.action = 'trySubmitContact';
        ajax.post( 'name='+fname+'&email='+femail+'&phone='+fp+'&body='+fbody+'&question='+fq+'&company='+fco+'&city='+fci );
        ajax.onLoad = function() {
            if( this.response == 'ok' ) {
                $( '#contact_message' ).html( '<span style="color:green;padding:20px 0px;font-weight:bold;font-size:15px;">Wiadomość została wysłana</span>' );
                $( '#contact_message' ).show();
                $( '#contact_form' ).hide();
            } else {
                $( '#contact_message' ).html( '<span style="color:red;padding:20px 0px;font-weight:bold;font-size:15px;">'+this.response+'</span>' );
                $( '#contact_message' ).show();
            }


        }
    }
}
