function doBack() {
	
	while (window.location.hash != '') {
		history.go(-1);
	}
	
	history.go(-1);
	
	return false;
}


var curr_menu_lvl_3_id = '';

function showMenuLevel3(item_id) {
	
	hideMenuLevel3(curr_menu_lvl_3_id);
	
	if (item_id == '') return;
	if (!$('menu_lvl_3_' + item_id)) return;
	
	$('#menu_lvl_3_' + item_id).get(0).style.display = 'block';
	
	curr_menu_lvl_3_id = item_id;
}


function hideMenuLevel3(item_id) {
	
	if (item_id == '') return;
	if (!$('#menu_lvl_3_' + item_id).get(0)) return;
	$('#menu_lvl_3_' + item_id).get(0).style.display = 'none';
}


var scrolled_down = false;

function loadServiceData(service_id) {
	
	if (service_id == '') return;
	if (!$('#service_content').get(0)) return;
	
	
	// scroll down to show a loaded content
	// couldn't use window.location, because the flash is allready using the anchor
	var ieBody = (document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
	
	var scrollTop = document.all? ieBody.scrollTop : pageYOffset;
	
	if (!scrolled_down && scrollTop == 0) {
		
		window.scrollBy(0, 330);
		
		scrolled_down = true;
	}
	
	
	var objContent = $('#service_content');
	objContent.get(0).style.display = 'block';
	
	objContent.html( '<div class="preloader"><div><img src="'  + root_url + 'media/images/dec/preloader.gif" alt="" /></div></div>' );
	
	$.ajax( {
		type: 'get',
		url: root_url + 'index.php?act=js/load_service',
		data: { 'service_id': service_id },
		
		success: function(response) {
			if (response) {
				objContent.html( response );
			} else {
				objContent.get(0).style.display = 'none';
				objContent.html( '' );
			}
		},
		
		error: function (response) {
			objContent.get(0).style.display = 'none';
			objContent.html('');
		}
	} );
}


function loadServiceList(objSelect, floor_no, selected_service_id) {
	
	if (!objSelect) return;
	if (floor_no == '') return;
	if (!$('#service_list_content').get(0)) return;
	if (!objSelect.options[objSelect.selectedIndex].value) return;
	selected_service_id = selected_service_id || null;

	var objContent = $('#service_list_content');
	objContent.get(0).style.display = 'block';
	
	objContent.html( '<div class="preloader"><img src="'  + root_url + 'media/images/dec/preloader.gif" alt="" /></div>' );
	
	$.ajax( {
		type:'get',
		url: root_url + 'index.php?act=js/load_service_list',
		data: { 'service_list_id': objSelect.options[objSelect.selectedIndex].value, 'floor_no': floor_no, 'selected_service_id': selected_service_id },
		success: function(response) {
			if (response) {
				objContent.html( response );
			} else {
				objContent.get(0).style.display = 'none';
				objContent.html('');
			}
		},
		
		error: function (response) {
			objContent.style.display = 'none';
			objContent.html('');
		}
	} );
}


function loadCaptcha(captcha_holder_id) {
	
	if (!$('#'+captcha_holder_id).get(0)) return;
	
	$.ajax( {
		type: 'get',
		url: root_url + 'index.php?act=js/captcha',
		data: { 'get_new': 1 },		
		success: function(response) {
			if (response) {
				$('#'+captcha_holder_id).html(response);
			}
		}
	} );
}


function getFlashObject(objectId) {
	
	if (objectId == '') return;
	
	if (navigator.appName.indexOf('Microsoft') != -1) {
		return window[objectId];
	}
	
	return document[objectId];
}


active_service_in_list = -1;

function showServiceOnPlan(coords, number) {
	
	// this function will be used to invoke some Flash actions,
	// which produce current service marking on the plan.
	
	if (active_service_in_list > -1 && $('#serv_' + active_service_in_list).get(0)) {
		$('#serv_' + active_service_in_list).get(0).className='';
	}
	
	$('#serv_' + number).get(0).className = 'active';
	
	active_service_in_list = number;
	
	
	if (coords == '') return;
	
	try {
		
		// calling flash function...
		getFlashObject('plan_floor_flash').showService(coords);
		
	} catch(e) {}
}


function highlightEnterprise(enterpriseId) {
	
	try {
		
		var tds = $('#enterprise_menu td');
		
	} catch(e) {
		
		return false;
	}
	
	if (typeof tds != 'object') {
		
		return false;
	}
	
	tds.each(function(item, index) {
		
		item.className = item.className.replace(/_ov$/, '');
		
		if (item.id == 'e_menu_' + enterpriseId) {
			
			item.className = item.className + '_ov';
		}
	});
}


function highlightEnterprises(enterpriseTdIds) {
	
	try {
		
		var tds = $('#enterprise_menu td');
		
	} catch(e) {
		
		return false;
	}
	
	if (typeof tds != 'object') {
		
		return false;
	}
	
	tds.each(function(item, index) {
		
		item.className = item.className.replace(/_ov$/, '');
		
		if (inArray(item.id)) {
			
			item.className = item.className + '_ov';
		}
	});
}


inArray = function(value) {
	
	var i;
	
	for (i = 0; i < this.length; i++) {
		
		if (this[i] === value) return true;
	}
	
	return false;
}

function changeLoginEnterprise(enterpriseId) {
	
	$('#login_enterprise_id').get(0).value = enterpriseId;
	$('#change_enterprise').get(0).value = '1';
	$('#login_form').submit();
}

var last_act = 'none';

function filterEventsByEnterprise ( enterprise_id, date, selected_day ) {
	setMenuItem(enterprise_id);
	var objCalendar = $('#calendar_cnt').get(0);
	
	if (!date) date = '';
	if (!selected_day) selected_day = '';
	
	objCalendar.innerHTML = '';
	$( '#calendar' ).get(0).innerHTML = '<div class="preloader"><div><img src="'  + root_url + 'media/images/dec/preloader.gif" alt="" /></div></div>';
	
	var data = { enterprise_id : enterprise_id };
	
	if ( $( '#events_filter').get( 0 ) && selected_day == '' ) {
		
		date = selected_day = $( '#date_from' ).get( 0 ).value.replace( /\-/g, '' );		
	}
	
	if ( $( '#events_filter').get( 0 ) ) {
		
		var cats = [];
		
		$( '#events_categories input:checked' ).each( function() {
			
			cats.push( this.name.replace( /\D/g, '' ) );
		} );
		
		data = { 
			enterprise_id : data.enterprise_id,
			cats: cats
		};
	}
		
	$.ajax( {
		type: 'get',
		url: root_url + 'index.php?act=js/load_calendar&date='+date+'&sel_date='+selected_day,
		data: data,
		dataType: 'script',
		success: function(response) {
			objCalendar.innerHTML = '';
			$( '#calendar' ).get(0).style.height = 'auto';
			$( '#calendar_cnt' ).get(0).style.height = '0';
			$( '#calendar_cnt' ).get(0).style.lineHeight = '0';
			$( '#load_calendar' ).get(0).style.height = 'auto';
			$( '.thin_line' ).show();
		},
		error: function (response) {
			objCalendar.style.display = 'none';
			objCalendar.innerHTML = '';
		}
	});
}


function setMenuItem(ent_id) {
	if ($('#cal_menu').get(0)) {
		if (ent_id != last_act) {
			$('#cal_menu_item_act_'+ent_id).get(0).style.display = 'block';
			$('#cal_menu_item_'+ent_id).get(0).style.display = 'none';
			if ($('#cal_menu_item_'+last_act).get(0)) {
				$('#cal_menu_item_'+last_act).get(0).style.display = 'block';
				$('#cal_menu_item_act_'+last_act).get(0).style.display = 'none';
			}
			last_act = ent_id;
		}
	}
}

var currEnterpriseId = '';
var currSubdivisionId = '';

function changeSubdivision(enterpriseId, subdivisionId) {
	
	if (currEnterpriseId != '') {
		
		showHideObject('subdivision_name_' + currEnterpriseId);
		
		if (currSubdivisionId != '') {
			showHideObject('subdivision_head_name_' + currSubdivisionId);
			showHideObject('subdivision_head_phone_' + currSubdivisionId);
			showHideObject('subdivision_head_email_' + currSubdivisionId);
		}
		
		if ($('#subdivision_select_' + enterpriseId).get(0).selectedIndex != 0 && currEnterpriseId != enterpriseId) {
			$('#subdivision_select_' + enterpriseId).get(0).selectedIndex = 0;
		}
	}
	
	showHideObject('subdivision_name_' + enterpriseId);
	showHideObject('subdivision_head_name_' + subdivisionId);
	showHideObject('subdivision_head_phone_' + subdivisionId);
	showHideObject('subdivision_head_email_' + subdivisionId);
	
	
	currEnterpriseId = enterpriseId;
	currSubdivisionId = subdivisionId;
}

function newsletterSubscriptionPopupRequest( container_id, first_form_load_data ) {
	var container_element	= $( container_id );
	var form_element 		= container_element.find( 'form' );
	var content_element 	= container_element.find( '.content' );
	var request_params 		= form_element.get(0) ? form_element : first_form_load_data;

	$.ajax( {
		url: 'index.php?act=newsletter/subscription',
		type: form_element.get(0) && !first_form_load_data ? 'post' : 'get',
		data : request_params,
		
		success: function( responseText ) {
			content_element.hmtl( responseText );
			content_element.css( 'visibility', 'visible' );
			
			var new_form_element = content_element.find( 'form' );
			
			if ( new_form_element.get(0) ) {
				new_form_element.submit( function() {
					
					newsletterSubscriptionPopupRequest( container_id );
				} );
			}
		}
	} );
	
	content_element.css( 'visibility', 'hidden' );
}

function newOfferRequest( container_id, request_params ) {
	var container_element	= $( container_id );
	
	var request = new Request( {
		url: root_url + 'index.php?act=js/competition/offer',
		method: 'post',
		link: 'ignore',
		data : request_params,
		
		onSuccess: function( responseText ) {
			container_element.set( 'html', responseText );
			container_element.setStyle( 'visibility', 'visible' );
			
			var new_form_element = container_element.getElement( 'form' );
			
			if ( new_form_element ) {
				new_form_element.addEvent( 'submit', function( event ) {
					event.stop();
					newOfferRequest( container_id, new_form_element );
				} );
			}
		}
	} );
	
	request.send();
	container_element.setStyle( 'visibility', 'hidden' );
}

function tradeDoublerProductReport( custom_options ) {
	//if ( PROJECT_MODE != 'PRODUCTION' ) return false;

	var options = {
		'organization_id' 		: 1158609,
		'event_id' 				: 170422,
		'product_id'			: null,
		'product_name' 			: null,
		'product_price' 		: 1,
		'number_of_products' 	: 1
	}
	
	$.extend( options, custom_options );
	
	if ( !( options.product_id && options.product_name ) ) { 
		return false;
	}
		
	var protocol = window.location.protocol;
	
	if ( protocol.indexOf( ':' ) ) {
		protocol = protocol.substring( 0, protocol.indexOf( ':' ) );
	}
	
	var report_info = "f1=" + options.product_id + "&f2=" + options.product_name + "&f3=" + options.product_price + "&f4=" + options.number_of_products;

	var image = new Image(1,1);
	image.src = protocol + '://tbl.tradedoubler.com/report?organization=' + options.organization_id + '&event=' + options.event_id + '&leadNumber=auto&reportInfo=' + escape( report_info );

    return true;
}

function trackSessionCouponPrint() {
	if ( getCookie('coupon_print') == '') {
		pageTracker._trackPageview('/kuponas2009_dcpc_u_track');
		setCookie('coupon_print', 1);
	}
	return false;
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
    }
  }
	return "";
}

function whichElement(e) {
	var targ;
	
	if (!e) {
	
	  var e=window.event;
	}
	
	if (e.target) {
	  
	  targ=e.target;
	} else if (e.srcElement) {
	  targ=e.srcElement;
	}
	
	targ = $(targ);
	
	if (targ.get(0).nodeType==3 || typeof(targ.parents().get(0)) == 'undefined') {
	  
	  targ = $(targ.parentNode);
	}
	
	var clicked_in_box = false;
	
	var parents  = targ.parents();
	
	for( i=0; i < parents.length; i++ ) {
	
		if ( parents[i].id == 'select_box' ) {
		
			clicked_in_box = true;
			break;
		}  
	}
	
	if ( !clicked_in_box ) {
	
		var select_box_options = $( '#select_options' );
		
		if ( typeof( select_box_options.get(0) ) == 'undefined' ) {
		
			return false;
		}
	
		if ( ( select_box_options ) && ( select_box_options.length > 0 ) && ( select_box_options.get(0).style.display == 'block' ) ) {
		
			if ( $( '#service_list_content:visible' ).get(0) ) {
				
				select_box_options.css( 'display', 'none' );
			}
		}
	}
}

function expandBlock( elem ) {
	
	var expandable_block	= $( elem ).parents( '.expandable_block:first' );
	var content				= expandable_block.find( '.content' );
	var show_button			= expandable_block.find( '.show_button' );
	var hide_button			= expandable_block.find( '.hide_button' );
	var show 				= $( elem ).parent().hasClass( 'show_button' );

	var collapsed_expandables = $.cookie( 'collapsed_expandables' ) ? $.cookie( 'collapsed_expandables' ).split( '|' ) : [];

	if ( show ) {

		show_button.hide();
		hide_button.show();
		content.show();
		
		if ( $.inArray( expandable_block.attr( 'id' ), collapsed_expandables ) != -1 ) {

			collapsed_expandables.splice( $.inArray( expandable_block.attr( 'id' ), collapsed_expandables ), 1 );
		}
	} else {

		show_button.show();
		hide_button.hide();
		content.hide();

		if ( $.inArray( expandable_block.attr( 'id' ), collapsed_expandables ) == -1 ) {

			collapsed_expandables.push( expandable_block.attr( 'id' ) );
		}
	}

	$.cookie( 'collapsed_expandables', collapsed_expandables.join( '|' ), { expires: 31, path: '/' } );
}

function couponAction( a, enterprise_id, no_nav ) {
			
	var coupon_id = a.id.substr( 7 );
	var url = root_url + 'index.php?act=coupon&coupon_id=' + coupon_id + '&enterprise_id=' + enterprise_id + '&height=480&width=640';
	
	if ( no_nav ) {
		
		url += '&no_nav=1';
	}
	
	a.href = url;
	
	$( a ).fancybox( {
		showCloseButton : true,
		margin: 0,
		padding: 0,
		overlayOpacity: 0.6,
		overlayColor: 'black'
	} );
	
	$( a ).trigger( 'click' );
}

function overItem( offer_id ) {
	$( '#offer_top_' + offer_id ).addClass( 'over' );
	$( '#offer_' + offer_id ).addClass( 'over_mid' );
	$( '#offer_bot_' + offer_id ).addClass( 'over' );
}
function outItem( offer_id ) {
	$( '#offer_top_' + offer_id ).removeClass( 'over' );
	$( '#offer_' + offer_id ).removeClass( 'over_mid' );
	$( '#offer_bot_' + offer_id ).removeClass( 'over' );
	
}
