function changeimage(imagenum,newfilename)
{
  document.images[imagenum].src=newfilename;
}

function createRequestObject() 
{
  var tmpXmlHttpObject;
    
//depending on what the browser supports, use the right way to create the XMLHttpRequest object
  if (window.XMLHttpRequest) { 
// Mozilla, Safari would use this method ...
    tmpXmlHttpObject = new XMLHttpRequest();
	
  } else if (window.ActiveXObject) { 
// IE would use this method ...
      tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
  }
    
  return tmpXmlHttpObject;
}

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function makeGetRequest(kar_section,kar_func,kar_funcp1,kar_funcp2,kar_funcp3,kar_funcp4,kar_funcp5,kar_funcp6) 
{
	if (kar_func == "search") {
		if (tagstatus == 1) {
			animatedcollapse.toggle(opentag);
		}
	}


//make a connection to the server ... specifying that you intend to make a GET request 
//to the server. Specifiy the page name and the URL parameters to send
  http.open('get', '/kar_ajax.php?kar_func=' + kar_func + '&kar_funcp1=' + kar_funcp1 + '&kar_funcp2=' + kar_funcp2 + '&kar_funcp3=' + kar_funcp3 + '&kar_funcp4=' + kar_funcp4 + '&kar_funcp5=' + kar_funcp5 + '&kar_funcp6=' + kar_funcp6);

//assign a handler for the response
	http.divtag = kar_section;
  http.onreadystatechange = processResponse;

	
//actually send the request to the server
  http.send(null);

// Re-initialise new divtags after a search!  -- this is testcode!
//setup_divtags()

//	if (kar_func == "search") {
		animatedcollapse.init()
//	}


}

function testalert()
{
	alert('Test Alert!');
}

function processResponse() {
//check if the response has been received from the server
	if(http.readyState == 4){
//read and assign the response from the server
		var response = http.responseText;
//do additional parsing of the response, if needed
//in this case simply assign the response to the contents of the <div> on the page. 
    document.getElementById(http.divtag).innerHTML = response;

		throwMessage(response.substr(14,3));
//		throwMessage(response.charAt(14));
//If the server returned an error message like a 404 error, that message would be shown within the div tag!!. 
//So it may be worth doing some basic error before setting the contents of the <div>
    }
}

function call_ajax_settitle(thisform,discid)
{
	var title = eval('document.'+thisform+'.title.value');
	makeGetRequest('main','set_title',discid,title,'','','','');
}

function call_ajax_settitle_cart(thisform,discid)
{
	var title = eval('document.'+thisform+'.title.value');
	makeGetRequest('dispcart','set_title_cart',discid,title,'','','','');
}

function call_ajax_purchase()
{
	var title = eval('document.'+thisform+'.title.value');
//		alert(title+" "+thisform);
	makeGetRequest('main','purchase','','','','','','');
}

function throwMessage(result) 
{
	if (result.charAt(0) == "a") {
		var result = result.charAt(1);

		var message = "";
		if (result == 1) 
			var message="The track you selected is already in your compilation";
		if (result == 2) 
			var message="There is not enough track time available for the track you selected";
		if (result == 3) 
			var message="You can only have a maximum of 20 tracks per compilation";
		if (result == 4) 
			var message="You cannot have an empty compilation - please add tracks";
		if (result == 5) {
			var message="You are now in edit compilation mode";
			alert(message);
			window.location.reload();
			return;
		}
		if (result == 6) {
			var message="Your changes have been saved";
			alert(message);
			window.location.reload();
			return;
		}
		if (result == 7) {
			var message="Your compilation has been saved";
			alert(message);
			window.location.reload();
			return;
		}

		if (result == 9) {
			window.location.reload();
			return;
		}
	
		if ( message != "" ) {
			alert(message);
		}
	}

	if (result.charAt(0) == "b") {
		var result = result.substr(1,2);

		for (i=0;i<result;i++)
		{
			var tag = "settitle"+i;
		  dropdowncontent.init(tag,"left-bottom",10,"click")
		}
	}

	if (result.charAt(0) == "c") {
	  dropdowncontent.init("settitlepdf","right-top",10,"click")
	}

	if (result.charAt(0) == "d") {
		alert("Please allow 3 seconds before each search");
	}

}


function get_details(divtag,mfcode,type)
{
	var msg = divtag +" "+ mfcode + " "+ type;

	opentag = divtag;
	if (opentag == divtag) {
		if (tagstatus == 1) 
			tagstatus=0;	
		else
			tagstatus=1;	
	}

//	alert("after " + opentag + " " + tagstatus);

	//alert(msg);
	makeGetRequest(divtag,type,mfcode,'','','','','');

	animatedcollapse.toggle(divtag);
}


function setup_anim_divtags()
{
	var i;

	for (i=0;i<35;i++)
	{
		var divtag = "rowdiv" + i;
		animatedcollapse.addDiv(divtag, 'fade=1,height=320px,speed=1000')
//		animatedcollapse.addDiv(divtag, 'fade=1,height=320px,speed=1000,group=tracks')
	}

//animatedcollapse.addDiv('cat', 'fade=0,speed=400,group=pets')
//animatedcollapse.addDiv('dog', 'fade=0,speed=400,group=pets,persist=1,hide=1')
//animatedcollapse.addDiv('rabbit', 'fade=0,speed=400,group=pets,hide=1')

	animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
	//$: Access to jQuery
	//divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
	//state: "block" or "none", depending on state
	}

}

var opentag = "";
var tagstatus = 0;

setup_anim_divtags()
animatedcollapse.init()

