    function toupper(str, upper)
    /*
            Convert text to upper if upper is TRUE otherwise to lower
    */
    {
        if (upper==true)
        {
            str.value=str.value.toUpperCase();
        } else {
            str.value=str.value.toLowerCase();
        }
    }    
    function Left(str, n)
    /*
            IN: str - the string we are LEFTing
                n - the number of characters we want to return

            RETVAL: n characters from the left side of the string
    */
    {
            if (n <= 0)     // Invalid bound, return blank string
                    return "";
            else if (n > String(str).length)   // Invalid bound, return
                    return str;                // entire string
            else // Valid bound, return appropriate substring
                    return String(str).substring(0,n);
    }
    function Right(str, n)
    /*
            IN: str - the string we are RIGHTing
                n - the number of characters we want to return

            RETVAL: n characters from the right side of the string
    */
    {
            if (n <= 0)     // Invalid bound, return blank string
               return "";
            else if (n > String(str).length)   // Invalid bound, return
               return str;                     // entire string
            else { // Valid bound, return appropriate substring
               var iLen = String(str).length;
               return String(str).substring(iLen, iLen - n);
            }
    }
    function Len(str)
    /*
            IN: str - the string whose length we are interested in

            RETVAL: The number of characters in the string
    */
    {  
            return String(str).length;  
    }
    function Mid(str, start, len)
    /*
            IN: str - the string we are LEFTing
                start - our string's starting position (0 based!!)
                len - how many characters from start we want to get

            RETVAL: The substring from start to start+len
    */
    {
            // Make sure start and len are within proper bounds
            if (start < 0 || len < 0) return "";

            var iEnd, iLen = String(str).length;
            if (start + len > iLen)
                    iEnd = iLen;
            else
                    iEnd = start + len;

            return String(str).substring(start,iEnd);
    }
    function InStr(strSearch, charSearchFor)
    /*
    InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                               was found in the string str.  (If the character is not
                               found, -1 is returned.)
                               
    Requires use of:
        Mid function
        Len function
    */
    {
        for (i=0; i < Len(strSearch); i++)
        {
            if (charSearchFor == Mid(strSearch, i, 1))
            {
                return i;
            }
        }
        return -1;
    }    

    function toggleLayer(sLayer)
    /*
        Hide or show the sLayer
    */
    {
        var elem, vis;
    
        if( document.getElementById )
        { // this is the way the standards work
            elem = document.getElementById( sLayer );
        } else if ( document.all )
            { // this is the way old msie versions work
                elem = document.all[sLayer];
            } else if ( document.layers )
                { // this is the way nn4 works
                    elem = document.layers[sLayer];
                }
      
        vis = elem.style;
        // if the style.display value is blank we try to figure it out here
        if(vis.display == '' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)
        {
            vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
        }
        vis.display = (vis.display == '' || vis.display == 'block') ? 'none' : 'block';
    }

    function seterr(objName2,error)
    {
        var colorfield = objName2;
        if (error)
        {
            if (colorfield.style.color != "red")
            {
                document.forms.gesrec.numerr.value++;
            }
            colorfield.style.color = "red";
        } else {
            if (colorfield.style.color == "red")
            {
                document.forms.gesrec.numerr.value--;        
            }
            colorfield.style.color = "#333333";
        }
    }

    function subfrm(frmName)
    {
        var reffrm = frmName;
        if (reffrm.numerr.value != 0)
        {
            alert('Form contains ' + reffrm.numerr.value + ' errors, please correct them before resubmitting!!!');
        } else {
            reffrm.submit();
        }
    }

    function checkEAN(objName)
    {
        var eanfield = objName;
        var eanzero = '0000000000000';
        var totale = 0;
        
        if (eanfield.value.length != 13)
        {
            eanfield.value = eanzero.substr(0,13 - eanfield.value.length) + eanfield.value;
        }
        
        for (i = 0;i < 11;i = i + 2)
        {
            totale = totale + ( eanfield.value.substr(i,1) * 1 );
        }
        for (i = 1;i < 12;i = i + 2)
        {
            totale = totale + ( eanfield.value.substr(i,1) * 3 );
        }
        
        rightcheck = ( 10 - totale.toString().substr(totale.toString().length - 1,1));
        
        if ( rightcheck == 10) {
            rightcheck = 0;
        }
        
        if ( rightcheck != eanfield.value.substr(12,1))
        {
            alertsay = "The EAN Code Check Digit should be " + rightcheck;
            alert(alertsay);
            seterr(eanfield,true)
            return false;
        } else {
            seterr(eanfield,false)
            return true;
        }
    }
    
    function checkNoNull(objName)
    {
        var stringfield = objName;
        if (stringfield.length == 0 || stringfield.value == "" || stringfield.value == " ")
        {
            alertsay = "Please enter a value in the \"" + stringfield.name + "\" field."
            alert(alertsay);
            seterr(stringfield,true)
            return false;
        } else {
            seterr(stringfield,false)
            return true;
        }
    }
    
    function checkNumeric(objName,minval, maxval)
    {
        var numberfield = objName;
        if (chkNumeric(objName,minval,maxval) == false)
        {
            seterr(numberfield,true)
            return false;
        } else {
            seterr(numberfield,false)
            return true;
        }
    }
    
    function chkNumeric(objName,minval,maxval)
    {
        //If blank is supposed 0
        if (objName.value == '')
        {
            objName.value = '0';        
        }

        // only allow 0-9 be entered
        var checkOK = "0123456789";
        var checkStr = objName;
        var allValid = true;
        var decPoints = 0;
        var allNum = "";

        for (i = 0;  i < checkStr.value.length;  i++)
        {
            ch = checkStr.value.charAt(i);
            for (j = 0;  j < checkOK.length;  j++)
            if (ch == checkOK.charAt(j))
            break;
            if (j == checkOK.length)
            {
                allValid = false;
                break;
            }
            if (ch != ",")
               allNum += ch;
        }
        if (!allValid)
        {   
            alertsay = "Please enter only these values \""
            alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
            alert(alertsay);
            return (false);
        }
    
        // set the minimum and maximum
        var chkVal = allNum;
        var prsVal = parseInt(allNum);
        if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))  
        {
            alertsay = "Please enter a value greater than or "
            alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
            alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
            alert(alertsay);
            return (false);
        }
    }

    var dtCh= "-";
    var minYear=1900;
    var maxYear=3000;

    function isInteger(s)
    {
        var i;
        for (i = 0; i < s.length; i++)
        {   
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) return false;
        }
        // All characters are numbers.
        return true;
    }

    function stripCharsInBag(s, bag)
    {
        var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++)
        {   
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }

    function daysInFebruary(year)
    {
        // February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    }
    
    function DaysArray(n)
    {
        for (var i = 1; i <= n; i++)
        {
            this[i] = 31
            if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
            if (i==2) {this[i] = 29}
       } 
       return this
    }

    function checkDate(dtObj,yesnull)
    {
        var dtStr=dtObj.value;
        var daysInMonth = DaysArray(12)
        var pos1=dtStr.indexOf(dtCh)
        var pos2=dtStr.indexOf(dtCh,pos1+1)
        var strYear=dtStr.substring(0,pos1)
        var strMonth=dtStr.substring(pos1+1,pos2)
        var strDay=dtStr.substring(pos2+1)
        strYr=strYear
        if (dtStr.length == 0 && yesnull == true)
        {
            seterr(dtObj,false)
            return true                
        }
        if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
        if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
        for (var i = 1; i <= 3; i++)
        {
            if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
        }
        month=parseInt(strMonth)
        day=parseInt(strDay)
        year=parseInt(strYr)
        if (pos1==-1 || pos2==-1)
        {
            alert("The date format should be : yyyy-mm-dd in the \"" + dtObj.name + "\" field")
            seterr(dtObj,true)
            return false
        }
        if (strMonth.length<1 || month<1 || month>12)
        {
            alert("Please enter a valid month in the \"" + dtObj.name + "\" field")
            seterr(dtObj,true)
            return false
        }
        if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
        {
            alert("Please enter a valid day in the \"" + dtObj.name + "\" field")
            seterr(dtObj,true)
            return false
        }
        if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
        {
            alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+" in the \"" + dtObj.name + "\" field")
            seterr(dtObj,true)
            return false
        }
        if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
        {
            alert("Please enter a valid date in the \"" + dtObj.name + "\" field")
            seterr(dtObj,true)
            return false
        }
        seterr(dtObj,false)
        return true
    }
    
    function suppress()
    {
        return true;
    }
    
    function checkform(frmNameBlur)
    {
        
        var oEle = frmNameBlur.elements;

        for (cInd = 0; cInd < oEle.length; cInd++)
        {
            if (oEle[cInd].onblur != undefined)
            {
                oEle[cInd].onblur();
            }
        }
    
    }

    function SelAll()
    {

		var frm = document.forms["addact"];

		for (var i=0;i<frm.elements.length;i++)
		{
			var e=frm.elements[i];
			if ((e.name != 'delbef') && (e.name != 'chkall') && (e.type=='checkbox'))
			{
                e.checked = document.forms["addact"].elements["chkall"].checked;
			}
		}
        
    }
