/*
 * file - master.js
 * helps with catalog ajax stuff
 */ 
 
// ==================================================================
//  files needed to process
// ==================================================================
// ---------------------------- Start Custom -------------------------- //

function FormClear(i,j)
{
    objForm = document.forms['CustomerForm'];
    objFormElement = objForm.elements[i];
    if(objFormElement.value==j) {objFormElement.value='';}
}


function statusUpdate(theButton, fullUrl) {

    debug = false;
    if (debug) { alert(theButton.className +':'+ theButton.id +':'+ theButton.name ); }
    var specialText = theButton.className;
    
    // Get the current id from the status select that was changed.
    var uid = specialText.replace(/-.*$/, "");
    var msgBox = document.getElementById( uid + '-status');
    // Prepare the edit params and url to the ajax handler.
    var params = 'Catalog_EditPost?todo=status_edit&uid='+uid;
    
    // IN PROGRESS
    msgBox.innerHTML = 'Working...';
    
    if (debug)  { alert('about to update transaction '+uid);  }


    // FULL URL SHOULD BE FROM MODX http://www.wardjet.com/ ending with a slash
    if (debug) { alert('post to handler; ' + fullUrl + params); }


    var ajaxRequest = new Ajax(fullUrl + params, {
        method: 'post', 
        onComplete: function(response) { msgBox.innerHTML = response; }
    }).request();

    if (debug) { alert('Done with ajax on '+ msgBox.innerHTML); }
    
    // False keeps button / link click from happening
    // True will reload page - we DO NOT want that to happen.
    return false;
}

var msgBoxName = 'ajaxPingBack';

function callAjax(action,type)
{
    var debug = false;
    msgBox = document.getElementById(msgBoxName);
    

    var handler;
    var params = '';    
    switch (type)
    {
        case 'User':
            handler='ajax_customer'; 
            if (action == 'UpdatePasswd')
            {
                params += 'OldPasswd=' + document.getElementById('OldPasswd').value + '&';
                params += 'Passwd=' + document.getElementById('Passwd').value + '&';
                params += 'PasswdAgain=' + document.getElementById('PasswdAgain').value + '&';
            }
            else if (action == 'UpdateEmail')
            {
                params += 'NewEmail=' + document.getElementById('NewEmail').value + '&';
            }
        break;
        case 'User1':   handler='ajax_customer'; break;
        case 'User2':   handler='ajax_customer'; break;
        case 'User3':   handler='ajax_customer'; break;
        default:        handler='NoHandle';
    }

    msgBox.innerHTML = "Working . . ";
    params += 'action=' + action;
    if (debug) { alert(params); }
    
    if (handler != 'NoHandle')
    {


        var ajaxRequest = new Ajax(handler, { 
            method: 'post',
            postBody: params,
            onComplete: function(response) { msgBox.innerHTML = response; }
        }).request();
    
    }
    else
        { msgBox.innerHTML = 'Cannot handle request ' + type; }

    return false;
}

function clearMsgBox()
{
    document.getElementById(msgBoxName).innerHTML = "";
}

function removeItem(uid,uType){
    if (uid > 0)
    {
        var boxToRemove = document.getElementById('used_box_' + uid);
        var params = 'ruid=' + uid + '&action=delete&action_type=' + uType;
        var oldDataz = boxToRemove.innerHTML;
        // post method
        // post variables - theres a function somewhere
        
        var qxRequest = new Ajax('usedwaterjets_test',
        {
            method: 'get',
            postBody: params,
            onComplete: function(response) {
                //boxToRemove.innerHTML = response;                 boxToRemove.innerHTML = "Working . . .";    
                        
                window.setTimeout( function () { 
                    if (response)
                    {
                        document.getElementById('admin_header_' + uid).innerHTML = '';  
                        boxToRemove.innerHTML = "";
                    }
                    else
                        boxToRemove.innerHTML = oldDataz;
                }, 400);    
                // update box with status   
                // kill time                                        
                // remove box               
            }}              
        ).request();    
    }
    return false;
}

function addDetailBox()
    {
        var num = 1; // thats the original amount of details
        while (document.getElementById("detail-" + (++num) + "-a") != null)
        {}
        
        var newSpan = document.createElement("span");
        var link = document.getElementById("addAnother");
        //var insertBox = document.getElementById("water_details");
        //newSpan.id = "detail-"+num;
        link.parentNode.insertBefore(newSpan, link);
        //insertBox.insertBefore(newSpan, link);

        // now we should have a good number
        newSpan.innerHTML= "\r\n<b>Title:</b><input type=\"text\" name=\"detail-"+num+"-a\" id=\"detail-"+num+"-a\"  > </input><br /> "+
"       <b>Detail:</b><input type=\"text\" name=\"detail-"+num+"-b\" id=\"detail-"+num+"-b\" > </input><br />";
        
    }
    
// ---------------------------- End Custom -------------------------- //

