var ch=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;
		if(event && event.returnValue)
		  event.returnValue = false;
	}

	if(event && event.cancelBubble)
	   event.cancelBubble = true;

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


function VerifyEmptyCart( event )
{
	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!" );

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

		event.cancelBubble = true;

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

	}
	return true;
}





function SelectAll( form_id, shouldSelect )
{
	form = document.getElementById( form_id );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="checkbox" )
		{
			form.elements[i].checked = shouldSelect;
		}
	}
	ch=!ch;

	if( window.event )
		window.event.returnValue = false;

	return false;
}

function VerifyDeleteIcart( form_id )
{
	counter = 0;
	total = 0;
	form = document.getElementById( form_id );
	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!" );
		if( window.event )
			window.event.returnValue = false;

		return false;

	}
	return true;
}

function VerifyPurchaseGift( form_id )
{
	form = document.getElementById( form_id );

	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 );
			window.event.returnValue = false;
			return false;
		}
	}
	return true;
}

function VerifyAuthFields( form_id )
{
	form = document.getElementById( form_id );

	for( i=0; i<form.elements.length; i++ )
	{
		if( ( form.elements[i].type=="text" || form.elements[i].type=="password" ) && form.elements[i].value.length < 1 )
		{
			alert( "Either your e-mail or password wasn't specified!" );
			window.event.returnValue = false;
			return false;
		}
	}
	return true;
}

function VerifyRedeemGift( form_id )
{
	form = document.getElementById( form_id );
	for( i=0; i<form.elements.length; i++ )
	{
		if( form.elements[i].type=="text" && form.elements[i].value.length < 32 )
		{
			alert( "Your Gift certificate code doesn't conform to the expected code format!" );
			window.event.returnValue = false;
			return false;
		}
	}
	//alert( "reached" );
	return true;
}

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 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;
}*/

//new player
function FlashTrack( id, tid )
{	
	w = window.open( "/flash_player/flash_track.php?type=" + tid + "&id=" + id, "flashplayer", "width=450,height=350,status=no,toolbar=no,menubar=no" );
	window.w.focus();
	if(window && window.event && window.event.returnValue)
		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 MakeTotal()
{
	var fAlbTotal =0.0, fOrg, fRes;
	for( var key in pArray )
	{
		if( usr_dsc > 1 )
		{
			fOrg = pArray[key] * ( 1 - parseFloat(usr_dsc/100) );
			fRes = Math.ceil( fOrg * 100 ) / 100;
			fAlbTotal += fRes;
			document.getElementById( key ).innerHTML = fRes+'$';
		}
		else
		{
			fAlbTotal += pArray[key];
		}

	}

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

function InstantDownload( aname, total )
{
	var splt = new Array();
	splt = fAlbTotal.toString().split( "." );
	if(splt[1] && splt[1]!='undefined'){
	  var cents = splt[1].substr( 0, 2 )
	}else
	  var cents = '00';
	var fDscTotal = splt[0]+'.'+cents;

    var a = 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?' );
	if( a )
	{
		//document.getElementById( 'id_dlform' ).submit();
		SelectAllContent();
		VerifyEmptyCart();
		document.getElementById( 'id_addform' ).submit();
		return true;
	}
	return true;
}

function FlipInnerDivs()
{
	if( usr_dsc > 0 )
	{
		//document.getElementById( 'id_dsc_block' ).innerHTML = '<span class="tredb_s">***</span>Prelisten samples are <span class="tblueb_s">free-for-use</span> and exposed in low quality.<br>All MP3 prices are listed below according to <span class="tredb_s">Your discount</span>.<br><h2 class="tblackb_s">&nbsp;</h2>';

		var splt = new Array();
		splt = fAlbTotal.toString().split( "." );

		if(splt[1] && splt[1]!='undefined'){
		  var cents = splt[1].substr( 0, 2 )
		}else
		  var cents = '00';
		var formAlbTotal = splt[0]+'.'+cents;
		document.getElementById( 'id_alb_total' ).innerHTML = formAlbTotal+'$';
	}
}




function RaisePromoForm( aid )
{
	var promoBlock = document.getElementById( 'id_send_promo' );
	promoContent = promoBlock.innerHTML;

	promoBlock.style.width = 100 + '%';
	promoBlock.style.align = 'center';
	promoBlock.style.border = '1px solid black';
	promoBlock.innerHTML = //'<script type="text/javascript" src="/js/ajax.js"></script>' +

	'<table border="0">' +
	'<tr><td align="center" class="tblackr_s">' +
	'<div align="justify" style="width:100%">' +
	'<br><ul>'+
	'<li>Everyone loves music! And there is no place on the internet with such a great music collection. Why not share it with your friends? We are sure they are tired of high prices, DRMs and poor choice of other online music stores...</li><br>'+
	'<li>Just input your friends names and their emails into the following fields. Once they sign-up and add balance <b>you will get 10% of the amount they pay</b> to your balance. <br>And this is going to be <b>for a lifetime</b>!</li><br>'+
	'<li>No need to pay more - just tell friends about us! <br>'+
	'And we are sure they will appreciate it! <br>'+
	'Just like you!'+
	'</li>'+
	'</ul>'+
	'<div align="center">'+
	'<form action=/handlers/submit_promo_process.php method="POST" onsubmit="SubmitPromoForm();" id="id_formpromo">'+
	'<table border="0">'+
	'<tr>'+
		'<td class="tblueb_s" align="right">e-mail: </td><td><input type="text" name="email" id="id_promo_mail" class="tredb_s"></td>'+
	'</tr>'+
	'<tr>'+
		'<td class="tblueb_s" align="right">name: </td><td><input type="text" name="rcpt_name" id="id_promo_rcpt_name" class="tredb_s"></td>'+
	'</tr>'+
	'<tr>'+
		'<td colspan="2" align="right">'+
		'<input type="submit" value="Send!" class="tblackb_s">'+
		'<input type="submit" value="Hide" class="tblackr_s" onclick="HidePromoForm( '+aid+');">'+
		'</td>'+
	'</tr>'+
	'</table>'+
	'</form>'+
	'</div>'+
	'</div>'+
	'</td></tr>'+
	'</table>';


	//'<div align="center">e-mail:<br><input type="text" name="email" id="id_promo_mail" class="tredb_s">' +
	//'<br><input type="submit" value="Send!" class="tblackb_s" onclick="SubmitPromoForm();"> ' +
	//'<input type="submit" value="Hide" class="tblackb_s" onclick="HidePromoForm( '+aid+');"></div>';
	/*
	'<form action=/handlers/submit_promo_process.php method="POST" onsubmit="SubmitPromoForm();" id="id_formpromo">' +
	'<table width="100%"><tr>' +
	'<td class="tblackr_s" colspan="2"><div align="center">What is "<b>EXTRA BONUS</b>" on my balance?</div></td></tr><tr>' +
	'<td class="tblackr_s" colspan="2"><div align="left"> &nbsp; &nbsp; it is simple:</div><ul>' +
	'<li>You can share with the single click Your favourite tunes with Your friends!</li>' +
	'<li>You will get 10% of EVERY payment made by Your friends and therefore You will gain more cool music from our site!</li>' +
	'<li>Just put down Your friend\'s e-mail and link will be sent immediately!<br>' +
	'<br><div align="center">e-mail: <input type="text" name="email" id="id_promo_mail" class="tredb_s">' +
	'<br>name: <input type="text" name="rcpt_name" id="id_promo_rcpt_name" class="tredb_s">' +
	'<br><input type="submit" value="Send!" class="tblackb_s"> &nbsp; &nbsp; ' +
	'<input type="submit" value="Hide" class="tblackb_s" onclick="HidePromoForm( '+aid+');"></div></li><ul></td>' +
	'</tr></table></form>';
	*/



	//alert( 'album id was :: '+aid );
}

function HidePromoForm( aid )
{
	var promoBlock = document.getElementById( 'id_send_promo' );
	promoBlock.style.border = '';
	promoBlock.innerHTML = promoContent;
}

function SubmitPromoForm( event )
{
	var note = '';

	while( true )
	{
		var email = document.getElementById( 'id_promo_mail' );
		if( email.value.length < 1 )
		{
			note = 'You should supply recepient e-mail!';
			break;
		}
		reg = new RegExp( "[0-9a-zA-Z-._]+@[0-9a-zA-Z-._]+\.[0-9a-zA-Z]{2,4}", "g" );
		ar = reg.exec( email.value );
		if( !ar )
		{
			note = "Specified e-mail doesn't seem to be valid.\r\nPlease check out if You have typed it correctly.";
			break;
		}

		var name = document.getElementById( 'id_promo_rcpt_name' );
		if( name.value.length < 1 )
		{
			note = 'You should supply friend\'s name!';
			break;
		}

		break;
	}

	if( note.length > 0 )
	{
		alert( note );
		if (!event)
		{
			var event = window.event;
			event.returnValue = false;
		}

		event.cancelBubble = true;

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

	return true;

	//alert( 'Proceed with ' + email.value );

	//var promoBlock = document.getElementById( 'id_send_promo' );
	//promoBlock.style.width = 100 + '%';
	//promoBlock.style.height = 100 + '%';
	//promoBlock.style.align = 'center';
	//promoBlock.style.border = '1px solid black';
	//promoBlock.innerHTML = //'<script src="/handlers/send_promo_request_process.php"></script>';
	//'<script type="text/javascript" src="/js/promo.js"></script>' +
	//'please wait...';




}


function SearchFeat( query )
{
    window.open( "/Search/?q="+query+"&wh=1", "_self" );
    return;
}

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;
        case "99.95":
            summ = 9995
            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').replace(/amount\=9995/,'amount=SUMM_NUM');
        link_.href = link_.href.replace(/price\=1995/,'price=SUMM_NUM').replace(/price\=2995/,'price=SUMM_NUM').replace(/price\=4995/,'price=SUMM_NUM').replace(/price\=9995/,'price=SUMM_NUM');
        link_.href = link_.href.replace(/SUMM_NUM/,summ)
    }
}

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;
}

// -------------------------------

var http_req_archive;
var retryDelay_d=8000;
var fssichtid_d;
var fssichtid1_d;
var attempts = 1;
function DownloadPack()
{
    var div_download = document.getElementById('my_download');
	div_download.innerHTML = ''
    form = document.getElementById( 'id_activetracks' );
    var arr = new Array();
    var isCheck = false;
    for( i=0; i<form.elements.length; i++ )
    {
            if( form.elements[i].type=="checkbox" )
            {
                   if(form.elements[i].checked){if(form.elements[i].value)arr[i] = form.elements[i].value;isCheck = true;}
            }
    }

    if(!isCheck){
        alert('Please select some tracks for archive downloading process.');
        return false;
    }else{
        hideSelect();
        setInfo(0);
    }
    http_req_archive = new Subsys_JsHttpRequest_Js();
	http_req_archive.onreadystatechange = downloadProcessArchive;
    http_req_archive.caching = false;
	http_req_archive.open( 'GET', '/reverse/build_archive.php?start_process=1&id=' + arr.join(",") + "&uid=" + document.getElementById( 'user_id' ).value, true );
	http_req_archive.send({ dump: 1 });
    return false;
}

function setTimeoutTaskDownload()
{
    fssichtid1_d = setTimeout( 'DownloadPack()', 10000 );
}

function downloadProcessArchive()
{
	if( http_req_archive.readyState == 4 )
	{
		if( http_req_archive.responseJS )
		{
			attempts++;
			var check_stat = http_req_archive.responseJS.check_stat * 1;
			var retry = http_req_archive.responseJS.retry * 1;
			var responce_result_response = http_req_archive.responseJS.responce_result;
			if( check_stat == 1 )
			{
				// all ok
				// *
				var div_download = document.getElementById('my_download');
				div_download.innerHTML = '<a href="' + responce_result_response + '"><img src="/images/btn_an_download.gif" width="253" height="63" /></a>'
				hideSelect();
				var div_info = document.getElementById('tr_link');

				div_info.innerHTML = '<b><a href="#start" onclick="javascript:showInfo();return false;" class="black">Create new archive</a></b>';
                hideInfo();
                setInfo(1);

			}else{
			    alert('System is experiencing some technical difficulties at the moment.\nPlease try Your request again')
			}

			if( retry < 4 )
				return false;



			clearTimeout( fssichtid );
			clearTimeout( fssichtid1 );

		}

	}

}