function redirectTo(url){
	window.location = url;
}

function isNull(obj){
	if (obj == null || obj == '' || obj == 'undefined'){
		return true;
	}
	return false;
}


function isTrue(obj){
	if (obj == 'true' || obj == true){
		return true;
	}
	return false;
}

function isFalse(obj){
	if (obj == 'false' || obj == false){
		return true;
	}
	return false;
}

function createOption(id, val, isSelected){
	var option = '<option value="'+id+'" '+isSelected+'>'+val+'</option>';
	return option;
}


function addComma(index, array){
	return index != array.length -1 ? ', ' : '';
}


function disableUnchecked(){
	var allCheckboxes = $('input[name=teeTimeCheckbox]');
	for (var i=0; i < allCheckboxes.length; i++){
		var ckb = $(allCheckboxes[i]);
		if (!ckb.is(':checked')){
			ckb.attr ( "disabled" , true );
		}
	}
}

function enableAllCheckboxes(){
	$('input[name=teeTimeCheckbox]').removeAttr ( "disabled" );
}

function calculateRangeBetweenTimes(checkedItems){
	var secInHr = 3600;
	var time1 = checkedItems[0];
	var time2 = checkedItems[1];
	if (isNull(time1) || isNull(time2)){
		return false;
	}
	//validate range
	//time1
	var date1 = new Date();
	var indexHHmm1 = time1.value.split('#');
	var hhmm1 = indexHHmm1[1].split(":");
	date1.setHours(hhmm1[0], hhmm1[1], 0, 0);
	var timestamp1 = date1.getTime();
	
	//time2
	var indexHHmm2 = time2.value.split('#');
	var hhmm2 = indexHHmm2[1].split(":");
	date1.setHours(hhmm2[0], hhmm2[1], 0, 0);
	var timestamp2 = date1.getTime();
	
	//check the range
	var range = Math.abs(timestamp1 - timestamp2);
	var result = range / secInHr ; // 1hr = seconds * 60min
	return result;
}

function checkFirstBeforeSecond(firstTime, secondTime){
	var date1 = new Date();
	var indexHHmm1 = firstTime.split('#');
	var hhmm1 = indexHHmm1[1].split(":");
	date1.setHours(hhmm1[0], hhmm1[1], 0, 0);
	var timestamp1 = date1.getTime();
	
	var indexHHmm2 = secondTime.split('#');
	var hhmm2 = indexHHmm2[1].split(":");
	date1.setHours(hhmm2[0], hhmm2[1], 0, 0);
	var timestamp2 = date1.getTime();
	
	return timestamp1 < timestamp2;
}

function enableSpecialOffer(){
	var players = countCompanions();
	var offers = $('.offerItemRadio');
	for (var i=0; i< offers.length; i++){
		$(offers[i]).attr('disabled', true);
	}
	if (players >= 1){
		$('.players_1').attr('disabled', '');
	}
	if (players >= 2){
		$('.players_2').attr('disabled', '');
	}
	if (players >= 3){
		$('.players_3').attr('disabled', '');
	}
	if (players == 4){
		$('.players_4').attr('disabled', '');
	}
	
	$('input[name=reset_offers]').attr('disabled', '');
}

function isNameEmpty(name){
	if (isNull(name) || name == DEFAULT_MEMBERS_NAME_MSG){
		return true;
	}
	return false;
}

function checkTotalPlayersNumber(players){
	if (players > MAX_COMPANIONS_NUMBER){
		//NotifyDialog.error(TO_MANY_COMPANIONS_MSG);
		//$('#finishBtnId').attr('disabled', true);
	}else{
		//$('#finishBtnId').attr('disabled', '');
	}
}

function addDefaultValueToMemberName(){
		var types = $('.memberTypeName');
		for (var i=0; i< types.length; i++){
			$(types[i]).blur(function(){
			var membersNumber = countMembers();
			var visitorsNumber = countCompanions();
			var totalPlayers = membersNumber + visitorsNumber + 1;
			//checkTotalPlayersNumber(totalPlayers);
		});
		$(types[i]).defaultValue(DEFAULT_MEMBERS_NAME_MSG);
	}
}

function countCompanions(){
		var companions = $('.companionCounts');
		var players = 0;
		for (var i=0; i < companions.length; i++){
			players += parseInt(companions[i].value);
		}
		//checkTotalPlayersNumber(players + 1);
		return players;
	}

function countMembers(){
	var number = 0;
	var types = $('.memberTypeName');
	for (var i=0; i< types.length; i++){
		var name = $(types[i]).val();
		if (!isNameEmpty(name)){
			number++;
		}
	}
	return number;
}

function createOptions(val, count){
		var options = '';
		var selected = '';
		for (var i=0; i<= count; i++){
			if (val == i){
				selected = 'selected';
			}else{
				selected = ''
			}
			options += createOption(i, i, selected);
		}
		return options;
	}

function createOptions2(val, count){
	var options = '';
	var selected = '';
	var temp = parseInt(val) + parseInt(count);
	for (var i=0; i<= temp; i++){
		if (val == i){
			selected = 'selected';
		}else{
			selected = ''
		}
		options += createOption(i, i, selected);
	}
	return options;
}

function getCurrentTarget(event){
	var target = (event.currentTarget) ? event.currentTarget : event.srcElement;
	return target;
}


function applyTeeTimeRestrictions(typeIds,productIds){
	removeOptionByTypeIdFromDetails(typeIds);
	removeOptionByTypeIdFromCompanions(typeIds);
	removeCompanionByTypeId(typeIds);
	removeProductsById(productIds);
}

function removeOptionByTypeIdFromDetails(id){
	//set default user types
	$("select[name=userDetails.userTypeId]").html(USER_TYPES_OPTIONS);
	if (isNull(id)){
		return;
	}
	var arr = id.split(',');
	for (var i=0; i<arr.length; i++){
		$("select[name=userDetails.userTypeId] option[value='"+arr[i]+"']").remove();
	}
}

function removeOptionByTypeIdFromCompanions(id){
	$("select.memberTypes").html("<option value=''>Select Type</option>"+MEMBER_TYPES_OPTIONS);
	if (isNull(id)){
		return;
	}
	var arr = id.split(',');
	for (var i=0; i<arr.length; i++){
		$("select.memberTypes option[value='"+arr[i]+"']").remove();
	}
}

function removeCompanionByTypeId(id){
	//show all of them
	var companions = $('.companion_line');
	for (var i=0; i<companions.length; i++){
		$(companions[i]).show();
	}
	if (isNull(id)){
		return;
	}
	//exclude restricted types
	var arr = id.split(',');
	for (var i=0; i<arr.length; i++){
		$("#member_type_line_"+arr[i]).hide();
	}
}

function removeProductsById(productIdList){
	//show all of them
	var companions = $('.companion_line');
	for (var i=0; i<companions.length; i++){
		$(companions[i]).show();
	}
	if (isNull(productIdList)){
		return;
	}
	//exclude restricted products
	var arr = productIdList.split(',');
	for (var i=0; i<arr.length; i++){
		$("#member_type_line_"+arr[i]).hide();
	}
}

function hideSpecialOffersTab(){
	$('#offer_2for1_line_id').hide();
	$('#voucher_line_id').hide();
	$('#promo_code_line_id').hide();
	$('#special_offer_line_id').hide();
}

function showOffers(specialOfferAvailable, specOfferIds, promoOfferAvailable, voucherOfferAvailable, offer2For1Available){
	var listOffers = $('.specialOfferList');
	for (var i=0; i<listOffers.length;i++){
		$(listOffers[i]).hide();
	}
	$('#offer_2for1_line_id').show();
	$('#voucher_line_id').show();
	$('#promo_code_line_id').show();
	$('#special_offer_line_id').show();
	//hide if there are no offes for particular tee time
	if (isFalse(specialOfferAvailable)){
		$('#special_offer_line_id').hide();
	}else{
		var ids = specOfferIds.split(',');
		if (ids.length > 0){
			$('#specialOfferTitleId').show();
		}
		for (var i=0; i < ids.length; i++){
			$('#special_offer_'+ids[i]).show();
		}	
	}
	if (isFalse(promoOfferAvailable)){
		$('#promo_code_line_id').hide();
	}
	if (isFalse(voucherOfferAvailable)){
		$('#voucher_line_id').hide();
	}
	if (isFalse(offer2For1Available)){
		$('#offer_2for1_line_id').hide();
	}
}

function getSizeGroup(){
	var size = $('select[name=numberStandardGroupPlayers]').val();
	return parseInt(size);
}

function openInvoice(appName, invoiceId){
	if (isNull(invoiceId)){
		return;
	}
	var url = appName+"/starter/booking/invoice/download.pdf?invoiceId="+invoiceId;
	window.location = url;
}

/**
* It brings a pop up dialog with a confirmation messages. if the cancel
* button is hitten then the js stops ant browsers event else it works
* as normal; You can use it like this: <code>
* &lt;a href="/entity/delete/ onclick='confirmDialog(event)&gt;&lt;/a&gt;'"
* </code>
*/
function confirmDialog(message){
	if(isNull(message))
		message = ARE_YOU_SURE_DIALOG;
	var cfm = confirm(message);
	if (isFalse(cfm)){
		return false;
	}
	return true;
}

function cancelConfirm(event){
	var cfm = confirm(ARE_YOU_SURE_DIALOG);
	if (isFalse(cfm)){
		if (event.preventDefault){ 
			event.preventDefault(); 
		}else{ 
			event.returnValue = false; 
		}
		return false;
	}
	return true;
}

function updateEquipments(time, bookingDate){
	var url = '/popup/equipment.do';
	$.post(url, {'time': time, 'bookingDate': bookingDate}, function(data){
		if (data.status == ERROR){
			NotifyDialog.error(data.trace);
			return;
		}
		for (var j=0; j<data.length; j++){
			var item = data[j];
			var options = "";
			for (var i=0; i<= item.currently; i++){
				var val = i>9 ? i : '&nbsp;'+i+'&nbsp;';
				options += createOption(i, val, '');	
			}
			$('select.eq_'+item.id).html(options);
		}
	}, 'json');
}

function unreadMessages() {

	var url = '/messages/unreadMessages.do';
	$.post(url, {}, function(data){
	if ( data.count != '') {
		$("#messageCount").html(data.count + 'new');
	}
	},'json');
}

