var rmBpKeySelectMenu = new Component("rmBpKeySelectMenu");
//if click the submit button, clickSubmitFlag set true
var clickSubmitFlag = false;
var bpkeyValue = "";
var isAutoCompleteSelect = false;
rmBpKeySelectMenu.isProcessAjax = false;
var autoCompleteData = app.autoCompleteBpkeys ;

app.registerComponent(rmBpKeySelectMenu);
rmBpKeySelectMenu.on("load",function(){
	 var self = this;
     var data = {};
     data.bpKeyList = app.user.bpKey;
     return rmBpKeySelectMenu.render(
    		 "template/rmSelectBpKeyMenu.html", 
    		 "rmSelectBpKeyMenu", 
    		 data).done( function(){    		     
    		     rmBpKeySelectMenu.bpKeyInput = $("#wmdBpkNum");
    		     rmBpKeySelectMenu.autoCompleteAccontNumber( autoCompleteData );
    		     rmBpKeySelectMenu.buttonEvent();
    		     $("#wmdBpkNum").prop("disabled",false);
 	         });
});

rmBpKeySelectMenu.autoCompleteAccontNumber = function(autoCompleteData){
    var autocompleteData =autoCompleteData;    
    $("#wmdBpkNum").autocomplete({       
        source: autocompleteData,
        minLength : 0,        
        select : function(event,ui){           
            isAutoCompleteSelect = true;
            //alert(ui.item.value);
            rmBpKeySelectMenu.checkBpk(ui.item.value);
            $this = $(this);
            setTimeout(function(){                
                $this.blur();               
            },2);
        }        
    }).focus(function(){
        $(this).autocomplete("search");
        return false;
    });
};

rmBpKeySelectMenu.uniqueArray = function(array){
    var retArray = array.concat();
    for (var i = 0 ; i < retArray.length ; i++){
        for(var j=i+1; j<retArray.length ; ++j){
            if (retArray[i] === retArray[j]){
                retArray.splice(j--,1);
            }
        }        
    }
    return retArray ;
};

/*
rmBpKeySelectMenu.initSelectEvent = function(){
	var bpKeySelected = $("select[name='RmbpKeySelected']");
	$("#bpKeyValueMenu").html(bpKeySelected.val());
	app.selectBpKey = bpKeySelected.val();
	bpKeySelected.die();
	bpKeySelected.live("change",function(){
		var selectValue = $(this).val();
		app.selectBpKey = selectValue;
		$("#bpKeyValueMenu").html(selectValue);
	});
};
*/
rmBpKeySelectMenu.buttonEvent = function() {	
	
	$("#wmdBpkNum").die().live("keydown", function(event){
		getEnter(event);
	});
	
	/*$("#wmdBpkNum").die().live("keypress", function(event){
	    isAutoCompleteSelect = false;
	    getEnter(event);
    });*/
	
	$("#wmdBpkNum").die().live("keyup", function(event){
	    checkNumber();
	    isAutoCompleteSelect = false;
        getEnter(event);        
    });
	
	$("#bpkeySubmit").die().live("click", function(){
	    var bpkId = $.trim($("#wmdBpkNum").val());
	    rmBpKeySelectMenu.checkBpk(bpkId);
	});
};

rmBpKeySelectMenu.bpkOnBlur = function(){
    if (isAutoCompleteSelect){        
        return;
    }
    else{           
        clickSubmitFlag = false;
        window.setTimeout(restBpkeyInput, 200);
    }
};

rmBpKeySelectMenu.checkBpk = function(bpkId){
    rmBpKeySelectMenu.isProcessAjax = false;
    //var bpkId = $.trim($("#wmdBpkNum").val());
    bpkId = bpkId.replace(/[^\d]/g,"");   
    if (bpkId == ""){
        bpkeyValue = "";
        return;
    }
   
    var request = { url : app.EPRESTAPI_CONTEXT_ROOT + "businessPartners/check/" + bpkId, 
                    data: {'asOfDate':app.selectDateValue}, 
                    type : "GET" , 
                    async : true
                  };
    $("#wmdBpkNum").prop("disabled",true);   
    rmBpKeySelectMenu.isProcessAjax = true;
    return ajaxCall(request).always(function(){
        rmBpKeySelectMenu.isProcessAjax = false;        
        isAutoCompleteSelect = true;
        $("#wmdBpkNum").prop("disabled",false);
    }).done(function(retData){
       if (retData.data.result=='validBp') {           
           bpkeyValue = bpkId;
           app.trigger("bpKeyChange", null , bpkId);
           $("#wmdBpkNum").val(bpkeyValue);
           clickSubmitFlag = true;
           //======modify autocomplete datasource===========================
           autoCompleteData.splice(0,0,bpkId);
           var uniqueData = rmBpKeySelectMenu.uniqueArray(autoCompleteData);
           rmBpKeySelectMenu.autoCompleteAccontNumber(uniqueData);
           autoCompleteData = uniqueData ;
           //======modify autocomplete datasource end ======================               
       }else if(retData.data.result=='closedBp') {   
    	   clickSubmitFlag = false;
           dialog.show('<span msgid="msgClosedBp">Account has been closed</span>', "Ok");  
           restBpkeyInput();  
       }
       else{
           clickSubmitFlag = false;
           dialog.show('<span msgid="msgBpkValidation">bpkey validation error</span>', "Ok");  
           restBpkeyInput();           
       }
    });
};

restBpkeyInput = function() {
	if(!clickSubmitFlag) {
		//clear value
	    if (!rmBpKeySelectMenu.isProcessAjax){
		    $("#wmdBpkNum").val(bpkeyValue);
	    }
		//console.log("bpkeyValue = " + bpkeyValue);
	}
};

checkNumber = function(){
    var bpkId = $.trim($("#wmdBpkNum").val());
    bpkId = bpkId.replace(/[^\d]/g,""); 
    $("#wmdBpkNum").val(bpkId);
};

getEnter = function(event) {
    var event = event ? event :window.event ;
    var keyCode = event.keyCode ? event.keyCode : event.which ;   
	if( keyCode == 13 ) {
		//event.returnValue = false;
		$("#bpkeySubmit").click();
		$("#wmdBpkNum").blur();
		return false;
	}
};

