
var RM_Form = {
	
	pasteExample: function(target_id, text, options)
	{
		var obInput = $('#'+target_id)
		obInput.val(text);
		
		this.fieldHint(obInput,'focus');
		this.fieldHint(obInput);
		
		if ( options && options.icon )
			this.fieldStateIcon(obInput);
	},  
	
	fieldStateIcon: function(obInputField)
	{
		obInputField = $(obInputField);
		var id = $(obInputField).attr('id');
		//alert(obInputField.val());
		var additionalState = true;
		if (obInputField.val())
		{
			if ( $('#for_'+id).attr('src') == '/images3/main/form/flag.gif' )
			{
				obInputField.attr('state',1);
				$('#for_'+id).attr('src', '/images3/main/ico/etrain/flag.gif');
			}
			delete(srcString);
		}
		else
		{
			obInputField.attr('state',0);
			if ($('#for_'+id).attr('src') != null)
				$('#for_'+id).attr('src','/images3/main/form/flag.gif');
		}
	},
	
	fieldHint: function(obInputField,act)
	{
//			var suggestClass = 'suggest' + $(obInputField).attr('id');
//			eval( 'if ( typeof(' + suggestClass + ') != \'undefined\' ) var obSuggest = ' + suggestClass + ';' );
		
		obInputField = $(obInputField);
		if (act == 'focus')
		{
			if (obInputField.val() != '' && obInputField.val() == obInputField.attr('default'))
			{
//					if ( obSuggest != undefined )
//					{
//						if ( obSuggest.isInputEmpty )
//								obInputField.val('');
//					}
//					else
					obInputField.val('')	
			}
			obInputField.removeClass( 'gray' );
			
			/*if ( obSuggest != undefined )
				obSuggest.loadList = true;*/
		}
		else
		{
			if (obInputField.val() == '')
			{
				obInputField.val(obInputField.attr('default'));
				obInputField.addClass( 'gray' );
				/*
				if ( obSuggest != undefined )
				{
					obSuggest.isInputEmpty = true;
					obSuggest.loadList = false;
				}*/
			}
			return false;
		}
	},
	
	checkInputState: function(obForm,fieldExp)
	{
		var common_state = true;
		obForm = $(obForm);
		$(fieldExp,obForm).each( function(){
			common_state = $(this).attr('state') == 0 ? false : common_state;
		});
		return common_state;
	},
	
	validateForm: function(obForm)
	{
		var common_state = true;
		obForm = $(obForm);
		$('input',obForm).each(function()
		{
			if ($(this).rmeasysuggest)
			{
				try
				{
					common_state = $(this).rmeasysuggest().valid() ? common_state : false;
				}
				catch($error)
				{

				}
			}
		});
		return common_state;
	},
	
	
	turn: function (departureStationId, departureStationInputId, arrivalStationId, arrivalStationInputId)
	{
		var isSelectBox = $('#searchDepartureStationSelectBox').length && $('#searchArrivalStationSelectBox').length; 
		var s1 = [$('#'+departureStationInputId).val(), $('#'+departureStationId).val(), $('#'+departureStationId).attr('default')];
		if (isSelectBox)
		{
			s1[s1.length] = 'Departure';
			s1[s1.length] = $('#searchDepartureStationSelectBox').html();
		}
		var s2 = [$('#'+arrivalStationInputId).val(), $('#'+arrivalStationId).val(), $('#'+arrivalStationId).attr('default')];
		if (isSelectBox)
		{
			s2[s2.length] = 'Arrival';
			s2[s2.length] = $('#searchArrivalStationSelectBox').html();
		}
		if ((s1[0] != '' && s1[1] != s1[2]) || (s2[0] != '' && s2[1] != s2[2]))
		{
			if (s2[0] != '' && s2[1] != s2[2])
			{
				$('#'+departureStationId).rmeasysuggest().pasteValue(s2[1], s2[0]);
				if (isSelectBox)
				{
					$('#search'+s1[3]+'StationSelectBox').html(s2[4]);
					$('#search'+s1[3]+'StationSelectBox').val(s2[0]); 
					this.initSelectBox(s1[3]);
				}
			}
			else
				$('#'+departureStationId).rmeasysuggest().pasteValue('', 0);
			if (s1[0] != '' && s1[1] != s1[2])
			{
				$('#'+arrivalStationId).rmeasysuggest().pasteValue(s1[1], s1[0]);
				if (isSelectBox)
				{
					$('#search'+s2[3]+'StationSelectBox').html(s1[4]);
					$('#search'+s2[3]+'StationSelectBox').val(s1[0]); 
					this.initSelectBox(s2[3]);
				}
			}
			else
				$('#'+arrivalStationId).rmeasysuggest().pasteValue('', 0);
		}
	},
	
	
	showDateCleanLink: function( fieldId )
	{
		var pattern = new RegExp(/^\d{2}\.\d{2}\.\d{4}$/);
		var textList = {
			'сегодня'		: true,
			'завтра'		: true,
			'послезавтра'	: true
		};
		
		if ( (pattern.test( $('#' + fieldId).val() ) || textList[ $('#' + fieldId).val() ]) && $('#' + fieldId + '_clean').is(':hidden') )
		{
			$('#' + fieldId + '_example').toggle();
			$('#' + fieldId + '_clean').toggle();
		}
	},
	
	
	/**
	 * Требует наличия jsPlugin('rm.overform.popup.js'), рассчитан на использование с нашей фирменной туту-кнопкой.
	 * 
	 * @param	{}		formId
	 * @param	{}		fieldId
	 * @param	{}		buttonObject
	 * @param	{}		errorText
	 * @param	{}		offsetTop
	 * @param	{}		offsetLeft
	 * @return	bool
	 */
	checkFormFieldWithPopup: function (formId, fieldId, buttonObject, errorText, offsetTop, offsetLeft)
	{
		offsetTop = parseInt(offsetTop ? offsetTop : -20);
		offsetLeft = parseInt(offsetLeft ? offsetLeft : 150);
		
		var fieldList = jQuery.isArray(fieldId) ? fieldId : [fieldId];
		var obPopup = new RM_Overform_Popup();
		
		for (i in fieldList)
		{
			$('#'+fieldList[i]).keyup(function(){
				if (this.value != '')
					obPopup.destroy();
			}).change(function(){
				if (this.value != '')
					obPopup.destroy();
			}).focus(function(){
				if (this.value != '')
					obPopup.destroy();
			});
		}
		
		$('#'+formId).submit(function(e)
		{
			for (i in fieldList)
			{
				var obInput = $('#'+fieldList[i]);
				var inputVal = obInput.val().replace(/^\s*(.*)\s*$/, '$1');
				
				if (inputVal == '' || inputVal == 'Откуда' || inputVal == 'Куда')
				{
					buttonObject.reset();
					obPopup.showMessage(errorText, obInput.offset().left+offsetLeft-80, obInput.offset().top+offsetTop, 180, 10, 0);
					return false;
				}
			}
			
			obPopup.destroy();
			return true;
		});
	},
	initSelectBox: function (type)
	{
		if ($('#search'+type+'StationSelectBox_input'))
			$('#search'+type+'StationSelectBox_input').remove();
		if ($('#search'+type+'StationSelectBox_container'))
			$('#search'+type+'StationSelectBox_container').remove();
		$('#search'+type+'StationSelectBox').selectbox({
			'notCalcWidth'		: true,
			'notInitValue'		: true,
			'suggestId'			: 'search'+type+'StationId',
			'suggestName'		: 'search'+type+'StationName',
			'maxHeight'			: 360
		});
	}
	
	
};
