function Page_Init()
{
    if (window.Cart !== undefined)
        Cart.updateMiniBasket();
	UI.FixPrimaryNavigation();
}; 
Event.observe( window, "load", Page_Init, false );

function rightTrim(sString)
{
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function leftTrim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function ImageViewer( path )
{
	OpenWindow( "/ImageViewer.aspx?Path=" + path, "ImageViewer", 500, 500, "" );
}

function askQuestion()
{
	alert( "coming soon" );
}

function viewBuyersGuide()
{
	alert( "coming soon" );
}
									
function UpdateDetailsValidateEmail( source, args )
{
	// Do not validate if email is not valid in the first place
	if( !validEmailAddress( args.Value ) ) return;
	
	var sendEmailRequest = new Ajax.Request(
		'/AjaxHandlers/VerifyEmail.ashx', {
			method: 'post',
			parameters: 'Email=' + args.Value,
			asynchronous: false
		});
		
	var responseText = Number( sendEmailRequest.transport.responseText );
	args.IsValid = responseText == 1;
}

function ValidateCustomerPasswordMatch( source, args )
{
	var sendEmailRequest = new Ajax.Request(
		'/AjaxHandlers/CheckPassword.ashx', {
			method: 'post',
			parameters: 'Password=' + args.Value,
			asynchronous: false
		});
		
	var responseText = Number( sendEmailRequest.transport.responseText );
	args.IsValid = responseText == 1;
}

var imageGallery = Class.create();
imageGallery.prototype = 
{
	initialize: function()
	{
		this._width = 755;
		this._height = 550;
		this._features = "scrollbars=0,resize=0,status=1";
		this._window = null;
		this._windowUri = "/Products/ImageGallery.aspx?$PDK=";
		this._windowTitle = "ImageGallery";
	},
	
	show: function( key )
	{
		if( null != this._window )
		{
			this._window.close();
		}
		
		this._window = OpenWindow( this._windowUri + key, this._windowTitle, this._width, this._height, this._features );
	},
	
	cleanUp: function()
	{
		this._window = null;
	}
};
var ImageGallery = new imageGallery();

var orderReceipt = Class.create();
orderReceipt.prototype = 
{
	initialize: function()
	{
		this._width = 500;
		this._height = 500;
		this._features = "scrollbars=1,resize=0,status=1";
		this._window = null;
		this._windowUri = "/MyAccount/OrderReciept.aspx?$OK=";
		this._windowTitle = "OrderReceipt";
	},
	
	show: function( key )
	{
		if( null != this._window )
		{
			this._window.close();
		}
		
		this._window = OpenWindow( this._windowUri + key, this._windowTitle, this._width, this._height, this._features );
	},
	
	cleanUp: function()
	{
		this._window = null;
	}
};
var OrderReceipt = new orderReceipt();

// Global Product Search Object
var ProductSearcher = Class.create();
ProductSearcher.prototype = 
{
	initialize: function()
	{
		this._containerDiv = $( "AdvancedSearch" );
	},
	
	show: function()
	{
		Element.show( this._containerDiv );
		new Rico.Effect.FadeTo( this._containerDiv, .95, 200, 7 );
	},
	
	hide: function()
	{
		new Rico.Effect.FadeTo( 
		this._containerDiv, .0, 200, 7, {
			complete: function(){
				Element.hide( "AdvancedSearch" );
			}
		});
	},
	
	invoke: function()
	{
		this._keyword = $F( this._keywordTextbox );
		this._manufacturerId = $F( this._manufacturerList );
		this._sectionId = $F( this._sectionList );
		this._order = $F( this._orderList );
		this._orderDirection = $F( this._orderDirectionList );
		
		window.location = "/Products/Search.aspx?$KW=" + leftTrim(rightTrim(escape(this._keyword)))
//			+ "&$MK=" + this._manufacturerId 
//			+ "&$SK=" + this._sectionId
			+ "&$OR=" + this._order
			+ "&$ORD=" + this._orderDirection
            ;
	},
	
	checkKeyPress: function( e )
	{
		var code = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which;
		if( code == Event.KEY_RETURN ) this.invoke();
	}
};

// Email A Friend
var EmailFriend = Class.create();
EmailFriend.prototype = 
{
	initialize: function()
	{
		this._containerDiv = $( "EmailFriend" );
		this._form = $( "EmailFriendForm" );
		this._complete = $( "EmailFriendComplete" );
	},
	
	show: function()
	{
		Element.show( this._containerDiv );
		new Rico.Effect.FadeTo( this._containerDiv, .95, 200, 7 );
		
		Element.hide( this._complete );
		Element.show( this._form );
		
		resetFormFields(this._form);
	},
	
	hide: function()
	{
		new Rico.Effect.FadeTo( 
		this._containerDiv, .0, 200, 7, {
			complete: function(){
				Element.hide( "EmailFriend" );
			}
		});
	},
	
	invoke: function()
	{
		var form = $( "EmailFriendForm" );
		var complete = $( "EmailFriendComplete" );
		
		var fromName = $F( "FromName" );
		var fromEmail = $F( "FromEmail" );
		var friendsName = $F( "FriendsName" );
		var friendsEmail = $F( "FriendsEmail" );
		var message = $F( "Message" );
		
		var errors = new Array();
		var errorMessage = "Please ammend the errors found below:\n\n";
		
		if( fromName.length == 0 ) errors.push( "Please enter your name" );
		if( !validEmailAddress( fromEmail ) ) errors.push( "Your email address appears to be invalid" );
		if( friendsName.length == 0 ) errors.push( "Please enter your friends name" );
		if( !validEmailAddress( friendsEmail ) ) errors.push( "Your friends email address appears to be invalid" );
		
		if( errors.length > 0 )
		{
			for( var i = 0; i < errors.length; i++ )
				errorMessage += "~ " + errors[i] + ".\n";
			
			alert( errorMessage );
			return false;
		}
		else
		{
			var params = "FromName=" + fromName 
				+ "&FromEmail=" + fromEmail 
				+ "&FriendsName=" + friendsName 
				+ "&FriendsEmail=" + friendsEmail 
				+ "&Message=" + message;
				
			var sendEmailRequest = new Ajax.Request(
				"/AjaxHandlers/EmailFriend.ashx", {
					method: "post",
					parameters: params,
					onComplete: this._requestComplete
				});
			
			// Retain box dimensions
			complete.style.height = ( form.offsetHeight + "px" );
			
			return true;
		}
	},
	
	_requestComplete: function( response )
	{
		var completeContainer = $( "EmailFriendComplete" );
		var completeMessage = completeContainer.getElementsByTagName( "p" )[0];
		
		completeMessage.innerHTML = response.responseText;
		
		// Hide form and show complete
		Element.hide( "EmailFriendForm" );
		Element.show( "EmailFriendComplete" );
	}
};

var UI = {
	FixPrimaryNavigation: function(){
		if( $( 'PrimaryNavigation' ) ){
			var parentElements = new Array();
			
			var elements = document.getElementById( "PrimaryNavigation" ).getElementsByTagName( "LI" );
			for( var i = 0; i < elements.length; i++ ){	
				if( elements[i].getAttribute( "rel" ) == "0" ){
					parentElements.push( elements[i] );
				}
			}
            
            if (parentElements.length > 0)
                parentElements[parentElements.length-1].style.borderRight = "none";
		}
	}
}
