﻿// JScript File

AutoPopulate = {
LoadForm: function(form,cookieName)
{
    var i;
    if (form.id != null && form.id != "") {
        var cookie = $.cookie(cookieName);
        if (cookie != null)
        {    
            var vals = cookie.split('&');
            var valmap = {};
            for( i=0;i<vals.length;i++)
            {
                var nv = vals[i].split('=');
                if (nv != null && nv.length > 1)
                {
                    valmap[nv[0]] = unescape(nv[1]);
                }
            }
            $('input',form).each(function(i) {
                var e = this;
                if (e.type != 'hidden' && e.type != 'file')
                {
                    var n = e.id;
                    var v = valmap[e.id || e.name];
                    this.value=v;
                }
            });
            $('select',form).each(function(i) {
                var e = this;
                if (e.type != 'hidden' && e.type != 'file')
                {
                    var v = valmap[e.id];
                    for(var i=0;i<this.options.length;i++)
                    {
                        if (this.options[i].value==v)
                        {
                            this.options[i].selected = true;
                            this.selectedIndex=i;
                        }
                    }
                }
            });
        }
    }
},
LoadValue: function(element,formId,valueId)
{
    element = $(element);
    var i;
    if (formId!=null && formId != "") {
        var cookie = Tremor.Util.GetCookie("formValues:"+formId);
        if (cookie != null)
        {    
            var vals = cookie.split('&');
            var valmap = {};
            for( i=0;i<vals.length;i++)
            {
                var nv = vals[i].split('=');
                if (nv != null && nv.length > 1 && nv[0]==valueId)
                {
                    element.innerHTML = unescape(nv[1]);
                    return;
                }
            }
        }
    }
},
Save: function(form,cookieName)
{
    var values = "";
    $('input',form).each(function(i) {
        var e = this;
        if (e.type != 'hidden' && e.type != 'file')
        {
            var n = e.id || e.name;
            values = values+"&"+n +"="+escape(this.value);
        }
    });
    $('select',form).each(function(i) {
        var e = this;
        if (e.type != 'hidden' && e.type != 'file')
        {
            var n = e.id || e.name;
            values = values+"&"+n +"="+escape(this.options[this.selectedIndex].value);
        }
    });
    $.cookie(cookieName,values,{expires:30});
}
}


function GetForm(e)
{
    while(e != null && e.tagName != "FORM")
    {
        e = e.parentNode;
    }
    return e;
}

 $(document).ready(function(){
    
    $('.autoPopulate').each(function (i) {
        var form = GetForm(this);
        var cookieName = "formValues-"+this.id;
        if (form != null)
        {
            AutoPopulate.LoadForm(form,cookieName);
            $('input',form).click(function(i) 
              {AutoPopulate.Save(GetForm(this),cookieName);}
              );
         
        }
     });
      
    $('input','#roomAndBoardRow').click(function(i) { 
        var enabled = this.value == 0;
        $('input','#mealPlanInputs').each(function(i) {
            this.disabled = enabled;// ? "disabled" : null;
            });
    });
    $('input','#mealPlanInputs').each(function(i) { 
        var enabled = true;
        $('input','#roomAndBoardRow').each(function(i)
                        {
                            if (this.value=='0' && this.checked)
                            {
                                enabled = false;
                            }
                        });
            this.disabled=!enabled;
    });
    
});
