/*
 * yui-ext
 * 
 */

var DOM = YAHOO.util.Dom;
function RegistrationDialog () {
    var dialog, txtUsername, txtPassword;
    var postBtn, redirectLink;
    var wait, error, errorMsg;
    var dialogW = 850;
    var dialogH = 525;

    
   return {
		iSubmit: 0,
      
      show : function(linkId, rL){
        if(dialog.isVisible()){
		     dialog.hide();
	    }
        
        redirectLink = rL;
        var position = new Array;
          position = DOM.getXY(linkId);
          
          this.init(position);
          
          	//dialog.resizeTo(dialogW, dialogH);
      		//dialog.moveTo(position[0]-dialogW+37, position[1]+19);
      		
            var linkObj = getEl(linkId);
            dialog.show(linkObj);
      },
    
        init : function(position){
           
			//txtname_restaurant		= getEl('f[name_restaurant]');
			//txtcity					= getEl('f[city]');
			//txtstore_contact		= getEl('f[store_contact]');
			//txtname 				= getEl('f[name]');
			//txtemail				= getEl('f[email]');
			txtname_first_name		= getEl('f_reg[first_name]');
           
             wait = getEl('post-wait-registration');
             error = getEl('post-error-registration');
             errorMsg = getEl('post-error-msg-registration');
             this.createDialog(position);
        },
        
        // submit the comment to the server
        submitComment : function(){
           	if(this.iSubmit==0){	           
	            wait.radioClass('active-msg');
	            YAHOO.util.Connect.setForm(DOM.get('registration-form'));
	         }

      var commentSuccess = function(o){

                //alert(o.responseText);

              //var result = eval('new Object(' + o.responseText + ')');
              var result = YAHOO.ext.util.JSON.decode(o.responseText);

                //postBtn.enable();
                wait.removeClass('active-msg'); 
                              
                if(result.link != false && result.errors.length == 0){                    
                    dialog.hide();
                    /*if(redirectLink == '' || redirectLink == null || /^index.php(.)*$/.test(redirectLink)){
			            setTimeout(location = result.link, 100);
			         }
			         else{
			            setTimeout(location = redirectLink, 100);
			          }*/
			         location.href = redirectLink;
                    //setTimeout(location = result.link, 100);
                }else{
                   error.radioClass('active-msg');
                  var err = '';
                  for(i in result.errors){
                    err += '<div class="Error" style="border: solid 0px orange; margin:0px; padding:0px">' + result.errors[i] + '</div>';
                  }
                  errorMsg.update(err);
                }
            };
            
            var commentFailure = function(o){
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
		    if(this.iSubmit==0){
	            this.iSubmit=1;
	            YAHOO.util.Connect.asyncRequest('POST', 'registration_ajax.php', 
	                    {success: commentSuccess, failure: commentFailure});          
	        }
        },
        
        createDialog : function(position){
		  		
 		      var _shim=false;
  	          if(navigator.appMinorVersion) _shim=true; 
	          dialog = new YAHOO.ext.BasicDialog("registration-dlg", {
	          draggable: false,
	                    modal:false,
						closed:true,
	                    width: dialogW,
	                    height: dialogH,
	                    x: position[0]-dialogW+37,
	                    y: position[1]+19,
	                    shadow:false,
	                    minWidth:dialogW,
	                    minHeight:dialogH,
	                    autoScroll: true,
	                    resizable: false,
	       				shim: _shim
	         });
	       
      	 	dialog.toFront();
            dialog.addKeyListener(27, dialog.hide, dialog);
            //dialog.addKeyListener(13, this.submitComment, this);
            
            postBtn = getEl('post-dialog-btn-registration');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('cancel-dialog-btn-registration');
            cancelBtn.on('click', dialog.hide, dialog);
            
            
            // clear any messages and indicators when the dialog is closed
            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');
                
				//txtname_restaurant.dom.value = '';
				//txtcity.dom.value = '';
				//txtstore_contact.dom.value = '';
				//txtname.dom.value = '';
				//txtemail.dom.value = '';
				

            });
            
            dialog.on('show', function(){
             
              //txtUsername.value = tmpUsername;
              txtname_first_name.focus();
              
            });
        }
    };
};
var REGD = new RegistrationDialog();
YAHOO.ext.EventManager.onDocumentReady(REGD.init, REGD, true);
if(document.getElementById('RegisterLink'))
	document.getElementById('RegisterLink').style.display = '';
