function navigate(oForm, direction, factor) {
	oForm.mapnav.value = direction;
	oForm.mapnavfac.value = factor;
	oForm.submit();
}

function zoomtowidth(oForm, width) {
	oForm.mapzl.value = width;
	oForm.submit();
}

function selectLocation(selectElement) {
	document.forms['mapform'].mapnav.value = direction;
	document.forms['mapform'].mapnavfac.value = factor;
	document.forms['mapform'].submit();
}

function cidChange(eSelect) {
	var newCID = eSelect.options[eSelect.selectedIndex].value;
	document.forms['mapform'].cid.value = newCID;
	document.forms['addressform'].cid.value = newCID;
	if(document.forms['phoneform']){
		document.forms['phoneform'].cid.value = newCID;
	}
}

function cidChangeRadio(eButton) {
	var newCID = eButton.value;
	document.forms['mapform'].cid.value = newCID;
	document.forms['addressform'].cid.value = newCID;
	if(document.forms['phoneform']){
		document.forms['phoneform'].cid.value = newCID;
	}
}



function nnoverrideChange(eSelect) {
	var newNN = eSelect.options[eSelect.selectedIndex].value;
	document.forms['mapform'].nn.value = newNN;
	document.forms['addressform'].nn.value = newNN;
//	document.forms['freetextform'].nn.value = newNN;
}


function printInformation(url, width, height) {
	var theWindow = window.open(url, 'fnfprn', 'toolbar=no,menubar=no,width=' + width + ',height=' + height + ',location=no,resize=yes,scrollbars=no');
	theWindow.focus();
}

function mailInformation(url, width, height) {
	var theWindow = window.open(url, 'fnfmail', 'toolbar=no,menubar=no,width=' + width + ',height=' + height + ',location=no,resize=yes,scrollbars=no');
	theWindow.focus();
}

function generalPopUp(url, width, height) {
	var theWindow = window.open(url, 'Gpop', 'toolbar=yes,menubar=yes,width=' + width + ',height=' + height + ',location=yes,resizable=yes,scrollbars=auto,status=yes');
	theWindow.focus();
}

function mailinformationcheck(sourcemail, destinationmail, errmsgsource, errmsgdestination) {
	if(!checkEmail(destinationmail)) {
		alert(errmsgdestination);
		return false;
	}
	if(!checkEmail(sourcemail)) {
		alert(errmsgsource);
		return false;
	}
	return true;
}

function checkEmail(mail) {
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; // Special characters not allowed in address ( ) < > @ , ; : \ " . [ ]
	var validChars = "\[^\\s" + specialChars + "\]"; // Valid characters actually the ones not allowed
	var quotedUser = "(\"[^\"]*\")"; // Patteren for quotes usernames
	var atom = validChars + '+'; // Represents an atom, non-special characters
	var word = "(" + atom + "|" + quotedUser + ")"; // Represents a word in the username
	var patEmail = /^(.+)@(.+)$/; // Pattern for checking the user@domain format
	var rgMatch = mail.match(patEmail); // Break up the mail address into basic parts
	if (null == rgMatch) {
		return false; // Overall structure is wrong
	}

	var user = rgMatch[1];
	var domain = rgMatch[2];
	var patUser = new RegExp("^" + word + "(\\." + word + ")*$"); // Pattern describing the username structure
	if (null == user.match(patUser)) {
		return false; // Invalid username format
	}

	var patIpDomain = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; // Pattern for use when domain is a ip
	var rgIp = domain.match(patIpDomain);
	if (null != rgIp) {
		for(var i = 1; i <= 4; i++) { // Domain formatted as an ip-address check range
			if (rgIp[i] > 255) {
				return false; // Invalid range
			}
		}
		return true
	}

	var patDomain = new RegExp("^" + atom + "(\\." + atom +")*$"); // Pattern describing a normal domainname
	if (null == domain.match(patDomain)) {
		return false; // Invalid doamin format
	}

	var rgDomain = domain.match(new RegExp(atom,"g"));
	var nDomainElements = rgDomain.length;
	if (nDomainElements < 2) {
	   return false; // No hostname infront of the domainname
	}

	if ((rgDomain[nDomainElements - 1].length < 2) || (rgDomain[nDomainElements - 1].length > 4)) {
	   return false; // Address must end with a 2,3 or 4 letter word. (4 includes .info)
	}
	return true;
}


