var ch=true;

function SubmitCart()
{
	counter = 0;
	total = 0;
	form = document.getElementById( 'id_addform' );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			total++;
			if( !form.elements[i].checked )
			{
				counter++;
			}
		}
	}

	if( counter == total )
	{
		alert( "At least one track should be selected!" );
		return false;

	}
	form.submit();
	return true;
}

function SubmitAddSearch()
{
	counter = 0;
	total = 0;
	form = document.getElementById( 'id_tracklist' );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			total++;
			if( !form.elements[i].checked )
			{
				counter++;
			}
		}
	}

	if( counter == total )
	{
		alert( "At least one track should be selected!" );
		return false;

	}
	form.submit();
	return true;
}


function SelectAllContent( event )
{
	form = document.getElementById( 'id_addform' );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			form.elements[i].checked = ch;
		}
	}
	ch=!ch;

	if (!event)
	{
		var event = window.event;
		event.returnValue = false;
	}

	event.cancelBubble = true;

	if( event.stopPropagation )
	{
		event.stopPropagation();
	}
	return false;
}



function InstantDownload( aname, total )
{
	var splt = new Array();
	splt = fAlbTotal.toString().split( "." );

	var fDscTotal = splt[0]+'.'+splt[1].substr( 0, 2 );


	if( confirm( 'You are going to purchase "'+aname+'" album.\r\nYour balance will be charged at '+fDscTotal+'$ rate.\r\n\r\nDo You wish to proceed?' ) )
	{
		//alert( 'Proceed...\r\nThis feature is unavailable yet...' );
		document.getElementById( 'id_dlform' ).submit();
		return true;
	}

	return true;
}


function AddBookmark()
{
	//window.external.AddFavorite('http://justmusicstore.com/','JustMusicStore.com :: mp3 download');
	title = "JustMusicStore.com :: mp3 download";
	// Blogger - Replace with <$BlogItemTitle$>   // MovableType - Replace with <$MTEntryTitle$>
	url = "http://justmusicstore.com/";
	// Blogger - Replace with <$BlogItemPermalinkURL$>   // MovableType - Replace with <$MTEntryPermalink$>  // WordPress - <?php bloginfo('url'); ?>
	if (window.sidebar)
	{ // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.external )
	{
		// IE Favorite
		window.external.AddFavorite( url, title);
	}
	else if(window.opera && window.print)
	{
			// Opera Hotlist
			return true;
	}
}



function RecPass()
{
	w = window.open( "/RecoverPassword/", "recpass", "width=750,height=350,status=no,toolbar=no,menubar=no" );
	window.w.focus();
	//if( window.event != null )
		window.event.returnValue = false;
	return false;
}



function SubmitSearch()
{
	form = document.getElementById( 'id_searchform' );
	if( form.q.value.length < 1 )
	{
		alert( "You have to specify string to search!" );
		return false;
	}


	form.submit();
	return true;
}



function SelectCart()
{
	form = document.getElementById( 'id_icartform' );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			form.elements[i].checked = ch;
		}
	}
	ch=!ch;
	return false;
}



function ProceedCart( iact )
{
	counter = 0;
	total = 0;
	form = document.getElementById( 'id_icartform' );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			total++;
			if( !form.elements[i].checked )
			{
				counter++;
			}
		}
	}

	if( counter == total )
	{
		alert( "At least one track should be selected!" );
		return false;

	}

	if( iact < 1 )
		// delete from iCart
		form.iaction.value = 0;


	form.submit();
	return true;
}

function ProceedCheckout()
{
	document.getElementById('id_checkout').submit();
	return true;
}


function ProceedRedeemGift()
{
	counter = 0;
	total = 0;
	form = document.getElementById( 'id_redeemform' );
	if( form.gift_refid.value.length != 32 )
	{
		alert( 'specified code doesn\'t conform to valid Gift Certificate...' );
		return false;
	}


	form.submit();
	return true;
}



function ProceedGift( event )
{
	form = document.getElementById( 'id_giftform' );
	var proceed = true;


	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="text" && form.elements[i].value.length < 1 )
		{
			DisplayGiftErrorNote( form.elements[i].name );
			proceed = false;
			break;
		}
	}

	if( proceed )
		return true;

	if (!event)
	{
		var event = window.event;
		event.returnValue = false;
	}

	event.cancelBubble = true;

	if( event.stopPropagation )
	{
		event.stopPropagation();
	}
	return false;
}



function DisplayGiftErrorNote( name )
{
	switch( name )
	{
		case "gift_from":
			alert( "You haven't specified your name.\nPlease type it in the proper field." );
			return;
		case "gift_to":
			alert( "You haven't specified your friend's name.\nPlease type it in the proper field." );
			return;
		case "gift_toemail":
			alert( "You haven't specified your friend's e-mail.\nPlease type it in the proper field." );
			return;
		case "gift_toemail2":
			alert( "You haven't confirmed your friend's e-mail.\nPlease type it in the proper field." );
			return;
		case "gift_message":
			alert( "Please type some message attached to your gift! :)" );
			return;
		default:
			alert( "Unexpected" );
			return;
	}
}


function ProceedProfile( event )
{
	return true;
}

function ProceedAuthMain( event )
{
	form = document.getElementById( 'id_authmain' );
	var proceed = true;
	if( form.login_email.value.length < 1 || form.login_password.value.length < 1 )
	{
		proceed = false;
		alert( 'Either Your e-mail or password wasn\'t specified!' );
	}


	if( proceed )
		return true;

	if (!event)
	{
		var event = window.event;
		event.returnValue = false;
	}

	event.cancelBubble = true;

	if( event.stopPropagation )
		event.stopPropagation();

	return false;
}

function SubmitAuth()
{
	form = document.getElementById( 'id_loginform' );
	if( form.login_email.value.length < 1 || form.login_password.value.length < 1 )
	{
		alert( 'Either Your e-mail or password wasn\'t specified!' );
		return false;
	}

	form.submit();
	return true;
}


function ProceedAuth( event )
{
	form = document.getElementById( 'id_loginform' );
	var proceed = true;
	if( form.login_email.value.length < 1 || form.login_password.value.length < 1 )
	{
		proceed = false;
		alert( 'Either Your e-mail or password wasn\'t specified!' );
	}


	if( proceed )
	{
		//form.submit();
		return true;
	}

	if (!event)
	{
		var event = window.event;
		event.returnValue = false;
	}

	event.cancelBubble = true;

	if( event.stopPropagation )
		event.stopPropagation();

	return false;
}










function FlashTrack( id, art, alb, tr )
{
	var query = "trid="+id+"&artist="+escape( art )+"&album="+escape( alb )+"&track="+escape( tr );

	w = window.open( "/flash_track.php?"+query, "flashplayer", "width=250,height=140,status=no,toolbar=no,menubar=no" );
	window.w.focus();

	//window.event.returnValue = false;
	return false;
}

function WarpGenre()
{
	search = document.getElementById( 'id_searchform' );
	id = search.bg.value;

	window.open( "/Genre/"+id+"/", "_self" );
	return false;
}

function OpenHelpWnd()
{
	w = window.open( "/Help/#a11", "wnd", "width=800,height=600,status=no,toolbar=no,menubar=no,scrollbars=yes" );
	window.w.focus();
	window.event.returnValue = false;
	return false;
}


function FlipLangDiv()
{
	var lang_inner_wrap = document.getElementById( 'id_lang_inner_wrap' );
	lang_inner_wrap.style.position = 'absolute';
	lang_inner_wrap.style.width = 30+'px';

	//if( window.external )
	//	lang_inner_wrap.style.top = -30 +'px';
	//else
	//	lang_inner_wrap.style.top = -5 +'px';

	//lang_inner_wrap.style.height = 60+'px';
	//lang_inner_wrap.style.border = '1px solid #000000';
}






function MakeTotal()
{
	var fAlbTotal =0.0, fOrg, fRes;
	for( var key in pArray )
	{
		var trp_div = document.getElementById( key );
		if( usr_dsc > 1 )
		{
			fOrg = pArray[key] * ( 1 - parseFloat(usr_dsc/100) );
			fRes = Math.ceil( fOrg * 100 ) / 100;
			fAlbTotal += fRes;

			//if( conf_lang.value == 'en' )
			//	document.getElementById( key ).innerHTML = fRes+'$';
			//else
			//	document.getElementById( key ).innerHTML = fRes+'&#128;';
			trp_div.innerHTML = fRes+' '+price_char;
		}
		else
		{
			fAlbTotal += pArray[key];
			trp_div.innerHTML = pArray[key]+' '+price_char;
		}
	}

	fAlbTotal = Math.round( fAlbTotal * 100 ) / 100;
	return fAlbTotal;
}



function FlipInnerDivs( lang_id )
{
	if( usr_dsc > -1 )
	{

		var splt = fAlbTotal.toString().split( "." );
		var formAlbTotal = splt[0]+'.'+splt[1].substr( 0, 2 );

		var dsc_block = document.getElementById( 'id_dsc_block' );
		var alb_total = document.getElementById( 'id_alb_total' );


		alb_total.innerHTML = formAlbTotal+' '+price_char;

		switch( lang_id )
		{
			case 1:
				dsc_block.innerHTML = 'All MP3 prices are listed below according to <b>Your discount</b>.';
				break;

			case 2:
				dsc_block.innerHTML = 'Le prix des chansons est indiqu&#233; en tenant <b>compte de votre remise</b>.';
				break;

			case 3:
				dsc_block.innerHTML = '';
				break;

			case 4:
				dsc_block.innerHTML = '';
				break;

			case 5:
				dsc_block.innerHTML = '';
				break;

			default:
				dsc_block.innerHTML = 'All MP3 prices are listed below according to <b>Your discount</b>.';
				break;
		}

	}
}

function FlipReleases( cnt, r_list )
{
	r_splt = r_list.split( ";" );

	for( i=1;i<=cnt;i++ )
	{
		flip = document.getElementById('flid_'+i);
		flip.style.position = 'absolute';
		if( usr_dsc > 1 )
		{
			r_price = flip.innerHTML.substr( 1 ) * 1;
			r_dsc_price = 0.0;

			r_cell_list = r_splt[i-1].split( "," );
			for( var index in r_cell_list )
			{
				r_cell = r_cell_list[index].split( "x" );
				num = r_cell[0];
				t_price = r_cell[1];

				for( q=0;q<num;q++ )
					r_dsc_price += Math.ceil( t_price * (1 - parseFloat(usr_dsc/100) ) * 100 ) / 100;
			}

			d_splt = r_dsc_price.toString().split( "." );
			r_dsc_price = d_splt[0]+'.'+d_splt[1].substr( 0, 2 );
		}
		else
		{
			r_dsc_price = flip.innerHTML.substr( 1 ) * 1;
		}

		// flip release
		flip.innerHTML = '<font color="#FFFFFF">'+ price_char + r_dsc_price+'</font>';
	}
}

function FlipSaleImages( sale_cnt )
{
	for( i=1;i<=sale_cnt;i++ )
	{
		flip = document.getElementById('imgflid_'+i);
		flip.style.position = 'absolute';
		if( window.external )
			flip.style.top = -3 + 'px';
	}
}
function setPaymentValue(num)
{
    var link_ = document.getElementById('link_to_payment');
    var value = ''
    var summ = 0;
    switch(num)
    {
        case "19.95":
            summ = 1995
            break;
        case "29.95":
            summ = 2995
            break;
        case "49.95":
            summ = 4995
            break;
    }
    if(link_){
        link_.href = link_.href.replace(/amount\=1995/,'amount=SUMM_NUM').replace(/amount\=2995/,'amount=SUMM_NUM').replace(/amount\=4995/,'amount=SUMM_NUM')
        link_.href = link_.href.replace(/SUMM_NUM/,summ)
    }
}

function HandleICartPurchase( event )
{
	counter = 0;
	total = 0;
	form = document.getElementById( 'id_icartform' );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			total++;
			if( !form.elements[i].checked )
			{
				counter++;
			}
		}
	}

	if( counter == total )
	{
		alert( "At least one track should be selected!" );

	}

	form.iaction.value = 1;
	form.submit();
	return false;
}


function HandleICartRemove( event )
{
	counter = 0;
	total = 0;
	form = document.getElementById( 'id_icartform' );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			total++;
			if( !form.elements[i].checked )
			{
				counter++;
			}
		}
	}

	if( counter == total )
	{
		alert( "At least one track should be selected!" );

	}

	form.iaction.value = 0;
	form.submit();
	return false;
}
