// ===================================================================
// Orignal source code downloaded from
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
// 
// Modifications for use in Genesys Agent Scripting by:
// John Conneely <jconneely@RiverStarSoftware.com>
// Randy Meacham <rmeacham@RiverStarSoftware.com>
// ===================================================================

// HISTORY
// ------------------------------------------------------------------
// @01 - 04/01/2004 - RJM 
//		Added as input parameter to function "isNumeric" a string containing
//		valid numeric characters
//
// ------------------------------------------------------------------


function saveScrollCoordinates(theForm)
{
	theForm.WWGScrollX.value = (document.all)?document.body.scrollLeft:window.pageXOffset;
	theForm.WWGScrollY.value = (document.all)?document.body.scrollTop:window.pageYOffset;
}

function submitPage(branchFlag, threadNum)
{
	var theForm = eval("document.WWGForm" + threadNum);
	theForm.WWGObjectType.value = "";
	theForm.WWGObjectValue.value = "";
	theForm.WWGObjectRow.value = "";
	theForm.WWGBranchFlag.value = branchFlag;
	if( pageIsLocked(threadNum) )
	{
		return;
	}
	else if( branchFlag )
	{
		if( !eval("validData" + threadNum + "()") )
		{
			return;
		}
	}
	document.body.style.cursor='wait';
	theForm.WWGPageLock.value = 1;
	saveScrollCoordinates(theForm);

	theForm.submit();
}

function objectClickRow(objectValue, objectRow, branchFlag, threadNum)
{
	var theForm = eval("document.WWGForm" + threadNum);
	theForm.WWGObjectType.value = "TABLE";
	theForm.WWGObjectValue.value = objectValue;
	theForm.WWGObjectRow.value = objectRow;
	theForm.WWGBranchFlag.value = branchFlag;
	if( pageIsLocked(threadNum) )
	{
		return;
	}
	else if( branchFlag )
	{
		if( !eval("validData" + threadNum + "()") )
		{
			return;
		}
	}
	document.body.style.cursor='wait';
	theForm.WWGPageLock.value = 1;
	saveScrollCoordinates(theForm);
	theForm.submit();
}

function setRowStatus(objectValue, objectRow, allowMultiple, threadNum)
{

	var theForm = eval("document.WWGForm" + threadNum);
	var theField = eval("document.WWGForm" + threadNum + "." + objectValue);
	theField.value = theField.value.replace("C", "M");
	if( allowMultiple == 1 )
	{
	    if( theField.value.charAt(objectRow) == "M" )
		theField.value = theField.value.substring(0,objectRow) + " " + theField.value.substring(objectRow+1)
	    else
		theField.value = theField.value.substring(0,objectRow) + "C" + theField.value.substring(objectRow+1)
	}
	else
	{
	    theField.value = theField.value.replace("M", " ");
	    theField.value = theField.value.substring(0,objectRow) + "C" + theField.value.substring(objectRow+1)
	}

	saveScrollCoordinates(theForm);
}

function objectClickCol(objectValue, objectRow, branchFlag, threadNum)
{
	var theForm = eval("document.WWGForm" + threadNum);
	theForm.WWGObjectType.value = "COLUMN";
	theForm.WWGObjectValue.value = objectValue;
	theForm.WWGObjectRow.value = objectRow;
	theForm.WWGBranchFlag.value = branchFlag;
	if( pageIsLocked(threadNum) )
	{
		return;
	}
	else if( branchFlag )
	{
		if( !eval("validData" + threadNum + "()") )
		{
			return;
		}
	}
	document.body.style.cursor='wait';
	theForm.WWGPageLock.value = 1;

	saveScrollCoordinates(theForm);
	theForm.submit();
}


function objectClick(objectType, objectValue, validateData, branchFlag, skipValidation, threadNum)
{
	var theForm = eval("document.WWGForm" + threadNum);
	theForm.WWGObjectType.value = objectType;
	theForm.WWGObjectValue.value = objectValue;
	theForm.WWGObjectRow.value = "";
	theForm.WWGBranchFlag.value = branchFlag;
	if( pageIsLocked(threadNum) )
	{
		return;
	}
	if( !skipValidation )
	{
		if( validateData || branchFlag )
		{
			if( !eval("validData" + threadNum + "()") )
			{
				return;
			}
		}
	}
	document.body.style.cursor='wait';
	theForm.WWGPageLock.value = 1;
	saveScrollCoordinates(theForm);
	theForm.submit();
}

function confirmObjectClick(objectType, objectValue, confirmText, validateData, branchFlag, skipValidation, threadNum)
{
	var theForm = eval("document.WWGForm" + threadNum);
	theForm.WWGObjectType.value = objectType;
	theForm.WWGObjectValue.value = objectValue;
	theForm.WWGObjectRow.value = "";
	theForm.WWGBranchFlag.value = branchFlag;
	if( pageIsLocked(threadNum) )
	{
		return;
	}
	if( !skipValidation )
	{
		if( validateData || branchFlag )
		{
			if( !eval("validData" + threadNum + "()") )
			{
				return;
			}
		}
	}
	if( confirm(confirmText) )
	{
		document.body.style.cursor='wait';
		theForm.WWGPageLock.value = 1;
		saveScrollCoordinates(theForm);
		theForm.submit();
	}
}

function WWGUserAlert(msg)
{
	if( msg != "" )
	{
    		alert( msg  );
	}
}

function pageIsLocked(threadNum)
{
	var theForm = eval("document.WWGForm" + threadNum);
	if( theForm.WWGPageLock.value == 1)
	{
		alert(nlsPrompt("Please wait until the current process has completed.", ""));
		return true;
	}
	else
	{
		return false;
	}
}


function applyformat(theField, theFormat)
{
	var newvalue = "";
	var idx;
	var formatidx;
	var valchar = "0123456789";
	idx = 0;
	for( formatidx=0; formatidx < theFormat.length; formatidx++ )
	{
		if( idx < theField.value.length )
		{
			if( theFormat.charAt(formatidx) == "*" )
			{
				if(valchar.indexOf(theField.value.charAt(idx)) == -1
				&& theField.value.charAt(idx) != "*" )
				{
					idx++;
				}
				else
				{
					newvalue += theField.value.charAt(idx);
					idx++;
				}
			}
			else if( theFormat.charAt(formatidx) == "#" )
			{
				if(valchar.indexOf(theField.value.charAt(idx)) == -1)
				{
					idx++;
				}
				else
				{
					newvalue += theField.value.charAt(idx);
					idx++;
				}
			}
			else
			{
				newvalue += theFormat.charAt(formatidx);
				if( theFormat.charAt(formatidx) == theField.value.charAt(idx))
				{
					idx++;
				}
			}
		}
	}
	if( theField.value != newvalue)
	{
		theField.value = newvalue;
	}
}

function isNumeric(theField)	
{
	var valchar = WWGValidNumericChars;		//@01
	var theValue = theField.value;
	var idx;
	
	if( theValue == "" )
	{
	    return false;
	}

	for( idx=0; idx < theValue.length; idx++ )
	{
		if(valchar.indexOf(theValue.charAt(idx)) == -1)
		{
			return false;
		}
	}
	return true;
}

function nlsPrompt(thePrompt, theValue)
{

    var promptStr = WWGNLSPrompts;
    var returnVal = thePrompt;
    var prompts = promptStr.split(";");
    var i = 0;

    for( i=0; i < prompts.length; i+=2 )
    {

	if( prompts[i] == thePrompt )
	{
	    returnVal = prompts[i+1];
	    break;
	}
    }
    
    returnVal = returnVal.replace("{0}", theValue);
    return returnVal;
}




function WWGSelectField(formName, fieldName)
{
    var theField = eval("document." + formName + "." + fieldName);
    if (theField!=null)
    {
	theType = theField.type;
	if (theType==undefined) //Must be a list of fields (like radio-buttons or check boxes)
	{
	    var firstField=null;
	    var lastSelField=null;
	    var i;
	    for(i=0; i<theField.length; i++)
    	    {
		theType=theField[i].type;
		if (theType=="radio" || theType=="checkbox")
		{
		    if (theField[i].checked) theField[i].focus();
		}

	    }
	}
	else if (theType=="text" || theType=="textarea" )
	{
	    theField.select();
	    theField.focus();
	}
	else
	{
	    theField.focus();
	}

    }
}

function WWGChangeImage(objectName, imageName)
{
	if (document.images)
	{
		currentImage = document.images[objectName];
		currentImage.src = imageName;
	}
}

function WWGCaptureTableScroll(objectName, threadNum)
{
	var theObj = document.getElementById(objectName + "TableDiv" + threadNum);
	var theObjTop = document.getElementById(objectName + "ScrollTop" + threadNum);
	var theObjLeft = document.getElementById(objectName + "ScrollLeft" + threadNum);
	theObjTop.value = theObj.scrollTop;
	theObjLeft.value = theObj.scrollLeft;
}
function WWGResizeTableCols(tableName, threadNum, width)
{
	var bdy_row = document.getElementById(tableName + "BodyTab"+threadNum).getElementsByTagName("tr").item(0);
	if( bdy_row == null)
	{
		return width;
	}
	var bdy_cells = bdy_row.getElementsByTagName("td");
	var hdr_row = document.getElementById(tableName + "HeaderRow"+threadNum);
	var hdr_cells;
	if( hdr_row != null)
	{
		hdr_cells = hdr_row.getElementsByTagName("th");
	}
	var totWidth = 0;
	for(var i=0; i<bdy_cells.length; i++)
	{
		var bdy_cell = bdy_cells.item(i);
		if( hdr_row != null)
		{
			var hdr_cell = hdr_cells.item(i);
			if (bdy_cell.clientWidth>hdr_cell.clientWidth)
			{
				hdr_cell.width = bdy_cell.clientWidth + hdr_cell.clientWidth-hdr_cell.offsetWidth;
			}
			else
			{
				bdy_cell.width = hdr_cell.clientWidth + bdy_cell.clientWidth-bdy_cell.offsetWidth;
			}
		}
		totWidth += bdy_cell.offsetWidth;
	}
	if( hdr_row == null)
	{
		hdr_row = document.getElementById(tableName + "TabHeaderRow" + threadNum);
		if( hdr_row != null)
		{
			hdr_cells = hdr_row.getElementsByTagName("th");
			var hdr_cell = hdr_cells.item(0);
			hdr_cell.width = totWidth + (hdr_cell.offsetWidth - hdr_cell.clientWidth) + 15;
		}
	}

	if( width == 0 )
	{
	    return totWidth + 18;
	}
	else
	{
	    return width;
	}

}

function WWGCallIfExists(func_name)
{
    if (self[func_name])
    {
	eval(func_name + "()");
    }
}

function WWGFieldByName(obj, fieldName)
{
    if (obj==null) return null;
    var theForm = obj.form;
    if (theForm==null) theForm=obj;
    
    var theField = theForm.WWGProjBookIdent;
    if (theField==null) return null;
    var projBookIdent=theField.value;
    
    theField = eval("theForm.WWGField_" + projBookIdent + "_" + fieldName);
    if (theField==null)
        theField = eval("theForm.WWGField_global_" + fieldName);
    
    return theField;
}
