var MAX_AMEC_COOKIE_DAYS = 1;

function __log(msg) {
	if (window.console) window.console.log(msg);
}

function changeLanguage(locale) {
	var txt = '?';
	if ('' != ('' + window.location['search'])) {
		var search = window.location['search'];
		var start = search.indexOf ('lang=');
		var end = search.indexOf ('&', start);
		if (start > 0) {
			txt = search.substr (0, start)
			if (end > start) {
				txt += search.substr (end + 1);
			}
			if ('&' != txt.substr (txt.length - 1, 1)) {
				txt += '&';
			}
		} else {
			txt = search + '&';
		}
	}

	window.location = window.location.pathname + txt + 'lang=' + locale;
}

function opencatalogue(cat) {
	//window.open('literature/catalogues/' + cat, null, 'status=no,menubar=no,toolbar=no,resizable=yes,height=600,width:800');
	try {
		__log('fancybox = ' + $.fancybox);
		if (undefined == $.fancybox) {
			__log('setting fancybox href for cat: ' + cat);
			parent.$.fancybox.href='literature.php?catalogue=' + cat;
			//$.fancybox.resize();
			//parent.alert('parent!!');
		} else {
			__log('initialising fancybox for cat: ' + cat);
			$.fancybox({
				'href': 'literature.php?catalogue=' + cat
			});
		}
	} catch (ex) {
		alert(ex);
	}
	
	return false;
}

function showReportGroupMembers(id) {
	window.location = 'sf_report_group.php?id=' + id;
}

function openSalesMatrix(year, id) {
	if (('' + year) == '') {
		year = prompt('Please enter the budget year');
	}
	if (year) {
		window.open('sf_sales_matrix.php?year=' + year + '&id=' + id);
	}

	return false;
}

function openSalesMatrixRG(year, id) {
	if (('' + year) == '') {
		year = prompt('Please enter the budget year');
	}
	if (year) {
		window.open('sf_sales_matrix.php?year=' + year + '&account_group=' + id);
	}

	return false;
}

function openDRS(year, id) {
	if (('' + year) == '') {
		year = prompt('Please enter the budget year');
	}
	if (year) {
		window.open('sf_drs_report.php?year=' + year + '&id=' + id);
	}

	return false;
}

function openDRSRG(year, id) {
	if (('' + year) == '') {
		year = prompt('Please enter the budget year');
	}
	if (year) {
		window.open('sf_drs_report.php?year=' + year + '&account_group=' + id);
	}

	return false;
}

function showMembers(id) {
	window.location = 'sf_product_groups.php?id=' + id;
}

function onLiteratureDownloadSubmit(submitButton) {
	var hasErrors = false;
	var reqFields = new Array('name', 'company', 'telephone', 'email');
	var d = document;
	var obj = errObj = null;

	try {
		for (var i = reqFields.length - 1; i >= 0 ; -- i) {
			obj = d.getElementById(reqFields[i]);
			errObj = d.getElementById(reqFields[i] + '_error');
			if (obj) {
				if (('' + obj.value).length <= 0) {
					errObj.style.display = 'block';
					hasErrors = true;
				} else {
					errObj.style.display = 'none';
				}
			} else {
				alert('couldnt find obj ' + reqFields[i]);
			}
		}
	
		d.getElementById('form_error_notice').style.display = (hasErrors) ? 'block' : 'none';
	} catch (e) {
		alert('Error: ' + e);
		hasErrors = true;
	}

	if (!hasErrors) {
		var form = $(submitButton).closest('form');
		
		__log('need to submit to ' + $(form).attr('action'));
		try {
			$.post(
				$(form).attr('action'),
				$(form).serialize(),
				function(data) {
					__log('got response: ' + data)
					window.open(data, null, 'status=no,menubar=no,toolbar=no,resizable=yes,height=600,width:800');
				}
			);
		
			close_fancybox();
		} catch (ex) {
			return false;
		}
		
		return false;
	} else {
		try {
			parent.$.fancybox.resize();
			__log('fancybox resized?');
		} catch (ex) {
			__log('error resizing fancybox: ' + ex);
		}
		return false;
	}
}

function close_fancybox(is_child) {
	var is_child = (undefined == is_child || is_child);
	try {
		$.fancybox.close();
	} catch (ex) {
		
	}
	
	try {
		__log('closing fancy box for window: ' + document.title);
		if (is_child) {
			// try closing from the parent (using this same function)
			close_fancybox(false);
		} else {
			$.fancybox.close();
		}
	} catch (ex) {
		__log('error closing fancy box from ' + (is_child ? 'CHILD' : 'PARENT') + ': ' + ex);
		// try closing from this frame/window?
		return (is_child)
			? close_fancybox(false)
			: false
		;
	}

	return true;
}
