// <!--
// JavaScript Library
//
// 29.11.2008
//
// (c) 2000 - 2008 Milan MACAN
//

	function RequiredField(field) 
	{
		if (IsOK) 
		{
			if (field) 
			{
				if (field.value == "")  
				{
					ErrorMess(field, message[0]);
				}
			}
		}
	}

	function SetField(field, set_val) 
	{
		if (IsOK) 
		{
			if (field) 
			{
				if (field.value) 
				{
					field.value = set_val;
				}
			}
		}
	}

	function CheckInteger(field) 
	{
		if (IsOK) 
		{
			if (field) 
			{
				if(!IntegerChars(field.value)) 
				{
					ErrorMess(field, message[1]);
				}
			}
		}		
	}

	function CheckDecimal(field) 
	{
		if (IsOK) 
		{
			if (field) 
			{
				if(!DecimalChars(field.value)) 
				{
					ErrorMess(field, message[2]);
				}
			}
		}		
	}

	function CheckDate(field) 
	{
		if (IsOK) 
		{
			if (field) 
			{
				if (field.value != '') 
				{
					if(!GetDate(field, NavigatorName())) 
					{
						ErrorMess(field, message[3]);
					}
				}
			}
		}		
	}

	function CheckDateTime(field) 
	{
		if (IsOK) 
		{
			if (field) 
			{
				if (field.value != '') 
				{
					if(!GetDate(field, NavigatorName())) 
					{
						ErrorMess(field, message[7]);
					}
				}
			}
		}		
	}

	function CheckTwoDatesRange(field, field2) 
	{
		if (IsOK) 
		{
			if (field && field2) 
			{
				dateFrom = GetDate(field, NavigatorName());
				dateTo 	 = GetDate(field2, NavigatorName());
				if(dateFrom > dateTo) 
				{
					ErrorMess(field2, message[5]);
				}
			}
		}		
	}

	function CheckEmail(field) 
	{
		if (IsOK) 
		{
			if (field.value) 
			{
				if (window.RegExp) 
				{
		    	// Rules for the email regular expression:
		    	//  The start of the email must have at least one character before the @ sign
		    	//  There may be either a . or a -, but not together before the @ sign
		    	//  There must be an @ sign
		    	//  At least once character must follow the @ sign
		    	//  There may be either a . or a -, but not together in the address
		    	//  The address must end with a . followed by at least 2 characters
		    	var re;
		    	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
		    
					if (re.test(field.value) == false) 
					{
						ErrorMess(field, message[6]);
					}
				}
			}
		}
	}

	function CheckHexColor(field, length) 
	{
		if (IsOK) 
		{
			if (field) 
			{
        if(!HexChars(field.value))
				{
					errorMess(field, message[4]);
				}
				else if (field.value.length != length)
				{
					errorMess(field, message[4]);
				}
				/*if (field.value != '' && (field.value.length != length || field.value.charAt(0) != '#'))
				{
					ErrorMess(field, message[4]);
				}
				else
				{
					value = field.value.substring(1);
					if(!HexChars(value))
					{
						ErrorMess(field, message[4]);
					}
				}*/
			}
		}
	}

	function CheckSmallAscii(field)
	{
		if (IsOK) 
		{
			if (field) 
			{
				if(!SmallAlfaAsciiChars(field.value)) 
				{
					ErrorMess(field, message[8]);
				}
			}
		}		
	}

	function HexChars(value) 
	{
		var good_chars = "0123456789abcdefABCDEF";
		return CheckGoodChars(good_chars, value);
	}

	function SmallAlfaAsciiChars(value) 
	{
		var good_chars = "qwertyuiopasdfghjklzxcvbnm";
		return CheckGoodChars(good_chars, value);
	}

	function IntegerChars(value) 
	{
		var good_chars = "-0123456789";
		return CheckGoodChars(good_chars, value);
	}

	function DecimalChars(value) 
	{
		var delim = '.';
		var good_chars = "0123456789" + delim;
		var dot_check = (value.indexOf(delim) == value.lastIndexOf(delim));
		return (CheckGoodChars(good_chars, value) && dot_check);
	}

	function CheckGoodChars(chars, value) 
	{
		if (value != "") 
		{
			var len = value.length;
			for (i = 0; i < len; i++) 
			{
				if (chars.indexOf(value.charAt(i)) == -1) 
				{
					return false;
				}
			}
		}
		return true;
	}

	function CheckCheckboxList (frm, start, mess) 
	{
		if (IsOK) {
			if (!CheckboxList(frm, start)) 
			{
				ErrorMess(false, message[mess]);
			}
		}
	}

	function ErrorMess(field, mess) 
	{
		var id;
		var labelTag;
		IsOK = false;

		//focus
		if (field) 
		{
			field.focus();
		}
		//select text
		if (field.type) 
		{
			if (field.type == 'text') 
			{
				field.select();
			}
		}
		//label
		if (field.id != '') 
		{
			id = field.id;
		}
		else 
		{
			id = field.name;
		}
		if (document.getElementById) 
		{
			id = 'l.' + id;
			labelTag = document.getElementById(id);
			if (labelTag) 
			{
				if (labelTag.innerHTML) 
				{
					mess = '[' + labelTag.innerHTML + ']  ' + mess;
				}
			}
		}
		if (!ShowInfoBox(mess))
		{
			alert(mess);
		}
	}
	
	function ShowInfoBox(mess)
	{
		var elem = document.getElementById('infoBox');
		if (elem)
		{
			elem.style.display = '';
			elem.innerHTML = mess;
			return true;
		}
		return false;
	}

	function CheckboxList(frm, start_index) {
		//at least one checkbox has to be checked
		var end_index = start_index + parseInt(frm.elements.value) - 1;
		var checked = false;
		for (i = start_index; i <= end_index; i++) 
		{
			if (frm[i].checked) 
			{
				checked = true;
				break;
			}
		}
		return checked;
	}

	function SelectedValue(field) 
	{
		if (field) 
		{
			if (field.options) 
			{
				return field.options[field.selectedIndex].value;
			}
		}
	}

	//*********** Date check - return js Date object on True *************
	function GetDate(field, navName) 
	{
		var j = 0;
   
		CheckedDate = field.value;
		
		//Length check - between 4 and 16 is right (d.m. || dd.mm.YYYY || dd.mm.YYYY HH:ii )
		if ((CheckedDate.length > 16) || (CheckedDate.length < 4)) 
		{
			return false;
		}
   
		//Separators check - EUR two dots is ok
		var separator = ".";
		var pos1, pos2;
      
		for (var i = 0; i < CheckedDate.length; i++) 
		{
			if (CheckedDate.substring(i, i + 1) == separator) 
			{
				j++;
				if (j == 1)  pos1 = i;
				if (j == 2)  pos2 = i;
			}
		}
             
		if (j != 2) 
		{
			return false;
		}
	    
		//** Date Format Validity **
		Day = CheckedDate.substring(0, pos1);
		Month = CheckedDate.substring(pos1 + 1, pos2);
		
		//if time
		pos3 = CheckedDate.indexOf(' ');
		pos4 = CheckedDate.indexOf(':');
		if (pos3 != -1 && pos4 != -1)
		{
			Year = CheckedDate.substring(pos2 + 1, pos3);
			Hour = CheckedDate.substring(pos3 + 1, pos4);
			Minute = CheckedDate.substring(pos4 + 1, CheckedDate.length);
		}
		else
		{
			Year = CheckedDate.substring(pos2 + 1, CheckedDate.length);
			Hour = 0;
			Minute = 0;
		}

		if (Year.length != 4) 
		{
			//fill current year if missing
			CurrentDate = new Date();
			Year = Math.abs(CurrentDate.getYear());
			if (Year < 200)
			{
				Year = Year + 1900 + "";
			}
			field.value = CheckedDate + Year;

			//return false;
		}

		//with zero start octal numbers !
		if (Month.substring(0, 1) == "0" && Month.length == 2)  
		{
			Month = Month.substring(1, 2);
		}
		if (Day.substring(0, 1) == "0" && Day.length == 2)
		{
			Day = Day.substring(1, 2);
		}
		if (Hour != 0 && Hour.substring(0, 1) == "0" && Hour.length == 2)
		{
			Hour = Hour.substring(1, 2);
		}
		if (Minute != 0 && Minute.substring(0, 1) == "0" && Minute.length == 2)
		{
			Minute = Minute.substring(1, 2);
		}

		CompDate = new Date(Year, Month - 1, Day, Hour, Minute, 0);

		if ((Year.substring(0,2) == "19") || (navName == "netscape")) 
		{  
			CompYear = (Math.abs(CompDate.getYear())) + 1900;
		} 
    else 
		{ 
			CompYear = Math.abs(CompDate.getYear());
		}
		
		//alert(Year + "," + Month + "," + Day + "," + Hour + "," + Minute);
      
		//Isn't date 
		if ((CompYear != parseInt(Year)) || (CompDate.getMonth()+1) != parseInt(Month) || (CompDate.getDate() != parseInt(Day))) 
		{
			return false;
		}
		//time
		if (CompDate.getHours() != parseInt(Hour) || CompDate.getMinutes() != parseInt(Minute))
		{
			return false;
		}

		return CompDate;
	}

	//browser check ------------
	function NavigatorName() 
	{ 
		if ((navigator.appName.toLowerCase().indexOf("netscape") != "-1") 
				|| (navigator.userAgent.toLowerCase().indexOf("opera") != "-1")) 
		{
			navName = "netscape";
		}
		else 
		{  
			navName = "explorer";
		}
		return navName;
	}

	//Print page ---------------
	function DoPrint() 
	{
		var Name = NavigatorName();
		var ver = navigator.appVersion;
		var mess = "You have old version of WWW client. Please use print function from browser."; 

		if (Name =="netscape" && parseInt(ver) < 4)  
		{
			alert(mess);
		}
		else if (Name =="explorer" && ver.charAt(ver.indexOf('MSIE')+5) < 5)  
		{
			alert(mess);
		}
		else 
		{
			window.print();
		}
	}

	//PopUp Window ---------------
	function PopUp(href, width, height) 
	{
		var newWin = window.open(href, 'popup', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width='+ width +',height='+ height, true);
		
		newWin = window.open(href, 'popup');	//REDUNDANT CALL TO FIX MAC BUG
	}
	
	function SendForm(frm)
	{
		if (frm.onsubmit())
		{
			frm.submit();
		}
	}

//-->
