/* Formular Functions  copyright 2008 by Luna-Park */

$(document).ready(function(){

  setFormularSubmit();
});


function setFormularSubmit(){

//  $("[ff=main]").attr("onsubmit", "return checkFormular()");
  
//  $("[ffplus]").each(function(){
 //     if($(this).attr("ffplus") == "email"){
 //       $(this).attr("onchange", "checkEmail('"+$(this).attr('id')+"');");
 //     }  
 // });
}
function checkFormular2() {
  var error = 0;
	$("#sfrage2Form input").each(function() {
		if($(this).val() == ""){
			$(this).css("border-color", "#cc0000")
				   .css("border-width", "2px")
				   .css("padding", "1px");
			$(this).bind("change", function() { $(this).css("border", "1px solid #bdbdbd").css("padding", "2px"); }, false);
			error = 1;
		}
	});
	$("#sfrage2Form textarea").each(function() {
		if($(this).val() == ""){
			$(this).css("border", "2px solid #cc0000")
				   .css("padding", "1px");
			$(this).bind("change", function() { $(this).css("border", "1px solid #bdbdbd").css("padding", "2px"); }, false);
			
			error = 1;
		}
	});
  
  if(error == 1){
    return false;
  } else {
    return true;
  }
}

function checkFormular() {
  var error = 0;
  $("[ff=required]").each(function(){
    actid = $(this).attr("id");
    
    if($(this).attr("value") == ""){
      error = 1;
      
      actid = $(this).attr("id");
      setError(actid);
    }
  });
  
  if(error == 1){
    return false;
  } else {
    return true;
  }
}

function setError(id){
  $("#"+id).css("padding", "3px");
  $("#"+id).css("border", "2px solid #FF0000");
  
  $("[for="+id+"] + ul").slideDown();
  
  if($("#"+id).attr("ffplus") == "email"){
  } else {
  //  $("#"+id).attr("onchange", "replaceError('" + id + "')");
  }
}

function replaceError(id){

  if($("#"+id).val() != ""){
  $("#"+id).css("padding", "4px");
  $("#"+id).css("border", "1px solid #666");
  
  $("[for="+id+"] + ul").fadeOut();
  }
}

function checkEmail(id){
  s = $("#"+id).val();
  if(docheckEmail(s, id) == false){
    setError(id);
  } else {
    replaceError(id);
  }
}

function docheckEmail(s, id){
 var a = false;
 var res = false;
 if(typeof(RegExp) == 'function')
 {
  var b = new RegExp('abc');
  if(b.test('abc') == true){a = true;}
  }

 if(a == true)
 {
  reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  res = (reg.test(s));
 }
 else
 {
  res = (s.search('@') >= 1 &&
         s.lastIndexOf('.') > s.search('@') &&
         s.lastIndexOf('.') >= s.length-5)
 }
 
 return(res);
}

// STUDIEN AUFKLAPPMENUE
function openLayer(lID){
   $("#"+lID).slideToggle();
}
function sDropLayer(lID){
    $("#"+lID+" .dropdown").slideToggle();
}
