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 = '';
var active_block = '';

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 ShowBlock (block_id)
{
	if (block_id && (block_obj = FindObject(block_id + '-div')) != null) {
		if ('' !== active_block && block_id + '-div' !== active_block) {
			hide(active_block);
		}
		block_obj.style.visibility == 'visible' ? hide(block_obj) : show(block_obj);
		active_block = block_id + '-div';
	}
	return false;
}

function Goto (url, target)
{
	if ('' != url) {
		if ('undefined' != typeof target && 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 GetFormElementValue (form_element)
{
	result = null;
	if ('undefined' != typeof form_element && 'undefined' != typeof form_element.type) {
		switch (form_element.type) {
			case 'select-one':
				result = form_element[form_element.selectedIndex].value;
				break;

			case 'input':
				break;
		}
	}
	return result;
}
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 CheckTime (direction, start_selector, end_selector, delta)
{
	direction = 'end' != direction ? 'start' : 'end';
	delta = parseInt(delta) == 0 ? 4 : parseInt(delta);
	if ('undefined' != typeof start_selector && 'select-one' == start_selector.type &&
		'undefined' != typeof end_selector && 'select-one' == end_selector.type)
	{
		start = parseInt(start_selector.value.replace(/:.+/, ''));
		end = parseInt(end_selector.value.replace(/:.+/, ''));
		if ('start' == direction) {
			if (start + delta > end) {
				end_selector.selectedIndex = start_selector.selectedIndex + delta;
			}
		}
		else {
			if (end - delta < start) {
				start_selector.selectedIndex = end_selector.selectedIndex - delta;
			}
		}
	}
	setTimeout('', 500);
}

function CheckAndSubmitPhone (value)
{
	if (false != CheckPhone(value)) {
		var url = '/email/direct_contact/?phone_number=' + value;
		oHttpRequest.open('GET', url, true);
		oHttpRequest.onreadystatechange = SubmitFeedback;
		oHttpRequest.send(null);
	}
}

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);
	}
}

function SubmitFeedback ()
{
	var result = {};
	if (4 == oHttpRequest.readyState) {
		if (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$/.test(oHttpRequest.responseText.replace(/\\./g, '@').replace(/'[^'\\\n\r]*'/g, ''))) {
			result = eval('(' + oHttpRequest.responseText + ');');
			switch (result['action']) {
				case 'direct_contact':
					if ('undefined' == typeof result.error || '' == result.error) {
						show('success_direct_contact');
						setTimeout('hide("success_direct_contact")', 3000);
					}
					else {
						show('error');
						setTimeout('hide("error")', 3000);
					}
					break;
					
				default:
					alert('3333333333333333');
					break;
			}
		}
		else {
			alert('Server wrong responce.');
		}
	}
}
