var enableWarningIcons = "enable"; // enable/disable
var warningIconPath = "img/icons/warning.gif"
var warningIconClass = "validationWarningIcon";

function insertAfter(referenceNode, newNode) {
    referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
}

function hideWarningIcons(theForm) {
	for (var i = 0; i < theForm.elements.length; i++) {
		var e = theForm.elements[i];
		var eWarning = document.getElementById(e.name+"_warning");

		hideWarningIcon(eWarning);
	}
}

function hideWarningIcon(element) {
	if (element) {
		element.parentNode.removeChild(element);
	}
}

function showWarningIcon(element) {
	//show Warning Icon after element (e.g. form field)
	var warningIconSpan = document.createElement('span');
	if (element.name) warningIconSpan.id = element.name+"_warning";
	else warningIconSpan.id = element.id+"_warning";
	warningIconSpan.className = warningIconClass;
	var warningIconImg = document.createElement('img');
	warningIconImg.src = warningIconPath;
	warningIconImg.align = "absmiddle";
	warningIconSpan.appendChild(warningIconImg);
	if (!document.getElementById(warningIconSpan.id)) insertAfter(element,warningIconSpan);
}

function warningIcons(theForm) {
	var ok = true;

	//remove all warning icons
	hideWarningIcons(theForm);

	//check form and show warning icons
	for (var i = 0; i < theForm.elements.length; i++) {
		var e = theForm.elements[i];

		if (!(e.name.indexOf('def_')>= 0)) {

			if ( !(typeof(eval('theForm.elements["dfs_' + e.name + '"]')) == "undefined") )			
				{
				defselval = eval('theForm.elements["dfs_' + e.name + '"].value');
				selvalues = defselval.split('_');
				selrequired = selvalues[0];
				selalert = selvalues[1];
				if (selrequired == 'req') {
					sreq = true;
				}
				else
					sreq = false;
					
				if (sreq && e.value == 'null') {
					showWarningIcon(e);
				}
				
			} else if ( !(typeof(eval('theForm.elements["def_' + e.name + '"]')) == "undefined") ) {
				defval = eval('theForm.elements["def_' + e.name + '"].value');
				
				values = defval.split('_');
				datatype = values[0];
				required = values[1];
				alertmessage = values[2];
				alertmessage2 = values[3];
			
				if (required == 'req')
					req = true;
				else
					req = false;
				
				if (datatype == 'int' || datatype == 'float') {
					needN = true;
				}
				else
					needN = false;
					
					
				if ((req && e.value == '') || (!checknumber(e.value) && needN)) {
					showWarningIcon(e);
				}

			}
		}
	}

}

function checknumber(object_value)
    {

    if (object_value.length == 0)
        return false;
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;


	check_char = start_format.indexOf(object_value.charAt(0))

	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        

	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)	
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	

    return true
}


function CheckFormItems(theForm)
{
	if (enableWarningIcons=="enable") warningIcons(theForm);
	
	var ok = true;

	for (var i = 0; i < theForm.elements.length; i++) 
	{
			var e = theForm.elements[i];
			

			
			
			if (!(e.name.indexOf('def_')>= 0))
			
			{

				if ( !(typeof(eval('theForm.elements["dfs_' + e.name + '"]')) == "undefined") )			
					{
					defselval = eval('theForm.elements["dfs_' + e.name + '"].value');
					selvalues = defselval.split('_');
					selrequired = selvalues[0];
					selalert = selvalues[1];
					if (selrequired == 'req')
						{
						sreq = true;
						}
					else
						sreq = false;
						
					if (sreq && e.value == 'null')
						{
						alert(selalert);
						return false;
						}
					
					}

			
				if ( !(typeof(eval('theForm.elements["def_' + e.name + '"]')) == "undefined") )
					{
					defval = eval('theForm.elements["def_' + e.name + '"].value');
					
					values = defval.split('_');
					datatype = values[0];
					required = values[1];
					alertmessage = values[2];
					alertmessage2 = values[3];
				
					if (required == 'req')
						req = true;
					else
						req = false;
					
					if (datatype == 'int' || datatype == 'float')
						{
						needN = true;
						}
					else
						needN = false;
						
						
					if (req && e.value == '')
						{
						alert(alertmessage);
						e.focus();
						return(false);
						}
						
					if (!checknumber(e.value) && needN)
						{
						alert(alertmessage2);
						e.focus();
						return(false);
						}

					}
			}
	}

	return (true);

}


function OpenCalendar(formfield,language,type,date,mapdir)
{

url = mapdir + '/js/calendar.cfm?language=' + language + '&type=' + type + '&formfield=' + formfield + '&date=' + date;
win = window.open(url,'calendar','height=240, width=360, top=200, left=250, scrollbars=false');
win.focus();

}


