
// store variables to control where the edit code popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var xOffset = 1;
var yOffset = 1;

var lastTag = null;
var Code = "";
var Service = "";
var StatusID = "";
var newStatus = "";
var NetID = "";
var active = false;

function showEditMenu(e) {
    if (!e) var e = window.event;
    if (e) {
        // First of all get the page scroll lengths

        var newX, newY;

        if (e.pageX || e.pageY)
        {
        	newX = e.pageX+xOffset;
        	newY = e.pageY+yOffset;
        }
        else if (e.clientX || e.clientY)
        {
            var scrx,scry;
            if (self.pageYOffset) // all except Explorer
            {
            	scrx = self.pageXOffset;
            	scry = self.pageYOffset;
            }
            else if (document.documentElement && document.documentElement.scrollTop)
            	// Explorer 6 Strict
            {
            	scrx = document.documentElement.scrollLeft;
            	scry = document.documentElement.scrollTop;
            }
            else if (document.body) // all other Explorers
            {
            	scrx = document.body.scrollLeft;
            	scry = document.body.scrollTop;
            }

        	newX = e.clientX + scrx+xOffset;
        	newY = e.clientY + scry+yOffset;
        }

        swapLayer('editCodeDIV', true); // and make it visible
    	moveObject("editCodeDIV", newX, newY);
	}
}

function initData(tag) {
    var info = "";
    // get the hidden values for this cell
    if (tag.getElementsByTagName('input')) {
        info = tag.getElementsByTagName('input').namedItem("info").value;
        var infoArr = info.split(',');
        Code = infoArr[0];
        Service = infoArr[1];
        StatusID = infoArr[2];
        NetID = infoArr[3];
        lastTag = tag;
    }
	// change menu so the current status is selected
    if (getDocObject("status")) {
        getDocObject("status").selectedIndex = StatusID-1;
    }
}

function editStatus(event, tag) {
    var info = "";
    var hasOwner = 0;
    var Code = "";
    if (tag.getElementsByTagName('input')) {
        info = tag.getElementsByTagName('input').namedItem("info").value;
        var infoArr = info.split(',');
        var Code = infoArr[0];
        hasOwner = infoArr[4];
    }
    /*if(hasOwner == 0){
        alert("Status change not allowed for code "+Code+"!");
        return false;
    }*/
    showEditMenu(event);
    initData(tag);
    return true;
}

function replaceImage(objid, newSrc) {
    getDocObject(objid).setAttribute('src', newSrc);
    getStyleObject(objid).visibility = "visible";
}

function closeEditWindow() {
    swapLayer('editCodeDIV', false);
}

// AJAX functions below.
function saveStatusEdit(select) {
    // confirm the change first.
    if (confirm("Are you sure you wish to change the status of this code?")) {
        // check the last edit has been saved first
        if (active) {
            alert("Please wait until the last edit has been saved or cancelled.");
            return false;
        }
        newStatus = select.value;
        //alert("Code = " + Code + "\nService = " + Service + "\nNetID = " + NetID+ "\nStatusID = " + StatusID+"\nnewStatusID = " + newStatus);
        if (newStatus > 0) {
          active = true;
          sndAJAXReq('changeStatus', handleResponse, 'sc='+Code+'&serv='+Service+'&netid='+NetID+'&status='+newStatus);
        }
    } else {
      closeEditWindow();
    }
    return true;
}

function handleResponse() {
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var response = xmlhttp.responseText;
        if (response == '1') {
            // 1. close the edit status drop down menu.
            swapLayer('editCodeDIV', false);
            if (lastTag) {
                // 1. create a temp id so it is easy to access the img object
                tempID = Code+Service+NetID;
                lastTag.id =tempID;
                lastTag.getElementsByTagName('img')[0].id = tempID+'src';
                // 1.2 update the hidden field for this image so it has the right status
                lastTag.getElementsByTagName('input').namedItem("info").value = Code+','+Service+','+newStatus+','+NetID;
                // 2. hide thd old image
                getStyleObject(tempID+'src').visibility = "hidden";
                // hide the TR background image
                lastTag.parentNode.className = "";
                //replaceImage(tempID+'src', "../images/pixel.gif");
                // 3. use the yellow fader to show that the record has saved.
                Fat.fade_element(tempID);
                // 3. replace the old image with the new one
                setTimeout("replaceImage('"+tempID+"src', '../images/status_"+newStatus+".gif')", 1900);
                // 4. set active to false so that we are ready for the next edit.
                active = false;
            }
        } else {
            alert("Sorry, could not change status at this time. Please try again.");
        }
    }
}

function reclaimCode(code) {

    var msg = "Are you sure you want to reclaim this code and set the status back to available?\n\nThis action will email the third party code owner, all network operators and any users that have registered interest in the code.";
    if (confirm(msg)) {
        window.location.href = "../admin/reclaimcode.php?code=" + code;
        return false;
    } else {
      return false;
    }

}

function addEditEvents() {

    // get all alinks in the data table.
    var cells = getDocObject('codestable').getElementsByTagName('td');
    // loop through cells and test to see if it is an edit status cell
    for (var i = 0; i < cells.length; i++) test(cells[i]);

    function test(tag) {
        if (tag.className.indexOf("status"+UserOp) >= 0 || (UserOp == 0 && tag.className.indexOf("status") >= 0)) {
            addEvent(tag, "click", function dothis(event) { editStatus(event, tag) });
        } else if (tag.className.indexOf("note") >= 0) {
            lnk = tag.getElementsByTagName('img')[0];
            lnk.setAttribute("nicetitle",lnk.title);
            lnk.removeAttribute("title");
            addEvent(lnk,"mouseover",showNiceTitle);
            addEvent(lnk,"mouseout",hideNiceTitle);
            addEvent(lnk,"focus",showNiceTitle);
            addEvent(lnk,"blur",hideNiceTitle);
        }
    }
}
