﻿// JScript File

KmDev = function()
{
    Version: '1.1.0'
}

KmDev.Form = function(){};

//class to handle lists
KmDev.Form.Lists = function(){};

//class to handle tables
KmDev.Form.Tables = function(){};

//class to handle Dom
KmDev.Dom = function(){};

//class to handle ajax
KmDev.Ajax = function(){};

//fill list from a given array
KmDev.Form.Lists.fillList = function(inArray,inList)
    {
        if(inArray==null){alert("array is null");return;}  //check for array
        
        //check for string (name of element) or element
        if(typeof inList=='string'){inList=document.getElementById(inList);}
        if(typeof inList=='undefined'){alert("element is undefined");return;}
        
        this.clearList(inList); //clear items from the list element
        var listItem;           //holds an option element
        var pairs;              //array that hold value from each cell in the main array
        
        //loop on array to create options
        for(i=0,l=inArray.length-1;i<=l;i++)
        {
            if(inArray[i]!="")
            {
                pairs = inArray[i].split(':');
                listItem = document.createElement("OPTION"); //create new option element
                listItem.text = pairs[1];                  //set option text
                listItem.value = pairs[0];                 //set option value
                inList.options.add(listItem);                //add the new elment to the combo
            }
        }
    }

//clear items from a list element
KmDev.Form.Lists.clearList = function(inList)
{

    var iCount=0;
    
    //check if ther are items
    if(inList.length>0)
    {
        iCount = inList.length-1   //set counter to list length
        
        //loop until counter is less the zero
        while(iCount>=0)
        {
            inList.remove(iCount); //remove the item from the source list
            iCount--;   //decrease the counter
        }
    }
}

KmDev.Form.Lists.getIndexByValue = function(inList,index){
    var iIndex=-1;

    //check if ther are items
    if(inList.length>0)
    {
        //loop until counter is less the zero
        for(i=0,l=inList.length-1;i<=l;i++)
        {
            if(inList.options[i].value=index){
                iIndex = i;
                alert(inList.options[i].value);
                alert(i);
                break;
            }        
        }
    }
    
    return iIndex;
}


//********************************** km-development tables **************************************/
KmDev.Form.Tables.clearTable = function(inTable)
{
    var iCount = 0;
    
    if(inTable.rows.length>0)
    {   
        iCount = inTable.rows.length-1
        while(iCount>=0)
        {
            inTable.deleteRow(iCount);
            iCount--;   //decrease the counter
        }    
    }
}

//this funtion removes rows not including the header from a given table
KmDev.Form.Tables.clearTableRowsOnly = function (inTable){

    var l = inTable.rows.length-1;
    
    for(i=l;i>0;i--){
        inTable.deleteRow(i);
    }
}

//********************************* km-development DOM ******************************************/



KmDev.Dom.getElementsByTagAndClassName = function(tagName, className) {
  if ( tagName == null )
     tagName = '*';

  var children = document.getElementsByTagName(tagName) || document.all;
  var elements = new Array();

  if ( className == null )
    return children;

  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  }

  return elements;
}

//*********************************** km-development Ajax ***********************************************
KmDev.Ajax.openAjax = function(url)
{
    var scriptTag = document.createElement("script");
    scriptTag.src = url;
    var headTag = document.getElementsByTagName("head").item(0);
    headTag.appendChild(scriptTag);
}