var psdmModal  = '';
var psdmAutoTriggers = {};
window.onclick = function (event) {
	if (event.target === psdmModal) {
		psdmModal.style.display = "none";
	}
}


function psdmShowModal(element) {
	var psdmDialogName      = 'psdm-modal-' + element.value;
	psdmModal               = document.getElementById( element.value );
	psdmModal.style.display = "block";
}

function psdmShowButtonModal(id) {
	var psdmDialogName      = 'psdm-modal-' + id;
	psdmModal               = document.getElementById( id );
	if (psdmAutoTriggers[id.toString()]) {
		psdmAutoTriggers[id.toString()].hasShown = true;
	}
	psdmModal.style.display = "block";
}

function psdmRegisterAutoTriggers(id, options) {
	var triggerId = id.toString();
	if (psdmAutoTriggers[triggerId]) {
		return;
	}

	psdmAutoTriggers[triggerId] = {
		hasShown: false
	};

	var trigger = psdmAutoTriggers[triggerId];
	var showModal = function () {
		if (trigger.hasShown) {
			return;
		}

		var modal = document.getElementById( triggerId );
		if (! modal) {
			return;
		}

		psdmShowButtonModal( triggerId );
	};

	if (options.delaySeconds > 0) {
		window.setTimeout( showModal, options.delaySeconds * 1000 );
	}

	if (options.exitIntent) {
		document.addEventListener(
			'mouseout',
			function (event) {
				if (! event.relatedTarget && event.clientY <= 0) {
					showModal();
				}
			}
		);
	}
}

window.psdmRegisterAutoTriggers = psdmRegisterAutoTriggers;

if (window.psdmAutoTriggerQueue) {
	window.psdmAutoTriggerQueue.forEach(
		function (trigger) {
			psdmRegisterAutoTriggers( trigger[0], trigger[1] );
		}
	);
}

function psdmCloseModal() {
	psdmModal.style.display = "none";
}

function psdmdownload(id) {
	var inError             = false;
	var psdm_id             = 'psdm_file_' + id;
	var psdm_url            = 'psdm_url_' + id;
	var psdm_name_required  = document.getElementById( 'psdm_name_required_' + id ).value;
	var psdm_hp_required    = document.getElementById( 'psdm_hp_required_' + id ).value;
	var psdm_name_error     = '#psdm_name_error_' + id;
	var psdm_email_error    = '#psdm_email_error_' + id;
	var psdm_required_error = '#psdm_required_error_' + id;
	if (psdm_name_required === '1') {
		var psdm_dataName = document.getElementById( 'psdm_name_' + id ).value;
	} else {
		var psdm_dataName = '';
	}
	if (psdm_hp_required === '1') {
		var psdm_dataHp = document.getElementById( 'psdm_pot_' + id ).value;
	} else {
		var psdm_dataHp = '';
	}
	var psdm_dataEmail     = document.getElementById( 'psdm_email_' + id ).value;
	var psdm_permission    = document.querySelector( '.psdm_permission_' + id ).checked.toString();
	var psdm_dataSubmitted = 'psdm_file_' + id;
	var psdm_aj_url        = document.getElementById( psdm_url ).value;
	var data               = {
		'action': 'psdm_download_file',
		'psdm_use_nonce': document.getElementById( 'psdm_nonce' ).value,
		'psdm_name': psdm_dataName,
		'psdm_email': psdm_dataEmail,
		'psdm_permission' : psdm_permission,
		'psdm_hp' : psdm_dataHp,
		'psdm_submitted': document.getElementById( psdm_dataSubmitted ).value,
		'psdm_file': document.getElementById( psdm_id ).value,
	};
	if (psdm_dataName == '' && psdm_name_required === '1') {
		jQuery( psdm_name_error ).html( 'Name is required' );
		inError = true;
	}
	if (psdm_dataEmail == '') {
		jQuery( psdm_email_error ).html( 'Email address is required' );
		inError = true;
	}
	if (psdmEmailIsValid( psdm_dataEmail ) == false) {
		jQuery( psdm_email_error ).html( 'Please enter a valid email address' );
		inError = true;
	}
	if (psdm_permission == 'false') {
		jQuery( psdm_required_error ).html( 'Please click to give permission for us to send emails to you.' );
		inError = true;
	}
	if (inError) {
		inError = false;
		return;
	}
	jQuery.post(
		psdm_aj_url,
		data,
		function (response) {
			switch (response) {
				case '0':
					alert( 'Sorry something went wrong. Please refresh the page and try again.' );
					break;
				default:
					var a      = document.createElement( "a" );
					a.href     = response;
					fileName   = response.split( "/" ).pop();
					a.download = fileName;
					document.body.appendChild( a );
					a.click();
					window.URL.revokeObjectURL( response );
					a.remove();
					break;

			}
		}
	);
	psdmModal.style.display = "none";
	// jQuery.modal.close();
}

function psdmjustsend(url) {
	var a      = document.createElement( "a" );
	a.href     = url;
	fileName   = url.split( "/" ).pop();
	a.download = fileName;
	document.body.appendChild( a );
	a.click();
	window.URL.revokeObjectURL( url );
	a.remove();
}

function psdmEmailIsValid(email) {
	return /\S+@\S+\.\S+/.test( email )
}
