
window.addEvent('domready', function() { 
		
		
	/*****
	**	Slider for Login Page 
	*****/	
	var ShowCV2 = $('ShowCV2');	
	if( ShowCV2 ) 
	{
		var mySlide = new Fx.Slide('WhatisOrderRef').hide();
		
		ShowCV2.addEvent('click', function(th)
		{		
			th = new Event(th);
			mySlide.toggle();
			th.stop();
		});
	}
	init_AddMyReview();
	checkAllowContinueBtn();
});


function checkAllowContinueBtn()
{
	var completed = $$('h3.completedItemReview');
	var proceed = $('reviewProceed');
	if( completed.length >= 1 )
	{
		if( proceed )
			proceed.setStyle('display','block');
	}  
	
	
	var reviewCompany = $('completedShopReview');
	if( reviewCompany )
	{
		if( proceed )
			proceed.setStyle('display','block');
	}
	
	/*
	var reviewCompany = $('completedShopReview');
	if( reviewCompany )
	{
		var completed = $$('h3.completedItemReview');
		if( completed.length >= 1 )
		{
			var proceed = $('reviewProceed');
			if( proceed )
				proceed.setStyle('display','block');
		}
	}
	*/
}

/*****
**	Add My Review
*****/
function init_AddMyReview()
{
	$$('.rate').each(function(element,i){
			
		element.addEvent('click', function(e){
			new Event(e).stop();
			var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar'];
			myStyles.each(function(myStyle){
				if(element.getParent().hasClass(myStyle)){
					element.getParent().removeClass(myStyle)
				}
			});		
			
			myStyles.each(function(myStyle, index){
				id = element.getProperty('id').split("_");
				if(index == id[2]){
					element.getParent().toggleClass(myStyle);
					$(id[0]+'_'+id[1]).setProperty('value', id[2]);
				}
			});		
			
			
		});
		element.addEvent('mouseover', function(){
				
			var myStyles = ['nostar_hover', 'onestar_hover', 'twostar_hover', 'threestar_hover', 'fourstar_hover', 'fivestar_hover'];
			myStyles.each(function(myStyle){
				if(element.getParent().hasClass(myStyle)){
					element.getParent().removeClass(myStyle)
				}
			});		
			myStyles.each(function(myStyle, index){
				id = element.getProperty('id').split("_");
				if(index == id[2]){
					element.getParent().toggleClass(myStyle);
				}
			});		
		});
		element.addEvent('mouseout', function(){
			var myStyles = ['nostar_hover', 'onestar_hover', 'twostar_hover', 'threestar_hover', 'fourstar_hover', 'fivestar_hover'];
			myStyles.each(function(myStyle){
				if(element.getParent().hasClass(myStyle)){
					element.getParent().removeClass(myStyle)
				}
			});			
		});
	});			

	
	/******
	**	Disable Item Submit Actions
	******/		
	var itemReviews = $$('.reviewItem');  
	itemReviews.each(function(ir) {
		if(ir)
		{			 
			ir.addEvent('submit', function(e) {
					
				new Event(e).stop();
	 
				/**
				 * This empties the log and shows the spinning indicator
				 */
				var tmp = ir.getProperty('id').split("_");
				var itemId = tmp[1];
				var log = $('Log_'+tmp[1]);
			 
				/**
				 * send takes care of encoding and returns the Ajax instance.
				 * onComplete removes the spinner from the log.
				 */		
				this.set('send',{
					evalScripts: true,
					onSuccess: function (output ) {
						log.innerHTML=output; 
						init_AddMyReview(); 
						
						var rating = $('ItemRating_'+itemId); 
						if( rating ) 
						{
							// Rating is present, review is uncomplete
							RatingValue = rating.getProperty('value'); 
						
							$$('#ReviewItem_'+itemId+' li.rate' ).each( function( element, i ) {
									
								tmpId = element.getProperty('id').split("_");
								
								if( (tmpId[1] == itemId) && (RatingValue == tmpId[2]) )
								{
									
									var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar'];
									myStyles.each(function(myStyle){
										if(element.getParent().hasClass(myStyle)){
											element.getParent().removeClass(myStyle)
										}
									});		
									myStyles.each(function(myStyle, index){
										id = element.getProperty('id').split("_");
										if(index == tmpId[2]){
											element.getParent().toggleClass(myStyle);
											$(tmpId[0]+'_'+tmpId[1]).setProperty('value', tmpId[2]);
										}
									});
									
								}
								
							});
						}
						checkAllowContinueBtn();
					}		
				});
				this.send();
			});
		}
	});
	
	
	/******
	**	Disable Babythings4u Submit Action
	******/	
	var ir = $('ReviewBabythings4u');
	if(ir)
	{
		ir.addEvent('submit', function(e) {
		
			new Event(e).stop();
			
			var tmp = ir.getProperty('id').split("_");
			var log = $('Log_Babythings4u');
					
			this.set('send', {
				evalScripts: true,
				onSuccess: function (output ) { 
					log.innerHTML=output; 
					init_AddMyReview(); 
					
					var rating = $('Babythings4uRating_1');
					if( rating )
					{
						// Rating is present, review uncompleted
						
						RatingValue = $('Babythings4uRating_1').getProperty('value'); 
														
						$$('#ReviewBabythings4u li.rate' ).each( function( element, i ) {
														
							tmpId = element.getProperty('id').split("_");
							
							if(RatingValue == tmpId[2] )
							{									
								var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar'];
								myStyles.each(function(myStyle){
									if(element.getParent().hasClass(myStyle)){
										element.getParent().removeClass(myStyle)
									}
								});		
								myStyles.each(function(myStyle, index){
									id = element.getProperty('id').split("_");
									if(index == tmpId[2]){
										element.getParent().toggleClass(myStyle);
										$(tmpId[0]+'_'+tmpId[1]).setProperty('value', tmpId[2]);
									}
								});
								
							}
							
						});
					}
					checkAllowContinueBtn();
				}		
			});
			this.send();
		});
	}	
}
