// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// when set to true, display detailed error messages
var showErrors = true;
// initialize the validation requests cache 
var cache = new Array();

// array storing whether a unique check should set an error or a message
// key = fieldName
// value = true whether it's an error, false if it's a message
var uniqueError = new Array();

// check whether the array of localized strings is defined
if (typeof(lang) == "undefined") {
	var lang = new Array();
	
	lang['empty'] = 'this field cannot be empty';
	lang['nan'] = 'this field must be numeric';
	lang['notyear'] = 'this is not a valid value for a year';
	lang['notdate'] = 'not a valid date';
	lang['pwnomatch'] = 'passwords do not match';
	lang['notunique'] = 'this value already exists in the database';
	lang['checkunique'] = 'checking if it is unique';
}

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {} // ignore potential error
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    displayError("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// function that displays an error message
function displayError($message)
{
  // ignore errors if showErrors is false
  if (showErrors)
  {
    // turn error displaying Off
    showErrors = false;
    // display error message
 
    alert("Error encountered: \n" + $message);
    // retry validation after 10 seconds
    setTimeout("checkUnique();", 10000);
  }
}

// the function handles the validation for any form field
function checkUnique(inputValue, fieldID, primaryKey, serverAddress, isError)
{
  // document.getElementById("httpchecking").innerHTML = "fieldID " + fieldID;

  // save whether it should be an error or a simple message
  uniqueError[fieldID] = isError;

  // we don't check empty values
  if (inputValue == '' && fieldID) {
  	// this means that before we showed an error since the field was not unique
  	// but now the field is empty so remove the error
	  
    removeError(fieldID, lang['notunique'], isError);
    
    // also the possible message "checking again..."
    removeError(fieldID, lang['checkunique'], isError);
	  
	return;
  }

  // if there's already an error shown, it means we're checking again
  // so, let's notify this
  if (fieldID) {
    // find the HTML element that displays the error
	var message = document.getElementById(fieldID + (isError ? "Err" : "Msg"));
	
	if (message.innerHTML != '') {
		removeError(fieldID, lang['notunique'], isError);
	    // also the possible message "checking again..."
	    addError(fieldID, lang['checkunique'], isError);
	}
  }
  
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // if we received non-null parameters, we add them to cache in the
    // form of the query string to be sent to the server for validation
    if (fieldID)
    {
      // encode values for safely adding them to an HTTP request query string
      inputValue = encodeURIComponent(inputValue);
      fieldID = encodeURIComponent(fieldID);
      // add the values to the queue
      cache.push("request=find&value=" + inputValue + "&field=" + fieldID + "&primarykey=" + primaryKey);
    }
    // try to connect to the server
    try
    {
      // continue only if the XMLHttpRequest object isn't busy
      // and the cache is not empty
      if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
         && cache.length > 0)
      {
        // get a new set of parameters from the cache
        var cacheEntry = cache.shift();
        // make a server request to validate the extracted data
        xmlHttp.open("POST", serverAddress, true);
        xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleRequestStateChange;
        xmlHttp.send(cacheEntry);
      }
    }
    catch (e)
    {
      // display an error when failing to connect to the server
      displayError(e.toString());
    }
  }
}

// function that handles the HTTP response
function handleRequestStateChange() 
{
  // when readyState is 4, we read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // read the response from the server
        readResponse();
      }
      catch(e)
      {
        // display error message
        displayError(e.toString());
      }
    }
    else
    {
      // display error message
      displayError(xmlHttp.statusText);
    }
  }
}

// read server's response 
function readResponse()
{
  // retrieve the server's response 
  var response = xmlHttp.responseText;
  
  //document.getElementById("httpchecking").innerHTML = "Response " + response;
  
  // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error:") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);

  try {
	  // get response in XML format (assume the response is valid XML)
	  responseXml = xmlHttp.responseXML;
	  // get the document element
	  xmlDoc = responseXml.documentElement;
	  result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
	  fieldID = xmlDoc.getElementsByTagName("field")[0].firstChild.data;
  } catch (e) {
  	  throw(e.toString() + ' - ' + 
  	  	(response.length == 0 ? "Server reponse empty." : "Server response: " + response));
  }
  
  var isError = uniqueError[fieldID];
  
  // show or hide the error
  if (result != "0") {
    addError(fieldID, lang['notunique'], isError);
    // also the possible message "checking again..."
    removeError(fieldID, lang['checkunique'], isError);
  } else {
    removeError(fieldID, lang['notunique'], isError);
    // also the possible message "checking again..."
    removeError(fieldID, lang['checkunique'], isError);
  }
  // call checkUnique() again, in case there are values left in the cache
  setTimeout("checkUnique();", 500);
}

// sets focus on the specified field of the form
function setFocus(elem)    
{
  document.getElementById(elem).focus();
}

// check that the specified input value is not empty
function checkNotEmpty(inputValue, fieldID, isError)
{
  //document.getElementById("httpchecking").innerHTML = "checking " + fieldID + "...";

  if (isEmpty(inputValue)) {
	addError(fieldID, lang['empty'], isError);
  } else {
  	removeError(fieldID, lang['empty'], isError);
  }
}

function checkNotEmptyDate(fieldID, isError)
{
	// first check for their emptyness
	var day = document.getElementById(fieldID + "_day").value;
	var month = document.getElementById(fieldID + "_month").value;
	var year = document.getElementById(fieldID + "_year").value;

	//document.getElementById("httpchecking").innerHTML = "checking date " + year + "-" + month + "-" + day;

	if (isEmpty(day) || isEmpty(month) || isEmpty(year) ||
		day == '00' || month == '00' || year == '0000') {
	  addError(fieldID, lang['empty'], isError);
    } else {
  	  removeError(fieldID, lang['empty'], isError);
    }
}

// check that the specified input value represents
function checkNumber(inputValue, fieldID, isError)
{
  if (isNaN(inputValue)) {
	addError(fieldID, lang['nan'], isError);
  } else {
  	removeError(fieldID, lang['nan'], isError);
  }
}

// check that the specified input value represents
function checkYear(inputValue, fieldID, isError)
{
  if (isNaN(inputValue)) {
	addError(fieldID, lang['notyear'], isError);
  } else {
  	removeError(fieldID, lang['notyear'], isError);
  }
}


// check that the two passwords match
function checkPassword(pass1, pass2, fieldID)
{
  if (pass1 != pass2) {
	  addError(fieldID, lang['pwnomatch'], true);
  } else {
  	  removeError(fieldID, lang['pwnomatch'], true);
  }
}

function checkCorrectDate(name)
{
	// first check for their emptyness
	var day = document.getElementById(name + "_day").value;
	var month = document.getElementById(name + "_month").value;
	var year = document.getElementById(name + "_year").value;

	//document.getElementById("httpchecking").innerHTML = "checking date " + year + "-" + month + "-" + day;

	// accept completely empty dates (00-00-0000 is mysql empty date)
	if ((isEmpty(day) && isEmpty(month) && isEmpty(year)) ||
		(day == '00' && month == '00' && year == '0000'))
		return true;

	if (isEmpty(day) || isEmpty(month) || isEmpty(year) ||
		day == '00' || month == '00' || year == '0000')
		return false;
		
	if (isNaN(day) || isNaN(month) || isNaN(year))
		return false;	

	day = parseInt(document.getElementById(name + "_day").value, 10);
	month = parseInt(document.getElementById(name + "_month").value, 10);
	year = parseInt(document.getElementById(name + "_year").value, 10);

	//document.getElementById("httpchecking").innerHTML = "checking date " + year + "-" + month + "-" + day;

	var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	if (year/4 == parseInt(year/4))
		monthLength[1] = 29;

	if (month < 1 || month > 12)
		return false;

	if (day > monthLength[month-1])
		return false;

	monthLength[1] = 28;

	var now = new Date();
	now = now.getTime();

	var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	var checkDate = dateToCheck.getTime();

	var futureDate = (now < checkDate);
	var pastDate = (now > checkDate);

	return true;
}

function checkDate(fieldID)
{

  //document.getElementById("httpchecking").innerHTML = "checking date " + fieldID + "...";
  
  // find the HTML element that displays the error
  message = document.getElementById(fieldID + "Err");

  if (!checkCorrectDate(fieldID)) {
	  addError(fieldID, lang['notdate'], true);
  } else {
  	  removeError(fieldID, lang['notdate'], true);
  }
}

// extract the file name from a path
function basename(path)
{
    var filename = path.split('/');
    if (filename.length == 1) {
    	// windows?
        var filename = path.split("\\");    
    }
    filename = filename[filename.length-1];
    return filename;
}

// simple trim function
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isEmpty(s) {
	return !s || trim(s) == '';
}

// this function adds an error at the end of a possible previous
// error message
function addError(elementId, error, isErrorOrMsg)
{
	// find the HTML element that displays the error
	var message = document.getElementById(elementId + (isErrorOrMsg ? "Err" : "Msg"));
	
	if (message.innerHTML == '' || message.innerHTML.indexOf(error) == -1) {
		// OK this error is not already shown, so add it and show the error
		message.className = (isErrorOrMsg ? "error" : "msg");
		message.innerHTML += error + '<br>';
		// the error is appended to the possible current one
	} 
	// otherwise this error is already shown
}

// this function removes an error from a possible previous
// error message.  Then, if the error message is empty, also makes it hidden
function removeError(elementId, error, isErrorOrMsg)
{
	// find the HTML element that displays the error
	var message = document.getElementById(elementId + (isErrorOrMsg ? "Err" : "Msg"));

	// some browsers might change the case of <br>, e.g., Firefox
	var toreplace = error + "<br>";
	if (message.innerHTML.indexOf(toreplace) == -1) {
		toreplace = error + "<BR>";
	}
	
	message.innerHTML = message.innerHTML.replace(toreplace, "");
	
	/*
	for (i = 0; i < message.innerHTML.length; ++i) {
		document.getElementById("httpchecking").innerHTML += message.innerHTML.charAt(i) + ' ';
	}
	*/
	
	if (message.innerHTML == '') {
		// OK this error empty so we hide it
		message.className = "hidden";
	}
}
