var webpath = '/'
function getHTTPObject() {
    var xmlhttp;
    if (!xmlhttp && window.XMLHttpRequest) {
	try {
	    xmlhttp = new XMLHttpRequest();
	} catch (e) {
	    xmlhttp = false;
	}
    } else if(window.ActiveXObject) {

	try {
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
	    try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

	    } 
	    catch(e) {
		xmlhttp = false;
	    }
	}
    }
    return xmlhttp;
}

function AjaxRequestFunction(request, func) {
    var req = getHTTPObject();
    var r = 0;
    var response = '';
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
		response = req.responseText;
		func(response);
	    } 
    }
    req.open("GET", request, true);
    req.send(null);
}
function AjaxRequestFunctionPOST(request,str, func) {
    var req = getHTTPObject();
    var r = 0;
    var response = '';
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
		response = req.responseText;
		func(response);
	    } 
    }
    req.open("POST", request, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=windows-1251");
    req.send(str);
}

function AjaxRequest(request) {
    var req = getHTTPObject();
    var r = 0;
    var response = '';
    req.open("GET", request, false);
    req.send(null);
    if (req.readyState == 4) {
    		response = req.responseText;
    } 
    return response;
}

function updatecalendar(mon, year) {
    var calendar = document.getElementById("calendar");
    var calheader = document.getElementById("calheader");
    var req = getHTTPObject();
    req.onreadystatechange = function() {
	if (req.readyState == 4) {
	    var update = new Array();
	    var response = req.responseText;
	    if(response.indexOf('|' != -1)) {
    		update = response.split('|');
		calendar.innerHTML = update[1];
		calheader.innerHTML = update[0];
    	    }
	}
  }
  req.open("GET", webpath+'calendar/'+year+'/'+mon, true);
  req.send(null);
}
function getrequests() {
    var req = getHTTPObject();
    req.onreadystatechange = function() {
	if (req.readyState == 4) {
	}
    }
  req.open("GET", 'index.pl?act=requests&head=2', true);
  req.send(null);
}
function addrequest(artist, album, name) {
    var req = getHTTPObject();
    req.onreadystatechange = function() {
	if (req.readyState == 4) {
	    requests.innerHTML = req.responseText;
	    reqform.reset();
	}
    }
  req.open("GET", webpath+'?q=requests/add&artist='+escape(artist)+'&album='+escape(album)+'&name='+escape(name), true);
  req.send(null);
    
}
function init_drag(item) {
    document.getElementById(item).onmousedown=function(event) {
	this.dx = parseInt(event.clientX)-parseInt(this.style.left);
	this.dy = parseInt(event.clientY)-parseInt(this.style.top);
    	this.is_move=1;
	this.style.cursor='move';
	this.style.opacity=0.9;
    }
    document.getElementById(item).onmouseup=function() {
	this.is_move=0;
	this.style.cursor='';
	this.style.opacity=1;
    }
    document.getElementById(item).onmousemove=function (event) {
    if(this.is_move) {
	this.style.left = event.clientX-this.dx;
	this.style.top = event.clientY-this.dy;
    }
    }
}
function create_check(div_id, input_name, text) {
    var id1 = document.getElementById(div_id);
    id1.innerHTML = '<input type=hidden id='+input_name+' name='+input_name+' value=0><table><tr><td><img src=images/tick0.gif id='+input_name+'_img></td><td>'+text+'</td></tr></table>';
    id1.input_name = input_name 
    id1.onclick = function() {
	var val = 1-document.getElementById(this.input_name).value
	document.getElementById(this.input_name).value=val;
	if (val==1) {
	    document.getElementById(this.input_name+'_img').src = 'images/tick1.gif';
	} else {
	    document.getElementById(this.input_name+'_img').src = 'images/tick0.gif';
	}
    }
}
function saveartist() {
    AjaxRequestFunctionPOST(webpath+'artist/save','artist_id='+getval('artist_id')+'&artist='+getval('artist')+'&style='+getval('style')+'&biography='+escape(getval('biography')), function(response){
	eval(response);
    });
}
function delartist() {
    AjaxRequestFunction(webpath+'artist/delete/'+getval('artist_id'), function(response){
	eval(response);
    });
}
function checkboxonload() { 
    var inputs = document.getElementsByTagName("input");
    for (i=0; i < inputs.length; i++) {
	if (inputs[i].className&& (inputs[i].className=='checkbox')) {
	    var div0 = document.createElement('div');
	    div0.input_name = inputs[i].id
	    div0.innerHTML='<table><tr><td><img src='+webpath+'images/tick0.gif id='+div0.input_name+'_img></td><td>'+inputs[i].title+'</td></tr></table>';
	    var parent = inputs[i].parentNode;
	    div0.onclick = function() {
		var val = 1-document.getElementById(this.input_name).value
		document.getElementById(this.input_name).value=val;
		document.getElementById(this.input_name).checked=val;
//		alert(this.input_name);
	        document.getElementById(this.input_name+'_img').src = webpath+'images/tick'+val+'.gif';
    	    }
	    parent.insertBefore(div0,inputs[i]);
	    var newElem = document.createElement("input");
	    newElem.setAttribute('type', 'hidden');
	    newElem.setAttribute('value', 0);
	    parent.replaceChild(newElem, inputs[i]);
	    newElem.setAttribute('id', div0.input_name);
	    newElem.setAttribute('name', div0.input_name);
	};
    }
}
function setval(id, value) {
    document.getElementById(id).value = value;
}
function submitform(form) {
	document.getElementsByName(form)[0].submit();
}

var oldonload = window.onload;
if (typeof window.onload != 'function') {
    window.onload = function() {
      checkboxonload();
    };
  } else {
    window.onload = function() {
      oldonload();
      checkboxonload();
    };
}

