$(document).ready(function(){
	
	$( "#keyword" ).autocomplete({
		source: "/ajrgfx/ajax/getEducation.asp",
		minLength: 1,
		select: function( event, ui ) {
		
			$('#keyword').val(ui.item.id)
		
		}
	});
	
	if($.cookie('arriva')=='true'){$('#arriva').attr('checked',true)};
	if($.cookie('bybus')=='true'){$('#bybus').attr('checked',true)};
	if($.cookie('madbillet')=='true'){$('#madbillet').attr('checked',true)};
	if($.cookie('reminder')=='true'){$('#reminder').attr('checked',true)};
	if($.cookie('panel')=='true'){$('#panel').attr('checked',true)};
	
	$(document).find('.scrambleemail').each(function(){
		scrambleEmail($(this));
	});

	$('#toggleform').click(function(){
		$('#orderinfo').toggle();
	});

	$('#datoselector').mouseout(function(){
		$(this).oneTime(500, function() {
		  $(this).hide();
		});
		$(this).mouseover(function(){
		  $(this).stopTime();
		});
	});	

	$('#timeselector').mouseout(function(){
		$(this).oneTime(500, function() {
		  $(this).hide();
		});
		$(this).mouseover(function(){
		  $(this).stopTime();
		});
	});	

	$('#interestselector').mouseout(function(){
		$(this).oneTime(500, function() {
		  $(this).hide();
		});
		$(this).mouseover(function(){
		  $(this).stopTime();
		});
	});		

	$('#datotitle').click(function(){
		$('#timeselector').hide();
		$('#interestselector').hide();
	});

	$('#interesttitle').click(function(){
		$('#datoselector').hide();
		$('#timeselector').hide();
	});
	
	$('#timetitle').click(function(){
		$('#datoselector').hide();
		$('#interestselector').hide();
	});
	
	$('#interesttitle').click(function(){
		$('#datoselector').hide();
		$('#timeselector').hide();
	});
	
	
	$(".partners img").hover(
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('off','on'));
	  },
	  function () {
	    $(this).attr("src",$(this).attr("src").replace('on','off'));
	  }
	);

});

function toggle(id){
	$('#'+id).toggle();
}

function selectthis(me,id,ddid,valueid){
	document.getElementById(id).innerHTML = me.innerHTML;
	document.getElementById(valueid).value = me.innerHTML;
	toggle(ddid);
}

function ShowRandomImage(xmlpath,id){

	$.ajax({
	    type: "GET",
		url: xmlpath,
		dataType: "xml",
		success: function(xml) {
		 
			var images = $(xml).find('filepath');

			var index = randomXToY(0,(images.length-1),0);
			
			document.getElementById(id).innerHTML = '<img src="' + $(images[index]).text() +'" alt="" />';
		 
		}
	});
	
}

function randomXToY(minVal,maxVal,floatVal){
	var randVal = minVal+(Math.random()*(maxVal-minVal));
	return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}


function resetform(){

	document.getElementById('keyword').value = 'Søg uddannelser';
	
	document.getElementById('datotitle').innerHTML = 'Vælg dato';
	document.getElementById('timetitle').innerHTML = 'Vælg tidspunkt';
	document.getElementById('interesttitle').innerHTML = 'Vælg interesseområde';
	
	document.getElementById('datovalue').value = '';
	document.getElementById('timevalue').value = '';
	document.getElementById('interestvalue').value = '';	

}

function scrambleEmail(string){

	var emailarray = string.html().split('|');
	var account = emailarray[0];
	var domain = emailarray[1];
	var subject = '';
	
	if(emailarray[2]){subject=emailarray[2]};
	
	var email = account + '@' + domain;
	
	string.html('<a href="mailto:'+email+'?subject='+subject+'">'+email+'</a>');

}

function checkorderform(me) {
		
	resetfieldclasses(me);
	
	if (me.email.value=='Indtast e-mail'){alert('Din mailadresse er ikke gyldig.'); redalert(me.email); return false;}
	if (!emailCheck(me.email.value)){alert('Din mailadresse er ikke gyldig.'); redalert(me.email); return false;}

	if (me.postnummer.value=='Indtast postnummer'){redalert(me.postnummer); return false;}
	if (me.postnummer.value==''){redalert(me.postnummer); return false;}
	
	if(me.reminder.checked){
		if (me.mobil.value=='Indtast mobilnummer'){alert('Angiv dit mobilnummer for at modtage reminders.'); redalert(me.mobil); return false;}
		if (me.mobil.value==''){alert('Angiv dit mobilnummer for at modtage reminders.'); redalert(me.mobil); return false;}
	}
	
	me.submit();

}

function resetfieldclasses(me){
	for ( var i=0;i<me.elements.length;i++ )
	{
		if(me.elements[i].name != ''){
			if( me.elements[i].type=='text' )
			{
			me.elements[i].style.border='1px solid #d8d8d8';
			}
		}		
	}
}

function redalert(me){
	me.style.border='1px solid red';
	me.focus();
}
