var xmlhttp;
var global_year;
var global_month;
var global_date;
function getXMLHttpRequest() {  
  if (window.XMLHttpRequest) {
    xmlhttp1 = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    xmlhttp1 = new ActiveXObject("MSXML2.XMLHTTP");
  }
}

function trim(str) {
  return str.replace(/^\s+/g, '');
}

// Loading calendar
function onloadcalendar(year,month) {
  global_year=year;
  global_month=month;
  getXMLHttpRequest();
  var checkURL = "main.php";
  var postURL = "year="+year+"&month="+month;
  xmlhttp1.open("POST",checkURL,true);
  xmlhttp1.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp1.send(postURL);
  xmlhttp1.onreadystatechange= displayvalue;
}

function createdairy(year,month,dateval) {
  getXMLHttpRequest();
  var checkURL = "createdairy.php";
  var postURL = "year="+year+"&month="+month+"&dateval="+dateval;  
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showdairy;
    
}

function createdairy_actionajax(dateofdiary,notes,remainder_status,reminder_preference,remainder_reason,year,month) {
  global_year=year;
  global_month=month;
  getXMLHttpRequest();
  var checkURL = "createdairy_action.php";
  var postURL = "dateofdiary="+dateofdiary+"&notes="+notes+"&remainder_status="+remainder_status+"&reminder_preference="+reminder_preference+"&remainder_reason="+remainder_reason;  
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showdairy;
}

function editdairy_actionajax(did,notes,remainder_status,reminder_preference,remainder_reason,year,month) {
  global_year=year;
  global_month=month;
  getXMLHttpRequest();
  var checkURL = "editdairy_action.php";
  var postURL = "did="+did+"&notes="+notes+"&remainder_status="+remainder_status+"&reminder_preference="+reminder_preference+"&remainder_reason="+remainder_reason;  
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showdairy;
}

function deletedairy_actionajax(did,year,month) {
  global_year=year;
  global_month=month;
  getXMLHttpRequest();
  var checkURL = "deletedairy_action.php";
  var postURL = "did="+did;  
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showdairy;
}

function changepassword() {
  getXMLHttpRequest();
  var checkURL = "changepassword.php";
  var postURL = "";
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showchangepassword;
}

function changepasswordaction_ajax(oldpwd,newpwd) {
  getXMLHttpRequest();
  var checkURL = "changepassword_action.php";
  var postURL = "old_password="+oldpwd+"&new_password="+newpwd;
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showchangepassword;
}

function registration_action(user_name,dob_date,dob_month,dob_year,sex,username,user_password,email,contact_no,address,hobbie1,hobbie2,hobbie3) {
  getXMLHttpRequest();
  var checkURL = "registration_action.php";
  var postURL = "user_name="+user_name+"&dob_date="+dob_date+"&dob_month="+dob_month+"&dob_year="+dob_year+"&sex="+sex+"&username="+username+"&user_password="+user_password+"&email="+email+"&contact_no="+contact_no+"&address="+address+"&hobbie1="+hobbie1+"&hobbie2="+hobbie2+"&hobbie3="+hobbie3;
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showregistration;
}
function checkavailableusername(username,email){
  getXMLHttpRequest();
  var checkURL = "checkavailableusername.php";
  var postURL = "username="+username+"&email="+email;
  xmlhttp.open("POST",checkURL,true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  xmlhttp.send(postURL);
  xmlhttp.onreadystatechange= showavailstatus;
}

function showregistration() {
  if (xmlhttp.readyState==4) {
    document.getElementById('registerdiv').innerHTML = xmlhttp.responseText;
  } else {    
    document.getElementById('registerdiv').innerHTML = "";
  }
}

function showchangepassword() {
  if (xmlhttp.readyState==4) {
    document.getElementById('showchangepwd').innerHTML = xmlhttp.responseText;
    document.getElementById('showchangepwd').style.display = "block";
  } else {    
    document.getElementById('showchangepwd').innerHTML = "";
  }
}

function showavailstatus() {
  if (xmlhttp.readyState==4) {
    //return xmlhttp.responseText;
  } else {    
  }
}

function showdairy() {
  if (xmlhttp.readyState==4) {
    document.getElementById('createdairy').innerHTML = xmlhttp.responseText;  
  onloadcalendar(global_year,global_month);
  } else {    
    document.getElementById('createdairy').innerHTML = "";
  }
}

function displayvalue() {
if(xmlhttp1.readyState==4) {    
    document.getElementById('cal1Container').innerHTML = xmlhttp1.responseText;  
  } else {    
    document.getElementById('cal1Container').innerHTML = "<b>Loading.....</b>";
  }
    
}

function showevents(events_id)
{

  window.location='http://www.my-ng2.co.uk/events/events.php?events_id=' + events_id ;
}