//Document Ready code
$(document).ready(function() {

	$('.jquery_datepicker').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd'
	});

	// Convert Multi Selects
	$('.jquery_multiselect').multiselect({width: '500'});

	// Country Picker
	$('#CountryPicker').change(function(){
		$(location).attr('href', this.options[this.selectedIndex].value);
	});

	// Preferred Picker
	$('#PreferredPicker').change(function(){
		$.get('/ajax/changepreferred', {countryId: $('#PreferredPicker').val()}, function(success) {
			if (success) {
				location.reload();
			}
		});
	});

	// Partner Picker
	$('#PartnerPicker').change(function(){
		$(location).attr('href', this.options[this.selectedIndex].value);
	});

	// Visa Picker
	$('#VisaPicker').change(function(){
		$(location).attr('href', window.location+'/'+this.options[this.selectedIndex].value);
	});

	// Get Country of Birth
	$('#CanDoB').change(function(){
		getCountries('#CanCoB', '#CanDoB');
	});

	// Results Summary
	$("div.solutions_summary").accordion({
		collapsible: true,
		active: false,
		autoHeight: false
	});

	// DIY Packs
	$("div.diypacks_summary").accordion({
		collapsible: true,
		active: false,
		autoHeight: false
	});

	$( "#dialog-message" ).dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$( this ).dialog( "close" );
			}
		}
	});

	$('.close_link').click(function() {
		if (confirm('Are you sure?')) {
			var el = $(this);
			var user = el.attr('href').match(/.*\/([\d]+)$/)[1];
			$.ajax({
				url: '/ajax/close',
				method: 'GET',
				data: {id: user},
				success: function() {
					el.text('Closed');
				}
			});
		}
		return false;
	});

	$('#CountryID, #VisaTypeID').change(function() {
		var visa_type = $('#VisaTypeID').val();
		var country = $('#CountryID').val();
		if (!!country && !!visa_type) {
			getSolution(country, visa_type);
		}
	});

	$('#DateFrom, #DateTo, #PartnerID, #SourceID').change(function() {
		$('#sent_monthly').hide().text('Sent: 0');
		var dateFrom = $('#DateFrom').val();
		var dateTo = $('#DateTo').val();
		var parentId = $('#PartnerID').val();
		var sourceId = $('#SourceID').val();
		if (!!dateFrom && !!dateTo && !!parentId && !!sourceId) {
			showLoader('monthly_ajax');
			$.getJSON('/ajax/email-count', {dateFrom: dateFrom, dateTo: dateTo, parentId: parentId, sourceId: sourceId, type: 'monthly'}, function(j) {
				$('#total_monthly').text(j.total);
				hideLoader('monthly_ajax');
			});
		}
	});

	$('#CountryID, #VisaTypeID, #SolutionID').change(function() {
		$('#sent_specific').hide().text('Sent: 0');
		var countryId = $('#CountryID').val();
		var visaTypeId = $('#VisaTypeID').val();
		var solutionId = $('#SolutionID').val();
		if (!!countryId && !!visaTypeId && !!solutionId) {
			showLoader('specific_ajax');
			$('#CountryID, #VisaTypeID, #SolutionID').attr('disabled', 'disabled');
			$.getJSON('/ajax/email-count', {countryId: countryId, visaTypeId: visaTypeId, solutionId: solutionId, type: 'specific'}, function(j) {
				$('#total_specific').text(j.total);
				hideLoader('specific_ajax');
				$('#CountryID, #VisaTypeID, #SolutionID').removeAttr('disabled');
			});
		}
	});

	$('#DateFrom_filtered, #DateTo_filtered, #PartnerID_filtered, #SourceID_filtered').change(function() {
		$('#sent_filtered').hide().text('Sent: 0');
		var dateFrom = $('#DateFrom_filtered').val();
		var dateTo = $('#DateTo_filtered').val();
		var parentId = $('#PartnerID_filtered').val();
		var sourceId = $('#SourceID_filtered').val();
		if (!!dateFrom && !!dateTo && !!parentId && !!sourceId) {
			showLoader('filtered_ajax');
			$.getJSON('/ajax/email-count', {dateFrom: dateFrom, dateTo: dateTo, parentId: parentId, sourceId: sourceId, type: 'filtered'}, function(j) {
				$('#total_filtered').text(j.total);
				hideLoader('filtered_ajax');
			});
		}
	});

	$('#form_monthly').submit(function() {
		var dateFrom = $('#DateFrom').val();
		var dateTo = $('#DateTo').val();
		var parentId = $('#PartnerID').val();
		var sourceId = $('#SourceID').val();
		if (!!dateFrom && !!dateTo && !!parentId && !!sourceId) {
			$('#sent_monthly').hide().text('Sent: 0');
			showLoader('monthly_ajax');
			createIFrame($(this).serialize());
		}
		return false;
	});

	$('#form_specific').submit(function() {
		var countryId = $('#CountryID').val();
		var visaTypeId = $('#VisaTypeID').val();
		var solutionId = $('#SolutionID').val();
		if (!!countryId && !!visaTypeId && !!solutionId) {
			$('#sent_specific').hide().text('Sent: 0');
			showLoader('specific_ajax');
			createIFrame($(this).serialize());
		}
		return false;
	});

	$('#form_filtered').submit(function() {
		var dateFrom = $('#DateFrom_filtered').val();
		var dateTo = $('#DateTo_filtered').val();
		var parentId = $('#PartnerID_filtered').val();
		var sourceId = $('#SourceID_filtered').val();
		if (!!dateFrom && !!dateTo && !!parentId && !!sourceId) {
			$('#sent_filtered').hide().text('Sent: 0');
			showLoader('filtered_ajax');
			createIFrame($(this).serialize());
		}
		return false;
	});

	$('#form_custom').submit(function() {
		var body = CKEDITOR.instances.Email_Body.getData();
		$('#Email_Body').val(body);
		$('#sent_filtered').hide().text('Sent: 0');
		showLoader('custom_ajax');
		createIFrame($(this).serialize());
		return false;
	});

	$('#send_email').click(function() {
		showPopup('email-specific');
	});

	$('#show_terms').click(function() {
		showPopup('terms');
	});

	$('#send_specific').click(function() {
		var body = CKEDITOR.instances.body_specific.getData();
		$('#body_specific').val(body);
		$.ajax({
			url: '/ajax/email',
			type: 'POST',
			data: $('#specific_email_form').serialize(),
			success: function() {
				window.location.reload();
			}
		});
		disablePopup();
		return false;
	});

	$('#cancel_specific').click(function() {
		disablePopup();
	});

});

function saveSearch() {
	$.loading(true, {text: 'Saving...', mask: true, pulse: 'ellipsis'});
	$('#formdest').val('close');
	$('#frmSearch').submit();
}

function saveClose() {
	$.loading(true, {text: 'Saving...', mask: true, pulse: 'ellipsis'});
	$('#frmSearch').submit();
}

function closeClient() {
	$.loading(true, {text: 'Closing...', mask: true, pulse: 'ellipsis'});
	document.location = '/screener/close';
}

function editContactDetails(link) {
	$('#client-contact-static').toggle();
	$('#client-contact-edit').toggle();

	if ($(link).html() == 'edit') {
		$(link).html('cancel');
	} else {
		$(link).html('edit');
	}
}

function toggleFieldset(fieldset) {
	$('#fieldset-'+fieldset).animate({"height": "toggle", "opacity": "toggle"}, "slow");
}

function showFieldset(fieldset) {
	$('#fieldset-'+fieldset).animate({"height": "show", "opacity": "show"}, "slow");
}

function hideFieldset(fieldset) {
	$('#fieldset-'+fieldset).animate({"height": "hide", "opacity": "hide"}, "slow");
}

function toggleRequired(target) {
	if ( $('#'+target+'-row label em').length ) {
		hideRequired(target);
	} else {
		showRequired(target);
	}
}

function showRequired(target) {
	if ( !$('#'+target+'-row label em').length ) {
		$('#'+target+'-row label').append('<em><img alt="*" src="/images/site-required.gif"></em>');
	}
}

function hideRequired(target) {
	if ( !$('#'+target+'-row label em').length ) {
		$('#'+target+'-row label em').remove();
	}
}

function getVisas(visaSelect, countrySelect, visaTypeSelect) {
	$.getJSON('/ajax/visas', {countryId: $(countrySelect).val(), visaTypeId: $(visaTypeSelect).val()}, function(j) {
		var options = '';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		}

		if (j.length == 1) {
			$(visaSelect).attr('disabled', 'disabled');
		} else {
			$(visaSelect).removeAttr('disabled');
		}

		$(visaSelect).html(options);
	});
}


function getSectors(sectorSelect, industrySelect) {
	$.getJSON('/ajax/sectors', {industryId: $(industrySelect).val()}, function(j) {
		var options = '';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		}

		if (j.length == 1) {
			$(sectorSelect).attr('disabled', 'disabled');
		} else {
			$(sectorSelect).removeAttr('disabled');
		}

		$(sectorSelect).html(options);
	});
}

function getCountries(countrySelect, dateSelect) {
	if (dateSelect === undefined) {
		var date = null;
	} else {
		var date = $(dateSelect).val();
	}

	$.getJSON('/ajax/countries', {date: date}, function(j) {
		var origCountry = $(countrySelect).val();

		var options = '';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		}

		if (j.length == 1) {
			$(countrySelect).attr('disabled', 'disabled');
		} else {
			$(countrySelect).removeAttr('disabled');
		}

		$(countrySelect).html(options);

		$(countrySelect).val(origCountry);
	});
}

function getSolution(country, visa_type) {
	$.getJSON(
		'/ajax/solution',
		{country: country, visa_type: visa_type},
		function(j) {
			var options = '';
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}

			if (j.length == 1) {
				$('#SolutionID').attr('disabled', 'disabled');
			} else {
				$('#SolutionID').removeAttr('disabled');
			}

			$('#SolutionID').html(options).val('');
		}
	);
}

function showLoader(lId) {
	$('#' + lId).show();
}

function hideLoader(lId) {
	$('#' + lId).hide();
}

function Monthly_Update(data) {
	$('#sent_monthly').text('Sent: ' + ~~data.text).show();
}

function Monthly_Finish() {
	hideLoader('monthly_ajax');
}

function Specific_Update(data) {
	$('#sent_specific').text('Sent: ' + ~~data.text).show();
}

function Specific_Finish() {
	hideLoader('specific_ajax');
}

function Filtered_Update(data) {
	$('#sent_filtered').text('Sent: ' + ~~data.text).show();
}

function Filtered_Finish() {
	hideLoader('filtered_ajax');
}

function Custom_Update(data) {
	$('#sent_custom').text('Sent: ' + ~~data.text).show();
}

function Custom_Finish() {
	hideLoader('custom_ajax');
}

function createIFrame(params) {
	var iFrame = document.createElement('iframe');
	iFrame.style.display = 'none';
	document.getElementsByTagName('body')[0].appendChild(iFrame);
	iFrame.src = '/screener/send-email?' + params;
}
