/*
file name:jquery.select.js
auth:David.Yang
*/
//get select option count
jQuery.fn.size = function()
{
    return jQuery(this).get(0).options.length;
}
jQuery.fn.getSelectedIndex = function()
{
    return jQuery(this).get(0).selectedIndex;
}
jQuery.fn.getSelectedText = function()
{
    if(this.size() == 0)
    {
        return "no option";
    }
    else
    {
        var index = this.getSelectedIndex();      
        return jQuery(this).get(0).options[index].text;
    }
}
jQuery.fn.getSelectedValue = function()
{    
    if(this.size() == 0)
    {
        return "no option";
    }
    else
    {
        return jQuery(this).val();
    }
}
jQuery.fn.setSelectedValue = function(value)
{
    jQuery(this).get(0).value = value;
}
jQuery.fn.setSelectedText = function(text)
{
    var isExist = false;
    var count = this.size();
    for(var i=0;i<count;i++)
    {
        if(jQuery(this).get(0).options[i].text == text)
        {
            jQuery(this).get(0).options[i].selected = true;
            isExist = true;
            break;
        }
    }
    if(!isExist)
    {
        alert("option not exist");
    }
}
jQuery.fn.setSelectedIndex = function(index)
{
    var count = this.size();    
    if(index >= count || index < 0)
    {
        //alert("");
    }
    else
    {
        jQuery(this).get(0).selectedIndex = index;
    }
}
jQuery.fn.isExistItem = function(value)
{
    var isExist = false;
    var count = this.size();
    for(var i=0;i<count;i++)
    {
        if(jQuery(this).get(0).options[i].value == value)
        {
            isExist = true;
            break;
        }
    }
    return isExist;
}
jQuery.fn.addOption = function(text,value)
{
    if(this.isExistItem(value))
    {
        //alert("option is exist");
    }
    else
    {
        jQuery(this).get(0).options.add(new Option(text,value));
    }
}
jQuery.fn.removeItem = function(value)
{    
    if(this.isExistItem(value))
    {
        var count = this.size();        
        for(var i=0;i<count;i++)
        {
            if(jQuery(this).get(0).options[i].value == value)
            {
                jQuery(this).get(0).remove(i);
                break;
            }
        }        
    }
    else
    {
        //alert("");
    }
}
jQuery.fn.removeIndex = function(index)
{
    var count = this.size();
    if(index >= count || index < 0)
    {
        //alert("");
    }
    else
    {
        jQuery(this).get(0).remove(index);
    }
}
//delete select selected optioon
jQuery.fn.removeSelected = function()
{
    var index = this.getSelectedIndex();
    this.removeIndex(index);
}
//clean select all selected option
jQuery.fn.clearAll = function()
{
    jQuery(this).get(0).options.length = 0;
}
