/*
// 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' ], i, ii, j, aeInput, sValue;
	for( i in asInput ){
		if (typeof (asInput[i]) == 'function')
			continue;
		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;
	
	//addEvent(this.eForm,'submit',x_Check_form);
	
	//alert(this.special ? this.special : this.getAttribute('special'));
	

	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 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{1,3}\\x20?)?(\\d{3,4}\\x20?|\\(\\d{3,4}\\)\\x20?)?\\d{2,3}\\-?\\d\\d\\-?\\d\\d(\\s[\\d\\s\(\)]*)?$" );
						}
						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": 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 ){
				//alert('a');
				aHandler[aHandler.length] = xRequiredGroup;
			}
		}
		if( eInput.x_mask ){
			aHandler[aHandler.length] = xCheck_mask;
			eInput.onblur = function(){
				xCheck_mask( this );
			}
			eInput.onfocus = function(){
				removeClass( this, "(in)?valid" );
			}
		}
		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( matchClass( eParent, "row" ) ){
				return eParent;
			}
			eParent = eParent.parentNode;
		}
		return false;
	}
}

function x_Check_form(){
	var eWrong = false;
	for( var i in this.hControl ){
		if( !this.hControl[i].disabled
			&& ( matchClass( this.hControl[i], "required" )
				|| ( matchClass( this.hControl[i], "required-ok" )
					&& matchClass( this.hControl[i], "invalid" ) ) ) ){
			if( !eWrong ){
				eWrong = this.hControl[i];
			}
			//if( this.hControl[i].eLabel ){
				var eDIV = document.createElement( "INS" );
				eDIV.className = "warning";
				if( matchClass( 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;
	}
	sendForm(this);
	return false;
}

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 ){
			replaceClass( aeInput[i], "required", "required-ok" );
			replaceClass( aeInput[i].eRow, "required", "required-ok" );
			replaceClass( aeInput[i].eLabel, "required", "required-ok" );
		}else{
			replaceClass( aeInput[i], "required-ok", "required" );
			replaceClass( aeInput[i].eRow, "required-ok", "required" );
			replaceClass( 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 ){
			replaceClass( aeInput[i], "required", "required-ok" );
			replaceClass( aeInput[i].eRow, "required", "required-ok" );
			replaceClass( aeInput[i].eLabel, "required", "required-ok" );
		}else{
			replaceClass( aeInput[i], "required-ok", "required" );
			replaceClass( aeInput[i].eRow, "required-ok", "required" );
			replaceClass( 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 ) ){
		replaceClass( eInput, "valid", "invalid" );
		replaceClass( eInput.eRow, "valid", "invalid" );
	}else{
		replaceClass( eInput, "invalid", "valid" );
		replaceClass( 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_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{
			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 };
	addEvent( eFrom, "blur", function(){ eThis.on_text_depended_changed(); } );
	addEvent( eFrom, "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 ) eThis.focus();
		if( eThis.onkeyup ){ eThis.onkeyup(); }
		if( eThis.onclick ){ eThis.onclick(); }
	}
	addEvent( 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(); }
	}
	addEvent( 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(); }
	}
	addEvent( 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;
					}
				}
			}
			addEvent( 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' );
		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 ){
	return false;
	eInput.eCalendar = document.createElement( 'span' );
	var sHTML = '';
	if( bDay ){ sHTML = '<input size="2" />'; }
	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" />'; }
	eInput.eCalendar.innerHTML += sHTML;
	eInput.parentNode.insertBefore( eInput.eCalendar, eInput );
	eInput.style.visibility = 'hidden';
}
