// Javascript library for core common functions
// To be reused by other higher level Javascript files
// Author: Paul


/**
 * Add a key value pair to a parameter line.
 * @param {String} parameterLine parameter string
 * @param {String} key key name to be added
 * @param {String} value for the key
 */
function paramAddKeyValuePair(parameterLine, key, value) {

    var params = parameterLine;
    if (params != null && params != '') {
        params = params + '&';
    }
    return params + paramMakeKeyValuePair(key, value);
}


/**
 * Create a key value pair parameter line.
 * @param {String} key key name to be added
 * @param {String} value for the key
 */
function paramMakeKeyValuePair(key, value) {

    return key + '=' + escape(value);
}

/**
 * Create 2 key value pairs on a parameter line.
 * @param {String} key key name to be added
 * @param {String} value data value
 * @param {String} key2 second key name to be added
 * @param {String} value2 second value
 */
function paramMakeKeyValuePairs(key, value, key2, value2) {
    var params1 = paramMakeKeyValuePair(key, value);
    var params2 = paramMakeKeyValuePair(key2, value2);

    if (params1 != null && params1 != '' && params2 != null && params2 != '') {
        params1 = params1 + '&'; //for joining next parameter 
    }
    return params1 + params2;
}


/**
 * Forms Ajax request when a key is pressed.
 * @param {String} url the next url to be called
 * @param {String} params the parameters passed to the next url
 * @param {String} func the function called when ajax processed - to reflect
 *       back to the browser the updated state.
 */
function keyPressed(url, params, func) {
    //console.log("keyPressed - url: " + url);
    //console.log("params: " + params);

    var myAjax = new Ajax.Request(
        url, {
            method: 'get', 
            parameters: params, 
            onComplete: func
        }); 
} 


/**
 * Function called after checked validation of the field data.
 * @param {String} originalRequest When the field KeyPressed AJAX call returns
 * this is the text returned from the server.
 * @param {String} sectionId id to go into the return html snippet
 * @param {String} itemClass class to go into the return html snippet
 * @param {String} imageId id of the image to go into the return html snippet
 * @param {String} graphicPathSuccess success image name and path
 * @param {String} graphicPathFailure failure image name and path
 */
function fieldChange(originalRequest, sectionId, itemClass, imageId,
        graphicPathSuccess, graphicPathFailure) {

    // console.log("return: " + originalRequest.responseText);
    var JSONResponse = originalRequest.responseText;
    var newReturn = eval('(' + JSONResponse + ')');

    var imageInsert = ajaxMakeImageResponse(imageId, newReturn.message,
        newReturn.valid, graphicPathSuccess, graphicPathFailure);
    
    //remove the dynamic image
    ajaxRemoveFeedback(imageId);

    if (!newReturn.blank) {
        //re-insert the dynamic image, if not blank (hidden icon state)
        ajaxInsertFeedback(imageInsert, sectionId, itemClass);
    }
}



/**
 * Function to make an image (html) as a response from the given parameters.  
 * @param {String} imageId id of the image to go into the html snippet
 * @param {String} imageMessage message text to go into the image
 * @param {String} valid true/false used for which image to display
 * @param {String} graphicPathSuccess success image name and path
 * @param {String} graphicPathFailure failure image name and path
 * @return {String} html snippet of the constructed image
 */
function ajaxMakeImageResponse(imageId, imageMessage, valid, graphicPathSuccess,
        graphicPathFailure) {

    var imageInsert = '';

    if (graphicPathSuccess != null && graphicPathSuccess != ''
            && graphicPathFailure != null && graphicPathFailure != '') {

        //make image
        imageInsert = '<img id="' + imageId + '" title="' + imageMessage + '" '; 
        
        if (valid == true) {
            // console.log("change valid");
            imageInsert = imageInsert + ' src="' + graphicPathSuccess + '" />';
        } else {
            // console.log("change fail");
            imageInsert = imageInsert + ' src="' + graphicPathFailure + '" />';
        }
    }

    return imageInsert;
}


/**
 * Function to remove previous dynamic Ajax feedback.
 * @param {String} itemId unique id of the feedback item (typically an image)
 */
function ajaxRemoveFeedback(itemId) {

    if (itemId != null && itemId != '') {

        //remove the dynamic feedback item (eg Ajax image tick/cross)    
        var feedback = $(itemId);
        if (feedback != null) {
            feedback.remove();
        }
    }
}


/**
 * Function to insert the dynamic Html screen part.
 * @param {String} htmlPart section of the html to insert.
 * @param {String} sectionId id of the section to find the items.
 * @param {String} itemClass class of the items where insertion will occur.
 */
function ajaxInsertFeedback(htmlPart, sectionId, itemClass) {

    if (htmlPart != null && htmlPart != ''
            && itemClass != null && itemClass != '') {

        //re-insert the dynamic image
        $A($(sectionId).getElementsByClassName(itemClass)).each(
            function(item) {
                new Insertion.After(item, htmlPart);
            }
        )
    }
}



