var selectOptionArray = new Array();
var selectOptionArrayIndex = 0;
 
function initCompanySelection(){
    var localCompanys = getCompanys();
    if(localCompanys.length!=0){
        insertDefaultOption();
        
        //localCompanys = localCompanys.substring(0,localCompanys.length-1);
        var companyList = localCompanys.split(",");
        for(var i=0;i<companyList.length;i++){
            var companyName = trim(companyList[i]);
            //if(!isExist(companyName) && !isEmpty(companyName))
                insertSelectOption(companyName,companyName);
        }
    }
}
 
function getCompanys(){
    return companys;
}
 
function insertDefaultOption(){
    insertSelectOption(ec_member_company_querypassword,"");
}
 
function insertSelectOption(optionText, optionValue){
    var selectionElement = getSelectionElement();
    var optionElement = document.createElement("option");
    selectionElement.options.add(optionElement);
    optionElement.text = optionText;
    optionElement.value = optionValue;
    if(isOptionSelected(optionValue))
        optionElement.selected = true;

    //selectionElement.add(optionElement);

    selectOptionArray[selectOptionArrayIndex++] = optionText;
}
 
function isOptionSelected(value){
    return false;
}
 
var accountSeparator = "#";
 
function appendCompanyInfoToAccount(){
    var selectionElement = getSelectionElement();
    var selectedOption = selectionElement.options[selectionElement.selectedIndex];
    var accountElement = getAccountElement();
    var accountInputElement = getAccountInputElement();
    if(selectedOption.value!=""){
        var newAccount = accountInputElement.value + accountSeparator + selectedOption.value;
        accountElement.value = newAccount;
    }else{
        accountElement.value = accountInputElement.value;
    }
}

function getSelectionElement(){
    var theForm = document.queryPasswordForm;
    var selectionElement = document.getElementById("CompanySelection");
    return selectionElement;
}
 
function getAccountElement(){
    var theForm = document.queryPasswordForm;
    var accountElement = theForm.PortalAccount;
    return accountElement;
}
 
function getAccountInputElement(){
    var theForm = document.queryPasswordForm;
    var accountElement = theForm.PortalAccountInput;
    return accountElement;
}

function ltrim(instr){
    return instr.replace(/^[\s]*/gi,"");
}
 
function rtrim(instr){
    return instr.replace(/[\s]*$/gi,"");
}
 
function trim(instr){
    instr = ltrim(instr);
    instr = rtrim(instr);
    return instr;
}

function isEmpty(value){
    return value=="";
}
 
 
function isExist(value){
    for(var i=0;i<=selectOptionArrayIndex;i++){
        if(value == selectOptionArray[i])
            return true;
    }
    return false;
}
