//	function LightEm()
//	{
//		setTimeout(LightUpCategoryImages, 700);
//	}
//	function LightUpCategoryImages()
//	{
//		<%
//		i = 0.0
//		if IsDefined("ProductCategories"):
//			for pc in ProductCategories:
//			%>
//				new Effect.Appear('ProductCategoryImageContainer_${pc.ProductCategoryId}',{delay:${i},from:0.0,to:1.0} );
//			<%
//			i = i + .10
//			end
//		end%>
//	}

	var theWindow = null;
	var theWindowOpen = false;
	var curPageHeight = 0;
	var curPageWidth = 0;
	var curPageX = 0;
	var curPageY = 0;
	
	//handle all scroll events
	if ("onmousewheel" in document)
	{
		document.onmousewheel = ScrollDetected;
	}
	if (window.addEventListener)
	{
		window.addEventListener("DOMMouseScroll", ScrollDetected, false);
		window.addEventListener("scroll", ScrollDetected, false);
		window.addEventListener("mousemove", ScrollDetected, true);
	}
	else if (document.addEventListener) // Opera 7+
	{
		document.addEventListener("scroll", ScrollDetected, false);
	}
	else if (document.all && document.compatMode && document.compatMode == "CSS1Compat")
	{
		if ("onscroll" in self)
		{	
			window.onscroll = ScrollDetected;
		}
	}
	
	function ScrollDetected()
	{
		if (theWindowOpen)
		{
			getPageDimensions();
			getScroll();
			
			var nTop = curPageY - curPageHeight;
			if(nTop < 0)
				nTop = 0;
				
			var nHeight = curPageHeight * 2;
			$('lightwindow_overlay').setStyle(
				{
					left: 0,
					top: nTop + 'px',
					width: curPageWidth + 'px',
					height: nHeight + 'px'
				});
			
		}
	}
	
	function getPageDimensions()
	{
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) 
		{
			xScroll = document.body.scrollWidth;
			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;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) 
		{
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} 
		else if (document.documentElement && document.documentElement.clientHeight) 
		{
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} 
		else if (document.body) 
		{
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	

		//if (yScroll < windowHeight)
		//{
			curPageHeight = windowHeight;
		//} 
		//else 
		//{
		//	curPageHeight = yScroll;
		//}

		//if (xScroll < windowWidth)
		//{
			curPageWidth = windowWidth;
		//} 
		//else 
		//{
		//	curPageWidth = xScroll;
		//}
	}
	
	function getScroll()
	{
		if ( typeof(window.pageYOffset) == 'number') 
		{
        	curPageX = window.pageXOffset;
        	curPageY = window.pageYOffset;
      	} 
      	else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) 
      	{
	       	curPageX = document.body.scrollLeft;
        	curPageY = document.body.scrollTop;
		} 
		else if (document.documentElement) 
		{
        	curPageX = document.documentElement.scrollLeft;
        	curPageY = document.documentElement.scrollTop;
      	}
	}

	function ShowLightbox(link)
	{	
		if(!theWindowOpen)
		{
			theWindowOpen = true;
			
			if(!theWindow)
				theWindow = new lightwindow();
				
			theWindow.activateWindow(
			{
				href: link,
				title: '',
				type: 'page'
			});
			
			ScrollDetected();
		}
	}

	function ShowLightboxIframe(link)
	{
		if(!theWindowOpen)
		{
			theWindowOpen = true;	
		
			if(!theWindow)
				theWindow = new lightwindow();
				
			theWindow.activateWindow(
			{
				href: link,
				title: '',
				type: 'external'
			});	
			
			ScrollDetected();
		}
	}
	
	function ValidateAndSubmit(area)
	{
		//alert("HI");
		var valid = new Validation('form1', {onSubmit:false});
		var result = valid.validate();
		
		if(result == true)
		{
			//alert("good");
			
			//new Ajax.Updater('AppraisalValue', '/' + area + '/Cart/BeginAppraisal-UpdatePrice.rails', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)});
			
			//valid.reset();
			//$('AcceptButtonImage').show();
			
			new Ajax.Updater('AppraisalValue', '/' + area + '/Cart/BeginAppraisal-UpdatePrice.rails', {parameters:$('form1').serialize(),onComplete:function(){ new Effect.Appear('submitContainer');new Effect.Highlight('AppraisalValue'); setTimeout("$('AcceptButtonImage').focus()",1000)  } });

			
		}
		else
		{
			//alert("bad");
			//$('acceptButtonImage').hide();
			ResetTheElements();
		}
		
		return false;
	}
	
	function ValidateShippingAndSubmit()
	{
		var valid = new Validation('form1', {useTitles : true});

	    
		var result = valid.validate();
		
		if(result == true)
		{
			$('form1').submit();
		}
		else
		{
			//$('AgreeToTermsText').className = 'validation-failed';
			
			 new Effect.Highlight('AgreeToTermsText', {duration:2.0});
			 new Effect.Highlight('AgreeToTermsText2', {duration:2.0});
			
			$('errors').update('Please Complete All Fields.');
			$('errors').className = 'visible';
			$('errors').addClassName('validation-failed');
		}
		
		return false;
	}	
		
	function ResetTheElements()
	{
		new Effect.Fade('submitContainer',{duration:0.4});
		$('AppraisalValue').update('--');
		
		return false;
	}
	
	function ValidateComponentSets(el)
	{
		// Find and set the parents.
		var idOfSelectedItem = $F(el);
		var objectSelectedItem = $(idOfSelectedItem);
		var objectSelectedItemClassSplitArray = $A(objectSelectedItem.classNames())[0];
		var parentIdOfSelectedItem = objectSelectedItemClassSplitArray.split('-')[1];

		// Find all SELECT OPTION items with a VALUE of idOfSelectedItem and select it.
		//console.log($(parentIdOfSelectedItem));
		if($(parentIdOfSelectedItem) != null) {
			$(parentIdOfSelectedItem).selected = true;	
		}
		
		// Find and switch the set for children of set parent.  In the loop of each
		// SELECT element that has the parentSet-[id], find the other SELECT elements
		// with the same modelComponentTypeId-[componentTypeId] as the current.  Hide it,
		// and show the current.

		var childSets = $$('.parentSet-' + parentIdOfSelectedItem + '');
		//console.log(childSets);

		// Hide all of the SELECT elements with the same modelComponentTypeId-[]
		childSets.each(function(s) {
				// Get this SELECT elements current modelComponentTypeId
				var parent_classNamesArray = $A(s.classNames());
				var sibling_componentTypeName = parent_classNamesArray[0];
				//console.log(sibling_componentTypeName);

				// Find the modelComponentTypeId-[] siblings.
				var siblings = $$('.' + sibling_componentTypeName + '');
				//console.log(siblings);
				
				// Hide the siblings.
				siblings.each(function(c) {
						//console.log(c);
						c.hide();
					}
				);
				
				// Show the correct one, with .parentSet-[parentIdOfSelectedItem].
				var correctSet = $$('.parentSet-' + parentIdOfSelectedItem + '').each(function(i) {
						i.show();
					}
				);
				
				// Reset the selectedIndex on all HIDDEN siblings.
				siblings.each(function(c) {
						if(!$(c).visible())
						{
							$(c).selectedIndex = 0;
						}
					}
				);
			}
		);
		
		// Find and switch the set for children of selected item.  In the loop of each
		// SELECT element that has the parentSet-[id], find the other SELECT elements
		// with the same modelComponentTypeId-[componentTypeId] as the current.  Hide it,
		// and show the current.
		var childSets = $$('.parentSet-' + idOfSelectedItem + '');
		//console.log(childSets);

		// Hide all of the SELECT elements with the same modelComponentTypeId-[]
		childSets.each(function(s) {
				// Get this SELECT elements current modelComponentTypeId
				var parent_classNamesArray = $A(s.classNames());
				var sibling_componentTypeName = parent_classNamesArray[0];
				//console.log(sibling_componentTypeName);

				// Find the modelComponentTypeId-[] siblings.
				var siblings = $$('.' + sibling_componentTypeName + '');
				//console.log(siblings);
				
				// Hide the siblings.
				siblings.each(function(c) {
						//console.log(c);
						c.hide();
					}
				);
				
				// Show the correct one, with .parentSet-[parentIdOfSelectedItem].
				var correctSet = $$('.parentSet-' + idOfSelectedItem + '').each(function(i) {
						i.show();
					}
				);
				
				// Reset the selectedIndex on all HIDDEN siblings.
				siblings.each(function(c) {
						if(!$(c).visible())
						{
							$(c).selectedIndex = 0;
						}
					}
				);
			}
		);
	}
	
	function UpdateCondition(conditionId, area, idToUpdate, updateMessage)
	{
		$('ConditionStars').className = 'rating star_' + conditionId;
		$('Condition').value = conditionId;
		$('Condition_DisplayName').value = updateMessage;
		UpdateConditionDescription(idToUpdate, updateMessage);

		//new Effect.Fade('SubmitContainer',{duration:0.1});
		
		ValidateAndSubmit(area);
		
		//var t=setTimeout("$('AcceptButtonImage').focus()",1000);
		//$('AcceptButtonImage').AddClassName('visible');
	}
	
	function UpdateConditionDescription(theElementId, message)
	{
		//new Effect.Fade(theElementId,{duration:0.3});
		$(theElementId).update(message);
		//new Effect.Appear(theElementId,{duration:0.3});
	}
	
	function ResetSelectedDescription(theElementId)
	{
		UpdateConditionDescription(theElementId, $('Condition_DisplayName').value);
	}
	
		
	function ShowModelPopup(modelId, modelName, width, height)
	{
		var day = new Date();
		var id = day.getTime();
		var ww = width;
		var wh = height + 125;
		var params = 'width=' + ww + ',height=' + wh + ',menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no';
		var imgSrc = '/showModelThumb/Show.rails?modelId=' + modelId + '&h=' + height + '&w=' + width;
		var msg = '<html><head><title>' + modelName + '</title></head><body style="margin: 0; padding: 0;"><div style="margin: 0; padding: 0;" align="center"><img style="margin: 0; padding: 0;" src="' + imgSrc + '" alt="' + modelName + '" />\n' +
				  '<hr style="margin: 0; padding: 0;" width="100%" /><form><input style="margin: 0; padding: 0;" type="button" onClick="javascript:window.close()" value="Close Window"></form></div></body></html>\n';
		
		var win = window.open('', id, params);
		win.document.write(msg);
		win.document.close();
	}
	
	function ShowTutorialPopup(url)
	{
		var day = new Date();
		var id = day.getTime();
		var params = 'width=775,height=715,menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no';
		window.open(url, id, params);
	}
	
	function renewSession()
	{
		$("renewSession").src = "/renewSession.aspx?par=" + Math.random();
		//alert("Session Is New Again!");
	}	

	function page_loaded(evt)
	{
	  //Event.observe('my_menu', 'mouseover', menus, false)
	  window.setInterval("renewSession()", 240000);
	}
	
	Event.observe(window, 'load', page_loaded, false);
	
	newimage0 = new Image();
	newimage0.src = "/Content/images/lightwindow/black-80.png";
	newimage1 = new Image();
	newimage1.src = "/Content/images/lightwindow/black.png";
	newimage2 = new Image();
	newimage2.src = "/Content/images/lightwindow/ajax-loading.gif";
	newimage3 = new Image();
	newimage3.src = "/Content/images/cexchange/star_rating/star-matrix2.jpg";
	newimage4 = new Image();
	newimage4.src = "/Content/images/cexchange/body/grdButtonAccept.gif";
	newimage5 = new Image();
	newimage5.src = "/Content/images/cexchange/body/grdButtonAccept_1.gif";
	newimage6 = new Image();
	newimage6.src = "/Content/images/cexchange/body/grdButtonCalc.gif";
	newimage7 = new Image();
	newimage7.src = "/Content/images/cexchange/body/grdButtonCalc_1.gif";
