//---------------------------------------------------------------------------------------
// function funMarkAll(field), funUnMarkAll(field)
// Description:
//    Mark or unMark all the given input fields as checked
// Precodition:
//    All the input fields must contain "checked" property (checkbox, and radio) 
// Revision History:
//    Jia Mao       07-09-2001   Initial Version
//    Howard Zhou   09-18-2001   Handle non array situation
//--------------------------------------------------------------------------------------  
function funMarkAll(field)	
{
    if (field)
    { 
        for (i = 0; i < field.length; i++)
	        	if (!field[i].disabled)
		        	field[i].checked = true ;	
		if (!field.disabled)
		    field.checked=true;
    }
}
function funUnMarkAll(field)
{
    if (field)
    { 
        for (i = 0; i < field.length; i++)
	        	if (!field[i].disabled)
		        	field[i].checked = true ;	
		if (!field.disabled)
		    field.checked=false;
    }
}

//---------------------------------------------------------------------------------------
// function funChangeFrame(target, toFrame)
// Description:
//    Change frame
// Precodition:
// Revision History:
//    Unknown   07-09-2001   Initial Version
//--------------------------------------------------------------------------------------  
function funChangeFrame(target, toFrame) 
{
  parent.frames[target].document.location = toFrame;
}


//---------------------------------------------------------------------------------------
// function funSwapImageRestore()
// Description:
//    Restore the image stored in document. 
// 
// Arguments      : None
// Returns        : None
// Preconditions  : document.aryImgSrc must contain images
// Postconditions : Not that I know of
//    
// Revision History:
//    Vidya         07-17-2001   Initial version
//    Howard Zhou   07-19-2001   Used more expressive variable names
//                               Added comments
//    Howard Zhou   08-22-2001   Added condition such that if the image is already 
//                               selected(highlighted), it will not be swapped.
//-------------------------------------------------------------------------------------- 
function funSwapImageRestore() 
{	//Javascript v3.0
	var iCounter;
	var objImage;	
	var aryImages = document.aryObjImages

	for(iCounter = 0; aryImages && iCounter < aryImages.length && (objImage = aryImages[iCounter]) && objImage.oSrc; iCounter++) 
	
		// if the this image is not the selected, then restore it
		// note: shortcircuit
		if (!document.aryObjImagesSelected || document.aryObjImagesSelected[0] != objImage)
			//swap image src
			objImage.src = objImage.oSrc;
}


//---------------------------------------------------------------------------------------
// function funPreloadImages()
// Description:
//    Preload the images for the buttons
// 
// Arguments      : None
// Returns        : None
// Preconditions  : Not that I know of
// Postconditions : Not that I know of
//    
// Revision History:
//    Vidya         07-17-2001   Initial version
//    Howard Zhou   07-19-2001   Used more expressive variable names
//                               Added comments

//-------------------------------------------------------------------------------------- 
function funPreloadImages() 
{   //Javascript v3.0 
	var iCounter;
	var iImgSrcCounter;
	var aryArguments = funPreloadImages.arguments; 

		
	// if the Images Object is supported do the following
	if(document.images)
	{ 
		if(!document.aryObjImages) 
			document.aryObjImages = new Array();
	 
		iImgSrcCounter = document.aryObjImages.length;

		for(iCounter = 0; iCounter < aryArguments.length; iCounter++)
			if (aryArguments[iCounter].indexOf("#") != 0)
			{	objDocument.aryObjImages[iImgSrcCounter] = new Image; 
				objDocument.aryObjImages[iImgSrcCounter++].src = aryArguments[iCounter];
			}
	}
}

//---------------------------------------------------------------------------------------
// function funFindObj(strName, objDocument)
// Description:
//    Find the object in the given objDocument(document object) having strName(object name in string)
// 
// Arguments      : strName     : string name of the desired object
//                  objDocument : optional document object
// Returns        : objReturn   : desired object
// Preconditions  : Not that I know of
// Postconditions : Not that I know of
//    
// Revision History:
//    Vidya         07-17-2001   Initial version
//    Howard Zhou   07-19-2001   Used more expressive variable names
//                               Added much needed comments
//-------------------------------------------------------------------------------------- 
function funFindObj(strName, objDocument) 
{   //Javascript v3.0
	var iCounter;
	var iPos;
	var objReturn;  
	
	// if no document object passed in, set objDocument as the current document(page)
	if(!objDocument) 
		objDocument = document; 
	
	if((iPos=strName.indexOf("?"))>0 && parent.frames.length) 
	{
		// if the current document is a frame of its parent document (by testing whether parent.frames.length>0)
		// then set objDocument to be the frame indicated in the strName argument followed by
		// a "?" (i.e. strName = "img_name?some_frame_name"
		objDocument = parent.frames[strName.substring(iPos+1)].document; 
		strName = strName.substring(0,iPos);
	}

	// first try to assign objReturn = objDocument[strName]
	// if the above fails and the browser supports document.all property
	//     then try assign objReturn = objDocument.all[strName]
	if(!(objReturn = objDocument[strName]) && objDocument.all) 
		objReturn = objDocument.all[strName]; 
	
	// if the above search for the desired object by strName failed (!objReturn)
	//     then go through all the forms contained in objDocument and try to find the desired object in all the forms
	if (objDocument.forms)
	{	for (iCounter = 0; !objReturn && iCounter < objDocument.forms.length; iCounter++) 
			objReturn = objDocument.forms[iCounter][strName];
	}
 
	// if the above search failed again (!objReturn) and objDocument support layers (DHTML v4.0)
	//     then recursively calling this function to search each layer for the desired object
	for(iCounter=0; !objReturn && objDocument.layers && iCounter < objDocument.layers.length ;iCounter++) 
		objReturn = funFindObj(strName, objDocument.layers[iCounter].document); 
	
	return objReturn;
}


//---------------------------------------------------------------------------------------
// function funSwapImage()
// Description:
//    Swap images of according to the given arguments
// 
// Arguments      : 
// Returns        : 
// Preconditions  : Not that I know of
// Postconditions : Not that I know of
//    
// Usage:
//    funSwapImage('references', '' , 'images/ReferencesSel.gif?contents',1)"
//
// Revision History:
//    Vidya         07-17-2001   Initial version
//    Howard Zhou   07-19-2001   Used more expressive variable names
//                               Added comments
//    Howard Zhou   08-22-2001   Added condition such that if the image is already 
//                               selected(highlighted), it will not be swapped.
//-------------------------------------------------------------------------------------- 
function funSwapImage(strImageName, objDocument, strImageSrc, i) 
{   //Javascript v3.0

	var iCounter;
	var iSrcCounter = 0; 
	var objImage; 
		//var aryArguments = funSwapImage.arguments;
	
	
	// if no document object passed in, set objDocument as the current document(page)
	if(!objDocument) 
		objDocument = document; 
		 
	objDocument.aryObjImages = new Array; 
				
		//for(iCounter = 0; iCounter < (aryArguments.length-2); iCounter+=3)
		// if we find the object in the current document 
	if ((objImage = funFindObj(strImageName, objDocument))!=null)
	{	
		objDocument.aryObjImages[iSrcCounter++] = objImage; 
			
		// if objImage.oSrc is not initialized
		if(!objImage.oSrc) 
			objImage.oSrc = objImage.src;
			
		// if objImage is already selected, do not swap image
		// note: shortcircuit
		if (!objDocument.aryObjImagesSelected || objDocument.aryObjImagesSelected[0] != objImage)		
			objImage.src = strImageSrc
	}
}


//---------------------------------------------------------------------------------------
// function funSelectImage
// Description:
//    store the passed in strImageName ("<IMG NAME=") into a static(belongs to document object)
//    array aryObjImagesSelected so that "funSwapImage" and "funSwapImageRestore" will
//    not affect this selected image
// 
// Arguments      : 
// Returns        : 
// Preconditions  : The Now selected image must already be swapped
// Postconditions : Not that I know of
//    
// Revision History:
//    Howard Zhou   08-22-2001   Initial version
//-------------------------------------------------------------------------------------- 
function funSelectImage(objDocument, strImageName)
{
	// if no document object passed in, set objDocument as the current document(page)
	if(!objDocument) 
		objDocument = document; 
		
	var objImage
	if ((objImage = funFindObj(strImageName, objDocument)) != null)
	{
		if (!objDocument.aryObjImagesSelected)
			objDocument.aryObjImagesSelected = new Array(1);	
		objDocument.aryObjImagesSelected[0] = objImage;
	}
}


//---------------------------------------------------------------------------------------
// function funUnSelectImage
// Description:
//    clean array aryObjImagesSelected so that "funSwapImage" and "funSwapImageRestore" will
//    affect this selected image again
//    restore the selected image (if any) to its original state
// 
// Arguments      : 
// Returns        : 
// Preconditions  : 
// Postconditions : Not that I know of
//    
// Revision History:
//    Howard Zhou   08-22-2001   Initial version
//-------------------------------------------------------------------------------------- 
function funUnselectImage(objDocument)
{
	var objImage
	
	// if no document object passed in, set objDocument as the current document(page)
	if(!objDocument) 
		objDocument = document; 
		
	if(objDocument.aryObjImagesSelected)
	{
		objImage = objDocument.aryObjImagesSelected[0];
		if (objImage != null)
		{	
			objImage.src = objImage.oSrc;
			objDocument.aryObjImagesSelected[0] = null;
		}
	}
}


//---------------------------------------------------------------------------------------
// function funSelectNavigationButton(strImageName, strImageSrc)
// Description:
//    1. Unselect the previously selected button
//    2. Swap the image of the currently selected button
//    3. Select the currently selected button
//
// Arguments      : 
// Returns        : 
// Preconditions  : Not that I know of
// Postconditions : Not that I know of
//    
// Revision History:
//    Howard Zhou   08-22-2001   Initial version
//-------------------------------------------------------------------------------------- 
function funSelectNavigationButton(objDocument, strImageName, strImageSrc)
{
	funUnselectImage(objDocument)
	funSwapImage(strImageName, objDocument, strImageSrc, 1);
	funSelectImage(objDocument, strImageName);
}

//------------------------------------------------------------------------------------
// function ChangeFormAction(strFormName, strAction)
// Description:
//    Change the action attribute of the form.
//
// Arguments     : None
// Return        : None
// Precondition  : None
// PostCondition : change the action attribute of the form.
//
// Revision History:
//   Sanjay Patel   07-24-2001   Initial version 
//------------------------------------------------------------------------------------	
function funChangeFormAction(strFormName, strAction)
{
    //strFormName.action = strAction;
	document.forms[strFormName].action = strAction;	
}

//------------------------------------------------------------------------------------
// function funTrim(str)
// Description:
//    Change the action attribute of the form.
//
// Arguments     : None
// Return        : None
// Precondition  : None
// PostCondition : change the action attribute of the form.
//
// Revision History:
//   Sanjay Patel   07-24-2001   Initial version 
//------------------------------------------------------------------------------------	
function funTrim(str) 
{
	var len = str.length;
	var begin = 0, end = len - 1;
	while (str.charAt(begin) == " " && begin < len) begin++;
	while (str.charAt(end) == " " && begin < end) end--;
	return str.substring(begin, end+1);
}

//------------------------------------------------------------------------------------
// function funParseString(myString) 
// Description:
//    Change the action attribute of the form.
//
// Arguments     : None
// Return        : None
// Precondition  : None
// PostCondition : change the action attribute of the form.
//
// Revision History:
//   Sanjay Patel   07-24-2001   Initial version 
//------------------------------------------------------------------------------------	
function funParseString(myString) 
{
	var commapos, dashpos, currpos, finalstring, currnum, currdash;
	var blankspace, blankpos
	blankspace = " ";
	while (myString.indexOf(blankspace) >= 0) {		
	blankpos = myString.indexOf(blankspace);
	myString = myString.substring(0, blankpos) + myString.substring(blankpos + 1);
	}

	currpos = 0;
	finalstring = "";
		
	while (myString.length != 0) 
	{			
		dashpos = myString.indexOf("-");			
		commapos = myString.indexOf(",");			
		/*if there are no more commas or dashes in the string - there must
		only be one number left*/
		if (commapos < 0 && dashpos < 0){
		    finalstring = finalstring + "," + myString;
			break;
		}			
		/*if there are no more commas but there is a dashed substring left - there
		must only be this one item left, i.e. 1-5*/
		if (commapos < 0) {
			finalstring = finalstring + funHelpDash(myString);
		    break;
		}

		/*if there are no more dashes, but there are commas still remaining -
		this means just concat the rest of the string on*/

		if (dashpos < 0 && commapos >= 0) {
		    finalstring = finalstring + "," + myString;
			break;
		}	

		/*if there are both dashes and commas left in the string - now we are in trouble!*/
			
		/*if things with commas come before dashes*/
		if (commapos < dashpos) 
		{					
			while (commapos < dashpos && commapos >= 0) 
				{
				currnum = myString.substring(currpos, commapos - currpos);
				finalstring = finalstring + "," + currnum;
				myString = myString.substring(commapos + 1);
				commapos = myString.indexOf(",");
				dashpos = myString.indexOf("-");
				}
		}else {
				/*dashes come before further commas*/
				currdash = myString.substring(currpos, commapos - currpos);
				finalstring = finalstring + funHelpDash(currdash);
				myString = myString.substring(commapos + 1);
				}
			
	}

	if (finalstring.substring(0,1) == ",") 
	{
		finalstring = finalstring.substring(1)
	}
	else if (finalstring.substring(finalstring.length() - 1, 1) == ",") 
	{ 
			finalstring = finalstring.substring(0,finalstring.length() -1)
	}
	
	return funMakeArray(finalstring);
}


//------------------------------------------------------------------------------------
// function funHelpDash(dstring) 
// Description:
//    Change the action attribute of the form.
//
// Arguments     : None
// Return        : None
// Precondition  : None
// PostCondition : change the action attribute of the form.
//
// Revision History:
//   Sanjay Patel   07-24-2001   Initial version 
//------------------------------------------------------------------------------------	
function funHelpDash(dstring) 
{
	var beg, last, newString, dashpos, counter;
	newString = "";
	dashpos = dstring.indexOf("-");
	beg = parseInt(dstring.substring(0, dashpos));
	last = parseInt(dstring.substring(dashpos + 1));
	
	while (beg <= last) {
		newString = newString + "," + beg;
		beg++;
	}
	return newString;
}


//------------------------------------------------------------------------------------
// function funMakeArray(str)
// Description:
//    Change the action attribute of the form.
//
// Arguments     : None
// Return        : None
// Precondition  : string is not null
// PostCondition : change the action attribute of the form.
//
// Revision History:
//   Sanjay Patel   07-24-2001   Initial version 
//------------------------------------------------------------------------------------	
function funMakeArray(str) 
{	
	var strlen, commapos, nextcommapos, counter, currnum, currpos;		
	if (str == null) return;
		
	strlen = str.length;
	var myArray = new Array(strlen);
		
	counter = currpos=0;		
		
	while(str !=null) 
	{
		commapos = str.indexOf(",");			
		if (commapos < 0 ) 
		{
			myArray[counter] = str;
			break	;
		}else {
			currnum = str.substring(currpos, commapos - currpos);								
			myArray[counter] = currnum.toString();				
			str = str.substring(commapos + 1);
			counter++;
		}
	}
		
	var retArray = new Array(counter);
	var i
	for( i=0;i<=counter;i++)
		retArray[i]=myArray[i];
			
	return retArray;
}

//------------------------------------------------------------------------------------
// function funCheckChar(str)
// Description:
//    Change the action attribute of the form.
//
// Arguments     : None
// Return        : None
// Precondition  : string is not null
// PostCondition : change the action attribute of the form.
//
// Revision History:
//   Sanjay Patel   07-24-2001   Initial version 
//------------------------------------------------------------------------------------	
function funCheckChar(str)
{
	var parsed = true;
	var validchars = "0123456789,- ";  
	for (var i=0; i < str.length; i++) {			
		if (validchars.indexOf(str.charAt(i)) != -1)continue;			
		parsed = false;
		break;
	}
	return parsed;
}

//------------------------------------------------------------------------------------
// function funIsSpace(str)
// Description:
//    Check to see if the passed-in string consists of only space chars 
//
// Arguments     : None
// Return        : None
// Precondition  : None
// PostCondition : None
//
// Revision History:
//   Howard Zhou   09-10-2001   Initial version 
//------------------------------------------------------------------------------------
function funIsSpace(str)
{  
    if (str == "") 
        return true;
    else if (funTrim(str) == "")
        return true;
    
    return false;
}