// JavaScript Document

function saveAlbum() {
    return;
}

function saveSongs(pk)
{
	deleteCookie('basket', '/');
	price=0;
	sng=new Array();
	sng_pk='';
	if(typeof(pk)!='undefined')
	{
		sng[pk]=new Object(parse_str(songs[pk]));
		price=parseFloat(sng[pk]['price']);
	}
	else
	{
		lst=document.getElementById('traklist').getElementsByTagName('input');
		for(i=0; i<lst.length; i++) if (lst[i].type=='checkbox' && lst[i].checked)
		{
			pk=lst[i].id.substr(8);
			sng[pk]=new Object(parse_str(songs[pk]));
			price+=parseFloat(sng[pk]['price']);
			sng_pk += pk + ',';
		}
	}
	if(sng.length>0)
		{
		//var obj=new Object({'artist':parse_str(artist), 'album':parse_str(album), 'songs':sng_pk});
		setCookie('basket', sng_pk, null, '/');
		}
	else
		deleteCookie('basket', '/');
    
	price=Math.round(price*100)/100;

	return price;

}

function buyAlbum(id, price)
{
	
	
	if (isDisabledButtons()) {
		return false;
	}

    if (is_special_user) {
        var msg = 'Album is available for downloading';
    }else{
        var msg = 'Your account will be charged at the rate of $' + price + ' for this operation.';
    }
    

	if (!confirm(msg)) {
		return false;
	}

	if (isDisabledButtons()) {
		return false;
	}

	
	disableAllButtons();
	changeToWaitingButton(id, 'album');
	JsHttpRequest.query(
		'/basket/buyAjax/', // backend
		{
			'buy_type': 'A',
			'buy_id': id
		},
		function(result, errors) {
			changeToDownloadButton(id, 'album');
			enableAllButtons();
			showDownloadMessage(result, 'album');
			
		},
		true // disable caching
	);
	
	//alert("Ok");
	return false;
}

function buySong(id, price)
{
	if (isDisabledButtons()) {
		return false;
	}
    
    if (is_special_user) {
        var msg = 'Track is available for downloading';
    }else{
        var msg = 'Your account will be charged at the rate of $' + price + ' for this operation.';
    }  

	if (!confirm(msg)) {
		return false;
	}

	if (isDisabledButtons()) {
		return false;
	}

	disableAllButtons();
	changeToWaitingButton(id, 'song');
	JsHttpRequest.query(
		'/basket/buyAjax/', // backend
		{
			'buy_type': 'S',
			'buy_id': id
		},
		function(result, errors) {
			changeToDownloadButton(id, 'song');
			enableAllButtons();
			showDownloadMessage(result, 'song');
			
		},
		true // disable caching
	);
	
	//alert("Ok");
	return false;
}

function showDownloadMessage(result, type)
{
	if ("error" == result["type"]) {
		switch (result["data"]) {
			case "noauth":
				alert("Error!!!\nYou are not authtorized. Please login.");
				break;
			case "notfound":
				alert("Error!!!\nItem not found.");
				break;
			case "notenougthmoney":
				openPopupNotEnouthWindow();
				//alert("Error!!!\nYou do not have enought money.");
				break;
			case "busy":
				alert("Error!!!\nWe are sorry but the server is closed for maintainance.");
				break;
		}
	} else {
		correctlyPurchased(result, type);
	}
}

function changeToWaitingButton(id, type)
{
	var obj = document.getElementById('downbut_' + type + "_" + id);
	var obj_unc = document.getElementById('downbut_' + type + "_" + id + "_unc");
	if (!obj || !obj_unc) {
		return false;
	}

	obj.style.display = "none";
	obj = document.getElementById('downbut_' + type + "_" + id + "_don");
	if (obj) {
		obj.style.display = "none";
	}

	obj_unc.style.display = "";
}

function correctlyPurchased(data, type)
{
	var obj = document.getElementById('auth_user_balance');
	if (obj && data["balance"]) {
		obj.innerHTML = "&nbsp;$" + correctPrice(data["balance"]);
	}

	openPopupWindow();
}

function correctPrice(price)
{
	price = Math.ceil(price*100); // bug 0.00000001
	price = price + "";
	while (price.length < 3) {
		price = "0" + price;
	}

	var start_price = price.substr(0, price.length - 2);
	var end_price = price.substr(price.length - 2, 2);
	return start_price + "." + end_price;
}

function changeToDownloadButton(id, type)
{
	var obj = document.getElementById('downbut_' + type + "_" + id + "_don");
	var obj_unc = document.getElementById('downbut_' + type + "_" + id + "_unc");
	if (!obj || !obj_unc) {
		return false;
	}

	obj_unc.style.display = "none";
	obj.style.display = "";
}

function disableAllButtons()
{
	disabled_buttons = true;
}

function enableAllButtons()
{
	disabled_buttons = false;
}

function isDisabledButtons()
{
	return disabled_buttons;
}

	
function fnSelectionInverse(p)
{
	chboxes = document.getElementById('traklist').getElementsByTagName('input');
	for (i=0; i<chboxes.length; i++) {
		chboxes[i].checked=!chboxes[i].checked;
	}
}

function checkout(ptr)
{
	if (isDisabledButtons()) {
		return false;
	}
	p = saveSongs();

	if (p!=0) {        
		deleteCookie('basket', '/');
        
        if (p > balance) {
            openPopupNotEnouthWindow();
            return false;
        }
        
		if (!confirm('Your account will be charged at the rate of $' + p + ' for this operation.')) {
			return false;
		}

		if (isDisabledButtons()) {
			return false;
		}

		disableAllButtons();
		changeToWaitingButton(0, 'songs');
		var ids = new Array();
		var lst = document.getElementById('traklist').getElementsByTagName('input');
		for(i=0; i<lst.length; i++) { 
			if (lst[i].type=='checkbox' && lst[i].checked) {
				ids[ids.length] = lst[i].id.substr(8);
			}
		}
		JsHttpRequest.query(
			'/basket/buyAjax/', // backend
			{
				'buy_type': 'S',
				'buy_id': ids
			},
			function(result, errors) {
				changeToDownloadButton(0, 'songs');
				enableAllButtons();
				showDownloadMessage(result, 'songs');
			},
			true // disable caching
		);
	} else {
		alert('Please, choose tracks for downloading.');
		return false;
	}

	return false;
}

function closePopupWindow()
{
	var obj = document.getElementById('popup');
	if (obj) {
		obj.style.display = "none";
	}
}

function openPopupWindow()
{
	var obj = document.getElementById('popup');
	if (obj) {
		var scrollTop = 0;
		if (window.pageYOffset) {  
			scrollTop = window.pageYOffset 
		} else if(document.documentElement && document.documentElement.scrollTop) { 
			scrollTop = document.documentElement.scrollTop; 
		} else if(document.body) { 
			scrollTop = document.body.scrollTop; 
		}

		var eTop;
		if (document.body.clientWidth <= 550) {
			obj.style.top = scrollTop + 30 + "px";
		} else {
			obj.style.top = scrollTop + 150 + "px";
		}

		obj.style.display='block';
	}
}

function openPopupNotEnouthWindow()
{
	var obj = document.getElementById('popup_not_enouth_money');
	if (obj) {
		var scrollTop = 0;
		if (window.pageYOffset) {  
			scrollTop = window.pageYOffset 
		} else if(document.documentElement && document.documentElement.scrollTop) { 
			scrollTop = document.documentElement.scrollTop; 
		} else if(document.body) { 
			scrollTop = document.body.scrollTop; 
		}

		var eTop;
		if (document.body.clientWidth <= 550) {
			obj.style.top = scrollTop + 30 + "px";
		} else {
			obj.style.top = scrollTop + 150 + "px";
		}

		obj.style.display='block';
	}
}

function closePopupNotEnouthWindow()
{
	var obj = document.getElementById('popup_not_enouth_money');
	if (obj) {
		obj.style.display = "none";
	}
}