function overlayPopup(id, action) {
    if(action == 'show'){
	    var widthHeight = $(id).getDimensions();
    	$(id).setStyle({
    	    visibility: 'visible',
            display: 'inline',
            top: -(widthHeight.height + 3) + 'px'
    	});
    }
    else{
        $(id).hide();
    }
}
  

function getPaymentForm(paymentTypeId){
    showFormDiv(paymentTypeId);
}

function showFormDiv(divToShow){
	$(divToShow).show();
	hideOtherForms(divToShow);
}

function hideOtherForms(divToShow){
	
	var poId = 'poPayment';
	var checkId = 'checkPayment';
	var credit = 'ccPayment';
		
	switch(divToShow){
		// timeout in each case is set to persist focus, and prevent ie from popping an error
		case poId:
		    $(checkId, credit).invoke('hide'); 
		    try{
		        setTimeout(function(){ Field.focus('poNumber'); }, 0);		   
		    }
		    catch(er){
		    	//igoring; clicking too fast b/w tabs in ie shows an error
		    }
		break;
		
		case checkId:
		    $(poId, credit).invoke('hide');
		break;
		
		case credit:
		    $(poId, checkId).invoke('hide');
		break;
	}
}

function toggleClass(id){
    if($(id).hasClassName('descending')){
    	$(id).removeClassName('descending');
    	$(id).addClassName('ascending');
    }
    else{
    	if($(id).hasClassName('ascending')){
    	    $(id).removeClassName('ascending');
    	    $(id).addClassName('descending');
        }
    }
}

function sort(tableToSort, fieldToSort){
	var direction = null;
	if($(fieldToSort).hasClassName('descending')){
		direction = 'desc';
	}
	else{
		direction = 'asc';
	}
	var url = "index.php";
    var pars = 'module=KITraining&action=sort&direction='+direction+'&field='+fieldToSort;
    var myAjax = new Ajax.Request(
    		url,
    		{
    			method: 'get',
    			parameters: pars,
    			onSuccess: function(transport) {
    				var fullText = transport.responseText;

    				var start = (transport.responseText.indexOf("<tbody id=\""+tableToSort+"\">") + (("<tbody id=\""+tableToSort+"\">").length));
				    var end = transport.responseText.indexOf("</tbody><!DOCTYPE");

				    var subText =fullText.substring(start,end);


    				$(tableToSort).update(subText);

    			}
    		}    		
    	)
}

