/* 
 * File to handle adding addresses on the page.
 */
var newAddressNumbersToAdd = new Array();

function getNewMaxAddressNumber(){
    // Set the result to zero for now
    var result = 0;
    
   // Loop through the result numbers to get the highest
   for (var loopAddressNumber in newAddressNumbersToAdd){
       // Determine if the number is higher than the current number
       if (newAddressNumbersToAdd[loopAddressNumber]>result){
           // make this the new high
           result = newAddressNumbersToAdd[loopAddressNumber];
       }
   }
   
    // Return the highest result
    return result;
}

function removeNewAddressNumber(addressNumberToRemove){
    // Set the removal item to zero for now
    var addressLocationToRemove = -1;
    
   // Loop through the result numbers to get the highest
   for (var loopAddressNumber in newAddressNumbersToAdd){
       // Determine if the number is higher than the current number
       if (newAddressNumbersToAdd[loopAddressNumber]===addressNumberToRemove){
           // set this as the one to remove
           addressLocationToRemove = loopAddressNumber;
           
           // Break out of the list
           break;
       }
   }
   
   // Remove it from the list
   newAddressNumbersToAdd.splice(addressLocationToRemove,1);
}

function createNewAddressNumberString(){
    // Set the result to zero for now
    var result = '';
    
   // Loop through the result numbers
   for (var loopAddressNumber in newAddressNumbersToAdd){
       // Add it to the string
       result = result+newAddressNumbersToAdd[loopAddressNumber].toString()+'|';
   }

    // Remove the last comma
    result=result.slice(0, (result.length-1));
    
    // Return the highest result
    return result;
}

function addNewAddress_NicelyFormedRow(type) {
    console.log("in addAddress");

    // Determine if the type is of user type
    if (type==="USER"){
        // Make the Info tab active, because that is where the new address controls are
        $( "#tabs" ).tabs({ active: 0 });
    }

    // Increment the amount that is currently in the array
    var newAddressAmountID=getNewMaxAddressNumber()+1;
    
    // Create a div to hold the address titles
    var newTitleRowDiv = document.createElement("div");
    newTitleRowDiv.classList.add ('Row');
    newTitleRowDiv.id = 'newAddressTitleRow';
    newTitleRowDiv.name = 'newAddressTitleRow';
    
    // Create all divs that are right under the main row
    var cell1TitleDiv = document.createElement("div");
    cell1TitleDiv.style="vertical-align: middle;";
    cell1TitleDiv.classList.add ('cell');
    cell1TitleDiv.classList.add ('cell1');
    var addressesTitleDiv = document.createElement("div");
    addressesTitleDiv.style="vertical-align: left;";
    addressesTitleDiv.classList.add ('cell'); 
    addressesTitleDiv.classList.add ('addressCell'); 
    var cityTitleDiv = document.createElement("div");
    cityTitleDiv.style="vertical-align: middle;";
    cityTitleDiv.classList.add ('cell');
    cityTitleDiv.classList.add ('cityCell');
    var stateTitleDiv = document.createElement("div");
    stateTitleDiv.style="vertical-align: middle;";
    stateTitleDiv.classList.add ('cell');
    stateTitleDiv.classList.add ('stateCell');
    var zipTitleDiv = document.createElement("div");
    zipTitleDiv.style="vertical-align: middle;";
    zipTitleDiv.classList.add ('cell');
    zipTitleDiv.classList.add ('zipCell');
    var phonesTitleDiv = document.createElement("div");
    phonesTitleDiv.style="vertical-align: middle;";
    phonesTitleDiv.classList.add ('cell');
    phonesTitleDiv.classList.add ('phoneCell');
    var buttonsTitleDiv = document.createElement("div");
    buttonsTitleDiv.style="vertical-align: middle;";
    buttonsTitleDiv.classList.add ('cell');
    buttonsTitleDiv.classList.add ('buttonCell');

    // Create the address label
    var addressTitleLabel = document.createElement("label");
    addressTitleLabel.textContent="Address";
    
    // Add the address cell to the address div
    addressesTitleDiv.appendChild(addressTitleLabel);
    
    // Create the city label
    var cityTitleLabel = document.createElement("label");
    cityTitleLabel.textContent="City";
    
    // Add the city label to the city div
    cityTitleDiv.appendChild(cityTitleLabel);
    
    // Create the state label
    var stateTitleLabel = document.createElement("label");
    stateTitleLabel.textContent="State";
    
    // Add the state label to the state div
    stateTitleDiv.appendChild(stateTitleLabel);
    
    // Create the zip label
    var zipTitleLabel = document.createElement("label");
    zipTitleLabel.textContent="Zip";
    
    // Add the zip label to the zip div
    zipTitleDiv.appendChild(zipTitleLabel);

    // Create the phone label
    var phoneTitleLabel = document.createElement("label");
    phoneTitleLabel.textContent="Phone";
    
    // Add the phone label to the phone div
    phonesTitleDiv.appendChild(phoneTitleLabel);
    
    // Create the buttons label
    var buttonsTitleLabel = document.createElement("label");
    buttonsTitleLabel.textContent="Remove";
    
    // Add the buttons label to the buttons div
    buttonsTitleDiv.appendChild(buttonsTitleLabel);

    // Add the divs to the main row
    newTitleRowDiv.appendChild(cell1TitleDiv);
    newTitleRowDiv.appendChild(addressesTitleDiv);
    newTitleRowDiv.appendChild(cityTitleDiv);
    newTitleRowDiv.appendChild(stateTitleDiv);
    newTitleRowDiv.appendChild(zipTitleDiv);
    newTitleRowDiv.appendChild(phonesTitleDiv);
    newTitleRowDiv.appendChild(buttonsTitleDiv);
    
    // Create a div to hold the addresses
    var newFullAddressRowDiv = document.createElement("div");
    newFullAddressRowDiv.classList.add ('Row');
    newFullAddressRowDiv.id = 'newAddress' + newAddressAmountID;
    newFullAddressRowDiv.name = 'newAddress' + newAddressAmountID;
    
    // Create all divs that are right under the main row
    var cell1Div = document.createElement("div");
    cell1Div.style="vertical-align: middle; height:60px;";
    cell1Div.classList.add ('cell');
    cell1Div.classList.add ('cell1');
    var addressesDiv = document.createElement("div");
    addressesDiv.style="vertical-align: middle;";
    addressesDiv.classList.add ('cell'); 
    var cityDiv = document.createElement("div");
    cityDiv.style="vertical-align: middle;";
    cityDiv.classList.add ('cell');
    cityDiv.classList.add ('cityCell');
    var stateDiv = document.createElement("div");
    stateDiv.style="vertical-align: middle;";
    stateDiv.classList.add ('cell');
    stateDiv.classList.add ('stateCell');
    var zipDiv = document.createElement("div");
    zipDiv.style="vertical-align: middle;";
    zipDiv.classList.add ('cell');
    zipDiv.classList.add ('zipCell');
    var phonesDiv = document.createElement("div");
    phonesDiv.style="vertical-align: middle;";
    phonesDiv.classList.add ('cell');
    phonesDiv.classList.add ('phoneCell');
    var buttonsDiv = document.createElement("div");
    buttonsDiv.style="vertical-align: middle;";
    buttonsDiv.classList.add ('cell');
    buttonsDiv.classList.add ('buttonCell');

    // Create the cell 1 text
    var cell1Label = document.createElement("label");
    cell1Label.textContent=newAddressAmountID;
    cell1Label.id = 'cell1Label' + newAddressAmountID;    

    // Add the new number to the cell div
    cell1Div.appendChild(cell1Label);
    
    // Create the address 1 label
    var address1Label = document.createElement("label");
    address1Label.textContent="1-";
    address1Label.id = 'address1Label' + newAddressAmountID; 
    address1Label.htmlFor='address1Text';
    
    // Create the address 1 text
    var address1Text = document.createElement("input");
    address1Text.type = "text";
    address1Text.style = 'background-color: transparent; width: 90%; border: 1px dotted; padding: 2px 2px 2px 2px;';
    address1Text.id = 'address1Text' + newAddressAmountID;    
    address1Text.name = 'address1Text' + newAddressAmountID;    
    address1Text.maxLength = 150;
    address1Text.classList.add ('intext');

    // Create the address 2 label
    var address2Label = document.createElement("label");
    address2Label.textContent="2-";
    address2Label.id = 'address2Label' + newAddressAmountID;    
    address2Label.htmlFor='address2Text';
    
    // Create the address 2 text
    var address2Text = document.createElement("input");
    address2Text.type = "text";
    address2Text.style = 'background-color: transparent; width: 90%; border: 1px dotted; padding: 2px 2px 2px 2px;';
    address2Text.id = 'address2Text' + newAddressAmountID;    
    address2Text.name = 'address2Text' + newAddressAmountID;    
    address2Text.maxLength = 150;
    address2Text.classList.add ('intext');

    // Add the address row to the address div
    addressesDiv.appendChild(document.createElement("br"));
    addressesDiv.appendChild(address1Label);
    addressesDiv.appendChild(address1Text);
    addressesDiv.appendChild(document.createElement("br"));
    addressesDiv.appendChild(document.createElement("br"));
    addressesDiv.appendChild(address2Label);
    addressesDiv.appendChild(address2Text);
    addressesDiv.appendChild(document.createElement("br"));
    addressesDiv.appendChild(document.createElement("br"));
    
    // Create the city text
    var cityText = document.createElement("input");
    cityText.type = "text";
    cityText.style = 'background-color: transparent; width: 100%; border: 1px dotted; padding: 2px 2px 2px 2px;';
    cityText.id = 'cityText' + newAddressAmountID;    
    cityText.name = 'cityText' + newAddressAmountID;    
    cityText.maxLength = 75;
    cityText.classList.add ('intext');

    // Add the address row to the address div
    cityDiv.appendChild(cityText);

    // Create and add the state text combo
    stateDiv.innerHTML = getStatesAsSelectOptions('CO', 'stateText' + newAddressAmountID, true);

    // Create the zip text
    var zipText = document.createElement("input");
    zipText.type = "text";
    zipText.style = 'background-color: transparent; border: 1px dotted; padding: 2px 2px 2px 2px; width: 50px;';
    zipText.id = 'zipText' + newAddressAmountID;    
    zipText.name = 'zipText' + newAddressAmountID;    
    zipText.maxLength = 5;
    zipText.classList.add ('intext');

    // Add the zip text to the city/state/zip div
    zipDiv.appendChild(zipText);

    // Create the phone 1 label
    var phone1Label = document.createElement("label");
    phone1Label.textContent="1-";
    phone1Label.id = 'phone1Label' + newAddressAmountID;    
    phone1Label.htmlFor='phone1Text';
    
    // Create the phone 1 text
    var phone1Text = document.createElement("input");
    phone1Text.type = "text";
    phone1Text.style = 'background-color: transparent; border: 1px dotted; padding: 2px 2px 2px 2px; width: 90%;';
    phone1Text.id = 'phone1Text' + newAddressAmountID;    
    phone1Text.name = 'phone1Text' + newAddressAmountID;    
    phone1Text.maxLength = 12;
    phone1Text.classList.add ('intext');

    // Create the phone 2 label
    var phone2Label = document.createElement("label");
    phone2Label.textContent="2-";
    phone2Label.id = 'phone2Label' + newAddressAmountID;    
    phone2Label.htmlFor='phone2Text';
    
    // Create the phone 2 text
    var phone2Text = document.createElement("input");
    phone2Text.type = "text";
    phone2Text.style = 'background-color: transparent; border: 1px dotted; padding: 2px 2px 2px 2px; width: 90%;';
    phone2Text.id = 'phone2Text' + newAddressAmountID;    
    phone2Text.name = 'phone2Text' + newAddressAmountID;    
    phone2Text.maxLength = 12;
    phone2Text.classList.add ('intext');

    // Add the phone row to the phone div
    phonesDiv.appendChild(document.createElement("br"));
    phonesDiv.appendChild(phone1Label);
    phonesDiv.appendChild(phone1Text);
    phonesDiv.appendChild(document.createElement("br"));
    phonesDiv.appendChild(document.createElement("br"));
    phonesDiv.appendChild(phone2Label);
    phonesDiv.appendChild(phone2Text);
    phonesDiv.appendChild(document.createElement("br"));
    phonesDiv.appendChild(document.createElement("br"));
    
    // Create the remove answer button
    var removeBtn = document.createElement("button");
    removeBtn.textContent="X";
    removeBtn.type = "button";
    removeBtn.id = 'outRemoveBtn' + newAddressAmountID;    
    removeBtn.style = 'margin: auto 5px; align: center;';
    removeBtn.onclick = function(i) {  
        return function() {
           removeNewAddress(i);
        };
    }(newAddressAmountID);

    // Add elements to the newly created DIV tags
    buttonsDiv.appendChild(removeBtn);

    // Add the divs to the main row
    newFullAddressRowDiv.appendChild(cell1Div);
    newFullAddressRowDiv.appendChild(addressesDiv);
    newFullAddressRowDiv.appendChild(cityDiv);
    newFullAddressRowDiv.appendChild(stateDiv);
    newFullAddressRowDiv.appendChild(zipDiv);
    newFullAddressRowDiv.appendChild(phonesDiv);
    newFullAddressRowDiv.appendChild(buttonsDiv);
    
    // Add the new div tag to the main div tag
    var container = document.getElementById("newAddresses");

    // Determine if we are on the first one to add title row
    if (newAddressNumbersToAdd.length===0){
        // Add title row
        container.appendChild(newTitleRowDiv);
    }
    
    // Add address row
    container.appendChild(newFullAddressRowDiv);
    
    // Add this ID to the list
    newAddressNumbersToAdd.push(newAddressAmountID);
    
    // Get the element and change the amount of addresses
    var addressAmountDiv = document.getElementById("newAddressNumbers");
    addressAmountDiv.value=newAddressNumbersToAdd.join('|');
}

function removeNewAddress(addressToRemove){
    console.log("in removeNewAddress");

    // Get the element to remove
    var address = document.getElementById('newAddress'+addressToRemove);

    // Remove it
    address.parentNode.removeChild(address);    
    
    // Remove this from the list
    removeNewAddressNumber(addressToRemove);
    
    // Determine if the address list is empty
    var addressAmountDiv = document.getElementById("newAddressNumbers");
    if (newAddressNumbersToAdd.length===0){
        // Get the element to remove
        var addressTitleRow = document.getElementById('newAddressTitleRow');

        // Remove it
        addressTitleRow.parentNode.removeChild(addressTitleRow);    
        
        // Set it to nothing
        addressAmountDiv.value=0;
    }
    else {
        // Get the element and change the amount of addresses
        addressAmountDiv.value=createNewAddressNumberString();
    }
}

function handleAddressClick(cb){
    // Get the hidden field holding the values to remove
    var hiddenElement=document.getElementById('deleteCurrentAddresses');
    
    // Determine if the checkbox is checked
    if (cb.checked){
        // Append the value to the values to be deleted
        hiddenElement.value=hiddenElement.value+cb.value+',';
    }
    else{
        // Remove this value to be deleted
        var value=hiddenElement.value;
        hiddenElement.value=value.replace(cb.value+',', '');
    }
}



