/***********************************
** modules/tenant/includes/shoppingcart.js
**
** Matthew Story
** JS functions specific to shoppingcart page for tenant ** section of FreshDM **
** June 21, 2007
************************************/

function insertAtCursor(myField, myValue)
{
    //IE support
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == 0)
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
    }
    else
    {
        myField.value += myValue;
    }

}

String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

function appendSelectedElementsToDragElement(draggingElement) {
    var element = Selectables.lastClickedElement;
    var highlightedCampaigns = Selectables.getSelectedElements();
    if ($('dragWrapper') && !window.appendedClones) {
        for (var i=0;i<highlightedCampaigns.length;i++) {
            if (!highlightedCampaigns[i].cloneId && highlightedCampaigns[i].id != element.id) {
                var cloneElement = highlightedCampaigns[i].cloneNode(true);
                cloneElement.id = element.id + 'Clone';
                cloneElement.cloneId = highlightedCampaigns[i].id;
                $('dragWrapper').appendChild(cloneElement);
            }
        }
    }

    window.appendedClones = true;
    return true;
}

function dragStop(draggingElement) {
    window.appendedClones = false;

    return true;
}

function moveElementWithScroll(event, elementId) {
    var scrollY = (document.all) ? document.documentElement.scrollTop:window.pageYOffset;
    $(elementId).style.top = scrollY + 'px';

    return true;
}

function addCostCenterInline(orderPartId) {
	if ($(orderPartId + 'newCostCenterNameInput').value.trim() == '' || $(orderPartId + 'newCostCenterNameInput').value.trim() == '<New Cost Center>') {
		$(orderPartId + 'newCostCenterError').innerHTML = "Cost Center Name Can't Be Blank!";
		$(orderPartId + 'newCostCenterNameInput').focus();
	} else {
		var url = baseHTTPSDir + '/modules/tenant/costcenteraddaction.php';
		var urlParams = 'costCenter=' + $(orderPartId + 'newCostCenterNameInput').value.trim() + '&orderPartId=' + orderPartId;

		var AJAX = new Ajax.Request(url, {method: 'get', parameters: urlParams, onFailure: function() {alert('error!');}, onSuccess: evalAddCostCenterInlineResponse});
	}

	return true;
}

function evalAddCostCenterInlineResponse(response, json) {
	if (json.success) {
		var newOption = Builder.node('option', {value: json.costCenterId}, json.costCenter);
		if (json.orderPartId) {
			var costCenterSelects = document.getElementsByClassName('costCenterInput');
			var costCenterSelectsFocus = document.getElementsByClassName('costCenterInputFocus');
			for (var i=0;i<costCenterSelectsFocus.length;i++) {
				costCenterSelects.push(costCenterSelectsFocus[i]);
			}
			for (var i=0;i<costCenterSelects.length;i++) {
				var cloneOption = newOption.cloneNode(true);
				costCenterSelects[i].appendChild(cloneOption);
				if (parseInt(costCenterSelects[i].id) == json.orderPartId) {
					cloneOption.selected = 'selected';
				}
			}
		} else {
			$('costCenterSelect').appendChild(newOption);
			newOption.selected = 'selected';
			$('costCenterSelect').focus();
			$('newCostCenterNameInput').value = '<New Cost Center>';
		}
	} else {
		json.orderPartId = (json.orderPartId) ? json.orderPartId:'';
		$(json.orderPartId + 'newCostCenterError').innerHTML = json.errorMessage;
		if ($(json.orderPartId + 'newCostCenterWrapper').style.display == 'none') {
			Effect.toggle(json.orderPartId + 'newCostCenterWrapper', 'appear');
			$(json.orderPartId + 'addCostCenterA').innerHTML = 'Hide';
		}
		$(json.orderPartId + 'newCostCenterNameInput').focus();
	}
}
