/**
 * Sets the MCMS page name to the user-entered title.
 */
function SetNewPageName()
{
	var div = document.getElementById("NewsHeadline$AuthoringModeControlsContainer$DisplayName")
	
	try
    {
		var title = trim(div.value);
		var mcmsName = stripIllegalChars(title);
 
        // Make sure we have a valid MCMS name.
		if (mcmsName.toLowerCase() != 'news' && mcmsName != '')
		{
		    //MCMS functions to set page name & tile then save & exit.
		    WBC_setNewPageInfo(mcmsName, title);
		    WBC_doAuthoringNewSaveAndExit();
		    return true;
		}
		else
		{
		    alert('Please provide an Article Headline.');
		    return false;
		}
	}
	catch(e)
	{
	    alert("Could not set PageName: " + title + ", " + mcmsName);
		return false;
	}
}


// Strips spaces from either end of the string.
function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


// Get page name from the title stripping out invalid characters.
   
function SetNewSplashPageName()
{
	try
    {
		//var title = obj.value;
		var title = "Splash";
		var page = "splash";
 
		// Set the new page name.
		WBC_setNewPageInfo(page, title); //CMS FUNCTION TO SET PAGE NAME AND TITLE
		
		return true;
	}
	catch(e)
	{
		return false;
		alert("Could not set PageName: " + title + ", " + page);
	}
}

function stripIllegalChars(title) {
    // the list of characters allowable by MCMS
    var allowableChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _-0123456789().";
    var output = "";

	// Name can't end with a period.
	if (title.substr(title.length -1, 1) == ".") {
		title = title.substr(0, title.length - 1);
	}
	
	var iPrevChar = "";
	for (var i = 0; i < title.length; i++) {
	    var iChar = title.charAt(i);
	    // ensure consecutive period chars ("..") aren't allowed
	    if (!(iPrevChar == "." && iChar == ".")) {
	        if (allowableChars.indexOf(iChar) != -1) {
	            output += iChar;
	        }
	    }
	    iPrevChar = iChar;
	}
	return output;
}


function returnElements(obj, elName)
{
	if (obj.getElementsByTagName)
	{
		return obj.getElementsByTagName(elName)
	}
	else if (obj.tags[elName]) 
	{
		return obj.tags[elName]
	} 
	else alert("Can not find tag " + elName)
}
