﻿/* Utilities.js -- This Javascript file contains helper functions and utilities used
	throughout the Site Collection. */
	
	var navBarHelpOverrideKey = "wssmain";
	var custname = new Array(); 

// setLookupFromFieldName: Set a form field value using its fieldname to find it in the page
// Arguments:
//		fieldName:	The name of the list column
//		value:		Set the fieldName to this value
//
	function setLookupFromFieldName(fieldName, value) {
		alert('setLookupFromFieldName: fieldname=' + fieldName + ' value=' + value);
		if (value == undefined) return;
		var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
		if(theInput != null) {
			theInput.value = value;
		}
	}

// getValueWithFieldName: Get a form field value using its tagName, fieldname, and identifier to find it in the page
// Arguments:
//		tagName:	The type of input field (input, select, etc.)
//		fieldName:	The name of the list column
//		identifier:	The identifier for the instance of the fieldName (ff1, ff2, etc.)
//
	function getValueWithFieldName(tagName, fieldName, identifier) {
		//alert('getValueWithFieldName: tagName=' + tagName + ' fieldname=' + fieldName + ' identifier' + identifier);
		var theInput = getTagFromIdentifierAndFieldname(tagName, fieldName, identifier);
		//alert('getValueWithFieldName: theInput.id=' + theInput.id + ' theInput.value=' + theInput.value);
		return theInput.value;
	}

// setValueWithFieldName: Set a form field value using its tagName, fieldname, and identifier to find it in the page
// Arguments:
//		tagName:	The type of input field (input, select, etc.)
//		fieldName:	The name of the list column
//		identifier:	The identifier for the instance of the fieldName (ff1, ff2, etc.)
//		value:		Set the fieldName to this value
//
	function setValueWithFieldName(tagName, fieldName, identifier, value) {
		//alert('setValueWithFieldName: tagName=' + tagName + ' fieldname=' + fieldName + ' identifier' + identifier + ' value=' + value);
		var theInput = getTagFromIdentifierAndFieldname(tagName, fieldName, identifier);
		//alert('setValueWithFieldName: theInput.value (current value)=' + theInput.value);
		if(theInput != null) {
			theInput.value = value;
		//alert('setValueWithFieldName: theInput.value (new value)=' + theInput.value);
		}
	}

// getTagFromIdentifierAndFieldname: Get an input field's tag object using its tagName, fieldname, and identifier to find it in the page
// Arguments:
//		tagName:	The type of input field (input, select, etc.)
//		fieldName:	The name of the list column
//		identifier:	The identifier for the instance of the fieldName (ff1, ff2, etc.)
//
	function getTagFromIdentifierAndFieldname(tagName, fieldName, identifier) {
		//alert('getTagFromIdentifierAndTitle: tagName=' + tagName + ' fieldname=' + fieldName + ' identifier' + identifier);
		var len = identifier.length;
		var tags = document.getElementsByTagName(tagName);
		for (var i=0; i < tags.length; i++) {
			if (tags[i].title == fieldName) {
				//alert('HIT tags[' + i + '].title' + tags[i].title + ' tags[' + i + '].id=' + tags[i].id + ' identifier=' + identifier);
				if((tags[i].id == identifier) || (tags[i].id.indexOf(identifier) > 0)) {
					return tags[i];
				}
			}
		}
		return null;
	}
// getTagFromIdentifierAndTitle: Find a form field object using its tagName, identifier, and title to find it in the page
// Arguments:
//		tagName:	The type of input field (input, select, etc.)
//		identifier:	The identifier for the instance of the fieldName (ff1, ff2, etc.)
//		title:		The title of the list column
//
	function getTagFromIdentifierAndTitle(tagName, identifier, title) {
		var len = identifier.length;
		var tags = document.getElementsByTagName(tagName);

		for (var i=0; i < tags.length; i++) {
			var tempString = tags[i].id;
			if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
				return tags[i];
			}
		}
		return null;
	}
	
// getPickerInputElement: Find a People Picker's value using its identifier to find it in the page
// Arguments:
//		identifier:	The identifier for the instance of the fieldName (ff1, ff2, etc.)
//
	function getPickerInputElement(identifier) {
		var tags = document.getElementsByTagName('DIV');

		for (var i=0; i < tags.length; i++) {
			var tempString = tags[i].id;
			//alert('tags[' + i + '].id = ' + tempString);
			if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0)){
				//alert('HIT for ' + identifier + ' id=' + tags[i].id + ' value=' + tags[i].value);
				var innerSpans = tags[i].getElementsByTagName("SPAN");
				for(var j=0; j < innerSpans.length; j++) { 
					//alert('innerSpans[' + j + '].id = ' + innerSpans[j].id);
					if(innerSpans[j].id == 'content') {
						//alert('HIT for ' + identifier + ' id=' + innerSpans[j].id + ' innerHTML=' + innerSpans[j].innerHTML);
						return innerSpans[j].innerHTML;
					}
				}		
			}
		}
		return null;
	}
	
// findParentElement: Find an object's specified parent element
// Arguments:
//		thisElement:	The element you want to search from
//		parentTag:		The parent tag you want to find
//
	function findParentElement(thisElement, parentTag) {
		var currentElement = thisElement;
		
		while(currentElement.tagName != parentTag) {
			currentElement = currentElement.parentNode;
			//alert(currentElement.tagName);
			if(currentElement.tagName == parentTag) {
				//alert('HIT:' + currentElement.tagName);
				return currentElement;
			}
		}
		return null;
	}
	
// setSelectWidth: Set the width of a lookup column's select box based on the values it contains
// Arguments:
//		columnName:		The name of the column which is being displayed in the select box
//
	function setSelectWidth(columnName) {
		//alert("setSelectWidth(" + columnName +")");
		var charFactor = 7; // Approximate pixels per character
		
		// Left side
		leftColumnSelect = getTagFromIdentifierAndTitle("select","",columnName + " possible values");
		if(leftColumnSelect != null) {
			leftColumnSelectDIV = findParentElement(leftColumnSelect, "DIV");
	
			// Right side
			rightColumnSelect = getTagFromIdentifierAndTitle("select","",columnName + " selected values");
			rightColumnSelectDIV = findParentElement(rightColumnSelect, "DIV");
	
			// Find the longest value
			var longestValue = 0;
			for (var i=0; i < leftColumnSelect.options.length; i++) {
				if(leftColumnSelect.options[i].text.length > longestValue)
					longestValue = leftColumnSelect.options[i].text.length;
			}
			for (var i=0; i < rightColumnSelect.options.length; i++) {
				if(rightColumnSelect.options[i].text.length > longestValue)
					longestValue = rightColumnSelect.options[i].text.length;
		}
		
			// Set the widths of the two selects and their containing DIVs
			var newWidth = charFactor * longestValue;
			leftColumnSelectDIV.style.width = newWidth;
			leftColumnSelect.style.width = newWidth;
			rightColumnSelectDIV.style.width = newWidth;
			rightColumnSelect.style.width = newWidth;
		}
	}