/*
 * yui-ext
 * 
 */

var DOM = YAHOO.util.Dom;

function DetailsDialog () {
    var dialog, txtUsername, txtPassword;
    var postBtn, redirectLink, command;
    var wait, error, errorMsg;
    var dialogW = 420;
    var dialogH = 380;
    
   return {
 	 iSubmit: 0,
 	 txtname_1_tmp :0,
	 txtname_2_tmp :0,
	 txtname_3_tmp :0,
	 txtname_4_tmp :0,
	 txtname_5_tmp :0,
      show : function(linkId, rL,command_var){
			
	 	 if(dialog.isVisible()){
		       dialog.hide();
	     }
        
        redirectLink = rL;
        command=command_var;
        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.toFront();
            dialog.show(linkObj);
      },
    
        init : function(position){

			txtname_1		= getEl('f[is_delivery]');
			txtname_2		= getEl('f[delivery_date]');
			txtname_3		= getEl('f[delivery_time]');
			txtname_4		= getEl('f[d_pickup_city]');
			txtname_5		= getEl('f[zip]');		

			//if(txtname_1){
			 	//txtname_1_tmp 	=txtname_1.dom.checked;			 	
			//}
			txtname_1_tmp 	=document.forms['order'].elements['f[is_delivery]'][0].checked;
			
			if(txtname_2){

				if(txtname_2.dom.options){
					  for (i=0;i<txtname_2.dom.options.length;i++)
					  {
						    var current = txtname_2.dom.options[i];
						    if (current.selected)
						    {
  							    //alert(current.value);
								txtname_2_tmp   =i;break;
							}
				 	 }
				}
			}
			if(txtname_3){
				if(txtname_3.dom.options){
					 for (i=0;i<txtname_3.dom.options.length;i++)
					 {
					    var current = txtname_3.dom.options[i];
					    if (current.selected)
					    {							
							//alert(current.value);
							txtname_3_tmp   =i;break;
						}
					}
				}
			}	

			if(txtname_4){
				if(txtname_4.dom.options){
			    	 for (i=0;i<txtname_4.dom.options.length;i++)
					 {
					    var current = txtname_4.dom.options[i];
					    if (current.selected)
					    {
							txtname_4_tmp   =i;
							break;
						}
					}
				}
		    }
		    if(txtname_5){
	 			txtname_5_tmp   =txtname_5.dom.value; 	
	 		}

             wait = getEl('post-wait-details');
             error = getEl('post-error-details');
             errorMsg = getEl('post-error-msg-details');          
             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('details-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 == 'on'){
						if(document.continue_form){
							document.continue_form.submit();
						}							
					}
					else{
	                    
	                    /*if(redirectLink == 'on_two')	                    
	                    {
	                    	setTimeout(location = 'user_order_step_3n.php', 100);
	                    }
	                    else
	                    {*/
		                   // if(redirectLink == '' || redirectLink == null || /^index.php(.)*$/.test(redirectLink)){
					        //    setTimeout(location = result.link, 100);
					        // }
					       //  else{
					       location.href = redirectLink;
					      //setTimeout("location.href = "+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; padding-top:10px">' + result.errors[i] + '</div>';
                  }
                  
                  errorMsg.update(err);
                   //alert(result.error); 
                  if(command == 'refferals'){
                  	  if(result.error!=''){						  
			
						  MDR.show('post-dialog-btn-details',result.error,"Change order details","View Other Restaurants","user_order_step_2n.php");

						  /*var conf= confirm(result.error);
						  if(conf){
						  	  location.href="user_order_step_2n.php";
						  }*/
					   }
                  }else{
                  	  //if(result.error!=''){			
	                  //	 err = '<div class="Error" style="border: solid 0px orange; margin:0px; padding:0px">' + result.error + '</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', 'user_order_step_1_ajax.php', 
	                    {success: commentSuccess, failure: commentFailure});          
	        }
        },
        
        createDialog : function(position){
         var _shim=false;
  	        if(navigator.appMinorVersion) _shim=true; 
            dialog = new YAHOO.ext.BasicDialog("details-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.addKeyListener(27, dialog.hide, dialog);
            dialog.addKeyListener(13, this.submitComment, this);
            
            postBtn = getEl('post-dialog-btn-details');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('cancel-dialog-btn-details');
            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');
				
				//if(txtname_1){										
				//	txtname_1.dom.checked = txtname_1_tmp;
					if(this.txtname_1_tmp){
						document.forms['order'].elements['f[is_delivery]'][0].checked=true;
						document.forms['order'].elements['f[is_delivery]'][1].checked=false;
						//txtname_1.dom.value=1;
					}else{
						document.forms['order'].elements['f[is_delivery]'][0].checked=false;
						document.forms['order'].elements['f[is_delivery]'][1].checked=true;
						//txtname_1.dom.value=0;
					}
				//}
				if(txtname_2){
					if(txtname_2.dom.options){
						txtname_2.dom.options[this.txtname_2_tmp].selected = true;
					}
				}
				if(txtname_3){
					if(txtname_3.dom.options){
						txtname_3.dom.options[this.txtname_3_tmp].selected = true;
					}
				}
				if(txtname_4){
					if(txtname_4.dom.options){
						txtname_4.dom.options[this.txtname_4_tmp].selected = true;
					}
				}
				if(txtname_5){
					txtname_5.dom.value = this.txtname_5_tmp;
				}				
				Delivery_set();
				show_date_time();
				show_other_time(true);	
            });
            
            dialog.on('show', function(){
              //txtUsername.value = tmpUsername;
              //txtname_1.focus();                 
            });
        }
    };
};

var DD = new DetailsDialog();

YAHOO.ext.EventManager.onDocumentReady(DD.init, DD, true);
if(document.getElementById('DetailsLink'))
	document.getElementById('DetailsLink').style.display = '';

