// JavaScript Document
// JScript file

jQuery.fn.extend({
    /****************************************************
     *  Get servers of a game and append to the select box
     *  GameId:    the game's id server belongs to
     *  value:   the value which you want to select after loaded
     ****************************************************/
    BindServer: function(GameId, value) { 
        return this.each(function () { jQuery(this).GetSelectList("game_product_ajax_handler.php", "action=SelectServer&game_id="+GameId, value)});       
		
    },
       
	   
	 /****************************************************
     *  Get servers of a game and append to the select box
     *  GameId:    the game's id server belongs to
     *  value:   the value which you want to select after loaded
     ****************************************************/
    BindFraction: function(GameId, value) { 		
		return this.each(function () { 
            var sel=jQuery(this);
            jQuery.ajax({
                url: "game_product_ajax_handler.php",
                data: "action=SelectFraction&game_id="+GameId,
				type:"post",
                beforeSend: function(){ sel.empty().append("<option value='0'>.........</option>"); },
                success: function(msg){ 
					if('' == msg){
						sel.hide().empty();
					}
					else{
						sel.show().empty().append(msg).val(value); 
					}					
				},
                error: function(msg){ }
                });
            });
    },
	
	 /****************************************************
     *  Get servers of a game and append to the select box
     *  GameId:    the game's id server belongs to
     *  value:   the value which you want to select after loaded
     ****************************************************/
    BindDeliveryMethod: function(GameId, value) { 
        return this.each(function () { jQuery(this).GetSelectList("game_product_ajax_handler.php", "action=BindDeliveryMethod&game_id="+GameId, value)});  
    },
    
	 /****************************************************
     *  Get products by conditions and append to the select box
     *  GameId:    the game's id server belongs to
     *  value:   the value which you want to select after loaded
     ****************************************************/
    BindGoldProduct: function(GameId,ServerId,FractionId, value) { 
        return this.each(function () { jQuery(this).GetSelectList("game_product_ajax_handler.php", "action=SelectProduct&game_id="+GameId+"&server_id="+ServerId+"&fraction_id="+FractionId, value)});       
		
    },
	
    /****************************************************
     *  Get select's options from server
     *  url:    server url
     *  data:   data send to server
     *  value:  the value which you want to selecte after loaded
     ****************************************************/
    GetSelectList: function(p_url,p_data, p_value) { 
        return this.each(function () { 
            var sel=jQuery(this);
            jQuery.ajax({
                url: p_url,
                data: p_data,
				type:"post",
                beforeSend: function(){ sel.empty().append("<option value='0'>.........</option>"); },
                success: function(msg){ sel.empty().append(msg).val(p_value); },
                error: function(msg){ }
                });
            });
    },
	
    
    /****************************************************
     *  summary:   this function split the text in a div 
     *          or td, and so on
     *  intLen:    lenth
     *  
     * 
     ****************************************************/
    BreakWord: function(intLen) {
        return this.each(function () { 
            var pan = jQuery(this);
            var strContent=pan.html();
            var strTemp="";
            while(strContent.length>intLen)
            {
                strTemp+=strContent.substr(0,intLen)+"<br/>";
                strContent=strContent.substr(intLen,strContent.length);
            }
            strTemp+=strContent;
            pan.html(strTemp);
        });
    },
    
    /****************************************************
     *  summary:    add some restrict to matched text box
     *  textType:   char type can input
     *  length:     max length can input, if not restrict set to 0
     * 
     ****************************************************/
    TextBoxRestrict: function(textType, length) {
        return this.each(function () {
            jQuery(this).bind("keydown", function(e){
                    
                    var ev = window.event || e; 
                    var keyCode=ev.keyCode;
            
                    //alert(keyCode);
            
                    if(keyCode==46 || keyCode==8 || keyCode==37 || keyCode==39 || keyCode==9)
                        return;
                    //restrict length
                    if(length>0 && jQuery(this).val().length>=length) ev.returnValue=false;
                    
                    //integer text box
                    if(textType=="int")
                    if(!((keyCode>=48&&keyCode<=57)||(keyCode>=96&&keyCode<=105)))ev.returnValue=false;
                                        
                    //float text box
                    if(textType=="float")
                    {
                        if(!((keyCode>=48&&keyCode<=57)||(keyCode>=96&&keyCode<=105)))                        
                        if((!ev.shiftKey && keyCode!=110) || keyCode!=190)
                        {
                            if(jQuery(this).val().indexOf(".")>0)ev.returnValue=false;
                            else return; 
                        }                       
                        
                        if(!((keyCode>=48&&keyCode<=57)||(keyCode>=96&&keyCode<=105)))ev.returnValue=false; 
                    }
                });
                
                
        });
    }
    
}); 