var SUCCESS = "#8CD137";
var ERROR = "#FF3891";

String.prototype.trim = function() {
	return this.replace( /^\s+|\s+$/g, "" );
}
String.prototype.ltrim = function() {
	return this.replace( /^\s+/, "" );
}
String.prototype.rtrim = function() {
	return this.replace( /\s+$/, "" );
}

function createAjaxObject() {
  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) {
        alert("Your browser does not support AJAX!");
      }
    }
  }
  
  return xmlHttp;
}

function refreshCalendar( date ) {
  var cell = document.getElementById( 'calendar' ).innerHTML = '<center><img src="/portal/img/ehodowca_loading.gif" title="Trwa wczytywanie..." alt="Trwa wczytywanie..." /></center>';
  
  var xmlHttp = createAjaxObject();
  if( xmlHttp == null )
    return;
  
  // receive response
  // xmlHttp.readyState = response status
  // 0	The request is not initialized
  // 1	The request has been set up
  // 2	The request has been sent
  // 3	The request is in process
  // 4	The request is complete
  // xmlHttp.responseText = code send from server
  xmlHttp.onreadystatechange = function() {
    switch( xmlHttp.readyState ) {
      case 0:
        break;
      case 1:
        break;
      case 2:
        break;
      case 3:
        break;
      case 4:
        // if everything was OK
        if( xmlHttp.status == 200 ) {
          document.getElementById( 'calendar' ).innerHTML = xmlHttp.responseText;
        }
        break;
    }
  }
  
  var url = "calendar.php?mydate="+ date;
  xmlHttp.open( "GET", url, true );
  xmlHttp.send( null );
}

function checkAccountName( textBox ) {
  if( textBox == null )
    return;
  
  if( textBox.value.trim() == "" ) {
    textBox.style.background = ERROR;
    return;
  }
  
  var xmlHttp = createAjaxObject();
  if( xmlHttp == null )
    return;
  
  // receive response
  // xmlHttp.readyState = response status
  // 0	The request is not initialized
  // 1	The request has been set up
  // 2	The request has been sent
  // 3	The request is in process
  // 4	The request is complete
  // xmlHttp.responseText = code send from server
  xmlHttp.onreadystatechange = function() {
    switch( xmlHttp.readyState ) {
      case 0:
        break;
      case 1:
        break;
      case 2:
        break;
      case 3:
        break;
      case 4:
        // if everything was OK
        if( xmlHttp.status == 200 ) {
          if( xmlHttp.responseText == 1 ) {
            textBox.style.background = SUCCESS;
          } else {
            textBox.style.background = ERROR;
          }
        }
        break;
    }
  }
  
  var url = "accountCheckName.php";
  url += "?name="+ textBox.value;
  url += "&sid="+ Math.random();
  xmlHttp.open( "GET", url, true );
  xmlHttp.send( null );
}

// xmlHttp.open( method, url, asynchronous );
// xmlHttp.send( null );

