var last_album_img_link = "";
var last_highlighted_item = "";
var link_base = "";
var counter = 0;
var timer_ = null;
var millisec = 1000;
var start = 0;
var end = 100;
var array_sort = new Array();

function sort_table(index,type) {
	el = document.getElementById("h"+index);
	if(el.getAttribute("sort") == null)
		el.setAttribute("sort","asc")
	else if(el.getAttribute("sort") == "asc")
		el.setAttribute("sort","desc")
	else if(el.getAttribute("sort") == "desc")
		el.setAttribute("sort","asc")
	
	elem1 = new Array();
	table = document.getElementById("s_table");
	tbody = table.firstChild;
	for(i=0; i < tbody.childNodes.length; i++) {
		elem = tbody.childNodes[i].childNodes;
		elem1=elem
		if (elem[0].firstChild.nodeName == "A") {
			if(elem[0].firstChild.firstChild.innerHTML != "&nbsp;" && elem[0].firstChild.firstChild.innerHTML != " &nbsp;" && 
		  		 elem[0].firstChild.firstChild.innerHTML != "&nbsp; " && elem[0].firstChild.firstChild.innerHTML != null) {
				array_sort[i] = new Array();
				for(j = 0; j < elem.length; j++) {
				   array_sort[i][j] = elem[j].firstChild.firstChild.innerHTML;
				}
			}
		} else {
			if(elem[0].firstChild.innerHTML != "&nbsp;" && elem[0].firstChild.innerHTML != " &nbsp;" && 
		  		 elem[0].firstChild.innerHTML != "&nbsp; " && elem[0].firstChild.innerHTML != null) {
				array_sort[i] = new Array();
				for(j = 0; j < elem.length; j++) {
				   array_sort[i][j] = elem[j].firstChild.innerHTML;
				}
			}	
		}
	}
	new_length = array_sort.length;
	for(k = 0; k < array_sort.length; k++) {
		for(z = k+1; z < array_sort.length; z++) {
			temp_array = new Array();
			temp_array[0] = array_sort[k][index]
			temp_array[1] = array_sort[z][index]
			if(type=="s")
				return_value = compare_fields_for_sort_s(temp_array[0], temp_array[1])
			else if(type=="n")
				return_value = compare_fields_for_sort_n(temp_array[0], temp_array[1])
			else if(type=="d")
				return_value = compare_fields_for_sort_d(temp_array[0], temp_array[1])
			if(el.getAttribute("sort") == "asc") {
				if(return_value == 1) {
					temp_a = new Array();
					temp_a = array_sort[k];
					array_sort[k] = array_sort[z];
					array_sort[z] = temp_a;
				}
			}else if(el.getAttribute("sort") == "desc") {
				if(return_value == -1) {
					temp_a = new Array();
					temp_a = array_sort[k];
					array_sort[k] = array_sort[z];
					array_sort[z] = temp_a;
				}
			}
		}	
	}
	
	for(i=0; i < new_length; i++) {
		elem = tbody.childNodes[i].childNodes;
		if (elem[0].firstChild.nodeName == "A") {
			for(j = 0; j < elem.length; j++) {
		   		elem[j].firstChild.firstChild.innerHTML = array_sort[i][j];
			}
		}else {
			for(j = 0; j < elem.length; j++) {
		   		elem[j].firstChild.innerHTML = array_sort[i][j];
			}
		}
	}
}

function compare_fields_for_sort_n(arg1, arg2) {
	arg1 = arg1.substr(2, arg1.length)
	arg2 = arg2.substr(2, arg2.length)
	float1 = parseFloat(arg1);
	float2 = parseFloat(arg2);
	if(float1 > float2)
		return 1;
	else
		return -1;
	
}
function compare_fields_for_sort_d(arg1, arg2) {
	int1 = parseInt(arg1);
	int2 = parseInt(arg2);
	if(int1 > int2)
		return 1;
	else
		return -1;
	
}

function compare_fields_for_sort_s(arg1, arg2) {
	arg1 = arg1.toUpperCase();
	arg2 = arg2.toUpperCase();
	min = arg1.length > arg2.length ? arg2.length : arg1.length;
	for(no = 0; no < min; no++) {
		if(arg1.charCodeAt(no) < arg2.charCodeAt(no)) {
			return -1;
		}
		
		if(arg1.charCodeAt(no) > arg2.charCodeAt(no)) {
			return 1;
		}
		
		if(no == min-1) {
			return -1;
		}
		
	}
}


function no_cd() {
	link = document.getElementById("link_rip");
	link_now = link.href;
	if(link_now.indexOf("choosecd") != -1) { 
		alert("Please choose a cd first");	
	}
}

function focus_to_password() {
	document.getElementById("password").focus();
}

function fadeimg() {
	processfadeimg(0);
}

function processfadeimg(number_of_img) {	
	do_fade_image(number_of_img)	
}

function do_fade_image(elemid) 
{	
	opacity(elemid, 0, 100);
}

function slide_track(side,e) {
	user_share_e = document.getElementById("i1");
	exchangerate_e = document.getElementById("exchangerate");
	var exchangerate = parseFloat(exchangerate_e.value);
	
	if(side == "left") {
		user_share = (parseFloat(user_share_e.value)/exchangerate) * 200 - 2;
//		user_share = parseFloat(user_share_e.value) * 200 - 2;
	}else if(side == "right") {
		user_share = (parseFloat(user_share_e.value)/exchangerate) * 200 + 2;
//		user_share = parseFloat(user_share_e.value) * 200 + 2;
	}
	temp = parseFloat(user_share/200);
	if(side == "left") {
		if(temp >= 0) {
//			val =  parseFloat(user_share/200);
			val =  parseFloat((user_share*exchangerate)/200);
			user_share_e.value = val.toFixed(2)
			slider_element.style.cssText = "left:" + user_share + "px;";
			change_price(exchangerate);
			update_graph();
		}
	}else if(side == "right") {
		if(temp <= 1) {
//			val =  parseFloat(user_share/200);
			val =  parseFloat((user_share*exchangerate)/200);
			user_share_e.value = val.toFixed(2)
			slider_element.style.cssText = "left:" + user_share + "px;";
			change_price(exchangerate);
			update_graph();
		}
	}
}

function register_onload(functioncall,num, nrows) {
//alert(num);
//alert(nrows);
	var OnLoad = fadetext(num,nrows);
	window.onload = function() {eval(OnLoad);};
}

function fadetext(num,nrows) {
//alert(num);
//alert(nrows);
	timer_ = setInterval("do_fade(" + num + "," +  nrows + ")",40);
}

function do_fade(num,nrows) {
	var fadetextspan = document.getElementsByName("trt");
	if(counter >= num) {
		clearInterval(timer_);
		timer_ = null;
		
		if(document.getElementById("idalbum") != null) {
			opacity_single("idalbum", 0, 100,2000)
		}
		//part for deleting empty rows
		/*if(document.getElementById("search_table") == null) {
			
		}else {
			delete_empty_rows(nrows);
		} */
	}else {
		if(fadetextspan[counter] != null) {
			fadetextspan[counter].style.cssText = "visibility: visible";
			counter++;
		}	
	}
}

function delete_empty_rows(nrows) {
	table_temp = document.getElementById("search_table");
	if(table_temp) {
		for(i=0; i < nrows; i++) {
		tr = document.getElementById("tr" + i.toString());
		for(j=0; j < tr.childNodes.length; j++) {
			elem = tr.childNodes[j].childNodes[0];
			if (elem.nodeName == "A") elem = elem.childNodes[0];
			if(elem.innerHTML == "&nbsp;" ||
			   elem.innerHTML == " &nbsp;" ||
			   elem.innerHTML == "&nbsp; " || 
			   elem.innerHTML == null) {
				tr.childNodes[0].style.cssText = "border-left: 3px solid #ffffff; border-right: 0px; border-bottom: 0px;"
				for(k = 1; k < tr.childNodes.length; k++) {
					 tr.childNodes[k].style.cssText = "border-left: 0px solid #ffffff; border-right: 0px; border-bottom: 0px;"				
				}
				break;
			}
		}
	  }
	}
}
  
function onclick_album_img(e) {
	if(last_album_img_link != "") {
		last_img = document.getElementsByName(last_album_img_link);
		last_img[0].style.cssText = "border:0px;"; 	
	}
	last_album_img_link = e.name;
	link = document.getElementsByName("rip_page");	
	link[0].href = "index.php?cmd=rip&album_id=" + e.name;
	e.style.cssText="border:3px solid #FF962D;";
}

function highlight(e,sellerid) {

	if(last_highlighted_item == "") {
		highlighted_default = document.getElementById("0");
		highlighted_default.style.cssText = "background-color: black;";
		last_highlight = document.getElementById(e.id);
		last_highlight.style.cssText = "background-color: #99CCFF;";
		last_highlighted_item = e.id; 	
	}else {
		last_highlight = document.getElementById(last_highlighted_item);
		last_highlight.style.cssText = "background-color: black;";
		last_highlighted_item = e.id;
		new_highlight = document.getElementById(last_highlighted_item);
		new_highlight.style.cssText = "background-color: #99CCFF;";
	}
	
	uploader = document.getElementById("uploader" + e.id);
	uploader_text = uploader.innerHTML;
	add_text = document.getElementById("add_on_select");
	add_text.innerHTML= uploader_text;
	
	if(link_base == "") {
		link = document.getElementById("buylink");
		link_base = link.href.substr(0,link.href.lastIndexOf("&"));		
	}
	
	link = document.getElementById("buylink");
	link.href = link_base + "&salesaller_id=" + sellerid; 
}

function onmouseoverhighlightandfont(e, color1) {
	e.style.cssText = "background-color:" +  color1 + ";";
	
	if (document.getElementById("helpme")) {
		help = document.getElementById("helpme").title;
	} else { help = ""; }
	if(e.id == "tr0" && help == "yes") {	
		tr = document.getElementById("tr0");
		for(j=0; j < tr.childNodes.length; j++) {
			tr.childNodes[j].style.cssText = "color:#FFFFFF";
		}
	}
}

function onmouseouthighlightandfont(e,color) {
//	e.style.cssText = "background-color:transparent;";
	e.style.cssText = "background-color:transparent;color:" + color;
	if (document.getElementById("helpme")) {
		help = document.getElementById("helpme").title;
	} else { help = ""; }
/*	if(e.id == "tr0" && help == "yes") {	
		tr = document.getElementById("tr0");
		for(j=0; j < tr.childNodes.length; j++)
			tr.childNodes[j].style.cssText = "color:" + color;
	}
*/

}

function onmouseoverhighlight(e, color) {
	e.style.cssText = "background-color:" +  color;
	
}

function onmouseouthighlight(e) {
	e.style.cssText = "background-color:transparent";
}

function GetXmlHttpObject() {		// make ajax object
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function update_graph() {
	whole_price_e = document.getElementById("0");
	whole_price = parseFloat(whole_price_e.innerHTML);
	user_share_e = document.getElementById("i1");
	user_share = parseFloat(user_share_e.value);
	zoimusic_share_e = document.getElementById("2");
	zoimusic_share =  parseFloat(zoimusic_share_e.innerHTML);
	artist_share_e = document.getElementById("3");
	artist_share = parseFloat(artist_share_e.innerHTML);
	vat_e = document.getElementById("4");
	vat = parseFloat(vat_e.innerHTML);
	total_e = document.getElementById("5");
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	url = "index.php?cmd=piechartservice&action=pie_chart&whole_price=" + whole_price+ "&user_share=" + user_share + "&zoimusic_share=" + 
		  zoimusic_share + "&artist_share=" + artist_share + "&vat=" + vat;
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);
	documentValue = xmlHttp.responseText;
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			img = document.getElementById("pie");
			img.src = "application/image/site/"  + documentValue;
		}
	} else {
		alert("Problem retrieving XML data")
	}
}

function change_price(exchangeRate) {
//alert(exchangeRate);
	whole_price_e = document.getElementById("0");
	whole_price = parseFloat(whole_price_e.innerHTML);
	
	user_share_e = document.getElementById("i1");

	user_share = parseFloat(user_share_e.value);
//alert(user_share);
//	user_share = user_share*exchangeRate;
//	user_share = user_share.toPrecision(2);
//alert(user_share);

	zoimusic_share_e = document.getElementById("2");
	zoimusic_share =  parseFloat(zoimusic_share_e.innerHTML);

	artist_share_e = document.getElementById("3");
	artist_share = parseFloat(artist_share_e.innerHTML);

	vat_e = document.getElementById("4");
	vat = parseFloat(vat_e.innerHTML);

	total_e = document.getElementById("5");

	zoimusic_share = (whole_price + user_share) * 0.05; 
	artist_share = (whole_price + user_share + zoimusic_share)*(100/(100-8)-1);
	vat = ((whole_price + user_share + zoimusic_share + artist_share) * 17.5)/100;
	total = zoimusic_share + artist_share + user_share + whole_price + vat;
	
//	total_e.innerHTML = total.toPrecision(3);
	total_e.innerHTML = total.toFixed(2);
	vat_e.innerHTML = vat.toFixed(2); 
	artist_share_e.innerHTML = artist_share.toFixed(2);
	zoimusic_share_e.innerHTML = zoimusic_share.toFixed(2);
	
	
	//setting hidden values
	user_share_e = document.getElementsByName("saleusershare");
//	user_share_e[0].value = user_share.toPrecision(2)
	user_share_e[0].value = user_share;
	
	zoimusic_share_e = document.getElementsByName("salezoishare");
	zoimusic_share_e[0].value = zoimusic_share.toFixed(2);
	
	whole_price_e = document.getElementsByName("salewholesaleprice");
	whole_price_e[0].value = whole_price.toFixed(2);
	
	artist_share_e = document.getElementsByName("saleartistshare");
	artist_share_e[0].value = artist_share.toFixed(2);
	
	vat_e = document.getElementsByName("salevat");
	vat_e[0].value = vat.toFixed(2);
	
}

function set_slider() {

	slider_element = document.getElementById("slider_id");

	if(slider_element) {
		exchangerate_e = document.getElementById("exchangerate");
		var exchangerate = parseFloat(exchangerate_e.value);

		user_share_e = document.getElementById("i1");
//alert(parseFloat(user_share_e.value));		
		user_share = parseFloat(user_share_e.value) * (200/exchangerate);
//alert(user_share);		
		slider_element.style.cssText = "left:" + user_share + "px;";
	}
}

var currRow = 0;
var currItem = 0;

function drawItem(elemid, totalrow)
{
	if ( currRow < totalrow)
	{
		if ( currItem < 20 )
		{
			var currCell = document.getElementById('div' + currRow);
			
			if(currItem == 0)
			{
				currCell.style.background = 'url(application/image/site/bar.gif)';
				currCell.style.backgroundRepeat = 'no-repeat';
				currCell.style.backgroundPosition = '0px -300px';
			}
			
			currCell.style.backgroundPosition = -300 + (currItem * 5);
			currCell.childNodes[0].innerText = (currItem * 5) + "%";
			currItem += 1;
			initGrid(totalrow);
		}
		else
		{
			var currCell = document.getElementById('div' + currRow);
			currCell.style.backgroundPosition = -300 + (currItem * 5);
			currCell.childNodes[0].innerText = (currItem * 5) + "%";
			currItem = 0;
			currRow += 1;
			initGrid(totalrow);
		}
	}
	else {
		album_id = document.getElementById("div_album_id");
		id = album_id.innerHTML;
		window.location = "index.php?cmd=setprice&album_id=" + id + "&salesaller_id=0";
	}
}

function initGrid(totalRow) {
	setTimeout("drawItem('progress_" + currRow + "', '" + totalRow + "')", 60);
}

function rip_cd(number_of_rows) {
	initGrid(number_of_rows);
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}
