<!--
//global variables
var ieScroll = 0;
var currentColor;
var selectorId = false;
var timer;
var op = 100;

window.onscroll = updateScroll;

//change the opacity of an element in every modern browser
//@opacity int - the % of opacity
//@id int/object - the object or id of the object to change the opacity of
function changeOpac(opacity, id) 
{ 
	var object = (typeof(id) == 'object') ? id.style : document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
} 
//creates a fade out effect
//@id int/object - the object or the id of the object to create the fade out effect on
//@offset int - the amount to decresse/increase the opacity on each call
function fadeOut(id, offset)
{
	if(op == 0) clearInterval(timer);
	changeOpac(op, id);
	op = op + offset;
	if(op == 100) clearInterval(timer);
}

//updates ieScroll  with the vertical scroll as a dynamic fix for ie6's lack of support for position:fixed
function updateScroll()
{
	ieScroll = document.documentElement.scrollTop;
}

//puts a transparent cover over the entire page
//@text string - the text to show as a loading message
function fakeModal(text)
{
	var modal;
	if(window.ie6)hideSelect();
	if(document.getElementById("coverAll"))
	{
		modal = document.getElementById("coverAll");		
	}
	else
	{
		modal = document.createElement("div");
		modal.id = "coverAll";
		document.body.appendChild(modal);		
	}
	modal.className = "active";
	if(text) showAjaxMsg(text);
}

//Removes the transparent cover over the page
function fakeModalClose()
{
	if(window.ie6)showSelect();
	document.getElementById("coverAll").className = '';
	closeAjaxMsg();	
}
	

//opens the correct page in a new window
//@el object - the button pushed
function popup(el)
{
	var location;
	switch(el.id)
	{
		case "hideEditLinks":
			location = intNLevelId == 0 ? '/index.aspx?frontendeditlinks=hide' : '/index.aspx?nid=' + intNLevelId + '&frontendeditlinks=hide';
			CPwindow = window.open(location,'CPwindow'); 
			CPwindow.focus();
			break;
		case 'viewSite':
			location = intNLevelId == 0 ? '/index.aspx' : '/index.aspx?nid=' + intNLevelId;
			CPwindow = window.open(location,'CPwindow'); 
			CPwindow.focus();
			break;
		case 'frontEndEdit':
			location = intNLevelId == 0 ? '/index.aspx?frontendeditlinks=on' : '/index.aspx?nid=' + intNLevelId + '&frontendeditlinks=on';
			CPwindow = window.open(location,'CPwindow'); 
			CPwindow.focus();
			break;
		case 'siteHelp':
			supportLogin();
			break;
	}
}

//show/hide the loading message
//@msg string - the message to display
function showAjaxMsg(msg)
{
	clearInterval(timer);
	op = 100;
	var el = document.getElementById('ajaxMsg');
	changeOpac(op, el);
	el.className = 'show'
	el.innerHTML = '<img src="/common/images/indicator.gif"> ' + msg;
}

//shows a success message that fades out slowly
function closeAjaxMsg()
{
	document.getElementById("ajaxMsg").innerHTML = 'Success';
	timer = window.setInterval("fadeOut('ajaxMsg', -5)" , 50);
}

//show the page picker
function showMenu()
{
	fakeModal();
	var menu = document.getElementById('menu');
	if(menu.parentNode != document.body) document.body.appendChild(menu);
	menu.className = 'show';
}

//close the page picker
function closeMenu()
{
	document.getElementById("menu").className = '';
	fakeModalClose();
}

//goes to the page picked from the dropdown list
//@el object - the select box
function changeNidSelect(el)
{
	var menu = document.getElementById('menu');
	menu.className = '';
	fakeModal('Loading...');
	document.changeNid.intNLevelID.value = el.value;
	document.changeNid.submit();
}

//goes to the page picked by the mouse over nmenu
//@e object - the event trigger (not used)
function changeNidSelectPM(e) 
{
	var menu = document.getElementById('menu');
	menu.className = '';
	fakeModal('Loading...');	
	document.changeNid.intNLevelID.value = lastNid;
	document.changeNid.submit();
}

//goes to the page typed into the text box
function changeNidManual()
{
	var menu = document.getElementById('menu');
	menu.className = '';
	fakeModal('Loading...');
}

//shows/hides the style sections
//@heading object - the section to show/hide
function toggleSection(heading)
{
	var section = heading.parentNode;
	if (section.className == 'sectionOld show') section.className = 'sectionOld';
	else section.className = 'sectionOld show';
}

//style edit button
//clears out the update box and makes an ajax call to populate it with the selected styles declaration
//@id int - the id of the style to be edited
function styleEdit(id)
{
	fakeModal();
	var updateBox = document.getElementById('updateStyles');
	
	var nodes = updateBox.getElementsByTagName('div');
	for(var i = 0; i < nodes.length; i++) 
	{
		if(nodes[i].className == 'err') 
		{
			nodes[i].className = '';
			nodes[i].getElementsByTagName('span')[0].innerHTML = '';
		}
		
	}	
	if (updateBox.parentNode != document.body) document.body.appendChild(updateBox);
	document.getElementById('inStyleAdd').value = 0;
	selectorId = id;
	makeHttpRequest('/common/admin/styleAdmin/styleAdminDeclarationGet.aspx?sid=' + id + '&default=0', 'populateFeatureBox("bogus"', false);	
}

//style add button
//clears out the update box and makes an ajax call to populate it with the default style with the id passed in
//@id int - the id of the default style to be added
function styleAdd(id)
{
	fakeModal();
	styleClear();
	var updateBox = document.getElementById('updateStyles');
	
	var nodes = updateBox.getElementsByTagName('div');
	for(var i = 0; i < nodes.length; i++) 
	{
		if(nodes[i].className == 'err') 
		{
			nodes[i].className = '';
			nodes[i].getElementsByTagName('span')[0].innerHTML = '';
		}		
	}	
	if (updateBox.parentNode != document.body) document.body.appendChild(updateBox);
	selectorId = id;	
	document.getElementById('inStyleAdd').value = 1;
	makeHttpRequest('/common/admin/styleAdmin/styleAdminDeclarationGet.aspx?sid=' + id + '&default=1', 'populateFeatureBox("bogus"', false);
}

//populates the update box with the provided declaration and shows the update box
//@bogus ?? - junk varibale required by the generic ajax call
//@jsonDoc object - an object with a property for each style in the declartion
function populateFeatureBox(bogus, jsonDoc)
{
	var declaration = eval('(' + jsonDoc + ')');
	
	document.getElementById('fontFamily').value = declaration.fontFamily;
	document.getElementById('fontSize').value = declaration.fontSize;
	document.getElementById('color').value = currentColor = declaration.color;
	document.getElementById('fontWeight').value = declaration.fontWeight;
	document.getElementById('fontStyle').value = declaration.fontStyle;
	document.getElementById('textDecoration').value = declaration.textDecoration;
	
	var updateBox = document.getElementById('updateStyles');
	updateBox.className = 'show';
}

//style remove button
//makes the remove and edit button inactive and makes an ajax call to remove the selected declaration
//@remove object - the event trigger (image of an x)
//@id int - the id of the declaration to be removed
function styleRemove(remove, id)
{	
	if(confirm('Are you sure you want to remove this style?'))
	{
		fakeModal('Removing...');
		toggleRemove(remove, id);
		makeHttpRequest('/common/admin/styleAdmin/styleAdminDeclarationRemove.aspx?sid=' + id + '&nid=' + intNLevelId, 'styleRemoveAjax(' + id, false);	
	}
}

//Updated the selector id with the default id for the style and changes the edit button to an add button
//@id int - the id of the old declaration
//@newId int - the id of the new style
function styleRemoveAjax(id, newId)
{
	var row = document.getElementById('selector' + id);
	var edit = row.getElementsByTagName('img')[0];	
	row.id = 'selector' + newId;
	toAdd(edit, newId);
	fakeModalClose();
}

//closes the update box, used when canceling an update
function closeUpdate()
{
	var updateBox = document.getElementById('updateStyles');
	fakeModalClose();
	updateBox.className = '';
}

//clears out the update box of any style settings
function styleClear()
{
	document.getElementById('textDecoration').selectedIndex = document.getElementById('fontWeight').selectedIndex = document.getElementById('fontStyle').selectedIndex = 0; 
	document.getElementById('fontFamily').value = document.getElementById('fontSize').value = currentColor = document.getElementById('color').value = '';
}

//Validate styles and make an ajax call to update the database
function stylesValidate()
{
	if(updateStylesValidate())
	{
	if(selectorId)
	{
		showAjaxMsg('Updating...');
		var isDefault = document.getElementById('inStyleAdd').value;
		var sid = selectorId;
		var row = document.getElementById('selector' + sid);
		var selector = row.getElementsByTagName('td')[1];
		
		var declaration = '';		
		if(document.getElementById('fontWeight').value != '') declaration += 'font-weight:' + document.getElementById('fontWeight').value + ';';		
		if(document.getElementById('fontFamily').value != '') declaration += 'font-family:' + document.getElementById('fontFamily').value + ';';
		if(document.getElementById('textDecoration').value != '') declaration += 'text-decoration:' + document.getElementById('textDecoration').value + ';';
		if(document.getElementById('fontStyle').value != '') declaration += 'font-style:' + document.getElementById('fontStyle').value + ';';
		if(document.getElementById('fontSize').value != '') declaration += 'font-size:' + document.getElementById('fontSize').value + 'pt;';
		if(document.getElementById('color').value != '') 
		{
			declaration += 'color:' + document.getElementById('color').value + ';';
			if(selector.title == 'hr.menu') declaration += 'background-color:' + document.getElementById('color').value + ';';
		}		
		declaration = escape(declaration);
		
		makeHttpRequest('/common/admin/styleAdmin/styleAdminDeclarationUpdate.aspx?sid=' + sid + '&declaration=' + declaration + '&default=' + isDefault + '&nid=' + intNLevelId, 'updateStyles(' + sid + ',' + isDefault, false);
		selectorId = false;
	}
		
	}
}

//closes the update box and if the style updated was a default style change the add button to an edit button, add the remove button, and update the selecor id
//id int - the id of the style
//isDefault bit - 1 if the style was added, 0 if it was updated
//newId int - if the style was added the new id of the style
function updateStyles(id, isDefault, newId)
{
	var updateBox = document.getElementById('updateStyles');
	if(isDefault == 1)
	{
		var row = document.getElementById('selector' + id);
		var edit = row.getElementsByTagName('img')[0];
		var remove = row.getElementsByTagName('img')[1];
		row.id = 'selector' + newId;
		toEdit(edit, newId);
		toggleRemove(remove, newId);
	}
	updateBox.className = '';
	fakeModalClose();
}

//validates the styles
function updateStylesValidate() 
{
	var colorText = 'Color must be in entered as a hex value';
	if (!RegExValidate('^#[0-9a-f]{3}|#[0-9a-f]{6}$', currentColor, 'i')) colorText += ' or ' + currentColor;
	var fontSize = validate('^[0-9]*$', 'fontSize', 'i', 'Font size must be a number');
	var fontFamily = validate('^[a-z0-9 ]*$', 'fontFamily', 'i', 'Font can only contain letters, numbers and spaces');
	var color = validate('^(#[0-9a-f]{3}|#[0-9a-f]{6}|' + currentColor + ')?$', 'color', 'i' ,colorText);
	if (fontSize && fontFamily && color) return true;

	return false;
}

//used by updateStylesValidate to validate styles, if the form value does not match the regex passed in it adds an error message to the form
//@regex string - the regular expression
//@id int - the id of the event trigger
//@param string - the regex params (case, global, etc.)
//@msg string - the message to display if their is an error
function validate(regex, id, param, msg) 
{
	var el = document.getElementById(id);
	var wrapper = el.parentNode;
	var elMsg = el.parentNode.getElementsByTagName('span')[0];
	
	if(RegExValidate(regex, el.value, param)) 
	{
		elMsg.innerHTML = "";
		wrapper.className = "";
		return true;
	} 
	else if (wrapper.className != 'err')
	{
		elMsg.innerHTML = msg;
		wrapper.className = "err";
		return false;
	}
	return false;
}

//removes this feature from the dropdown list and makes an ajax call to add the styles to this section
//@img object - the event trigger (add feature button)
//@section int - the id of the section to add the feature to
function featureAdd(img, section)
{
	fakeModal('Adding Feature...');
	var form = img.parentNode;
	var select = form[0];
	var feature = select.value;
	var featureName = select[select.selectedIndex].innerHTML;
	
	select.removeChild(select[select.selectedIndex]);
	if(select.length == 0)
	{
		var table = document.getElementById('section' + section);
		table.getElementsByTagName('tfoot')[0].className = 'hide';
	}
	
	makeHttpRequest('/common/admin/styleAdmin/styleAdminFeatureAdd.aspx?nid=' + intNLevelId + '&fid=' + feature + '&sid=' + section,'featureAddToTable(' + section + ',"' + feature + '","' + featureName + '"', false);
}

//adds all the styles from the feature to the provided section
//@section int - the id of the section
//@feature int - the id of the feature to add
//@featureName string - the name of the feature to add
//@jsonDoc object - an object with properties for each style to add (name, selector, id, etc)
function featureAddToTable(section, feature, featureName, jsonDoc)
{
	var selectors = eval('(' + jsonDoc + ')');
	
	var table = document.getElementById('section' + section);
	
	var newRow;
	var rowHeader;
	var name;
	var selector;
	var edit;
	
	var tbody = document.createElement("TBODY");
	tbody.id = 'section' + section + 'Feature' + feature;
	
	newRow = table.insertRow(-1);
	
	name = document.createElement("th");
	name.className = "featureName";
	name.onclick = function() {featureHideToggle('section' + section + 'Feature' + feature);};
	remove = document.createElement("th");
	
	newRow.appendChild(name);
	newRow.appendChild(remove);
	
	name.innerHTML = featureName;
	name.colSpan = "3";
	remove.className = "remove";
	remove.innerHTML = '<img src="/common/images/delete.gif" alt="Remove This Feature" onclick="featureRemove(' + section + ', ' + feature + ', \'' + featureName + '\');"/>';
	
	tbody.appendChild(newRow);
	for(var i = 0; i < selectors.selectors.length; i++)
	{

		newRow = table.insertRow(-1);
		newRow.id = 'selector' + selectors.selectors[i].id;
		newRow.className = 'featureSelector';
		
		name = newRow.insertCell(-1);
		selector = newRow.insertCell(-1);
		edit = newRow.insertCell(-1);
		remove = newRow.insertCell(-1);
		
		name.innerHTML = selectors.selectors[i].name;
		selector.innerHTML = "<div>" + selectors.selectors[i].selector + "</div>";
		selector.className = "selector";
		remove.className = "remove";
		if(selectors.selectors[i].edit == 1)
		{
			remove.innerHTML = '<img src="/common/images/delete.gif"  alt="Remove this Style" onclick="styleRemove(this, ' + selectors.selectors[i].id + ');" />';
			edit.className = "edit";
			edit.innerHTML = '<img src="/common/images/edit.gif" alt="Edit this style" onclick="styleEdit(' + selectors.selectors[i].id + ');"/>';
		}
		else
		{			
			remove.innerHTML = '<img src="/common/images/delete.gif"  class="inactive" alt="Remove this Style" />';
			edit.className = "edit";
			edit.innerHTML = '<img src="/common/images/plus4.gif"alt="Add this style" onclick="styleAdd(' + selectors.selectors[i].id + ');"/>';
		}
			
		tbody.appendChild(newRow);
	}
	
	table.appendChild(tbody);
	fakeModalClose();
}

//change the add button to an edit button
//@img object - the button to change
//@id int - the id of the style
function toEdit(img, id)
{
	img.src = '/common/images/edit.gif';
	img.alt = 'Edit this style';
	img.className = '';
	img.onclick = function(){styleEdit(id);};
}

//change the edit button to an add button
//@img object - the button to change
//@id int - the id of the style
function toAdd(img, id)
{
	img.src = "/common/images/plus4.gif";
	img.alt = "Add this Style";
	img.className = '';
	img.onclick = function(){styleAdd(id);};
}

//activate/deactivate the remove button
//@img object - the button to change
//@id int - the id of the style
function toggleRemove(img, id)
{
	if(img.className == 'inactive')
	{
		img.className = '';
		img.onclick = function(){styleRemove(this, id);};
	}
		else
	{
		img.className = "inactive";
		img.onclick = "";
	}
}

//makes an ajax call to rebuild the css file for this page
//@button object - the rebuild css button
function rebuildCSS(button)
{
	if(confirm('This will take some time\nand the site may look funny during the process.\nAre you sure?'))
	{
		showAjaxMsg('Rebuilding...');
		button.disabled = true;
		makeHttpRequest('/common/admin/styleAdmin/styleadminRebuildCSS.aspx?nid=' + intNLevelId, 'rebuildCSSAjax("bogus"', false);
		
	} 
}

//activates the rebuild css button after a successfull rebuild
//@bogus ?? - junk variable needed due to bug in makeHttpRequest that requires at least two variables
//@text string - the return text from ajax call (not used)
function rebuildCSSAjax(bogus, text)
{
	document.getElementById('rebuild').disabled = false;
	closeAjaxMsg();
}

//disables the inherit checkbox and makes an ajax call to toggle the inheritance in the database for this page
//@button object - the inherit checkbox
function toggleInherit(button)
{
	showAjaxMsg('Updating...');
	button.disabled = true;
	var checked = button.checked ? 1 : 0;
	makeHttpRequest('/common/admin/styleAdmin/styleadminToggleInherit.aspx?nid=' + intNLevelId + '&checked=' + checked, 'toggleInheritAjax("bogus"', false);
}

//enables the inherit checkbox and displays a success message
//@bogus ?? - junk variable needed due to bug in makeHttpRequest that requires at least two variables
//@text string - the return text from ajax call (not used)
function toggleInheritAjax(bogus, text)
{
		document.getElementById('ysnApplyStyles').disabled = false;
		closeAjaxMsg();
}

//Removes a feature section from the dom and makes an ajax call to remove it from the db
//@section int - the id of the section to remove the feature from
//@feature int - the id of the feature to remove
//@name string - the name of the feature to remove
function featureRemove(section, feature, name)
{
	if(confirm('Are you sure you want to remove this feature.'))
	{
		var objFeature = document.getElementById('section' + section + 'Feature' + feature);
		objFeature.parentNode.removeChild(objFeature);
		makeHttpRequest('/common/admin/styleAdmin/styleadminorphanget.aspx?nid=' + intNLevelId + '&fid=' + feature + '&sid=' + section,'featureAddToDropDown(' + section + ',"' + name + '","' + feature + '"', false);
	}
}

//if the feature is in layout admin for this page the style is added back to the dropdown box
//@section int - the section to add the drop down feature back into
//@name string - the name of the feature
//@feature int - the id of the feature
//@isOrphan bit - 1 if the feature is not int layout admin, 0 if it is
function featureAddToDropDown(section, name, feature, isOrphan)
{
	if(isOrphan == 0)
	{	
		var tfoot = document.getElementById('section' + section).getElementsByTagName('TFOOT')[0];
		tfoot.className = "";
		var select = document.getElementById('addFeature' + section);
		var option = document.createElement('option');
		option.value = feature;
		option.innerHTML = name;
		select.appendChild(option);	
	}
}

//toggles the visiblity of an html element
//@id int - the id of the element to show/hide
function featureHideToggle(id)
{
	var tbody = document.getElementById(id);
	if(tbody.className == 'hide') tbody.className = '';
	else tbody.className = 'hide';
}
-->