// cnav.js
function cpContractPal()
{
    this.getForm = function() {return document.forms["cp-page-form"];}
    this.ajaxActionElement=null;
}

cpContractPal.prototype.handleAction=function(action,conf,ajaxTarget,ajaxHandler,validator,elementReference)
{
// ajaxHandler is not used
    if (conf!="")
    {
        if (!confirm(conf))
        {
            return;
        }
    }

    var f = this.getForm();
    if (validator!=null && !validator=="")
    {
        if (this.handleValidation(f,validator)==false)
        {
            return;
        }
    }
    if (ajaxTarget!="")
    {
        if(elementReference && elementReference.style)
        {
            elementReference.disabled = true;
            this.ajaxActionElement = elementReference;
        }
        else
        {
            this.ajaxActionElement = null;
        }
        this.sendAjaxForm(f,action,ajaxTarget);
        return;
    }

    this.sendAction(action);

}

cpContractPal.prototype.sendAjaxForm = function(f,action,ajaxTarget)
{
    this.loadForm(f);
    f.elements["cp-doaction"].value=action;
    var request = new AjaxRequest("RunWebAjax.do","ContractPal.handleAjaxResponse");
    request.addForm(f);
    request.setProperty("target",ajaxTarget);
    request.send();
}

cpContractPal.prototype.sendAction=function(action)
{
    if (this.lockButton==true)
    {
        return;
    }
    if (!this.lockRequest())
    {
        return;
    }

    var f = this.getForm();
    this.loadForm(f);
    f.elements["cp-doaction"].value=action;
    this.lockButton = true;
    f.submit();
}

cpContractPal.prototype.lockRequest=function()
{
    if (this.requestPending)
    {
        return false;
    }
    this.requestPending=true;
    return true;
}

cpContractPal.prototype.unLockRequest=function()
{
    this.requestPending=false;
}

cpContractPal.prototype.addElementToPost=function(id,value)
{
    var f = this.getForm();
    this.addElement(f,id,value);
}

cpContractPal.prototype.addElement=function(f,name,value)
{
    if (name==null || name=="")
    {
        return;
    }
    var newElement = document.createElement("input");
    newElement.setAttribute("type","hidden");
    newElement.setAttribute("name",name);
    newElement.setAttribute("value",value);
    newElement.setAttribute("cp-trash","true");
    f.appendChild(newElement);
}

cpContractPal.prototype.loadForm=function(f)
{
    // clear the form
    var els=f.elements;
    var n=els.length;
    for (var i=n-1;i>=0;i--)
    {
        if (els[i].getAttribute("cp-trash")!=null)
        {
            els[i].parentNode.removeChild(els[i]);
        }
    }

    this.addElements(f,document.getElementsByTagName("input"));
    this.addElements(f,document.getElementsByTagName("select"));
    this.addElements(f,document.getElementsByTagName("textarea"));

}

cpContractPal.prototype.addElements=function(f,els)
{
    var n = els.length;
    for (var i=0;i<n;i++)
    {
        if (els[i].name && els[i].name=="cp-doaction")
        {
            continue;
        }
        if (els[i].type && els[i].type=="radio")
        {
            if (els[i].checked)
            {
                this.addElement(f,els[i].name,els[i].value);
            }
        }
        else if (els[i].type && els[i].type=="checkbox")
        {
            if (els[i].checked)
            {
                this.addElement(f,els[i].name,els[i].value);
                this.addElement(f,"cp-checkbox-"+els[i].name,"cp-on");
            }
            else
            {
                this.addElement(f,"cp-checkbox-"+els[i].name,"cp-off");
            }
        }
        else
        {
            this.addElement(f,els[i].name,els[i].value);
        }
    }
}

cpContractPal.prototype.handleAjaxResponse=function(response)
{
    if(this.ajaxActionElement && this.ajaxActionElement.style)
    {
        this.ajaxActionElement.disabled = false;
    }

    this.unLockRequest();

    var target = response.getProperty("target");
    var o = response.getJSON();

    if (o==null)
    {
        if (document.getElementById("debugWrapper")!=null || this.getForm().getAttribute("cp-test")=="true")
        {
            alert("Server failed to return a valid response");
            alert(response.getText());
        }
        return;
    }
    target = (o.data.target?o.data.target:target);
    var h = o.data.html;
    if (target!=null && target!="" && document.getElementById(target)!=null && h!=null)
    {
        var targetElement = document.getElementById(target);
        if (targetElement.value) {targetElement.value=h;}
        else {targetElement.innerHTML=h;}
    }

    var js = (o.data.javascript?o.data.javascript:null);
    var ajs = (o.data.ajavascript?o.data.ajavascript:null);
    var dbg = (o.data.debugInfo?o.data.debugInfo:null);
    var views = (o.data.views?o.data.views:null);

    if (dbg!=null)
    {
        if (document.getElementById("debugWrapper")!=null)
        {
            document.getElementById("debugWrapper").innerHTML=dbg;
        }
    }
    if (js!=null) {eval(js);}

    if (views!=null)
    {
        var list = views.targets;
        if (list!=null)
        {
            var targets = list.split(",");
            for (var i=0;i<targets.length;i++)
            {
                if (document.getElementById(targets[i])!=null)
                {
                    h = eval("views."+targets[i]);
                    if (h!=null)
                    {
                        document.getElementById(targets[i]).innerHTML=h;
                    }
                }
            }
        }
    }

    if (ajs)
    {
        try
        {
            eval(ajs);
        }
        catch (ex)
        {
            if (this.test)
            {
                throw(ex);
            }
        }
    }
}

cpContractPal.prototype.handleValidation=function(f,validator)
{
    var result = false;
    var expression = "result="+validator+"(f)";
    eval(expression);
    return (result==true || result=="true");
}

cpContractPal.prototype.callAjaxHandler=function(script)
{
    try
    {
        eval(script);
    }
    catch (e)
    {
        if(this.test)
        {
            alert("Error running Ajax Handler script: "+script);
        }
    }
}



var ContractPal = new cpContractPal();

