function countrySelected(list){
	var client=isis.ajax({'url':'/p/ajax/xaction/update_country/country/'+list.options[list.selectedIndex].value, 'async':false});
	location.reload(true);
}
function loadCountryList(list){
	var client=isis.ajax({'url':'/ajax/xaction/get_json_country_list', 'async':false});
	var clist=new Object();
	list.onclick=function(){};
	eval(client.client.responseText);
	var selected=list.options[list.selectedIndex].value;
	list.options.length = 0;
	var i=0;
	var selectedIndex=0;
	for (var country in clist) {
		if(country==selected)selectedIndex=i;
		list.options[i]=new Option(clist[country], country);
		i++;
	}
	list.selectedIndex=selectedIndex;
	var li=list;
	isis.addEventHandler(list, 'change', function(){
		countrySelected(li);
	});
}
function loadmenu(){
	var ulmenu=document.getElementById('ulmenu');
	var menuholder=document.getElementById('menuholder');
	if(ulmenu&&menuholder){
		isis.menu({"holder":"menuholder",
				 "config_type":"ul",
				 "config": ulmenu,
				 "menuclass":"menu",
				 "itemclass":"menuitem",
				 "itemhoverclass":"menuhoveritem",
				 "mainitemclass":"mainmenuitem"});
	}
}
function changeLang(newlan, url){
	window.location.href='/'+newlan+url;
}
function loadPage(url){
	var obj=document.getElementById('adminmaindiv');
	obj.appendChild(document.createTextNode('Loading...'));
	var t=setInterval(function(){obj.appendChild(document.createTextNode('.'));}, 1000);
	isis.ajax({
	'url': url,
	'loadedCallback': function(html, status){
		if(status==200){
			obj.innerHTML=html;
		}else{
			obj.appendChild(document.createTextNode('Error loading'));
		}
		clearInterval(t);
	}
	});
}
function populateList(pagename, ownerlist, list, tablename){
	owner=ownerlist.options[ownerlist.selectedIndex].value;
	url= pagename+'?p=javascript&jsaction=populatelist&list='+list+'&tablename='+tablename+'&owner='+owner;
	window.frames['dataframe'].window.location.replace(url);
}
function playmidi(songgsn){
	document.getElementById('midiframe').src='/index.php?p=playmidi&gsn='+songgsn;
}
var divHTML = "<div id='tooltip' class=\"isisToolTip\" style='display: none; position:absolute; z-index: 9; border: 0px'></div>";

document.write(divHTML);
var timer;
function showToolTop(element, e, t){
	var pp = document.getElementById('tooltip');
	selectHide();
	if(String(t) == "undefined"){
		t = element.innerHTML;
	}

	inner = "<table border='0' cellpadding='0' cellspacing='2' class='maintable'>" +
	"<tr>" +
	"<td valign='middle' class='headerrow'><b>" + t + "</b></td>" +
	"</tr>" +
	"</table>";

	pp.innerHTML = inner;
	pp.style.display = '';
	pp.style.top = e.clientY - 5 + document.body.scrollTop;
	pp.style.left = e.clientX + 20  + document.body.scrollLeft;
	element.onmouseout = hideToolTip;
	timer = window.setTimeout( "hidePopup()", 60000);
}
function hideToolTip()
{
	selectShow();
	var pp = document.getElementById('tooltip');
	pp.style.display = 'none';
	window.clearTimeout(timer);
}
function selectHide(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}
function selectShow(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}
function hideLevel( _levelId) {
	var thisLevel = document.getElementById( _levelId );
	thisLevel.style.display = "none";
}

function showLevel( _levelId) {
	var thisLevel = document.getElementById( _levelId );
	thisLevel.style.display = "block";
}
function toggleLevel( _levelId) {
	var thisLevel = document.getElementById( _levelId );
	if(thisLevel.style.display == "none") {
		thisLevel.style.display = "block";
	}else{
		thisLevel.style.display = "none";
	}
}

function writeToDiv(id, str){
	var obj;
	if(document.all){
		obj = document.all[id];
	}else{
		obj = document.getElementById(id);
	}
	obj.innerHTML = str;
}
function confirm_delete(newurl)
{
	input_box=confirm("Really Delete this?\nIt can not be undone");
	if (input_box==true)

	{
		// Output when OK is clicked
		window.location=newurl;
	}
}
function findMenuPos(gsn){
	//alert(num)
	if(bw.ns4){   //Netscape 4
		x = document.layers["layerMenu"+gsn].pageX;
		y = document.layers["layerMenu"+gsn].pageY;
	}else{ //other browsers
		x=0; y=0; var el,temp;
		el = bw.ie4?document.all["divMenu"+gsn]:document.getElementById("divMenu"+gsn);
		if(el.offsetParent){
			temp = el;
			while(temp.offsetParent){ //Looping parent elements to get the offset of them as well
				temp=temp.offsetParent;
				x+=temp.offsetLeft;
				y+=temp.offsetTop;
			}
		}
		x+=el.offsetLeft;
		y+=el.offsetTop;
	}
	//  alert(gsn + ' ' + x + ' ' + y)
	//Returning the x and y as an array
	return [x,y];
}
String.prototype.insert = function(chr, pos){
	return this.substring(0,pos) + chr + this.substring(pos);
}
String.prototype.insert.insertNthChar=function(chr,nth) {
	var output = '';
	for (var i=0; i<this.length; i++) {
		if (i>0 && i%4 == 0)
		output += chr;
		output += this.charAt(i);
	}
	return output;
}
String.prototype.formatAsMoney = function(){
	/*
	var i = parseFloat(this);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	*/
	/*
	amount=this;
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
	var nn = n.substr(n.length-3);
	a.unshift(nn);
	n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
	*/

	re=/[^0-9.]/g;
	re2=/[^0-9.]/g;
	var origenal_str=this.replace(re, "");
	var tmp_str=origenal_str.replace(re2, "");
	var bits=tmp_str.split(".");
	tmp_str=bits[0];
	if(tmp_str.length>=4){
		var i=tmp_str.length;
		var count=-1;
		while(i>0){
			if(count>=2){
				tmp_str=tmp_str.insert(",", i);
				count=0;
			}else{
				count++;
			}
			i--;
		}
	}
	if(tmp_str.length==0){
		tmp_str+=0;
	}
	if(origenal_str.lastIndexOf(".")>0){
		tmp_str=tmp_str.insert(".", tmp_str.length);
		if(bits[1].length==1){
			bits[1]+='0';
		}
		if(bits[1]){
			tmp_str=tmp_str+bits[1].substr(0,2);
		}
	}else{
		tmp_str+=".00";
	}
	if(origenal_str.lastIndexOf("-")!=-1){
		tmp_str=tmp_str.insert("-", 0);
	}
	return tmp_str;

}
function openPopupWin(url, width, height){
	window.open(url, 'none', 'scrollbars=yes,width='+width+',height='+height);
}
function FormatMoney(box) {
	re=/[^0-9.]/g;
	re2=/[^0-9.]/g;
	var origenal_str=box.value.replace(re, "");
	var tmp_str=origenal_str.replace(re2, "");
	var bits=tmp_str.split(".");
	tmp_str=bits[0];
	if(tmp_str.length>=4){
		var i=tmp_str.length;
		var count=-1;
		while(i>0){
			if(count>=2){
				tmp_str=tmp_str.insert(",", i);
				count=0;
			}else{
				count++;
			}
			i--;
		}
	}
	if(tmp_str.length==0){
		tmp_str+=0;
	}
	if(origenal_str.lastIndexOf(".")>0){
		tmp_str=tmp_str.insert(".", tmp_str.length);
		if(bits[1]){
			tmp_str=tmp_str+bits[1].substr(0,2);
		}
	}else{
		tmp_str+=".00";
	}
	if(origenal_str.lastIndexOf("-")!=-1){
		tmp_str=tmp_str.insert("-", 0);
	}
	box.value=tmp_str;
	return box.value;
}

function setupinstruments(){
	list=document.getElementById('instrument_type');
	val = list.options[list.selectedIndex].value;
	if (val==1005543){ // string quartet
		list=document.getElementById('instrument_type2');
		list.options[list.selectedIndex].value = 1005511;	// violin
		list.selectedIndex=27;	// alphabetic position
		list=document.getElementById('instrument_type3');	// violin
		list.options[list.selectedIndex].value = 1005511;
		list.selectedIndex=27;
		list=document.getElementById('instrument_type4');
		list.options[list.selectedIndex].value = 1005549;	// viola
		list.selectedIndex=26;
		list=document.getElementById('instrument_type5');
		list.options[list.selectedIndex].value = 1005540;	// cello
		list.selectedIndex=5;
		list=document.getElementById('instrument_type6');
		list.options[list.selectedIndex].value = 0;
		list.selectedIndex=0;
		list=document.getElementById('instrument_type7');
		list.options[list.selectedIndex].value = 0;
		list.selectedIndex=0;
		document.getElementById('headensemble').value='1';
	}
	else if (val==1005658){	// brass quintet
		list=document.getElementById('instrument_type2');
		list.options[list.selectedIndex].value = 1005542;	// trumpet
		list.selectedIndex=24;
		list=document.getElementById('instrument_type3');
		list.options[list.selectedIndex].value = 1005542;	// trumpet
		list.selectedIndex=24;
		list=document.getElementById('instrument_type4');
		list.options[list.selectedIndex].value = 1005551;	// french horn
		list.selectedIndex=11;
		list=document.getElementById('instrument_type5');
		list.options[list.selectedIndex].value = 1005550;	// trombone
		list.selectedIndex=23;
		list=document.getElementById('instrument_type6');
		list.options[list.selectedIndex].value = 1005660;	// tuba
		list.selectedIndex=25;
		list=document.getElementById('instrument_type7');
		list.options[list.selectedIndex].value = 0;
		list.selectedIndex=0;
		document.getElementById('headensemble').value='1';
	}
}
function settoday(){
	var a, now = new Date(), d, m, sub;
	for (type=0; type<document.reportactivity.defaulttoday.length; type++)
		if (document.reportactivity.defaulttoday[type].checked==true)
			break;
	if (type==0)
		return;
	if (type==2)
		sub=1;
	else sub=0;
	d=now.getDate()-sub;
	if (type>2)
		d=1;
	if (d<10)
		d='0'+d;
	m=now.getMonth()+1;
	if (m<10)
		m='0'+m;
	a=d+"/"+m+"/"+now.getFullYear();
	document.getElementById('report_activity_fromdate').value=a;
	if (type>2){
		if (type==3){
			d=now.getDate();
			if (d<10)
				d='0'+d;
		}
		else d=32 - new Date(now.getFullYear(), now.getMonth, 32).getDate();
		if (type==3)
			m=now.getMonth()+1;
		else m=now.getMonth();
		if (m<10)
			m='0'+m;
		a=d+"/"+m+"/"+now.getFullYear();
	}
	document.getElementById('report_activity_todate').value=a;
}
