var ButtonSendForm = function( idButon, idForm, sUrl, sCommand, arrFields, bUpload, sRedirect, nameFunction ) {
	var _idButon = idButon;
	var _idForm = idForm;
	var _ref = this;
	var _arrFields = arrFields;
	var _sUrl = sUrl;
	var bUpload = bUpload || false;
	ButtonSendForm.superclass.constructor.call( this,  _idButon );
	
	this.sendForm = function() {
	
	  var formObject = document.getElementById( _idForm );
	  var _refSendForm = this;
	  
	  this.Success = function(o) {
	 		var jsonString = o.responseText;
	 		try {
	  		var obj = YAHOO.lang.JSON.parse( jsonString );
		  	if ( obj.type == 'complex' ){
					var status = 'error';
					if ( obj.status == 'error' ) { 
						status = Message.STATUS_ERR;
					}
					if ( obj.status == 'ok' ){ 
						status = Message.STATUS_OK;
						
						if( nameFunction != null ){
				  		eval ( nameFunction + '( obj )' );
				  	} 
						if ( sRedirect ){
							if ( sRedirect == 'true' ){
								window.location.reload(true);
							}
							if ( sRedirect != 'true' && sRedirect != 'false' ){
								window.location = sRedirect.replace(/&amp;/g,'&');
							}
						}
						if ( 'Question_Create' == sCommand || 'Application_Create' == sCommand ){
							var formObject = document.getElementById( _idForm );
							formObject.reset();
						}
					}
						if ( obj.status == 'ok' && ( 'User_Login' == sCommand || 'User_Logout' == sCommand ) ){
						}else{
							oQue.add( new Message( status, obj.textMessage ) );
						}
					
					
				}
	  	}
	  	catch( e ) {
	  		oQue.add( new Message( Message.STATUS_ERR, "Error while executing command: " + jsonString + e) );
	  	}
	    return true;
	  }
	
	  this.Failure = function(o) {
	    oQue.add( new Message( Message.STATUS_ERR, 'Connection fail.' ) );
	  }
	
	  var responseNoUpload = 
	  {
	    success: _refSendForm.Success,
	    failure: _refSendForm.Failure
	  }
	  var responseUpload =
	  {
	    upload: _refSendForm.Success,
	    failure: _refSendForm.Failure
	  }
	  var check = false;
	  try {
	  	aV = new FormValidator( _idForm, _arrFields );
	  	var check = aV.checkIsValid();
			if( check ) {
				YAHOO.util.Connect.setForm( formObject, bUpload );
				var _stUrl =  _sUrl.replace( /&amp;/g, "&" );
				var ch = '&';
				if( -1 == _stUrl.indexOf( '?' ) ) {
					ch = '?';
				}
				var response;
				_stUrl += ch + 'command=' + sCommand;
				
				if( !bUpload ) {
					 response = responseNoUpload;
				}else{
					 response = responseUpload;
				}
		  
				var cObj = YAHOO.util.Connect.asyncRequest( 'POST', _stUrl, responseNoUpload );
			}
		}
		catch( e ) {
			 oQue.add( e );
		}
		return false;
	}
	this.on("click", this.sendForm); 
}

YAHOO.lang.extend( ButtonSendForm, YAHOO.widget.Button );


var ButtonOnDel = function( idButon, idForm, sUrl, sCommand, nameFunction) {
	var _idButon = idButon;
	var _idForm = idForm;
	var _ref = this;

	ButtonOnDel.superclass.constructor.call( this,  _idButon );
	
	this.sendForm = function() {
	
	  var formObject = document.getElementById( _idForm );
	  var _ref = this;
	 	
	  
	  this.Success = function(o) {
	 		var jsonString = o.responseText;
	 		
	 		try {
	  		var obj = YAHOO.lang.JSON.parse( jsonString );
		  	if ( obj.type = 'complex' ){
					var status = 'error';
					if ( obj.status == 'error' ) { 
						status = Message.STATUS_ERR;
					}
					if ( obj.status == 'ok' ){ 
						status = Message.STATUS_OK;
						if( nameFunction != null ){
				  		eval ( nameFunction + '( obj )' );
				  	} 
					}
					oQue.add( new Message( status, obj.textMessage ) );
				}
	  	}
	  	catch( e ) {
	  		oQue.add( new Message( Message.STATUS_ERR, "Error while executing command: " + jsonString ) );
	  	}
	    return true;
	  }
	
	  this.Failure = function(o) {
	    oQue.add( new Message( Message.STATUS_ERR, 'Connection fail.' ) );
	  }
	
	  var response =
	  {
	    succes: _ref.Success,
	    failure: _ref.Failure
	  }
	  var check = false;
	 
		YAHOO.util.Connect.setForm( formObject );
		var _stUrl =  _sUrl.replace( /&amp;/g, "&" );
		var ch = '&';
		if( -1 == _stUrl.indexOf( '?' ) ) {
			ch = '?';
		}
		alert(_stUrl)
		_stUrl += ch + 'command=' + sCommand;
		var cObj = YAHOO.util.Connect.asyncRequest( 'POST', _stUrl, response );
			
	 
	}
	this.on("click", this.sendForm); 
}

YAHOO.lang.extend( ButtonOnDel, YAHOO.widget.Button );



// диалог загрузки картинки
var DialogPreviewImageUpload = function( url, sCommand, nameEl, pathImage, idInnerHTML ) {
	var _ref = this;
	this.id = nameEl;
	this.idInnerHTML = idInnerHTML;
	this.rnd = Math.round(Math.random()*1000);
	var handleSubmit = function() {
		var formObject = document.getElementById( 'idFormPeviewImageUpload'+_ref.rnd );
	  var _refSubmit = this;
	 	
	  this.Success = function(o) {
			var jsonString = o.responseText;
	 		try {
	  		var obj = YAHOO.lang.JSON.parse( jsonString );
		  	if ( 'complex' == obj.type  ){
					var status = 'error';
					if ( 'error' == obj.status ) { 
						status = Message.STATUS_ERR;
					}
					if ( 'ok' == obj.status ){
						status = Message.STATUS_OK;
						var formObject = document.getElementById( 'idFormPeviewImageUpload'+_ref.rnd );

						document.getElementById( idInnerHTML + formObject[nameEl].value ).innerHTML = '<img src="'+ pathImage + formObject[nameEl].value + '.png?'+Math.floor(Math.random()*1000)+'" />'
						
					}
					oQue.add( new Message( status, obj.textMessage ) );
				}
	  	}
	  	catch( e ) {
	  		oQue.add( new Message( Message.STATUS_ERR, "Error while executing command: " + jsonString + e) );
	  	}
	    return true;
	  }
			
		this.Failure = function(o) {
			alert("Submission failed: " + o.status);
		};
		
	 
	
	  var response =
	  {
	    upload: this.Success,
	    failure: this.Failure
	  }
		YAHOO.util.Connect.setForm( formObject,true );
		var cObj = YAHOO.util.Connect.asyncRequest( 'POST', url, response );
		_ref.hide();
	};
	
	var handleCancel = function() {
		_ref.cancel();
	};
	
		DialogPreviewImageUpload.superclass.constructor.call(this, "dialog"+_ref.rnd,{ width : "255px", visible : false,
			buttons : [ { text:"Изменить", handler: handleSubmit, isDefault:true },
			{ text:"Отмена", handler:handleCancel } ]
		} );
	var kl_close = new YAHOO.util.KeyListener(document, {keys:27 },
												  { fn: this.hide,
														scope: this,
														correctScope:true }, "keyup" ); 
													
	this.cfg.queueProperty("keylisteners", kl_close);
	
	this.setHeader("Выбирите файл"); 
	this.setBody("<form id='idFormPeviewImageUpload"+_ref.rnd+"'><input type='hidden' name='"+nameEl+"' value=''/><input type='hidden' name='command' value='"+sCommand+"'/><input type='file' name='preview' /></form>"); 
	
	this.render( document.body );
	
	this.show = function( evint, hash ) {
		_ref.cfg.setProperty("context", [ _ref.idInnerHTML + hash.id, "tl", "tl"]); 
		var formObject = document.getElementById( 'idFormPeviewImageUpload'+_ref.rnd );
		formObject.reset();
		formObject[nameEl].value = hash.id;
	  DialogPreviewImageUpload.superclass.show.call( _ref );
	}
	
}

YAHOO.lang.extend( DialogPreviewImageUpload, YAHOO.widget.Dialog );

/**-----------**/

var ButtonDel = function( idButton, idForm, url, sCommand, nameEl, idHide ) {
	ref = this;
	ButtonDel.superclass.constructor.call( this, idButton );
	
//	 	alert('this');
	this.del = function( evan, hash ) {
		var _ref = this;
		var _id = hash.id;
	 	verification = confirm( "Are you shure delete?" );
	 	
	  if( verification ){
	  	this.JSFailDel = function (o) {
		    alert( 'Connection fail: '+o );
		    return false;
		  }
		  this.JSSuccessDel = function (o) {
		    var jsonString = o.responseText;
		    try {
			  	var obj = YAHOO.lang.JSON.parse( jsonString );
			  	if ( 'complex' == obj.type  ) {  		
						var status = 'error';
						if (  'error' == obj.status ) { 
							status = Message.STATUS_ERR;
						}
						if ( 'ok' == obj.status ){ 
							status = Message.STATUS_OK;
							hideInline ( idHide + _id );
							
						}
						oQue.add( new Message( status, obj.textMessage ) );
					}
				}
				catch( e ) {
					oQue.add( new Message( Message.STATUS_ERR, jsonString + e ) );
				}
				
				return true;
		  }
		  
		  var responseDel =
		  {
		    failure: this.JSFailDel,
		    success: this.JSSuccessDel
		  }
		  var form = document.getElementById( idForm );
    	form.command.value = sCommand;
		  form[ nameEl ].value = _id;
		  
		  YAHOO.util.Connect.setForm( idForm ,false );
		  
		  var transaction = YAHOO.util.Connect.asyncRequest('POST', url, responseDel);	
	  }
	 }  
}

YAHOO.lang.extend( ButtonDel, YAHOO.widget.Button );
	
	/*
	var thedivs = document.getElementsByTagName("div"); 

 for(var x=0;x!=thedivs.length;x++){ 
   thedivs[x].style.visibility = "hidden"; 
   thedivs[x].style.display = "none"; 
 } 
 
 */