// checks and controls maxlength of a textarea 
function isMaxLength(obj, max_length) 
{ 
  if(obj.value.length > max_length)
    obj.value = obj.value.substring(0,max_length) 
} 

function confirmAction(message)
{
  var agree = confirm(message);

  if(agree)
    return true;
  else
    return false;
} 

// by Peter-Paul Koch & Alex Tingle 
// http://blog.firetree.net/2005/07/04/javascript-find-position/
// finds the x and y position of an html element

function findPosX(obj) {
  var curleft = 0;
  if(obj.offsetParent) {
    while(1) {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
	}
  else if(obj.x)
    curleft += obj.x;
  return curleft;
} 

// by Peter-Paul Koch & Alex Tingle 
// http://blog.firetree.net/2005/07/04/javascript-find-position/
// finds the x and y position of an html element

function findPosY(obj) {
  var curtop = 0;
  if(obj.offsetParent) {
    while(1){
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
	}
  else if(obj.y)
    curtop += obj.y;
  return curtop;
} 

// scrolls to passed element
function setFocus(element,ydisplace)
{
  document.getElementById(element).focus();
	var y = findPosY(document.getElementById(element)) - ydisplace;
	window.scrollTo(0,y);
} 

/* pass an input field id and length, once length is reached will tab to next element on form. 
   assumes only one form on document. */
function maxLen(field,len) 
{
  var nextElementIndex = getElementIndex(document.getElementById(field)) + 1;
	
  if(document.getElementById(field).value.length >= len){
	  
		// checks to see if nextElement exists
	  if(typeof document.forms[0].elements[nextElementIndex] == 'object') {
			try{
			  err = document.forms[0].elements[nextElementIndex].focus();
			}
			catch(err){
				//catch exceptions here, needed for function not to throw error warnings when focus goes from
				//input to select
			}
		}
	}
} 

/* unhilites all the elements of the form, changes from yellow to white */
function unhilite(form) {
	  var len = document.getElementById(form).elements.length;
		for(i=0;i<len;i++){
		  if((document.getElementById(form).elements[i].style.backgroundColor == "yellow")||(document.getElementById(form).elements[i].style.backgroundColor == "#ffff00"))
			  document.getElementById(form).elements[i].style.backgroundColor = "white"
		}
} 

/* unhilites all the elements of the form, changes from yellow to white */
function unhilite2(formIndex) {
	  var len = document.forms[formIndex].elements.length;
		for(i=0;i<len;i++){
		  if((document.forms[formIndex].elements[i].style.backgroundColor == "yellow")||(document.forms[formIndex].elements[i].style.backgroundColor == "#ffff00"))
			  document.forms[formIndex].elements[i].style.backgroundColor = "white"
		}
}
 

function unhiliteRadio(radio_group)
{
  var element = "lbl_"+radio_group;
	
  if((document.getElementById(element).style.backgroundColor == "yellow")||(document.getElementById(element).style.backgroundColor == "#ffff00"))
			  document.getElementById(element).style.backgroundColor = "white";
} 

/* This highlights the text box to Yellow -  You can put in any color!!!! */
function hilite(field) 
{
	document.getElementById(field).style.backgroundColor="yellow";
} 

/* checks field to see if empty */
function checkEmpty(field) 
{
  if(document.getElementById(field).value == ""){
	  hilite(field);
	  return true;
	}
} 

/* checks field to see if empty */
function checkEmpty2(field) 
{
  if(document.getElementById(field).value == "")
	  return true;
} 

/* checks to see if null value is not selected */
function checkSelect(field) {
	var val = document.getElementById(field).options[document.getElementById(field).options.selectedIndex].value;
	if(val == ""){
		hilite(field);
		return true;
  }
} 

/* checks to see if null value is not selected */
function checkSelect2(field) 
{
	var val = document.getElementById(field).options[document.getElementById(field).options.selectedIndex].value;
	if(val == "")
		return true;
} 

function checkRadio(radio_group,size)
{
  var errFlag = true;
	var field,label;
	
	label = "lbl_" + radio_group;
	
  for(var i=1;i<=size;i++)
	{
	  field = radio_group + i;
		if(document.getElementById(field).checked == true)
		  return false;
	}
	
	hilite(label);
	return true;
} 

/* checks to see if all values are numeric with length = len	*/
function checkNum(field,len) {
  // creates regular expression string
  regex = new RegExp("^[0-9]{"+len+"}$");
	
	// compares regular expression to a given string
	var x = regex.exec(document.getElementById(field).value);
	
	if(x == null){
	    hilite(field);
		  return true;
	}
} 

/* checks to see if all values are numeric with length = len	*/
function checkNum2(field,len) {
  // creates regular expression string
  regex = new RegExp("^[0-9]{"+len+"}$");
	
	// compares regular expression to a given string
	var x = regex.exec(document.getElementById(field).value);
	
	if(x == null)
		  return true;
} 

/* checks to see if all values are numeric */
function checkNum3(field) {
  // creates regular expression string
  regex = new RegExp("^[0-9]+$");
	
	// compares regular expression to a given string
	var x = regex.exec(document.getElementById(field).value);
	
	if(x == null){
	    hilite(field);
		  return true;
	}
} 

/* checks to see if all values are numeric */
function checkNum4(field) {
  // creates regular expression string
  regex = new RegExp("^[0-9]+$");
	
	// compares regular expression to a given string
	var x = regex.exec(document.getElementById(field).value);
	
	if(x == null)
		  return true;
}
 

/* checks to see if all values are numeric with whole and decimal part	*/
function checkCurrency(field) {
  // creates regular expression string
  regex = new RegExp("^[0-9]+[\.]{0,1}[0-9]{0,2}$");
	
	// compares regular expression to a given string
	var x = regex.exec(document.getElementById(field).value);
	
	if(x == null){
	    hilite(field);
		  return true;
	}
} 

/* checks to see if all values are numeric with whole and decimal part	*/
function checkCurrency2(field) {
  // creates regular expression string
  regex = new RegExp("^[0-9]+[\.]{0,1}[0-9]{0,2}$");
	
	// compares regular expression to a given string
	var x = regex.exec(document.getElementById(field).value);
	
	if(x == null)
		  return true;
} 

// CSS Browser Selector   v0.2.5
// Documentation:         http://rafael.adm.br/css_browser_selector
// License:               http://creativecommons.org/licenses/by/2.5/
// Author:                Rafael Lima (http://rafael.adm.br)
// Contributors:          http://rafael.adm.br/css_browser_selector#contributors
// http://rafael.adm.br/css_browser_selector/ 

var css_browser_selector = function() {
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
	var c=b+os+' js';
	h.className += h.className?' '+c:c;
}(); 

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Changes:
/* 1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.

1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to 
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.

1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).

1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the 
restriction that an address must end in a two or three letter
word.

1.1: Rewrote most of the function to conform more closely to RFC 822.

1.0: Original  */
// -->

<!-- Begin
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//  End --> 

var Spell_Win_Name;
var Work_Form_Name;
var Work_Field_Name;

function SpellCheck(form_name,field_name) 
{
  //¡Àa*o`a*i'a* i'a` o^i^©£i`a`o`a` a^u'a^ i"©£i^i`a*i'e"e`a^a`o`a` textform
  //a`e^i^ o^i^©£i`a`o`a` n~a* e^a`c,a^a`: proba 
  //a` i"i^e"a*o`i^ c,a` i"©£i^a^a*©£e^a` a*: description_short
  //i"©£a`a^e`e"i'i^o`i^ e`c,i"i^e"c,a^a`i'a* o`©£y"a'a^a` a"a` a'u'a"a*
  //var textform = self.document.proba.description_short.value;
  //var textform = self.document["proba"]["description_short"].value;
  
  var textform = self.document[form_name][field_name].value;
  
  //textform = "dfghfhfghfghfgh";
  //i^a'©£a`o`i'i^ c,a`i"e`n~a^a`i'a* a^ o^i^©£i`a`o`a`
  //self.document[form_name][field_name].value = "alabala i towa e";
  //n~u'i^a'u`a*i'e`a*
  //alert (textform);
  
  //open popup window and submit to spell_check.php ONLY
  //1. IF popup window is not open or popup window was closed
  //2. OR click comes from another form-field
  
  //otherwise spellcheck click just focus popup window
  function win_pop(URL)
  {	
    winname=window.open(URL,'WIN','width=600,height=380,left=210,top=210,resizable=yes,scrollbars=yes,status=yes'); 
		return winname;
  }
	
  if ( ! Spell_Win_Name || Spell_Win_Name.closed || (Work_Form_Name != form_name || Work_Field_Name != field_name))
  {
  Spell_Win_Name = win_pop('');
  if (Spell_Win_Name.focus) { Spell_Win_Name.focus(); }
  
  self.document.hidden_form.form_name.value = form_name; 	//w skritata forma se izpolzwa ime na pole form_name i field_name
  self.document.hidden_form.field_name.value = field_name;  	// koito nqmat nishto obshto s promrnliwite w tazi funkciq
  self.document.hidden_form.first_time_text.value = textform;
  self.document.hidden_form.submit();
  
  Work_Form_Name=form_name;
  Work_Field_Name=field_name;
  }
  else
  {
  if (Spell_Win_Name.focus) { Spell_Win_Name.focus(); }
  }
} 

function chg_display(frm)
{
	 var len = document.forms[frm].elements["display_loc"].length;
	 
	 for(i=len;i>0;i--)
			document.forms[frm].elements["display_loc"].remove(i-1);
	 	 
   if(document.forms[frm].elements["sec_id"].options[document.forms[frm].elements["sec_id"].selectedIndex].text.indexOf('Division') > 0)
	 {   
			if(document.all)
			{
				 document.forms[frm].elements["display_loc"].add(new Option("Please select","",false,false));
				 document.forms[frm].elements["display_loc"].add(new Option("Divisional and Departmental Calendars","a",false,false));
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental Calendar Only","i",false,false));
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental and Sub-departmental Calendars","s",false,false));
				 document.forms[frm].elements["display_loc"].add(new Option("No Display","n",false,false));
			}
			else //if(document.getElementById)
			{
				 document.forms[frm].elements["display_loc"].add(new Option("Please select","",false,false),null);
				 document.forms[frm].elements["display_loc"].add(new Option("Divisional and Departmental Calendars","a",false,false),null);
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental Calendar Only","i",false,false),null);
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental and Sub-departmental Calendars","s",false,false),null);
				 document.forms[frm].elements["display_loc"].add(new Option("No Display","n",false,false),null);
			} 
	 }
	 else
	 {
	    if(document.all)
			{	
	       document.forms[frm].elements["display_loc"].add(new Option("Please select","",false,false));
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental Calendar Only","i",false,false));
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental and Sub-departmental Calendars","s",false,false));
				 document.forms[frm].elements["display_loc"].add(new Option("No Display","n",false,false)); 
			}
			else //if(document.getElementById)
			{
			   document.forms[frm].elements["display_loc"].add(new Option("Please select","",false,false),null);
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental Calendar Only","i",false,false),null);
				 document.forms[frm].elements["display_loc"].add(new Option("Departmental and Sub-departmental Calendars","s",false,false),null);
				 document.forms[frm].elements["display_loc"].add(new Option("No Display","n",false,false),null); 
			}
	 }
} 

