var isNav, isIE;
var coll="";
var styleObj="";
var nameExistsMsg = "The Name you entered already exists, please enter a valid name";
var mustBeANmuberMsg = "The Number of columns field must be a number";
var emptyNameFieldMsg  = "The Name field cannot be empty";
var emptyResTypeFieldMsg  = "The Resource Type field cannot be empty";
var questionTextMsg = "Question text cannot be empty";
var filterTextMsg = "Answer text cannot be empty";
var emptyColumnFieldMsg = "The Number of columns field cannot be empty";
var numOfQueryPageColumns = 10;
var dbNameLength = 90;
var dbDescriptionLength = 240;
var dbImageLength = 120;
var dbDataSheetLength = 120;


if(parseInt(navigator.appVersion)>=4){
    if(navigator.appName=="Netscape"){
        //alert("NetScape");
        isNav=true;
    }else{
        //alert("IE");
        isIE=true;
        coll="all.";
        styleObj=".style";
    }
}

function resetContent(inputText, messageString)
{
	alert(messageString);
	inputText.value = "";
	inputText.focus();
}

function notEmptyString(testString)
{
	if( testString.length < 1 )
	{
		return false;
	}
	return true;
}

function getFTFromValue(str)
{
	idx = str.indexOf('_');
	var retStr;
	if ( idx > -1 )
		retStr = new String(str.substring(0,idx));
	else
		retStr = new String(str);
	return retStr;
}

function containsValue(listBox, val)
{
	var len = listBox.options.length;
	for (i=0;i<len;i++)
	{
		if ( listBox.options[i].value.indexOf('_') == -1 )
		{
			if ( listBox.options[i].value == val )
				return i; 
		}
	}
	return -1;
}
function createMoveAndAddElements(toLB, startIndex, fromLB, selArray)
{		
	var fromIndex = toLB.options.length-1;
	var addLen = selArray.length;
	var moveLen = fromIndex - startIndex;
	
	//Create room for new elements
	for ( i=0; i < addLen; i++ )
	{			
		toLB.options[toLB.options.length] = new Option( "", "", false, false);
	}
	//Move existing elements
	var toIndex = toLB.options.length-1;
	for ( i=0; i <= moveLen; i++ )
	{			
		toLB.options[toIndex].text = toLB.options[fromIndex].text;
		toLB.options[toIndex].value = toLB.options[fromIndex].value;			
		fromIndex--;
		toIndex--;
	}
	//Add new elements
	for ( i=0; i < addLen; i++ )
	{
		toLB.options[startIndex].text = fromLB[selArray[i]].text;
		toLB.options[startIndex].value = fromLB[selArray[i]].value;			
		startIndex++;
	}
}

function createAndAddElements(toLB, fromLB, selArray, ftLB)
{
	// First add the Feature type
	var ftIdx = ftLB.selectedIndex;		
	toLB.options[toLB.options.length] = new Option( ftLB[ftIdx].text, 
													ftLB[ftIdx].value, 
													false, false);		
	// Now add all features
	var addLen = selArray.length;
	for ( var i=0; i < addLen; i++ )
	{
		toLB.options[toLB.options.length] = new Option( fromLB[selArray[i]].text, 
														fromLB[selArray[i]].value, 
														false, false);
	}
}
function unassignFeatures(assFeaturesLB, unassFeaturesLB, ftLB)
{
	// to restore back, we'll create an array of arrays of options, one for each
	// feature type
	//if ( ftLB.length < 1 )
	//	return;

	var keepOptionsList = new Array();	// options to keep in the fromList
//	var fTypeId = ftLB.options[ftLB.selectedIndex].value;
	var fTypeId = -1;
	if(ftLB.selectedIndex != -1)
		fTypeId = ftLB.options[ftLB.selectedIndex].value;

	len = assFeaturesLB.options.length;
	var i = 0;
	var j = 0;
	var somethingSelected = false;
	var addToAssignLB = false;
	while (i<len)
	{
		if ( assFeaturesLB.options[i].selected )
		{
			somethingSelected = true;
			// See if moving entire feature type
			if ( assFeaturesLB.options[i].value.indexOf('_') == -1 && i < (len -1) )
			{
				assFeaturesLB.options[i].selected = false;
				i++;
				var FTId = getFTFromValue(assFeaturesLB.options[i].value);
				while (i < len && assFeaturesLB.options[i].value.indexOf('_') > -1)
				{
					if ( FTId == fTypeId )
					{
						unassFeaturesLB[unassFeaturesLB.options.length] = new Option(assFeaturesLB.options[i].text,
																				   assFeaturesLB.options[i].value, false, false);
					}
					assFeaturesLB.options[i].selected = false;
					i++;																				
				}
			}
			else
			{
				var FTId = getFTFromValue(assFeaturesLB.options[i].value);
				if ( FTId == fTypeId )
				{
					unassFeaturesLB[unassFeaturesLB.options.length] = new Option(assFeaturesLB.options[i].text,
																			assFeaturesLB.options[i].value, false, false);
				}
				assFeaturesLB.options[i].selected = false;
				i++;
			}
		}
		else
		{
			keepOptionsList[keepOptionsList.length] = new Option(assFeaturesLB.options[i].text,
						assFeaturesLB.options[i].value, false, false);
			i++;
		}
	}	
	rebuildList(assFeaturesLB, keepOptionsList);
	if (!somethingSelected)
	{
		alert("Please select an item to assign or unassign.");
	}
}

function rebuildList(lb, list)
{
	var i = lb.options.length-1;
	// Loop one and loop two look like they can be combined, but it does not work in IE.
	while (i >= 0)
	{
		lb.options[i--].selected = false;
	}
	var i = lb.options.length-1;
	while (i >= 0)
	{
		lb.options[i--] = null;
	}
	lb.options.length = 0;
	for (i=0;i<list.length;i++)
	{
		lb.options[i] = list[i];
		lb.options[i].selected = false;
	}
}

function assignFeatures(assFeaturesLB, unassFeaturesLB, ftLB)
{
	if ( ftLB.length < 1 )
		return;
	
	var fTypeId = ftLB.options[ftLB.selectedIndex].value;
	var selArray = new Array();
	getSelectedArray( unassFeaturesLB, selArray );
	var ftIndex = containsValue(assFeaturesLB, fTypeId)
	if ( ftIndex > -1 )
	{
		createMoveAndAddElements(assFeaturesLB, ftIndex+1, unassFeaturesLB, selArray);
	}
	else
	{
		createAndAddElements(assFeaturesLB, unassFeaturesLB, selArray, ftLB);
	}
	deleteElements( selArray, unassFeaturesLB );
}

function buildAssignedFeatureList(listBox, hiddenVal)
{	
	i = listBox.options.length;
	var list = "";
	var first = true;
	
	for(j=0;j<i;j++)
	{
		var delimiterIndex = listBox.options[j].value.indexOf('_');
		if ( delimiterIndex > -1 )
		{
			var len = listBox.options[j].value.length;
			var str = new String(listBox.options[j].value.substring(delimiterIndex+1,len));
			if ( !first )
				list = list + "," + str;
			else
			{
				first = false;
				list = str;
			}
			listBox.options[j].selected=false;
		}
	}		
	hiddenVal.value=list;
}

function getSelectedArray(listBox, selArray)
{
	var len = listBox.options.length;
	var j = 0;
	for (i=0;i<len;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			selArray[j++] = i;
			listBox.options[i].selected = false;
		}
	}
}

function buildSelectedList(listBox, hiddenVal)
{
	var len = listBox.options.length;
	var	first = true;
	var list = "";
	for (i=0;i<len;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			if ( !first )
				list = list + "," + listBox.options[i].value;
			else
			{
				first = false;
				list = listBox.options[i].value;
			}

		}
	}		
	hiddenVal.value=list;
}

function addToList(toLB, fromLB, selArray)
{
	var i = 0;
	var len = selArray.length;
	var selName = "";
	var selValue = "";
	while (i < len)
	{
		selName = fromLB.options[selArray[i]].text;
		selValue = fromLB.options[selArray[i]].value;
		toLB.options[toLB.options.length] = new Option(selName, selValue, false, false);
		i++;
	}	
}

function makeAssignments(toLB, fromLB)
{
	var selArray = new Array();
	getSelectedArray( fromLB, selArray );
	addToList(toLB, fromLB, selArray);		
	deleteElements( selArray, fromLB );		
}

function buildListSeqValues(listBox, hiddenVal)
{	
	i = listBox.options.length;
	var list = "";
	var first = true;

	for(j=0;j<i;j++)
	{
		if ( !first )
			list = list + "," + listBox.options[j].value;
		else
		{
			first = false;
			list = listBox.options[j].value;
		}

		listBox.options[j].selected=false;
	}
	hiddenVal.value=list;
}
	
function onlyOneSelected(listBox)
{
	var selCount = 0;
	for (i=0;i<listBox.options.length;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			selIndex = i;
			selCount++;
			if (selCount > 1)
				return false;
		}
	}
	return true;
}

// moves a list item up or down
// args: listBox - the listBox (single select model) 
// 	   up - boolean, true if move up, false if move down
function moveItem(listBox, up)
{
	i = listBox.selectedIndex;
	if ( i==-1 )
	{
		alert("You did not select an item to move. Please select one item and click the Move Up or Move Down button.");
		return;
	}
	if (onlyOneSelected(listBox))
	{
		if ( up && selIndex==0 )
			return;
		else if ( !up && selIndex==listBox.length-1 )
			return;

		var selectedText = listBox.options[selIndex].text;
		var selectedVal = listBox.options[selIndex].value;
    			
		j = selIndex-1;
		if ( !up )
			j = selIndex+1;

		var swapText = listBox.options[j].text;
		var swapVal = listBox.options[j].value;

		listBox.options[selIndex].text = swapText;
		listBox.options[selIndex].value = swapVal;
		listBox.options[j].text = selectedText;
		listBox.options[j].value = selectedVal;
		listBox.options[selIndex].selected = false;
		listBox.options[j].selected = true;
	}
	else
	{
		alert("Only one item can be selected to move up or move down.");
	}
}	

function deleteSelectedItems(listBox, msg)
{
	var length = listBox.options.length;
	for (var i=0;i<length;i++)
	{
		var isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			if ( confirm(msg) )
			{
				deleteItems(listBox);
				return true;	
			} else
				return false;
		}
	}
    return false;
}

function deleteSelectedItems1(listBox, msg)
{
	var length = listBox.options.length;
	for (var i=0;i<length;i++)
	{
		var isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			if ( confirm(msg) )
			{
				deleteItems(listBox);
				return 1;	
			}
			return 0;
		}
	}
    return -1;
}

function deleteItems(listBox)
{
	var length = listBox.options.length;
	var selArray = new Array();
	var j = 0;
	for (var i=0;i<length;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			selArray[j++] = i;
			listBox.options[i].selected = false;
		}
	}
	deleteElements(selArray, listBox);
}


function deleteSelectedFromList(listBox)
{
	var confirmed = false;
	var length = listBox.options.length;
	var selArray = new Array();
	var j = 0;
	for (i=0;i<length;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			if (!confirmed)
			{
				confirmed = true;
				if ( !confirm("Do you want to delete all selected items?"))
				{
					return false;
				}
			}
			selArray[j++] = i;
			listBox.options[i].selected = false;
		}
	}
	deleteElements(selArray, listBox);
	return true;
}



function deleteFromList(listBox, delArray)
{
	var confirmed = false;
	var length = listBox.options.length;
	var selArray = new Array();
	var j = 0;
	var len = delArray.length;
	for (i=0;i<length;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			if (!confirmed)
			{
				confirmed = true;
				if ( !confirm("Do you want to delete all selected items?"))
				{
					return;
				}
			}
			selArray[j] = i;
			delArray[len+j++] = listBox.options[i].value;
			//delArray[j++] = listBox.options[i].value;
			listBox.options[i].selected = false;
		}
	}
	deleteElements(selArray, listBox);
}


function deleteFromListWithMsg(listBox, delArray, msg)
{
	var confirmed = false;
	var length = listBox.options.length;
	var selArray = new Array();
	var j = 0;
	var len = delArray.length;
	for (i=0;i<length;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			if (!confirmed)
			{
				confirmed = true;
				if ( !confirm(msg))
				{
					return;
				}
			}
			selArray[j] = i;
			delArray[len+j++] = listBox.options[i].value;
			//delArray[j++] = listBox.options[i].value;
			listBox.options[i].selected = false;
		}
	}
	deleteElements(selArray, listBox);
}

function getSelectedListItems(listBox)
{
	var confirmed = false;
	var length = listBox.options.length;
	var j = 0;
	var selArray = new Array();
	for (i=0;i<length;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			if (!confirmed)
			{
				confirmed = true;
				if ( !confirm("Do you want to delete all selected items?"))
				{
					break;
				}
			}
			selArray[j++] = i;
			listBox.options[i].selected = false;
		}
	}
	return selArray;
}

function deleteElements(sel, listBox)
{
	var i = sel.length;
	while (i > 0)
	{
		listBox.options[sel[i-1]] = null;
		i--;
	}
}
    	
function buildSelectValueList(listBox, delimiter)
{
    var list = "";
    if(listBox.options.length > 0){
        list += listBox.options[0].value;

	    for(j=1;j<listBox.options.length;j++)
	    {
			list = list + delimiter + listBox.options[j].value;
	    }
	}
	return list;
}

function buildValueList(array, delimiter)
{
    var list = "";
    if(array.length > 0){
        list = list + array[0];
    }
    for(i = 1; i < array.length; i++){
        list = list + delimiter + array[i];
    }
    return list;
}
function isInteger(aNumber)
{
	var s = new String(aNumber);
	var n = s.indexOf("e");
	if(n >= 0 && n < s.length-1)
		return false;
	n = s.indexOf("E");
	if(n >= 0 && n < s.length-1)
		return false;
	n = s.indexOf(".");
	if( n >= 0 )
		return false;
	
	// catch the trailing non-digit
	if (isNaN(aNumber))
		return false;

	// note: this function return NaN if aNumber is empty.
	var n = parseInt(aNumber);
	return !isNaN(n);
}
function isFloat(aNumber)
{
	// catch the trailing non-digit
	if (isNaN(aNumber))
		return false;

	// note: this function return NaN if aNumber is empty.
	var n = parseFloat(aNumber);
	return !isNaN(n);
}

function initQueryPageColumns(qpColLB, colVal, nukeNum)
{
	for ( var i = 0; i < nukeNum; i++ )
	{
		if (qpColLB.options.length > 0)
			qpColLB.options[0] = null;
	}
	for ( var j = 0; j < numOfQueryPageColumns; j++ )
	{
		qpColLB[j] = new Option(j+1, j+1, false, false);
		if (colVal == j+1)
		{
			qpColLB[j].selected = true;		
		}
		else
		{
			qpColLB[j].selected = false;
		}
	}
}

function showPricingError(msg)
{
	alert(msg);
}

function removeLeadingAndTrailingBlanks(str)
{
    str.value = trim(str.value);
}

// trims preceeding and trailing whitespaces 

function trim(strText) 
{	
        // this will get rid of leading spaces 
        while (strText.substring(0,1) == ' ') 
            strText = strText.substring(1, strText.length);

        // this will get rid of trailing spaces 
        while (strText.substring(strText.length-1,strText.length) == ' ')
            strText = strText.substring(0, strText.length-1);

        return strText;
} 
	
function containsSubstring(str, substr) 
{	
    if (str == null)
        return false;
    if ( str.indexOf(substr,0) == -1)
        return false;         
    return true;
} 

function validateNameLength(str) 
{
	return validateLength(str, dbNameLength);
}

function validateDescriptionLength(str)
{
	return validateLength(str, dbDescriptionLength);
}

function validateProductDescriptionLength(str)
{
// product description is 480 characters in length. 
	return validateLength(str, 480);
}

function validateImageLength(str)
{
	return validateLength(str, dbImageLength);
}

function validateDataSheetLength(str)
{
	return validateLength(str, dbDataSheetLength);
}

function validateLength(str, l)
{
	//alert("validating length " + l + " string " + str);
	if (str.length <= l) return true;
	else return false;
}

function validateFloat(num) 
{
    if (num > 9999999999) return false;
    return true;   
}

function validateInteger(num) 
{
    //alert("validating integer " + num);
    if (!isInteger(num) || (num > 9999999999)) return false;
    return true;   
}

function containsElement(listBox, val)
{
	var length = listBox.options.length;
	for (var i=0;i<length;i++)
	{		
		if (listBox.options[i].text.toLowerCase() == val.toLowerCase())
		{
			return true;
		}
	}
	return false;
}

/*
	authur: satheesh
	Construct array of selected element list and array of selected index list.
	selElemList : list of Object. Object has two fields, they are value and text
	selIndexList : array of selected elements index.
*/
function getSelectedElements(listBox,selElemList,selIndexList)
{
	var len = listBox.options.length;
	var j = 0;
	var obj;
	for (i=0;i<len;i++)
	{
		isSelected = listBox.options[i].selected;
		if (isSelected)
		{
			obj = new Object();
			obj.value = listBox.options[i].value;
			obj.text = listBox.options[i].text;
			selElemList[j] = obj;
			selIndexList[j++] = i;
		}
	}	
}

/* Empty the listbox
*/
function initializeListToNull(listToPopulate)
{
	var i = listToPopulate.options.length - 1;
	while(i >= 0)
	{
		listToPopulate.options[i--] = null;
	}
}
/* 
	Test whether a file is a web page file.
*/
function isWebpageFile(name)
{
	if (name == "")
		return false;
	var listArray = name.split(".");
	var suffix = listArray[listArray.length -1];
	suffix = suffix.toLowerCase();
	if (suffix == "" || suffix == "jsp" || suffix == "jspf" || suffix == "html" || suffix == "htm" ||
		suffix == "js")
		return true;
	else
		return false;
}
    
