/**
 * File:          $Workfile: bo_application.js $"
 * Changed:       $Date: 03-12-01 14:39 $"
 * Modified:      $Modtime: 03-12-01 14:36 $"
 * Changed by:    $Author: Mats $"
 * 
 *
 *				
 * @author		Thomas Malm (TM)
 * @version		$Revision: 23 $
 *  
 * Date			Author			Changes
 * 2002-12-20	TM				Created
 */

function validateChildAge(sSSN) {
	var iYear = parseInt(sSSN.substr(0, 2));
	var dCurrentDate = new Date();
	var iCurrentYear = parseInt(dCurrentDate.getFullYear());	
	
	if (iYear < 50)
		iYear = 2000 + iYear;
	else
		iYear = 1900 + iYear;
		
	if (Math.abs(iCurrentYear - iYear) > 12)
		return false;
	else
		return true;
} 
 
function checkDate(sDate) {
	var sPattern = new RegExp("[0-9]{8}");
	var bDateOK = sPattern.test(sDate);
	return bDateOK;
}

function checkTime(sTime) {
	var sPattern = new RegExp("[0-9]{4}");
	var bTimeOK = sPattern.test(sTime);
	return bTimeOK;
}
 
function checkLength(obj)
{
	value=obj.value;
	var oForm = document.forms[0];
	if(value.length==256)
	{
		alert(oForm.errormessage7.value);
	}
	if(value.length>256)
	{
		alert(oForm.errormessage7.value);
	}	
}

//Funktion som används i bo/admin/edit_unit/edit_ward
function checkLengthLongText(obj)
{
	value=obj.value;
	var oForm = document.forms[0];
	if(value.length==2047)
	{
		alert(oForm.errormessage7.value);
	}
	if(value.length>2047)
	{
		alert(oForm.errormessage8.value);
	}
}

//Tfn.nummervalidering
function checkPhoneNumberFormat(phoneString,phoneNumberSize) {
	var pattern = new RegExp("[+,(,),0-9,/, ,-]{"+phoneNumberSize+"}");
 	var phoneNumberCheck=pattern.test(phoneString);
 	return phoneNumberCheck;
}
 
function confirmStep1() {
	var oForm = document.forms[0];
	var sChildPersonId = oForm.ChildPersonId.value;
	var sApplicantId = oForm.ApplicantId.value;
	var sCoApplicantId = oForm.CoApplicantId.value;
	
	var sErrorMessage1 = oForm.errormessage1.value;
	var sErrorMessage2 = oForm.errormessage2.value;	
	var sErrorMessage3 = oForm.errormessage3.value;	
	var sErrorMessage4 = oForm.errormessage4.value;
	var sErrorMessage5 = oForm.errormessage5.value;
	var sErrorMessage6 = oForm.errormessage6.value;
	var sErrorMessage7 = oForm.errormessage7.value;
	
	if (sChildPersonId != '' && sChildPersonId.indexOf("-") == -1) {
		sChildPersonId = sChildPersonId.substr(0, 6) + '-' + sChildPersonId.substr(6, 4);
		oForm.ChildPersonId.value = sChildPersonId;
	}
		
	if (!validateSSN(Trim(sChildPersonId))) {
		alert(sErrorMessage1);
		return;
	}
	
	if (!validateChildAge(sChildPersonId)) {
		alert(sErrorMessage4);
		return;
	}
		
	if (sApplicantId != '' && sApplicantId.indexOf("-") == -1) {
		sApplicantId = sApplicantId.substr(0, 6) + '-' + sApplicantId.substr(6, 4);
		oForm.ApplicantId.value = sApplicantId;
	}
		
	if (!validateSSN(Trim(sApplicantId))) {
		alert(sErrorMessage2);
		return;
	}
	
	if (sCoApplicantId != '' && sCoApplicantId.indexOf("-") == -1) {
		sCoApplicantId = sCoApplicantId.substr(0, 6) + '-' + sCoApplicantId.substr(6, 4);
		oForm.CoApplicantId.value = sCoApplicantId;
	}
		
	if (sCoApplicantId != '' && !validateSSN(Trim(sCoApplicantId))) {
		alert(sErrorMessage3);
		return;
	}		
	if (oForm.parent_location.value=='')
	{
		var sReferrens=document.referrer;
		while (sReferrens.indexOf("&") > -1)
		{
			sReferrens = sReferrens.replace("&","'amp;");
		}
		while (sReferrens.indexOf("'") > -1)
		{
			sReferrens = sReferrens.replace("'","&");
		}
		oForm.parent_location.value=sReferrens;
	}
	
	//Koll på att personnumrerna inte är desamma i de olika fälten
	if (sApplicantId ==  sChildPersonId) {
		alert(sErrorMessage5);
		return;
	}
	
	if (sCoApplicantId != '' && sCoApplicantId == sChildPersonId) {
		alert(sErrorMessage6);
		return;
	}
	
	if (sCoApplicantId != '' && sCoApplicantId == sApplicantId) {
		alert(sErrorMessage7);
		return;
	}
	
	//oForm.action = "debuginfo.jsp";	
	doSubmit();
}

// Koll på att alla namn och adressuppgifter börjar med en versal
function checkString(sString){

	sString.value = sString.value.substr(0, 1).toUpperCase() + sString.value.substr(1, sString.value.length);
}

function checkEmailString(sEmailString){

	sEmailString.value = sEmailString.value.toLowerCase();
}

function confirmContactInfo(sAction, sApplicationId, sCustodianInfo) {
	var oForm = document.forms[0];
	var sErrorMessage = '';
	var sChildFirstName = oForm.childsfirstname.value + '\n';
	var sChildSurName = oForm.childssurname.value + '\n';
	var sApplicantFirstName = oForm.applicantfirstname.value + '\n';
	var sApplicantSurName = oForm.applicantsurname.value + '\n';
	var sCoApplicantFirstName = oForm.coapplicantfirstname.value + '\n';
	var sCoApplicantSurName = oForm.coapplicantsurname.value + '\n';
	var sStreetAddress = oForm.streetaddress.value + '\n';
	var sZipCode = oForm.zipcode.value + '\n';
	var sCity = oForm.city.value + '\n';
	var sWork = oForm.work.value + '\n';
	var sPhoneWork = oForm.phonework.value + '\n';
	var sCoWork = oForm.cowork.value + '\n';
	var sCoPhoneWork = oForm.cophonework.value + '\n';
	var sApplEmail = oForm.applemail.value + '\n';
	var sCoApplEmail = oForm.coapplemail.value + '\n';
	var sEmailError = oForm.errormessage1.value;
	var sCoEmailError = oForm.errormessage5.value;
	var sPhoneError = oForm.text29.value + '\n\n' + oForm.text30.value + '\n' + oForm.text31.value;
	var sMessage = oForm.errormessage2.value + '\n\n';
	var sToManyCharsError;
	var sPhoneHome = oForm.homephone.value + '\n';
	
		
	if (oForm.errormessage6!=null) {
		sToManyCharsError = oForm.errormessage6.value;
	}
				
	if (oForm.ChildFirstName != null && oForm.ChildFirstName.value == '')
		sErrorMessage += sChildFirstName;

 	if (oForm.ChildSurName != null && oForm.ChildSurName.value == '')
		sErrorMessage += sChildSurName;
		
	
	if (oForm.ApplFirstName != null && oForm.ApplFirstName.value == '')
		sErrorMessage += sApplicantFirstName;
	
	if (oForm.ApplSurName != null && oForm.ApplSurName.value == '')
		sErrorMessage += sApplicantSurName;
	
	if (oForm.CoApplicantId != null && oForm.CoApplicantId.value != '') {
		if (oForm.CoApplFirstName != null && oForm.CoApplFirstName.value == '')
			sErrorMessage += sCoApplicantFirstName;
		
		if (oForm.CoApplSurName != null && oForm.CoApplSurName.value == '')
			sErrorMessage += sCoApplicantSurName;
	}
	
	if (oForm.ApplAddress.value == '')
		sErrorMessage += sStreetAddress;
	
	if (oForm.ApplZipCode.value == '')
		sErrorMessage += sZipCode;
	
	if (oForm.ApplCity.value == '')
		sErrorMessage += sCity;
	
	//Validering av sökandes hemtelefon
	if (oForm.ApplPhoneHome.value != '' && oForm.ApplPhoneHome.value != null){
		var applPhoneHomeSize = oForm.ApplPhoneHome.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.ApplPhoneHome.value,applPhoneHomeSize);
		if(formatIsValid == false) {
			alert(oForm.applhomephonemessage.value);
			return;
		}
		oForm.ApplPhoneHome.value = replaceAll(oForm.ApplPhoneHome.value, [['/', '-']]);
	}
	
	//Validering av sökandes mobiltelefon 
	if (oForm.ApplPhoneMobile.value != '' && oForm.ApplPhoneMobile.value != null){
		var applPhoneMobileSize = oForm.ApplPhoneMobile.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.ApplPhoneMobile.value,applPhoneMobileSize);
		if(formatIsValid == false) {
			alert(oForm.applmobilephonemessage.value);
			oForm.ApplPhoneMobile.focus();
			return;
		}
		//Ny formattering av mobilnumret (ED-1108)
		//Landsnummer inlagt. Sparas enl. formatet +46 70-123 45 67
		if(oForm.ApplPhoneMobile.value.substr(0,2) == '00') {
			oForm.ApplPhoneMobile.value = '+' + oForm.ApplPhoneMobile.value.substr(2,oForm.ApplPhoneMobile.value.length-1)
		}
		var appl_stripped = oForm.ApplPhoneMobile.value.replace(/[\(\)\.\-\+\/\ ]/g, '');
		if(oForm.ApplPhoneMobile.value.charAt(0) == '+'){
			if(appl_stripped.length != 11) {
				alert(oForm.applmobilephonenumbermessage.value);
				oForm.ApplPhoneMobile.focus();
				return;
			}
			else {
				oForm.ApplPhoneMobile.value = '+' + appl_stripped.substr(0,2) + ' ' + appl_stripped.substr(2,2) + '-' + appl_stripped.substr(4,3) + ' ' + appl_stripped.substr(7,2) + ' '+ appl_stripped.substr(9,2);
			}
		}
		//Sparas enl. formatet 070-123 45 67
		else {
			if(appl_stripped.length != 10) {
				alert(oForm.applmobilephonenumbermessage.value);
				oForm.ApplPhoneMobile.focus();
				return;
			}
			else {
				oForm.ApplPhoneMobile.value = appl_stripped.substr(0,3) + '-' + appl_stripped.substr(3,3) + ' ' + appl_stripped.substr(6,2) + ' '+ appl_stripped.substr(8,2);
			}
		}
	}
	
	if (oForm.compwork.value == 'Y')
	{
		if (oForm.ApplWork.value=='')
			sErrorMessage += sWork;
	}
	
	//Validering av sökandes tfn arbete
	if (oForm.ApplPhoneWork.value != '' && oForm.ApplPhoneWork.value != null){
		var applPhoneWorkSize = oForm.ApplPhoneWork.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.ApplPhoneWork.value,applPhoneWorkSize);
		if(formatIsValid == false) {
			alert(oForm.applworkphonemessage.value);
			return;
		}
		oForm.ApplPhoneWork.value = replaceAll(oForm.ApplPhoneWork.value, [['/', '-']]);
	}
	
	if (oForm.compphonehome.value == 'Y')
	{
		if (oForm.ApplPhoneHome.value=='')
			sErrorMessage += sPhoneHome;
	}
		
	if (oForm.compphonework.value == 'Y')
	{
		if (oForm.ApplPhoneWork.value=='')
			sErrorMessage += sPhoneWork;
	}
	
	if (oForm.CoApplicantId != null && oForm.CoApplicantId.value != '')
	{
		
		//Validering av medsökandes mobiltelefon 
		if (oForm.CoApplPhoneMobile.value != '' && oForm.CoApplPhoneMobile.value != null){
			var coApplPhoneMobileSize = oForm.CoApplPhoneMobile.value.length;
			formatIsValid = checkPhoneNumberFormat(oForm.CoApplPhoneMobile.value,coApplPhoneMobileSize);
			if(formatIsValid == false) {
				alert(oForm.coapplmobilephonemessage.value);
				oForm.CoApplPhoneMobile.focus();
				return;
			}
			//Ny formattering av mobilnumret (ED-1108)
			//Landsnummer inlagt. Sparas enl. formatet +46 70-123 45 67
			if(oForm.CoApplPhoneMobile.value.substr(0,2) == '00') {
				oForm.CoApplPhoneMobile.value = '+' + oForm.CoApplPhoneMobile.value.substr(2,oForm.CoApplPhoneMobile.value.length-1)
			}
			var coappl_stripped = oForm.CoApplPhoneMobile.value.replace(/[\(\)\.\-\+\/\ ]/g, '');
			if(oForm.CoApplPhoneMobile.value.charAt(0) == '+'){
				if(coappl_stripped.length != 11) {
					alert(oForm.coapplmobilephonenumbermessage.value);
					oForm.CoApplPhoneMobile.focus();
					return;
				}
				else {
					oForm.CoApplPhoneMobile.value = '+' + coappl_stripped.substr(0,2) + ' ' + coappl_stripped.substr(2,2) + '-' + coappl_stripped.substr(4,3) + ' ' + coappl_stripped.substr(7,2) + ' '+ coappl_stripped.substr(9,2);
				}
			}
			//Sparas enl. formatet 070-123 45 67
			else {
				if(coappl_stripped.length != 10) {
					alert(oForm.coapplmobilephonenumbermessage.value);
					oForm.CoApplPhoneMobile.focus();
					return;
				}
				else {
					oForm.CoApplPhoneMobile.value = coappl_stripped.substr(0,3) + '-' + coappl_stripped.substr(3,3) + ' ' + coappl_stripped.substr(6,2) + ' '+ coappl_stripped.substr(8,2);
				}
			}
		}
		
		if (oForm.compcowork.value == 'Y')
		{
			if (oForm.CoApplWork.value=='')
				sErrorMessage += sCoWork;
		}
		
		//Validering av medsökandes tfn arbete
		if (oForm.CoApplPhoneWork.value != '' && oForm.CoApplPhoneWork.value != null){
			var coApplPhoneWorkSize = oForm.CoApplPhoneWork.value.length;
			formatIsValid = checkPhoneNumberFormat(oForm.CoApplPhoneWork.value,coApplPhoneWorkSize);
			if(formatIsValid == false) {
				alert(oForm.coapplworkphonemessage.value);
				return;
			}
			oForm.CoApplPhoneWork.value = replaceAll(oForm.CoApplPhoneWork.value, [['/', '-']]);
		}
		
		if (oForm.compcophonework.value == 'Y')
		{
			if (oForm.CoApplPhoneWork.value=='')
				sErrorMessage += sCoPhoneWork;
		}	
	}
	
	if (oForm.compemail.value == 'Y')
	{
		if (oForm.ApplEmail.value=='')
			sErrorMessage += sApplEmail;
	}
	
	if (oForm.CoApplicantId != null && oForm.CoApplicantId.value != '')
	{
		if (oForm.compcoemail.value == 'Y')
		{
			if (oForm.CoApplEmail.value=='')
				sErrorMessage += sCoApplEmail;
		}		
	}
		
	//Validering av vårdnadshavarens hemtelefon
	// Kan bara vara aktuellt när man editerar kontaktuppgifterna
	if (oForm.CuApplPhoneHome.value != '' && oForm.CuApplPhoneHome.value != null){
		var cuApplPhoneHomeSize = oForm.CuApplPhoneHome.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.CuApplPhoneHome.value,cuApplPhoneHomeSize);
		if(formatIsValid == false) {
			alert(oForm.cuapplhomephonemessage.value);
			return;
		}
	}
	
	//Validering av vårdnadshavarens mobiltelefon 
	if (oForm.CuApplPhoneMobile.value != '' && oForm.CuApplPhoneMobile.value != null){
		var cuApplPhoneMobileSize = oForm.CuApplPhoneMobile.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.CuApplPhoneMobile.value,cuApplPhoneMobileSize);
		if(formatIsValid == false) {
			alert(oForm.cuapplmobilephonemessage.value);
			oForm.CuApplPhoneMobile.focus();
			return;
		}
		//Ny formattering av mobilnumret (ED-1108)
			//Landsnummer inlagt. Sparas enl. formatet +46 70-123 45 67
			if(oForm.CuApplPhoneMobile.value.substr(0,2) == '00') {
				oForm.CuApplPhoneMobile.value = '+' + oForm.CuApplPhoneMobile.value.substr(2,oForm.CuApplPhoneMobile.value.length-1)
			}
			var cuappl_stripped = oForm.CuApplPhoneMobile.value.replace(/[\(\)\.\-\+\/\ ]/g, '');
			if(oForm.CuApplPhoneMobile.value.charAt(0) == '+'){
				if(cuappl_stripped.length != 11) {
					alert(oForm.cuapplmobilephonenumbermessage.value);
					oForm.CuApplPhoneMobile.focus();
					return;
				}
				else {
					oForm.CuApplPhoneMobile.value = '+' + cuappl_stripped.substr(0,2) + ' ' + cuappl_stripped.substr(2,2) + '-' + cuappl_stripped.substr(4,3) + ' ' + cuappl_stripped.substr(7,2) + ' '+ cuappl_stripped.substr(9,2);
				}
			}
			//Sparas enl. formatet 070-123 45 67
			else {
				if(cuappl_stripped.length != 10) {
					alert(oForm.cuapplmobilephonenumbermessage.value);
					oForm.CuApplPhoneMobile.focus();
					return;
				}
				else {
					oForm.CuApplPhoneMobile.value = cuappl_stripped.substr(0,3) + '-' + cuappl_stripped.substr(3,3) + ' ' + cuappl_stripped.substr(6,2) + ' '+ cuappl_stripped.substr(8,2);
				}
			}
	}
			
	//Validering av vårdnadshavarens tfn arbete
	if (oForm.CuApplPhoneWork.value != '' && oForm.CuApplPhoneWork.value != null){
		var cuApplPhoneWorkSize = oForm.CuApplPhoneWork.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.CuApplPhoneWork.value,cuApplPhoneWorkSize);
		if(formatIsValid == false) {
			alert(oForm.cuapplworkphonemessage.value);
			return;
		}
	}
	
	if (oForm.InfoCustodian!=null)
	{
		if (oForm.InfoCustodian.value!=null && oForm.InfoCustodian.value!='')	
		{
			if (oForm.InfoCustodian.value.length>256)
			{
				alert(sToManyCharsError);
				return;
			}	
		}
	}
	
	if (sErrorMessage != '')
		alert(sMessage + sErrorMessage);
	else 
	{
		if (oForm.ApplEmail.value != '' && !isEmailAddressValid(oForm.ApplEmail.value)) {
			alert(sEmailError);
			return;
		}
		
		if (oForm.CoApplicantId != null && oForm.CoApplicantId.value != '')
		{
			if (oForm.CoApplEmail.value != '' && !isEmailAddressValid(oForm.CoApplEmail.value)) 
			{
				alert(sCoEmailError);
				return;
			}
		}
		
		if (oForm.showphonehome.value == 'Y' && oForm.compphonehome.value != 'Y' && oForm.ApplPhoneHome.value == '' && oForm.ApplPhoneMobile.value == '') {
			if (confirm(sPhoneError)) {
				if (sAction == 'insertcontactinfo'){
					// Går till vårdnadshavarsidan
					if (sCustodianInfo == 'oneonly' || sCustodianInfo == 'existsdb' || sCustodianInfo == 'notexistsdb' || sCustodianInfo == 'otherpersonexistsnoaddress' || sCustodianInfo == 'infomissing') {
						oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&CustodianInfo=' + sCustodianInfo + '&saveaction=insertcontactinfo&redirectpage=bo/bas/application_custodianinfo';
					}	
					// Går vidare direkt till steg 3
					if (sCustodianInfo == 'both' || sCustodianInfo == 'infoexists' || sCustodianInfo == 'otherpersonexistsaddress' || sCustodianInfo == 'cancelled'){
					oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&CustodianInfo=' + sCustodianInfo + '&saveaction=insertcontactinfo&redirectpage=bo/bas/application$step3.xsl';
					}
				}
				else if (sAction == 'updatecontactinfo')
					oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&saveaction=updatecontactinfo&redirectpage=bo/bas/application$step45.xsl';			
				
				doSubmit();				
			}
		}
		else {
			if (sAction == 'insertcontactinfo'){
				// Går till vårdnadshavarsidan
				if (sCustodianInfo == 'oneonly' || sCustodianInfo == 'existsdb' || sCustodianInfo == 'notexistsdb' || sCustodianInfo == 'otherpersonexistsnoaddress' || sCustodianInfo == 'infomissing') {
						oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&CustodianInfo=' + sCustodianInfo + '&saveaction=insertcontactinfo&redirectpage=bo/bas/application_custodianinfo';
					}	
					// Går vidare direkt till steg 3
					if (sCustodianInfo == 'both' || sCustodianInfo == 'infoexists'  || sCustodianInfo == 'otherpersonexistsaddress' || sCustodianInfo == 'cancelled'){
					oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&CustodianInfo=' + sCustodianInfo + '&saveaction=insertcontactinfo&redirectpage=bo/bas/application$step3.xsl';
					}
			}
			else if (sAction == 'updatecontactinfo')
				oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&saveaction=updatecontactinfo&redirectpage=bo/bas/application$step45.xsl';			
			
			doSubmit();
		}
	}

}

function updateCustodianInfo(sAction, sApplicationId) {
	var oForm = document.forms[0];
	var sCuEmailError = oForm.errormessage1.value;
	var sToManyCharsError;
	
	
	//Validering av vårdnadshavarens hemtelefon
	if (oForm.CuApplPhoneHome.value != '' && oForm.CuApplPhoneHome.value != null){
		var cuApplPhoneHomeSize = oForm.CuApplPhoneHome.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.CuApplPhoneHome.value,cuApplPhoneHomeSize);
		if(formatIsValid == false) {
			alert(oForm.cuapplhomephonemessage.value);
			return;
		}
	}
	
	//Validering av vårdnadshavarens mobiltelefon 
	if (oForm.CuApplPhoneMobile.value != '' && oForm.CuApplPhoneMobile.value != null){
		var cuApplPhoneMobileSize = oForm.CuApplPhoneMobile.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.CuApplPhoneMobile.value,cuApplPhoneMobileSize);
		if(formatIsValid == false) {
			alert(oForm.cuapplmobilephonemessage.value);
			return;
		}
	}
			
	//Validering av vårdnadshavarens tfn arbete
	if (oForm.CuApplPhoneWork.value != '' && oForm.CuApplPhoneWork.value != null){
		var cuApplPhoneWorkSize = oForm.CuApplPhoneWork.value.length;
		formatIsValid = checkPhoneNumberFormat(oForm.CuApplPhoneWork.value,cuApplPhoneWorkSize);
		if(formatIsValid == false) {
			alert(oForm.cuapplworkphonemessage.value);
			return;
		}
	}
	
	if (oForm.errormessage6!=null)
	{
		sToManyCharsError = oForm.errormessage6.value;
	}
	
		if (oForm.CuApplEmail.value != '' && !isEmailAddressValid(oForm.CuApplEmail.value)) {
			alert(sCuEmailError);
			return;
		}
	
	if (oForm.InfoCustodian!=null)
	{
		if (oForm.InfoCustodian.value!=null && oForm.InfoCustodian.value!='')	
		{
			if (oForm.InfoCustodian.value.length>256)
			{
				alert(sToManyCharsError);
				return;	
			}	
		}
	}
	
		oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId +  '&saveaction=updatecontactinfo&redirectpage=bo/bas/application$step3.xsl';
		
		doSubmit();

}

function changeCheckBox1(){
	var oForm = document.forms[0];
	
	if(oForm.Opt1.value !== '' && oForm.Opt1.value !== null)
	{
		if(oForm.Opt1.value == 0)
			oForm.Opt1.value = 1;
		else
			oForm.Opt1.value = 0;
	}	
}

function changeCheckBox2(){
	var oForm = document.forms[0];
	
	if(oForm.Opt2.value !== '' && oForm.Opt2.value !== null)
	{
		if(oForm.Opt2.value == 0)
			oForm.Opt2.value = 1;
		else
			oForm.Opt2.value = 0;
	}	
}

function changeCheckBox3(){
	var oForm = document.forms[0];
	
	if(oForm.Opt3.value !== '' && oForm.Opt3.value !== null)
	{
		if(oForm.Opt3.value == 0)
			oForm.Opt3.value = 1;
		else
			oForm.Opt3.value = 0;
	}	
}

function confirmGeneralInfo(sApplicationId) {
	var oForm = document.forms[0];
	
	var sReqDateOfPlac = oForm.tmpReqDateOfPlac.value;
	var iAvHrPerWeek = oForm.tmpAvHrPerWeek.value;
	var sEarlyArrival = oForm.tmpEarlyArrival.value;
	var sLateDeparture = oForm.tmpLateDeparture.value;
	var sOtherInfo = oForm.OtherInfo.value;
	var sEarliestPlacDate=oForm.earliestplacdate.value;
	var ToEarly;
	var sEarliestPlacError;
	
	var sErrorMessage1 = oForm.errormessage1.value;
	var sErrorMessage2 = oForm.errormessage2.value;
	var sErrorMessage3 = oForm.errormessage3.value;
	var sErrorMessage4 = oForm.errormessage4.value;
	var sErrorMessage5 = oForm.errormessage5.value;
	var sErrorMessage6  =oForm.errormessage6.value;
	var sErrorMessage7 = oForm.errormessage7.value;
	var sErrorMessage8 = oForm.errormessage8.value;	
	var sErrorMessage9 = oForm.errormessage9.value;
	var sErrorMessage10 = oForm.errormessage10.value;
	var sErrorMessage11 = oForm.errormessage11.value;
	var sErrorMessage12 = oForm.errormessage12.value;
	var sErrorMessage13 = oForm.errormessage13.value;
	var sErrorMessage15 = oForm.errormessage15.value;
	var sErrorMessage16 = oForm.errormessage16.value;
	var sErrorMessage17 = oForm.errormessage17.value;
	var sOpt1Answer = oForm.Opt1.value;
	var sOpt2Answer = oForm.Opt2.value;
	var sOpt3Answer = oForm.Opt3.value;
	var sReasonForCoAppl = oForm.ReasonForCoAppl.value;
	
		
	if(oForm.Opt1Question.value != '' && oForm.Opt1Question.value != null)
	{
		if (oForm.Opt1.checked)
			sOpt1Answer = "1";
	}
			
	if(oForm.Opt2Question.value != '' && oForm.Opt2Question.value != null)
	{
		if (oForm.Opt2.checked)
			sOpt2Answer = "1";
	}
			
	if(oForm.Opt3Question.value != '' && oForm.Opt3Question.value != null)
	{	
		if (oForm.Opt3.checked)
				sOpt3Answer = "1";
	}
	
			
	if (oForm.compmothertongue.value == 'Y')
	{
		if (oForm.fk_MotherTongue.options[oForm.fk_MotherTongue.selectedIndex].value=='')
			{alert(sErrorMessage8);
		return;}
	}
	//Grund för placering - sökande
	if (oForm.compreasonforplac.value == 'Y')
	{
		if (oForm.fk_ReasonForPlac.options[oForm.fk_ReasonForPlac.selectedIndex].value=='')
		{
			alert(sErrorMessage9);
			return;
		}
	}
	//Grund för placering - medsökande
	if (oForm.CoApplicantId != null && oForm.CoApplicantId.value != '' && oForm.compcoreasonforplac.value == 'Y')
	{
		if (oForm.fk_CoReasonForPlac.options[oForm.fk_CoReasonForPlac.selectedIndex].value=='')
		{
			alert(sReasonForCoAppl);
			return;
		}
	}
	
	//Datumvalidering
	formatIsValid = checkDateFormat(sReqDateOfPlac);

	if(formatIsValid == false) {
		alert(sErrorMessage1);
		return;
	}

	//Kontrollera så att placeringsdatum ej är för tidigt
	
	if (sEarliestPlacDate=='' || sEarliestPlacDate==null)	{
		sEarliestPlacDate='0';
		sEarliestPlacError=sErrorMessage16;
	}
	else {
		sEarliestPlacError=sErrorMessage15;
	}
	
	// Det görs ingen check om användaren kommer in via Köstatus
	if(oForm.ChildQueueId.value == '' || oForm.ChildQueueId.value == null) {	
		ToEarly=CheckPlaceDate(sReqDateOfPlac, sEarliestPlacDate);
		if (ToEarly==false)
		{
			alert(sEarliestPlacError);
			return;
		}
	}
	
	//Kan inte ansöka om plats längre fram än i år och nästa år.
	if (oForm.maxaheaddate.value!='')
	{
		TooFarAhead = CheckAHead(sReqDateOfPlac,parseInt(oForm.maxaheaddate.value)); 
	
		if (TooFarAhead)	{
			alert(sErrorMessage5);
			return;
		}	
	}
	if (oForm.compavhrperweek.value == 'Y')
	{
		if (iAvHrPerWeek=='')
		{
			alert(sErrorMessage10);
			return;
		}
	}
	
	if (iAvHrPerWeek != '' && isNaN(iAvHrPerWeek)) {
		alert(sErrorMessage2);
		return;
	}

	if (oForm.compearlyarrival.value == 'Y')
	{
		if (sEarlyArrival=='')
		{
			alert(sErrorMessage11);
			return;
		}
	}

	if (sEarlyArrival != '' && !checkTime(sEarlyArrival)) {
		alert(sErrorMessage3);
		return;
	}

	if (oForm.complatedeparture.value == 'Y')
	{
		if (sLateDeparture=='')
		{
			alert(sErrorMessage12);
			return;
		}
	}

	if (sLateDeparture != '' && !checkTime(sLateDeparture)) {
		alert(sErrorMessage4);
		return;
	}	

	if (oForm.compotherinfo.value == 'Y')
	{
		if (sOtherInfo.length<1)
		{
			alert(sErrorMessage13);
			return;
		}
	}	
	
	if (oForm.Question1Answer!=null)
	{
		if (oForm.Question1Answer.value!=null && oForm.Question1Answer.value!='')	
		{
			if (oForm.Question1Answer.value.length>256)
			{
				alert(oForm.Question1.value + sErrorMessage17);
				return;	
			}
			// Ta bort dubbelfnuttar, fnuttar och apostrofer i svaren - bc 22663
			oForm.Question1Answer.value = replaceAll(oForm.Question1Answer.value, [['\'', '']]);
			oForm.Question1Answer.value = replaceAll(oForm.Question1Answer.value, [['´', '']]);
			oForm.Question1Answer.value = replaceAll(oForm.Question1Answer.value, [['"', '']]);
		}
	}	
	
	if (oForm.Question2Answer!=null)
	{
		if (oForm.Question2Answer.value!=null && oForm.Question2Answer.value!='')	
		{
			if (oForm.Question2Answer.value.length>256)
			{
				alert(oForm.Question2.value + sErrorMessage17);
				return;	
			}
			// Ta bort dubbelfnuttar, fnuttar och apostrofer i svaren - bc 22663
			oForm.Question2Answer.value = replaceAll(oForm.Question2Answer.value, [['\'', '']]);
			oForm.Question2Answer.value = replaceAll(oForm.Question2Answer.value, [['´', '']]);
			oForm.Question2Answer.value = replaceAll(oForm.Question2Answer.value, [['"', '']]);	
		}
	}
	
	if (oForm.Question3Answer!=null)
	{
		if (oForm.Question3Answer.value!=null && oForm.Question3Answer.value!='')	
		{
			if (oForm.Question3Answer.value.length>256)
			{
				alert(oForm.Question3.value + sErrorMessage17);
				return;	
			}	
			// Ta bort dubbelfnuttar, fnuttar och apostrofer i svaren - bc 22663
			oForm.Question3Answer.value = replaceAll(oForm.Question3Answer.value, [['\'', '']]);
			oForm.Question3Answer.value = replaceAll(oForm.Question3Answer.value, [['´', '']]);
			oForm.Question3Answer.value = replaceAll(oForm.Question3Answer.value, [['"', '']]);
		}
	}
	
	if (oForm.Question4Answer!=null)
	{
		if (oForm.Question4Answer.value!=null && oForm.Question4Answer.value!='')	
		{
			if (oForm.Question4Answer.value.length>256)
			{
				alert(oForm.Question4.value + sErrorMessage17);
				return;	
			}
			// Ta bort dubbelfnuttar, fnuttar och apostrofer i svaren - bc 22663
			oForm.Question4Answer.value = replaceAll(oForm.Question4Answer.value, [['\'', '']]);
			oForm.Question4Answer.value = replaceAll(oForm.Question4Answer.value, [['´', '']]);
			oForm.Question4Answer.value = replaceAll(oForm.Question4Answer.value, [['"', '']]);	
		}
	}
	
	if (sOtherInfo!='')
	{	
		if (sOtherInfo.length>256) 
		{
			alert(sErrorMessage6);
			return;
		}
		// Ta bort dubbelfnuttar, fnuttar och apostrofer i svaren - bc 22663
		oForm.OtherInfo.value = replaceAll(oForm.OtherInfo.value, [['\'', '']]);
		oForm.OtherInfo.value = replaceAll(oForm.OtherInfo.value, [['´', '']]);
		oForm.OtherInfo.value = replaceAll(oForm.OtherInfo.value, [['"', '']]);
	}
		
	if (iAvHrPerWeek == '')
		iAvHrPerWeek = 0;
	
	if (iAvHrPerWeek > 168){
		alert(oForm.toomanyhours.value);
		return;
	}
		

	if (sEarlyArrival != '')
		sEarlyArrival = sEarlyArrival.substr(0, 2) + ':' + sEarlyArrival.substr(2, 2) + ':00';

	if (sLateDeparture != '')
		sLateDeparture = sLateDeparture.substr(0, 2) + ':' + sLateDeparture.substr(2, 2) + ':00';
	
	//oForm.action = "debuginfo.jsp";			
	oForm.action = 'Default.asp?page=bo/bas/application&ReqDateOfPlac=' + sReqDateOfPlac + '&AvHrPerWeek=' + iAvHrPerWeek + '&EarlyArrival=' + sEarlyArrival + '&LateDeparture=' + sLateDeparture + '&ApplicationId=' + sApplicationId + '&Opt1Answer=' + sOpt1Answer + '&Opt2Answer=' + sOpt2Answer + '&Opt3Answer=' + sOpt3Answer + '&saveaction=updategeneralinfo&redirectpage=bo/bas/application$step45.xsl';
	
	doSubmit();
}

function checkDistrict(sApplicationId) {
	var form = document.forms[0];
	var districtId = form.DistrictId.value;
	
	disableElements("select-one", form.DistrictId);
	disableElements("button", null);

	form.action += '&ApplicationId=' + sApplicationId + '&qDistrictId=' + districtId;	
	submitForm(0);
}

function checkArea(sApplicationId) {
	var form = document.forms[0];
	var areaId = form.AreaId.value;
	
	disableElements("select-one", form.AreaId);
	disableElements("button", null);
	
	form.action += '&ApplicationId=' + sApplicationId + '&qAreaId=' + areaId;
	submitForm(0);
}

function checkTypeOfChildCare(sApplicationId) {
	var form = document.forms[0];
	var typeofchildcareId = form.TypeOfChildCareId.value;
	var districtId = form.DistrictId.value;
	var areaId = form.AreaId.value;
	
	disableElements("select-one", form.TypeOfChildCareId);
	disableElements("button", null);
	
	//form.action += "&qTypeOfChildCareId=" + typeofchildcareId;
	form.action += '&ApplicationId=' + sApplicationId + '&qTypeOfChildCareId=' + typeofchildcareId + '&qDistrictId=' + districtId + '&qAreaId=' + areaId;
	submitForm(0);
}

function checkUnit(sApplicationId) {
	var form = document.forms[0];
	var unitId = form.UnitId.value;
	//var varunitId = form.UnitTypeId.value;
	var typeofchildcareId = form.TypeOfChildCareId.value;
	
	disableElements("select-one", form.UnitId);
	disableElements("button", null);
	
  form.action += '&ApplicationId=' + sApplicationId + '&qUnitId=' + unitId + '&qTypeOfChildCareId=' + typeofchildcareId;
	submitForm(0);
 
 }

function checkWard(sApplicationId) {
	var form = document.forms[0];
	var wardId = form.WardId.value;
	var unitId = form.UnitId.value;
	var typeofchildcareId = form.TypeOfChildCareId.value;
	
	disableElements("select-one", form.WardId);
	disableElements("button", null);
	
	form.action += '&ApplicationId=' + sApplicationId + '&qWardId=' + wardId + '&qUnitId=' + unitId + '&qTypeOfChildCareId=' + typeofchildcareId;
	submitForm(0);
}


function confirmPlacAlt(sAction, sApplicationId, sPlacAltId) {
	var oForm = document.forms[0];
	
	var sArea = oForm.AreaId.value;
	var sReqDateOfPlac = oForm.tmpReqDateOfPlac.value;
	var iAvHrPerWeek = oForm.tmpAvHrPerWeek.value;
	var sOtherInfo = oForm.OtherInfo.value;
	var sEarliestPlacDate=oForm.earliestplacdate.value;
	var ToEarly;
	var sEarliestPlacError;
	
	var sErrorMessage1 = oForm.errormessage1.value;
	var sErrorMessage2 = oForm.errormessage2.value;
	var sErrorMessage3 = oForm.errormessage3.value;
	var sErrorMessage4 = oForm.errormessage4.value;  		
	var sErrorMessage5 = oForm.errormessage5.value;
	var sErrorMessage6 = oForm.errormessage6.value;
	var sErrorMessage7 = oForm.errormessage7.value;
	var sErrorMessage8 = oForm.errormessage8.value;
	var sErrorMessage9 = oForm.errormessage9.value;
	var sErrorMessage10 = oForm.errormessage10.value;
	var sErrorMessage15 = oForm.errormessage15.value;
	var sErrorMessage16 = oForm.errormessage16.value;
	
	if (oForm.ShowDistrict.value == 'Y' && oForm.DistrictId.value == '') {
		alert(sErrorMessage1);
		return;
	}
	
	if (sArea == '') {
		alert(sErrorMessage2);
		return;
	}
	
	//Om enhet är obligatorisk
	if (oForm.UnitIsCompulsory.value == 'Y' && oForm.UnitId.value == '') {
		alert(sErrorMessage8);
		return;
	}
	
	//Om avdelning är obligatorisk
	if (oForm.WardIsCompulsory.value == 'Y' && oForm.WardId.value == '' && oForm.ShowWardApplication.value == 'Y') {
		alert(sErrorMessage9);
		return;
	}
	
	//Datumvalidering	
	formatIsValid = checkDateFormat(sReqDateOfPlac);
		
	if(formatIsValid == false) {
		alert(sErrorMessage3);
		return;
	}
	
	//Kontrollera så att placeringsdatum ej är för tidigt
	if (sEarliestPlacDate=='' || sEarliestPlacDate==null)
	{
		sEarliestPlacDate='0';
		sEarliestPlacError=sErrorMessage16;
	}
	else
	{
		sEarliestPlacError=sErrorMessage15;
	}
	
	// Det görs ingen check om användaren kommer in via Köstatus
	if(oForm.ChildQueueId.value == '' || oForm.ChildQueueId.value == null) {
		ToEarly=CheckPlaceDate(sReqDateOfPlac, sEarliestPlacDate);
		if (ToEarly==false)	{
			alert(sEarliestPlacError);
			return;
		}
	}
	//Kan inte ansöka om plats längre fram än i år och nästa år.
	if (oForm.maxaheaddate.value!='')
	{
		TooFarAhead = CheckAHead(sReqDateOfPlac,parseInt(oForm.maxaheaddate.value)); 
	
		if (TooFarAhead)	{
			alert(sErrorMessage5);
			return;
		}	
	}
	
	if (oForm.ShowAvHrPerWeek.value == 'Y' && iAvHrPerWeek != '' && isNaN(iAvHrPerWeek)) {
		alert(sErrorMessage4);
		return;
	}	
	
	
	if (sOtherInfo!='')
	{
		if (sOtherInfo.length>=256) 
		{
			alert(sErrorMessage6);
			return;
		}
		// Ta bort dubbelfnuttar, fnuttar och apostrofer i svaren - bc 22663
		oForm.OtherInfo.value = replaceAll(oForm.OtherInfo.value, [['\'', '']]);
		oForm.OtherInfo.value = replaceAll(oForm.OtherInfo.value, [['´', '']]);
		oForm.OtherInfo.value = replaceAll(oForm.OtherInfo.value, [['"', '']]);
	}
	
	if (oForm.compavhrperweek.value == 'Y')
	{
		if (iAvHrPerWeek=='')
		{
			alert(sErrorMessage10);
			return;
		}
	}
	
	if (iAvHrPerWeek > 168){
		alert(oForm.toomanyhours.value);
		return;
	}
			
	if (iAvHrPerWeek == '')
		iAvHrPerWeek = 0;
			
	if (sAction == 'insertplacalt')
		oForm.action = 'Default.asp?page=bo/bas/application&ReqDateOfPlac=' + sReqDateOfPlac + '&AvHrPerWeek=' + iAvHrPerWeek + '&ApplicationId=' + sApplicationId + '&saveaction=' + sAction + '&redirectpage=bo/bas/application$step45.xsl';
		
	else if (sAction == 'updateplacalt')
		oForm.action = 'Default.asp?page=bo/bas/application&ReqDateOfPlac=' + sReqDateOfPlac + '&AvHrPerWeek=' + iAvHrPerWeek + '&ApplicationId=' + sApplicationId + '&PlacAltId=' + sPlacAltId + '&saveaction=' + sAction + '&redirectpage=bo/bas/application$step45.xsl';
	
	doSubmit();	
}

function deletePlacAlt(sApplicationId, sPlacAltId, iAltNo) {
	var oForm = document.forms[0];
	
	var sMessage1 = oForm.message1.value + ' ';

	if (confirm(sMessage1 + iAltNo + '?')) {
		oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&PlacAltId=' + sPlacAltId + '&saveaction=deleteplacalt&redirectpage=bo/bas/application$step45.xsl';
		doSubmit();
	}
}

function confirmApplication(sApplicationId, sStatus) {
	var oForm = document.forms[0];
	var sEmail = oForm.Email.value;
	
	var sMessage2 = oForm.message2.value;
	var sMsgToManyAlt = oForm.MsgToManyAlt.value;
	var iMaxAlt = oForm.MaxNoAlt.value;
	var iChoosenAlt = oForm.ChoosenNoAlt.value;
	
	if (iMaxAlt!='' && eval(iChoosenAlt) > eval(iMaxAlt))
	{
		alert(sMsgToManyAlt);
		return;	
	}
	
	
	if (sStatus == '')
		sStatus = 'NEW';
	else
		sStatus = 'UPDATE';
	
	var sAction = 'Default.asp?page=bo/bas/application&ApplicationId=' + sApplicationId + '&Status=' + sStatus + '&saveaction=updateapplication&redirectpage=bo/bas/application$step45.xsl';
	
	if (confirm(sMessage2)) {
		if (sEmail != '')
			sAction += '&Send=yes&email=' + sEmail;
		oForm.action = sAction;
		oForm.check_back.value=1;
		doSubmit();
	}
}

function changeApplication() {
	var oForm = document.queueplace;
	
	//oForm.action="debuginfo.jsp";
	oForm.action = 'Default.asp?page=bo/bas/application&saveaction=insertapplication&redirectpage=bo/bas/application$step45.xsl';
	submitForm(1);
}


function addField (form, fieldName, fieldValue) {
  if (document.getElementById) {
    var input = document.createElement('INPUT');
      if (document.all) { // what follows should work 
                          // with NN6 but doesn't in M14
        input.type = "hidden";
        input.name = fieldName;
        input.value = fieldValue;
      }
      else if (document.getElementById) { // so here is the
                                          // NN6 workaround
        input.setAttribute('type', "hidden");
        input.setAttribute('name', fieldName);
        input.setAttribute('value', fieldValue);
      }
    form.appendChild(input);
  }
}

function confirmPlacaltEliminate()
{
	var oForm = document.placalt;
	var oForm2 = document.queueplace;
	var counter_placalt = oForm2.counterPlacAlt.value;
	var i = 0;
	var counter_checked=0;
	var sMessageEliminateQueuePlaceConfirm = oForm2.messageEliminateConfirm.value;
	var sMessageEliminateApplConfirm = oForm2.messageEliminateApplConfirm.value;
	var sMessageRollBackConfirm = oForm2.messageRollBackConfirm.value;

	for (i=0;i<counter_placalt;i++)
	{
		var obj = eval("oForm.EliminateCheck" + (i+1));
		if (obj.checked)
		{
			counter_checked += 1;
			addField(oForm2,'Eliminate_QATId$' + counter_checked, eval("oForm2.QueueApplTemplId$" + (i+1)).value);
			addField(oForm2,'Eliminate_StopDate$' + counter_checked, eval("oForm2.EndDate").value);
		}
	}
	addField(oForm2,'Eliminate_QATCount', counter_checked);
	if (counter_checked == counter_placalt)
	{
		if (confirm(sMessageEliminateQueuePlaceConfirm)) {
			oForm2.action = 'Default.asp?page=bo/bas/application&saveaction=eliminateapplication&redirectpage=bo/bas/application_queuestatus';
			submitForm(1);
		}
	}
	if (counter_checked < counter_placalt && counter_checked > 0)
	{
		if (confirm(sMessageEliminateApplConfirm)) {
			oForm2.action = 'Default.asp?page=bo/bas/application&saveaction=eliminateplacalt&redirectpage=bo/bas/application_queuestatus';
			submitForm(1);
		}
	}
	if (counter_checked == 0)
	{
		if (confirm(sMessageRollBackConfirm)) {
			oForm2.action = 'Default.asp?page=bo/bas/application&saveaction=rollbackapplication&redirectpage=bo/bas/application_queuestatus';
			submitForm(1);
		}
	}
}

function deleteApplication() {
	var oForm = document.queueplace;
	var sMessage = oForm.message1.value;
	var sChildQueueId = oForm.ChildQueueId.value;
			
	if (confirm(sMessage)) {
		oForm.action = 'Default.asp?page=bo/bas/application&saveaction=deleteapplication&redirectpage=bo/bas/application_queuestatus';
		submitForm(1);
	}
}

function confirmCancel() {
	var oForm = document.forms[0];
	var sCancelMessage = oForm.cancelmessage1.value + '\n\n' + oForm.cancelmessage2.value + '\n'+ oForm.cancelmessage3.value;
			
	if (confirm(sCancelMessage)) 
	window.location='Default.asp?page=auth/common/startpage';
}

//ändra < och > till &lt; och &gt; vid MarkUp fält
function changeCharUnit()
{
		var oForm = document.forms[0];
		
		if (oForm.Welcometext.value.length>255)
		{
			alert(oForm.errormessage9.value);
			return false;	
		}
		if (oForm.OtherInfo.value.length>2047)
		{
			alert(oForm.errormessage10.value);
			return false;	
		}	
		var txtWelcome = oForm.Welcometext.value;
		txtWelcome=changeCharacter(txtWelcome);
		if (txtWelcome=='false')
		{
			alert(oForm.errormessage11.value);
			return false;	
		}
		else
		{
			oForm.Welcometext.value=txtWelcome;
		}
		var txtOther = oForm.OtherInfo.value;
		txtOther=changeCharacter(txtOther);
		if (txtOther=='false')
		{
			alert(oForm.errormessage11.value);
			return false;	
		}
		else
		{
			oForm.OtherInfo.value=txtOther;
		}
				
		if (oForm.Country.value == 'dk')
		{
			if (oForm.MinAge!=null)
			{
				if (oForm.MinAge.value!='' && isNaN(parseInt(oForm.MinAge.value)))
				{
					alert(oForm.errormessage13.value);
					return false;	
				}
				if (oForm.MaxAge.value!='' && isNaN(parseInt(oForm.MaxAge.value)))
				{
					alert(oForm.errormessage13.value);
					return false;	
				}
			}
			//Om infot uppdateras men inget skrivs i min- och maxage eller admin nullar 0-värdena ska dessa inte uppdateras 
			// De ska fortfarande var tomma/null i databasen
			
			if (((oForm.MinAge.value == '0' && oForm.MaxAge.value == '0') || (oForm.MinAge.value == '' && oForm.MaxAge.value == '')) && (oForm.oldminage.value == '0' && oForm.oldmaxage.value == '0'))
			{
				oForm.saveaction.value = 'updateunitnoage';
			}
			//Försöker kunden "nulla" värden som tidigare satts, sätts dom till 0
			// Går inte att uppdatera dom till null eller tömma dom
			if (oForm.oldminage.value != '0' && oForm.oldmaxage.value != '0' && oForm.MinAge.value == '' && oForm.MaxAge.value == '')
			{
				oForm.MinAge.value = 0;
				oForm.MaxAge.value = 0;
			}
		}
		//oForm.action = "debuginfo.jsp";
		doSubmit();
}

function changeCharWard()
{
		var oForm = document.forms[0];
		
		//Visning av avd i utbudet för Norge (cq 24445)
		if (oForm.Country.value == 'no')
		{
			if(oForm.cbShowWard.checked)
			{
				oForm.ShowWard.value = 'Y';
			}
			else
			{
				oForm.ShowWard.value = 'N';
			}
		}	
			
		if (oForm.WelcomeText.value.length>255)
		{
			alert(oForm.errormessage9.value);
			return false;	
		}
		if (oForm.OtherInfoPublic.value.length>2047)
		{
			alert(oForm.errormessage10.value);
			return false;	
		}	
		if (oForm.StaffList.value.length>2047)
		{
			alert(oForm.errormessage11.value);
			return false;	
		}	
		if (oForm.OtherInfoLoggedIn.value.length>2047)
		{
			alert(oForm.errormessage12.value);
			return false;	
		}
		if (oForm.Country.value != 'no')
		{
			if (oForm.MinAge!=null)
			{
				if (oForm.MinAge.value!='' && isNaN(parseInt(oForm.MinAge.value)))
				{
					alert(oForm.errormessage13.value);
					return false;	
				}
				//else if(oForm.MinAge.value=='')
				//{
				//	oForm.MinAge.value='(empty)';	
				//}
				if (oForm.MaxAge.value!='' && isNaN(parseInt(oForm.MaxAge.value)))
				{
					alert(oForm.errormessage13.value);
					return false;	
				}
				//else if(oForm.MaxAge.value=='')
				//{
				//	oForm.MaxAge.value='(empty)';	
				//}
			}
		
			//Om infot uppdateras men inget skrivs i min- och maxage ska dessa inte uppdateras
			// De ska fortfarande var tomma/null i databasen
			if (oForm.MinAge.value == 0 && oForm.MaxAge.value == 0 && oForm.oldminage.value == 0 && oForm.oldmaxage.value == 0)
			{
				if(oForm.operation.value == 'Ward')
				{
				oForm.saveaction.value = 'updatewardnoage';
				}
				else if(oForm.operation.value == 'Unit')
				{
				oForm.saveaction.value = 'updateunitnoage';
				}
			}
			//Försöker kunden "nulla" värdena sätts dom till 0
			if ((oForm.MinAge.value == null || oForm.MinAge.value == '') && (oForm.MaxAge.value == null || oForm.MaxAge.value == ''))
			{
				oForm.MinAge.value = 0;
				oForm.MaxAge.value = 0;
			}
		}
		var txtWelcome = oForm.WelcomeText.value;
		txtWelcome=changeCharacter(txtWelcome);
		if (txtWelcome=='false')
		{
			alert(oForm.errormessage14.value);
			return false;	
		}
		else
		{
			oForm.WelcomeText.value=txtWelcome;
		}	
		var txtOther = oForm.OtherInfoPublic.value;
		txtOther=changeCharacter(txtOther);
		if (txtOther=='false')
		{
			alert(oForm.errormessage14.value);
			return false;	
		}
		else
		{
			oForm.OtherInfoPublic.value=txtOther;
		}		
		var txtStafflist = oForm.StaffList.value;
		txtStafflist=changeCharacter(txtStafflist);
		if (txtStafflist=='false')
		{
			alert(oForm.errormessage14.value);
			return false;	
		}
		else
		{
			oForm.StaffList.value=txtStafflist;
		}		
		var txtOtherInfoLoggedIn = oForm.OtherInfoLoggedIn.value;
		txtOtherInfoLoggedIn=changeCharacter(txtOtherInfoLoggedIn);
		if (txtOtherInfoLoggedIn=='false')
		{
			alert(oForm.errormessage14.value);
			return false;	
		}
		else
		{
			oForm.OtherInfoLoggedIn.value=txtOtherInfoLoggedIn;
		}
		//oForm.action = "debuginfo.jsp";
		doSubmit();
}
	
function changeCharacter(strValue)
{	
	// för att ändra alla < och > som inte räknas som taggar
	var re = /<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'))?)+\s*|\s*)\/?>/i,m;
		
  var strOld=strValue;
  var strOld1=strValue;
  var strOld2=strValue;
  var strOld3=strValue;
  var strNew='';
  var strNew1='';
  var strNew2='';
  var strNew3='';
  var strIndex=0;
  
  // kontrollera antalet </ />
	var re1 = /<\/\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?')\s*|[^\/])?)+\s*|\s*)\/>/i,m;
  strIndex=0;
  var counter1 = 0;
  var m1 = re1.exec(strOld1);
  while (m1 != null)
  {
  	counter1 = counter1 + 1;
  	strIndex = m1.lastIndex;
  	strNew2 = strNew1 + strOld1.slice(m1.index,strIndex);
  	strOld1 = strOld1.slice(strIndex);
  	m1 = re1.exec(strOld1);
 	}
 	
  // kontrollera antalet < >
	var re2 = /<\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'))?)+\s*|\s*)>/i,m;
  strIndex=0;
  var counter2 = 0;
  var m2 = re2.exec(strOld2);
  while (m2 != null)
  {
  	counter2 = counter2 + 1;
  	strIndex = m2.lastIndex;
  	strNew2 = strNew2 + strOld2.slice(m2.index,strIndex);
  	strOld2 = strOld2.slice(strIndex);
  	m2 = re2.exec(strOld2);
 	}
  
  // kontrollera antalet </  >
	var re3 = /<\/+\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'))?)+\s*|\s*)>/i,m;
 	strIndex=0;
  var counter3 = 0;
  var m3 = re3.exec(strOld3);
  while (m3 != null)
  {
  	counter3 = counter3 + 1;
  	strIndex = m3.lastIndex;
  	strNew3 = strNew3 + strOld3.slice(m3.index,strIndex);
  	strOld3 = strOld3.slice(strIndex);
  	m3 = re3.exec(strOld3);
 	}
  
  //kontroll för att se om html är skapad rätt
  if (counter1 > 0 || counter2 != counter3)
  {
  	return "false";
  }
  
  var strBetween='';
  var m = re.exec(strOld);
  strIndex=0;
  if (m!=null)
  {
	  while (m != null)
	 	{
	  	strIndex=m.lastIndex;
	  	if (m.index != 0)
	  	{
	  		strBetween = strOld.slice(0,m.index);	
	  		strNew=strNew + replaceChar(strBetween);
	  	}
	  	strNew = strNew + strOld.slice(m.index,strIndex);
	  	strOld = strOld.slice(strIndex);
	  	m = re.exec(strOld);
	  }
	  if (strOld != '' && strOld != null)
	  {
	  	strNew = strNew + replaceChar(strOld);
	  }
	 }
	 else
	 {
	 	strNew=replaceChar(strValue);
	 }
  return strNew;
}

function replaceChar(strIn)
{
	while (strIn.indexOf(">") > -1)
 			{strIn = strIn.replace(">","&gt;");}	
	while (strIn.indexOf("<") > -1)
 			{strIn = strIn.replace("<","&lt;");}
 	return strIn;
}

//Jämför datum 
function CheckPlaceDate(sReqDateOfPlac, sEarliestPlacDate)
{
	var sDate=new Date();
	sEarliestPlacDate=sEarliestPlacDate.replace(',', '.');
	sEarliestPlacDate=sEarliestPlacDate*30;
	sEarliestPlacDate=addDays(sEarliestPlacDate);
	if (sReqDateOfPlac>=sEarliestPlacDate)
	{
		return true;
	}
	else
	{
		return false;	
	}
}

function addDays(daysToAdd)
{
  var d = new Date();
  var aDay = (86400*daysToAdd)*1000; // Lägg till dagar
  d.setTime(d.getTime() + aDay);
  var theYear = d.getFullYear();
  var theMonth = d.getMonth()+1;
  var theDate = d.getDate();
  return d.getFullYear() + '-' + (theMonth<10?'0':'') + theMonth + '-' + (theDate<10?'0':'') + theDate;
}

function checkCustodian(custodianSSN){
	var oForm = document.forms[0];
	var sCustodianSSN = custodianSSN.value;
	
	if (sCustodianSSN != '' && sCustodianSSN.indexOf("-") == -1) {
		sCustodianSSN = sCustodianSSN.substr(0, 6) + '-' + sCustodianSSN.substr(6, 4);
	}
		
	if (!validateSSN(Trim(sCustodianSSN))) {
		alert(oForm.errormessage4.value);
		//custodianSSN.focus();
	}
	else
	custodianSSN.value = sCustodianSSN;
}




function controlOnLoad()
{
	var oForm=document.forms[0];
	if (oForm!=null)
	{
		if (oForm.check_back!=null)
		{
			if (oForm.check_back.value==1 || oForm.check_back.value=='1')	
			{
				history.forward();
			}
		}
	}
}

function sendOnLoad()
{
	var oForm=document.forms[0];
	if (oForm!=null)
	{
		if (oForm.check_back!=null)
		{
			if (oForm.check_back.value==1 || oForm.check_back.value=='1')	
			{
				history.forward();
			}
			else
			{
				oForm.check_back.value=1;
				doSubmit();	
			}
		}
	}
}

function confirmCancelAnonymous(sBack) {
	var oForm = document.forms[0];
	var sCancelMessage = oForm.cancelmessage1.value + '\n\n' + oForm.cancelmessage2.value + '\n'+ oForm.cancelmessage3.value;
			
	if (confirm(sCancelMessage)) 
		self.parent.location = sBack
}

function exitAnonymous(sBack){
	self.parent.location = sBack
	
}

function CheckAHead(RequestedDate,months) 
{
	var sDate = new Date();
	var extraDays = 0;
	var newDay = sDate.getDate();
	
	if(months > 1)
	//Bc 33995 - Lägga till en extra dag varannan månad. Kan bli problem vid månadskiften annars
	extraDays = months/2;
	extraDays=parseInt(extraDays);
	// Om februari ligger i sökperioden, ta bort två dagar
	if (sDate.getFullYear() <  RequestedDate.substr(0,4))
	{
		if (RequestedDate.substr(5,2) > 02)
			extraDays = extraDays - 2;
	}
	else
	{
		if (sDate.getMonth() < 2 && RequestedDate.substr(5,2) > 02)
			extraDays = extraDays - 2;
	}	
	sDate=addDays((months*30) + extraDays);
	//Ge kontrolldatumet samma dag som dagens datum		
	sDate=sDate.substr(0,8) + newDay;
			
	if (RequestedDate>sDate)
	{
		return true;
	}
	else
	{
		return false;	
	}
}

//Ändra ordning på placeringsalternativ i ansökan
function changeAltNo(AppId, PlacId, AltNo, sDirection)
{
		var oForm=document.forms[0];
		if (sDirection=='up')
		{
			iNewNo=AltNo-1;	
		}
		else
		{
			iNewNo=AltNo+1;	
		}
		oForm.action = 'Default.asp?page=bo/bas/application&ApplicationId=' + AppId +  '&PlacId=' + PlacId + '&newNo=' + iNewNo +'&oldNo=' + AltNo + '&saveaction=updatealtorder&redirectpage=bo/bas/application$step45.xsl';
		doSubmit();
}
