function checkNonEmpty(text) {
  	return text != '' && text != null && text != 'undefined';
}

function checkRequiredParams( requiredParams ) {
	var result = true; 
	if (requiredParams instanceof Array) {
		for (var i = 0; i < requiredParams.length; i++) {
			result = result && checkNonEmpty( getCookie(requiredParams[i]) );
		}
	}
	return result;
}

function commonDefendDownload( resourceUrl, resourceName, registrationUrl, requiredParams, commonEmail ) {
	var cookieValue = getCookie(USER_REGISTERED_COOKIE_VALUE);
	
    var navigateToUrl;
    if ( cookieValue != '' && cookieValue != null && cookieValue != 'undefined' && 
		checkRequiredParams(requiredParams) ) {
			
		navigateToUrl = resourceUrl;
		var date = getExpirationDate(COOKIE_DEFAULT_EXPIRATION_PERIOD);
		setCookie('filename', resourceUrl, date, COOKIE_DEFAULT_PATH);
		// Sending email about downloading file
		try {
			if (commonEmail) {
				commonSendEmail(requiredParams);
			} else {
				sendEmail();
			}
		} catch (e)
		{
		// Can't send email...
		}
	}
    else
    {
        var date = getExpirationDate(COOKIE_DEFAULT_EXPIRATION_PERIOD);
        setCookie(REQUESTED_RESOURCE_URL_COOKIE_VALUE, resourceUrl, date, COOKIE_DEFAULT_PATH);
        setCookie(REQUESTED_RESOURCE_NAME_COOKIE_VALUE, resourceName, date, COOKIE_DEFAULT_PATH);
		setCookie('filename', resourceUrl, date, COOKIE_DEFAULT_PATH);
        navigateToUrl = registrationUrl;
    }
    window.open(navigateToUrl);
    return false;
}

function gartnerDefendDownload( resourceUrl, resourceName, registrationUrl ) {
	return commonDefendDownload( resourceUrl, resourceName, registrationUrl, 
		["first_name","last_name","company","phone","work_email","country"], true );
}

function defendDownload( resourceUrl, resourceName, registrationUrl ) {
	return commonDefendDownload( resourceUrl, resourceName, registrationUrl, 
		["first_name","last_name","company_name","city","phone","email","country"], false );
}
