﻿/* 
Filename:	/scripts/process.js
Updated:	2005-04-03 00:33
Comments:	Process functions
*/

var bProcess = true;
var bFormBack = false;

function saveFieldgroup(frm, fieldgroup) {
}

function setError(fieldEl, bWriteError) {
	if (typeof(fields) == "undefined") return;
	fieldObj = getArrayObj(fieldEl.name, fields);
	if (fieldObj != null) {
		errors[errors.length] = new errorObject(fieldEl.name, fieldObj.erra);
		if (bWriteError) writeField(fieldEl, false);
	}
	return false;
}

function writeField(el, bValid) {
	if (typeof(fields) == "undefined") return;
	obj = getArrayObj(el.name, fields);
	if (obj != null) {
		if ((obj.message != null) || (obj.erra != null)) {
			writeMessage(obj, el, !bValid);
			return;
		}
	}
	obj = new fieldObject(el.name, null, 2999);
	writeMessage(obj, el, !bValid);
}

function unlockFocus() {
	str = getQuery('unlock');
	if (str != '') {

	}
}

function validateOtherField(str) {
    if (str == null || trim(str).length == 0) {
       return true;
    }
    str = makeValidStair(str);
    var rx = /[0-9]+/g;
    str = str.replace(rx, '');
    str = str.replace('/', '');
    var validAbbreviations = new Array('ÖG', 'TR', '/TR',  'U', 'UH', 'UV', 'N', 'BV', 'LÄG', 'RUM');
    var vStr = str.split(' ');	
	var isValid = true;
	for (i = 0; i < vStr.length; i++) {
		var found = false;
		for (j = 0; j < validAbbreviations.length; j++) {
			if (vStr[i] == validAbbreviations[j]) {
				found = true;
				break;
			}
		}
		if (!found) {
			if (vStr[i] != '') {
				isValid = false;
				break;
			}
		}
	}
    return isValid;
}

function setStep(frm, step, bSubmit, substep) {
	if (step != null) {
		bValid = true;
		if (typeof(frm) == "string") frm = eval("document." + frm);
		if (typeof(substep) == "string") frm.substep.value = substep
		else if (typeof(substep) != "undefined") {
		//	if (bFormBack) return;
		//	bFormBack = true;
			bValid = true;
		}
		else if (bValidate && bSubmit) bValid = validateStep(currentStep);
		if (bValid) {
			frm.step.value = step;
			if (bSubmit) frm.submit();
		}
	}
}

function messageObject(id, name, message) {
	this.id = id;
	this.name = name;
	this.message = message;
}

function fieldObject(id, message, erra) {
	this.id 	= id;
	this.message 	= message;
	this.erra	= erra;
}

function errorObject(id, erra) {
	this.id 	= id;
	this.erra 	= erra;
}

function getArrayObj(id, arr) {
	for (i=0; i<arr.length; i++) {
		if (id == arr[i].id) return arr[i];
	}
	return null;
}

function getMessageObj(errObj) {
	obj = getArrayObj(errObj.erra, messages);
	if (obj == null) obj = new messageObject(0, errObj.id, '');
	return obj;
}

function writeIcon(fieldObj, id, bError) {
	if ((bError) && (fieldObj.erra != null)) id = fieldObj.erra;
	fieldId = fieldObj.id;
	spanEl = document.getElementById("span_" + fieldId);
	if (spanEl != null) {
		if (bError) iconStr = alertIcon
		else iconStr = infoIcon
		str = '<a href="javascript://" onclick="toggleInfo(\'message_' + id + '\', \'' + fieldId + '\')" id="link_' + fieldId + '" tabIndex="-1"><img src="' + iconStr + '" width="16" height="16" hspace="3" align="absmiddle" border="0" /></a>';
		spanEl.innerHTML = str;
	}
}

function writeMessage(fieldObj, fieldEl, bError) {
	var obj = null;
	var bFound = false;
	if (fieldEl == null) {
		if (b.alias == "IE") fieldEl = frm.elements(fieldObj.id)
		else fieldEl = frm.elements[fieldObj.id];
	}
	spanEl = document.getElementById("span_" + fieldObj.id);
	if (fieldEl != null) {
		for (i=0; i<errors.length; i++) {
			if (fieldObj.id == errors[i].id) {
				if (fieldEl.type == "text") fieldEl.className = 'inputFieldError';
				obj = getArrayObj(errors[i].erra, messages);
				if (obj != null) {
					bFound = true;
					writeIcon(fieldObj, obj.id, true);
					break;
				}
			}
		}
		if ((!bFound) && ((fieldObj.message != null) || (fieldObj.erra != null))) {
			if (fieldEl.type == "text") {
				if (bError) fieldEl.className = 'inputFieldError'
				else fieldEl.className = 'inputField';
			}
			if ((fieldObj.erra != null) && bError) obj = getArrayObj(fieldObj.erra, messages)
			else if (fieldObj.message != null) obj = getArrayObj(fieldObj.message, messages);
			if (obj != null) {
				if (bError) writeIcon(fieldObj, obj.id, true)
				else writeIcon(fieldObj, obj.id, false);
			}
			else if (spanEl != null) spanEl.innerHTML = '';
		}
	}
}

function writeMessages(frm) {
	if (typeof(fields) == "undefined") return;
	for (j=0; j<fields.length; j++) {
		writeMessage(fields[j]);
	}
}

function writeInfoLayers() {
	if (typeof(messages) == "undefined") return;
	for (j=0; j<messages.length; j++) {
		var str = '';
		if (messages[j].id < 2000) {
			classStr = 'divInfoLayer';
			iconStr = 'images/big_info_icon.gif';
			closeStr = 'images/close.gif';
		}
		else {
			classStr = 'divAlertLayer';
			iconStr = 'images/big_alert_icon.gif';
			closeStr = 'images/close_white.gif';
		}
		str += '<div class="' + classStr + '" id="message_' + messages[j].id + '">';
		str += ' <img src="' + iconStr + '" width="28" height="28" align="absmiddle" /><span class="bigText"><b>&#160;' + messages[j].name + '</b></span>';
		str += ' <div class="divInner">' + messages[j].message + '</div>';
		str += ' <div class="divClose" align="right"><a href="javascript://" onclick="toggleInfo(\'message_' + messages[j].id + '\')" tabIndex="-1">Stäng<img src="' + closeStr + '" width="19" height="18" border="0" hspace="6" align="absmiddle" /></a></div>';
		str += '</div>';
		document.write(str);
	}
}

function writeErrors(bClient, titleStr, messageStr) {
	if (typeof(titleStr) == "undefined") {
		if (errors.length == 0) return;
		if (bClient) titleStr = titleStr = 'Formuläret du försöker skicka innehåller följande fel';
		else titleStr = 'Formuläret du skickade innehåller följande fel';
	}
	var str = '';
	str += '<div class="divError">';
	str += ' <img src="images/alert_shaded.gif" height="4" width="535" /><br/>';
	str += ' <div class="divOuter">';
	str += '  <div class="divBorder">';
	str += '   <img src="images/big_alert_icon.gif" width="28" height="28" align="absmiddle" /><span class="bigText"><b>&#160;' + titleStr + '</b></span>';
	str += '   <div class="divInner">';
	if (typeof(messageStr) == "undefined") {
		for (j=0; j<errors.length; j++) {
			obj = getMessageObj(errors[j]);
			rowStr = '- ' + obj.name + ': ' + obj.message + '<br/>';
			if (str.indexOf(rowStr) == -1) str += rowStr;
		}
	}
	else str += messageStr;
	str += '   </div>';
	str += '  </div>';
	str += ' </div>';
	str += ' <img src="images/alert_bottom.gif" height="3" width="535" /><br/>';
	str += '</div>';
	if (bClient || (typeof(messageStr) != "undefined")) {
		divErrorEl = document.getElementById('divErrors');
		divErrorEl.innerHTML = str;
		errors.length = 0;
	}
	else document.write(str);
}

function validateMaxLength(el, len, bAlert) {
	frm = el.form;
	str = el.value;
	if (!bAlert) len--;
	if (str.length > len) {
		el.value = str.substring(0, len);
		if (bAlert) {
			alert('Fältet får vara max ' + len + ' tecken långt!');
			el.focus();
		}
	}
}

function validatePostalNumber(el) {
	el.value = trim(el.value);
	bValid = isValidPostalNo(el.value);
	if (bValid) {
		str = el.value.replace(/([^0-9.])/g, '');
		el.value = str.substring(0,3) + ' ' + str.substring(3);
	}
	writeField(el, bValid);
}

function validateName(el) {
	el.value = trim(el.value);
}

function validateStreetNumber(el, bRequired) {		
	el.value = trim(el.value);
	bValid = isValidStreetNumber(el.value);
	if (!bValid && (bRequired || (el.value != ''))) writeField(el, false)
	else writeField(el, true);
}

function validateStair(el) {    
	el.value = trim(el.value);	
	str = makeValidStair(el.value);
	bValid = isValidStair(str);
	if (bValid) el.value = str;
	writeField(el, bValid);
}

function validatePostalAddress(el) {
	el.value = trim(el.value);
	bValid = isValidPostalAddress(el.value);
	if (bValid) el.value = trim(el.value);
	writeField(el, bValid);
}

function validatePnr(el) {
	el.value = trim(el.value);
	bValid = isValidPnr(el.value);
	if (bValid) el.value = stripPnr(el.value).substring(0, 6) + '-' + stripPnr(el.value).substring(6);
	writeField(el, bValid);
}

function validateEmail(el, bRequired) {
	el.value = trim(el.value);
	bValid = isValidEmail(el.value);
	if (!bValid && (bRequired || (el.value != ''))) writeField(el, false)
	else writeField(el, true);
}

function validateStreet(el) {
	el.value = trim(el.value);
	bValid = isValidStreet(el.value);
	writeField(el, bValid);
}

function validateRequired(el) {
	el.value = trim(el.value);
	if (el.value == '') writeField(el, false)
	else writeField(el, true);
}

function validateEntrance(el) {
    if (el.value == null || trim(el.value).length == 0) {
        el.value = '';
        return true;
    }
    var rx = /[a-z,A-Z,å,ä,ö,Å,Ä,Ö]{1}[\w\d]{0,2}/i;
    var f = el.value.replace(rx, '');
    if (f.length == 0) {
        el.value = trim(el.value.toUpperCase());
        return true;
    } else {
        return false;
    }
	//el.value = trim(el.value.toUpperCase());	
}

function validateTelephone(el, bRequired) {
	el.value = trim(el.value);
	bValid = isValidTelephone(el.value);
	if (!bValid && (bRequired || (el.value != ''))) writeField(el, false)
	else writeField(el, true);
}

function validateCellphone(el, bRequired) {
	el.value = trim(el.value);
	bValid = isValidCellphone(el.value);
	if (!bValid && (bRequired || (el.value != ''))) writeField(el, false)
	else writeField(el, true);
}

function validateDate(el, bRequired) {
	el.value = trim(el.value);
	bValid = isValidDate(el.value);
	if (!bValid && (bRequired || (el.value != ''))) writeField(el, false)
	else writeField(el, true);
}

function isValidPnr(str) {
	var rx = /([^0-9.])/g;
	str = str.replace(rx, '');
	if (str.length != 10) return false;
	total = 0;
	for (i = 0; i < 9; i++) {
		sum = i%2 == 0 ? str.substring(i, i+1) * 2 : str.substring(i, i+1) * 1;
		sum = sum.toString();
		for (c = 0; c < sum.length; c++) {
			total += (sum.substring(c, c+1)) * 1;
		}
	}
	return ( (100 - total) % 10) == (str.substring(str.length-1, str.length) * 1);	
}

function isValidPostalNo(str) {
	var rx = /([^0-9.])/g;
	str  = str.replace(rx, '');
	if (str.length != 5) return false
	else return true;
}

function isValidStreet(str) {		
	if ( isNaN( str.substring(str.length-1, str.length) * 1)) return true
	else return false;
}

function isValidEmail(str) {
	var rx = /(^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$)/i;
	return rx.test(str);
}

function isValidStreetNumber(str) {
	/*if (str.length == 0 || str.substring(0,1) == '0' || isNaN(str*1)) return false
	else return true;*/
	if (str == null || str.length == 0) {
        return false;
	}
	var rx = /[1-9][0-9]{0,8}([-][1-9][0-9]{0,8})?/i;
	var f = str.replace(rx, '');	
	return f.length == 0;
}

function isValidPostalAddress(str) {	
	var rx = /([^a-zA-ZåäöÅÄÖáéíóúýÁÉÍÓÚÝñÑõÕ -])/g;
	if (str.match(rx) || str == '') return false
	else return true;
}

function isValidStair(str) {
	return true;
}

function isValidRequired(str) {
	if (trim(fieldEl.value) == '') return false
	else return true;
}

function isNumeric(str) {	
	return !isNaN(str*1);
}

function isValidTelephone(str) {  
	var rx = /([0-9]|-){8,11}/g;
	if (str.match(rx) == str) {
		str = str.replace('-', '');
		for (i=1; i<str.length; i++) {
			if (str.charAt(i-1) != str.charAt(i)) return true;
		}
	}
	return false;
}

function isValidCellphone(str) {
	var rx = /([0-9]|-){8,20}/g;
	if (str.match(rx) == str) {
		str = str.replace('-', '');
		for (i=1; i<str.length; i++) {
			if (str.charAt(i-1) != str.charAt(i)) return true;
		}
	}
	return false;
}

function isValidDate(str) {
	var rx = /(^[0-9]{4}(\-[0-1][0-9]\-[0-3][0-9])$)/g;
	if (str.match(rx)) return true
	else return false;
}

function isValidKnr(str) {
	if (str.length != 10) return false;
	i = parseInt(str);
	if (isNaN(i)) return false;
	if (i != str) return false;
	return true;
}

function makeValidStair(str) {
    str = str.toUpperCase();
    for (i = 30; i > 0; i--) {
        str = str.replace(i + ':A', i);
        str = str.replace('VÅNING ' + i, i + ' VÅNING');
        str = str.replace('VÅN ' + i, i + ' VÅNING');        
        str = str.replace('TRAPPA ' + i, i + ' TRAPPA');
        str = str.replace('TRAPP ' + i, i + ' TRAPP');
        str = str.replace('PLAN ' + i, i + ' PLAN');
        str = str.replace('PL ' + i, i + ' PL');
    }
    str = str.replace('TRAPPOR', 'TR');
    str = str.replace('TRAPPAN', 'TR');
    str = str.replace('TRAPPA', 'TR');
    str = str.replace('TRAPP', 'TR');
    str = str.replace('½', '1/2');
    str = str.replace('.', '');
    str = str.replace('T R', 'TR');
    str = str.replace('PLAN', 'TR');
    str = str.replace('PL', 'TR');
	str = str.replace('UPPGÅNG', 'U');
	str = str.replace('UPPG', 'U');
	str = str.replace('UPG', 'U');	
	str = str.replace('LÄGENHETSNR', 'LÄG');
	str = str.replace('LÄGENHETSNUMMER', 'LÄG');
	str = str.replace('LÄGENHET', 'LÄG');
	str = str.replace('LGH', 'LÄG');
	str = str.replace('#', 'LÄG');    
	str = str.replace('BOTTEN VÅNING', 'BV');
	str = str.replace('BOTTENVÅNING', 'BV');	
	str = str.replace('NEDRE BOTTEN', 'BV');
	str = str.replace('NEDREBOTTEN', 'BV');
	str = str.replace('BOTTEN', 'BV');
	str = str.replace('VÅNING', 'TR');
    	str = str.replace('VÅN', 'TR');
	str = str.replace('NB', 'BV');	
	str = str.replace('TR NED', 'TR N');
	str = str.replace('TR NER', 'TR N');
	str = str.replace('ÖVER GATAN', 'ÖG');
	str = str.replace('Ö G', 'ÖG');	
	for (i = 10; i >= 0; i--) {	    
	    str = str.replace('U ' + i, 'U' + i);
	    str = str.replace(i + ' TR', i + 'TR');
	    str = str.replace(i + 'Ö G', i + ' Ö G');
	    str = str.replace('LÄG ' + i, 'LÄG' + i);
	}
	str = str.replace('  ', ' ');
	originalString = str;
	if (str == null || trim(str).length == 0) {
		return trim(str);
	}
	vstr = str.split(' ');	
	str = '';
	for (i = 0; i < vstr.length; i++) {	    
	    if (isNumeric(vstr[i])) {
	        if (vstr[i].length < 3) {
	            if (originalString.indexOf('TR') == -1) {
	                str += str.length == 0 ? vstr[i] + 'TR' : ' ' + vstr[i] + 'TR';
	            } else {
	                str += str.length == 0 ? vstr[i] : ' ' + vstr[i];
	            }
	        } else if (vstr[i].length > 2 && vstr[i].length < 5) {
	            if (originalString.indexOf('LÄG') == -1) {
	                str += str.length == 0 ? 'LÄG' + vstr[i] : ' LÄG' + vstr[i];
	            } else {
	                str += str.length == 0 ? vstr[i] : ' ' + vstr[i];
	            }
	        } else {
	            str += str.length == 0 ? vstr[i] : ' ' + vstr[i];
	        }
	    } else { 
	        str += str.length == 0 ? vstr[i] : ' ' + vstr[i];
	    }
	}

	if (str.indexOf('1/2') > -1) {
	    str = str.replace('TR', '');
	    str = str.replace('1/2', '1/2TR');
	}

	return trim(str);
}

function trim(str) {
	return str.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');
}

function stripPnr(str) {
	var rx = /([^0-9.])/g;
	return str.replace(rx, '');
}

