// File name: kavaleksolutions.checkform.js
// Last change: 2007-16-10
// Author: Tomas Kavalek, KAVALEK.NET
// E-mail: tomas.kavalek@gmail.com
// WWW: http://www.kavalek.net/
// WWW: http://www.kavalek.eu/

/**
 * Functions for checking form before sending to server
 *
 * @package Libraries
 * @author Tomas Kavalek <tomas.kavalek@gmail.com>
 * @version 0.1
 */

/**
 * Compare captcha with its hash
 *
 * Function comparing captcha hash with hashed user input captcha 
 *
 * @param string captcha id of XHTML element with user input captcha 
 * @param string hash id of XHTML element with hashed server stored captcha   
 * @return bool
 */ 
function checkCaptcha(captcha, hash) {
  var captcha = document.getElementById(captcha);
  var hash = document.getElementById(hash);
  if(MD5(captcha.value) == hash.value) return true;
  else return false;
}

/**
 * Check input element according regular expressions
 *
 * Function for checking user input according regular expressions 
 *
 * @param string e id of XHTML element with user input 
 * @param string type type of regular expression (mail, name, phone or text)   
 * @return bool
 */ 
function checkInput(e, type) {
  var input = document.getElementById(e);
  var pattern;
  var advArr;
  var subPattern;
  var advCount = 0;
  var result;
  switch(type) {
    case 'mail' :
      pattern = /\w+@\w{1,}\u002e\w{2,}/;
      subPattern = /@/g;
      break;
    case 'name' :
      pattern = /[^ ]+/;
      break;
    case 'phone' :
      pattern = /\+\d{3} \d{3} \d{3} \d{3}$/;
      break;
    default :
      return false;
      break;
  }
  switch(type) {
    case 'mail' :
      while((advArr = subPattern.exec(input.value)) != null) advCount++;
      if(advCount == 1) result = pattern.test(input.value);
      else result = false;
      break;
    default :
      result = pattern.test(input.value);
      break;
  }
  return result;
}

function checkText(e) {
  var input = document.getElementById(e);
  if(input.value.length > 0) return true;
  else return false;
}


/**
 * Change element style
 *
 * Function for changing element style in case of bad value 
 *
 * @param string e id of XHTML element with user input 
 * @return null
 */ 
function badField(e) {
  var input = document.getElementById(e);
  input.style.borderColor = '#FF0000';
  input.onfocus = function() {this.style.borderColor = '#000000';}
}


/**
 * Check form
 *
 * Function for checking whole form before sending to server
 *
 * @return null
 */ 
function checkForm() {
  var result = true;
  if(!checkCaptcha('captcha', 'hash')) {
    badField('captcha');
    result = false;
  }
  if(!checkInput('mail', 'mail')) {
    badField('mail');
    result = false;
  }
  if(!checkInput('phone', 'name')) {
    badField('phone');
    result = false;
  }
  if(!checkInput('beds', 'name')) {
    badField('beds');
    result = false;
  }
  if(!checkInput('from', 'name')) {
    badField('from');
    result = false;
  }
  if(!checkInput('to', 'name')) {
    badField('to');
    result = false;
  }
  if(!checkInput('name', 'name')) {
    badField('name');
    result = false;
  }
  if(!result) {
    document.getElementById('error').style.display = 'block';
  }
  return result;
}
 
/**
 * Check form
 *
 * Function for checking whole form before sending to server
 *
 * @return null
 */ 
function checkMailForm() {
  var result = true;
  if(!checkCaptcha('captcha', 'hash')) {
    badField('captcha');
    result = false;
  }
  if(!checkInput('mail', 'mail')) {
    badField('mail');
    result = false;
  }
  if(!checkInput('name', 'name')) {
    badField('name');
    result = false;
  }
  if(!checkText('body')) {
    badField('body');
    result = false;
  }
  if(!result) {
    document.getElementById('error').style.display = 'block';
  }
  return result;
}

