/*
 * tn library

 *
 * Copyright (c) 2009 Tantus Netowks (www.tantusnetworks.com)
 *
 * http://www.tantusnetworks.com
 *
 */
	
	function tnShowLoader() {
		$('#tn_ajax_loader').show();
		$('#tn_ajax_loader_shadow').show();
		return true;
	}
	
	/**
	 * Function passign the order and sort parameter to the POST data
	 */
	function tnOrder(sOrderBy, sSortBy, iFormId) {
		iParamId = iFormId.substr(0,iFormId.length-1);
		$('#'+iFormId+' input[name=\''+iParamId+'[sOrder]\']').val(sOrderBy);
		$('#'+iFormId+' input[name=\''+iParamId+'[sSort]\']').val(sSortBy);
		$('#'+iFormId).append('<input type="hidden" name="DomainViewParam[doNotLog]" value="1" />');
		$('#'+iFormId).submit();
	}
	
	/**
	 * Function passing the offset parameter to the POST data 
	 */
	function tnPaging(iOffset, iPagingLimit, iFormId) {
		iParamId = iFormId.substr(0,iFormId.length-1);
		iPaging = (iOffset-1)*iPagingLimit;
		$('#'+iFormId+' input[name=\''+iParamId+'[iOffset]\']').val(iPaging);
		$('#'+iFormId).append('<input type="hidden" name="DomainViewParam[doNotLog]" value="1" />');
		$('#'+iFormId).submit();
	}
	
	/**
	* Function used to do searches
	*/
	function tnTableSearch(sType,sTableParamId,iFieldId,iFormId) {
		if(sType == 'clear') {
			$('#'+iFormId+' input:not(:hidden):not(:submit)').remove();
			$('#'+iFormId+' select').remove();
		}
		else {
			if(sType == 'text') { $('#'+iFieldId).val(''); }
			else if(sType == 'checkbox') { $('.checkbox_'+iFieldId).remove(); }
			else if(sType == 'radio') { $('.radio_'+iFieldId).remove(); }
			else if(sType == 'select') { $('#'+iFieldId).remove(); }
			else if(sType == 'date') { $('#'+iFieldId).remove(); }
			setTimeout("$('."+sTableParamId+"_options').hide();",100);
		}
		$('#'+iFormId).submit();
	}
	
	/** 
	* Function generating messages
	*/
	function tnMessageDisplay() {
		if(asMsg.length > 0) {
			$('#info').html(' ');
			$('#info').show();
		}
		location.hash = "#menu_top";
		for(var iMsgId in asMsg) {
			if(asMsg[iMsgId].iStatus == 1 && asMsg[iMsgId].sInfoPrio != -1) {
				$('#info').prepend('<p id="infoBox'+iMsgId+'"  class="info_box_'+asMsg[iMsgId].sInfoPrio+'"><span onClick="tnMessageHide('+iMsgId+');">'+asMsg[iMsgId].sInfo+'</span></p>');
				$('#infoBox'+iMsgId).show();
			}
		}
	}
	
	// function resposinbile for manipulation of checkboxes
	function tnCheckOptions(sField, iType, sPosition) {
		if(iType ==  3) {
			if(sPosition) {
				sFields = '.'+sField+':not(:'+sPosition+')';
			}
			else {
				sFields = '.'+sField;
			}
			$(sFields).each(function() {
				if($(this).attr("checked")) { $(this).removeAttr("checked"); }
				else { $(this).attr("checked", "checked"); }
			});
			if(sPosition) { tnCheckOptions(sField, 1, sPosition); }
		}
		if(iType ==  2) {
			$('.'+sField+':not(:'+sPosition+')').each(function() {
				if($('.'+sField+':'+sPosition).attr("checked")) { $(this).attr("checked", "checked"); }
				else { $(this).removeAttr("checked"); }
			});
		}
		else if(iType == 1) {
			checked = 0;
			total = 0;
			notchecked = 0;
			$('.'+sField+':not(:'+sPosition+')').each(function() {
				if(this.checked) { checked++; }
				else { notchecked++; }
				total++;
			});
			if(checked == total && total !== 0) { $('.'+sField+':'+sPosition).attr("checked","checked"); }
			else { $('.'+sField+':'+sPosition).removeAttr("checked"); }
			return false;
		}
	}
	
	/**
	* Function generating linked calendars (data pickers)
	*/
	function tnAddLinkedCalendars(calendarName, secondCalendarName, minDate) {
		// Update three select controls to match a date picker selection 
		function calendarUpdateLinked(date) { 
			if(this.id == "linkedDates"+secondCalendarName) {
				$("#calendarMonth"+secondCalendarName).val(date.substring(5, 7)); 
				$("#calendarDay"+secondCalendarName).val(date.substring(8, 10)); 
				$("#calendarYear"+secondCalendarName).val(date.substring(0, 4)); 
			}
			else {
				$("#calendarMonth"+calendarName).val(date.substring(5, 7)); 
				$("#calendarDay"+calendarName).val(date.substring(8, 10)); 
				$("#calendarYear"+calendarName).val(date.substring(0, 4)); 
			}
		}
		
		// Prepare to show a date picker linked to three select controls 
		function calendarReadLinked(date) { 
			$("#linkedDates"+secondCalendarName).val($("#calendarYear"+secondCalendarName).val() + "-" + $("#calendarMonth"+secondCalendarName).val() + "-" + $("#calendarDay"+secondCalendarName).val()); 
			$("#linkedDates"+calendarName).val($("#calendarYear"+calendarName).val() + "-" + $("#calendarMonth"+calendarName).val() + "-" + $("#calendarDay"+calendarName).val()); 
			return {minDate: (date.id == "linkedDates"+secondCalendarName ? $("#linkedDates"+calendarName).datepicker("getDate") : null), 
					maxDate: (date.id == "linkedDates"+calendarName ? $("#linkedDates"+secondCalendarName).datepicker("getDate") : null)};
		}
		
		// Prevent selection of invalid dates through the select controls 
		function calendarCheckLinkedDays(checkCalendarName, checkSecondCalendarName, sType) { 
			var d = new Date();
			$("#calendarDay"+checkCalendarName+" option").attr("disabled", ""); 
			$("#calendarMonth"+checkCalendarName+" option").attr("disabled", ""); 
			$("#calendarYear"+checkCalendarName+" option").attr("disabled", ""); 
			$("#calendarDay"+checkSecondCalendarName+" option").attr("disabled", ""); 
			$("#calendarMonth"+checkSecondCalendarName+" option").attr("disabled", ""); 
			$("#calendarYear"+checkSecondCalendarName+" option").attr("disabled", ""); 
			if(sType == "start") {
				var daysInMonth = 32 - new Date($("#calendarYear".checkCalendarName).val(), 
					$("#calendarMonth"+checkCalendarName).val() - 1, 32).getDate(); 
				$("#calendarDay"+checkCalendarName+" option").attr("disabled", ""); 
				$("#calendarDay"+checkCalendarName+" option:gt(" + (daysInMonth - 1) +")").attr("disabled", "disabled"); 
				if ($("#calendarDay"+checkCalendarName).val() > daysInMonth) {
					$("#calendarDay"+checkCalendarName).val(daysInMonth); 
				}
				if($("#calendarYear"+checkSecondCalendarName).val() < $("#calendarYear"+checkCalendarName).val()) {
					$("#calendarYear"+checkCalendarName).val($("#calendarYear"+checkSecondCalendarName).val());
				}
				if(
					$("#calendarMonth"+checkSecondCalendarName).val() < $("#calendarMonth"+checkCalendarName).val() &&
					$("#calendarYear"+checkSecondCalendarName).val() == $("#calendarYear"+checkCalendarName).val()
				) {
					$("#calendarMonth"+checkCalendarName).val($("#calendarMonth"+checkSecondCalendarName).val());
				}
				if(
					$("#calendarDay"+checkSecondCalendarName).val() < $("#calendarDay"+checkCalendarName).val() && 
					$("#calendarMonth"+checkSecondCalendarName).val() == $("#calendarMonth"+checkCalendarName).val() &&
					$("#calendarYear"+checkSecondCalendarName).val() == $("#calendarYear"+checkCalendarName).val()
				) {
					$("#calendarDay"+checkCalendarName).val($("#calendarDay"+checkSecondCalendarName).val());
				}
			}
			else if(sType == "end") {
				var daysInMonthSnd = 32 - new Date($("#calendarYear".checkCalendarName).val(), 
					$("#calendarMonth"+checkCalendarName).val() - 1, 32).getDate(); 
				$("#calendarDay"+checkCalendarName+" option").attr("disabled", ""); 
				$("#calendarDay"+checkCalendarName+" option:gt(" + (daysInMonthSnd - 1) +")").attr("disabled", "disabled"); 
				if ($("#calendarDay"+checkCalendarName).val() > daysInMonthSnd) {
					$("#calendarDay"+checkCalendarName).val(daysInMonthSnd); 
				}
				if($("#calendarYear"+checkCalendarName).val() > $("#calendarYear"+checkSecondCalendarName).val()) {
					$("#calendarYear"+checkSecondCalendarName).val($("#calendarYear"+checkCalendarName).val());
				}
				if(
					$("#calendarMonth"+checkCalendarName).val() > $("#calendarMonth"+checkSecondCalendarName).val() &&
					$("#calendarYear"+checkCalendarName).val() == $("#calendarYear"+checkSecondCalendarName).val()
				) {
					$("#calendarMonth"+checkSecondCalendarName).val($("#calendarMonth"+checkCalendarName).val());
				}
				if(
					$("#calendarDay"+checkCalendarName).val() > $("#calendarDay"+checkSecondCalendarName).val() && 
					$("#calendarMonth"+checkCalendarName).val() == $("#calendarMonth"+checkSecondCalendarName).val() &&
					$("#calendarYear"+checkCalendarName).val() == $("#calendarYear"+checkSecondCalendarName).val()
				) {
					$("#calendarDay"+checkSecondCalendarName).val($("#calendarDay"+checkCalendarName).val());
				}
			}
			if(
				$("#calendarMonth"+checkSecondCalendarName).val() == $("#calendarMonth"+checkCalendarName).val() &&
				$("#calendarYear"+checkSecondCalendarName).val() == $("#calendarYear"+checkCalendarName).val()
			) {
				$("#calendarDay"+checkSecondCalendarName+" option:lt(" + ($("#calendarDay"+checkCalendarName).val() - 1) +")").attr("disabled", "disabled"); 
			}
			if($("#calendarYear"+checkSecondCalendarName).val() == $("#calendarYear"+checkCalendarName).val()) {
				$("#calendarMonth"+checkSecondCalendarName+" option:lt(" + ($("#calendarMonth"+checkCalendarName).val() - 1) +")").attr("disabled", "disabled"); 
			}
			$("#calendarYear"+checkSecondCalendarName+" option:lt(" + ($("#calendarYear"+checkCalendarName).val() - d.getFullYear()) +")").attr("disabled", "disabled"); 
			if(
				$("#calendarMonth"+checkCalendarName).val() == $("#calendarMonth"+checkSecondCalendarName).val() &&
				$("#calendarYear"+checkCalendarName).val() == $("#calendarYear"+checkSecondCalendarName).val()
			) {
				$("#calendarDay"+checkCalendarName+" option:gt(" + ($("#calendarDay"+checkSecondCalendarName).val() - 1) +")").attr("disabled", "disabled"); 
			}
			if($("#calendarYear"+checkCalendarName).val() == $("#calendarYear"+checkSecondCalendarName).val()) {
				$("#calendarMonth"+checkCalendarName+" option:gt(" + ($("#calendarMonth"+checkSecondCalendarName).val() - 1) +")").attr("disabled", "disabled"); 
			}
			$("#calendarYear"+checkCalendarName+" option:gt(" + ($("#calendarYear"+checkSecondCalendarName).val() - d.getFullYear()) +")").attr("disabled", "disabled"); 
		}
		
		if(minDate !== "") {
			$("#linkedDates"+calendarName+",#linkedDates"+secondCalendarName).datepicker({ 
				beforeShow: calendarReadLinked, 
				onSelect: calendarUpdateLinked, 
				minDate: minDate,
				showOn: "both", 
				buttonImage: "templates/site/images/calendar.gif", 
				buttonImageOnly: true 
			}); 
		}
		else {
			$("#linkedDates"+calendarName+",#linkedDates"+secondCalendarName).datepicker({ 
				beforeShow: calendarReadLinked, 
				onSelect: calendarUpdateLinked, 
				showOn: "both", 
				buttonImage: "templates/site/images/calendar.gif", 
				buttonImageOnly: true 
			}); 
		}
		calendarCheckLinkedDays(calendarName,secondCalendarName,'start');
		$('#calendarDay'+calendarName+', #calendarMonth'+calendarName+', #calendarYear'+calendarName).change(function() { calendarCheckLinkedDays(calendarName,secondCalendarName,'start'); });
		$('#calendarDay'+secondCalendarName+', #calendarMonth'+secondCalendarName+', #calendarYear'+secondCalendarName).change(function() {calendarCheckLinkedDays(calendarName,secondCalendarName,'end'); });
	}
	
	
	/**
	* Function used to display calendar menu
	*/
	function tnSetViewMonth(iMonth, sFormId, sAdt) {
		var daysInMonth = 32 - new Date(parseInt($('#sShowTimeYear').val(),10),iMonth - 1, 32).getDate(); 
		if(sAdt !== "") {
			$("#calendarDay"+sAdt+"Start").val('01'); 
			$("#calendarDay"+sAdt+"End").val(daysInMonth); 
			$("#calendarMonth"+sAdt+"Start").val(iMonth); 
			$("#calendarMonth"+sAdt+"End").val(iMonth); 
			$("#calendarYear"+sAdt+"Start").val(parseInt($('#sShowTimeYear').val(),10)); 
			$("#calendarYear"+sAdt+"End").val(parseInt($('#sShowTimeYear').val(),10)); 
		}
		$('#'+sFormId+' input[name=\'month\']').val(iMonth);
		$('#'+sFormId).submit();
	}
	
	/**
	* Function used to do searches
	*/
	function tnSearch(sFormName, sClear) {
		$('.rewrite').each(function(){   
			if(sClear !== "") { $(this).val(''); }
			$('#' + this.id.replace(/Front/g,'')).val($(this).val());
		}); 
		$('#'+sFormName).trigger('submit');
	}
	$(document).ready(function() { $('.rewrite').each(function(){ $(this).keypress(function (e) { if(e.which == 13) { $('#' + this.id + ' ~ img:first').trigger('click'); } }); }); });
	
	
	/**
	* Function used to .... kto to tu wkleil i nie napisal opisu ???????? !!!!!!!!!!!!!
	*/
	function popup(mylink, windowname)	{
		if (! window.focus) { return true; }
		var href;
		if (typeof(mylink) == 'string') {
		   href=mylink;
		}
		else {
			href=mylink.href;
		}
		window.open(href, windowname, 'width=600,height=450,scrollbars=yes,top=200,left=250');
		return false;
	}
	
	/**
	* Function for predictive search (the one on the top)
	*/
	function updateList() {
		$.ajax({
			url: "ajaxindex.php?mod=domain&sec=_ajax_domain_list",
			type: "POST",
			data: "sQuery="+$("#sSearchTop").val(),
			success: function(response){
				lines = response.split("\n");
				var table_content = '';
				if (lines.length > 1) {
					$('#domainList').fadeIn();
				} else {
					$('#domainList').fadeOut();
				}
				for (i=0;i<lines.length;i++) {
					if (typeof(lines[i]) != "undefined" && lines[i] != '') {
						line = lines[i].split(",");
						if(typeof(line[1]) != "undefined") {
							table_content += '<a class="listFont" href="'+domainpath+line[0]+'">'+line[1]+'</a>';
						}
					}
				}
				table_content += '';
				$('#domainListInner').html(table_content);
			}
		});
	}
	$(document).ready(function(){
		$('#domainList').click(function(e) { e.stopPropagation(); });
		$(document).click(function() { $('#domainList').fadeOut(); }); 
		$("#sSearchTop").keyup(function() { updateList(); }); 
	}); 
	
	/**
	* Function for predictive search (in the big search)
	*/
	function updateListBig() {
		$.ajax({
			url: "ajaxindex.php?mod=domain&sec=_ajax_domain_list",
			type: "POST",
			data: "sQuery="+$("#advanced_search_name_input_box").val(),
			success: function(response){
				lines = response.split("\n");
				var table_content = '';
				if (lines.length > 1) {
					$('#domainListBig').fadeIn();
				} else {
					$('#domainListBig').fadeOut();
				}
				for (i=0;i<lines.length;i++) {
					if (typeof(lines[i]) != "undefined" && lines[i] != '') {
						line = lines[i].split(",");
						if(typeof(line[1]) != "undefined") {
							table_content += '<a class="listFont" href="'+domainpath+line[0]+'">'+line[1]+'</a>';
						}
					}
				}
				table_content += '';
				$('#domainListBigInner').html(table_content);
			}
		});
	}
	$(document).ready(function(){
		$('#domainListBig').click(function(e) { e.stopPropagation(); });
		$(document).click(function() { 
			$('#domainListBig').fadeOut(); 
			$("#advanced_search_name_input_box").keyup(function() { updateListBig(); }); 
		}); 
		$(document).keypress(function (e) {
			if(e.which == 0) {
				$('#advanced_search:visible').fadeOut();
			}
		});
		$('#advanced_search_name_input_box').keypress(function (e) {
			if(e.which == 13) {
				$('.advanced_search_submit').trigger('click');
			}
		});
		$('#sUserLoginBox, #sUserPasswordBox').keypress(function (e) {
			if(e.which == 13) {
				$('#box_login_submit').trigger("click");
				return false;
			}
		});
		$("#box_login_submit").click(function(){
			$.ajax({
				beforeSend: function() { $("#box_login").html("<div style=\"margin-left:70px;margin-top:40px;margin-bottom:36px;\"><img src=\"logic_frontend/templates/site/images/ajaxloader.gif\" alt=\"Loading\" /></div>"); },
				type: "POST",
				url: "ajaxindex.php?mod=static&sec=login_box",
				data: "sUserLogin="+$('#sUserLoginBox').val()+"&sUserPass="+$('#sUserPasswordBox').val()+"&action=log_in",
				success: function(data){
					specialReload();
					$("#box_login").html(data);
				}
			});
			return false;
		});
		$("#box_login_password_reminder").click(function(){
			$.ajax({
				beforeSend: function() { $("#box_login").html("<div style=\"margin-left:70px;margin-top:40px;margin-bottom:36px;\"><img src=\"logic_frontend/templates/site/images/ajaxloader.gif\" alt=\"Loading\" /></div>"); },
				type: "POST",
				url: "ajaxindex.php?mod=static&sec=password_reminder_box",
				success: function(data){
					$("#box_login").html(data);
				}
			});
		});
	});