function show_must_login(){
    $("#mustlogin").show("fast");
}

function processBusiness()
{
	$("#processing").show();
	$("#btnSubmit").hide();
	
	$('#errorMsg').slideUp();
	$("#errorMsg").html('');
	
	var currency_ids = '';
	$.each($("input[name='currency_id[]']:checked"), function() {
		currency_ids += $(this).val()+',';
	});
	if(currency_ids != '') currency_ids = currency_ids.slice(0, -1);
	
	var card_type_ids = '';
	$.each($("input[name='card_type_id[]']:checked"), function() {
		card_type_ids += $(this).val()+',';
	});
	if(card_type_ids != '') card_type_ids = card_type_ids.slice(0, -1);
	
	
	$.post(SITE_ROOT+"ajax/business_process.php",
		{ 
			operation: 'processBusiness', 
			cat_id: $("#cat_id").val(),
			cat_type_id: $("#cat_type_id").val(),
			addr1: $("#addr1").val(),
			addr2: $("#addr2").val(),
			description_long: $("#description_long").val(),
			description_short: $("#description_short").val(),
			description_tiny: $("#description_tiny").val(),
			region_id: $("#region_id").val(),
			phone_800: $("#phone_8001").val()+$("#phone_8002").val()+$("#phone_8003").val(),
			phone_local: $("#phone_local1").val()+$("#phone_local2").val()+$("#phone_local3").val(),
			phone_alt: $("#phone_alt1").val()+$("#phone_alt2").val()+$("#phone_alt3").val(),
			fax: $("#fax1").val()+$("#fax2").val()+$("#fax3").val(),
			email: $("#email").val(),
			url: $("#url").val(),
			google_location: $("#google_location").val(),
			longitude: $("#longitude").val(),
			latitude: $("#latitude").val(),
			youtube: $("#youtube").val(),
			twitter: $("#twitter").val(),
			virtual_tour: $("#virtual_tour").val(),
			current_special: $("#current_special").val(),
			currency_id: currency_ids,
			card_type_id: card_type_ids,
			price_low: $("#price_low").val(),
			price_high: $("#price_high").val(),
			number_rooms: $("#number_rooms").val(),
			number_floors: $("#number_floors").val(),
			parking: $("#parking").val(),
			//star_rating: $("input[name='star_rating']:checked").val(),
			is_pool: ($("#is_pool").attr("checked") ? '1' : '0'),
			is_wifi: ($("#is_wifi").attr("checked") ? '1' : '0'),
			is_consierge: ($("#is_consierge").attr("checked") ? '1' : '0'),
			is_newspaper: ($("#is_newspaper").attr("checked") ? '1' : '0'),
			is_fitness: ($("#is_fitness").attr("checked") ? '1' : '0'),
			is_bar: ($("#is_bar").attr("checked") ? '1' : '0'),
			is_restaurant: ($("#is_restaurant").attr("checked") ? '1' : '0'),
			is_roomservice: ($("#is_roomservice").attr("checked") ? '1' : '0'),
			is_breakfast: ($("#is_breakfast").attr("checked") ? '1' : '0'),
			is_safedeposit: ($("#is_safedeposit").attr("checked") ? '1' : '0'),
			is_multilingual: ($("#is_multilingual").attr("checked") ? '1' : '0'),
			is_24hour: ($("#is_24hour").attr("checked") ? '1' : '0'),
			is_doorman: ($("#is_doorman").attr("checked") ? '1' : '0'),
			is_expresscheckout: ($("#is_expresscheckout").attr("checked") ? '1' : '0'),
			is_expresscheckin: ($("#is_expresscheckin").attr("checked") ? '1' : '0'),
			is_laundry: ($("#is_laundry").attr("checked") ? '1' : '0'),
			is_elevator: ($("#is_elevator").attr("checked") ? '1' : '0'),
			is_ac: ($("#is_ac").attr("checked") ? '1' : '0'),
			is_parking: ($("#is_parking").attr("checked") ? '1' : '0'),
			is_smokefree: ($("#is_smokefree").attr("checked") ? '1' : '0'),
			other_amenity_1: $("#other_amenity_1").val(),
			other_amenity_2: $("#other_amenity_2").val(),
			other_amenity_3: $("#other_amenity_3").val(),
			other_amenity_4: $("#other_amenity_4").val(),
			activity_level_id: $("#activity_level_id").val(),
			cuisine_type: $("#cuisine_type").val()
		},
		function(data){
			$('#errorMsg').slideUp();
			$("#errorMsg").html('');
			
			if(data[0].result=="success")
			{
				$("#biz_upload_form").submit(); //this form post will trigger either showUploadErrors or uploadSuccess when it has completed.
			}
			else
			{    
				$("#processing").hide();
				showErrors(data);
			}
		}, 
		"json"
	);
}

function showUploadErrors(data)
{
	$("#processing").hide();
	clearErrors();
		
	//loop through the JSON array and display the errors
	$("#errorMsg").html('Please correct the fields highlighted in red below.');
	$("#errorMsg").slideDown();	
	$.each(data[0], function(key,value) {                    
		if(key != "result")
		{
			$('label[for='+key+']').parent().addClass("error");
			$('#'+key).parent().addClass("error");
			
			$("#imgErrors").html($("#imgErrors").html()+value+"<br />");
			$("#imgErrors").slideDown();					
		}
	});
	$.scrollTo($('#scrollPosition'),800);
	$('#btnSubmit').show();
}

function uploadSuccess()
{
	$.post(SITE_ROOT+"ajax/business_process.php",
		{ 
			operation: 'updateStatus', 
			status_id: $("#status_id").val()
		},
		function(data){
			if(data[0].result=="success"){
				if($("#status_id").val() =="EDI") {
					$('#errorMsg').slideUp();
					$("#errorMsg").html('');
					$("#processing").hide();
				    $("#btnSubmit").show();
				    clearErrors();
				    $.scrollTo($('#scrollPosition'),800);
//				    $("#thankyouMsg").slideDown('slow',function(){
//				    	setTimeout(function(){ $("#thankyouMsg").slideUp("slow"); }, 3000);
//				    });
				    window.location=SITE_ROOT+"saved.php";
				}
				else
				{
					window.location=SITE_ROOT+"thanks.php";
				}
			}
		}, 
		"json"
	);
}

function setCategory()
{
	$.post(SITE_ROOT+"ajax/business_process.php",
		{ 
			operation: 'getCatTypes', 
			cat_id: $("#cat_id").val(),
			cat_type_id: $("#cat_type_id").val()
		},
		function(data){
			$("#cat_type_id_holder").html(data);
			checkAdditionalInfo();
		}, 
		"html"
	);
}

function checkAdditionalInfo()
{
	if($("#cat_id").val()=="HOT")
	{
		$("#hotel_info").show();
	}
	else
	{   
		$("#hotel_info").hide();
	}
	
	if($("#cat_id").val()=="ACT")
	{
		$("#activity_info").show();
	}
	else
	{   
		$("#activity_info").hide();
	}
	
	if($("#cat_id").val()=="RES")
	{
		$("#restaurant_info").show();
	}
	else
	{   
		$("#restaurant_info").hide();
	}
	checkGenInfo();
}
function checkGenInfo()
{
	$.post(SITE_ROOT+"ajax/business_process.php",
		{ 
			operation: 'getCatGeneralInfo', 
			cat_id: $("#cat_id").val()
		},
		function(data){
			if(data[0].gen_info=="1")
			{
				//$("#general_info").slideDown();
				$("#general_info").show();
			}
			else
			{   
				$("#general_info").hide();
			}
		}, 
		"json"
	);
}

function checkStatus()
{
	$.post(SITE_ROOT+"ajax/business_process.php",
		{ 
			operation: 'getStatus'
		},
		function(data){
			if(data[0].status_id=="DEL")
			{
				$("#biz_edit").hide();
				$("#biz_waiting").hide();
				$("#biz_del").show();
			}
			else if(data[0].status_id=="WAT")
			{
				$("#biz_edit").hide();
				$("#biz_del").hide();
				$("#biz_waiting").show();
			}
			else
			{   
				$("#biz_waiting").hide();
				$("#biz_del").hide();
				$("#biz_edit").show();
			}
		}, 
		"json"
	);
}

function resetFileField(photo_num)
{
	$('#photo_'+photo_num+'_holder').html('<input name="photo_'+photo_num+'" type="file" id="photo_'+photo_num+'" />');
}


$(document).ready(function() { 
	//getCharsRemaining('description_long',400);
	//getCharsRemaining('description_short',140);
	//getCharsRemaining('description_tiny',40);
	//getCharsRemaining('current_special',400);
	checkStatus();
	setCategory();
});
