/*
	FILE: mp3songmill.js
	PURPOSE: to handle all mp3songmill.com specific js methods
*/

function TogglePopularSong(divPopularSongId)
{
	//
	//	Name: TogglePopularSong
	//	Purpose: to toggle the popular song div from seleted to not-selected
	//	Parameters:
	//		divPopularSongId (string): the id of the popular song div
	//	Details: none
	//

	var divPopularSong = getEl(divPopularSongId);

	if (divPopularSong.style.color == '')
	{
		divPopularSong.style.backgroundColor = '#444444';
		divPopularSong.style.backgroundImage = 'url()';
		divPopularSong.style.color = 'white';
	}
	else
	{
		divPopularSong.style.backgroundColor = '#C0C0C0';
		divPopularSong.style.backgroundImage = 'url(images_new/imgGrayBackground.jpg)';
		divPopularSong.style.color = '';
	}
}

function DisplayAlbumsByGenre_Release(genre_id)
{
	getEl('pnlCenter').src = root + 'panels/pnlAlbums.php?op=release&genre_id='+ genre_id;
}

function Login_callback(result)
{

	if (result == 1 || result == 2)
	{
		//
		//	change login area to logout information
		//
		
		if (result == 2)
			ShowAdmin();

		LoggedIn();

		alert('You have logged in.');

		window.location = 'pnlFeaturedAlbums.php';
	}
	else
	{

		//
		//	Show message about incorrect login
		//

		alert("Incorrect login or password");

	}
}

function Login()
{

	//
	//	Function: Login
	//	Purpose: to pass prep an ajax login
	//	Parameters:
	//		username (string): retrieved from textbox
	//		password (string): retrieved from textbox
	//

	//
	//	Variables
	//

	var username = getEl('txtUsername').value;
	var password = getEl('txtPassword').value;

	if (username.length > 0 && password.length > 0)
	{
		agent.call(root + 'include/services.php','Login', 'Login_callback', username, password);
	}
	else
	{
		//
		//	Show error message
		//

		alert("You must enter a username and password");
	}
}

function Logout_callback(result)
{
	HideAdmin();
	LoggedOut();
	
	alert('You have logged out.');
}

function Logout()
{
	//
	//	Function: Logout
	//	Purpose: log the user out of the system
	//	Parameters: none
	//

	agent.call(root + 'include/services.php', 'Logout', 'Logout_callback');
}


function LoggedIn()
{
	getEl('btnSmallShowLogin', window.top).style.display = 'none';
	getEl('btnSmallLogout', window.top).style.display = '';
}

function LoggedOut()
{
	getEl('btnSmallLogout', window.top).style.display = 'none';
	getEl('btnSmallShowLogin', window.top).style.display = '';
}

function Register_callback(result)
{
	if (result == "OK")
	{
		//
		//	redirect to registration confirmation page
		//

		Message("Account Pending", "You should receive an email shortly with details on how to activate your account.");
	}
	else
	{

		//
		//	Show error message
		//

		alert(result);

	}
}

function Register()
{
	//
	//	Function: Register
	//	Purpose: to pass prep an ajax login
	//	Parameters:
	//		fullname (string): retrieved from textbox
	//		username (string): retrieved from textbox
	//		password1 (string): retrieved from textbox
	//		password2 (string): retrieved from textbox
	//		email (string): retrieved from textbox
	//

	//
	//	Variables
	//

	var fullname = getEl('txtFullname').value;
	var username = getEl('txtUsername').value;
	var password1 = getEl('txtPassword1').value;
	var password2 = getEl('txtPassword2').value;
	var email = getEl('txtEmail').value;
	var artist = getEl('chkArtist').checked;
	var artist_value = "No";

	if (artist) artist_value = "Yes";

	if (password1 == password2)
		agent.call(root + 'include/services.php','Register', 'Register_callback', fullname, username, password1, email, artist_value);
	else
		alert("The passwords did not match.");

}

function AddCouponToCart_callback(result)
{
	printfire("COUPON " + result);

	if (result.substring(0, 2) == "**")
	{
		getEl('divShoppingCartMessage').innerHTML = result.substring(2);
	}
	else
	{
		UpdateBasket(result);
	}
}

function AddCoupon()
{
	var coupon = getEl('txtCoupon').value;
	agent.call(root + 'include/services.php','AddCouponToCart', 'AddCouponToCart_callback', coupon);
}

function UpdateCart_callback(result)
{

	if (result.substring(0, 2) == "**")
	{
		getEl('divShoppingCartMessage').innerHTML = result.substring(2);
	}
	else
	{
		UpdateBasket(result);
	}
}

function UpdateCart()
{
	var coupon = getEl('txtCoupon').value;
	agent.call(root + 'include/services.php','UpdateCart', 'UpdateCart_callback', coupon);
}


function AddToCart_callback(result)
{
	printfire(result);
	UpdateBasket(result);
}

function AddToCart(item_id, type_id, price, desc, physical)
{
	//
	//	Function: AddToCart
	//	Purpose: To add an item to the cart
	//	Parameters:
	//		item_id (integer): id of item to add to cart
	//		type_id (integer): id of item type 1=song, 2=album, 3=coupon
	//		desc (string): description of item being added to cart
	//		physical (booelan): whether this product is a cd
	//

	//
	//	Variables
	//

	//
	//	Make sure we redirect this call to the parent window
	//

	if (window != window.top)
		window.top.AddToCart(item_id, type_id, price, desc, physical);
	else
	{
		agent.call(root + 'include/services.php','AddToCart', 'AddToCart_callback', item_id, type_id, price, desc, physical, 1);
	}
}


function RemoveFromCart_callback(result)
{
	UpdateBasket(result);
}

function RemoveFromCart(key)
{

	//
	//	Function: RemoveFromCart
	//	Purpose: To remove an item to the cart
	//	Parameters:
	//		item_id (integer): id of item to remove from the cart
	//

	//
	//	Variables
	//

	if (window != window.top)
		window.top.RemoveFromCart(key);
	else
		agent.call(root + 'include/services.php','RemoveFromCart', 'RemoveFromCart_callback', key);

}

function UpdateBasket(contents)
{
	var ultimate_parent = window.top;
	var divmysongs = null;

	//
	//	update the cart window if it is open
	//

	if (window.frames['pnlCenter'].document.location.href.indexOf('pnlShoppingCart.php') > 0)
	{
		//
		//	the cart page is displaying, refresh it
		//
		//alert('update');
		
		window.frames['pnlCenter'].document.location.href = '';
		window.frames['pnlCenter'].document.location.href = root + 'panels/pnlShoppingCart.php';
	}

	//
	//	update the cart panel
	//

	divmysongs = getEl('divMySongs', ultimate_parent);

	divmysongs.innerHTML = contents;

}

function EraseCart()
{
	//
	//	Function: EraseCart
	//	Purpose: erases the contents of the lower left shopping cart, cart in session is not affected
	//	Parameters: none
	//

	var ultimate_parent = window.top;
	var divmysongs = getEl('divMySongs', ultimate_parent);
	divmysongs.innerHTML = "";
}

function PlaySong(song_location, albumcover_location, artist_name, song_name)
{
	//
	//	Function: PlaySong
	//	Purpose: to play a song from any frame
	//	Parameters:
	//		song_location (string): location of song file
	//		albumcover_location (string): location of image for albumcover
	//		artist_name (string): name of artist
	//		song_name (string): name of song

	window.top.flash.PlaySong(song_location, albumcover_location, artist_name, song_name);
}


function ShowBuyDownload(btnID)
{

	//
	//	Function: ShowBuyDownload
	//	Purpose: display the buy download image
	//	parameters:
	//		btnID {string}: id of button to change
	//

	getEl('imgBuyCD' + btnID).style.display = 'none';
	getEl('imgBuyDownload' + btnID).style.display = '';

}

function ShowBuyCD(btnID)
{

	//
	//	Function: ShowBuyCD
	//	Purpose: display the buy CD image
	//	parameters:
	//		btnID (string): id of button to change
	//

	getEl('imgBuyDownload' + btnID).style.display = 'none';
	getEl('imgBuyCD' + btnID).style.display = '';

}

function ShowAccount()
{
	//
	//	Function: ShowAccount
	//	Purpose: display account page
	//	parameters: none
	//

	//PrimeWholeCenter();
	
	getEl('pnlCenter', window.top).src = root + 'panels/pnlAccount.php';

}

function SubmitLogin(e)
{

	//
	//	Function: SubmitLogin
	//	Purpose: detect enter key and submit login
	//	Parameters: event
	//

	var key = window.event ? e.keyCode : e.which;
	if (key == 13)
	{
		Login();
	}

}

function SubmitPasswordChange(e)
{

	//
	//	Function: SubmitPasswordChange
	//	Purpose: detect enter key and submit PasswordChange
	//	Parameters: event
	//

	var key = window.event ? e.keyCode : e.which;
	if (key == 13)
	{
		PasswordChange();
	}

}

function PasswordChange_callback(result)
{
	if (result == "OK")
	{
		Message("Password Changed", "Your password has been changed.");
	}
	else
	{
		alert(result);
	}
}


function PasswordChange()
{
	//
	//	Function: PasswordChange
	//	Purpose: to pass prep an ajax password change
	//	Parameters:
	//		oldpassword (string): retrieved from textbox
	//		newpassword1 (string): retrieved from textbox
	//		newpassword2 (string): retrieved from textbox
	//

	//
	//	Variables
	//

	var oldPassword = getEl('txtOldPassword').value;
	var newPassword1 = getEl('txtNewPassword1').value;
	var newPassword2 = getEl('txtNewPassword2').value;

	if (newPassword1 == newPassword2)
	{
		agent.call(root + 'include/services.php','PasswordChange', 'PasswordChange_callback', oldPassword, newPassword1);
	}
	else
	{
		//
		//	Show error message
		//

		alert("The password you typed did not match the verification password.");
	}
}

function ChangeArtistStatus_callback(result)
{
	ShowAccount();
}

function ChangeArtistStatus()
{
	//
	//	Function: Logout
	//	Purpose: log the user out of the system
	//	Parameters: none
	//

	agent.call(root + 'include/services.php', 'ChangeArtistStatus', 'ChangeArtistStatus_callback');
}

function ShowPackage(pkgnum)
{
	getEl("Package1").style.display = 'none';
	getEl("Package2").style.display = 'none';
	getEl("Breakdown").style.display = 'none';

	switch(pkgnum){
		case 1:
			getEl("Package1").style.display = 'block';
			break;
		case 2:
			getEl("Package2").style.display = 'block';
			break;
		case 3:
			getEl("Breakdown").style.display = 'block';
	} // switch
}

function ArtistUpdate_callback(result)
{
	if (result == "OK")
	{
		Message("Updated", "Your information has been updated.");
	}
	else
	{
		alert(result);
	}
}


function ArtistUpdate()
{
	//
	//	Function: ArtistUpdate
	//	Purpose: to pass prep an ajax password change
	//	Parameters:
	//		artist_id (integer): from local var
	//		address (string): retrieved from textbox
	//		payment_notes (string): retrieved from textbox
	//		comments (string): retrieved from textbox
	//

	//
	//	Variables
	//

	var artist_id = getEl('txtArtistId').value;
	var address = getEl('txtAddress').value;
	var payment_notes = getEl('txtPaymentNotes').value;
	var comments = getEl('txtComments').value;

	agent.call(root + 'include/services.php','ArtistUpdate', 'ArtistUpdate_callback', artist_id, address, payment_notes, comments);
}


function Activate_callback(result)
{
	if (result == "OK")
	{
		//
		//	redirect to registration confirmation page
		//

		Message("Account Activated", "Your account has been activated.  You should now be able to login.");
	}
	else
	{

		//
		//	Show error message
		//

		alert(result);

	}
}

function Activate()
{
	//
	//	Function: Activate
	//	Purpose: to pass prep an ajax activation
	//	Parameters:
	//		activationcode (string): retrieved from textbox
	//

	//
	//	Variables
	//

	var activationcode = getEl('txtActivationCode').value;

	if (activationcode != "")
		agent.call(root + 'include/services.php','Activate', 'Activate_callback', activationcode);
	else
		alert("You must enter an activation code.");

}

function ListDownloads_callback(result)
{

    getEl('divDownloads').innerHTML = result;
}

function ListDownloads()
{
    //
    //  Function: ListDownloads
    //  Purpose: to list the downloads available from a transaction id
    //  Prameters:
    //      TransactionId (string): retrieved from text box
    //
    
    var purchase_code = getEl('txtPurchaseCode').value;
    
    agent.call(root + 'include/services.php', 'ListDownloads', 'ListDownloads_callback', purchase_code);
    
}

function Search()
{
    //
    //  Function: Search
    //  Purpose: to open up search page with search results
    //  Parameters:
    //      search (string): retrived from txtSearch text box
    //
    
 	getEl('pnlCenter').src = root + 'panels/pnlSearch.php?search=' + URLencode(getEl('txtSearch').value);
 	
 	//
 	//  Clear search text
 	//
 	
 	getEl('txtSearch').value = "";
   
}

function SubmitSearch(e)
{

	//
	//	Function: SubmitLogin
	//	Purpose: detect enter key and submit login
	//	Parameters: event
	//

	var key = window.event ? e.keyCode : e.which;
	if (key == 13)
	{
		Search();
	}

}

function EditBlurb()
{
    //
    //  Function: EditBlurb
    //  Purpose: to send admin to area to edit blurb
    //  Parameters: none
    //
    
    
}

function ShowAdmin()
{
	//
	//	Function: ShowAdmin
	//	Purpose: to show all admin related buttons on the index page
	//	Parameters: none
	//
	
	getEl('btnEditBlurb', window.top).style.display = 'block';	
	getEl('btnAdministration', window.top).style.display = 'block';	
	getEl('btnEditNetwork', window.top).style.display = 'block';	
}

function HideAdmin()
{
	//
	//	Function: HideAdmin
	//	Purpose: to hide all admin related buttons on the index page
	//	Parameters: none
	//
	
	getEl('btnEditBlurb', window.top).style.display = 'none';	
	getEl('btnAdministration', window.top).style.display = 'none';	
	getEl('btnEditNetwork', window.top).style.display = 'none';	
}



function ForgottenPassword_callback(result)
{
	if (result == "OK")
		Message("New Password", "If the email address entered was valid, a new password has been sent.");
	else
	{
		alert(result);
	}
}

function ForgottenPassword()
{

	//
	//	Function: Login
	//	Purpose: to pass prep an ajax login
	//	Parameters:
	//		username (string): retrieved from textbox
	//		password (string): retrieved from textbox
	//

	//
	//	Variables
	//

	var email = getEl('txtEmail').value;

	if (email.length > 0)
	{
		agent.call(root + 'include/services.php','ForgottenPassword', 'ForgottenPassword_callback', email);
	}
	else
	{
		//
		//	Show error message
		//

		alert("You must enter an email address");
	}
}
