// $Id: dynamic.js,v 1.6 2007/04/27 16:47:52 jvitelli Exp $  

/*
This file has all fucntion related to dhtml / ajax work

if calling function saveItem, than the parent file must also include /utilities.js
*/


function  makeHTML(pID, pValue, pMaxLength, pFunctionID){
	return "<a href=\"#\" onclick=\"makeInput(this, " + pID + ",'" + pValue + "'," + pMaxLength + ", " + pFunctionID +");return false;\">" + pValue + "</a>"
}

//function makeInput(pID, pValue, pMaxLength, pParent){
function makeInput(pElement, pID, pValue, pMaxLength, pFunctionID){
	
	var oParent
	var sHTML
	var oInput
	
	if(!pValue){
		pValue = ''
	}
	
	oParent = pElement.parentNode
		
	if(oParent){
		
		saveState = oParent.innerHTML
		
		//store the original state 
		resultStore(oParent.id, saveState, 2);
		
		sHTML = "<input ID=\"inp_"+ oParent.id +"\" type=\"text\" style=\"width:120px\" value=\"" + pValue + "\" maxlength=\"" + pMaxLength + "\" />";
		sHTML += "&nbsp;<a href=\"#\" onclick=\"saveItem('inp_" + oParent.id + "', '" + oParent.id + "', "+ pID +", "+ pFunctionID +");return false;\"><img src=\"/img/buttons/btn_go_sm.gif\" border=\"0\" align=\"absmiddle\" /></a><a href=\"#\" onclick=\"cancelInput('" + oParent.id + "');return false;\"><img src=\"/img/buttons/btn_close.gif\" border=\"0\" align=\"absmiddle\" /></a>";		
		//alert(sHTML);
		oParent.innerHTML = sHTML;	
		
		var oInput = document.getElementById("inp_" + oParent.id);
		oInput.focus();			
		
	}
}

function cancelInput(pElementID){
	
	sResult = resultStore(pElementID, null, 0);
	oElement = document.getElementById(pElementID)
	oElement.innerHTML = sResult
}

function saveItem(pInputID, pElementID, pDataID, pFunctionID){
	
	var oInput
	oInput = document.getElementById(pInputID);
	
	if(bRequest){
	
	} else {
		if(oInput){
			if(oInput.value == ''){
				alert('Please supply a name');
				oInput.focus();
				return false
			} else if (!isAlphaNumeric(oInput.value, true)){
				alert('Value can only contain letters and numbers.');
				oInput.value = "";
				oInput.focus();
				return false
			} else {						
				oElement = document.getElementById(pElementID);
				//oElement.innerHTML = "<img src=\"/img/loading_bar.gif\" border=\"0\" />";
				saveRequest(pDataID, oInput.value, pFunctionID);
			}
		}
	}
}


//this function Assumes data is stored in pairs, first = ID, seconds = string data
//pAction {0=get, 1=add, 2=update}
//if 2 and not in array, it will then add.

function resultStore(pElementID, pData, pAction){
	
	var nLength;
	//find the end of the array
	nLength = aryResultStore.length;
				
	if(pAction == 1){
		//store incoming data
		aryResultStore[nLength] = pElementID;
		aryResultStore[nLength+1] = pData;		
		return true;
	} else if(pAction == 2){
		//update existing data for the passed in ID
		for(x=0; x < nLength; x++){
			if(pElementID == aryResultStore[x]){						
				aryResultStore[x+1] = pData;
				return true;
			}
		}
		//store the data
		aryResultStore[nLength] = pElementID;
		aryResultStore[nLength+1] = pData;
		
		return true;		
	} 	else if(pAction == 0){
		//get data, return false if none
		for(x=0; x < nLength; x++){
			if(pElementID == aryResultStore[x]){		
				return aryResultStore[x+1];
			}
		}
		//if here, now match
		return false;				
	} else {
		return false;
	}
}

function deleteRowItem(pElement, pDataID, pFunctionID){
	if(confirm("Are you sure you want to delete this item?")){
		//run delete
		nAction = 0;
		oElement = pElement.parentNode;
		oRow = oElement.parentNode;
		var sUrl ="/mvc/logic/http_proc.asp?FunctionID=" + pFunctionID + "&dataID=" + pDataID;		
		makeHttpRequest("GET", sUrl, true);
		bRequest = true		
	} else {
		
		return false;
	}
}

var nSteps
var faderID = null
var faderRunning = false
var millidelay
var nAction

function doFader(pRowID,pAction, pMilliDelay){
    // Set the length of the fader, in seconds
	//alert(pRowID);
	millidelay = pMilliDelay
    nSteps = 4
	nAction = pAction  //action 0 removes the row (sets display::hidden)
    oFadeElement = document.getElementById(pRowID);  //the object you want to fade
	stopFader();
    startFader();
}

function stopFader(){
    if(faderRunning){
        clearTimeout(faderID);
    	faderRunning = false;
	}
}

function startFader(){	
    
	if (nSteps==0){
    stopFader();
	  oFadeElement.style.backgroundColor = '';
		if(nAction == 0){
			oFadeElement.style.display = "none";
		}
    } else {        
		
		if(nAction == 0){
			if(nSteps == 4){
				sColor = "#666";
			} else if(nSteps == 3){
				sColor = "#999";
			} else if(nSteps == 2){
				sColor = "#ccc";
			} else  if(nSteps == 1){
				sColor = "#fff";
			}
			oFadeElement.style.color = sColor;
		} else if(nAction == 1){
			if(nSteps == 4){
				sColor = "#f90";
			} else if(nSteps == 3){
				sColor = "#999";
			} else if(nSteps == 2){
				sColor = "#ccc";
			} else  if(nSteps == 1){
				sColor = "#fff";
			}
		}
		
		oFadeElement.style.backgroundColor = sColor;
        nSteps = nSteps - 1;
        faderRunning = true;
        faderID = self.setTimeout("startFader()", millidelay);
    }
}

//pass in an ID and this function will set the objects visibility to hidden
function doClose(pIdName){
	oRBox = document.getElementById(pIdName);
	oRBox.style.visibility = "hidden";
	oRBox.style.position = "absolute";
}

//finds the X and Y of an element on the screen
function getXandY(pObj){

	var l_objElement = pObj			
	var rd = {x:0, y:0}
	do{
		rd.x += l_objElement.offsetLeft
		rd.y += l_objElement.offsetTop
		l_objElement = l_objElement.offsetParent
	} 
	while (l_objElement)
	gX = rd.x
	gY = rd.y
			
}

