function isInteger(numbertotest) {
    var IsFound = /^-?\d+$/.test(numbertotest); return IsFound;
}

function isPositiveInteger(numbertotest) {
    var IsFound = /^\d+$/.test(numbertotest); return IsFound;
}

function isDouble(numbertotest) {
    var pattern = /^-?\d+.?\d*$/;
    if ( numbertotest.match(pattern)==null )
        return false;
    else
        return true;
}

function isPositiveDouble(numbertotest) {
    var pattern = /^\d+.?\d*$/;
    if ( numbertotest.match(pattern)==null )
        return false;
    else
        return true;
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}

function removeHTMLTags(strInputCode){
    strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){ return (p1 == "lt")? "<" : ">"; });
    var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
    //alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);
    return strTagStrippedText;
}

function round_number(number, dec_places)
{
    //(c) Copyright 2008, Russell Walker, Netshine Software Limited. www.netshinesoftware.com
    //Version 2.0. Change log:
    //18/12/08 Fixed bug where digits after decimal point greater than 995
    //29/01/09 Added support for negative numbers (symmetrical rounding) and strip white space
    var new_number = '';
    var i = 0; //Just used in loops
    var sign = ""; //If negative, a minus sign will be prefixed to the result
    number = number.toString(); //We need to operate on and return a string, not a number
    number = number.replace(/^\s+|\s+$/g, ''); //Remove any excess white space

    //Do we have a negative number?
    if (number.charCodeAt(0) == 45) //minus sign
    { sign = '-'; number = number.substr(1).replace(/^\s+|\s+$/g, ''); }

    dec_places = dec_places * 1; //We need an integer
    dec_point_pos = number.lastIndexOf(".");

    //If there is nothing before the decimal point, prefix with a zero
    if (dec_point_pos == 0) { number = "0" + number; dec_point_pos = 1; }

    //Has an integer been passed in?
    if (dec_point_pos == -1 || dec_point_pos == number.length - 1) {
        if (dec_places > 0) {
            new_number = number + ".";
            for(i=0; i<dec_places; i++) { new_number += "0"; }
            if (new_number == 0) { sign = ""; }
            return sign + new_number;
        } else {
            return sign + number;
        }
    }

    //Do we already have the right number of decimal places?
    var existing_places = (number.length - 1) - dec_point_pos;
    if (existing_places == dec_places) {
        return sign + number; //If so, just return the input value
    }

    //Do we already have less than the number of decimal places we want?
    if (existing_places < dec_places) {
        //If so, pad out with zeros
        new_number = number;
        for(i=existing_places; i<dec_places; i++) { new_number += "0"; }
        if (new_number == 0) { sign = ""; }
        return sign + new_number;
    }

    //Work out whether to round up or not
    var end_pos = (dec_point_pos * 1) + dec_places;
    var round_up = false; //Whether or not to round up (add 1 to) the next digit along
    if ((number.charAt(end_pos + 1) * 1) > 4) { round_up = true; }

    //Record each digit in an array for easier manipulation
    var digit_array = new Array();
    for(i=0; i<=end_pos; i++) { digit_array[i] = number.charAt(i); }

    //Round up the last digit if required, and continue until no more 9's are found
    for(i=digit_array.length - 1; i>=0; i--) {
        if (digit_array[i] == ".") { continue; }
        if (round_up) {
            digit_array[i]++;
            if (digit_array[i] < 10) { break; }
        } else {
            break;
        }
    }

    //Reconstruct the string, converting any 10's to 0's
    for (i=0; i<=end_pos; i++) {
        if (digit_array[i] == "." || digit_array[i] < 10) {
            new_number += digit_array[i];
        } else {
            new_number += "0";
        }
    }

    //If there are no decimal places, we don't need a decimal point
    if (dec_places == 0) { new_number = new_number.replace(".", ""); }

    if (new_number == 0) { sign = ""; }

    //That should do it!
    return sign + new_number;
}

function getMouseCoord(evt) {
    var IE=false;
    if(navigator.appName == "Microsoft Internet Explorer") IE = true;
    if(IE) {
        x = evt.clientX + document.body.scrollLeft;
        y = evt.clientY + document.body.scrollTop;
    } else {
        x = evt.pageX;
        y = evt.pageY;
    }
    return [ x , y ];
}
    
function setComboSelected(comboid, text) {
    var combo = document.getElementById(comboid);
    for (var i=0 ; i < combo.length; i++) {
        if (combo[i].text==text) { combo.options[i].selected = true; }
    }
}

function unsetComboSelected(comboid, text) {
    var combo = document.getElementById(comboid);
    for (var i=0 ; i < combo.length; i++) {
        if (combo[i].text==text) { combo.options[i].selected = false; }
    }
}

function setComboAllSelected(comboid) {
    var combo = document.getElementById(comboid);
    for (var i=0 ; i < combo.length; i++) {
        combo.options[i].selected = true;
    }
}

function setComboAllUnSelected(comboid) {
    var combo = document.getElementById(comboid);
    for (var i=0 ; i < combo.length; i++) {
        combo.options[i].selected = false;
    }
}

function setComboSelectionDisable(comboid, text, truefalse) {
    var combo = document.getElementById(comboid);
    for (var i=0 ; i < combo.length; i++) {
        if (combo[i].text==text) { combo.options[i].disabled = truefalse; }
    }
}
    


/*function getMonth(index)
{
   var months = new Array(13);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";
   return months[index]
}*/

function emailcheck(str) {
    // false = Invalid E-mail ID
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1) return false;
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
    if (str.indexOf(at,(lat+1))!=-1) return false;
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
    if (str.indexOf(dot,(lat+2))==-1) return false;
    if (str.indexOf(" ")!=-1) return false;
    return true					
}

function checkemail(str){
    // false = Invalid E-mail ID
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
        return true;
    else{
        return false;
    }
}

var UTF8 = {
    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

// ===========================================

function checkPositiveNumber(id, max, alternative) {
    if (!isPositiveInteger(document.getElementById(id).value)) document.getElementById(id).value = (alternative==null?"0":alternative);
    if (max!=null) if (document.getElementById(id).value > max) document.getElementById(id).value = max;
}

function checkPositiveDecimal(id, max, alternative) {
    if (!isPositiveDouble(document.getElementById(id).value)) document.getElementById(id).value = (alternative==null?"0.00":alternative);
    if (max!=null) if (document.getElementById(id).value > max) document.getElementById(id).value = max;
}

function getSelectedCheckboxValues(buttonGroup) {
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = buttonGroup[i].value;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = buttonGroup.value; // return zero as the only array value
      }
   }
   return retArr;
}

function styleTable1(id) {
    var table = document.getElementById(id);
    table.style.border = 0;
    table.style.backgroundColor = "#D5D3CA"; //#D0D0D0
    var trs = table.getElementsByTagName("tr");
    for (i=0; i<trs.length; i++) {
        if (i % 2 == 1) { trs[i].style.backgroundColor = "#FFFFFF"; } else { trs[i].style.backgroundColor = "#F4F4F4"; }
        //trs[i].style.backgroundColor = "#F7F7F7";
        var tds = trs[i].getElementsByTagName("td");
        for (var j=0; j<tds.length; j++) {
            tds[j].style.border = '1px solid white';
        }
        var ths = trs[i].getElementsByTagName("th");
        for (var j=0; j<ths.length; j++) {
            ths[j].style.border = '1px solid white';
        }
    }
    var theads = table.getElementsByTagName("thead");
    if (theads.length>0) {
        //trs[0].style.backgroundColor = "#D5D3CA"; //#D0D0D0
        for (i=0; i<theads.length; i++) {
            var trs = theads[i].getElementsByTagName("tr");
            for (j=0; j<trs.length; j++) {
                trs[j].style.backgroundColor = "#D5D3CA"; //#D0D0D0
            }
        }
    }
    var tfoots = table.getElementsByTagName("tfoot");
    if (tfoots.length>0) {
        //trs[trs.length-1].style.backgroundColor = "#D5D3CA"; //#D0D0D0
        for (i=0; i<tfoots.length; i++) {
            var trs = tfoots[i].getElementsByTagName("tr");
            for (j=0; j<trs.length; j++) {
                trs[j].style.backgroundColor = "#D5D3CA"; //#D0D0D0
            }
        }
    }
}


// ===========================================
// Not so standard - dependent on other files
// ===========================================

function checkDate(id) {
    var formats = new Array("yyyy-M-d","yyyy/M/d","MMM d, y","MMM d y","d MMM y","d/M/y","d-M-y","yyyy-MM-dd");
    var datestr = document.getElementById(id).value;
    var error = true;
    for (i=0; i<formats.length; i++) {
        if (isDate(datestr,formats[i])) {
            document.getElementById(id).value = formatDate(new Date(getDateFromFormat(datestr,formats[i])),"yyyy-MM-dd");
            error = false;
            break;
        }
    }
    if (error) document.getElementById(id).value = "";
}

