/*******************************************************************************
' Function Name		On load javascript functions
'
' Description		This file is used to handle the page load functions			
'*******************************************************************************/
function fnOnload()
{
	// Use this to call all other functions
	
	// Setting default values for fields
	fnSetDefaultValuesForFields();
	
	// Display Line Break
	fnTitleValidate();
	
	// Add domain to the title
	document.title = document.domain + " - " + document.title;
}


/*******************************************************************************
' Function Name		fnSetDefaultValuesForFields	(and related functions)			
'
' Description		This function sets the field default values
'					on pageload.
'*******************************************************************************/

/*******************************************************************************
SharePoint Field Type	|	identifier		|	tagName
*******************************************************************************
Single Line of Text 	|	TextField 		|	input
Multiple Lines of Text 	|	TextField 		|	input
Number 			        |	TextField 		|	input
Choice (dropdown) 	    |	DropDownChoice	| 	select
Yes/No 			        |	BooleanField 	|	input
'*******************************************************************************/
function fnSetDefaultValuesForFields() 
{
	
	// Set the default values
	fnSetDefaultValues("select","DropDownChoice", "Display Left Nav", "Yes");
	fnSetDefaultValues("select","DropDownChoice", "Window Style", "Self");
	fnSetDefaultValues("select","DropDownChoice", "Display User Control", "Yes");
}


function fnSetDefaultValues(tagName, identifier, title, value)
{
	var theSelect = fnGetTagFromIdentifierAndTitle(tagName.toLowerCase(), identifier.toLowerCase(), title.toLowerCase());

	if (tagName.toLowerCase() == "select")
	{
		if (theSelect != null && theSelect.value == "")
			fnSetSelectedOption(theSelect, value);
	}
	else if (tagName.toLowerCase() == "input")
	{		
		if (identifier.toLowerCase() == "booleanfield")
		{
			if (theSelect != null && theSelect.checked == false)
				theSelect.checked = true;
		}
		else
		{	
			if (theSelect != null && theSelect.value == "")
				theSelect.value = value;
		}
	}
} 

function fnSetSelectedOption(select, value) 
{
	var opts = select.options;
	var l = opts.length;
	
	if (select == null) return;
	
	for (var i = 0; i < l; i++) 
	{
		if (opts[i].value.toLowerCase() == value.toLowerCase()) 
		{		
			select.selectedIndex = i;
			return true;
    	}
  	}
  	return false;
}

function fnGetTagFromIdentifierAndTitle(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.toLowerCase();
		
		if (tags[i].title.toLowerCase() == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) 
		{
			return tags[i];
		}
  	}
  	
  	return null;
}


/*******************************************************************************
' Function Name		fnTitleValidate				
'
' Description		This function handles hiding and displaying of underline
'					on pageload.
'*******************************************************************************/
function fnTitleValidate()
{
	if (document.getElementById("ctl00_PlaceHolderMain_rchPageTitle__ControlWrapper_RichHtmlField"))
	{
		var tableText = document.getElementById("ctl00_PlaceHolderMain_rchPageTitle__ControlWrapper_RichHtmlField");

		if (tableText.innerHTML == "")
		{
			document.getElementById("TitleRow").style.display = "none";	
		}
	}	
}

// Push onload function

_spBodyOnLoadFunctionNames.push("fnOnload");
