/*
System - range of functions

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!DO NOT CHANGE THIS FILE!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


* functionality of links and buttons on startup
* fn ValideKontaktform()	- validates contactform
* fn EMail()				- called by ValideKontaktform()
* fn showLogin()			- generates Loginpanel
* fn liveSearch()			- shows live results in result-div (not for dysant)
* fn sendSearch()			- post search value, redirect to search site
  fn submitNewsletterSubscription()
*/


function submitNewsletterSubscription(){
	errorMsg=""
	err=false;
	
	$('form .requested').each(function(){
			if($(this).attr('value') == ""){
				var name = '';
				switch($(this).attr('name')){
					case 'Name':
						name = 'Imi\u0119 i nazwisko'
						break
					case 'Email':
						name = 'Email'
						break
				}
				errorMsg += '<p>Prosz\u0119 wype\u0142ni\u0107 pole: "' + name +'"!</p>';
				err=true;
			}
	})
		
	if(err){
		$('#Mitmachen_fehlermeldung').html(errorMsg);
		return false
	}
	document.forms['NLSubscribe'].submit();
}

$(document).ready(function(){
//LoginBox aufr\u00FCfbar machen
		$('#loginCiSweb4').click(function(){showLogin($(this));})
		$('#closeLogin, #LoginBG').click(function(){$('#LoginBG, #loginBox').fadeOut()})

//LoginBox Actions
        $("#Login_Send").click(function(){
       		url="/names.nsf?login";
			txt= "&username=" + $("#LoginName").val() + "&password=" + $("#LoginPW").val()
			$.ajax({
				url: url + txt,
	  			cache: false,
	  			processData: false,
	  			success: function(html){
    				if(html.search(/names.nsf/)== -1){
  						window.location="/" + CISDBName;
  					}else{
	  					$("#loginBox input").addClass('error');
	  					$("#LoginName").val("");
	  					$("#LoginPW").val("");
	  				}
  				}
			});
		});

//Search button functionality	
	$("#searchButton").click(function(){
		if($("#searchvalue").val()!="")
			window.location= DBName + "/SearchSite.xsp?SearchValue=" + $("#searchvalue").val();
	});
	
	$("#searchvalue").keydown(function(e){
		if(e.which==13){
			if($("#searchvalue").val()!="")
			window.location= DBName + "/SearchSite.xsp?SearchValue=" + $("#searchvalue").val();
			return false;
		}
		if($("#searchvalue").val().length>1){
		 	/*
			// Dysant: disabled because causes errors
			$.ajax({
				url: DBName + "/(livesearchagent)?openAgent&query=" + $("#searchvalue").val(),
	  			cache: false,
	  			processData: false,
	  		  	
	  			success: function(xml){
	  			   	liveSearch(xml);
	  			}
	  		})
	  		*/
  		};
  	
  	});
        
});

function ValideKontaktform(){
	errorMsg=""
	err=false;
	
	$('form .requested').each(function(){
			if($(this).attr('value') == ""){
				var name = '';
				switch($(this).attr('name')){
					case 'firstname':
						name = 'Imi\u0119'
						break
					case 'lastname':
						name = 'Nazwisko'
						break
					case 'email':
						name = 'Email'
						break
					case 'phone':
						name = 'Telefon'
						break
					case 'street':
						name = 'Ulica'
						break
					case 'city':
						name = 'Miasto'
						break
					case 'zip':
						name = 'Kod'
						break
					case 'captchaText':
						name = 'Kod CAPTCHA'
						break
					case 'subject':
						name = 'Temat'
						break
					case 'message':
						name = 'Wiadomo\u015B\u0107'
						break		
				}
				errorMsg += '<p>Prosz\u0119 wype\u0142ni\u0107 pole: "' + name +'"!</p>';
				err=true;
			}
	})
		
	if(err){
		$('#Mitmachen_fehlermeldung').html(errorMsg);
		return false
	}
	
	dojo.xhrPost({
		url: dojo.byId('contactForm').action,
		form: dojo.byId('contactForm'),
		handle: function(data){
			if(data != '1'){
				$('#Mitmachen_fehlermeldung').html('<p>Prosz\u0119 wprowadzi\u0107 poprawny kod CAPTCHA!</p>');
			}
			else{
				var path = location.pathname.split('/');
				path.pop();
				path.push('OK');
				location.pathname = path.join('/');
			}
		}
	});
	
	return false;
}

function EMail(s)
{
  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);
 }
 
 function showLogin(a){
		$('#LoginBG').fadeTo(200,0.7)
		$('#loginBox').fadeIn()
		return false;
}
 
 function liveSearch(xmldoc) {
	document.getElementById("result").innerHTML="";
    var i=0;
    var text="";
   
   while(xmldoc.getElementsByTagName('string')[i] ){
      text=text + "<a href=\"" + DBName + "/SearchSite.xsp?SearchValue=" +  xmldoc.getElementsByTagName('string')[i].getElementsByTagName('text')[0].firstChild.nodeValue + "\">" +  xmldoc.getElementsByTagName('string')[i].getElementsByTagName('text')[0].firstChild.nodeValue + "</a>";
  	  text+="<div class='box220_end'></div>";
  	  i++;
  }
           
  if(text!="") {
       document.getElementById("result").style.display="block";
       document.getElementById("result").innerHTML=text;
       setTimeout("document.getElementById('result').style.display='none';" , 4000);
  }
}

function sendSearch(){
	if($("#searchvalue").val()!="" && $("#searchvalue").val()!="Suchbegriff eingeben")
			window.location= DBName + "/SearchSite.xsp?SearchValue=" + $("#searchvalue").val();
}
 
 
