function prepFormFields() {
  var htmlElements = Array('select', 'input', 'textarea');
  var htmlTempArray;
  for (var i = 0; i < htmlElements.length; i++) {
   htmlTempArray = document.getElementsByTagName(htmlElements[i]);
   for (var j = 0; j < htmlTempArray.length; j++) {
    htmlTempArray[j].onmouseover = htmlTempArray[j].onfocus = function() {
     this.className = "selected";
     }
    htmlTempArray[j].onmouseout = htmlTempArray[j].onblur = function() {
     this.className = "";
    }
   }
  }
 }

function isNotEmpty(elem) {
  var str = elem.value;
  if(str == null || str.length == 0)
   return false;
  else
   return true;
 }

 // THIS FUNCTION PERFORMS VALIDATION BASED ON A SET OF CUSTOM HTML ATTRIBUTES
 function validate(form) {
  var attrVal, attrReg, attrEq, attrFail, strTemp;
  var formFail = form.getAttribute("failure");

  for (var i = 0; i < form.length; i++) {
   attrVal = form[i].getAttribute("validate");

   switch (attrVal) {
    case 'required' :
    if (!isNotEmpty(form[i])) {
     attrFail = form[i].getAttribute("failure");

    if(attrFail) {
	    alert(attrFail);
 	} else if(formFail) {
	 	alert(formFail);
	} else {
     	 alert('You must complete all required form fields.');
  	}
     form[i].focus();
     return false;
    }
    break;

    case 'regex' :
    attrReg = form[i].getAttribute("regex");
    if (attrReg != null && attrReg.length != 0) {
     var regex = new RegExp(attrReg);
     strTemp = form[i].value;
     if (!strTemp.match(regex)) {
      attrFail = form[i].getAttribute("failure");

      if (attrFail)
       alert(attrFail);
      else
       alert('Invalid data format at field "' + form[i].name + '".');
      form[i].focus();
      return false;
     }
    }
    break;
	
	/*case 'ajax':

		var resp = function(originalRequest) {
			var txt = originalRequest.responseText;
			if(txt != 'ok') {
				alert(txt);
				return false;
			}			
		}
	
		var str = form[i].value;
		var url = form[i].getAttribute("href");
		var pars = 'str='+str;
		var args = {method:'post',parameters: pars,evalScripts: true,onComplete: resp};	
		var req = new Ajax.Request(url,args);
		
		return false;
		
	break;*/

    case 'email' :

    	var str = form[i].value;
    	var chk = (str.indexOf(".") > 0) && (str.indexOf("@") > 0);

    	if(!chk) {
	    	attrFail = form[i].getAttribute("failure");
     		if (attrFail) {
      			alert(attrFail);
 			} else {
      			alert('Podany adres nie jest poprawnym adresem e-mail.');
  			}
  		form[i].focus();
     	return false;
		}

    break;

    case 'checked' :

    	if(form[i].checked == false) {
	    	attrFail = form[i].getAttribute("failure");
     		if (attrFail) {
      			alert(attrFail);
 			} else {
      			alert('You must provide valid email.');
  			}
  		form[i].focus();
     	return false;
		}

    break;


    case 'equals' :
    attrEq = form[i].getAttribute("equals");
    var objEq = document.getElementById(attrEq);
    if (objEq) {
     if (form[i].value != objEq.value) {
      attrFail = form[i].getAttribute("failure");

      if (attrFail)
       alert(attrFail);
      else
       alert('Form fields do not match');

      form[i].focus();
      return false;
     }
    }
    break;
   }

  }

  return true;
}

function month(fid) {
	if($(fid).value != '') {
		var dat = $(fid).value;
		var mnt = dat.substr(5,2) - 1;
		return mnt;
	} else {
		return null;
	}
}

function send_form(formid) {
	
	var formObj = $(formid);
		
	if(validate(formObj)) {
		
		var form1 = Form.serialize(formid);
		var pars = form1;
		var args = {method:'post',parameters: pars,evalScripts: true};
		var url = "/index.php?mod=email&action=send_form&ajax=true";
		formObj.innerHTML = 'Trwa wysyłanie formularza... <img src="/images/spinner.gif" />';
		var AjaxRez = new Ajax.Updater(formid,url,args);
		
	}
	
}

/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var chk = Form.getInputs($(el),'radio', radioGroup)
	var checked = chk.find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

function selectAll(selectBox,selectAll) {
	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}
	// is the select box a multiple select box?
	//alert(selectBox.type);
	if (selectBox.type == "select-multiple") {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = true;
		}
	}
}