function searchInIFrame(inline, iFrameId, recipeId, multiAdd, form) {
    var freetext = form.freetext.value;
    var searchType = getRadioValue(form.searchType);
    var loc = '';
    if (searchType == 4) {
        freetext = escape(freetext.length > 0 ? freetext : '*');
        loc = addCategory('/mynd/foods-' + freetext + '000000000000000000000.html?iframe=true');
    } else {
        loc = '/foods/myfoods?iframe=true&search_type=' + searchType + '&freetext=' + freetext;
        if (recipeId && recipeId > -1) {
            loc += '&food_id=' + recipeId;
        }
    }
    if (multiAdd) {
        loc += '&multiAdd=true&result_view=iframeMultiAddContextResults';
    }
    if (inline) {
        document.location = loc;
    } else {
        iFrame = document.getElementById(iFrameId);
        iFrame.src = loc;
    }
    return false;
}

function hasFoodsSelected() {
    var foodsListField = document.trackingSearchForm.foodsListStr;
    return foodsListField.value.length > 0;
}

function getRadioValue(radio) {
    for (i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return radio[i].value;
        }
    }
    return null;
}

function addRecipeIngredient(foodId, measureIndex, quantity) {
    var targetForm = parent.document.forms['recipeForm'];
    targetForm.addIngredientCommand.value = foodId + '|' + measureIndex + '|' + quantity;
    //todo get rid of this
    targetForm.targetAction.value = 'edit';
    targetForm.submit();
}

function removeRecipeIngredient(foodId, targetForm) {
    targetForm.removeIngredientCommand.value = foodId;
    //todo get rid of this
    targetForm.targetAction.value = 'edit';
    targetForm.submit();
}

function clearQuantities(form) {
    for (i = 0; i < form.elements.length; i++) {
        if (form.elements[i].id == 'quantity') {
            form.elements[i].value = '';
        }
    }
}

function saveFoodsSuccess() {
    window.location = '/foods/myfoods?iframe=true&search_type=2&result_view=iframeMultiAddContextResults&clearSelections=true';
}

var secondSubmit = false;

function addToTracking() {
    if(secondSubmit){
        return;
    }
    var parentForm = parent.document.trackingSearchForm;
    var foodsStr = parentForm.foodsListStr.value;
    if(foodsStr.length == 0){
        parent.window.location.reload();
        return;
    }
    secondSubmit = true;
    jQuery.ajax({
    	type: "POST",
        url: "/mynd/mytracking/addtotracking?foodsListStr=" + foodsStr,
        data: "{}",
        complete: function(){ parent.window.location.reload(); }
    });

}

function formModified(targetForm){
    for (var i=0; i < targetForm.elements.length; i++) {
        var field = targetForm.elements[i];
        if (field.type == 'checkbox' || field.type == 'radio') {
            if(field.checked != field.defaultChecked){
                return true;
            }
        } else if (field.type == 'hidden' || field.type == 'password' || field.type == 'text' || field.type == 'textarea') {
            if(field.value != field.defaultValue){
                return true;
            }
        } else if (field.type == 'select-one' || field.type == 'select-multiple') {
            for (var j=0;  j < field.options.length; j++) {
                if (field.options[j].selected && !field.options[j].defaultSelected) {
                    return true;
                }
            }
        }
    }
    return false;
}

function uncheckIframeCheckBoxes(iframeId){
    var iframe = document.getElementById(iframeId);
    var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
    var targetForm = iframeDoc.searchResultsForm;
    if(targetForm){
        for (var i=0; i < targetForm.elements.length; i++) {
            var field = targetForm.elements[i];
            if (field.type == 'checkbox') {
                field.checked = false;
            }
        }
    }
}

function confirmOpenAddToTrackingModal(form){
    if(form && formModified(form)){
        jQuery('#confirmDirty').jqmShow();
    } else {
        openAddToTrackingModal();
    }
}

function openAddToTrackingModal(){
    jQuery('#ex2').jqmShow();
    var iframe = document.getElementById('myndResults_multi');
    iframe.src = '/foods/myfoods?iframe=true&search_type=2&result_view=iframeMultiAddContextResults&clearSelections=true';           
    if ((jQuery.browser.msie) && (jQuery.browser.version.indexOf("7") != -1)) {
        jQuery('.chart').css({display: 'none'})
    }
}

function confirmCloseAddToTrackingModal(){
    if(hasFoodsSelected()){
        jQuery('#confirmClose').jqmShow();
    } else {
        closeAddToTrackingModal();
    }
}

function closeAddToTrackingModal(){
    jQuery('#ex2').jqmHide();
    if ((jQuery.browser.msie) && (jQuery.browser.version.indexOf("7") != -1)) {
        jQuery('.chart').css({display: 'block'})
    }
}