function askForPrice( title, pid ) {
	document.location.replace( '/zapytanie/produkt/' + pid );
}

function try_contact() {
	var h		= get( 'hashcode' );
	var e 	= getValue( 'email' );
	var b 	= getValue( 'body' );
	var c		= getValue( 'company' );
	var n		= getValue( 'username' );
	var p 	= getValue( 'phone' );
	if( ( e == '' ) || ( b == '' ) || ( c == '' ) || ( n == '' ) || ( p == '' ) ) {
		alert( 'Proszę wypełnić wszystkie pola formularza' );
	} else {
		h.value = 'foto';
		var ajax	= new myAjax();
		hide( 'div_contact' );
		ajax.action = 'try_contact';
		ajax.post( 'username=' + n + '&company=' + c + '&email=' + e + '&body=' + b + '&phone=' + p + '&hashcode=' + h.value, 'contact_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				show( 'contact_message_send' );
			} else {
				show( 'div_contact' );
			}
		}
	}
}

function getSelected(opt) {
	var selected = new Array();
	var index = 0;
	for (var intLoop=0; intLoop < opt.length; intLoop++) {
		if ( opt[ intLoop ].selected ) {
			index = selected.length;
			selected[ index ] = new Object;
			selected[ index ].value = opt[ intLoop ].value;
			selected[ index ].index = intLoop;
	 }
  }
  return selected[ index ];
}

function get( id ) {
	return document.getElementById( id );
}

function getValue( str ) {
	var st = get( str );
	if( st != null ) {
		return trim( st.value );
	} else {
		return ' ';
	}
}

function getRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function isValidEmail(str) {
	var dotin = str.lastIndexOf( "." );
	var atin = str.lastIndexOf( "@" );	
   return ( dotin > 2) && ( atin > 0) && ( dotin > atin ); 
}

function swapHashCode() {
	var h		= get( 'hashcode' );
	h.value = 'foto';
}

function hide( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		h.style.display = "none";	
	} else {
		alert( 'błąd (hide)' );
	}
}

function show( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		h.style.display = "block";
	} 
}

function changeVisible( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );	
	if( h != undefined ) {
		if( h.style.display == 'none' ) {
			h.style.display = 'block';
		} else {
			h.style.display = 'none';
		}
	} else {
		alert( 'błąd (changeVisible)' );
	}
}

function trimLeft( s ) {
	while( s.substring( 0, 1 ) == " " ){
		s = s.substr( 1 );
	}
	return s;
}

function inArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return true;
			}
	}
	return false;
}

function indexOfArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return i;
			}
	}
	return -1;	
}

function trimRight( s ) {
	while( s.charAt( s.length - 1 ) == " " ) {
		s = s.substr( 0, s.length - 1 );
	}
	return s;
}

function trim( s ) {
	s = trimLeft( s );
	s = trimRight( s );
	return s;
}

function searcher( target ) {
	get( target ).value = trim( getValue( target ) );
	var t = getValue( target );
	t = t.split( ' ' ).join( '+' );
//	var o = getValue( 'qcategory' );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		document.location = '/szukaj/'+t;//+'/kategoria/'+o;
	}
}

function clearLogin( id ) {
	switch( id ) {
		case 'public_login':
			if( trim( getValue( id ) ) == 'adres e-mail' ) {
				get( id ).value = '';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == 'hasło' ) {
				get( id ).value = '';
			}
			break;
	}
}

function fillLogin( id ) {
	switch( id ) {
		case 'public_login':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'adres e-mail';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'hasło';
			}
			break;
	}
}

function publicLogin() {
	var l 	= getValue( 'public_login' );
	var p	= getValue( 'public_password' );
	get( 'loginForm' ).value = '';
	var ajax = new myAjax();
	var pstr = 'login='+l+'&pass='+p+'&cn=Client';
	ajax.action = 'publicLogin';
	ajax.post (pstr, 'loginForm');
	ajax.onLoad = function() {
		if( this.response == 'Zalogowano' ) {
			document.location.reload();
		}
	}
}


function pLogin( act ) {
	var l 	= getValue( 'p_login' );
	var p	= getValue( 'p_p' );
	hide( 'odpowiedz' );
	var ajax = new myAjax();
	var pstr = 'login='+l+'&pass='+p+'&cn=Client';
	ajax.action = 'publicLogin';
	ajax.post (pstr, 'loginForm');
	ajax.onLoad = function() {
		if( this.response == 'Zalogowano' ) {
			if( act == true ) {
				document.location.reload();
			} else {
				document.location.replace( '/' );
			}
		}
	}
}

function logIn() {
	var f 	= document.forms[ 'log_in_form' ];
	var l 	= getValue( 'cms_login' );
	var p	= getValue( 'cms_password' );
	if( l == '' ) {
		alert( 'Brak loginu' );
	} else if( p == '' ) {
		alert( 'Brak hasła' );
	} else {
		f.submit();
	}
}

function showNewsletter() {
	hide( 'newsletter_blank' );
	show( 'newsletter' );
}

function hideNewsletter() {
	hide( 'newsletter' );
	show( 'newsletter_blank' );
}

function newsletterSubscribe() {
	var t = get( 'newsletter_message' );
	var b = get( 'newsletter_blank' );
	var email 	= getValue( 'newsletter_mail' );
	var type 	= getValue( 'newsletter_type' );
	var ajax 	= new myAjax();
	t.innerHTML = '';
	ajax.post( 'action=newsletter_subscribe&newsletter_mail='+email+'&newsletter_type='+type );
	ajax.onLoad = function() {
//		t.innerHTML = this.response;
		switch( trim( this.response ) ) {
			case 'ok':
				hideNewsletter();
				b.innerHTML += '<span style="color:#ffffff; ">Dziękujemy<br/>Twój adres e-mail został zapisany.</span>';	
				break;
			case 'bad_email':
				t.innerHTML += '<span style="color:#ffffff; ">Wpisano błędny adres email</span>';	
				break;
			default:
				break;
		}
	}
}

function newsletterUnsubscribe() {
	var t = get( 'newsletter_message' );
	var b = get( 'newsletter_blank' );	
	var email 	= getValue( 'newsletter_mail' );
	var ajax 	= new myAjax();
	t.innerHTML = '';
	ajax.post( 'action=newsletter_unsubscribe&newsletter_mail='+email );
	ajax.onLoad = function() {
		show( 'newsletter_message' );
//		t.innerHTML = this.response;
		switch( trim( this.response ) ) {
			case 'ok':
				hideNewsletter();
				b.innerHTML += '<span style="color:#ffffff; ">Twój adres e-mail został skasowany.</span>';	
				break;
			case 'bad_email':
				t.innerHTML += '<span style="color:#ffffff; ">Wpisano błędny adres email</span>';	
				break;
			default:
				break;
		}
	}
}

function clearFormValue( id, val ) {
	var t = get( id );
	if( t.value == val ) {
		t.value = '';
	}
}

function fillFormValue( id, val ) {
	var t = get( id );
	if( trim( t.value ) == '' ) {
		t.value = val;
	}
}

function confirmDelete(what)
{
	var te = what.split('{BR}').join("\n");
    var is_confirmed = confirm('Czy jesteś pewien, że chcesz skasować '+te+' ? ');
    return is_confirmed;
} 

function confirmDeleteUrl( what, url ) {
	var te = what.split('{BR}').join("\n");
    var is_confirmed = confirm('Czy jesteś pewien, że chcesz skasować '+te+' ? ');
	if( is_confirmed ) {
		document.location = url;
	}
}