/**
 * @author JNB
 */

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for (var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == " ") {
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}



/**
 * submitQuickfinderForm
 * @param {String} dom_form_id
 * @param {String} path
 * @param {String} key
 * @param {String} locale
 * @param {String} url_alias
 */
function submitQuickfinderForm(dom_form_id, path, key, locale, url_alias) {

	showAjaxLoader();

	// get form dijits
	var treatment_select_list = $('#' + dom_form_id + '_treatment');
	var country_select_list = $('#' + dom_form_id + '_country');

	if ((treatment_select_list.val() == "" || treatment_select_list.val() == "0") && (country_select_list.val() == "" || country_select_list.val() == "0")) {
		hideAjaxLoader();
		// show error dialog
		$.nyroModalManual({
			content: "<h4>" + quickfinder_not_valid_title + "</h4>" + "<p>" + quickfinder_not_valid_text + "</p><button class='nyroModalClose'>" + quickfinder_not_valid_button + "</button>",
			modal: true,
			minHeight: 0
		});
	} else {
		//var search_options = new Array();
		var search_options = new Object;

		// first element = treatment
		search_options.treatment_category = treatment_select_list.val();
		// second element = country
		search_options.country = country_select_list.val();

		// prepare labels for search query
		if (locale == "de") {
			var category_label = "kategorie";
			var country_label = "land";
		} else if (locale == "en") {
			var category_label = "category";
			var country_label = "country";
		} else if (locale == "ru") {
			var category_label = "kategoriya";
			var country_label = "strana";
		}
		var search_query_string_parts = new Array();
		if (search_options.treatment_category != "" && search_options.treatment_category != "0") {
			search_query_string_parts.push(category_label + "=" + search_options.treatment_category);
		}
		if (search_options.country != "" && search_options.country != "0") {
			search_query_string_parts.push(country_label + "=" + search_options.country);
		}
		var search_query_string = "?";
		search_query_string += search_query_string_parts.join("&");

		// redirect to search result page
		document.location.href = path + url_alias + search_query_string;
	}
}


/**
 * submitSearchForm
 * @param {String} dom_form_id
 * @param {String} url_alias
 */
function submitSearchForm(dom_form_id, url_alias) {

	showAjaxLoader();

	var search_options = new Object;
	var validate = false;

	for (i = 0; i < dual_select_groups.length; i++) {
		dual_select_group = dual_select_groups[i];
		var standard_form = $(dom_form_id);

		var htmlOptions = new Array();
		if (dual_select_group == "treatments") {
			htmlOptions = $("#target_treatments")[0].options;
		} else if (dual_select_group == "countries") {
			htmlOptions = $("#target_countries")[0].options;
		} else if (dual_select_group == "languages") {
			htmlOptions = $("#target_languages")[0].options;
		}

		var options = new Array();
		for (a = 0; a < htmlOptions.length; a++) {
			options.push(htmlOptions[a].value);
		}
		if (options.length > 0) {
			validate = true;
		}
		var options_string = options.join("|");

		if (dual_select_group == "treatments") {
			search_options.treatment = options_string;
		} else if (dual_select_group == "countries") {
			search_options.country = options_string;
		} else if (dual_select_group == "languages") {
			search_options.language = options_string;
		}
	}

	if (validate == true) {
		// post form content to server
		$.getJSON(path + 'application/controller/handleProviderSearchData.NEW.php?action=search_provider&sid=' + key,
			search_options,
			function(json){
				if (json.success == "true") {
					//console.debug(json.success);
					document.location.href = path + url_alias + '/?sid=' + key;
				} else {
					hideAjaxLoader();

					// show failure dialog box
					$.nyroModalManual({
						content: "<h4>" + no_search_result_found_title + "</h4>" + "<p>" + no_search_result_found_text + "</p><button class='nyroModalClose'>" + searchform_not_valid_button + "</button>",
						modal: true,
						minHeight: 0
					});
				}
			}
		);
	} else {
		hideAjaxLoader();

		// show error dialog
		$.nyroModalManual({
			content: "<h4>" + searchform_not_valid_title + "</h4>" + "<p>" + searchform_not_valid_text + "</p><button class='nyroModalClose'>" + searchform_not_valid_button + "</button>",
			modal: true,
			minHeight: 0
		});
	}
}

/**
 * sendSearchRequest
 * send search request by link
 *
 * IMPORTANT: javascript variables used from quickfinder:
 * path, key, locale, url_alias, quickfinder_no_search_result_found_title, quickfinder_no_search_result_found_text, quickfinder_no_search_result_found_button
 *
 * Parameters must be given in the format x|y|z
 * Example:
 * <a href="#" onclick="sendSearchRequest('1|2',null,'IN',null); return false;">Augenbehandlungen und Zahnbehandlungen in Indien</a>
 *
 * @param {String} treatment_category
 * @param {String} treatment
 * @param {String} country
 * @param {String} language
 */
function sendSearchRequest(treatment_category, treatment, country, language) {
	showAjaxLoader();

	var search_options = new Object;
	var validate = false;
	// treatment_category handling
	if (typeof treatment_category == "string") {
		search_options.treatment_category = treatment_category;
	} else {
		search_options.treatment_category = '';
	}
	// treatment handling
	if (typeof treatment == "string") {
		search_options.treatment = treatment;
	} else {
		search_options.treatment = '';
	}
	// country handling
	if (typeof country == "string") {
		search_options.country = country;
	} else {
		search_options.country = '';
	}
	// language handling
	if (typeof language == "string") {
		search_options.language = language;
	} else {
		search_options.language = '';
	}

	// prepare labels for search query
	if (locale == "de") {
		var category_label = "kategorie";
		var treatment_label = "behandlung";
		var country_label = "land";
	} else if (locale == "en") {
		var category_label = "category";
		var treatment_label = "treatment";
		var country_label = "country";
	} else if (locale == "ru") {
		var category_label = "kategoriya";
		var treatment_label = "lechenie";
		var country_label = "strana";
	}

	var search_query_string_parts = new Array();
	if (search_options.treatment_category != "" && search_options.treatment_category != "0") {
		search_query_string_parts.push(category_label + "=" + search_options.treatment_category);
	}
	if (search_options.treatment != "" && search_options.treatment != "0") {
		search_query_string_parts.push(treatment_label + "=" + search_options.treatment);
	}
	if (search_options.country != "" && search_options.country != "0") {
		search_query_string_parts.push(country_label + "=" + search_options.country);
	}
	var search_query_string = "?";
	search_query_string += search_query_string_parts.join("&");

	// redirect to search result page
	document.location.href = path + url_alias + search_query_string;
}



/**
 * memorizeProviderOnNotepad
 * @param {String} user_id
 * @param {String} provider_id
 * @param {String} link_id
 * @param {String} referrer
 */
function memorizeProviderOnNotepad(user_id, provider_id, link_id, referrer){
	if (typeof user_id != "undefined" && user_id > 0 && typeof provider_id != "undefined") {
		// post request to server
		$.getJSON(path + 'application/controller/handleFrontendUserData.NEW.php?action=add_notepad_entry&user_id=' + user_id + '&provider_id=' + provider_id,
			function(json){
				if (json.success == true) {
					// set new image for memorized provider
					$("#" + link_id).html('<img src="' + path + 'application/cms/img/content/note_active.png" />');
					// show notepad link in quickfinder
					$("#frontend_user_notepad_link").css('visibility', 'visible');
					$("#frontend_user_notepad_link_container").css('display', 'block');
				} else {
					//
				}
			}
		);
	} else {  // no user id, means no user logged in
		if (url_alias != "") {
			referrer = url_alias;
		}
		document.location.href = path + "user/login/?destination=" + referrer;
	}
}


/**
 * deleteProviderFromNotepad
 * @param {Object} user_id
 * @param {Object} provider_id
 */
function deleteProviderFromNotepad(user_id, provider_id){
	if (typeof user_id != "undefined" && typeof provider_id != "undefined") {
		// post request to server
		$.getJSON(path + 'application/controller/handleFrontendUserData.NEW.php?action=delete_notepad_entry&user_id=' + user_id + '&provider_id=' + provider_id + '&sid=' + key,
			function(json){
				if (json.success == true) {
					// hide row incl. seperator row in notepad list
					$("#notepad_row_seperator_" + provider_id).css('visibility', 'hidden');
					$("#notepad_row_" + provider_id).css('visibility', 'hidden');
				} else {
					//
				}
			}
		);
	}
}

/* ajax loader image */

function windowDimensions() {
	var x, y;
	if (self.innerHeight) {
		// all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	if (!x) x = 0;
	if (!y) y = 0;
	arrayWindowSize = new Array(x,y);
	return arrayWindowSize;
}

function scrollOffset() {
	var x, y;
	if (self.pageYOffset) {
		// all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		// Explorer 6 Strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	} else if (document.body) {
		// all other Explorers
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	if (!x) x = 0;
	if (!y) y = 0;
	arrayScrollOffset = new Array(x,y);
	return arrayScrollOffset;
}

function showAjaxLoader() {
	var element = $('#ajax_loader');
	var win = windowDimensions();
	var scrol = scrollOffset();
	element.css({
		"display": "inline",
		"top": (win[1] / 2) + scrol[1] - (element.height() / 2) + "px",
		"left": (win[0] / 2) + scrol[0] - (element.width() / 2) + "px"
	});
}

function hideAjaxLoader() {
	$('#ajax_loader').css({"display": "none"});
}