/*
 * yui-ext
 *
 */

var DOM = YAHOO.util.Dom;
function RDialog () {
    var dialog, txtUsername, txtPassword;
    var postBtn, redirectLink;
    var wait, error, errorMsg;
    var dialogW = 300;
    var dialogH = 300;

   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]');

             wait = getEl('post-wait-request');
             error = getEl('post-error-request');
             errorMsg = getEl('post-error-msg-request');
             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('request-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);
			          }*/
                    //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', 'company_req_restaurant_ajax.php',
	                    {success: commentSuccess, failure: commentFailure});
	        }
        },

        createDialog : function(position){
		//-dialogW
           var _shim=false;
  	          if(navigator.appMinorVersion) _shim=true;
            dialog = new YAHOO.ext.BasicDialog("request-dlg", {
          draggable: false,
                    modal:false,
					closed:true,
                    width: dialogW,
                    height: dialogH,
                    x: position[0]+160,
                    y: position[1]+15,
                    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-request');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('cancel-dialog-btn-request');
            cancelBtn.on('click', dialog.hide, dialog);

            //postBtn = dialog.addButton('Sign-In', this.submitComment, this);
            //dialog.addButton('Cancel', 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_restaurant.focus();
            });
        }
    };
};
var RD = new RDialog();
YAHOO.ext.EventManager.onDocumentReady(RD.init, RD, true);
if(document.getElementById('RequestLink'))
	document.getElementById('RequestLink').style.display = '';
