// =====================================================================================
// <copyright>
// SiteMaker JS.
// Copyright (c) 2007, by Innovative Way, Inc.  All rights reserved.
// This work is a trade secret and unauthorized use or copying is strictly prohibited.
// </copyright>
// =====================================================================================

var dialogShown = false;
var countMoves = 0;

function isArray(TestObj)
{isArrayTest=true;try
{if(isNaN(TestObj.length))
{isArrayTest=false;}}
catch(e)
{isArrayTest=false;}
return(isArrayTest);}

function disableAll()
{
	var oTemp = $('formdesign');
	var el = oTemp.elements;

	for(var i=0; i<el.length; i++)
	{
		el[i].setAttribute('disabled',true);
	}
}

function PostBackForm(FormName)
{ScrollList=GetScrollXY();WebForm=document.getElementById(FormName);PageOffset=ScrollList[1];if(PageOffset==0)
{PageOffset=1;}
PostBackField=document.getElementsByName("postback");if(isArray(PostBackField))
{for(x=0;x<PostBackField.length;x++)
{PostBackField[x].value=PageOffset;}}
else
{PostBackField.value=PageOffset;}
if(WebForm.action.indexOf("?")!=-1)
{WebForm.action+="&PostBack="+PageOffset;}
else
{WebForm.action+="?PostBack="+PageOffset;}
WebForm.submit();}

function PostBackButton(FormName,SubmitButtonName)
{ScrollList=GetScrollXY();WebForm=document.getElementById(FormName);WebButton=document.getElementById(SubmitButtonName);PageOffset=ScrollList[1];if(PageOffset==0)
{PageOffset=1;}
if(WebForm.action.indexOf("?")!=-1)
{WebForm.action+="&PostBack="+PageOffset;}
else
{WebForm.action+="?PostBack="+PageOffset;}
WebButton.click();}

function GetScrollXY()
{var scrOfX=0,scrOfY=0;if(typeof(window.pageYOffset)=='number')
{scrOfY=window.pageYOffset;scrOfX=window.pageXOffset;}
else
{if(document.body&&(document.body.scrollLeft||document.body.scrollTop))
{scrOfY=document.body.scrollTop;scrOfX=document.body.scrollLeft;}
else
{if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop))
{scrOfY=document.documentElement.scrollTop;scrOfX=document.documentElement.scrollLeft;}}}
return[scrOfX,scrOfY];}

function date2cookiedate(dTemp)
{var sDay,sMonth,sYear;var aMonth=new Array(12);aMonth[0]='January';aMonth[1]='February';aMonth[2]='March';aMonth[3]='April';aMonth[4]='May';aMonth[5]='June';aMonth[6]='July';aMonth[7]='August';aMonth[8]='September';aMonth[9]='October';aMonth[10]='November';aMonth[11]='December';sMonth=aMonth[dTemp.getUTCMonth()];sDay=dTemp.getUTCDate();sYear=dTemp.getUTCFullYear();return(sMonth+' '+sDay+', '+sYear);}

function date2access()
{if(!date2access.arguments.length)
dDate=new Date();else
dDate=date2access.arguments[0];str=(dDate.getMonth()+1)+"/"+dDate.getDate()+"/"+dDate.getFullYear();return str;}

function date2time()
{if(!date2time.arguments.length)
dDate=new Date();else
dDate=date2time.arguments[0];sMin=dDate.getMinutes();if(sMin<10)
sMin="0"+sMin;sSec=dDate.getSeconds();if(sSec<10)
sSec="0"+sSec;sHrs=dDate.getHours();sPM="AM";if(sHrs>=12)
{sHrs=sHrs-12;sPM="PM";}
if(sHrs==0)
sHrs=12;str=sHrs+":"+sMin;return str;}

function date2timeindicator()
{if(!date2timeindicator.arguments.length)
dDate=new Date();else
dDate=date2timeindicator.arguments[0];sPM="AM";sHrs=dDate.getHours();if(sHrs>=12)
{sHrs=sHrs-12;sPM="PM";}
return sPM;}

function date2datetime()
{if(!date2datetime.arguments.length)
dDate=new Date();else
dDate=date2datetime.arguments[0];sMin=dDate.getMinutes();if(sMin<10)
sMin="0"+sMin;sSec=dDate.getSeconds();if(sSec<10)
sSec="0"+sSec;sHrs=dDate.getHours();sPM="AM";if(sHrs>=12)
{sHrs=sHrs-12;sPM="PM";}
if(sHrs==0)
sHrs=12;str=(dDate.getMonth()+1)+"/"+dDate.getDate()+"/"+dDate.getFullYear()+" "+sHrs+":"+sMin+":"+sSec+" "+sPM;return str;}

function date2datetimetable()
{if(!date2datetimetable.arguments.length)
dDate=new Date();else
dDate=date2datetimetable.arguments[0];sMin=dDate.getMinutes();if(sMin<10)
sMin="0"+sMin;sSec=dDate.getSeconds();if(sSec<10)
sSec="0"+sSec;sHrs=dDate.getHours();sPM="AM";if(sHrs>=12)
{sHrs=sHrs-12;sPM="PM";}
if(sHrs==0)
sHrs=12;str=(dDate.getMonth()+1)+"/"+dDate.getDate()+"/"+dDate.getFullYear()+"<br>"+sHrs+":"+sMin+" "+sPM;return str;}

function dateadd(iTemp)
{if(dateadd.arguments.length!=2)
dDate=new Date();else
dDate=dateadd.arguments[1];offset=dDate.getDate()+iTemp;tdate=dDate;tdate.setDate(offset);return tdate;}

function monthadd(iTemp)
{if(monthadd.arguments.length!=2)
dDate=new Date();else
dDate=monthadd.arguments[1];offset=dDate.getMonth()+iTemp;tdate=dDate;tdate.setMonth(offset);return tdate;}

function text2date(sTemp)
{rval=null;d=new Date(Date.parse(sTemp));if(!isNaN(d))
rval=d;return rval;}

function daylightstart()
{rval=null;dl=new Date();if(daylightstart.arguments.length>0)
dl.setFullYear(daylightstart.arguments[0]);dl.setMonth(3);dl.setDate(1);dl.setHours(3);dl.setMinutes(0);dl.setSeconds(0);dl.setMilliseconds(0);offset=dl.getDay();if(offset!=0)
{offset=7-dl.getDay()+dl.getDate();dl.setDate(offset);}
rval=dl;return rval;}

function daylightend()
{rval=null;dl=new Date();if(daylightend.arguments.length>0)
dl.setFullYear(daylightend.arguments[0]);dl.setMonth(9);dl.setDate(31);dl.setHours(2);dl.setMinutes(0);dl.setSeconds(1);dl.setMilliseconds(0);offset=dl.getDay();if(offset!=0)
{offset=0-dl.getDay()+dl.getDate();dl.setDate(offset);}
rval=dl;return rval;}

function text2cst(sTemp)
{GMTOffset=-6;GMTDSOffset=-5;rval=null;if(sTemp!=null)
{d=text2date(sTemp);}
else
{d=new Date();}
ds_start=daylightstart(d.getFullYear());ds_end=daylightend(d.getFullYear());if(d!=null)
{if((ds_start.valueOf()<=d.valueOf())&&(d.valueOf()<=ds_end.valueOf()))
offset=d.getHours()+GMTDSOffset;else
offset=d.getHours()+GMTOffset;d.setHours(offset);rval=d;}
return rval;}

function csttext2date(sTemp)
{GMTOffset=6;GMTDSOffset=5;rval=null;if(sTemp!=null)
{d=text2date(sTemp);}
else
{d=new Date();}
ds_start=daylightstart(d.getFullYear());ds_end=daylightend(d.getFullYear());if(d!=null)
{if((ds_start.valueOf()<=d.valueOf())&&(d.valueOf()<=ds_end.valueOf()))
offset=d.getHours()+GMTDSOffset;else
offset=d.getHours()+GMTOffset;d.setHours(offset);rval=d;}
return rval;}

function toggle(element)
{if(document.getElementById)
{var target=document.getElementById(element);if(target.style.display=="none")
{target.style.display="";}
else
{target.style.display="none";}}}

function DivideAndPut(textInput,hiddenInput)
{var inputElement=document.getElementById(textInput);var hiddenElement=document.getElementById(hiddenInput);var inputValue=inputElement.value;var last=inputValue.indexOf(")$$");var first=inputValue.indexOf("$$(");var holder=inputValue.substring((first+3),last);hiddenElement.value=holder;inputElement.value=inputValue.substring(0,first);}

// Form: Checkbox and Radiobuttons
var highlightClassName = "highlight";

function formSetDefaultHighlightCSS(tempCSS)
{
	if (tempCSS == "")
	{
		tempCSS = "highlight";
	}

	highlightClassName = tempCSS;
}

function formSelectAll(tempCSSSelector)
{
	var elements = $$(tempCSSSelector);

	for (var i=0; i<elements.length; i++)
	{	
		if (!elements[i].checked)
		{
			elements[i].click();
		}
	}
}

function formSelectNone(tempCSSSelector)
{
	var elements = $$(tempCSSSelector);

	for (var i=0; i<elements.length; i++)
	{	
		if (elements[i].checked)
		{
			elements[i].click();
		}
	}
}

function SelectAll(elementNames)
{
	var elements=document.getElementsByName(elementNames);

	for(var i=0;i<elements.length;i++)
	{
		elements[i].checked=true;
		var parent=elements[i].parentNode;
		if(parent.className.indexOf(highlightClassName)==-1)
		{
			parent.className+=' '+highlightClassName;
		}
	}
}

function SelectNone(elementNames)
{
	var elements=document.getElementsByName(elementNames);

	for(var i=0;i<elements.length;i++)
	{
		elements[i].checked=false;
		var parent=elements[i].parentNode;
		parent.className=parent.className.replace(''+highlightClassName,'');
	}
}

var highlightInputs = Class.create();
highlightInputs.prototype = 
{ 
	initialize: function()
	{
		var inputList = document.getElementsByTagName('input'); 
		for (var x=0;x<inputList.length;x++)
		{
			if((inputList[x].type.toLowerCase()=='checkbox')||(inputList[x].type.toLowerCase()=='radio'))
			{	
				if(inputList[x].checked)
				{
					var parent=inputList[x].parentNode;
					if(parent.className.indexOf(highlightClassName)==-1)
					{
						parent.className+=' '+highlightClassName;
					}
				}
				else
				{
					var parent=inputList[x].parentNode;
					parent.className = parent.className.replace(''+highlightClassName,'');
				}
				
				var input=inputList[x];
				if(!input.onclick)
				{
					input.onclick = function(){ myHighlight.highlight(this); return true; }
				}
			}
		}
	}, 
	
	highlight: function(checkElement)
	{
		var parent;
		
		if(checkElement.type.toLowerCase()=='radio')
		{
			var Elements=document.getElementsByName(checkElement.name);
			for(var i=0;i<Elements.length;i++)
			{
				parent=Elements[i].parentNode;
				parent.className=parent.className.replace(''+highlightClassName,'');
			}
		}
		
		parent=checkElement.parentNode;
		if(checkElement.checked)
		{
			if(parent.className.indexOf(highlightClassName)==-1)
			{
				parent.className+=' '+highlightClassName;
			}
		}
		else
		{
			parent.className=parent.className.replace(''+highlightClassName,'');
		}
	}
}

function initHighlight()
{
	myHighlight = new highlightInputs();
}
Event.observe(window,'load',initHighlight);

function balancePanels()
{
	var sidePanel = $('sidepanel');
	var updatePanel = $('updatepanel');
	var previewPanel = $('previewpanel');

	if ( (sidePanel != null) && (updatePanel != null) && (previewPanel != null) )
	{
		var sidePanelY = Element.getHeight(sidePanel);
		var updatePanelY = Element.getHeight(updatePanel);
		var previewPanelOldY = Element.getHeight(previewPanel);
		var previewPanelY = sidePanelY - updatePanelY;

		if (previewPanelY > previewPanelOldY)
		{
			previewPanel.style.height = previewPanelY+"px";
		}
	}
}
Event.observe(window,'load',balancePanels);

function showDialog(sdmID, sdmWidth, sdmHeight, sdmMeasureType, sdmButton, sdmImagePath, sdmAppearDuration, sdmBackGroundColor)
{
	var viewHeight, viewWidth;
	var xSize,ySize;
	var pagePositionX, pagePositionY;
	var showDialogBGDiv;
	var showDialogCloseDiv;
	var showDialogMessagePanelID;
	var lightBoxWidth, lightBoxHeight;
	var percentageMultiplierX, percentageMultiplierY;
	var includeCloseButton;
	var scrollBarSize;
	var imagePath;
	var closeTable = "";
	var verticalSpacing;
	var bgDivName;

	bgDivName = 'bg'+sdmID+'bg';

	scrollBarSize = 0;
	includeCloseButton = sdmButton;
    lightBoxWidth = sdmWidth;
	lightBoxHeight = sdmHeight;
	showDialogMessagePanelID = sdmID;
	if ( (sdmImagePath == null) || (sdmImagePath == "") )
	{
		imagePath = "https://iwcdn.s3.amazonaws.com/";
	}
	else
	{
		imagePath = sdmImagePath;
	}

	if (sdmAppearDuration == null)
	{
		sdmAppearDuration = .5;
	}

	if (sdmBackGroundColor == null)
	{
		sdmBackGroundColor = "#333333";
	}


//Create the translucent background div if it doesn't exist
	if (!$(bgDivName))
	{
		showDialogBGDiv = document.createElement('div');
		showDialogBGDiv.setAttribute('id', bgDivName);
		showDialogBGDiv.style.position = "absolute";
		showDialogBGDiv.style.left = "0";
		showDialogBGDiv.style.top = "0";
		showDialogBGDiv.style.display = "none";

		document.body.appendChild(showDialogBGDiv);
	}

	$(bgDivName).style.background = sdmBackGroundColor;
	 
	if (self.innerHeight) // all except Explorer
	{
		xSize = self.innerWidth;
		ySize = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		xSize = document.documentElement.clientWidth;
		ySize = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		xSize = document.body.clientWidth;
		ySize = document.body.clientHeight;
	}

	viewHeight = ySize;
	viewWidth  = xSize;

	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = (window.innerWidth + window.scrollMaxX);
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else 
	{ 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
		 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		
		scrollBarSize = 18;
		xScroll = (window.innerWidth + window.scrollMaxX); 
	}
	else
	{
		if (document.body.scrollWidth)
		{
			xScroll = document.body.scrollWidth;
		}
	}

	if (xSize < xScroll)
	{
		xSize = xScroll;
		ySize = ySize - scrollBarSize;
		viewHeight = viewHeight - scrollBarSize;
	}

	if (ySize < yScroll)
	{
		ySize = yScroll;
		xSize = xSize - scrollBarSize;
		viewWidth = viewWidth - scrollBarSize;
	}
	
	if(typeof(window.pageYOffset) == 'number') 
	{
		pagePositionX = window.pageXOffset;
		pagePositionY = window.pageYOffset;
	} 
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) 
	{
		pagePositionX = document.body.scrollLeft;
		pagePositionY = document.body.scrollTop;
	} 
	else if(document.documentElement) 
	{
		pagePositionX = document.documentElement.scrollLeft;
		pagePositionY = document.documentElement.scrollTop;
	}

// Set size of translucent div to fill screen
	$(bgDivName).style.height = (ySize) + "px";
	$(bgDivName).style.width = (xSize) + "px";

//Determine percentage for centering lightbox
	if (sdmMeasureType == "%")
	{
		percentageMultiplierX = "."+Math.round((100 - lightBoxWidth)/2);
		percentageMultiplierY = "."+Math.round((100 - lightBoxHeight)/2);
		lightBoxWidth = Math.round(viewWidth * (lightBoxWidth/100));
		lightBoxHeight = Math.round(viewHeight * (lightBoxHeight/100));
	}
	else  
	{
		if (lightBoxWidth > viewWidth)
		{
			percentageMultiplierX = 0;
			lightBoxWidth = viewWidth;
		}
		else
		{
			percentageMultiplierX = "."+Math.round((100 - ((lightBoxWidth / viewWidth)*100))/2);
		}

		if (lightBoxHeight > viewHeight)
		{
			percentageMultiplierY = 0;
			lightBoxHeight = viewHeight;
		}
		else
		{
			percentageMultiplierY = "."+Math.round((100 - ((lightBoxHeight / viewHeight)*100))/2);
		}
	}

// Create close button/close div
	if (includeCloseButton == 1)
	{
		verticalSpacing = 25;
		if (!$(showDialogMessagePanelID+'Close'))
		{
			showDialogCloseDiv = document.createElement('div');
			showDialogCloseDiv.setAttribute('id', showDialogMessagePanelID+'Close');
			showDialogCloseDiv.style.position = "absolute";
			showDialogCloseDiv.style.zIndex = "1";
			showDialogCloseDiv.style.background = "white";
			showDialogCloseDiv.style.borderWidth = "1px";
			showDialogCloseDiv.style.borderColor = "#111111";
			showDialogCloseDiv.style.borderStyle = "solid";
			showDialogCloseDiv.style.height = (lightBoxHeight + 80) + "px";
			showDialogCloseDiv.style.width = (lightBoxWidth + 80) + "px";
			showDialogCloseDiv.style.top = ((pagePositionY + (viewHeight*percentageMultiplierY) - 40) - verticalSpacing) + "px"
			showDialogCloseDiv.style.left = (pagePositionX + (viewWidth*percentageMultiplierX) - 40) + "px";

			closeTable  = "";
			closeTable += "<div style=\"padding:10px 10px 0px 10px; position:relative;\" align=\"right\">";
			closeTable += "		<img style=\"cursor:pointer; cursor:hand; float:none; margin:0px;\" onclick=\"javascript: hideDialog('"+sdmID+"');\" src=\""+imagePath+"appimages/icon-close2.gif\" width=\"16\" height=\"16\" title=\"Close\" />";
			closeTable += "</div>";

			showDialogCloseDiv.innerHTML = closeTable;			
			showDialogCloseDiv.style.display = "none";
			document.body.appendChild(showDialogCloseDiv);
		}
		else
		{
		// We want to resize the close div in case it's matching display div has changed sizes
			$(sdmID+'Close').style.height = (lightBoxHeight + 80) + "px";
			$(sdmID+'Close').style.width = (lightBoxWidth + 80) + "px";
			$(sdmID+'Close').style.top = ((pagePositionY + (viewHeight*percentageMultiplierY) - 40) - verticalSpacing) + "px"
			$(sdmID+'Close').style.left = (pagePositionX + (viewWidth*percentageMultiplierX) - 40) + "px";

			closeTable  = "";
			closeTable += "<div style=\"padding:10px 10px 0px 10px; position:relative;\" align=\"right\">";
			closeTable += "		<img style=\"cursor:pointer; cursor:hand; float:none; margin:0px;\" onclick=\"javascript: hideDialog('"+sdmID+"');\" src=\""+imagePath+"appimages/icon-close2.gif\" width=\"16\" height=\"16\" title=\"Close\" />";
			closeTable += "</div>";
			
			$(sdmID+'Close').innerHTML = closeTable;			
		}
		$(showDialogMessagePanelID).style.borderWidth = "0px";
	}
	else
	{
		$(showDialogMessagePanelID).style.borderWidth = "1px";
		$(showDialogMessagePanelID).style.borderColor = "#111111";
		$(showDialogMessagePanelID).style.borderStyle = "solid";
		verticalSpacing = 18;
	}

// Position and set size of lightbox
	$(showDialogMessagePanelID).style.position = "absolute";
	$(showDialogMessagePanelID).style.zIndex = "2";
	$(showDialogMessagePanelID).style.width = lightBoxWidth + "px";
	$(showDialogMessagePanelID).style.height = lightBoxHeight + "px";
	$(showDialogMessagePanelID).style.top = ((pagePositionY + (viewHeight*percentageMultiplierY)) - verticalSpacing) + "px";
	$(showDialogMessagePanelID).style.left = (pagePositionX + (viewWidth*percentageMultiplierX)) + "px";

//Hide elements for IE 6 otherwise they'll show through the translucent div

	if (sdmBackGroundColor != '')
	{
		toggleSelectVisibility("hidden");
	}

// Make lightbox visible
	$(bgDivName).appear({ duration: .1, from: 0.0, to: 0.9 });
//	$(showDialogMessagePanelID).appear({ duration: .5 });
	$(showDialogMessagePanelID).appear({ duration: sdmAppearDuration });
	if (includeCloseButton == 1)
	{
		$(showDialogMessagePanelID+'Close').appear({ duration: .5 });
	} 
}

function toggleSelectVisibility(visibility)
{
	var selects = document.getElementsByTagName('select');
	
	for(var i = 0; i < selects.length; i++) 
	{
		selects[i].style.visibility = visibility;
	}
	var iframes = document.getElementsByTagName('iframe');
	for (i = 0; i != iframes.length; i++) 
	{
			iframes[i].style.visibility = visibility;
	}	
}

function hideDialog(sdmID, sdmFadeDuration)
{
	var bgDivName;

	bgDivName = 'bg'+sdmID+'bg';

	if (sdmFadeDuration == null)
	{
		sdmFadeDuration = 0;
	}

	if (sdmFadeDuration > 0)
	{
		$(bgDivName).fade({ duration: sdmFadeDuration });
		$(sdmID).fade({ duration: sdmFadeDuration });
	}
	else
	{
		$(bgDivName).style.display = 'none';
		$(sdmID).style.display = 'none';
	}
	
	if ($(sdmID+'Close'))
	{
		$(sdmID+'Close').style.display = 'none';	
	}
	//Show elements hidden for IE 6
	toggleSelectVisibility("visible");
}

function changeResultsPerPage(dropDown)
{
	var selectedValue = dropDown.options[dropDown.selectedIndex].value;
	var scriptPath = window.location.pathname;
	var queryString = window.location.search;
	var queryDelimiter = "&";

	if (queryString == "")
	{
		queryDelimiter = "?";
	}

	if (queryString.indexOf("limit") == -1)
	{
		// limit is not in the querystring
		queryString += queryDelimiter+"limit="+selectedValue;
	}
	else
	{
		// limit is in the querystring
		var re = new RegExp("limit=([0-9]*)");
		var m = re.exec(queryString);

		if (m != null)
		{
			// if found match
			var limitString = m[0];
			if (limitString != "")
			{
				queryString = queryString.replace(limitString,"limit="+selectedValue);
			}
		}
	}

	location.href = scriptPath+queryString;
}

function setResultsPerPage(userLimit)
{
	var dropDown = $('resultsperpage');
	var scriptPath = window.location.pathname;
	var queryString = window.location.search;
	var selectedIndex = -1;
	var limitParam = "";

	if (queryString != "")
	{
		// limit is in the querystring
		var re = new RegExp("limit=([0-9]*)");
		var m = re.exec(queryString);

		if (m != null)
		{
			// if found match
			limitParam = m[1];
		}
	}
	else
	{
		if ( (userLimit == null) || (userLimit == "undefined") )
		{
			limitParam = "";
		}
		else
		{
			limitParam = userLimit;
		}
	}

	if (limitParam != "")
	{
		for (i=0; i<dropDown.options.length; i++)
		{
			if (dropDown.options[i].value == limitParam)
			{
				selectedIndex = i;
				break;
			}
		}

		if (selectedIndex != -1)
		{
			dropDown.selectedIndex = selectedIndex;
		}
	}
	else
	{
		// limit is not specified, use default
		dropDown.selectedIndex = 1;
	}
}

// clear the default text of a form field: text or text area
function clearDefaultFieldValue(id,defaultText)
{
	var thisID = $(id);
	
	if (thisID.tagName == "TEXTAREA")
	{
		if (thisID.innerHTML == defaultText)
		{
			thisID.innerHTML = "";
		}
	}
	else
	{
		if (thisID.value == defaultText)
		{
			thisID.value = "";
		}
	}
}

// setup prototips for FormDesign fields
document.observe('dom:loaded', function() {
  $$('div.fb_prototip').each(function(element) {
	var sRPTemp = element.id;
	sRPTemp = sRPTemp.replace("tip_","");
	if (Prototip != null)
	{
		new Tip(sRPTemp, element.innerHTML, { style: 'darkgrey', stem: 'topLeft', hook: { tip: 'topLeft', mouse: true }, offset: { x: 14, y: 14 } });	
	}
  });
});



document.observe("dom:loaded", function() 
{

	displayLightBoxPanels();

});


Event.observe(window, 'load', function() 
{
	
	Event.observe(document, 'mousemove', function(e)
	{	
		if (countMoves > 1)
		{	
			closeLightBoxPanel('updatedDiv');
		}
		else
		{
			countMoves++;
		}	
	});

	Event.observe(document, 'click', function(e)
	{
		closeLightBoxPanel('updatedDiv');
	});

	Event.observe(document, 'keypress', function(e)
	{ 
		closeLightBoxPanel('updatedDiv');
	});

	Event.observe(document, 'keydown', function(e)
	{ 
		closeLightBoxPanel('updatedDiv');
		trapKeyDown(e);
	});
});




function closeLightBoxPanel(clbpID)
{
	if (dialogShown)
	{
		setTimeout ( "hideDialog('"+clbpID+"',0)", 200); 
		dialogShown = false;
	}
}

function trapKeyDown(e)
{ 
	var code;

	if (!e)
	{
		e = window.event;
	}

	if (e.keyCode) 
	{
		code = e.keyCode;
	}
	else 
	{
		if (e.which)
		{
			code = e.which;
		}	
	}
 
	if(e.ctrlKey)
	{
		//ctrl+s
		if (code == '83')
		{	

			if ($('submitpostback'))
			{					
				e.cancelBubble = true;
				e.returnValue = false;

				if (e.stopPropagation) 
				{
					e.stopPropagation();
					e.preventDefault();
				}

				processCtrlS();
			}
		}
	}
}

function displayLightBoxPanels()
{
	if ($('sysmessage'))
	{
		loadUpdatePanel();
	}
}

function readQueryString(rqsName)
{
	var queryString = window.location.search;
	
	if (queryString != "")
	{
		// limit is in the querystring
		var re = new RegExp(""+rqsName+"=([^&]*)(&|$)","i");
		var m = re.exec(queryString);

		if (m != null)
		{
			return m[1];
		}
	}
	else
	{
		return '';
	}

}


function loadUpdatePanel()
{
	var scrollToY;
	scrollToY = readCookie('vpyPos');
	deleteCookie('vpyPos');

	if (scrollToY)
	{
		window.scrollTo(0,scrollToY);

		if (!$('updatedDiv'))
		{
			createHiddenElement('div', 'updatedDiv');

			if ($('updatedDiv'))
			{
				$('updatedDiv').addClassName('popup-message');
				$('updatedDiv').addClassName('success');
				$('updatedDiv').update('<div><h1><img src="https://iwcdn.s3.amazonaws.com/appimages/message-icon-success.gif" /> Saved!</h1></div>');	
			}
		}

		showDialog("updatedDiv",380,104,'px',0,'',0,'');
		dialogShown = true;
	}

}

	

function processCtrlS()
{

	if (!$('submitpostback').disabled)
	{
		if (!$('savingDiv'))
		{
			createHiddenElement('div', 'savingDiv','Saving');

			if ($('savingDiv'))
			{
				$('savingDiv').addClassName('popup-message');
				$('savingDiv').addClassName('in-progress');
				$('savingDiv').update('<div><h1><img src="https://iwcdn.s3.amazonaws.com/appimages/message-icon-in-progress.gif" /> Saving...</h1></div>');
			}
		}

		showDialog("savingDiv",380,104,'px',0,'',0,'');
		
		//get viewport position
		setViewPortCookie();

		if (Prototype.Browser.IE)
		{
			setTimeout ( "$('submitpostback').click()", 1000); 
		}
		else
		{
			setTimeout ( "$('submitpostback').click()", 100); 
		}


//		$('submitpostback').click();
	}
}


function createHiddenElement(cheType, cheID)
{
	var cheElement;

	if (!$(cheID))
	{
		cheElement = document.createElement(cheType);
		cheElement.setAttribute('id', cheID);
		cheElement.style.position = "absolute";
		cheElement.style.left = "0";
		cheElement.style.top = "0";
		cheElement.style.display = "none";

		document.body.appendChild(cheElement);
	
		return true;
	}
	else
	{
		return false;
	}
}

//Create ViewPort Cookie
function setViewPortCookie()
{
	var cookieValue;
	var arrViewPort = document.viewport.getScrollOffsets();

	cookieValue = arrViewPort[1];

	if (!cookieValue)
	{
		cookieValue = 0;
	}

	createCookie("vpyPos",cookieValue,0);

}

function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = ";expires="+date.toGMTString();
	}
	else
	{
		var expires = "";
	}

	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var arrCookie = document.cookie.split(';');

	for(var i=0;i < arrCookie.length;i++) 
	{
		var cookieValues = arrCookie[i];
		while (cookieValues.charAt(0)==' ')
		{
			cookieValues = cookieValues.substring(1,cookieValues.length);
		}

		if (cookieValues.indexOf(nameEQ) == 0) 
		{
			return cookieValues.substring(nameEQ.length,cookieValues.length);
		}
	}
	return null;
}

function deleteCookie(name) 
{
	createCookie(name,"",-1);
}
