var resolutions = new Array(800, 1024, 1280);
month_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var gender_sections = new Object();
var collection = '';

function IncludeCss (domain, schema)
{
	var style = arguments[2] || 'site';
	for (var i = 0; i < resolutions.length; i++) {
		if (screen.width == resolutions[i] ||
			(screen.width > resolutions[i] && (resolutions.length == i + 1 || screen.width < resolutions[i + 1])) ||
			(screen.width < resolutions[i] && 0 > i - 1))
		{
			document.write('<link rel="stylesheet" type="text/css" href="http://' + domain + '/inc/' + schema + '/' + style + resolutions[i] + '.css"/>');
			break;
		}
	}
}

function ChangeLang (lang)
{
	document.location.href = document.location.href.replace(/\/([a-z]{2})\//, '/' + lang + '/');
}

function PopUp (name, width, height, resizable, scrolls, url)
{
	name = name || 'popup';
	width = width || 10;
	height = height || 10;
	resizable = resizable || false;
	scrolls = scrolls || false;
	popup = window.open('', name, 'width=' + width + ',height=' + height + ',resizable=' + (resizable ? '1' : '0') + ',scrollbars=' + (scrolls ? '1' : '0') + ',status=0,toolbar=0');
	if ('undefined' != typeof url && url != '') {
		popup.location.href = url;
	}
}

function ShowDialog (url, args, width, height)
{
	return showModalDialog(url, args, 'dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;help:no;scroll:auto;status:no');
}

function Goto (url, target)
{
	if (target != undefined && target == 'blank') {
		window.open(url, '');
	}
	else {
		document.location.href = url;
	}
}

function ShowImage (image_obj, lang)
{
	if ('object' == typeof image_obj && 'undefined' != typeof image_obj.src && null != (matches = /\/([0-9]+)\/?$/.exec(image_obj.src))) {
		PopUp('browse_image',30,30,false,false,'/browse/image/?uid=' + matches[1] + '&url_lang=' + lang);
	}
}

function CheckEmail (value)
{
	var result = false;
	if ('' != value) {
		var emailRegex1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var emailRegex2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; // valid
		if (!emailRegex1.test(value) && emailRegex2.test(value)) {// if syntax is valid
			result = true;
		}
	}
	return result;
}

function CheckPhone (value)
{
	var result = true;
	if ('' != value) {
		phoneRegex = /[^0-9\(\)\+\-\.\s]+/g;
		if (phoneRegex.test(value)) {
			result = false;
		}
	}
	return result;
}

function CheckAndSubmit (target_form)
{
	var error = '';
	var passwords = {};
	if ('undefined' == typeof target_form || 'undefined' == typeof target_form.elements) {
		alert('Wrong form.');
		return false;
	}
	else {
		if (target_form.submitted) {
			error += msg_submitted;
		}
		else {
			for (var i = 0; i < target_form.elements.length; i++) {
				var current = target_form.elements[i];
				if ('hidden' != current.type && false == current.disabled) {
					if ('' == current.value && 'yes' == current.required) {
						error += msg_empty.replace(/\[:field:\]/, current.caption);
					}
					switch (current.subtype) {
						case 'email':
							if ('' != current.value && false == CheckEmail(current.value)) {
								error += msg_wrong_email.replace(/\[:field:\]/, current.caption);
							}
							break;
						case 'phone':
							if ('' != current.value && false == CheckPhone(current.value)) {
								error += msg_wrong_phone.replace(/\[:field:\]/, current.caption);
							}
							break;
						case 'password':
							if ('undefined' == typeof passwords[current.name]) {
								passwords[current.name] = current.value;
							}
							else if ('' != current.value && current.value != passwords[current.name]) {
								error += msg_password_dmatch.replace(/\[:field:\]/, current.caption);
							}
							break;
					}
				}
			}
		}
		if ('' == error) {
			target_form.submitted = true;
			if (arguments.length > 1 && typeof arguments[1] == 'string') {
				PopUp(arguments[1],10,10);
			}
			target_form.submit();
			return true;
		}
		else {
			ShowError(target_form.name + '_error', error);
			return false;
		}
	}
}

function SetDate (day_selector, month_selector, year_selector, target)
{
	if ('undefined' != typeof day_selector && 'select-one' == day_selector.type &&
		'undefined' != typeof month_selector && 'select-one' == month_selector.type &&
		'undefined' != typeof year_selector && 'select-one' == year_selector.type &&
		'undefined' != typeof target && 'hidden' == target.type)
	{
		var day = day_selector.value;
		var month = month_selector.value;
		var year = parseInt(year_selector.value);
		var total_month_days = month == '02' && ((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) ? 29 : month_days[month - 1];
		if (parseInt(day) > total_month_days) {
			day_selector.options[total_month_days - 1].selected = true;
			day = total_month_days;
		}
		target.value = year + '-' + month + '-' + day;
	}
}

function SwitchDisabled (forma, switcher, fields, state)
{
	if (true != state) {
		state = false;
	}
	if ('undefined' != typeof switcher && ('checkbox' == switcher.type || 'radio' == switcher.type) && fields != '') {
		fields_names = fields.split('|');
		for (var i = 0; i < fields_names.length; i++) {
			if ('undefined' != typeof forma.elements[fields_names[i]]) {
				current = forma.elements[fields_names[i]];
				if ('undefined' != typeof current.length && 'undefined' != typeof switcher.index && 'undefined' != typeof current[switcher.index]) {
					current[switcher.index].disabled = switcher.checked ? !state : state;
				}
				else {
					current.disabled = switcher.checked ? !state : state;
				}
			}
		}
	}
}

function ShowError (obj, error)
{
	if (isW3C && null != (error_obj = FindObject(obj))) {
		hide(error_obj);
		error = error.split('\n');
		while (error_obj.childNodes.length > 0) {
			error_obj.removeChild(error_obj.firstChild);
		}
		for (var i = 0; i < error.length; i++) {
			error_obj.appendChild(document.createTextNode(error[i]));
			if (i + 1 < error.length) {
				error_obj.appendChild(document.createElement('br'));
			}
		}
		show(error_obj);
	}
	else {
		alert(error);
	}
}