// nav.js 

var delay;
var loadinganimno = 0;
var containerid;
var nl=new Image();
nl.src = "/images/navlogo1.gif"
var n2=new Image();
n2.src = "/images/navlogo2.gif"
var n3=new Image();
n3.src = "/images/navlogo3.gif"
var n4=new Image();
n4.src = "/images/navlogo4.gif"
var functiononload=null;
var xmlhttp;
var xmlhttppost;
var shipxmlhttp;
var shopxmlhttp;
var sbfade;
var fadedelay = 00;
var animtimer;
var maincontent;

function loadmain(theurl) {
  delay=0;
  openxmlhttp(theurl, mainloaded);
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
    if (shopopen) {
    shopopen=false;
    shopcontract();
  }
}

function mainfrommain(theurl) {
  delay=500;
  openxmlhttp(theurl, mainloaded);
  cleanupshoppulldowns();
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
    if (shopopen) {
    shopopen=false;
    shopcontract();
  }
}

function openxmlhttp(theurl, functionname) {
  xmlhttp=null;
  if (window.XMLHttpRequest) {// code for all new browsers
    xmlhttp=new XMLHttpRequest(); }
  else if (window.ActiveXObject) {// code for IE5 and IE6
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  if (xmlhttp!=null) {
    xmlhttp.onreadystatechange=functionname; //event handler function call;
    xmlhttp.open("GET",theurl,true);
    xmlhttp.send(null);
  }
  else {
    alert("Your browser does not support XMLHTTP.");
  }
}

function mainloaded() { //xmlhttp event handler 
  if (xmlhttp.readyState==4) {// 4 = "loaded"
    if (xmlhttp.status==200) {// 200 = OK
      setTimeout("mainloadeddo();", delay);
      loadinganimno =0;
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function mainloadeddo() {
  document.getElementById('maincontent').innerHTML=xmlhttp.responseText; 
  clickedfalse();
  eval(document.getElementById('mainfunction').innerHTML);
  navboxposition();
}

function loadinganim() {
  if (loadinganimno==0) {
    document.getElementById('navlogo').src ="/images/navlogo.gif";
    document.getElementById('navloading').innerHTML="<br/>";
    clearTimeout(animtimer);
  }
  else {
    if (loadinganimno==5) { document.getElementById('navlogo').src ="/images/navlogo.gif";}
    else { document.getElementById('navlogo').src ="/images/navlogo"+loadinganimno+".gif"; }
    loadinganimno += 1;
    if (loadinganimno > 5) {loadinganimno=1;}
    animtimer = setTimeout('loadinganim()', 100);
    document.getElementById('navloading').innerHTML="Loading...";
  }
}

function loader(theurl, container, time) {
  openxmlhttp(theurl, loaded);
  delay=time;
  containerid=container;
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
}

function loaded() {
  if (xmlhttp.readyState==4) {// 4 = "loaded"
    if (xmlhttp.status==200) {// 200 = OK
      setTimeout("document.getElementById('"+containerid+"').innerHTML=xmlhttp.responseText;", delay);
      setTimeout("clickedfalse();", 500);
      loadinganimno =0;
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function shopopenxmlhttp(theurl, functionname) {
  shopxmlhttp=null;
  if (window.XMLHttpRequest) {// code for all new browsers
    shopxmlhttp=new XMLHttpRequest(); }
  else if (window.ActiveXObject) {// code for IE5 and IE6
    shopxmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  if (xmlhttp!=null) {
    shopxmlhttp.onreadystatechange=functionname; //event handler function call;
    shopxmlhttp.open("GET",theurl,true);
    shopxmlhttp.send(null);
  }
  else {
    alert("Your browser does not support XMLHTTP.");
  }
}

function shoploader(theurl) {
  shopopenxmlhttp(theurl, shoploaded);
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
}

function shoploaded() {
  if (shopxmlhttp.readyState==4) {// 4 = "loaded"
    if (shopxmlhttp.status==200) {// 200 = OK
      setTimeout('shoploadeddo();', 500);
      loadinganimno =0;
    }
    else {
      //alert("shoploaded problem retrieving XML data");
    }
  }
}
 
function shoploadeddo() {
  document.getElementById('shopopencontents').innerHTML=shopxmlhttp.responseText;
  document.getElementById('shopopencontents').style.display = "";
  clickedfalse();
  eval(document.getElementById('shopfunction').innerHTML);
  currencyrefreshing = false;
  document.getElementById('knightblurb').innerHTML = knightblurb;
}

//from http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
function posttoxmlhttp(theurl, functionname, params) {
    xmlhttppost =null;
  if (window.XMLHttpRequest) {// code for all new browsers
    xmlhttppost = new XMLHttpRequest(); }
  else if (window.ActiveXObject) {// code for IE5 and IE6
    xmlhttppost = new ActiveXObject("Microsoft.XMLHTTP"); }
  if (xmlhttppost != null) {
    xmlhttppost.open("POST", theurl, true);
    xmlhttppost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttppost.setRequestHeader("Content-length", params.length);
    xmlhttppost.setRequestHeader("Connection", "close");
    xmlhttppost.onreadystatechange=functionname; //event handler function call;
    xmlhttppost.send(params);
  }
}

function shipopenxmlhttp(url, functionname) {
  if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  shipxmlhttp=null;
  if (window.XMLHttpRequest) {// code for all new browsers
    shipxmlhttp=new XMLHttpRequest(); }
  else if (window.ActiveXObject) {// code for IE5 and IE6
    shipxmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  if (shipxmlhttp!=null) {
    shipxmlhttp.onreadystatechange = functionname; //event handler function call;
    shipxmlhttp.open("GET",url,true);
    shipxmlhttp.send(null);
  }
  else {
    alert("Your browser does not support XMLHTTP.");
  }
}

function shipposttoxmlhttp(theurl, functionname, params) {
    shipxmlhttp =null;
  if (window.XMLHttpRequest) {// code for all new browsers
    shipxmlhttp = new XMLHttpRequest(); }
  else if (window.ActiveXObject) {// code for IE5 and IE6
    shipxmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  if (shipxmlhttp != null) {
    shipxmlhttp.open("POST", theurl, true);
    shipxmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    shipxmlhttp.setRequestHeader("Content-length", params.length);
    shipxmlhttp.setRequestHeader("Connection", "close");
    shipxmlhttp.onreadystatechange=functionname; //event handler function call;
    shipxmlhttp.send(params);
  }
}

function loadcheckout() {
  if (shipxmlhttp.readyState==4) {// 4 = "loaded"
    if (shipxmlhttp.status==200) {// 200 = OK
      loadinganimno =0;
      clearTimeout(animtimer);
      var a = location.href;
      a = a.replace(/http/, "https")+"secured";
      location.assign(a);
    }
    else {
      //alert("Problem retrieving XML data");
    }
  }
}

function regularmail() {
  setTimeout('clickedfalse()', 500);
  location.assign('mailto:info@darkcycles.com');
}

function contactemailsend() {
  var url = document.getElementById('contactemailform').action; //action is 'nav/contactemailpost
  var email_name = document.getElementById('email_name').value;
  var return_email = document.getElementById('return_email').value;
  var email_subject = document.getElementById('email_subject').value;
  var email_body = document.getElementById('email_body').value;
  if (email_name + return_email + email_subject + email_body != "") {
    var params = "";
    params += "email_name="+email_name;
    params += "&return_email="+return_email;
    params += "&email_subject="+email_subject;
    params += "&email_body="+email_body;
    params += "&authenticity_token="+document.getElementsByName('authenticity_token')[0].value;
    posttoxmlhttp(url, contactemailsent, params);
    if (loadinganimno==0) { loadinganimno =1;  loadinganim(); }
  }
  else { 
    setTimeout('clickedfalse()', 500);
  }
}

function contactemailsent() {
  if (xmlhttppost.readyState==4) {// 4 = "loaded"
    if (xmlhttppost.status==200) {// 200 = OK
      setTimeout('contactemailsentdo()', 500);
    }
  }
}

function contactemailsentdo() {
  loadinganimno = 0;
  clickedfalse();
  document.getElementById('contactemailformdiv').style.display = "none";
  document.getElementById('contactemailsent').style.display = "";
}

function contactemailsendanother() {
  clickedfalse();
  document.getElementById('email_name').value = "";
  document.getElementById('return_email').value = "";
  document.getElementById('email_subject').value = "";
  document.getElementById('email_body').value = "";
  document.getElementById('contactemailformdiv').style.display = "";
  document.getElementById('contactemailsent').style.display = "none";

}

