
// Create an array of valid file extensions
var validExts = new Array();
validExts[0] = "doc";
validExts[1] = "pdf";
validExts[2] = "xls";
validExts[3] = "swf";
validExts[4] = "ppt";
validExts[5] = "zip";
validExts[6] = "exe";
validExts[7] = "txt";
    
// Will return the width needed for the IFRAME based on the screen resolution
function getIFRAMEWidthByResolution()
{
	if( ( screen.width == 800 ) && ( screen.height == 600 ) )
		return 490;
		
	if( ( screen.width == 1024 ) && ( screen.height == 768 ) )
		return 690;
		
	if( ( screen.width == 1280 ) && ( screen.height == 1024 ) )
		return 900;
}

function getDocHeight(doc) 
{
	var docHt = 0, sh, oh;

	if( doc.height ) 
		docHt = doc.height;
	else if( doc.body ) 
	{
		if( doc.body.scrollHeight ) 
			docHt = sh = doc.body.scrollHeight;
			
		if( doc.body.offsetHeight ) 
			docHt = oh = doc.body.offsetHeight;
			
		if( sh && oh ) 
			docHt = Math.max(sh, oh);
	}

	return docHt;
}

function setIframeHeight( iframeName, height ) 
{
	try
	{
		if( parent.document.getElementById( iframeName ) )
		{
			var frameHt = parent.document.getElementById( iframeName ).height;
			parent.document.getElementById( iframeName ).height = parseInt( frameHt ) + height + 'px';
		}
	}
	catch(e)
	{
		alert('Javascript error: ' + e?e:e.description);
	}
}

function setIframeWidth( iframeName, elem ) 
{
	var iframeWin = window.frames[ iframeName ];
	var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;

	if( iframeEl && iframeWin ) 
	{
		var docHt = getDocHeight( iframeWin.document );
		
		if( document.getElementById( elem ).style.display == 'none' )
			iframeEl.style.width = getIFRAMEWidthByResolution() + 200 + "px";
		else
			iframeEl.style.width = getIFRAMEWidthByResolution() + "px";
	}
} 

function loadIframe( iframeName, url ) 
{
	if ( window.frames[iframeName] ) 
	{
		window.frames[iframeName].location = url;   
		return false;
	}
	else 
		return true;
}

// This will make sure that some kind of text was entered into a RAD Editor
function validateRadEditorText( fieldName )
{
	if( document.getElementById( fieldName ) )
	{
		var editor = GetRadEditor( fieldName );
		if( trim( editor.GetText() ) == '' )
			return false;
		else
			return true;
	}
	else 
		return true;
}

// Will return the file extension of a path given when using RAD Upload
function getFileExt( fieldName, field )
{
	if( window[ fieldName ] )
	{
		if( !field.ValidateExtensions() )
		{
			var validExts = ( new String( field.l ) ).split(',');
			msg = "You have entered a file with an incorrect file extension.\nPlease enter a file with one of the following extensions."
				
			for( var j=0; j < validExts.length; j++ )
				msg = msg + "\n- "+ validExts[j];
				
			alert(msg);
				
			return false;
		}
		else
			return true;
	}
	else
		return true;
}

// Will return the file extension of a path given when using a regular file upload
function getFileExt2( file )
{  
	extMatch=true;
	ext="";
	fileName="";
	f = "";

	if ( file.indexOf('.') > -1 )
	{
		extMatch=false;
			
		extArray = file.split('.');
		ext = extArray[extArray.length-1];
			
		fileName = extArray[0];
		files = fileName.split('\\');
		f = files[files.length-1];
			
		for(var x=0; x < validExts.length; x++)
		{
			if( validExts[x] == ext.toLowerCase() )
			{
				extMatch = true;
				break;
			}
		}
	}
		
	if ( !extMatch )
	{
		file = "";
		msg = "You have entered a file with the following file extension: "+ ext.toUpperCase() +".\nPlease enter a file with one of the following extensions."
			
		for(var j=0; j < validExts.length; j++)
			msg = msg + "\n- "+ validExts[j];
			
		alert(msg);
			
		return false;
	}

	return true;
}

// This will help in getting rid of caching issues using javascript
function getFullDateAndTime()
{
	d = new Date();
	
	year = d.getFullYear();
	month = d.getMonth();
	day = d.getDate();
	
	hour = d.getHours();
	min = d.getMinutes();
	sec = d.getSeconds();
	
	return year + month + day + hour + min + sec;
}

// This will add a noCache item to a url depending 
// if the ? exists or not to decide how it will be added
function addNoCache2Url( strUrl )
{
	if( trim( strUrl ) != '' )
	{
		var qMark = strUrl.indexOf('?');
		if( qMark == -1 )
			strUrl += '?';
		else
			strUrl += '&';
			
		strUrl += 'noCache=' + getFullDateAndTime();
	}
	
	return strUrl;
}

// Example of use: 
// d = new Date("01/30/2004");
// addMonth(d,1);
// Result: 02/29/2004
function addMonth( d, month )
{ 
	t = new Date( d ); 
	t.setMonth( d.getMonth() + month ); 
	
	if( t.getDate() < d.getDate() ) 
		t.setDate(0); 
		
	monthStr = t.getMonth() + 1;
	if( new String( monthStr ).length < 2 )
		monthStr = "0" + monthStr;
		
	dayStr = t.getDate();
	if( new String( dayStr ).length < 2 )
		dayStr = "0" + dayStr;
		
	newDate = monthStr + "/" + dayStr + "/" + t.getFullYear();
	
	return newDate;
} 

// This will validate dynamically created fields
function validateDynamicFields( field, fieldType )
{
	switch( fieldType )
	{
		case 'money':
			stripDollar( field );
			if( !isInteger( field.value ) )
			{
				alert('Please enter a valid money value');
				field.value = "$0.00";
			}
			break;
			
		case 'percent':
			stripPercent( field );
			if( !isInteger( field.value ) )
			{
				alert('Please enter a valid percentage');
				field.value = "0%";
			} 
			break;
			
		case 'email':
			if( !e_checkBlank( field ) )
			{
				alert('Enter a valid email address');
				field.value = '';
			}
			break;
			
		case 'phone':
			if( !phone_checkBlank( field ) )
			{
				alert('Enter a valid phone number');
				field.value = '';
			}
			break;
			
		default:
			// Do nothing
			break;
	}
}
			
// Creates a window that will be centered based on the height and width given
function popUpCenteredWindow(url,windowName,iHeight,iWidth) 
{
	  var iMyWidth;
	  var iMyHeight;
	  //gets top and left positions based on user's resolution so hint window is centered.
	  iMyWidth = (window.screen.width/2) - (parseInt(iWidth)/2 + 10); //half the screen width minus half the new window width (plus 5 pixel borders).
	  iMyHeight = (window.screen.height/2) - (parseInt(iHeight)/2 + 50); //half the screen height minus half the new window height (plus title and status bars).
	  var win1 = window.open(url,windowName,"status,height="+iHeight+",width="+iWidth+",resizable=yes,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",scrollbars=yes,menubar=yes");
	  win1.focus();
}

// Creates a window that will be centered based on the height and width given with no menu bar given
function popUpCenteredWindowNoMenuBar(url,windowName,iHeight,iWidth) 
{
	  var iMyWidth;
	  var iMyHeight;
	  //gets top and left positions based on user's resolution so hint window is centered.
	  iMyWidth = (window.screen.width/2) - (parseInt(iWidth)/2 + 10); //half the screen width minus half the new window width (plus 5 pixel borders).
	  iMyHeight = (window.screen.height/2) - (parseInt(iHeight)/2 + 50); //half the screen height minus half the new window height (plus title and status bars).
	  var win1 = window.open(url,windowName,"status,height="+iHeight+",width="+iWidth+",resizable=yes,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",scrollbars=yes,menubar=no");
	  win1.focus();
}

// This will refresh the parent window with the url passed to it
// and pass the query string that is needed
function refreshParentWindowWithUrl( url, qs )
{
	if( window.opener )
	{
		if( trim( url ) == '' )
		{
			// This will remove any url parameters the site had before
			var strUrl = new String(self.opener.location);
			
			var qMark = strUrl.indexOf('?');
			var newUrl = strUrl.substring(0, qMark);
		
			self.opener.location = newUrl + qs;
			self.opener.focus();
		}
		else
		{
			self.opener.location = url + qs;
			self.opener.focus();
		}
	}
	else
		document.location.href = url;
}

// Validate IP Address
function validateIP( fieldValue, allowBlank ) 
{
	if( trim( fieldValue ) == '' && allowBlank )
		return true;
		
    if( fieldValue.search(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) != -1 ) 
    {
        var myArray = fieldValue.split(/\./);
        
        if( myArray.length != 4 )
			return false;
			
        if( myArray[0] > 255 || myArray[1] > 255 || myArray[2] > 255 || myArray[3] > 255 )
            return false;
        
        if( myArray[0] == 0 && myArray[1] == 0 && myArray[2] == 0 && myArray[3] == 0 )
            return false;
        
        return true;
    }
    else
		return false;
}

// Email validation
function e_check(ctrl) {

	if ( trim( ctrl.value ) == "" || ctrl.value.indexOf ('@', 0) < 2 )
		return false;
	else
	{
		test = ctrl.value.indexOf('.', ctrl.value.indexOf ('@', 0))
		if (test != -1)
			return true;
		else
			return false;
	}
}

// Email validation that allows for the text to be blank
function e_checkBlank(ctrl) 
{
	if( trim( ctrl.value ) != '' )
	{
		if( ctrl.value.indexOf ('@', 0) < 2 )
			return false;
		else
		{
			test = ctrl.value.indexOf('.', ctrl.value.indexOf ('@', 0))
			if (test != -1)
				return true;
			else
				return false;
		}
	}
	else
		return true;
}

//This function strips chars not digits from phone value
function phone_check( field ) 
{
	char_storage="";
	char_right_p=')';
	char_left_p='(';
	char_dash='-';
	char_period='\.';
	char_space=' ';
	for (var i=0;i<field.value.length;i++){
		char_to_test=field.value.substring(i,i+1);
		if ((char_right_p.indexOf(char_to_test)<0)&&(char_left_p.indexOf(char_to_test)<0)
		&&(char_dash.indexOf(char_to_test)<0)&&(char_space.indexOf(char_to_test)<0)&&(char_period.indexOf(char_to_test)<0)){
			char_storage+=char_to_test;
		}
	}
	field.value=char_storage;
	
	if( trim(field.value) != "" && (field.value.length <= 9 || isNaN(field.value) || field.value.length >= 11) )
		return false;
	else 
		return true;
}

// This will check on the phone number and will not allow a blank one to be entered
function phone_checkBlank(field) 
{
	char_storage="";
	char_right_p=')';
	char_left_p='(';
	char_dash='-';
	char_period='\.';
	char_space=' ';
	for (var i=0;i<field.value.length;i++){
		char_to_test=field.value.substring(i,i+1);
		if ((char_right_p.indexOf(char_to_test)<0)&&(char_left_p.indexOf(char_to_test)<0)
		&&(char_dash.indexOf(char_to_test)<0)&&(char_space.indexOf(char_to_test)<0)&&(char_period.indexOf(char_to_test)<0)){
			char_storage+=char_to_test;
		}
	}
	field.value=char_storage;
	
	if ( field.value.length <= 9 || isNaN(field.value) || field.value.length >= 11 )
		return false;
	else 
		return true;
}

// This will strip the phone field to just numbers
function stripPhone( field )
{
	var newField = field.value.replace(/\./gi,"");
	var newField2 = newField.replace(/\(/gi,"");
	var newField3 = newField2.replace(/\)/gi,"");
	var newField4 = newField3.replace(/\-/gi,"");
	var newField5 = newField4.replace(/\s+/g,"");

	field.value = newField5;
}

function trim(argString) 
{
	var trimString = argString;

	while (trimString.charAt(0) == " ")
		trimString = trimString.substring(1, trimString.length);

	while (trimString.charAt(trimString.length - 1) == " ")
		trimString = trimString.substring(0, trimString.length - 1);

	return trimString;
}

// Pass the number you want to round to and 
// then enter the number of decimal places you want it rounded to
// Ex: var rnd = roundNumber( 124.567, 2 ) = 124.57
function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	
	return result;
}

function formatCurrency( num, decPlaces ) 
{
	num = num.toString().replace(/\$|\,/g,'');
	num = roundNumber( num, decPlaces );
	
	if( isNaN( num ) )
		num = "0";
		
	var arrCents = ( new String( num ) ).split('.');
	sign = ( num == ( num = Math.abs( num ) ) );
	num = Math.floor( num * 100 + 0.50000000001 );
	cents = '';
	
	if( arrCents.length > 1 )
		cents = arrCents[ 1 ];
	
	num = Math.floor( num / 100 ).toString();
	
	if( trim( cents ) != '' )
	{
		for( var xj = 0; xj < ( parseInt( decPlaces ) - parseInt( cents.length ) ); xj++ )
			cents = cents + "0";
	}
	else
	{
		for( var xj = 0; xj < ( parseInt( decPlaces ) ); xj++ )
			cents = cents + "0";
	}
	
	
	for( var i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++ )
		num = num.substring( 0, num.length-( 4 * i + 3 ) ) + ',' + num.substring( num.length - ( 4 * i + 3 ) );

	return ( ( (sign)?'':'-' ) + '$' + num + '.' + cents);
}


// This will retrieve the value of the radio button
function getRadioValue( buttonGroup ) 
{
	var v = "";
	for (var i = 0; i < buttonGroup.length; i++) 
	{
		if( buttonGroup[i].checked ) 
		{
			v = buttonGroup[i].value;
			break;
		}
	}
	
	return v;
}

// When dealing with ASP.NET you have to get the value of the 
// radio button list differently than how you would normally do it
// btnGroup - the name of the group of radiobuttons
// num - the number of radio buttons in the group
// Example: getRadioListValue( 'document.form.EmailPromotion_', 2 )
function getRadioListValue( btnGroup, num )
{
	var rlValue = "";
	
	for( var i=0; i < num; i++ )
	{
		cb = eval( btnGroup + i );
		if( cb.checked )
		{
			rlValue = cb.value;
		}
	}
	
	return rlValue;
}


//this checks the zip code to see if it is correct
function checkzip( field ) 
{
	if( trim( field.value ) == "" )
		return false;

	if( field.value.length != 5 && field.value.length != 10 )
		return false;

	// make sure first 5 digits are a valid integer
	if( field.value.charAt(0) == "-" || field.value.charAt(0) == "+" )
		return false;

	if( field.value.length == 5 )
		return true;

	// check if separator is either a'-' or ' '
	if( field.value.charAt(5) != "-" && field.value.charAt(5) != " " )
		return false;

	// check if last 4 digits are a valid integer
	if( field.value.charAt(6) == "-" || field.value.charAt(6) == "+" )
		return false;
}

//This function checks listboxes
function checkList( doc ) 
{
	var isSelected = false;
	
	for (var i=0; i < doc.length; i++) 
	{
		if (doc.options[i].value != "" && doc.options[i].selected == true)
		{
			isSelected = true;
			break;
		}
	}
	
	return isSelected;
}

//This function selects based on the type specified for a dropdown
function selectList( field, strSelect, strType ) 
{
	if( strType == 'value' )
	{
		for( var i=0; i < field.length; i++ ) 
		{
			if( field.options[i].value == strSelect )
			{
				field.options[i].selected = true;
				break;
			}
		}
	}
	else
	{
		for( var i=0; i < field.length; i++ ) 
		{
			if( field.options[i].text == strSelect )
			{
				field.options[i].selected = true;
				break;
			}
		}
	}
}

// This will return the value or text of a select object
function getSelectText( obj, type )
{
	var objText = "";
		
	// It will only return the text if the select value is not blank
	if( obj.options[obj.selectedIndex].value != "" && type == 'text' )
		objText = obj.options[obj.selectedIndex].text;
		
	// It will only return the value if the select value is not blank
	if( obj.options[obj.selectedIndex].value != "" && type == 'value' )
		objText = obj.options[obj.selectedIndex].value;
			
	return objText;
}

// This function will strip out the dollar signs
function stripDollar( elem )
{
   var newElem = elem.value.replace(/\$/gi,"");
   var newElem2 = newElem.replace(/\,/gi,"");
   
   elem.value = newElem2;
   
   return newElem2;
}

// This function will strip out the dollar signs
function stripDollarText( elem )
{
	var newElem = elem.replace(/\$/gi,"");
	var newElem2 = newElem.replace(/\,/gi,"");
		
	elem.value = newElem2;
		
	return newElem2;
}

function stripCurrencyByElem( elem )
{
	var newElem = elem.innerText.replace(/\$/gi,"");
	var newElem2 = newElem.replace(/\,/gi,"");
	
	return newElem2;
}

// This function will strip out the percentage signs
function stripPercent( elem )
{
   var newElem = elem.value.replace(/\%/gi,"");
   var newElem2 = newElem.replace(/\,/gi,"");
   
   elem.value = newElem2;
   
   return newElem2;
}

// This function will strip out the percentage signs
function stripPercentText( elem )
{
   var newElem = elem.replace(/\%/gi,"");
   var newElem2 = newElem.replace(/\,/gi,"");
   
   elem.value = newElem2;
   
   return newElem2;
}

// This function checks radio buttons and checkboxes
function Multiple( buttonGroup ) 
{
	var jx = 0;
	
	if( buttonGroup )
	{
		if( buttonGroup.length > 0 )
		{
			for( var i = 0; i < buttonGroup.length; i++ ) 
			{
				if( buttonGroup[i].checked ) 
				{
					jx = 1;
					break;
				}
			}
		}
		else
		{
			if( buttonGroup.checked )
				jx = 1;
		}
	}
	
	if (jx == 1)
		return true;
	else 
		return false;
}

// This will check that something has been selected within a 
// RadioList.  Should also be able to work with a CheckBoxList
function checkRadioList( buttonGroup, num ) 
{
	var checkedList = false;
	
	for( var i=0; i < num; i++ )
	{
		rad = eval( buttonGroup + i );
		if( rad && rad.checked )
		{
			checkedList = true;
			break;
		}
	}
	
	return checkedList;
}

// This will retrieve the value of the radio button
// Need to pass in an array of Radio button names
// because ASP.NET signifies each one differently in the id
function getRadioValue( buttonGroup, arrName )
{
	var radValue = "";
	
	for( var i=0; i < arrName.length; i++ )
	{
		rad = eval( buttonGroup + arrName[ i ] );
		if( rad && rad.checked )
		{
			radValue = rad.value;
			break;
		}
	}
	
	return radValue;
}

// This function will cause all the checkboxes to be checked
function checkAllCB( buttonGroup ) 
{
	if( buttonGroup )
	{
		if( buttonGroup.length > 0 )
		{
			for( var i = 0; i < buttonGroup.length; i++ ) 
				buttonGroup[i].checked = true;
		}
		else
			buttonGroup.checked = true;
	}
}

// This function will cause all the checkboxes to be unchecked
function unCheckAllCB( buttonGroup ) 
{
	if( buttonGroup )
	{
		if( buttonGroup.length > 0 )
		{
			for( var i = 0; i < buttonGroup.length; i++ ) 
				buttonGroup[i].checked = false;
		}
		else
			buttonGroup.checked = false;
	}
}

// This function will cause all the checkboxes but the one
// to be unchecked if it itself is checked
function unCheckCBButOne( buttonGroup, index ) 
{
	if( buttonGroup )
	{
		if( buttonGroup.length > 0 )
		{
			// if the checkbox is checked then uncheck all the rest
			if( buttonGroup[ index ].checked )
			{
				for( var i = 0; i < buttonGroup.length; i++ ) 
				{
					if( i == index )
						continue;
						
					buttonGroup[i].checked = false;
				}
			}
		}
	}
}

// This function will make sure that textarea fields are not too big
function textCounter( field,maxlimit ) 
{
    if (field.value.length > maxlimit) 
    {      
        alert('Your comments were too long it can only contain '+ maxlimit + ' characters.\nYou entered '+ field.value.length +' characters.');
        field.value = field.value.substring(0, maxlimit - 1 );
    }
}

// This function will return the number of options selected
function checkListCount(doc)
{
	var count = 0;
	for( var i=0; i < doc.length; i++ )
	{
		if( doc.options[i].selected == true )
			count++;
	}
	
	return count;
}
			
// This function will return the number of checkboxes checked
function checkCBCount( fieldName )
{
	var count=0;
	
	for( counter=0;counter<document.forms[0].elements.length;counter++ )
	{
		if( document.forms[0].elements[counter].type == "checkbox" && ( document.forms[0].elements[counter].name.indexOf( fieldName ) > -1 || document.forms[0].elements[counter].id.indexOf( fieldName ) > -1 ) )
		{
			if( document.forms[0].elements[counter].checked == true )
				count++;
		}
	}
	
	return count;
}

// This will look for the name of the checkboxes 
// and decide if it needs to check or uncheck all of them
function checkAndUnCheckAll( fieldName )
{
	for( counter=0; counter<document.forms[0].elements.length; counter++ )
	{
		if( document.forms[0].elements[counter].type == "checkbox" && ( document.forms[0].elements[counter].name.indexOf( fieldName ) > -1 || document.forms[0].elements[counter].id.indexOf( fieldName ) > -1 ) )
		{
			if( !document.forms[0].elements[counter].checked )
				document.forms[0].elements[counter].checked = true;
			else
				document.forms[0].elements[counter].checked = false;
		}
	}
}

// This will look for the name of the checkboxes 
// and decide if the checkbox is checked based on value
function IsCBCheckedByValue( fieldName, strValue )
{
	for( counter=0; counter<document.forms[0].elements.length; counter++ )
	{
		if( document.forms[0].elements[counter].type == "checkbox" && ( document.forms[0].elements[counter].name.indexOf( fieldName ) > -1 || document.forms[0].elements[counter].id.indexOf( fieldName ) > -1 ) )
		{
			if( document.forms[0].elements[counter].checked )
			{
				if( document.forms[0].elements[counter].value == strValue )
					return true;
			}
		}
	}
	
	return false;
}

// This will return the full id name of the partial field name and type given
function getFullFieldIdByPartialFieldNameAndType( fieldName, fieldType )
{
	var fullFieldId = '';
	
	for( var i=0; i < document.forms[0].elements.length; i++ )
	{
		if( document.forms[0].elements[ i ].type == fieldType && document.forms[0].elements[ i ].name.indexOf( fieldName ) > -1 )
		{
			if( document.forms[0].elements[ i ] )
			{
				fullFieldId = document.forms[0].elements[ i ].id;
				break;
			}
		}
	}
	
	return fullFieldId;
}

// This function will check if it is an integer or not
function isInteger( s )
{   
    var i;
        
    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt( i );

        if( !isDigit( c ) ) 
			return false;
    }

    // All characters are numbers.
    return true;
}

function isDigit( c )
{   
	return ( ((c >= "0") && (c <= "9")) || c == ".");
}

function isEmpty( s )
{   
	return (((s == null) || (s.length == 0)) || trim( s ) == "");
}

// Checks to make sure at least one checkbox was checked
function isCBChecked( buttonGroup )
{
    var blnChecked = false;
    
    if( buttonGroup.length > 0 )
    {
		for(var cb=0; cb < buttonGroup.length; cb++ )
		{
			if( buttonGroup[cb].checked == true )
			{
				blnChecked = true;
				break;
			}
		}
    }
    else
    {
        if( buttonGroup.checked == true)
			blnChecked=true;
    }
    
    return blnChecked;
}

// This will make sure that when deleting that the checkbox exists
function checkDelete( buttonGroup, btnName )
{
	if( buttonGroup )
	{
		if( buttonGroup.length > 0 )
		{
			var blnCheck = false;
			for(var i=0; i < buttonGroup.length; i++)
			{
				if( buttonGroup[ i ].checked )
				{
					blnCheck = true;
					break;
				}
			}
			
			if( blnCheck )
				return true;
			else
			{
				alert('No ' + btnName + ' were checked for deletion');
				return false;
			}
		}
		else
		{
			if( !buttonGroup.checked )
			{
				alert('No ' + btnName + ' were checked for deletion');
				return false;
			}
		}
	}
	else
	{
		alert('No ' + btnName + ' to delete');
		return false;
	}
	
	return true;
}

// This will set entire checkbox group checked or unchecked based on the following:
// blnType: true (all are checked)
// blnType: false (all are unchecked)
function setCB( buttonGroup, blnType )
{
    if( buttonGroup.length > 0 )
    {
        for( var cb=0; cb < buttonGroup.length; cb++ )
			buttonGroup[cb].checked = blnType;
    }
    else
		buttonGroup.checked = blnType;
}

// This will return if the browser is IE or not
function isIE()
{
	var agt = navigator.userAgent.toLowerCase();
	var appVer = navigator.appVersion.toLowerCase();
	var iePos = appVer.indexOf('msie');
	
    if( iePos != -1 ) 
		return true;
	else
		return false;
}


// This will assume that the code to validate
// Leave any of these blank if you don't want to use them
// strFunction - name of the function that will be used to validate the form
// strSubmitFunction - name of the function that will be used to submit it 
// strEvent - name of the event that will be used (only needed for Netscape )
function enterKeySubmit( strFunction, strSubmitFunction, event )
{
	if( isIE() && window.event && window.event.keyCode == 13 )
	{
		if( eval( strFunction ) )
			eval( strSubmitFunction );
	}
		
	if( !isIE() && event && event.which == 13 )
	{
		if( eval( strFunction ) )
			eval( strSubmitFunction );
	}
	
	// This will NOT work in FireFox but needs to
	if( document.addEventListener && event.keyCode == 13 )
	{
		if( eval( strFunction ) )
			eval( strSubmitFunction );
	}
}

// This will submit the form itself if the enter key has been clicked
function enterKeySubmitForm( strFormName, event )
{
	if( isIE() && window.event && window.event.keyCode == 13 )
		eval( "document." + strFormName + ".submit()" );
			
	if( !isIE() && event && event.which == 13 )
		eval( "document." + strFormName + ".submit()" );
}


// This will strip date formats that come in as MM/DD/YYYY to YYYYMMDD
function changeDateFormat( strDate )
{
	var arrDate = strDate.split('/');
	var newDate = "";
	
	// Add the YYYY
	newDate = arrDate[ 2 ];
	
	// then the MM
	newDate += arrDate[ 0 ];
	
	// then the DD
	newDate += arrDate[ 1 ];
	
	return newDate;
}

// This will check that either dateFrom or dateTo is not empty
// when the other has been entered along with making sure that
// the dateTo is not greater than the dateFrom
function EmptyDates( dateFrom, dateTo )
{
	if( trim(dateTo.value) != "" && trim(dateFrom.value) == "" )
	{
		alert('Please enter a date for Date From');
		dateFrom.focus();
		return false;
	}
	
	if( trim(dateFrom.value) != "" && trim(dateTo.value) == "" )
	{
		alert('Please enter a date for Date To');
		dateTo.focus();
		return false;
	}

	if( trim(dateFrom.value) != "" && trim(dateTo.value) != "" )
	{
		var dateFromInt = changeDateFormat( dateFrom.value );
		var dateToInt = changeDateFormat( dateTo.value );
		
		if( parseInt( dateFromInt ) > parseInt( dateToInt ) )
		{
			alert('Date From can not be a date greater than the Date To');
			dateFrom.focus();
			return false;
		}
	}
		
	return true;
}

// This will compare two dates and make sure that the firstDate
// is not greater than the secondDate
function compareDates( firstDate, secondDate )
{
	if( trim(firstDate.value) != "" && trim(secondDate.value) != "" )
	{
		var firstDateInt = changeDateFormat( firstDate.value );
		var secondDateInt = changeDateFormat( secondDate.value );
		
		if( parseInt( firstDateInt ) > parseInt( secondDateInt ) )
			return false;
		else
			return true;
	}
	else
		return true;
}

// This will select the dropdown based on the value or text given
function setDropDown( dl, str, type )
{
	// This will set a dropdown based on the value
	if( type == 'value' )
	{
		for( var i=0; i < dl.length; i++ ) 
		{
			if( dl.options[i].value == str )
				dl.options[i].selected = true;
		}
	}
	
	// This will set a dropdown based on the text
	if( type == 'text' )
	{
		for( var i=0; i < dl.length; i++ ) 
		{
			if( dl.options[i].text == str )
				dl.options[i].selected = true;
		}
	}
}

// This will select all options except the ones
// that don't have anything for their value
function selectOptions( field )
{
	// Only select them if the field exists
	if( field )
	{
		for (var i=0; i < field.length; i++) 
		{
			if( field.options[i].value != "" )
				field.options[i].selected = true;						
		}
	}
}

// This will cause a element to be shown or to be hidden
function showHideElement( elem, showElem )
{
	if( document.getElementById( elem ) )
	{
		if( showElem )
			document.getElementById( elem ).style.display = '';
		else
			document.getElementById( elem ).style.display = 'none';
	}
	else
		alert( elem + ' does not exist ');
}

// This will cause a element to be shown or to be hidden
function showHideElement2( elem )
{
	if( document.getElementById( elem ) )
	{
		if( document.getElementById( elem ).style.display == 'none' )
			document.getElementById( elem ).style.display = '';
		else
			document.getElementById( elem ).style.display = 'none';
	}
	else
		alert( elem + ' does not exist ');
}

// This will cause a element and an image to be shown and replaced
function showHideElementWithImg( elem, imgName, imgUp, imgDown )
{
	if( document.getElementById( elem ) )
	{
		if( document.getElementById( elem ).style.display == 'none' )
		{
			document.getElementById( elem ).style.display = '';
			document.images[ imgName ].src = imgUp;
		}
		else
		{
			document.getElementById( elem ).style.display = 'none';
			document.images[ imgName ].src = imgDown;
		}
	}
	else
		alert( elem + ' does not exist ');
}

// This will show and hide certain rows in a table
// tableIdName is the id name of the table to hide the rows in
// rowIdName is the id name of each row that needs to be hidden in
// the table but it only needs partial so if the id name is 'hideTR_1'
// then rowIdName would need to only be 'hideTR' and only those rows that
// have that name will be hidden
function showHideTR( tableIdName, rowIdName )
{
	theRows = document.getElementById( tableIdName ).rows;
	reg = new RegExp( "^" + rowIdName )
	
	for( i=0; i < theRows.length; i++ )
	{
		if( reg.test( theRows[i].id ) )
		{
			if( theRows[i].style.display == "none" ) 
				theRows[i].style.display = ""; 
			else
				theRows[i].style.display = "none";
		} 
	} 
}

// This will show and hide certain rows in a table
// tableIdName is the id name of the table to hide the rows in
// rowIdName is the id name of each row that needs to be hidden in
// the table but it only needs partial so if the id name is 'hideTR_1'
// then rowIdName would need to only be 'hideTR' and only those rows that
// have that name will be hidden
function showHideTRWithImg( tableIdName, rowIdName, imgName, imgUp, imgDown )
{
	theRows = document.getElementById( tableIdName ).rows;
	reg = new RegExp( "^" + rowIdName )
	
	for( i=0; i < theRows.length; i++ )
	{
		if( reg.test( theRows[i].id ) )
		{
			if( theRows[i].style.display == "none" ) 
			{
				theRows[i].style.display = ""; 
				document.images[ imgName ].src = imgUp;
			}
			else
			{
				theRows[i].style.display = "none";
				document.images[ imgName ].src = imgDown;
			}
		} 
	} 
}

// This will swap the image
function swapImage( imgName, img )
{
	if( document.images[ imgName ] )
		document.images[ imgName ].src = img;
}

// The following two functions will allow you to auto tab between fields
function Autotab( FormName, Field )
{
	var fieldLength = Field;

	var autoTabLength=0;
	var fieldPos;
	if( AutoTabFields != null )
	{
		autoTabLength = AutoTabFields.length;	
		fieldPos = getAutoTabPosition(Field.name);
		if( Field.value.length == fieldLength.size )
		{ 
			if( fieldPos < (autoTabLength-1) )
				AutoTabFields[fieldPos+1].focus();
		}
				
	}
}

function getAutoTabPosition(fieldName)
{
	if( AutoTabFields != null )
	{
		autoTabLength = AutoTabFields.length;	
		for (i=0; i< autoTabLength;i++)
		{
			if (AutoTabFields[i].name == fieldName)
				return i;
		}
	}
	else
		return -1;
}
