var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
if (!xmlhttp && document.createElement) {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
@end @*/

var History = new Array();
var Current = "suspects.php";
var EleHistory = new Array();
var CurrEle = "main";
var GoingBack = false;

function getPage(element_id, url_bit) { 
	var element = document.getElementById(element_id);
	var loader = document.getElementById("loadingzone");
	var perm = document.getElementById("permnotice");
	//If The temp notice area is not being set, then clean it
	if (element_id != 'tempnotice'){
		document.getElementById("tempnotice").innerHTML = '&nbsp';
	}
	//Set Loading signal
	loader.innerHTML = '<span class="warning">Loading ...</span>'; 
	//If we are not going back, then track history
	if ((element_id == 'main' || element_id == 'list')&& GoingBack == false){
    // If the url is not a save or edit page
    if(url_bit.indexOf("action=save") == -1 && url_bit.indexOf("action=dismiss") == -1
      && url_bit.indexOf("action=add") == -1 && url_bit.indexOf("action=new") == -1){
      History.push(Current);
      EleHistory.push(CurrEle);
      Current = url_bit;
      CurrEle = element_id;
		}
	}//IF
	
	if (!xmlhttp && document.createElement) {
		xmlhttp = new XMLHttpRequest(); 
	}
	
	xmlhttp.open("GET", url_bit, true); 
	xmlhttp.onreadystatechange = function() { 
		if (xmlhttp.readyState == 4){
		  if (xmlhttp.status == 200) { 
			 element.innerHTML = xmlhttp.responseText; 
			 loader.innerHTML = '&nbsp;';
		  }else{
		    alert("There was a problem retrieving the XML data:\n" +
                xmlhttp.statusText);
        loader.innerHTML = '&nbsp;';
      }
		}
	} 
	xmlhttp.send(null);
}

function updatePage(ele, url, pars, meth){
  if (typeof meth == "undefined") {
    meth = "get";
  }
 myOpts = {method: meth, parameters: pars, 
   onLoading: startLoading("loadingzone"),
   onComplete: doneLoading("loadingzone")}
 var myAjax = new Ajax.Updater(ele, url, myOpts);
 return false;
}

function limbsLoad(studio){
  if(studio == 1){
    new PeriodicalExecuter(function(pe) {
      alert('Press OK if you are here. OK?');
      updatePage("tempnotice", "studio/workers.php", "action=active");
    }, 1800);
  }
}


function startLoading(ele){
	d = $(ele);
	d.innerHTML = '<span class="warning">Loading ...</span>';
}

function doneLoading(ele){
	d = $(ele);
	d.innerHTML = '&nbsp;';
}

function reportError(request){
	alert('Sorry. There was an error.');
}

function viewReport(action){
	updatePage("dat_list", "studio/reports.php", "action="+action);
	$("sto_details").innerHTML = "";
	$("sto_list").innerHTML = ""; 
	return false;
}


function lastPage(){
  return History[(History.length -1)];
}

function goBack() {
	if (History.length > 0){
		GoingBack = true;
		Current = History.pop() ;
		CurrEle = EleHistory.pop() ;
		getPage(CurrEle, Current);
		GoingBack = false;
	}else{
		getPage("main", "suspects.php");
	}
	
}//goBack

function refresh(){
 	GoingBack = true;
 	getPage(CurrEle, Current);
 	GoingBack = false;
}
  
function confirm_click(Action){
  return confirm("Are you sure you want to "+Action+"?");
}

function blankOut(ele_id){
  var ele = document.getElementById(ele_id);
  ele.innerHTML = "&nbsp;";
}

function editBooking(bid, cid, year, month, day, studio){ 
	updatePage("calendar", "studio/calendar.php", "m="+month+"&y="+year);
	updatePage("sto_list", "studio/dates.php", "action=edit&SBID="+bid);
	updatePage("sto_details", "studio/customers.php", "action=edit&SCID="+cid);
	updatePage("dat_list", "studio/dates.php", "action=day&m="+month+"&y="+year+"&d="+day+"&studio="+studio); 
	return false;
}

function editReport(bid, cid){ 
	updatePage("sto_list", "studio/dates.php", "action=edit&SBID="+bid);
	updatePage("sto_details", "studio/customers.php", "action=edit&SCID="+cid);
	return false;
}

function editCallBack(cbid, cid){ 
	updatePage("sto_list", "studio/callbacks.php", "action=edit&ID="+cbid+"&SCID="+cid);
	updatePage("sto_details", "studio/customers.php", "action=edit&SCID="+cid);
	return false;
}

function deleteBooking(idname, sbid, mode, year, month, day, studio){
	if(confirm_click("delete this booking")){
		updatePage(idname+sbid, "studio/dates.php", "action=delete&mode="+mode+"&SBID="+sbid);
		updatePage("dat_list", "studio/dates.php", "action=day&m="+month+"&y="+year+"&d="+day+"&studio="+studio);
		return false;
	}
}

function hideDatCal(){
	Effect.BlindUp($('calendar'));
	Effect.BlindDown($('cal_control'));
	return false;
}

function showDatCal(override){
	if (!override && $('cal')){
		alert("This calendar can't be shown while the calendar below is showing");
	}else{
		Effect.BlindDown($('calendar'));
		Effect.BlindUp($('cal_control'));
	}
	return false;
}

function updateTC(){
	details = document.getElementsByClassName("TCqty");
	total = 0;
	for(i=0; i<details.length; i++)
	{
		total = Number(total) + Number(details[i].value);
	}
	$('tcTotal').value = total;
}

function updateTotals(){
	Qty = document.getElementsByClassName("tcValue");
	Cost = document.getElementsByClassName("price");
	ProTot = document.getElementsByClassName("prototal");
	Postage = document.getElementsByClassName("postage");
	total = 0;
	overallTotal = 0;
	for(i=0; i<Qty.length; i++)
	{
		total = Number(Qty[i].value) * Number(Cost[i].value);
		ProTot[i].value = total; 
		overallTotal = overallTotal + total;
		//alert(Number(Qty[i].value)+" * "+Number(Cost[i].value)+" = "+total);
	}
	overallTotal = overallTotal * 1.1 + Number(Postage[0].value) * 1.1;
	overallTotal = Math.round(overallTotal*100)/100;
	$('projectedTotal').value = overallTotal;
}

// Hide and show columns of a table
function hidcol (name, short)
{
	Header = $("a_"+name);
	Column = $("c_"+name);
	if (Header.innerHTML.length>1)
	{
		Header.innerHTML = short;
		Column.width = '1em';
	}else{
		Header.innerHTML = name;
		Column.width = 'auto';	
	}
	return false;
}

function check_invoice(type){
  invoiceForm = Form.getElements('invoicedetails');
  for(i=0; i<invoiceForm; i++)
  {
    if (invoiceForm[i].id == 'groupNumber' && isNaN(invoiceForm[i].value)){
      alert('You need to fill in the Number of Groups field');
      return false;
    }else if(invoiceForm[i].id == 'tcValue' && isNaN(invoiceForm[i].value)){
      alert('You need to put a number in for each for your Packs');
      return false;
    }
    
  }
}

function testing(targetName) {
  var target = $(targetName),
      ifrm = document.createElement("IFRAME"); 
  target.innerHTML = "";
  ifrm.setAttribute("src", "/limbs/r/admin/invoice"); 
  ifrm.style.top = 0+"px"; 
  ifrm.style.left = 0+"px"; 
  ifrm.style.width = 98+"%";
  ifrm.style.height = 100+"%";
  ifrm.scrolling = 'no';
  target.appendChild(ifrm);
}