/*********************************************************************
'***    Function: makeAjaxRequest( strPlaceHolderElemId, strURL, strParams, strNoResultMsg, strWaitMsg )
'***    Description: make ajax call
'***
'***    Parameters: strPlaceHolderElemId, - the element where result is be displayed
                    strURL, - the URL to make get 
                    strParams, - query string parameters to append to URL
                    strNoResultMsg, - message to display if result fails, can be an image
                    strWaitMsg - message to display while waiting, can be an image
'*** 
'***    Returns: result (HTML) from called URL
'***    Remarks: none
'***
'***    Created by: niloa
'***    Changed by: niloa
'***    Last change: 12/12/2007
'*********************************************************************/
function makeAjaxRequest( strPlaceHolderElemId, strURL, strParams, strWaitMsg, strResponseMsg, strNoResultMsg ){
    var strNoResult = ( ( strNoResultMsg==null ) ? "" : strNoResultMsg );

    //get the element where response will be placed
    var objPlaceHolder = $(strPlaceHolderElemId);
    
    strURL = new String( strURL );
    strParams = new String( strParams );
    
    //set defaults
    strWaitMsg = ( strWaitMsg == null ) ? "" : strWaitMsg;
    strResponseMsg = ( strResponseMsg == null ) ? "" : strResponseMsg;

    //append ? if none exists
    strURL = ( ( strParams.search(/(\?)/gim) > -1 ) ? strURL.concat( strParams ) : ( ( strURL.search(/(\?)/gim) > -1 ) ? strURL.concat( strParams ) : strURL.concat( "?", strParams ) ) );

    //set wait message
    objPlaceHolder.innerHTML = strWaitMsg;

    new Ajax.Request(strURL, { method:'get',
        onSuccess: function(transport){
            objPlaceHolder.innerHTML = ( strResponseMsg != "" ) ? strResponseMsg : transport.responseText;
        },
        onFailure: function(){
            objPlaceHolder.innerHTML = strNoResult;
        }
    });
}

/*********************************************************************
'***    Function: getAjaxData
'***    Description: make ajax call
'***
'***    Parameters: intDirection ("1" - move next;   "-1" - move right)
'*** 
'***    Returns: result (HTML) 
'***    Remarks: none
'***
'***    Created by: rburdan
'***    Changed by: 
'***    Last change: 08/22/2008
'*********************************************************************/
function getAjaxData(intDirection, strURL){
    var objID = null;
    
    var strHtml = "";
    var strID   = "";
    var strParams = "";
    
    var strPlaceHolderElemId  = "div_top_companies";
    
    var strDate = new Date();
    var strFirst_Last = "last";
    
    if(intDirection != null && intDirection == -1){
        strFirst_Last = "first";
    }
    
    if (eval('document.getElementById(strFirst_Last + "_key_field_ID")')) {
        objID = document.getElementById(strFirst_Last + "_key_field_ID");
        strID = objID.innerHTML;
        
        strParams = "?id=" + strID + "&dr=" + intDirection + "&time=" + strDate;
        strHtml = makeAjaxRequestEx(strPlaceHolderElemId, strURL, strParams);
    }
}

/*********************************************************************
'***    Function: makeAjaxRequest( strPlaceHolderElemId, strURL, strParams, strNoResultMsg, strWaitMsg )
'***    Description: make ajax call
'***
'***    Parameters: strPlaceHolderElemId, - the element where result is be displayed
                    strURL, - the URL to make get 
                    strParams, - query string parameters to append to URL
                    strNoResultMsg, - message to display if result fails, can be an image
                    strWaitMsg - message to display while waiting, can be an image
'*** 
'***    Returns: result (HTML) from called URL
'***    Remarks: none
'***
'***    Created by: niloa
'***    Changed by: rburdan
'***    Last change: 08/22/2008
'*********************************************************************/
function makeAjaxRequestEx( strPlaceHolderElemId, strURL, strParams, strWaitMsg, strResponseMsg, strNoResultMsg ){

    var strNoResult = ( ( strNoResultMsg==null ) ? "" : strNoResultMsg );

    //get the element where response will be placed
    var objPlaceHolder = document.getElementById(strPlaceHolderElemId);
    
    strURL = new String( strURL );
    strParams = new String( strParams );

    //set defaults
    strWaitMsg = ( strWaitMsg == null ) ? "" : strWaitMsg;
    strResponseMsg = ( strResponseMsg == null ) ? "" : strResponseMsg;

    //append ? if none exists
    strURL = ( ( strParams.search(/(\?)/gim) > -1 ) ? strURL.concat( strParams ) : ( ( strURL.search(/(\?)/gim) > -1 ) ? strURL.concat( strParams ) : strURL.concat( "?", strParams ) ) );

    //set wait message
    ///objPlaceHolder.innerHTML = strWaitMsg;
    

    new Ajax.Request(strURL, { method:'get',
        onSuccess: function(transport){
            objPlaceHolder.innerHTML =  transport.responseText;
        },
        onFailure: function(){
            objPlaceHolder.innerHTML = strNoResult;
        }
    });

}


/*********************************************************************
'***    Function: makeAjaxRequestToSaveData( strPlaceHolderElemId )
'***    Description: make ajax call
'***
'***    Parameters: 
'***               strURL, - the URL to make get 
'*** 
'***    Returns: 
'***    Remarks: none
'***
'***    Created by: rburdan
'***    Changed by: rburdan
'***    Last change: 09/04/08
'*********************************************************************/
function makeAjaxRequestToSaveData(strURL){
    new Ajax.Request(strURL, { method:'get' });
}

    
/*********************************************************************
'***    Function: AjaxGet
'***
'***    Parameters: 

			url - url to request
			resultId - id where result is shown (must be visible)
			errorMessage - custom error message in case of http error

'***    Returns: void
'***	Remarks: requires reference to prototype.js
'***
'***    Created by: dimab
'***    Changed by: 
'***    Last change: 14/03/2009
'*********************************************************************/
function AjaxGet( url, resultId, errorMessage ) {
	if (errorMessage == null)
		errorMessage = "error";
		
	new Ajax.Request( url,
		{ method:'get',	
			onSuccess: function(transport) {
				var e = document.getElementById(resultId);
				if (e != null)
					e.innerHTML = transport.responseText;
			},
			onFailure: function() {
				var e = document.getElementById(resultId);
				if (e != null)
					e.innerHTML = errorMessage;
			}
		});
}

/*********************************************************************
'***    Function: AjaxGetWithCallback
'***
'***    Parameters: 
        strURL      - url to issue ajax get
        containerId - elem to update with respnse result
        msgErr      - error msg to set to element if op. failed
        onSuccessFunc - callback func. if op. is successful
'***
'***    Returns: void
'***    Remarks: 
'***    - get all current resume's experience items
'***
'***    Created by: niloa
'***    Changed by: niloa
'***    Last change: 03/15/2009
'*********************************************************************/
function AjaxGetWithCallback(strURL, containerId, msgErr, onSuccessFunc) {
    msgErr = (msgErr == null) ? "request failed!" : msgErr;

    new Ajax.Request(strURL, { method: 'get',
        onSuccess: function(transport) {
            onSuccessFunc(transport);
        },
        onFailure: function() {
            //set error message
            var el = document.getElementById(containerId);
            if (el != null)
                el.innerHTML = msgErr;
        }
    });
}

/*********************************************************************
'***    Function: AjaxFormSubmit(
'***
'***    Parameters: 
        formID          - form element id submitting the data
        onCompleteFunc  - func ref. to call when op. is complete
        onSuccessFunc   - func ref. to call when op. is successful
        onFailureFunc   - func ref. to call when op. failed
'***
'***    Returns: void
'***    Remarks: 
        - serializes elements as <name> : <value> page
        - submits url on form.action, or current url empty
        - calls func ref as callback based on response state
        - depends on prototype.js
'***
'***    Created by: niloa
'***    Changed by: niloa
'***    Last change: 03/17/2009
'*********************************************************************/
function AjaxFormSubmit(formID, onCompleteFunc, onSuccessFunc, onFailureFunc) {
    //build options with callback functions
    var options = {
        //call call back when operation is complete
        onComplete: function(transport) {
            onCompleteFunc(transport);
        },
        //call call back function if operation is sucessful
        onSuccess: function(transport) {
            onSuccessFunc(transport);
        },
        //call call back function if operation failed
        onFailure: function(transport) {
            onFailureFunc(transport);
        }
    };

    //call form request which serializes form elements as {<name> : <value>} pair
    //and submits to form action url, if no action url is present it uses current url
    $(formID).request(options);
}
