﻿/*
// 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];
			if( !aeInput[ii].type.match( /^(radio|checkbox)$/ ) ){
				sValue = xGet_input_value( location.search, aeInput[ii].name );
				if( sValue ){
					aeInput[ii].value = sValue;
				}
			}
			x_Get_accompanied_elements( this.eForm.hControl[j] );
			x_Add_events( this.eForm.hControl[j] );
		}
	}

	this.eForm.onsubmit = x_Check_form;

}
function x_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 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-Z0-9][a-zA-Z0-9\\.\\-\\_\\~]*\\@[a-zA-Z0-9\\.\\-\\_]+\\.[a-zA-Z]{2,4}(\\?[a-zA-Z0-9][^\\x20\\t]*)?" );
					}
					break;
				case "x_phone":
					aHandler[aHandler.length] = xFormat_phone;
					if( !eInput.x_mask ){
						eInput.x_mask = new RegExp( "^((|,\\s)(\\+\\d{1,3}\\x20?)?(\\d{3,4}\\x20?|\\(\\d{3,4}\\)\\x20?)?\\d{2,3}\\-?\\d\\d\\-?\\d\\d(\\s[\\d\\s\(\)]*)?)*$" );
					}
					break;
				case "x_custom":
					var fn = 'xHandle_' + (eInput.oOptions && eInput.oOptions.type ? eInput.oOptions.type : '');
					if (typeof window[fn] == 'function') {
						aHandler[aHandler.length] = window[fn];
					}
					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(1\\d\\d\\d|2[01]\\d\\d)$" );
							}
						}
						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[aHandler.length] = xFormat_integer; break;
				case "x_decimal": aHandler[aHandler.length] = xFormat_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[aHandler.length] = xRequired;
		}
		if( bRequiredGroup ){				
			aHandler[aHandler.length] = xRequiredGroup;
		}
	}
	if( eInput.x_mask ){
		aHandler[aHandler.length] = xCheck_mask;
		eInput.onblur = function(){
			xCheck_mask( this );
		}
		eInput.onfocus = function(){
			Common.Class.remove( this, "(in)?valid" );
		}
	}
	if( aHandler && aHandler.length > 0 ){					
		eInput.onkeyup = function(){
			if (cmnMatch_class(eInput, 'skip_init_validation')) {
				cmnRemove_class(eInput, 'skip_init_validation');
				var ins = eInput.parentNode.getElementsByTagName('INS');
				if (ins && ins.length) {
					ins[0].parentNode.removeChild(ins[0]);
				} 
			}
				
			for( var i in aHandler ){
				aHandler[i]( eInput );
			}
		}
		eInput.onclick = eInput.onkeyup;
		eInput.onchange = eInput.onkeyup;
		eInput.onblur = eInput.onkeyup;
		
		if (!cmnMatch_class(eInput, 'skip_init_validation')) {
			eInput.onkeyup();
		}
	}
}

function x_Get_accompanied_elements( eInput ){
	eInput.eRow = x_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 x_Get_row_element( eInput ){
	var eParent = eInput.parentNode;
	while( eParent ){			
		if( Common.Class.match( eParent, "row" ) ){
			return eParent;
		}
		eParent = eParent.parentNode;
	}
	return false;
}
function x_Check_form(){
	var eWrong = false;
	var eType = null;
	
	for( var i in this.hControl ){
		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 eDIVtmp = this.hControl[i].parentNode.getElementsByTagName("INS");
				var isNew = true;
				
				if (eDIVtmp && eDIVtmp.length) {
					eDIV = eDIVtmp[0];
					isNew = false;
				} else {
					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;
						if( !eType ){ eType = 'required'; }
					}
				} else if( this.hControl[i].x_format ) {
					eDIV.innerHTML = this.hControl[i].x_format;
					this.hControl[i].x_format = false;
					
					if( !eType ){ eType = 'format'; }
				}
				
				if( eDIV.innerHTML && isNew ){
					//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 ){
		xFocusInView(eWrong);
		$(this).trigger('validation', {'iserror': true, 'field': eWrong, 'type': eType || 'required'});
		return false;
	} else {
		$(this).trigger('validation', {'iserror': false, 'field': null, 'type': null});
	}
	
	return true;
}

function xFocusInView(node) {
	if (!node || node.nodeType != 1) return;
	
	var target_y = $(node).offset().top;
	var target_min = $(window).scrollTop();
	var target_max = $(window).height() + target_min;
	
	if (target_y >= target_min && target_y <= target_max) {
	    node.focus();
	}
}

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].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 xRequiredGroup( eInput ){
	alert('cool');
	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].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 xFormat_date( eInput ){
	var sValue = eInput.value.replace( /[^\d\.\-\/]/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_decimal( eInput ){
	var sValue = eInput.value.replace( /,/g, "." );
	sValue = sValue.replace(/[^0-9.]/g, "");
	var lastComma = sValue && sValue.indexOf('.') == sValue.lastIndexOf('.') && sValue.indexOf('.') == (sValue.length - 1);
	sValue = parseFloat(sValue) + '';
	if(sValue == 'NaN'){
		sValue = '';	
	}
	if(lastComma) {
		sValue += ',';
	} else {
		sValue = sValue.replace(/\./, ',');
	}
	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( !eThis.aDependendFrom ){
			eThis.aDependendFrom = new Array();
		}
		eThis.aDependendFrom[eThis.aDependendFrom.length] = { 
			eFrom: eFrom, 
			sID: sID, 
			sFromID: sFromID, 
			aValue: aValue,
			bInverse: bInverse 
		};
	
		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++;
				
				sBaseName = sFromID.match(/^(.+)(_i\d+(_\d+))$/);
				if(sBaseName){
					eFrom = document.getElementById( sBaseName[1] + "_i" + i + (sBaseName[3] ? sBaseName[3] : '' ));
				} else {
					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++;
				sBaseName = sFromID.match(/^(.+)(_i\d+(_\d+))$/);
				if(sBaseName){
					eFrom = document.getElementById( sBaseName[1] + "_i" + i + (sBaseName[3] ? sBaseName[3] : '' ));
				} else {
					eFrom = document.getElementById(sFromID + "_i" + i);
				}
			}
		}else{
			x_Make_text_dependence( eThis, eFrom, bInverse );
		}

	}
}

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_Apply_disabled (eThis) {
	var p = eThis.parentNode.parentNode;
	if (p) {
		if (Common.Class.match(p, 'row')) {
			if (eThis.disabled) {
				Common.Class.add(p, 'disabled');
			} else {
				Common.Class.remove(p, '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;
		x_Apply_disabled(eThis);
		if( eThis.eLabel ) eThis.eLabel.setAttribute( "disabled", eThis.disabled );
		if( !eThis.disabled ) eThis.focus();
		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;
		x_Apply_disabled(eThis);
		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;
		x_Apply_disabled(eThis);
		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 );
	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' ){
			function change_depended(){
				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
		}
	}
}

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['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' );
		sURL = decodeURIComponent(sURL);
		
		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 ){
	eInput.eCalendar = document.createElement( 'span' );
	
	var oDate = null;
	
	if(eInput.value) {	
		oDate = new Date(eInput.value, 0, 1);
	}		
	
	var sHTML = '';
	if( bDay ){ sHTML = '<input size="2" style="margin-right: 0.5em"/>'; }
	if( bMonth ){
		if( !cmn_oInformation.sLanguage ){ cmnInit_Information(); }
		sHTML += '<select>';
		var sCase = bDay ? 'g' : 's';
		for( var i = 1 ; i < 13 ; i++ ){
			sHTML += '<option value="' + i + '">' + x_hLabels[cmn_oInformation.sLanguage]['month_' + i][sCase] + '</option>';
		}
		sHTML += '</select>';
	}
	if( bYear ){ sHTML += '<input size="4" style="margin-left: 0.5em" value="' + (oDate? oDate.getFullYear() : '') + '" />'; }
	eInput.eCalendar.innerHTML += sHTML;
	eInput.parentNode.insertBefore( eInput.eCalendar, eInput );
	eInput.style.visibility = 'hidden';
}



// alpha	

function x_Make_multiply(
	sElementId,
	sButtonAddId,
	sButtonRemoveId,
	iCountAddFields,
	fFunction
	) {
	
	new x_Cloner(
		sElementId,
		sButtonAddId,
		sButtonRemoveId,
		iCountAddFields,
		fFunction
		);

}

function x_Cloner(
	sElementId,
	sButtonAddId,
	sButtonRemoveId,
	iCountAddFields,
	fFunction
	) {
		
	if(sElementId.indexOf('row_') == 0) {
		sElementId = sElementId.substr(4);
	}
	this.sElementId = sElementId;
	this.oSourceElement = document.getElementById(sElementId);
	this.oBaseElement = this.oSourceElement.cloneNode(true);
	this.iPostfix = 1;
	this.iFirstElementPostfix = this.iPostfix;
	this.iCounter = 1;	
	this.oObjectInsertBefore = null;
	this.sButtonAddId = sButtonAddId;
	this.sButtonRemoveId = sButtonRemoveId;
	this.oButtonRemove = null;
	this.oButtonAdd = null;
	this.fFunction = fFunction;
	
	var oThis = this;
	var oButtonAdd = document.getElementById(sButtonAddId);			
	oButtonAdd.onclick = function() {
	
		var oNewElement = oThis.add(0);
		
		if(oThis.fFunction) {
			oThis.fFunction(oNewElement);
		}
	
	};	

	Common.Class.add(oButtonAdd, "first");
	
	//       
	
	var oButtonRemove = document.getElementById(sButtonRemoveId);				
	if(oButtonRemove){
		oButtonRemove.onclick = function() {
			oThis.remove(0);
		};				
    	Common.Class.add(oButtonRemove, "first_remove");
	}
	this.aElements = [{oElement : this.oSourceElement, oButtonRemove : oButtonRemove, oButtonAdd : oButtonAdd}];
	
	if(iCountAddFields > 0) {	
		this.processAddFields(iCountAddFields);		
	}
	
	this.checkForDisable();

}

x_Cloner.prototype = {
	
	processAddFields : function(iCountAddFields) {
	
		this.iPostfix += iCountAddFields;	
		this.iCounter += iCountAddFields;
		
		var
			oButtonAdd,
			oButtonRemove,
			oThis = this,
			iCurrentPostfix = this.iPostfix,
			oNewElement
			;
				
		for(var i = 1; i <= iCountAddFields; i++) {
		
			oNewElement = document.getElementById(this.sElementId + '_' + i);
			
			oButtonAdd = document.getElementById(this.sButtonAddId + '_' + i);
			
			if(oButtonAdd) {
			
				oButtonAdd.iPostfix = i;
			
				oButtonAdd.onclick = function() {
							
					var oNewElement = oThis.add(this.iPostfix);
							
					if(oThis.fFunction) {
						oThis.fFunction(oNewElement);
					}
					
				}
			
			}
			
			oButtonRemove = document.getElementById(this.sButtonRemoveId + '_' + i);
			
			if(oButtonRemove) {
			
				oButtonRemove.iPostfix = i;
			
				 oButtonRemove.onclick = function() {
							
					oThis.remove(this.iPostfix);											
					
				}
			
			}
			
			this.aElements[i] = {oElement : oNewElement, oButtonAdd : oButtonAdd, oButtonRemove : oButtonRemove};
			
		}
	
	},
	
	add : function(iPostfix) {

	
		var			
			oNewElement = this.oBaseElement.cloneNode(true),								
			oButtonRemove,
			oButtonAdd
			;	

		this.removeFieldsetLegend(oNewElement);

		if(oNewElement.id) {
			oNewElement.id += '_' + this.iPostfix;
		}
		
		if(oNewElement.name) {
			oNewElement.name += '_' + this.iPostfix;
		}


		var oElement = this.aElements[iPostfix].oElement;
		
		if(oElement.nextSibling) {
			oElement.parentNode.insertBefore(oNewElement, oElement.nextSibling);					
		}

		else {
			oElement.parentNode.appendChild(oNewElement);
		}

		this.changeAttributes(oNewElement);
		this.reindexNames(oNewElement);

		
		this.aElements[this.iPostfix] = {oElement : oNewElement, oButtonRemove : this.oButtonRemove, oButtonAdd : this.oButtonAdd};

		this.cloneDependend(oNewElement, this.oSourceElement);


		this.iPostfix++;
		this.iCounter++;				
		this.checkForDisable();
		
		//fix ie bug
		document.body.className += '';
		return oNewElement;
		
	},
	
	removeFieldsetLegend: function(oNewElement){
		if(oNewElement.tagName != 'FIELDSET') return;
		
		var aNodes = oNewElement.childNodes;
		for(var i = 0; i < aNodes.length; i++){
			var oNode = aNodes[i];
			if(oNode.nodeType != 1) continue;
			if(oNode.tagName == 'LEGEND'){
				oNewElement.removeChild(oNode);
				return;
			}
		}
	},
	
	reindexNames : function(
		oElement
		) {
	
		var aTagNames = ['input', 'select', 'textarea'];
		
		//alert(oElement.innerHTML);
		for(var j = 0; j < aTagNames.length; j++) {
			
			var aInputsChildren = oElement.getElementsByTagName(aTagNames[j]);
			for(var k = 0; k < aInputsChildren.length; k++) {
				
				if(aInputsChildren[k].name) {
					var sBaseName = aInputsChildren[k].name.match(/^(.+)_(\d+)$/);
					if(sBaseName) {
						this.setElementName(aInputsChildren[k], sBaseName[1] + '_' + this.iPostfix);
					} else {
						this.setElementName(aInputsChildren[k], aInputsChildren[k].name + '_' + this.iPostfix);
					}
				}
			
			}
			
		}							
		//alert(oElement.innerHTML);
	},
	
	
	setElementName: function(oInput, sName) {
		if(window.navigator.userAgent.indexOf ( "MSIE " ) != -1) {
			// Yuppi! We've got some ie related shit here
			var oNewInput = document.createElement('<' + oInput.tagName + ' name="' + sName + '" >');
			var oFields = {
				'INPUT': ['type', 'className', 'checked', 'selected', 'defaultChecked', 'defaultSelected', 'size', 'value'],
				'SELECT': ['className', 'innerHTML', 'selectedIndex'],
				'TEXTAREA': ['className', 'innerHTML', 'cols', 'rows']
			}
			var sId = oInput.id;
			for(var i = 0; i < oFields[oInput.tagName].length; i++){
				var sField = oFields[oInput.tagName][i];
				if(oInput[sField]){
					oNewInput[sField] = oInput[sField];
				}
			}
			oInput.parentNode.insertBefore(oNewInput, oInput);
			oInput.parentNode.removeChild(oInput);
			oNewInput.id = sId;
		} else {
			oInput.name = sName;			
		}
	},
	
	changeAttributes : function(oElement) {
	
		var
			oThis = this,			
			aNodes = oElement.childNodes,
			iCurrentPostfix = this.iPostfix
			;
		
		for(var i = 0; i < aNodes.length; i++) {
		
			if(aNodes[i].nodeType == 1) {
			
				if(aNodes[i].id) {				
				
					if(aNodes[i].id == this.sButtonAddId) {
						this.oButtonAdd = aNodes[i];
							
						this.oButtonAdd.onclick = function() {
							
							var oNewElement = oThis.add(iCurrentPostfix);
							
							if(oThis.fFunction) {
								oThis.fFunction(oNewElement);
							}
						
						}
							
					}
					else if (aNodes[i].id == this.sButtonRemoveId){
						
						this.oButtonRemove = aNodes[i];
							
						this.oButtonRemove.onclick = function() {
							
							oThis.remove(iCurrentPostfix);
						
						}
						
					}									
				
					aNodes[i].id += '_' + iCurrentPostfix;										
					
				}
				
				if(aNodes[i].htmlFor) {				
					aNodes[i].htmlFor += '_' + iCurrentPostfix;
				}
				
				if((aNodes[i].tagName.toLowerCase() == 'input' && aNodes[i].type == 'text') || aNodes[i].tagName.toLowerCase() == 'textarea') {
					aNodes[i].value = '';
				}
				
				if( aNodes[i].tagName.toLowerCase() == 'input' || aNodes[i].tagName.toLowerCase() == 'textarea' || aNodes[i].tagName.toLowerCase() == 'select' ) {
					x_Get_accompanied_elements( aNodes[i] );
					x_Add_events( aNodes[i] );
				}
				
				if(aNodes[i].childNodes.length > 0) {					
					this.changeAttributes(aNodes[i]);
				}
				
			}
			
				
		}					
	
	},
	
	remove : function(iPostfix) {
	
		var oElementForRemove = this.aElements[iPostfix];
		
		if(oElementForRemove) {
		
			//    ,   
			
			if(this.iFirstElementPostfix == iPostfix) {
							
				for(var i = iPostfix + 1, bFounded = false; i < this.aElements.length && !bFounded; i++) {
				
					if(this.aElements[i]) {
					
						bFounded = true;
						
						this.iFirstElementPostfix = i;
						Common.Class.add(this.aElements[i].oButtonAdd, "first");
						
					}
				
				}
			
			}
		
			oElementForRemove.oElement.parentNode.removeChild(oElementForRemove.oElement);
			oElementForRemove = null;						
		
		}
	
		this.iCounter--;
		
		this.checkForDisable();				
	
	},
	
	checkForDisable : function() {
		if(this.aElements[0].oButtonRemove){
			var bDisable = this.iCounter == 1? true : false;

			for(i = 0; i < this.aElements.lenght; i++) {
			
				if(this.aElements[i] ) {
				
					this.aElements[i].oButtonRemove.disabled = bDisable;
					
					bDisable? Common.Class.add(this.aElements[i].oButtonRemove, "disabled") : Common.Class.remove(this.aElements[i].oButtonRemove, "disabled");
				}
			}
		} else {
			if(this.aElements.length > 1) {
				var oTmp = this.aElements[this.aElements.length - 2].oButtonAdd;
				while(oTmp.tagName != 'DL' && oTmp) {
					oTmp = oTmp.parentNode;	
				}
				if(oTmp) {
					oTmp.style.display = 'none';
				} else {
					this.aElements[this.aElements.length - 2].oButtonAdd.style.display = 'none';
				}
			}
		}
	},
	
	cloneDependend: function(oNewElement, oOldElement) {
		
		var aOldChildren = oOldElement.getElementsByTagName('input');
		var aNewChildren = oNewElement.getElementsByTagName('input');
		for(var i = 0; i < aOldChildren.length; i++){
			var oOldChild = aOldChildren[i];
			var oNewChild = aNewChildren[i];
			if(oOldChild.aDependendFrom){
				for(var j = 0; j < oOldChild.aDependendFrom.length; j++){
					var oFrom = oOldChild.aDependendFrom[j]
					var oElement = this.findClone(oNewElement, oOldElement, oFrom.eFrom);
					if(oElement){
						x_Make_dependence( oNewChild.id, oElement.id, oFrom.aValue, oFrom.bInverse )
					}
/*					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();*/
				}
			}
		}
	},
	
	findClone: function(oNewElement, oOldElement, oElement) {
		var aChildren = oOldElement.getElementsByTagName(oElement.tagName);
		for(var i = 0; i < aChildren.length; i++){
			if(aChildren[i].id == oElement.id){
				var aTmp = oNewElement.getElementsByTagName(oElement.tagName);
				return aTmp[i];
			}
		}
		return null;
	}

};