﻿// LIST WITH US ROUTINES

    // Function to copy text value from one text box to another
    function copyText(fromElementName, toElementName){
        var fromElement = document.getElementById(fromElementName);
        var toElement = document.getElementById(toElementName);
        if (fromElement && toElement){
            toElement.value = fromElement.value;
        }
    }
    
    // Function that check array of objects on empty values
    // When one object have not empty value then return true
    function isNotEmptyFields(objectsArray){
        var result = false;
        for(var i = 0; i < objectsArray.length; i++){
            var obj = objectsArray[i];
            if (obj)
                if (obj.value != ""){
                    result = true;
                    break;
                }
        }
        return result;
    }
    
    // This function save previous Apartment information to hidden fields
    function savePreviousState(){
        
        if (addressBox2){
            addressHidden.value = addressBox2.value;
        }
        if (cityBox2){
            cityHidden.value = cityBox2.value;
        }
        if (stateBox2){
            stateHidden.value = stateBox2.value;
        }
        if (zipCodeBox2){
            zipCodeHidden.value = zipCodeBox2.value;
        }
    }
    
    // This function restore to Apartment information old info from the hidden fields
    function restorePreviousState(){
        if (addressBox2){
            addressBox2.value = addressHidden.value;
        }
        if (cityBox2){
            cityBox2.value = cityHidden.value;
        }
        if (stateBox2){
            stateBox2.value = stateHidden.value;
        }
        if (zipCodeBox2){
            zipCodeBox2.value = zipCodeHidden.value;
        }
    }
    
    // This function need to copy information from Contact Info to Apartment info
    function copyFields(){
        copyText(addressBox1.id, addressBox2.id);
        copyText(cityBox1.id, cityBox2.id);
        copyText(stateBox1.id, stateBox2.id);
        copyText(zipCodeBox1.id, zipCodeBox2.id);
    }
    
    // Occured when user click by "SameAsAbove" checkBox
    function sameAsAboveClicked(obj){
        if (obj.getAttribute("checked") == true){
            
            var objs = new Array();
            objs.push(addressBox2);
            objs.push(cityBox2);
            objs.push(stateBox2);
            objs.push(zipCodeBox2);
            
            savePreviousState();
            
            var isNotEmptyResult = isNotEmptyFields(objs);                    
            if (!isNotEmptyResult){
                copyFields();
            }
            else
                if (confirm("Would you like to overwrite previous Apartment information?")){
                    copyFields();
                }
        }
        else{
            restorePreviousState();
        }
    }


