var ch=true;



function SelectAllContent(ch)
{
	form = document.getElementById( 'id_addtoicart' );
	if (!ch)
	{
		for ( i = 0; (i < form.elements.length) && !ch; i++ )
			if (form.elements[i].type == "checkbox" && !form.elements[i].checked)
				ch = true;
	}
	for ( i = 0; i < form.elements.length; i++ )
		if ( form.elements[i].type == "checkbox" )
			form.elements[i].checked = ch;
	//ch = !ch;
	add_to_icart();
	stop_event_propagation();
	return false;
}

function stop_event_propagation( event )
{
	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();
}

function VerifyEmptyCart( event )
{
	counter = 0;
	total = 0;
	form = document.getElementById( 'id_addtoicart' );
	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 FLASH 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 a = confirm( 'You are going to purchase "'+aname+'" album.\r\nYour balance will be charged at '+ document.getElementById( 'id_alb_total' ).innerHTML + ' Credits.\r\n\r\nDo You wish to proceed?' );
	if( a ) {
		SelectAllContent(true);
		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 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, plan){
	checkPlan(plan)
    var link_ = document.getElementById('link_to_payment');
    var value = ''
    var summ = 0;
    switch(num)
    {
        case "SP4N77EVZ9YIQMXZSKART2A9ZCAW91DC":
            summ = "SP4N77EVZ9YIQMXZSKART2A9ZCAW91DC"
            break;
        case "THBJTD39SJTENR1M2BWSH6N6TPI6EZWG":
            summ = "THBJTD39SJTENR1M2BWSH6N6TPI6EZWG"
            break;
        case "JH439FKC5ZEES74DUYT968WFD91AH8ZY":
            summ = "JH439FKC5ZEES74DUYT968WFD91AH8ZY"
            break;
        case "ZTJ7IA7111HJEUSVSI73CM963HHZQXVY":
            summ = "ZTJ7IA7111HJEUSVSI73CM963HHZQXVY"
            break;
        case "14.95":
            summ = 1495
            break;
		case "19.95":
            summ = 1995
            break;
        case "24.95":
            summ = 2495
            break;
        case "29.95":
            summ = 2995
            break;
        case "39.95":
            summ = 3995
            break;
        case "49.95":
            summ = 4995
            break;
        case "99.95":
            summ = 9995
            break;
    }
    if(link_){
        link_.href = link_.href.replace(/amount\=1495/,'amount=SUMM_NUM').replace(/amount\=1995/,'amount=SUMM_NUM').replace(/amount\=2495/,'amount=SUMM_NUM').replace(/amount\=2995/,'amount=SUMM_NUM').replace(/amount\=3995/,'amount=SUMM_NUM').replace(/amount\=4995/,'amount=SUMM_NUM').replace(/amount\=9995/,'amount=SUMM_NUM');
        link_.href = link_.href.replace(/price\=1495/,'price=SUMM_NUM').replace(/price\=1995/,'price=SUMM_NUM').replace(/price\=2495/,'price=SUMM_NUM').replace(/price\=2995/,'price=SUMM_NUM').replace(/price\=3995/,'price=SUMM_NUM').replace(/price\=4995/,'price=SUMM_NUM').replace(/price\=9995/,'price=SUMM_NUM');
        link_.href = link_.href.replace(/pid\=SP4N77EVZ9YIQMXZSKART2A9ZCAW91DC/,'pid=SUMM_NUM').replace(/pid\=THBJTD39SJTENR1M2BWSH6N6TPI6EZWG/,'pid=SUMM_NUM').replace(/pid\=JH439FKC5ZEES74DUYT968WFD91AH8ZY/,'pid=SUMM_NUM').replace(/pid\=ZTJ7IA7111HJEUSVSI73CM963HHZQXVY/,'pid=SUMM_NUM');
        link_.href = link_.href.replace(/SUMM_NUM/,summ)
    }
}

function setPaymentValue2(num, plan){
	checkPlan2(plan)
    var link_2 = document.getElementById('link_to_payment2');
    var value = ''
    var summ = 0;
    switch(num)
    {
        case "14.95":
            summ = 1495
            break;
		case "19.95":
            summ = 1995
            break;
        case "24.95":
            summ = 2495
            break;
        case "29.95":
            summ = 2995
            break;
        case "39.95":
            summ = 3995
            break;
        case "49.95":
            summ = 4995
            break;
        case "99.95":
            summ = 9995
            break;
    }
    if(link_2){
        link_2.href = link_2.href.replace(/amount\=1495/,'amount=SUMM_NUM').replace(/amount\=1995/,'amount=SUMM_NUM').replace(/amount\=2495/,'amount=SUMM_NUM').replace(/amount\=2995/,'amount=SUMM_NUM').replace(/amount\=3995/,'amount=SUMM_NUM').replace(/amount\=4995/,'amount=SUMM_NUM').replace(/amount\=9995/,'amount=SUMM_NUM');
        link_2.href = link_2.href.replace(/SUMM_NUM/,summ)
    }
}

function checkPlan(p) {

               var ch_basic = document.getElementById('ch_basic')
               var ch_plus = document.getElementById('ch_plus')
               var ch_premium = document.getElementById('ch_premium')

               var plan_basic = document.getElementById('plan_basic')
               var plan_plus = document.getElementById('plan_plus')
               var plan_premium = document.getElementById('plan_premium')

               obj = document.getElementById('ch_' + p)
               plan = document.getElementById('plan_' + p)

               ch_basic.className = 'checkbox'
               ch_plus.className = 'checkbox'
               ch_premium.className = 'checkbox'

               plan_basic.className = 'plan'
               plan_plus.className = 'plan'
               plan_premium.className = 'plan'

               if (obj.className == 'checkbox') {

                       obj.className = 'checkbox_active'
                       plan.className = 'plan plan_active'

               }

               else {
                       obj.className = 'checkbox'
                       plan.className = 'plan'
               }
}

function checkPlan2(p) {

       var ch_basic = document.getElementById('ch2_basic')
       var ch_plus = document.getElementById('ch2_plus')
       var ch_premium = document.getElementById('ch2_premium')

       var plan_basic = document.getElementById('plan2_basic')
       var plan_plus = document.getElementById('plan2_plus')
       var plan_premium = document.getElementById('plan2_premium')

       obj = document.getElementById('ch2_' + p)
       plan = document.getElementById('plan2_' + p)

       ch_basic.className = 'checkbox'
       ch_plus.className = 'checkbox'
       ch_premium.className = 'checkbox'

       plan_basic.className = 'plan'
       plan_plus.className = 'plan'
       plan_premium.className = 'plan'

       if (obj.className == 'checkbox') {

               obj.className = 'checkbox_active'
               plan.className = 'plan plan_active'

       }

       else {
               obj.className = 'checkbox'
               plan.className = 'plan'
       }
}

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

		}

	}

}

function closeWin2(k)
{
	var win = document.getElementById(k);
	var win_bg = document.getElementById('win_bg2');
	win.style.display = "none";
	win_bg.style.display = "none";
}

function openWin2(k)
{
	var win = document.getElementById(k);
	var win_bg = document.getElementById('win_bg2');
	try
	{
		win.style.display = "table";
	}
	catch (error)
	{
		win.style.display = "block";
	};
	win_bg.style.display = "block";
}

function SetUnsTrial(num){   
    http_req_archive = new Subsys_JsHttpRequest_Js();
	http_req_archive.onreadystatechange = GetUnsTrial;
    http_req_archive.caching = false;
	http_req_archive.open( 'GET', '/reverse/trial_subscribe.php?start_process=1&num=' + num , true );
	http_req_archive.send({ dump: 1 });
    return false;
}

function GetUnsTrial(){
	if( http_req_archive.readyState == 4 ){
		if( http_req_archive.responseJS ){
			var check_stat = http_req_archive.responseJS.result * 1;
			if( check_stat == 1 )
			{
				// all ok
				// *
				var div_show = document.getElementById('subs_trial');
				var div_show2 = document.getElementById('subs_untrial');
				div_show.style.display = 'none';				
				div_show2.style.display = 'block';				

			}else if(check_stat == 2){
				var div_show = document.getElementById('subs_trial');
				var div_show2 = document.getElementById('subs_trial_new');
				div_show.style.display = 'none';				
				div_show2.style.display = 'block';	
			}else{
			    //alert('System is experiencing some technical difficulties at the moment.\nPlease try Your request again')
			}		
		}

	}
}