/*
'********************************************************************
'* NAME: functions.js
'* AUTHOR: kll, Landbrugets Raadgivningscenter
'*
'* DESCRIPTION: Javascript functions.
'* END DESCRIPTION
'*
'* REQUIRED OBJECTS:
'*
'* TEMPLATE DATE: 24-03-2003
'*
'--------------------------------------------------------------------
'*HISTORY:
'*Code: #001 *Date: 31-03-2003 *Author: kll *Description: Creation
'Functons moved from JavaSupport.asp to this file
'*END HISTORY
'********************************************************************
*/

// GotoPage is a wrapper for location.href.
// Basically, it just loads a new page into the browser
function GotoPage(page)
{
	location.href=page;
}

// PostData sets an input fields value to some value. It is usually used to set the value of some hidden field.
function PostData(Name,Value)
{
var obj=document.getElementById(Name);
	if(obj) obj.value=Value;
}

// Strip strips the defined character from the start of the input string.
function strip(strInput, stripChar)
{
var s=new String(), i;
	s=strInput;
	i=0;
	while(s.charAt(i)==stripChar)
	{
		i++;
	}
	return s.slice(i,999);
}

// IsEmpty checks whether an object can resolve to an empty string
function isEmpty(strInput)
{
var myStr;
	myStr=strInput + '';
	return (myStr.length==0)
}

// isDate checks whether the supplied string is a valid date.
// A special flag makes it possible to accept an empty field as a valid date
// The resulting, parsed date can be returned to a fields value for display.
function isDate(strInput, CanBeEmpty, whichObj)
{
var TestDato = new Date();
var DateParts, i, dg, mn, yr, MyString, TestString;
var idg,imn,iyr;
	dg = TestDato.getDate();
	mn = TestDato.getMonth()+1;
	yr = TestDato.getYear();
	TestString=DateToStr(dg, mn, yr);
	if(isEmpty(strInput))
	{
		return CanBeEmpty;
	}
	MyString = strInput
	DateParts = MyString.split("/");
	if(DateParts.length<2)
	{
		DateParts = MyString.split("-");
	}

	if(DateParts.length<2)
	{
		return false;
	}

	if(DateParts[0].length>2)
	{
		return false;
	}
	if(DateParts[1].length>2)
	{
		return false;
	}
	if(DateParts.length==3)
	{
		if(DateParts[2].length>4)
		{
			return false;
		}
	}
	else
	{
		iyr=yr;
	}

	if(isNaN(parseInt(strip(DateParts[0],'0'))))
	{
		return false;
	}
	if(isNaN(parseInt(strip(DateParts[1],'0'))))
	{
		return false;
	}
	if(DateParts.length==3)
	{
		if(isNaN(parseInt(strip(DateParts[2],'0'))))
		{
			return false;
		}
	}

	idg = parseInt(strip(DateParts[0],'0'));
	imn = parseInt(strip(DateParts[1],'0'));
	if(DateParts.length==3)
	{
		iyr = parseInt(strip(DateParts[2],'0'));
	}
	else
	{
		iyr=yr;
	}
	if(iyr<2000) iyr+=2000;
	TestDato.setFullYear(iyr, imn-1, idg);
	dg = TestDato.getDate();
	mn = TestDato.getMonth()+1;
	yr = TestDato.getYear();
	if(idg!=dg)
	{
		return false;
	}
	if(imn!=mn)
	{
		return false;
	}
	if(iyr!=yr)
	{
		return false;
	}
	if(whichObj)
	{
		document.getElementById(whichObj).value=DateToStr(dg,mn,yr);
	}
	return true;
}

// IntToStr coerces an integer variant into a string variant.
function IntToStr(iInt)
{
	return '' + iInt;
}

// IntToStrFix returns a string containing an integer value, left padded with '0'.
function IntToStrFix(iInt, iWid)
{
var str, str2;
	str=IntToStr(iInt);
	if(str.length<iWid)
	{
		str2='0000000000000000';
		str=str2.substr(0,iWid - str.length) + str;
	}
	return str;
}

// DateToStr converts a date to a fixed format string
function DateToStr(iDay, iMonth, iYear)
{
	return IntToStrFix(iDay,2) + '/' + IntToStrFix(iMonth, 2) + '/' + IntToStrFix(iYear, 4);
}

// isInteger checks that a value is an integer in a specific range. A flag ensures that empty values can be accepted as well.
function isInteger(iInt, bCanBeEmpty, iMin, iMax)
{
var i;
	if(isEmpty(iInt)) return bCanBeEmpty;
	i = parseInt(iInt);
	if(isNaN(i)) return false;
	if(iMin + '' != '')
	{
		if((i < iMin) || (i > iMax))
		{
			return false;
		}
	}
	return true;
}

// isGhita checks that a value is a float in a specific range. A flag ensures that empty values can be accepted as well.
// The Ghita version allows the special values 0.1 and 0.01 as the only decimal values
function isGhita(iInt, bCanBeEmpty, iMin, iMax)
{
var f,s;
	if(isEmpty(iInt)) return bCanBeEmpty;
	s=iInt.replace(",",".");
	f = parseFloat(s);
	if(isNaN(f)) return false;
	if(iMin + '' != '')
	{
		if((f < iMin) || (f > iMax))
		{
			return false;
		}
	}
	if(f>1 && f!=Math.round(f)) return false; // >1 and has decimals
	if(f<1 && f!=0.01 && f!=0.1 && f!=0) return false; // <1 and NOT 0.1 or 0.01! (Ghita)
	return true;
}

// entertotab converts <enter> keypresses to <tab> keypresses
function entertotab()
{
	if(window.event && window.event.keyCode == 13)
	{
		window.event.keyCode = 9;
	}
}

// CheckKey checks the validity of a keypress. There are no check for the content of whatever, only for the single keypress
function CheckKey(AllowMode, ConvertEnter)
{
var code, BreakFlag
	if(window.event)
	{
		code=window.event.keyCode;
		if(ConvertEnter)
		{
			if(code == 13)
			{
				code=9;
			}
		}
		switch(AllowMode)
		{
			case 2: // Comma or decimal point allowed
			case 3: // Numeric with / allowed (dates)
				BreakFlag=false;
				if(AllowMode==2)
				{
					switch(code)
					{
						case 110: // Numeric keypad comma (comma or full stop)
						case 188: // Alpha keypad comma
						case 190: // Alpha keypad full stop
							BreakFlag=true;
							break;
					}
				}
				else
				{
					switch(code)
					{
						case 111:
							BreakFlag=true;
							break;
					}
				}
				if(BreakFlag) // Fall trough to numeric check
				{
					break;
				}
			case 1: // Pure numeric
				switch(code)
				{
					case 8: //  Backspace
					case 9: //  tab
					case 13: // Enter
					case 37: // Arrow left
					case 38: // Arrow up
					case 39: // Arrow right
					case 40: // Arrow down
					case 46: // Delete
						break; // Allow enter, tab and delete always
					case 96: // Digits on numeric keyboard
					case 97:
					case 98:
					case 99:
					case 100:
					case 101:
					case 102:
					case 103:
					case 104:
					case 105:
					case 48: // Digits on alpha keyboard
					case 49:
					case 50:
					case 51:
					case 52:
					case 53:
					case 54:
					case 55:
					case 56:
					case 57:
						break; // Allow all numeric keys
					default:
						code=0; // Disallow all others
						break;
				}
				break;
			default: // All keys
				break;
		}
		if(code!=0)
		{
			window.event.keyCode=code;
		}
		else
		{
			window.event.returnValue = false;
		}
	}
}

// TopText funktioner!
function ClearTopText()
{
var obj
	obj=document.getElementById('topText')
	if(obj) obj.innerHTML='';
	document.title='';
}

function SetTopText(newText)
{
var obj
	obj=document.getElementById('topText')
	if(obj) obj.innerHTML=newText;
	document.title=newText;
}

function AddTopText(newText)
{
var obj
	obj=document.getElementById('topText')
	if(obj) obj.innerHTML+=newText;
	document.title+=newText;
}

function GetObject(objName)
{
	return document.getElementById(objName);
}

function validate()
{
var chkObject;
	if(isEmpty(Stamdata.beskrivelse.value))
	{
		alert("Beskrivelsesfelt skal udfyldes");
		return false;
	}

	chkObject=GetObject('Variant');
	if(chkObject)
	{
		if(Stamdata.Variant.value=="")
		{
			alert("Sort skal vælges");
			return false;
		}
	}

	chkObject=GetObject('SaaDato');
	if(chkObject)
	{
		if(!isDate(Stamdata.SaaDato.value,true,Stamdata.SaaDato.name))
		{
			alert("Sådato skal udfyldes");
			return false;
		}
	}
	
	chkObject=GetObject('Forfrugt');
	if(chkObject)
	{
		if(chkObject.selectedIndex==0)
		{
			alert("Forfrugt skal udfyldes");
			return false;
		}
	}

	chkObject=GetObject('Forforfrugt');
	if(chkObject)
	{
		if(chkObject.selectedIndex==0)
		{
			alert("Forforfrugt skal udfyldes");
			return false;
		}
	}

	chkObject=GetObject('Jbnr');
	if(chkObject)
	{
		if(chkObject.selectedIndex==0)
		{
			alert("Jbnr skal udfyldes");
			return false;
		}
	}

	chkObject=GetObject('Behandlet');
	if(chkObject)
	{
		if(chkObject.selectedIndex==0)
		{
			alert("Behandlet status skal udfyldes");
			return false;
		}
	}

	Stamdata.submit();
	return true;
}

