/* JavaScript för sidan Kontakta oss */

// Funktion som laddar andra funktioner vid window.onload.
function addLoadEvent(func){
  var oldonload = window.onload;
  if (typeof window.onload != 'function'){
    window.onload = func;
  } 
  else {
    window.onload = function(){
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

$("document").ready(function(){
	$("#specific-yes").click(function(){$("#specificWrap").slideDown(100)});
	$("#specific-no").click(function(){$("#specificWrap").slideUp(100)});
});

function hideForms()
{
	if($("#rbtValue").val()=="subjectD"){
		$("#formF").hide();
		$("#subjectD").attr("checked",true);
		showHideInputFields('subjectD');
	}
	else
	{
		$("#formF").hide();
		$("#formD").hide();
	}
	
	if($("#rbtValueVaraKontor:visible") && $("#divFVaraKontor:visible") && $("#rbtValueVaraKontor").val()=="F"){
		$("#divFVaraKontor").attr("checked",true);
		showHideInputFields('subjectF');
	}
}

function showHideInputFields(target){
	if(target=="subjectD"){
		$("#rbtValue").val("subjectD");
		$("#formF").css("display", "none");
		$("#formD").css("display", "block");
		
/*		$("#formF").slideUp(function(){
			$("#formD").slideDown();
		});*/
	}
	else if(target=="subjectF"){
		$("#formF").css("display", "block");
		$("#formD").css("display", "none");
/*		$("#formD").slideUp(function(){
			$("#formF").slideDown();
		});*/
	}
}

//Döljer/visar fält för kommun resp. fastighetsbeteckning
function showHideSpecific(){
	$("#specificWrap").hide();
	$("#specific-yes").attr("checked",false);
	$("#specific-no").attr("checked",false);
}

//validering för att säkerställa att man angett en kommun.
function validateMunicipalityForm(lang){
	if($("#kmnFormSelect").val()==""){
				if(lang=="SV"){
			alert("Du måste ange vilken kommun fastigheten ligger i");
		}
		else{
			alert("You need to enter the municipality where the property is located");
		}
		$("#kmnFormSelect").focus();
		return false;
	}
	return true;
}


//validering för att säkerställa att alla obligatoriska fält är ifyllda
function validateForm(lang, type){	
	var form = "";
	if(type=="f"){
		form = $("#formFContact");
	}
	else if(type=="d"){
		form = $("#formDContact");
	}
	  
	if(type == "f"){
		if (!$("#specific-yes").attr("checked") && !$("#specific-no").attr("checked")){
			if(lang=="SV"){
				alert("Vänligen ange om ditt ärende gäller en specifik fastighet eller inte");
			}
			else{
				alert("Please choose if your matter regards a specific property or not");
			}			
			return false;
		}
		
		if($("#specific-yes").attr("checked")){
			if($("#kommun").val()==""){
				if(lang=="SV"){
					alert("Du glömde att ange vilken kommun fastigheten ligger i");
				}
				else{
					alert("You need to enter a municipality");
				}
				$("#kommun").focus();
				return false;
			}
			if($("#beteckning").val()==""){
				if(lang == "SV"){
					alert("Du glömde att ange fastighetsbeteckning eller adress");
				}
				else{
					alert("You need to enter property unit designation or address");
				}
				$("#beteckning").focus();
				return false;
			}
		}
	}
	
	if(!$('input[name="type"]').attr("checked")){
		if(lang=="SV"){
			alert("Vänligen ange kategori");
		}
		else{
			alert("Please choose a category");
		}
		return false;
	}
		
	if ($("#contact-name" + type).val() == ""){
		if(lang=="SV"){
			alert("Du glömde att ange namn");
		}
		else{
			alert("You need to enter your name");
		}
		$("#contact-name" + type).focus();
		return false;
	}

	
	if ($("#contact-radio-phone"+type).attr("checked")){
		if ($("#contact-phone"+type).val()==""){
			if(lang=="SV"){
				alert("Du har valt att bli kontaktad via telefon men inte angett något telefonnummer");
			}
			else{
				alert("You need to enter your phone number");
			}
			$("#contact-phone"+type).focus();
			return false;
		}
		else
		{
			var str = $("#contact-phone"+type).val()

			if (!/[0-9+-]+/i.test(str)) {
				if(lang=="SV"){
					alert("Telefonnummer kan endast bestå av siffror samt bindestreck och plustecken");
				}
				else{
					alert("Phone number can only include digits, minus and plus");
				}
				$("#contact-phone"+type).focus();
				return false;
			}
		}		   
	}
	
	if ($("#contact-radio-email" + type).attr("checked")){
		if ($("#contact-email" + type).val()=="")	{
			if(lang=="SV"){
				alert("Du har valt att bli kontaktad via e-post men inte angett någon e-postadress");
			}
			else{
				alert("You need to enter your e-mail address");
			}
			$("#contact-email").focus();
			return false;
		}
		else{
			if(!validateEmail($("#contact-email"+type).val())){
				if(lang=="SV"){
					alert("E-postadressen är ogiltig");
				}
				else{
					alert("E-mail is invalid");
				}
				document.getElementById("contact-email"+type).focus();
				return false;
			}
		}		   
	}	
	
	
	if ($("#contact-message"+type).val()==""){
		if(lang=="SV"){
			alert("Du glömde att skriva ditt meddelande");
		}
		else{
			alert("You need to give a short information about your matter");
		}
		document.getElementById("contact-message"+type).focus();
		return false;
	}	
	return true;
}

function validateEmail(address) 
{ 
	if (/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}\b/i.test(address)) {
		return true;
	}
	return false;
} 

addLoadEvent(hideForms);
addLoadEvent(showHideSpecific);
