var xmlHttp = createXmlHttpRequestObject(); 
var lastname = "456";
var lastFacility = '';
var name = '';

function createXmlHttpRequestObject(){	
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try{
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}


// -------------------------------- Loading presenter showreels --------------------------------------

function ajaxvideoprocess(filename){
	
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    filename = encodeURIComponent(filename);
	var url = "ajax/videoprocessor.php?videofile=" + filename;
	//alert(url);
	xmlHttp.open("GET", url, true);  
	xmlHttp.onreadystatechange = handleServerResponseVideo;
    xmlHttp.send(null);
	
	try{document.getElementById("smallspeakervideo").innerHTML = '';}catch(err){}
	
  }else{ 
    setTimeout('ajaxvideoprocess()', 1000);
  }
}



// executed automatically when a message is received from the server
function handleServerResponseVideo() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      
	  xmlResponse = xmlHttp.responseXML;
      
	  xmlDocumentElement = xmlResponse.documentElement;

	  var myhtmlcode = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
	  //alert(myhtmlcode);

	  var video = xmlDocumentElement.getElementsByTagName("video").item(0).firstChild.data;
	  
      try{document.getElementById("videoplayer").innerHTML = myhtmlcode;}catch(err){}
	
	// Hide gallery for video files
	if(video == 1){
		try{document.getElementById("talentgallery").style.display = 'none';}catch(err){}
	}
	
	} else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}



function loadYouTube(id, reel_id){
	
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){

	var url = "ajax/youtubeprocessor.php?id=" + id + '&reel_id=' + reel_id;
	//alert(url);
	xmlHttp.open("GET", url, true);  
	xmlHttp.onreadystatechange = handleServerResponseLoadYoutube;
    xmlHttp.send(null);
	
	try{document.getElementById("smallspeakervideo").innerHTML = '';}catch(err){}
	
  }else{ 
    setTimeout('loadYouTube()', 1000);
  }
}



// executed automatically when a message is received from the server
function handleServerResponseLoadYoutube() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      
	  xmlResponse = xmlHttp.responseXML;
      
	  xmlDocumentElement = xmlResponse.documentElement;

	  var myhtmlcode = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
		
		
		
	  var video = xmlDocumentElement.getElementsByTagName("video").item(0).firstChild.data;
	  
      try{document.getElementById("videoplayer").innerHTML = myhtmlcode;}catch(err){}
	
	// Hide gallery for video files
	if(video == 1){
		try{document.getElementById("talentgallery").style.display = 'none';}catch(err){}
	}
	
	} else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}




function resetSearchResults(){
	document.getElementById('result_1').style.display = 'none';
	document.getElementById('result_2').style.display = 'none';
	document.getElementById('result_3').style.display = 'none';
	document.getElementById('result_4').style.display = 'none';
	document.getElementById('result_5').style.display = 'none';
	document.getElementById('result_6').style.display = 'none';
	document.getElementById('result_7').style.display = 'none';
	document.getElementById('result_8').style.display = 'none';
	document.getElementById('result_9').style.display = 'none';
	try{document.getElementById('pagelinkscontainer').innerHTML = '';}catch(err){}
}

//-----------------------------



// Voice search functions
function ajaxvoicesearch(gender, age, style, $querystring, page){
	
	contentPanelOn();
	resetMainPanel();
	resetSearchResults();
	
	try{document.getElementById('searchresultscontainer').style.display = 'block';}catch(err){}
	try{document.getElementById('audioplayer').style.display = 'none';}catch(err){}
	try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
	
	if(!page){$page=1;}
	
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
	
	var url = "ajax/voiceengine.php?action=voicesearch&name="+name+"&gender="+gender+"&age="+age+"&style="+style+'&'+$querystring+'&page='+page;
	
	//alert(url);
	//document.write(url)
	
	xmlHttp.open("GET", url, true);   
	xmlHttp.onreadystatechange = handlevoicesearchresponse;
    xmlHttp.send(null);
  }else{ 
    setTimeout('ajaxvoicesearch()', 1000);
  }
}



function handlevoicesearchresponse() {
	
	$duration = 0.4;
	
  if (xmlHttp.readyState == 4) {
	  
    if (xmlHttp.status == 200) {
      
	  xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
	  
	  // total items matching
	  var totalitems = xmlDocumentElement.getElementsByTagName("totalitems").item(0).firstChild.data;
	  
	  // pages
	  var totalpages = xmlDocumentElement.getElementsByTagName("totalpages").item(0).firstChild.data;
	  
	  // page
	  var pagenum = xmlDocumentElement.getElementsByTagName("pagenum").item(0).firstChild.data;
	  
	  // Items this page
	  var itemcount = xmlDocumentElement.getElementsByTagName("itemcount").item(0).firstChild.data;
	  
	  // Items this page
	 try{var pagelinks = xmlDocumentElement.getElementsByTagName("pagelinks").item(0).firstChild.data;}catch(err){}
	 if(!pagelinks){pagelinks='';}
      //alert('debug mode on');
	  
	  
	  try{document.getElementById('contentinner').innerHTML = '';}catch(err){}
	  
	  if(totalitems>=1){
	  	for(i=0;i<itemcount;i++){
			  // Parameters for voice
			  var voice_id = xmlDocumentElement.getElementsByTagName("item_"+i+"_id").item(0).firstChild.data;
			  var linky = xmlDocumentElement.getElementsByTagName("item_"+i+"_link").item(0).firstChild.data;
			  //alert(linky);
			  var name = xmlDocumentElement.getElementsByTagName("item_"+i+"_name").item(0).firstChild.data;
			  var bullets = xmlDocumentElement.getElementsByTagName("item_"+i+"_bullets").item(0).firstChild.data;
			  var photo = 'http://www.excellenttalent.com/'+xmlDocumentElement.getElementsByTagName("item_"+i+"_photo").item(0).firstChild.data;
			  html += '<p><a href="#" onclick="alert('+voice_id+')">'+name+'</a> ('+photo+')</p>';
			  var c = i + 1;
			  var div = ("result_"+c);
			  var resimg = ("resimg_"+c);
			  
			  var html = '<a href="'+linky+'">';
			  
			  // Photo
			  if(photo.indexOf('.swf') > 0){ // Flash photos
			  		html += '<div class="resultphoto">';
				  	html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="60" height="72" onclick="document.location.href=\'+'+linky+'\'">';
					html += '<param name="movie" value="'+photo+'" />';
					html += '<param name="quality" value="high" />';
					html += '<embed src="'+photo+'" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="460" height="72" onclick="document.location.href=\'+'+linky+'\'"></embed>';
					html += '</object>';
					html += '</div>';
			  }else{ // JPEG photos
			  	html += '<img src="'+photo+'" alt="" id="resimg_1" width="60" height="72" onclick="document.location.href=\''+linky+'\'"/></a>';
			 }
			  
			  // Name
			html += '<h2><a href="'+linky+'">'+name+'</a></h2>';
			
			// Bullets
			if(bullets != 'null'){
				html += '<ul>';
				html += bullets;
				html += '</ul>';
			}
			  $duration = $duration+0.2
			  $(div).appear({ duration: $duration, from: 0, to: 1 });
			  try{document.getElementById(div).innerHTML = html;}catch(err){}
	  	}
	  	if(totalitems){
			try{document.getElementById('pagelinkscontainer').innerHTML = pagelinks;}catch(err){}
		}
		
	  }else{
			 try{document.getElementById('contentinner').innerHTML = '<h1>&nbsp;</h1><p align="center">Your search returned no matching voices. Please try a more general search.</p>';}catch(err){}
	  }
	  
	  
    } else {
      	alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
	
  }
}




//------------- Talent ----------------



function ajaxTalentSearch($cat, $subcat, $page, $name){
	
	contentPanelOn();
	
	document.getElementById('searchresultscontainer').style.display = 'block';
	try{document.getElementById('audioplayer').style.display = 'none';}catch(err){}
	try{document.getElementById('homepageanim').style.display = 'none';}catch(err){}
	
	resetSearchResults();
	
	showLoader()

	if(!$page){$page=1;}
	
	if($subcat){
		setCookie('backlinkaction',$subcat,1);
	}
	
	document.getElementById('result_1').style.display = 'none';
	document.getElementById('result_2').style.display = 'none';
	document.getElementById('result_3').style.display = 'none';
	document.getElementById('result_4').style.display = 'none';
	document.getElementById('result_5').style.display = 'none';
	document.getElementById('result_6').style.display = 'none';
	document.getElementById('result_7').style.display = 'none';
	document.getElementById('result_8').style.display = 'none';
	document.getElementById('result_9').style.display = 'none';
	document.getElementById('pagelinkscontainer').innerHTML = '';
	
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
	
	var $url = "ajax/talentengine.php?action=talentsearch&cat="+$cat+"&subcat="+$subcat+'&page='+$page;
	if($name){
		$url += '&name='+$name;
	}
	
	//alert($url);
	xmlHttp.open("GET", $url, true);   
	
	xmlHttp.onreadystatechange = handletalentsearchresponse;
    xmlHttp.send(null);
  }else{ 
    setTimeout('ajaxTalentSearch()', 1000);
  }
}



function handletalentsearchresponse() {
	
	$duration = 0.4;

  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
	  
	  // total items matching
	  var totalitems = xmlDocumentElement.getElementsByTagName("totalitems").item(0).firstChild.data;
	  
	  // pages
	  var totalpages = xmlDocumentElement.getElementsByTagName("totalpages").item(0).firstChild.data;
	  
	  // page
	  var pagenum = xmlDocumentElement.getElementsByTagName("pagenum").item(0).firstChild.data;
	  
	  // Items this page
	  var itemcount = xmlDocumentElement.getElementsByTagName("itemcount").item(0).firstChild.data;
	  
	  // Items this page
	  var pagelinks = xmlDocumentElement.getElementsByTagName("pagelinks").item(0).firstChild.data;
      
	  
	  document.getElementById('contentinner').innerHTML = '';
	  
	  if(totalitems>=1){
	  	for(i=0;i<itemcount;i++){
			  // Parameters
			  var voice_id = xmlDocumentElement.getElementsByTagName("item_"+i+"_id").item(0).firstChild.data;
			  var linky = xmlDocumentElement.getElementsByTagName("item_"+i+"_link").item(0).firstChild.data;
			  
			  if(totalitems == 1){
				document.location.href = 'http://www.excellentvoice.co.uk/'+linky;
	  		  }
			  
			  var name = xmlDocumentElement.getElementsByTagName("item_"+i+"_name").item(0).firstChild.data;
			  var bullets = xmlDocumentElement.getElementsByTagName("item_"+i+"_bullets").item(0).firstChild.data;
			  var photo = 'http://www.excellentvoice.co.uk/'+xmlDocumentElement.getElementsByTagName("item_"+i+"_photo").item(0).firstChild.data;
			  html += '<p><a href="#" onclick="alert('+voice_id+')">'+name+'</a> ('+photo+')</p>';
			  var c = i + 1;
			  var div = ("result_"+c);
			  var resimg = ("resimg_"+c);
			  
			  var html = '<a href="'+linky+'">';
			  // Photo
			  if(photo.indexOf('.swf')){ // Flash photos
			  		html += '<div class="resultphoto">';
				  	html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="60" height="72">';
					html += '<param name="movie" value="'+photo+'" />';
					html += '<param name="quality" value="high" />';
					html += '<embed src="'+photo+'" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="460" height="72"></embed>';
					html += '</object>';
					html += '</div>';
			  }else{ // JPEG photos
			  	html += '<img src="'+photo+'" alt="" id="resimg_1" width="60" height="72"/></a>';
			  }
			
			// Name
			html += '<h2><a href="'+linky+'">'+name+'</a></h2>';
			
			// Bullets
			if(bullets != 'null'){
				html += '<p>'+bullets+'</p>';
			}
			
			$duration = $duration+0.2
			$(div).appear({ duration: $duration, from: 0, to: 1 });
			document.getElementById(div).innerHTML = html;
			
	  	}
	  	if(totalitems){
			document.getElementById('pagelinkscontainer').innerHTML = pagelinks;
		}
		
	  }else{
		  	document.getElementById('pagelinkscontainer').innerHTML = '&nbsp;';
			document.getElementById('contentinner').innerHTML = '<h1>&nbsp;</h1><p align="center">Your search returned no matching artists. Please try a more general search.</p>'; 
	  }
	  
	  
    } else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
	
  }
}






//-----------------------------




// p3 update
function ajaxupdatep3(){
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
	xmlHttp.open("GET", "ajax/videoprocessor.php?action=updatep3", true);  
	xmlHttp.onreadystatechange = handleServerResponseUpdatep3;
    xmlHttp.send(null);
  }
    setTimeout('ajaxupdatep3()', 12000);
}

function handleServerResponseUpdatep3() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
	  myhtmlcode = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
      document.getElementById("p3").innerHTML = myhtmlcode;
    } else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

// -----------Booking systsem


function checkDuration(){
	if(document.forms['bookingform'].duration.value == 'other'){
		alert('We’re unable to accept online bookings for more than 4 hours’ duration. \nIf you’d like to make a booking for longer than 4 hours, please call us on +44 (0) 20 7520 5650, or email info@excellenttalent.com');
	}
}

function calculateEndTime(){
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
	 var start_time = document.forms['bookingform'].start_time.value;
	 var duration = document.forms['bookingform'].duration.value;
	 var url = "ajax/videoprocessor.php?action=calculateEndTime&start_time="+start_time+'&duration='+duration;
	 if(duration == 'other'){
		 document.getElementById("end_time").value = '-';
	 }else{
		xmlHttp.open("GET", url, true);  
		xmlHttp.onreadystatechange = updateEndTime;
    	xmlHttp.send(null);
	 }
  }
}

function updateEndTime() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
	  myhtmlcode = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
      document.getElementById("end_time").value = myhtmlcode;
	 //alert(myhtmlcode);
    } else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}


function findFacility(){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		var facility = document.getElementById("facility").value;
		if(facility != lastFacility){
			var url = "ajax/videoprocessor.php?action=findFacility&terms="+facility;
			xmlHttp.open("GET", url, true);  
			xmlHttp.onreadystatechange = updateFacility;
    		xmlHttp.send(null);
			lastFacility = facility;
		}
	}
	setTimeout('findFacility()', 500);
}

function updateFacility(){
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
	  var code = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
	  var results = xmlDocumentElement.getElementsByTagName("results").item(0).firstChild.data;
	  code = code + '<br /><span class="smallprint">Click on a venue to select</span>';
	  document.getElementById("faclist").innerHTML = code;
	  
	  if(results >= 1){
		  document.getElementById("faclist").style.display= 'block';  
	  }else{
		  document.getElementById("faclist").style.display = 'none';
	  }
	 
	 //alert(code);
    } else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }	
}

function setFacility(facility_id){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		var url = "ajax/videoprocessor.php?action=setFacility&facility_id=" + facility_id;
		//alert(url);
		xmlHttp.open("GET", url, true);  
		xmlHttp.onreadystatechange = setFacilityField;
    	xmlHttp.send(null);
	}
}

function setFacilityField(){
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      xmlResponse = xmlHttp.responseXML;
      xmlDocumentElement = xmlResponse.documentElement;
		var code = xmlDocumentElement.getElementsByTagName("htmlcode").item(0).firstChild.data;
		var orgid = xmlDocumentElement.getElementsByTagName("OrgID").item(0).firstChild.data;
	  document.getElementById("facility").value = code;
	  document.getElementById("OrgID").value = orgid;
	 
	 //alert(code);
    } else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }	
}