function serialize_form(form) {
    var retVal = 'ajax=True';
   
    // Getting ALL elements inside of form element
    var els = form.getElementsByTagName('*');

    // Looping through all elements
    for(var idx = 0; idx < els.length; idx++) {
      var el = els[idx];
    
      retVal += '&' + el.name + '=' + encodeURIComponent(el.value);
    }
    
    return retVal;
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        // give up
        xmlHttp = null;
      }
    }
  }
  return xmlHttp;
}

function sendemail(id, form) {

  try { // do ajax maybe

    http = GetXmlHttpObject();
  
    // set up POST request
    var url = "/blog/wp-content/themes/jys/ajax_captcha.php";
    var params = "ajax=True&name="+form.name.value +"&email="+ 
    		form.email.value + "&message="+form.message.value +
    		"&recaptcha_challenge_field=" + Recaptcha.get_challenge() +
     		"&recaptcha_response_field=" + Recaptcha.get_response();

    Recaptcha.destroy();

    // change to loader
    var container = document.getElementById(id);
    container.id = "ajax_loading";
    container.innerHTML = '<img class = "spinner" src="/images/ajax-bar-loader.gif" />';
    
    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
 
    http.onreadystatechange = function() {  //Call a function when the state changes.
      try {
  	switch(http.readyState)
  	{
  	case 0:
  	  break;  	
  	case 1:
  	  break;
  	case 2:
  	  break;  	
  	case 3:
  	  break;
  	case 4:
  	  var container = document.getElementById("ajax_loading");
  	  if(http.status == 200) {
	    container.id = "ajax_success";
	  }
	  else {
	    container.id = "ajax_error";
	  }

  	  container.innerHTML = http.responseText;
  	  break;
	default:
	  break;
	}
      }
      catch(e) {
        form.submit();
      }
    }
    http.send(params);
    return false;
  }
  catch(e) { // no ajax
    return true;
  }
}

function insert_content(id, content) {
  var container = document.getElementById(id);
  container.innerHTML = content;
}

function activate_tab() {
  // activate the current tab onload
  try {
	var paths = location.pathname.split("/");
	var tab_name = paths[1];
	cur_tab = document.getElementById("tab_" + tab_name);
	if(cur_tab) {
	  cur_tab.className = cur_tab.id;
	  cur_tab.id = "active";
	}
  }
  catch(e) {
   // alert(e);
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function periodically_update(id) {

  try { // do ajax maybe

    http = GetXmlHttpObject();
  
    // set up POST request
    var url = "/ajax_slideshow";
    var params = "ajax=True";
    
    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
 
    http.onreadystatechange = function() {  //Call a function when the state changes.
      try {
  	switch(http.readyState)
  	{
  	case 0:
  	  break;  	
  	case 1:
  	  break;
  	case 2:
  	  break;  	
  	case 3:
  	  break;
  	case 4:
  	  var container = document.getElementById(id);
  	  container.innerHTML = http.responseText;
  	  break;
	default:
	  break;
	}
      }
      catch(e) {
        return true;
      }
    }
    http.send(params);
    return false;
  }
  catch(e) { // no ajax
    return true;
  }
}


addLoadEvent(activate_tab);
