window.onload = getPage();

function getPage() {
	var str = location.href;
	if(str.match(/#.+/gi)){
		tpg = str.split(/#/gi);
		tdom = tpg[0].split(/\//gi);
		if(tdom[2]=="jamfor.us"){
			tusr = tdom[3] + '/'; 	
		}else{
			tusr = "";	
		}
		
		location.replace('http://' + tdom[2] + '/' + tusr + tpg[1] + '/');

		return;
	}else if(str.match(/(404)/gi)){
		grabFile('page.php?page=404'); 
		zin();
		return;
	}
	//prepareForm();		
}

function show(show){
	location.href = "#"+show;
	grabFile('page.php?page='+show);		
}

function zin(){
	document.getElementById("header").style.zIndex= 10;
	document.getElementById("main").style.zIndex= 0;	
}

function onsub() {
    var data = "";
    for (var i=0; i<this.elements.length; i++) {
      data+= this.elements[i].name;
      data+= "=";
      data+= escape(this.elements[i].value); 
      data+= "&";
    }
    return !sendData(data);
}
function onsub2(frm) {
	if(frm.id =="del"){
		var r=confirm("Are you sure you want to cancel this show");;
		if (r==false){
			return false;
		}
	}
    var data = "";
    for (var i=0; i<frm.elements.length; i++) {
      data+= frm.elements[i].name;
      data+= "=";
      data+= escape(frm.elements[i].value); 
      data+= "&";
    }
	
    return !sendData(data);
}

function prepareForm() {
	
  if(!document.getElementById) {
    return;
  }
  if(!document.getElementById("showform")) {
	
    return;
  }  
  document.getElementById("showform").onsubmit = onsub();
  
}
function sendData(data) {
  var request = getHTTPObject();
  
  if (request) {
	  displayLoading(document.getElementById("main"));
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open("POST", "page.php?page=showinfo", true );
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(data);
	
    return true;

  } else {
    return false;
  }
}
function grabFile(file) {
	
	tmp = file.split(/=/gi);
	googAn(file);
	tmp[1].replace(/\//," ");
	document.title = document.domain + " :: " + tmp[1];
	//pgCh(tmp[1]);
  var request = getHTTPObject();
  if (request) {
    displayLoading(document.getElementById("main"));
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open("GET", file, true);
    request.send(null);
    return true;
  } else {
    return false;
  }
}

function parseResponse(request) {
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
      var details = document.getElementById("main");
      details.innerHTML = request.responseText;
    }
  }
}

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}

function displayLoading(element) {
  while (element.hasChildNodes()) {
    element.removeChild(element.lastChild);
  }
  var image = document.createElement("img");
  image.setAttribute("src","/loading.gif");
  image.setAttribute("alt","Loading...");
  element.appendChild(image);
}