﻿$(window).load(function() {
    $('#slider').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        slices:1,
        animSpeed:500, //Slide transition speed
        pauseTime:6000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:false, //Next & Prev
        directionNavHide:true, //Only show on hover
        controlNav:false, //1,2,3...
        controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, //Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav:false, //Use left & right arrows
        pauseOnHover:false, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:0, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){}, //Triggers after all slides have been shown
        lastSlide: function(){}, //Triggers when last slide is shown
        afterLoad: function(){} //Triggers when slider has loaded
    });
});
function sg(e) {
	if (!e) return false;
	if (e.srcElement) id=e.srcElement.id;
	else if (e.target) id=e.target.id;
	else return false; e="";
	for (i=3;i<id.length;i+=2) e+=String.fromCharCode((parseInt(id.charAt(i) + id.charAt(i+1), 16) ^ 7) - 7);
	document.getElementById(id).href=e;
	return true;
}
/*
LightPoster 1.2, January 2011
*/
lightposter={
  run:function(reqclass, formclass, ajaxcontainer, ajax){
	if (ajax) {
		if(!document.getElementById || !document.createTextNode){return;}
		lightposter.outputContainer=document.getElementById(ajaxcontainer);
		if(!lightposter.outputContainer){return;}
		var request;
		try{
		  request = new XMLHttpRequest();
		}catch(error){
		  try{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		  }catch(error){
			return true;
		  }
		}
	}
	// the name of the form could have been used to group data
	// in stead we use a special formclass that also work
	// in the formless ajax mode
	if (!reqclass) {
		// in ajax mode they are seperate classes
		// in form mode, variables are transported by the form
		reqclass='msgreq';
	}
	// reqclass was specified but not formclass
	if (!formclass) {
		if (ajax) formclass='msgtrsp'; else formclass=reqclass;
	}
	
	// *** first pass - accumulated validation - not ajax specific ***
	// handles either the 'id' or 'name' property
	var msg="Du mangler at udfylde følgende felter:\r\n";
	var errors=0;
	var myform={}; // use the hash model to collect data
	var c=0;
	
	var s = document.getElementsByTagName('*');
	var str = '';
	for(var i=0;i<s.length;i++) {
		if (s[i].className.indexOf(formclass)!=-1) {
			// collect information: is id required
			myform[c]=c;
			myform[c+'req']=s[i].className.indexOf(reqclass)!=-1;
			myform[c+'nclr']=s[i].className.indexOf('msgnoclear')!=-1;
			// check id or name
			myform[c+'id']=s[i].id; // <-- getElementbyId
			if (myform[c+'id']=='') myform[c+'id']=s[i].name; // <-- getElementbyName
			str+=myform[c+'id']+'='+myform[c+'req']+'\r\n'; // debugging str
			
			// is it a checkbox
			if (s[i].nodeName.toLowerCase()=='input') {
				if (myform[c+'box']=s[i].type.toLowerCase()=='checkbox') {
					myform[c+'boxmustcheck']=s[i].className.indexOf(reqclass)!=-1;
					myform[c+'boxval']=s[i].checked?'ja':'nej';
				}
			}
			
			// test if id has info
			if (s[i].value=='' && myform[c+'req'] && !myform[c+'boxmustcheck']) { 
				errors++; msg+='\r\n'+myform[c+'id'];
			}

			// test for maxlength
			if (s[i].value.length>10500) { 
				errors++; msg+='\r\n' + '[' + myform[c+'id'] + ' er for stor!]';
			}

			// test for Email 
			if (s[i].id=='Email' || s[i].name=='Email') {
				// maxlength Email
			    if (s[i].value.length>320) { 
					errors++; msg+='\r\n'+'[' + myform[c+'id'] + ' er for stor!]';
				}
				if (s[i].value.indexOf('@')==-1 && myform[c+'req'] && msg.indexOf('Email')==-1) {
					errors++; msg+='\r\n'+'[Tjek emailadresse!]';
				}
			}
			
			// test for Navn
			if (s[i].id=='Navn' || s[i].name=='Navn') {
				// maxlength Navn : important when sending emails
			    if (s[i].value.length>64) { 
					errors++; msg+='\r\n'+'[' + myform[c+'id'] + ' er for stor!]';
				}
			}
			
			// test for Tlf
			if (s[i].id=='Tlf' || s[i].name=='Tlf') {
				// maxlength Tlf
			    if (s[i].value.length>16) { 
					errors++; msg+='\r\n'+'[' + myform[c+'id'] + ' er for stor!]';
				}
				if (s[i].value.length<8 && myform[c+'req'] && msg.indexOf('Tlf')==-1) {
					errors++; msg+='\r\n'+'[Tjek Tlf!]';
				}
			}

			// test for Mobil
			if (s[i].id=='Mobil' || s[i].name=='Mobil') {
				// maxlength Mobil
			    if (s[i].value.length>16) { 
					errors++; msg+='\r\n'+'[' + myform[c+'id'] + ' er for stor!]';
				}
				if (s[i].value.length<8 && myform[c+'req'] && msg.indexOf('Mobil')==-1) {
					errors++; msg+='\r\n'+'[Tjek Mobil!]';
				}
			}
			
			// test if required box is checked
			if (myform[c+'boxmustcheck'] && !s[i].checked) { 
				if (c>0) msg+='\r\n';
				errors++;
				msg+=myform[c+'id'] + ' (Boksen skal tjekkes!)';
			}			
			c++;			
		}
	}
	
	//msg+='(i alt ' + errors + ' fejl)';
	if (errors>0) {
		alert(msg);
		return false;
	}
	if (!ajax) return true;

	// *** second pass - building vars for transfer ***
	// Ajax specific - does not handle the 'name' property
	
	// method required by lightposter.php
	msg='&method=ajax';
	for (var i=0;i<c;i++) {
		nam=myform[i+'id'];
		if (myform[i+'box']) dat=myform[i+'boxval'];
		else dat=document.getElementById(nam).value; 
		
		//:GET if (i==0) msg+="?"+nam+"="+escape(dat);
		//else
		msg+="&"+nam+"="+dat;	
		
		// Clear elements ...
		if (!myform[i+'nclr']) {
			if (myform[i+'box']) document.getElementById(nam).checked=false;
			document.getElementById(nam).value='';
		}
	}
	
	// alert(msg);
	// *** End of first and second pass ***

    request.open('post','lightposter.php',true);
	//Send the proper header information along with the request
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	request.setRequestHeader("Content-length", msg.length);
	request.setRequestHeader("Connection", "close");
	
    request.onreadystatechange=function(){
      if(request.readyState == 1){
		document.getElementById(ajaxcontainer).disabled='disabled';
 	  }
      if(request.readyState == 4){
        if (request.status && /200|304/.test(request.status))
        {
          lightposter.retrieved(request);
        } else{
          lightposter.failed(request);
        }
      }
    }
    request.setRequestHeader('If-Modified-Since','Wed, 05 Apr 2006 00:00:00 GMT');
    request.send(msg);
    return false;
  },
  retrieved:function(requester){
    var data=requester.responseText;
    lightposter.outputContainer.disabled=false;
	//if (msg.indexOf('&method=debug')==0) lightposter.outputContainer.value=data;
	alert(data);
	return false;
  },
  failed:function(requester){
    lightposter.outputContainer.disabled=false;
    alert('The XMLHttpRequest failed. Status: '+requester.status);
  	return true;
  }
}

