document.root_folder = '';

function togglePhoto( linker ) {
	get( 'ph_main' ).src = linker;
}

function triggerNotes( id, note ) {
	for( i = 1; i <= 5; i++ ) {
		var img = get( 'note_'+i.toString() );
		if( id >= i ) {
			img.src = getNoteSrc( i, 5 );
		} else {
			img.src = getNoteSrc( i, 0 );
		}
	}
}

function detriggerNotes( id, note ) {
	for( i = 1; i <= 5; i++ ) {
		var img = get( 'note_'+i.toString() );
		img.src = getNoteSrc( i, note );
	}	
}

function getNoteSrc( id, note ) {
	var tmp = '/graph/grade-star-ZERO.gif';
	if( ( id <= note ) && ( note - id != -0.5 ) ) {
		return tmp.replace( 'ZERO', 'full' );
	} else if( note - id == -0.5 ) {
		return tmp.replace( 'ZERO', 'half' );
	} else {
		return tmp.replace( 'ZERO', 'empty' );
	}
}



function sendAvatar() {
		
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}




function saveNewPassword() {
	var op = get( 'old_pass' ).value;
	var np = get( 'new_pass' ).value;
	var npr = get( 'new_pass_repeat' ).value;
	var ajax = new myAjax();
	ajax.action = 'saveNewPassword';
	ajax.post( 'op='+op+'&np='+np+'&npr='+npr );
	ajax.onLoad = function() {
		var d = get( 'passwordMessage' );
		switch( this.response ) {
			case 'ok':
				putHtml( d, 'Zapisano nowe hasło' );
				break;
			case 'not_empty':
				putHtml( d, 'Hasło nie może być puste' );
				break;
			case 'bad_compare':
				putHtml( d, 'Hasło nie zgadza się z powtórzeniem hasła' );
				break;
			case 'bad_pass':
				putHtml( d, 'Stare hasło jest nieprawidłowe' );
				break;
			default:
				putHtml( d, this.response );
				break;
		}
	}
}

function sifr_replace() {
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"div#login_panel div.sifr_l_ht", sFlashSrc:"/swf/sifr_century_gothic.swf", sColor:"#ffffff", sLinkColor:"#ffffff", sHoverColor:"#ffffff", sBgColor:'transparent', sWmode:'transparent'}));
	}
}

function searcherF( target ) {
	var t = getSearch( target );
	if( t.length < 1 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
		return false;
	} else {
		//alert('test');
		document.location.href = '/szukaj/'+t;//+'/kategoria/'+o;
	}
}

function searchCurrent( target, linker ) {
	var t = getSearch( target );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		var h = linker;
		var len = h.length;
		var ls = h.substr( len - 1, 1 );
		if( ls == '/' ) {
			h = h.substr( 0, len - 1 );
		}
		document.location.href = h + '/szukaj/'+t;
	}
}

function getSearch( target ) {
	get( target ).value = trim( getValue( target ) );
	return getValue( target ).split( ' ' ).join( '+' );
}

function publicLogin() {
	var l 	= getValue( 'public_login' );
	var p	= getValue( 'public_password' );
	var g = getRadioValueByName( 'save_pass' );
//	get( 'loginMessage' ).value = '';
	var ajax = new myAjax();
	var pstr = 'login='+l+'&pass='+p+'&save_pass='+g+'&cn=Client';
	ajax.action = 'publicLogin';
//	ajax.post ( pstr, 'loginMessage' );
	ajax.post ( pstr );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			document.location.reload();
		} else {
			alert( this.response );
		}
	}
}

var mouseOn = new Array();
function mouseeIn( id ){
	mouseOn[id] = true;
}

function mouseeOut( id ){
	mouseOn[id] = false;
}


function showMenu(id) {
	//document.getElementById(id).style.display='block';
	var idi = id.replace( '_sub', '' );
	idi = 'nav_'+idi;
	//get( idi ).style.backgroundPosition = "0 0";

	document.getElementById( idi ).className = document.getElementById( idi ).className+" active";
	setTimeout(	function(){
		if( mouseOn[ id ] ) {
			document.getElementById(id).style.display='block';
			document.getElementById( idi ).className = document.getElementById( idi ).className+" active";
		}
	} , 100 );
}

function hideMenu(id) {
	var idi = id.replace( '_sub', '' );
	idi = 'nav_'+idi;
	//get( idi ).style.backgroundPosition = "0 -30px";
	setTimeout(	function(){
		if( !mouseOn[ id ] ) {
			document.getElementById(id).style.display='none';
			var aold =  document.getElementById( idi ).className + '';
			var anew = aold.replace( ' active', '' );
			anew = anew.replace( ' active', '' );
			document.getElementById( idi ).className = anew;
		}
	} , 100 );
}

/*
*/
$(document).ready(function()
{
//	$("#offer_sub").mouseover(
//		function () {
//			$("#nav_offer").css({'background-position': '0px -30px'});
//		}
//	);
//	$("#nav_offer").mouseover(
//		function () {
//			$("#nav_offer").css({'background-position': '0px -30px'});
//		}
//	);
//	$("#nav_offer").mouseout(
//		function () {
//			$("#nav_offer").css({'background-position': '0px 0px'});
//		}
//	);
//	$("#offer_sub").mouseout(
//		function () {
//			$("#nav_offer").css({'background-position': '0px 0px'});
//		}
//	);
	
	$("input[name='contactby']").click(function(){
    if ($("input[name='contactby']:checked").val() == 'phone') {
        $("#phone").removeAttr("disabled");
				$("#phone").css("background-color", '#ffffff'); 
				$("#email").attr("disabled", true);
				$("#email").css("background-color", '#ece9d8'); 
				//alert('phone');
    }
    else if ($("input[name='contactby']:checked").val() == 'email') {
        // Code for handling value 'b'
				$("#email").removeAttr("disabled");
				$("#email").css("background-color", '#ffffff'); 
				$("#phone").attr("disabled", true);
				$("#phone").css("background-color", '#ece9d8'); 
				//alert('email');
    }
});
	
	//$(".left-list").each(function() { $(this.lastChild).addClassName("last"); });
	//$("ul.left-list li:last-child").each(function(x) { x.addClassName("last"); });
	$("ul.left-list li:last-child").addClass("last");



	
//if($.browser.msie) {
	$("#contactForm input[type=text], #contactForm textarea").focus( function() {
		$(this).addClass(" focused");} )
	.blur( function() {
		$(this).removeClass(" focused")} );
//	};

	
	var licount = 1;
	$(".print-images ul li").each(function(){
		if((licount%4)==0) {
			$(this).addClass(" last-elem");
		}
		licount++;
	});

	
}
);


function validateContactForm() {
	var s = get( 'unit' ).value;
	var q = get( 'question' ).value;
	var n = get( 'name' ).value;
	var p = get( 'phone' ).value;
	var e = get( 'email' ).value;
	
	if( trim( s ) == '' || trim( q ) == '' || trim( n ) == '' || (trim( p ) == '' && trim( e ) == '') ) {
		alert('Wszystkie pola są wymagane!');
		return false;
	}
	//alert( s + '-' + q + '-' + n);
	if( e != "" ) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(e) == false) {
			 alert('Podany adres email jest nieprawidłowy!');
			 return false;
		}
	}
	if( p != "" ) {
		if ( isNaN( parseInt( p ) ) ) {
			alert('Podany numer telefonu zawiera nieprawidłowe znaki!');
			return false;
		}
	}
	var form = get( 'contactForm' );
	form.submit();
}

function partnerListCheckboxes( nr ) {
	
	var x = $("#s_"+nr+":checked").length;
	if( x == 1 ) {
		
		$("#sp_"+nr).each(function(){
			$(this).find(':checkbox').attr('checked', 'checked');
		});
	} else {
		$("#sp_"+nr).each(function(){
			$(this).find(':checkbox').attr('checked', '');
		});
	}
	
}

function validateClientsForm() {
	var u = get( 'username' ).value;
	var p = get( 'password' ).value;
	if( trim(u) == '' || trim(p) == '' ) {
		alert('Wszystkie pola są wymagane!');
		return false;
	}
	var form = get( 'clientsForm' );
	form.submit();
}

function redirect(x){
	if(x==0) document.becomePartnerForm.opis.value="w jakim piśmie?"
	else if(x==1) document.becomePartnerForm.opis.value="w jakim portalu?"
	else if(x==2) document.becomePartnerForm.opis.value="w jakiej książce?"
	else if(x==5) document.becomePartnerForm.opis.value="jak?"
	else document.becomePartnerForm.opis.value="-";
}

function validateBecomePartnerForm() {
	var form = get( 'becomePartnerForm' );
	form.submit();
}

function setAlert(x) {
	alert(x);
}

function setState( state ) {
	var formP = get( 'partnerSearchForm' );
	//alert(state);
	if( state == 'undefined' ) {
		state = '';
	}
	formP.region.value = state;
	/*formP.submitbutton.value = "Filtruj wynik";*/
	formP.submit();
}

function validateOrderFileForm() {
	var firma = get( 'firma' ).value;
	var nip = get( 'nip' ).value;
	var imie = get( 'imie' ).value;
	var nazwisko = get( 'nazwisko' ).value;
	var nr = get( 'nr' ).value;
	var kod = get( 'kod' ).value;
	var miasto = get( 'miasto' ).value;
	var telefon = get( 'telefon' ).value;
	var email = get( 'email' ).value;
	if( trim( firma ) == '' ||
		 trim( nip ) == '' ||
		 trim( imie ) == '' ||
		 trim( nazwisko ) == '' || 
		 trim( nr ) == '' ||
		 trim( kod ) == '' ||
		 trim( miasto ) == '' ||
		 trim( telefon ) == '' ||
		 trim( email ) == '' 
		 ) {
		alert('Wszystkie pola oznaczone gwiazdką są wymagane!');
		return false;
	}
	if( email != "" ) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(email) == false) {
			 alert('Podany adres email jest nieprawidłowy!');
			 return false;
		}
	}
	if( telefon != "" ) {
		if ( isNaN( parseInt( telefon ) ) ) {
			alert('Podany numer telefonu zawiera nieprawidłowe znaki!');
			return false;
		}
	}
	var form = get( 'orderDemo' );
	form.submit();
}

function newsletterShow() {
	$("#newsletter-container").css('display','block');
}
function newsletterHide() {
	$("#newsletter-container").css('display','none');
}
function checkNewsletter() {
	var email = get( 'email' ).value;
	//alert(email);
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(reg.test(email) == false) {
		 alert('Podany adres email jest nieprawidłowy!');
		 return false;
	}	else {
		var form = get( 'newsletterForm' );
		form.submit();
		return false;
	}
}


Win=null;

function displayWindow(url,_width,_height) {
	if(window.screen){maxw=screen.availwidth;maxh=screen.availheight;}
	else{maxw=640;maxh=460;}
	
	LeftPosition = (screen.width) ? (screen.width-_width)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-_height)/2 : 0;
	
	cofg="left="+LeftPosition+","
			+"top="+TopPosition+","
			+"width="+_width+","
			+"height="+_height+","
			+"toolbar=no,"
			+"status=no,"
			+"menubar=no,"
			+"scrollbars=yes,"
			+"resizable=no";
	if (!(Win==null || Win.closed))
		Win.close();
	Win = window.open(url,"Nowości",cofg);
	Win.focus();
}

function displayWindowSB(url) {
	cofg="toolbar=no,"
			+"status=no,"
			+"menubar=no,"
			+"scrollbars=yes,"
			+"resizable=yes";
	if (!(Win==null || Win.closed))
		Win.close();
	Win = window.open(url,"Obsługa_klientów",cofg,true);
	Win.focus();
}

function showPromo( id ) {
	var olds = new Array();
	olds[ 0 ] = 'promo1';
	olds[ 1 ] = 'promo2';
	olds[ 2 ] = 'promo3';

	var new_id = 'r_'+id;
	var ids = '';
	if( get( new_id ).style.display == 'block' ) return false;

	for( var i=0;i<3;i++ ){
		ids = 'r_'+olds[ i ];
		get( ids ).style.display = 'none';
	}
	$( '#'+new_id ).show( 300 );
}
