function isArray(obj)
{
    return ( typeof ( obj.length ) == "undefined" ) ? false : true ;
}

function addListItem(submitValue, displayValue, optionBox) {
    optionBox.options.add(new Option(displayValue,submitValue,false,true));
}

function addListOption(submitValue, displayValue, optionBox) {
    var oOption = document.createElement("OPTION");
    oOption.text=displayValue;
    oOption.value=submitValue;
    optionBox.options.add(oOption);
}

function removeAllListItem(optionBox) {
    while ( optionBox.options.length > 0 ) {
        optionBox.options.remove(optionBox.options.length -1);
    }
}

function isValidSlashDate(element) {
    datePattern = new RegExp("^(([1][0-2])|([0]?[1-9]))\/(([0]?[1-9])|([1-2][0-9])|([3][0-1]))\/([1-3][0-9][0-9][0-9])$");
    if (element.value.match(datePattern) == null){
            return false;
    }
    return true;
}

function isChecked ( inThing )
{
    if ( isArray ( inThing ) )
    {
        for ( i = 0 ; i < inThing.length ; i++ )
        {
          if ( inThing [ i ].checked == true )
             return true ;
        }
    }
    else
    {
        if ( inThing.checked == true )
            return true ;
    }
    return false ;
}

function isEmpty(element) {
    if (element.value == null)
        return true;

    if (element.value == '')
        return true;

    for(var i=0;i<element.value.length;i++)
    {
        var c = element.value.charAt(i);
        if((c!=' ') && (c!= '\n') && (c!='\t'))
            return false;
    }
    return true;
}

function isValidPrice(element) {
    pricePattern = /^[$]?((\d+)|(\d{1,3}(\,\d{3})+))?(\.\d{0,3})*$/;
    //window.alert(pricePattern);
    if (element.value != '') {
        if (element.value.match(pricePattern) == null) {
            return false;
        }
    }
    return true;
}

//***********************************
// Div Content
//***********************************
function findElementById(id) {
    return main_findById(id);
}

function main_findById(id) {
    //alert("findElementById(" + id + ")");
    if (document.getElementById) { //ie5+ /ns6+
        obj = document.getElementById(id);
        return obj;
    }
    if (document.all){ //ie4+
        obj = document.all[id];
        return obj;
    }
    if (document.layers){ //ns4+
        obj = document.layers[id];
        return obj;
    }
    //alert("could not find element with id: " + id);
}

function updateDivContent(divid, newcontent) {
    main_setContent(divid, newcontent);
}

function main_setContent(elem, newcontent) {
    if (document.getElementById) { //ie5+ /ns6+
        obj = document.getElementById(elem);
       obj.innerHTML=newcontent;
        return true;
    }
    if (document.all){ //ie4+
        document.all[elem].innerHTML=newcontent;
        return true;
    }
    if (document.layers){ //ns4+
        with (document.layers[elem].document) {
            write(newcontent);
            close();
        }
    }
}

function getContent(elem) {
    if (document.getElementById) { //ie5+ /ns6+
        obj = document.getElementById(elem);
        return obj.innerHTML;
    }
    if (document.all){ //ie4+
        document.all[elem].innerHTML;
        return document.all[elem].innerHTML;
    }
    if (document.layers){ //ns4+
        with (document.layers[elem].document) {
            write(newcontent);
            close();
        }
    }
    return "notSupported";
}


function repositionDiv(divid, divleft, divtop, divwidth, divheight) {
    main_reposition(divid, divleft, divtop, divwidth, divheight);
}

function main_reposition(divid, divleft, divtop, divwidth, divheight) {
    var val = navigator.userAgent.toUpperCase()

    if (document.all){ //ie4+
        if (divleft>0) {
            document.all[divid].style.posLeft = divleft;
        }
        if (divtop>0) {
            document.all[divid].style.posTop = divtop;
        }
        if (divwidth>0) {
            document.all[divid].style.posWidth = divwidth;
        }
        if (divheight>0) {
            document.all[divid].style.posHeight = divheight;
        }
    }
    if (document.layers){ //ns4+
        if (divleft>0) {
            document.layers[divid].left = divleft;
        }
        if (divtop>0) {
            document.layers[divid].top = divtop;
        }
        if (divwidth>0) {
            document.layers[divid].width = divwidth;
        }
        if (divheight>0) {
            document.layers[divid].height = divheight;
        }
    }
}

function hideDiv(divId) {
    main_hide(divId);
}

function main_hide(elem) {
    if (document.getElementById) { //ie5+ /ns6
        obj = document.getElementById(elem);
        obj.style.visibility = "hidden";
        return true;
    }
    if (document.all) { //ie4+
        obj = document.all[elem].style.visibility = "hidden";
        return true;
    }

    if (document.layers) { //ns4
        document.layers[elem].layer.visibility = "hide";
        return true;
    }


    return false;
}

function showDiv(divId) {
    main_show(divId);
}

function main_show(elem) {
    if (document.getElementById) { //ie5+ /ns6
        obj = document.getElementById(elem);
        obj.style.visibility = "visible";
        return true;
    }
    if (document.all) {
        document.all[elem].style.visibility = "visible";
        return true;
    }
    if (document.layers) {
        document.layers[elem].layer.visibility = "show";
        return true;
    }
    return false;
}

function toggleDivVisibility(divId) {
    main_toggleVisibility(divId);
}

function main_toggleVisibility(elem) {
    if (document.getElementById) { //ie5+ /ns6
        obj = document.getElementById(elem);
        obj = obj.style;
        if (obj.visibility == "hidden") obj.visibility = "visible";
        else obj.visibility = "hidden";
        return true;
    }
    if (document.all) { //ie4+
        obj = document.all[elem].style
        if (obj.visibility == "hidden") obj.visibility = "visible";
        else obj.visibility = "hidden";
    }
    if (document.layers) { //ns4+
        obj = document.layers[elem].layer;
        if (obj.visibility == "hide") obj.visibility = "show";
        else obj.visibility = "hide";
    }
    return true;
}

function toggleDivDisplay(divId) {
    main_toggleDisplay(divId);
}

function main_toggleDisplay(elem) {
    if (document.getElementById) { //ie5+ / ns6+
        obj = document.getElementById(elem);
        obj = obj.style;
    }
    else if (document.all) obj = document.all[elem].style
    else if (document.layers) obj = document.layers[elem].layer;
    if (!obj) return false;

    if (obj.display == "none") obj.display = "";
    else obj.display = "none";

    return true;
}

function getSelected(elem) {
    index=elem.selectedIndex;
    if ( index != -1 ) {
        return elem[elem.selectedIndex].value;
    }
    return null;
}


function getSelectedRadio ( radio ) {
    if ( radio.length ) {
        for ( i=0; i < radio.length; i++ ) {
            if (radio[i].checked) {
                return radio[i].value;
            }
        }
    } else {
        if (radio.checked) {
            return radio.value;
        }
    }
    return null;
}

function setSelected(dropdown, selectValue, defaultPositionSelected) {
    if (selectValue != null) {
        for (x=0; x < dropdown.length; x++) {
            if (dropdown[x].value==selectValue) {
                dropdown[x].selected=true;
                return;
            }
        }
    } else if (defaultPositionSelected <= dropdown.length) {
        dropdown[defaultPositionSelected].selected=true;
        return;
    } else {
        dropdown[dropdown.length-1].selected=true;
        return;
    }
    dropdown[0].selected=true;
}

function setRadioSelected(radio, selectValue, defaultPositionSelected) {
    if (selectValue != null) {
        for (x=0; x < radio.length; x++) {
            if (radio[x].value==selectValue) {
                radio[x].checked=true;
                return;
            }
        }
    } else if (defaultPositionSelected <= radio.length) {
        radio[defaultPositionSelected].checked=true;
        return;
    } else {
        radio[radio.length-1].checked=true;
        return;
    }
    radio[0].checked=true;
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
}

function cleanMoneyField(formField) {
    if ( formField ) {
        formField.value=cleanMoney(formField.value);
    }
}


function cleanMoney(str) {
    str=replaceDelim(str,"-","");
    str=replaceDelim(str,"$","");
    str=replaceDelim(str,",","");
    str=replaceDelim(str,"#","");
    str=replaceDelim(str,"(","");
    str=replaceDelim(str,")","");
    str=replaceDelim(str," ","");
    return str;
}

function setNumberFormat(formField) {
    if ( formField ) {
        str=cleanMoney(formField.value);
        if ( str.length != 0 ) {
            formField.value=str;
            currencyFormat(formField);
        }
    }
}

function setNumberFormatNoSign(formField) {
    if ( formField ) {
        str=cleanMoney(formField.value)+"";
        if ( str.length != 0 ) {
            formField.value=str;
            currencyFormat(formField);
            formField.value=formField.value.replace("$","");
        }
    }
}

function currencyFormat(formField) {
    var textFormat = "";
    var t=0;
    var str="";

    formField.value=formField.value.replace(',','');

    strDec="";
    if ( formField.value.indexOf(".") > 0 ) {
        strDec=formField.value.substr(formField.value.indexOf("."), formField.value.length);
        formField.value=formField.value.substr(0,formField.value.indexOf("."));
    }
    str=formField.value;

    if (str.length != 0)
    {
        for (var k = str.length-1; k>=0 ; k--)
        {
            t++;
            if (t % 3 == 0)
            {
                textFormat = "," + str.substr(k,1) + textFormat;
            } else {
                textFormat =  str.substr(k,1) + textFormat;
            }
        }

        if (textFormat.substr(0,1) == ",")
        {
            formField.value = textFormat.substr(1,textFormat.length-1);
        } else {
            formField.value = textFormat;
        }
        formField.value="$"+formField.value + strDec;
    }
}

function replaceDelim(str, delim, newVal) {
    while (str.indexOf(delim) != -1) {
        str=str.replace(delim,newVal);
    }
    return str;
}


function cleanPhone(str) {
    str=replaceDelim(str,".","");
    str=replaceDelim(str,"-","");
    str=replaceDelim(str,"$","");
    str=replaceDelim(str,",","");
    str=replaceDelim(str,"#","");
    str=replaceDelim(str,"(","");
    str=replaceDelim(str,")","");
    str=replaceDelim(str," ","");
    return str;
}

function setPhoneFormat(formField) {

     if ( formField ) {
        str=cleanPhone(formField.value);
        if ( str.length != 0 ) {
            formField.value=str;
            phoneFormat(formField);
        }
    }
}

function phoneFormat(formField) {
    var str="";

    str=formField.value;
    formField.value="(";
    if (str.length >= 3 ) {
        formField.value+=str.substr(0,3);
        formField.value+=") ";
        str=str.substr(3,str.length);
        if (str.length >= 3) {
            formField.value+=str.substr(0,3);
            formField.value+="-";
            str=str.substr(3,str.length);
        }
    }
    formField.value+=str;

}
//***********************************
// JACK HUDSON--03/22/05
//***********************************
function gCheckEmptyByID(id,TorF)
{
    var Tb;
    var val;
    Tb=document.getElementById(id);
    val=Tb.value;

    if(val == "")
    {
        if(TorF)
        {
            alert("Required Field");
            Tb.focus();
        }
        return false;
    }

    return true;
}
//****************************************
function gCheckNumberByID(id,TorF)
{
    var Tb;
    var val;
    Tb=document.getElementById(id);
    //val=Tb.value;

    var val=parseFloat(Tb.value);
    if (isNaN(val))
    {
        if(TorF)
        {
            alert("Please Enter Numbers Only.");
            Tb.focus();
        }
        return false;
    }

    return true;
 }

// END JACK HUDSON--03/22/05
//****************************************
//JACK HUDSON--03/23/05
function gCheckDateByID(id,TorF)
{

    var Tb;
    var val;

    var dt=null;
    Tb=document.getElementById(id);
    val=Tb.value;

    if(!isValidSlashDate(Tb))
    {
        if(TorF)
        {
            alert("Please Enter a Valid Date.\nmm/dd/yyyy");
            Tb.focus();
        }
        return false;
    }

    return true;
 }
//***********************************
function gCheckDateByOBJ(obj,TorF)
{
    if(obj.value==""){return true;}

    obj.value=trim(obj.value)


    if(!isValidSlashDate(obj))
    {
        if(TorF)
        {
            alert("Please Enter a Valid Date.\nmm/dd/yyyy");
            obj.focus();
        }
        return false;
    }

    return true;
 }
//***********************************
//END JACK HUDSON--03/23/05
/*****************************/
//****************************************
//JACK HUDSON--03/25/05
function gCheckArrayOfDates(arr)
{
   var len=arr.length;
   for(a=0;a<len;a++)
    {
        val=arr[a].value;
        if(val!="" ){
        if(!gCheckDateByOBJ(arr[a],true)){return false;}}
    }
    return true;
}
//END JACK HUDSON--03/25/05
//****************************************
//JACK HUDSON--03/29/05
function gCheckZeroByID(id,TorF)
{
    var Tb;
    var val;

    Tb=document.getElementById(id);
    val=Tb.value;

    if(parseInt(val)!=0)
    {
        if(TorF)
        {
            alert("This Value Must Equal Zero");
            Tb.focus();
        }
        return false;
    }

    return true;
 }
//***********************************
//END JACK HUDSON--03/29/05
//JACK HUDSON--03/30/05
function trim(s)
{
  while (s.substring(0,1) == ' ')
  {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ')
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}
//END JACK HUDSON--03/30/05
//JACK HUDSON--03/31/05
/*************************************/
function gCheckArrayOfNumbers(arr)
{
   var len=arr.length;
   for(a=0;a<len;a++)
    {
        val=arr[a].value;
        if(val!="" ){
        if(!gCheckNumberByOBJ(arr[a],true)){return false;}}
    }
    return true;
}
//***********************************
function gCheckNumberByOBJ(obj,TorF)
{

     var val=parseFloat(obj.value);
    if (isNaN(val))
    {
        if(TorF)
        {
            alert("Please Enter Numbers Only.");
            obj.focus();
        }
        return false;
    }

    return true;
 }
 /*************************************/
function gCheckArrayOfEmpties(arr)
{
   var len=arr.length;
   for(a=0;a<len;a++)
    {
        val=arr[a].value;
        if(val=="" )
        {
            alert("Required Field");
            arr[a].focus();
            return false;
        }

    }
    return true;
}
/***********************************/
function gCheckEmptyByOBJ(obj,TorF)
{

    obj.value=trim(obj.value)
    if(obj.value =="")
    {
        if(TorF)
        {
            alert("Required Field");
            obj.focus();
        }
        return false;
    }

    return true;
 }
//END JACK HUDSON--03/31/05

// Checks to see if input is a number.
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 ;
}

// Email validation
function emailCheck ( str )
{
    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
}
