function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// ----------------------------------------------------------------------------
// HasClassName
//
// Description : returns boolean indicating whether the object has the class name
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function HasClassName(objElement, strClass)
   {

   // if there is a class
   if ( objElement.className )
      {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ )
         {

         // if class found
         if ( arrList[i].toUpperCase() == strClassUpper )
            {

            // we found it
            return true;

            }

         }

      }

   // if we got here then the class name is not there
   return false;

   }
// 
// HasClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// AddClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
function AddClassName(objElement, strClass, blnMayAlreadyExist)
   {

   // if there is a class
   if ( objElement.className )
      {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // if the new class name may already exist in list
      if ( blnMayAlreadyExist )
         {

         // get uppercase class for comparison purposes
         var strClassUpper = strClass.toUpperCase();

         // find all instances and remove them
         for ( var i = 0; i < arrList.length; i++ )
            {

            // if class found
            if ( arrList[i].toUpperCase() == strClassUpper )
               {

               // remove array item
               arrList.splice(i, 1);

               // decrement loop counter as we have adjusted the array's contents
               i--;

               }

            }

         }

      // add the new class to end of list
      arrList[arrList.length] = strClass;

      // add the new class to beginning of list
      //arrList.splice(0, 0, strClass);
      
      // assign modified class name attribute
      objElement.className = arrList.join(' ');

      }
   // if there was no class
   else
      {

      // assign modified class name attribute      
      objElement.className = strClass;
   
      }

   }
// 
// AddClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// RemoveClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to remove
//
function RemoveClassName(objElement, strClass)
   {

   // if there is a class
   if ( objElement.className )
      {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ )
         {

         // if class found
         if ( arrList[i].toUpperCase() == strClassUpper )
            {

            // remove array item
            arrList.splice(i, 1);

            // decrement loop counter as we have adjusted the array's contents
            i--;

            }

         }

      // assign modified class name attribute
      objElement.className = arrList.join(' ');

      }
   // if there was no class
   // there is nothing to remove

   }
// 
// RemoveClassName
// --------------------------------------------------------------------------

function Validate_Email_Address(email_address){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email_address) == false)     
      return false;
   else 
      return true;
}

function checkEmail(email_input) {
	var result;
	if( result = Validate_Email_Address(email_input.value)){
		RemoveClassName(email_input,'error');
	} else {
		AddClassName(email_input,'error');
		email_input.focus();
	}
	return result;
}


function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function validNumber(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  //return validateRequired(val);
}

function checkNumber(num_input) {
  validNumber(num_input);
}


function highlightActiveInput() {
  AddClassName(this,'inputHighlighted');
}

function blurActiveInput() {
  RemoveClassName(this,'inputHighlighted');
}



function setCursor(){
	document.forms.request_form.name.focus()
}


function validateRequired(item){
	if(item.value == "")  {
	  AddClassName(item,'error');
      item.focus();
      return false;
	} else {
	   RemoveClassName(item,'error');
	   	return true;
	}
}

function validateForm(form){
	var errors = 0;
	var inputs = document.getElementsByTagName("input");
	for (var i=inputs.length-1; i >= 0; i--){
		if(HasClassName(inputs[i],'required'))
		  errors += !validateRequired(inputs[i]);
		if(inputs[i].getAttribute('name') == "email") 
		  errors += !checkEmail(inputs[i]);
	}
	if(errors > 0) {
		node = document.getElementById('form_warning');
		if(!node) return false;
		var header = document.createElement('h3');
		var theTextOfTheParagraph = document.createTextNode('Některé položky nebyly správně vyplněny!');
		header.appendChild(theTextOfTheParagraph);
        node.appendChild(header);
		return false;
	}
}

function reqOnKeyUp(){
	return validateRequired(this);
}

function prepareInputs() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		 if(inputs[i].className == "residence" || inputs[i].className == "submit" ) continue;
			inputs[i].onfocus = highlightActiveInput;
			inputs[i].onblur = blurActiveInput;
		if(HasClassName(inputs[i],'required') && typeof inputs[i].onkeyup!= 'function')
		   inputs[i].onkeyup = reqOnKeyUp;
	}
}


addLoadEvent(prepareInputs);
addLoadEvent(setCursor); 

