﻿/*
// 2005-11-01
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/

function xForm( eFORM, sOptions ){
	this.eForm = eFORM;
	this.eForm.hControl = new Array();
	var asInput = [ 'input', 'textarea', 'select' ], i, ii, j, aeInput, sValue;
	for( i in asInput ){
		aeInput = this.eForm.getElementsByTagName( asInput[i] );
		for( ii = 0 ; ii < aeInput.length ; ii++ ){
			j = aeInput[ii].id;
			this.eForm.hControl[j] = aeInput[ii];
			sValue = xGet_input_value( location.search, aeInput[ii].name );
			if( sValue ){
				aeInput[ii].value = sValue;
			}
			get_accompanied_elements( this.eForm.hControl[j] );
			add_events( this.eForm.hControl[j] );
		}
	}

	this.eForm.onsubmit = x_Check_form;

	function add_events( eInput ){
		var sClass_name = eInput.className;
		if( sClass_name ){
			sClass_name = sClass_name.replace( /x_(min|max|default)_(\S*)/g, function( s, p1, p2 ){
				eInput[ "x_" + p1 ] = p2;
				return "";

		} );
			var asClass = sClass_name.split( /\s+/ );
			var bRequired = false;
			var bIntegerCheck = false;
			var bDateCheck = false;
			var bRequiredGroup = false;
			var aHandler = new Array();
			for( var i = 0, j ; i < asClass.length ; i++ ){
				switch( asClass[i] ){
					case "required":
					case "required-ok":
						bRequired = true;
						break;
					case "required-group":
						bRequiredGroup = true;
						break;
					case "x_email":
						aHandler[aHandler.length] = xFormat_email;
						if( !eInput.x_mask ){
							eInput.x_mask = new RegExp( ".+@.*\\.[A-Za-z]{2,4}" );
						}
						break;
					case "x_phone":
						aHandler[aHandler.length] = xFormat_phone;
						if( !eInput.x_mask ){
//							eInput.x_mask = new RegExp( "[\\d- ()+]{5,6}" );
//							eInput.x_mask = new RegExp( "^(\\+\\d{1,3}\\x20?)?(\\d{3,5}\\x20?|\\(\\d{3,5}\\)\\x20?)?\\d{1,3}\\-?\\d\\d\\-?\\d\\d(\\s[\\d\\s\(\)]*)?$" );
							eInput.x_mask = new RegExp( "^(\\+\\d{1,3}\\x20?)?(\\d{3,4}\\x20?|\\(\\d{3,4}\\)\\x20?)?\\d{1,3}\\-?\\d\\d\\-?\\d\\d(\\s[\\d\\s\(\)]*)?$" );
//							eInput.x_mask = new RegExp( "^(\\d{1,3}\\x20?)?(\\d{3,4}\\x20?|\\(\\d{3,4}\\)\\x20?)([\\d\\s\\(\\)-]{3,15})$" );
						}
						break;
					case "x_date":
						if( !eInput.eCalendar ){

							x_Make_calendar( eInput, true, true, true );
							function change_date(){
								var dtDate = new Date( eInput.eCalendar.childNodes[2].value, eInput.eCalendar.childNodes[1].value - 1, eInput.eCalendar.childNodes[0].value );
								var sDate = dtDate.getDate() + '.' + ( dtDate.getMonth() + 1 ) + '.' + dtDate.getFullYear();
								eInput.value = sDate == eInput.eCalendar.childNodes[0].value * 1 + '.' + eInput.eCalendar.childNodes[1].value * 1 + '.' + eInput.eCalendar.childNodes[2].value ? sDate : '';
								eInput.onkeyup();
							}
							for( var j = 0 ; j < eInput.eCalendar.childNodes.length ; j++ ){
								eInput.eCalendar.childNodes[j].onchange = change_date;
								eInput.eCalendar.childNodes[j].onblur = change_date;
								eInput.eCalendar.childNodes[j].onkeyup = change_date;
							}
							aHandler[aHandler.length] = xFormat_date;
							if( !eInput.x_mask ){
								eInput.x_mask = new RegExp( "^([12][0-9]|3[01]|0?[1-9])([\\.\\-/])(1[012]|0?[1-9])\\2(19\\d\\d|2[01]\\d\\d)$" );
							}
						}

						var min = $('#' + eInput.id).attr('min');
						var max = $('#' + eInput.id).attr('max');

						if(min || max){
							bDateCheck = true;
						}

						break;
					case "x_datetime":
						//aHandler[aHandler.length] = xFormat_datetime;
						if( !eInput.x_mask ){
							eInput.x_mask = new RegExp( "^([12][0-9]|3[01]|0?[1-9])([\\.\\-/])(1[012]|0?[1-9])\\2(1\\d\\d\\d|2[01]\\d\\d)\s+([01]?\\d|2[0-3]):[0-5]?\\d$" );
						}
						break;
					case "x_datemonth":
						//aHandler[aHandler.length] = xFormat_datemonth;
						/*if( !eInput.x_mask ){
							eInput.x_mask = new RegExp( "^(1[012]|0?[1-9])([\\.\\-/])(1\\d\\d\\d|2[01]\\d\\d)$" );
						}*/

						if( !eInput.eCalendar ){
							x_Make_calendar( eInput, true, true, false );
							function change_date_month(){
								var dtDate = new Date( eInput.eCalendar.childNodes[1].value, eInput.eCalendar.childNodes[0].value - 1, 1 );
								var sDate = ( dtDate.getMonth() + 1 ) + '.' + dtDate.getFullYear();
								eInput.value = sDate == eInput.eCalendar.childNodes[0].value * 1 + '.' + eInput.eCalendar.childNodes[1].value ? sDate : '';
								eInput.onkeyup();
							}
							for( var j = 0 ; j < eInput.eCalendar.childNodes.length ; j++ ){
								eInput.eCalendar.childNodes[j].onchange = change_date_month;
								eInput.eCalendar.childNodes[j].onblur = change_date_month;
								eInput.eCalendar.childNodes[j].onkeyup = change_date_month;
							}
							aHandler[aHandler.length] = xFormat_date;
							if( !eInput.x_mask ){
								eInput.x_mask = new RegExp( "^(1[012]|0?[1-9])([\\.\\-/])(1\\d\\d\\d|2[01]\\d\\d)$" );
							}
						}
						break;
					case "x_integer":
						aHandler.push(xFormat_integer);

						var min = $('#' + eInput.id).attr('min');
						var max = $('#' + eInput.id).attr('max');

						if(min || max){
							bIntegerCheck = true;
						}

						break;
					case "x_string": aHandler[aHandler.length] = xFormat_string; break;
					case "x_russtring": aHandler[aHandler.length] = xFormat_rusString; break;
					case "x_decimal": break;
					case "x_no_white_spaces": aHandler[aHandler.length] = xFormat_no_white_spaces; break;
					case "x_only_roman": aHandler[aHandler.length] = xFormat_only_roman; break;
				}
			}
			if( bRequired ){
				aHandler.push(xRequired);
			}
			if( bRequiredGroup ){
				alert('a');
				aHandler.push(xRequiredGroup);
			}
			if( bIntegerCheck ){
				aHandler.push(xCheck_integer);
			}
		}
		if( eInput.x_mask ){
			aHandler.push(xCheck_mask);
			eInput.onblur = function(){
				xCheck_mask( this );
			}
			eInput.onfocus = function(){
				cmnRemove_class( this, "(in)?valid" );
			}
		}
		if( bDateCheck ){
			aHandler.push(xCheck_date);
		}
		if( aHandler ){
			eInput.onkeyup = function(){
				for( var i in aHandler ){
					aHandler[i]( eInput );
				}
			}
			eInput.onclick = eInput.onkeyup;
			eInput.onchange = eInput.onkeyup;
			eInput.onblur = eInput.onkeyup;
			eInput.onkeyup();
		}
	}

	function get_accompanied_elements( eInput ){
		eInput.eRow = get_row_element( eInput );
		if( eInput.eRow ){
			var aeLabel = eInput.eRow.getElementsByTagName( "LABEL" );
			for( var i = 0; i < aeLabel.length ; i++ ){
				if( eInput.id == aeLabel[i].htmlFor ){
					eInput.eLabel = aeLabel[i];
				}
			}
		}
	}

	function get_row_element( eInput ){
		var eParent = eInput.parentNode;
		while( eParent ){
			if( cmnMatch_class( eParent, "row" ) ){
				return eParent;
			}
			eParent = eParent.parentNode;
		}
		return false;
	}

}

//	Validate entire form
function x_Check_form(){
	if(window.submitEnabled){
		return true;
	}else{
		var eWrong = false;
		for( var i in this.hControl ){

			// if it's enabled and had class = required OR required-ok + invalid = validate FAILED
			if( !this.hControl[i].disabled &&
				( cmnMatch_class( this.hControl[i], "required" ) ||
					( cmnMatch_class( this.hControl[i], "required-ok" ) && cmnMatch_class( this.hControl[i], "invalid" ) )
				)
			  )
			{
				if( !eWrong ){
					eWrong = this.hControl[i];
				}
				//if( this.hControl[i].eLabel ){
					var eDIV = document.createElement( "INS" );
					eDIV.className = "warning";
					if( cmnMatch_class( this.hControl[i], "required" ) ){
						if( this.hControl[i].x_required ){
							eDIV.innerHTML = this.hControl[i].x_required;
							this.hControl[i].x_required = false;
						}
					}else if( this.hControl[i].x_format ){
						eDIV.innerHTML = this.hControl[i].x_format;
						this.hControl[i].x_format = false;
					}
					if( eDIV.innerHTML ){
						//this.hControl[i].parentNode.insertBefore( eDIV, this.hControl[i].eLabel );
						this.hControl[i].parentNode.appendChild( document.createTextNode( ' ' ) );
						this.hControl[i].parentNode.appendChild( eDIV );
					}
				//}
			}
		}


		if( eWrong ){
			eWrong.focus();
			return false;
		} else {
			eWrong = x_Check_form_local();
			if( eWrong ){
				eWrong.focus();
				return false;
			}
		}
		return true;
	}
}

function x_Check_form_local() {
	return;
}

function xRequired( eInput ){
	var aeInput = eInput.aeFrom ? eInput.aeFrom : new Array( eInput );
	var bRequired = eInput.disabled ? false : true;
	for( var i = 0 ; i < aeInput.length ; i++ ){
		if( !aeInput[i].disabled && ((aeInput[i].getAttribute( 'type' ) != 'checkbox' && aeInput[i].getAttribute( 'type' ) != 'radio'  && aeInput[i].value && aeInput[i].value.trim() != '') || aeInput[i].checked ) ){
			bRequired = false;
			break;
		}
	}
	for( i = 0 ; i < aeInput.length ; i++ ){
		if( bRequired ){
			cmnSet_class( aeInput[i], "required", "required-ok" );
			cmnSet_class( aeInput[i].eRow, "required", "required-ok" );
			cmnSet_class( aeInput[i].eLabel, "required", "required-ok" );
		}else{
			cmnSet_class( aeInput[i], "required-ok", "required" );
			cmnSet_class( aeInput[i].eRow, "required-ok", "required" );
			cmnSet_class( aeInput[i].eLabel, "required-ok", "required" );
		}
	}
}

function xRequiredGroup( eInput ){
	var aeInput = eInput.aeFrom ? eInput.aeFrom : new Array( eInput );
	var bRequired = eInput.disabled ? false : true;
	for( var i = 0 ; i < aeInput.length ; i++ ){
		if( !aeInput[i].disabled && ( aeInput[i].getAttribute( 'type' ) != 'checkbox' && aeInput[i].getAttribute( 'type' ) != 'radio' && aeInput[i].value || aeInput[i].checked ) ){
			bRequired = false;
			break;
		}
	}
	for( i = 0 ; i < aeInput.length ; i++ ){
		if( bRequired ){
			cmnSet_class( aeInput[i], "required", "required-ok" );
			cmnSet_class( aeInput[i].eRow, "required", "required-ok" );
			cmnSet_class( aeInput[i].eLabel, "required", "required-ok" );
		}else{
			cmnSet_class( aeInput[i], "required-ok", "required" );
			cmnSet_class( aeInput[i].eRow, "required-ok", "required" );
			cmnSet_class( aeInput[i].eLabel, "required-ok", "required" );
		}
	}
}

function xFormat_email( eInput ){
	var sValue = eInput.value.replace( /[^\w\-\d\.@]/g, "" );
	sValue = sValue.replace( /^[^a-z\d]/i, "" );
	sValue = sValue.replace( /(@.*)@/g, "$1" );
	sValue = sValue.replace( /@\./, "@" );
	sValue = sValue.replace( /[^\w\d\-\.@]/g, "" );
	if( eInput.value != sValue ) eInput.value = sValue;
}

function xFormat_phone( eInput ){
	var sValue = eInput.value.replace( /[^\+\d\(\)\x20\-]/g, "" );
	if( eInput.value != sValue ) eInput.value = sValue;
}

function xCheck_mask( eInput ){
	if( eInput.value.match( eInput.x_mask ) ){
		cmnSet_class( eInput, "valid", "invalid" );
		cmnSet_class( eInput.eRow, "valid", "invalid" );
	}else{
		cmnSet_class( eInput, "invalid", "valid" );
		cmnSet_class( eInput.eRow, "invalid", "valid" );
	}
}

function xCheck_integer( eInput ){
	var eInputId = eInput.id;
	var min = parseInt($('#' + eInput.id).attr('min'));
	var max = parseInt($('#' + eInput.id).attr('max'));
	var value = parseInt($('#' + eInput.id).attr('value').replace(/\s/g,''),10);

	if( value < min || value > max ){
		cmnSet_class( eInput, "invalid", "valid" );
		cmnSet_class( eInput.eRow, "invalid", "valid" );
		cmnSet_class( eInput.eLabel, "required", "required-ok" );
	}else{
		cmnSet_class( eInput, "valid", "invalid" );
		cmnSet_class( eInput.eRow, "valid", "invalid" );
	}
}

function xCheck_date( eInput ){
	var eInputId = eInput.id;
	var min = $('#' + eInput.id).attr('min');
	var max = $('#' + eInput.id).attr('max');

	var dMin = dateFromString(min);
	var dMax = dateFromString(max);
	var dtDate = new Date( eInput.eCalendar.childNodes[2].value, eInput.eCalendar.childNodes[1].value - 1, eInput.eCalendar.childNodes[0].value );

	if((dMin != '' && dtDate < dMin) || (dMax != '' && dtDate > dMax)){
		cmnSet_class( eInput, "invalid", "valid" );
		cmnSet_class( eInput.eRow, "invalid", "valid" );
		cmnSet_class( eInput.eLabel, "required", "required-ok" );
	}else{
		cmnSet_class( eInput, "valid", "invalid" );
		cmnSet_class( eInput.eRow, "valid", "invalid" );
	}
}

function xFormat_date( eInput ){
	var sValue = eInput.value.replace( /[^\d\.\-\/]/g, "" );
	if( eInput.value != sValue ) eInput.value = sValue;
}

function xFormat_string( eInput ){
	//var sValue = eInput.value.replace( /(.)\-/g, "$1" );
	var sValue = eInput.value.replace( /[^a-zA-Zа-яА-Я-\ ]+/g, "" );

	if( eInput.value != sValue ) eInput.value = sValue;
}

function xFormat_rusString ( eInput ){
	var sValue = eInput.value.replace( /[^а-яА-Я-\ ]+/g, "" );

	if( eInput.value != sValue ) eInput.value = sValue;
}

function xFormat_integer( eInput ){
	var sValue = eInput.value.replace( /(.)\-/g, "$1" );
	sValue = sValue.replace( /[^\d\-]+/g, "" );
	if( eInput.x_min > 0 ){
		sValue = sValue.replace( /^[\-0]{1,2}/, "" )
	}else if( eInput.x_min == 0 ){
		sValue = sValue.replace( /^\-/, "" )
	}else if( eInput.x_max < 0 ){
		sValue = sValue.replace( /^(\-)/, "-$1" );
	}else if( eInput.x_max == 0 ){
		sValue = sValue.replace( /^[^\-0]/, "0" )
	}
	if( eInput.value != sValue ) eInput.value = sValue;
}

function xFormat_no_white_spaces( eInput ){
	var sValue = eInput.value.replace( /\s*/g, "" );
	if( eInput.value != sValue ) eInput.value = sValue;
}

function xFormat_only_roman( eInput ){
	var sValue = eInput.value.replace( /[^a-z]*/gi, "" );
	if( eInput.value != sValue ) eInput.value = sValue;
}

function x_Make_dependence( sID, sFromID, aValue, bInverse ){
	var eFrom = document.getElementById( sFromID + "_i0" );
	eFrom = eFrom ? eFrom : document.getElementById( sFromID );
	var eThis = document.getElementById( sID );
	if( eThis && eFrom ){
		if( eFrom.tagName.toUpperCase() == 'SELECT' ){
			x_Make_select_dependence( eThis, eFrom, aValue, bInverse );
		}else if( eFrom.getAttribute( "type" ) == "radio" ){
			var i = 0, j, bDepended;
			while( eFrom ){
				bDepended = false;
				for( j = 0 ; j < aValue.length ; j++ ){
					if( eFrom.value == aValue[j] ){
						bDepended = true;
						break;
					}
				}
				x_Make_radio_dependence( eThis, eFrom, bDepended, bInverse );
				i++;
				eFrom = document.getElementById( sFromID + "_i" + i );
			}
		}else if( eFrom.getAttribute( "type" ) == "checkbox" ){
			var i = 0, j;
			while( eFrom ){
				for( j = 0 ; j < aValue.length ; j++ ){
					if( eFrom.value == aValue[j] ){
						x_Make_checkbox_dependence( eThis, eFrom, bInverse );
						break;
					}
				}
				i++;
				eFrom = document.getElementById( sFromID + "_i" + i );
			}
		}else{
			if(cmnMatch_class( eFrom, "x_date" )){
				x_Make_date_dependence( eThis, eFrom, bInverse );
			}else{
				x_Make_text_dependence( eThis, eFrom, bInverse );
			}
		}
	}
}

function x_Make_date_dependence( eThis, eFrom, bInverse ){
	var id = eFrom.getAttribute('id');
	$('input#' + id).prev().find('input,select').each(function(index) {
		$(this).blur(function() {
			var thisId = eThis.getAttribute('id');
			$('input#' + thisId).prev().find('input:first').change();
		});
		$(this).keyup(function() {
			var thisId = eThis.getAttribute('id');
			$('input#' + thisId).prev().find('input:first').change();
		});
	});
}

function x_Make_text_dependence( eThis, eFrom, bInverse ){
	if( !eThis.aFrom ){
		eThis.aFrom = new Array();
		eThis.on_text_depended_changed = x_Text_depended_changed;
	}
	eThis.aFrom[eThis.aFrom.length] = { eFrom: eFrom, bInverse: bInverse };
	cmnAdd_events( eFrom, ["blur", "keyup"], function(){ eThis.on_text_depended_changed(); } );
	eThis.on_text_depended_changed();
}

function x_Text_depended_changed(){
	this.disabled = true;
	for( var i = 0, bDisabled ; i < this.aFrom.length ; i++ ){
		bDisabled = this.aFrom[i].eFrom.value ? false : true;
		bDisabled = this.aFrom[i].bInverse ? !bDisabled : bDisabled;
		if( !bDisabled ){ this.disabled = false; }
	}
	if( this.eLabel ) this.eLabel.setAttribute( "disabled", this.disabled );
}

function x_Make_select_dependence( eThis, eFrom, aValue, bInverse ){
	function change_depended(){
		eThis.disabled = ( eFrom.options[eFrom.selectedIndex].value == aValue[0] ? false : true );
		eThis.disabled = bInverse ? !eThis.disabled : eThis.disabled;
		if( eThis.eLabel ){ eThis.eLabel.setAttribute( "disabled", eThis.disabled ); }
		if( !eThis.disabled) { try{eThis.focus();}catch(e){} }
		if( eThis.onkeyup ){ eThis.onkeyup(); }
		if( eThis.onclick ){ eThis.onclick(); }
	}
	cmnAdd_event( eFrom, "change", change_depended );
	change_depended();
}

function x_Make_radio_dependence( eThis, eFrom, bDepended, bInverse ){
	function change_depended(){
		eThis.disabled = ( bDepended && eFrom.checked ) || ( !bDepended && !eFrom.checked ) ? false : true;
		eThis.disabled = bInverse ? !eThis.disabled : eThis.disabled;
		if( eThis.eLabel ) eThis.eLabel.setAttribute( "disabled", eThis.disabled );
		if( eThis.onkeyup ){ eThis.onkeyup(); }
		if( eThis.onclick ){ eThis.onclick(); }
	}
	cmnAdd_event( eFrom, "click", change_depended );
	if( eFrom.checked ){ change_depended(); }
}

function x_Make_checkbox_dependence( eThis, eFrom, bInverse ){
	function change_depended(){
		eThis.disabled = eFrom.checked ? false : true;
		eThis.disabled = bInverse ? !eThis.disabled : eThis.disabled;
		if( eThis.eLabel ) eThis.eLabel.setAttribute( "disabled", eThis.disabled );
		if( eThis.onkeyup ){ eThis.onkeyup(); }
		if( eThis.onclick ){ eThis.onclick(); }
	}
	cmnAdd_event( eFrom, "click", change_depended );
	change_depended();
}

function x_Make_depended_select( sID, sFromID, aValue ){
	var eFrom = document.getElementById( sFromID + '_i0');
	eFrom = eFrom ? eFrom : document.getElementById( sFromID );
	if (eFrom.tagName == 'META') {
		eFrom = document.getElementsByName('city')[1];
	}
	var eThis = document.getElementById( sID );
	if( eThis && eFrom ){
		eThis.aOption = new Array();
		for( var i = 0 ; i < eThis.options.length ; i++ ){
			eThis.aOption[i] = new Array();
			eThis.aOption[i].parent_value = aValue[i];
			eThis.aOption[i].value = eThis.options[i].value;
			eThis.aOption[i].text = eThis.options[i].text;
		}
		if( eFrom.tagName.toUpperCase() == 'SELECT' ){
			var change_depended = function(){
				var sValue = eFrom.options[eFrom.selectedIndex].value;
				eThis.options.length = 0;
				for( var i = 0, eTmp ; i < eThis.aOption.length ; i++ ){
					if( !eThis.aOption[i].parent_value || eThis.aOption[i].parent_value == sValue ){
						eTmp = new Option( eThis.aOption[i].text, eThis.aOption[i].value );
						eThis.options[eThis.options.length] = eTmp;
					}
				}
			}
			cmnAdd_event( eFrom, "change", change_depended );
			change_depended();
		}else{
			// for radio
			var change_depended = function(){
				var sValue = $('input[name="' + sFromID + '"]:checked').val();
				eThis.options.length = 0;
				for( var i = 0, eTmp ; i < eThis.aOption.length ; i++ ){
					if( !eThis.aOption[i].parent_value || eThis.aOption[i].parent_value == sValue ){
						eTmp = new Option( eThis.aOption[i].text, eThis.aOption[i].value );
						eThis.options[eThis.options.length] = eTmp;
					}
				}
			}
			$('input[name="' + sFromID + '"]').click(function(){
				change_depended();
			});
			//cmnAdd_event( eFrom, "click", change_depended );
			change_depended();
		}
	}
}

function x_Make_warning( asID, sType, sHTML, oOptions ){
	if( sType && sHTML ){
		for( var i = 0, eThis, eDepended ; i < asID.length ; i ++ ){
			eThis = document.getElementById( asID[i] );
			if( eThis ){
				eThis.oOptions = oOptions;
				if( !i || eThis.getAttribute( 'type' ) == 'checkbox' || eThis.getAttribute( 'type' ) == 'radio' ){ eThis['x_' + sType] = sHTML; }
				if( sType == 'required' ){
					eThis['aeFrom'] = new Array();
					for( var j = 0 ; j < asID.length ; j++ ){
						eDepended = document.getElementById( asID[j] );
						if( eDepended ){
							eThis['aeFrom'][eThis['aeFrom'].length] = eDepended;
						}
					}
				}
			}
		}
	}
}

function x_Make_mask( sID, sMask ){
	if( sID && sMask ){
		var eThis = document.getElementById( sID );
		if( eThis ){
			eThis.x_mask = new RegExp( sMask );
		}
	}
}

var x_hL = new Array();
x_hL['B8']='ё';x_hL['E9']='й';x_hL['F6']='ц';x_hL['F3']='у';x_hL['EA']='к';x_hL['E5']='е';x_hL['ED']='н';x_hL['E3']='г';x_hL['F8']='ш';x_hL['F9']='щ';x_hL['E7']='з';x_hL['F5']='х';x_hL['FA']='ъ';x_hL['F4']='ф';x_hL['FB']='ы';x_hL['E2']='в';x_hL['E0']='а';x_hL['EF']='п';x_hL['F0']='р';x_hL['EE']='о';x_hL['EB']='л';x_hL['E4']='д';x_hL['E6']='ж';x_hL['FD']='э';x_hL['FF']='я';x_hL['F7']='ч';x_hL['F1']='с';x_hL['EC']='м';x_hL['E8']='и';x_hL['F2']='т';x_hL['FC']='ь';x_hL['E1']='б';x_hL['FE']='ю';x_hL['A8']='Ё';x_hL['C9']='Й';x_hL['D6']='Ц';x_hL['D3']='У';x_hL['CA']='К';x_hL['C5']='Е';x_hL['CD']='Н';x_hL['C3']='Г';x_hL['D8']='Ш';x_hL['D9']='Щ';x_hL['C7']='З';x_hL['D5']='Х';x_hL['DA']='Ъ';x_hL['D4']='Ф';x_hL['DB']='Ы';x_hL['C2']='В';x_hL['C0']='А';x_hL['CF']='П';x_hL['D0']='Р';x_hL['CE']='О';x_hL['CB']='Л';x_hL['C4']='Д';x_hL['C6']='Ж';x_hL['DD']='Э';x_hL['DF']='Я';x_hL['D7']='Ч';x_hL['D1']='С';x_hL['CC']='М';x_hL['C8']='И';x_hL['D2']='Т';x_hL['DC']='Ь';x_hL['C1']='Б';x_hL['DE']='Ю';
var x_hA = new Array();

function xGet_input_value( sURL, sName ){
	var rPattern = new RegExp( '.*[\\?\\&]' + sName + '=([^\\&]+).*' );
	if( sURL.match( rPattern ) ){
		sURL = sURL.replace( rPattern, '$1' ).replace( /\+/g, '%20' );
		if( sURL.indexOf( '%' ) > -1 ){
			for( var i in x_hL ){
				sURL = sURL.replace( new RegExp( '%' + i, 'gi' ), x_hL[i] );
			}
			sURL = unescape( sURL );
		}
		return sURL.replace( /&nbsp;/g, ' ' );
	}else{
		return false;
	}
}

var x_hLabels = new Array();
x_hLabels.ru = {
	month_1:	{ s: 'Январь', g: 'января' },
	month_2:	{ s: 'Февраль', g: 'февраля' },
	month_3:	{ s: 'Март', g: 'марта' },
	month_4:	{ s: 'Апрель', g: 'апреля' },
	month_5:	{ s: 'Май', g: 'мая' },
	month_6:	{ s: 'Июнь', g: 'июня' },
	month_7:	{ s: 'Июль', g: 'июля' },
	month_8:	{ s: 'Август', g: 'августа' },
	month_9:	{ s: 'Сентябрь', g: 'сентября' },
	month_10:	{ s: 'Октябрь', g: 'октября' },
	month_11:	{ s: 'Ноябрь', g: 'ноября' },
	month_12:	{ s: 'Декабрь', g: 'декабря' }
};
x_hLabels.ua = {
	month_1:	{ s: 'Сiчень', g: 'сiчня' },
	month_2:	{ s: 'Лютий', g: 'лютого' },
	month_3:	{ s: 'Березень', g: 'березня' },
	month_4:	{ s: 'Квiтень', g: 'квiтня' },
	month_5:	{ s: 'Травень', g: 'травня' },
	month_6:	{ s: 'Червень', g: 'червня' },
	month_7:	{ s: 'Липень', g: 'липня' },
	month_8:	{ s: 'Серпень', g: 'серпня' },
	month_9:	{ s: 'Вересень', g: 'вересня' },
	month_10:	{ s: 'Жовтень', g: 'жовтня' },
	month_11:	{ s: 'Листопад', g: 'листопада' },
	month_12:	{ s: 'Грудень', g: 'грудня' }
};
x_hLabels.en = {
	month_1:	{ s: 'January', g: 'January' },
	month_2:	{ s: 'February', g: 'February' },
	month_3:	{ s: 'March', g: 'March' },
	month_4:	{ s: 'April', g: 'April' },
	month_5:	{ s: 'May', g: 'May' },
	month_6:	{ s: 'June', g: 'June' },
	month_7:	{ s: 'July', g: 'July' },
	month_8:	{ s: 'August', g: 'August' },
	month_9:	{ s: 'September', g: 'September' },
	month_10:	{ s: 'October', g: 'October' },
	month_11:	{ s: 'November', g: 'November' },
	month_12:	{ s: 'December', g: 'December' }
};

function x_Make_calendar( eInput, bYear, bMonth, bDay ){
	var seDay="", seMonth="", seYear="";
	var selectedDate = eInput.value.split(".");
	if (selectedDate && selectedDate.length == 3){
		seDay = selectedDate[0];
		seMonth = selectedDate[1];
		seYear = selectedDate[2];
	}

	eInput.eCalendar = document.createElement( 'span' );
	var sHTML = '';
	if( bDay ){ sHTML = '<input size="2" value="' + seDay + '" id="x_sDay" maxlength="2" />'; }
	if( bMonth ){
		if( !cmn_oInformation.sLanguage ){ cmnInit_Information(); }
		sHTML += '<select id="x_sMonth">';
		var sCase = bDay ? 'g' : 's';
		for( var i = 1 ; i < 13 ; i++ ){
			selectedS = (i == seMonth)?'selected':"";
			sHTML += '<option value="' + i + '" '+ selectedS +'>' + x_hLabels[cmn_oInformation.sLanguage]['month_' + i][sCase] + '</option>';
		}
		sHTML += '</select>';
	}
	if( bYear ){ sHTML += '<input size="4" value="' + seYear + '" id="x_sYear" maxlength="4" />'; }
	eInput.eCalendar.innerHTML += sHTML;
	eInput.parentNode.insertBefore( eInput.eCalendar, eInput );
	eInput.style.visibility = 'hidden';
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

//	creates date object from string like '1990-12-31' or 'now' or '#id'
function dateFromString( sqlString ){
	if(sqlString){
		var dd = sqlString.split('-');
		if(dd.length == 3){
			return(new Date(dd[0], (dd[1]-1), dd[2]));
		}else{
			if(sqlString == 'now'){
				return(new Date());
			}else if (sqlString != '' && sqlString.substring(0,1) == '#'){
				var date = $(sqlString).val();
				dd = date.split('.');
				if(dd.length == 3){
					return(new Date(dd[2], (dd[1]-1), dd[0]));
				}else{
					return '';
				}
			}else{
				return '';
			}
		}
	}else{
		return '';
	}
}

/* Star selector */

$(document).ready(function(){
	toggle_star_selector();
});

function toggle_star_selector(){
	if($('form.x_forms div.star-bg')){
		$('form.x_forms div.star-bg').each(function(index) {
			var id = $(this).attr('id');

			$(this).find('img').mouseover(function() {
				show_stars($(this));
			});
			$(this).find('img').click(function() {
				set_stars($(this));
			});
			$(this).mouseout(function() {
				restore_stars($(this));
			});
		});
		$('form.x_forms p.expandable a').click(function() {
			expand_stars_block($(this));
			return false;
		});
	}
}

function show_stars( img )
{
	var className = $(img).attr('class');
	var position = className.substring(5);	//	number of star in group

	$(img).parent().find('img').each(function(index) {
		if(index < position){
//			$(this).attr('src', '/f/1/global/i-star-med-a.gif');
			$(this).attr('src', '/f/1/i/i-star-'+ (index + 1) +'.gif');
		}else{
			$(this).attr('src', '/f/1/global/d-t.gif');
		}
	});
}

function set_stars( img )
{
	var className = $(img).attr('class');
	var position = className.substring(5);	//	number of star in group
	var id = $(img).parent().attr('id').substring(5);
	$('#'+id).val(position);
}

//	show currently checked amount of stars
function restore_stars( div )
{
	var id = $(div).attr('id').substring(5);
	var stars = $('#'+id).val();

	$(div).find('img').each(function(index) {
		if(index < stars){
			$(this).attr('src', '/f/1/i/i-star-'+ (index + 1) +'.gif');
//			$(this).attr('src', '/f/1/global/i-star-med-a.gif');
		}else{
			$(this).attr('src', '/f/1/global/d-t.gif');
		}
	});
}

function expand_stars_block( link )
{
	//	We search for tr.hidden in the starselect table and insert our custom field before
	$(link).parent().parent().parent().parent().find('tr.hidden').each(function(index) {
		var prefix = $(this).parent().parent().attr('id');

		var cells = '';
		var customRows = $(this).find('input[name="'+ prefix +'-custom-rows"]').val();

		if(!customRows)
			customRows = 0;

		//	We check if previous customRow table was rated properly
		var prevCheckFailed = false;
		$(this).find('td[@class!="hidden"]').each(function(index) {
			var id = prefix + '-' +$(this).attr('class') + customRows;
			if(customRows > 0){
				var prevId = prefix + '-' +$(this).attr('class') + (customRows -1);
				if($('#' + prevId).val() == 0 || $('#' + prevId).val() == ''){
					prevCheckFailed = true;
				}
			}

			cells += '<td><input type="hidden" value="" name="'+ id +'" id="'+ id +'" /><div class="star-bg" id="star-'+ id +'"><img src="/f/1/global/d-t.gif" width="16" height="11" alt="" class="star-1" /><img src="/f/1/global/d-t.gif" width="16" height="11" alt="" class="star-2" /><img src="/f/1/global/d-t.gif" width="16" height="11" alt="" class="star-3" /><img src="/f/1/global/d-t.gif" width="16" height="11" alt="" class="star-4" /><img src="/f/1/global/d-t.gif" width="16" height="11" alt="" class="star-5" /></div></td>';
		});

		if(customRows == 0){
			prevCheckFailed = false;
		}
		if(!prevCheckFailed){
			$(this).before('<tr id="custom-row-'+ prefix + '-' + customRows +'"><input type="hidden" name="custom-row-'+ prefix + '-' + customRows +'" value="" /><td><a href="" class="pseudo-href" onclick="toggle_change_stars_label(\''+prefix+'\', '+ customRows +'); return false;"></a></td>'+ cells +'</tr>');
			// bind events
			bind_stars_custom_row_events(prefix, customRows);
			toggle_change_stars_label( prefix, customRows );

			customRows++;
			$(this).find('input[name="'+ prefix +'-custom-rows"]').val(customRows);
		}else{
			toggle_change_stars_label(prefix, (customRows-1));
		}
	});
	$(link).parent().parent().parent().hide();
}

function bind_stars_custom_row_events( prefix, rowId )
{
	$('tr#custom-row-' + prefix + '-' + rowId + ' div.star-bg').each(function(index) {
		$(this).find('img').mouseover(function() {
			show_stars($(this));
		});
		$(this).find('img').click(function() {
			set_stars($(this));
		});
		$(this).mouseout(function() {
			restore_stars($(this));
		});
	});
}

function toggle_change_stars_label( prefix, rowId )
{
	var id = 'custom-row-' + prefix + '-' + rowId;

	$('tr#' + id + ' td:first').each(function(index) {
		var title = $(this).text();
		$(this).html('<input type="text" value="'+ title +'" onblur = "change_stars_label(\''+prefix+'\', '+rowId+', this.value)" />');
		$(this).find('input').focus();
	});
}

function change_stars_label( prefix, rowId, value )
{
	var id = 'custom-row-' + prefix + '-' + rowId;
	if(value == ''){value = 'другое'}
	//	Probably delete?

	$('tr#' + id + ' td:first').each(function(index) {
		var title = $(this).text();
		$(this).html('<a href="" class="pseudo-href" onclick="toggle_change_stars_label(\''+prefix+'\', '+ rowId +'); return false;">'+ value +'</a>');
	});
	$('tr#' +id + ' input[@type="hidden"]:first').val(value);
}

function checkFilesExt(fieldName, extensions)
{
	field = $('input[name=' + fieldName + ']');
	valueLength = field.val().toLowerCase().length;
	lengthToTrim = valueLength - 4;
	fieldValue = field.val().toLowerCase();
	fExtension = fieldValue.substr(lengthToTrim)

	if(extensions.match(fExtension)) {
		$('#fError').slideUp(100);
		field.parents('dl').find('dt label').removeClass('required');
	} else {
		$('#fError').slideDown(100);
		field.parents('dl').find('dt label').removeClass('required').addClass('required');
	}
}

/* /Star selector */

