var ch=true;


/* POPUP WINDOW */
function closeWin(k)
	{
  var win = document.getElementById(k)
  var win_bg = document.getElementById('win_bg')
  win.style.display = "none"
  win_bg.style.display = "none"
}

function openWin(k)
	{
  var win = document.getElementById(k)
  var win_bg = document.getElementById('win_bg')
  try
  {
  	win.style.display = "table"
  }
  catch (error)
  {
	  win.style.display = "block"
  }
  win_bg.style.display = "block"
}
/* END OF POPUP WINDOW */







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 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')
			}
		}

	}
}
