/*********************************************************************************
 * The contents of this file are subject to the AMPT License Version 1.0 
 * ("License"); You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://policies.ampt.co.za
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * See full license for requirements.
 *
 * Copyright (C) 2006 Clyral Creative Studio cc.;
 * All Rights Reserved.
 * Contributor(s): Rohland Lablache de Charmoy.
 *
 * This class serves as a utility class for execution of various functions relating to DOM manipulation
 ********************************************************************************/
 
 // ---------------------------------------
 // FUNCTION LISTING
 // ---------------------------------------
 
 /*
 
 addASPNETSelectItems       	    -- (ElementID, obj) -- Populates a drop down list from the ASP .NET AjaxDropDown class
 addSelectItems            		    -- (ElementID, itemArray, delimiter) Adds an array of items to a select list
 addWindowLoadEvent                 -- (function) Add an event handler for Window Onload plus preserver others
 disableElement             		-- (ElementID) Disables the Element
 displayAnimated            		-- (ElementID) Displays an animated GIF
 enableElement              		-- (ElementID) Enables the Element
 E		                    		-- (ElementID) Fetch an Element By Passing in the Element ID
 findPos                            -- (Element) Find the absolute position of an element
 fireEvent                  		-- (ElementID, evt)
 getRadioSelectedValue              -- (ElementID)
 getSelectedItem            		-- (ElementID) Get a selected Item from a drop down list
 getSelectedItemObject              -- (ElementID) Get a selected Item Object from a drop down list
 getSelectedItems           	    -- (ElementID, seperator) Get an array of selected Items from a drop down list
 getSelectedItemNames          	    -- (ElementID, seperator) Get an array of selected Items from a drop down list (returns names)
 getTextItemValue           	    -- (ElementID) Get the value of a Text field
 hide                               -- (ElementID) Hide an element
 isChecked                          -- (ElementID) Is a checkbox checked?
 navigate                           -- (Pagepath,param) Navigates to specific page
 reflections                        -- (object) Reflects the properties and methods of an object
 removeAllSelectItems      	        -- (ElementID) Remove all items from a select List
 setInnerHTML               		-- (ElementID, value) Set the innerHTML attribute of a HTML Element
 setValue                   		-- (ElementID, value) Set the value attribute of a HTML Element
 show                               -- (ElementID) Show the Element
 toggleValidatorEnabled             -- (ElementID, Enabled) Enable or disable an ASP .NET validator
 
 */
 
 
var ClyralScripts =
{ 
	 //-----------------------------------------------
	 //  Populates a drop down list from the ASP .NET AjaxDropDown class
	 // ----------------------------------------------
	 addASPNETSelectItems: function (ElementID, obj)
	 {
	    var selobj = document.getElementById(ElementID);

	    for (i = 0; i < obj.Length; i++)
        {
            selobj.options[i] = new Option(obj.Name[i],obj.Value[i]);
        }
	 },
	 
	 //-----------------------------------------------
	 // Add An array of items to a select list
	 // ----------------------------------------------
	 addSelectItems : function(ElementID, itemArray, delimiter)
	 {
	    var obj = document.getElementById(ElementID);
	    
	    for (i = 0; i < itemArray.length; i++)
        {
            var item = new Array();
            item = itemArray[i].split(delimiter);
            obj.options[i] = new Option(item[0],item[1]);
        }
	 },
	 
	 //-----------------------------------------------
	 // Add an event handler for Window Onload plus preserver others
	 // ----------------------------------------------
	 addWindowLoadEvent : function (func) 
	 {   
        var oldonload = window.onload;   
        if (typeof window.onload  != 'function') 
        {  
            window.onload  = func;   
        } 
        else 
        {    
                window.onload  = function() {   
                  oldonload();   
                  func();   
            }   
        }   
     },
	 
	 // ---------------------------------------------------
	 // Disable an HTML Element
	 // ----------------------------------------------------
	 disableElement : function(ElementID)
	 {
	    var obj = document.getElementById(ElementID);
	    obj.disabled = true;
	 },
	 
	 //-----------------------------------------------
	 // Reset an animated Image Source for IE Bug and display the image
	 // ----------------------------------------------
	 displayAnimated : function(ElementID)
	 {
	    var obj = document.getElementById(ElementID);
	    obj.style.display = '';
	    obj.src = obj.src;
	 },
	 
	 // ---------------------------------------------------
	 // Enable an HTML Element
	 // ----------------------------------------------------
	 enableElement : function(ElementID)
	 {
	    var obj = document.getElementById(ElementID);
	    obj.disabled = false;
	 },
	 
	 // ---------------------------------------------------
	 // Fetch an Element By Passing in the Element ID
	 // ---------------------------------------------------
	 E : function(ElementID)
	 {
	    return document.getElementById(ElementID);
	 },
	 
	 // ---------------------------------------------------
	 // Find the position of an element
	 // ---------------------------------------------------
	 findPos : function(obj) {
	    var curleft = curtop = 0;
	    if (obj.offsetParent) {
		    curleft = obj.offsetLeft;
		    curtop = obj.offsetTop;
		    while (obj = obj.offsetParent) {
			    curleft += obj.offsetLeft;
			    curtop += obj.offsetTop;
		    }
	    }
	    return [curleft,curtop];
    },
	 // ---------------------------------------------------
	 // Fire an event on an HTML object
	 // ---------------------------------------------------
	 fireEvent : function(ElementID, evnt)
	 {
	    var obj = document.getElementById(ElementID);
	    if (document.createEvent)
	    {   // Mozilla
            var evObj = document.createEvent('MouseEvents');
            evObj.initEvent( evnt, true, true );
            obj.dispatchEvent(evObj);
	    }
	    else if( document.createEventObject ) 
	    {   // IE
            if (evnt == 'click')
                obj.click();
             else
	            obj.fireEvent('on' + evnt);
	    }
	 },
	 
	 // ---------------------------------------------------
	 // Get the selected item value from a radio list
	 // ---------------------------------------------------
	 getRadioSelectedValue : function(elementID)
	 {
	    var obj = document.getElementById(elementID);
	    var options = obj.getElementsByTagName('input');
        for(i=0;i<options.length;i++){
            var opt = options[i];
            if(opt.checked){
                return opt.value;
            }
        }
	 },
	 
	 // ---------------------------------------------------
	 // Get the selected Item from a single drop down list
	 // // ------------------------------------------------
	 getSelectedItem : function(elementID)
	 {  
		var obj = document.getElementById(elementID);
		var selectedItemValue = obj.options[obj.selectedIndex].value;
		
		return  selectedItemValue;
	 } ,
	 
	 // ---------------------------------------------------
	 // Get the selected Item object from a single drop down list
	 // // ------------------------------------------------
	 getSelectedItemObject : function(elementID)
	 {  
		var obj = document.getElementById(elementID);
		tmpitem = obj.options[obj.selectedIndex];
		return tmpitem;
	 } ,
	 
	 // ----------------------------------------------
	 // Get all the selected Items from a Multi Select
	 // ----------------------------------------------
	 getSelectedItems : function(elementID, seperator)
	 {  
		var obj = document.getElementById(elementID);
		var selectedArray = '';
		for (i = 0; i < obj.options.length; i++)
		{
			if (obj.options[i].selected)
				selectedArray += obj.options[i].value + seperator;
		}
		
		return  selectedArray.length > 0 ? selectedArray.substring(0,selectedArray.length -1) : selectedArray;
	 },
	 
	 // ----------------------------------------------
	 // Get all the selected Items Names from a Multi Select
	 // ----------------------------------------------
	 getSelectedItemNames : function(elementID, seperator)
	 {  
		var obj = document.getElementById(elementID);
		var selectedArray = '';
		for (i = 0; i < obj.options.length; i++)
		{
			if (obj.options[i].selected)
				selectedArray += obj.options[i].text + seperator;
		}
		
		return  selectedArray.length > 0 ? selectedArray.substring(0,selectedArray.length -1) : selectedArray;
	 },
	 
	 // ----------------------------------------------
	 // Get a text input item value
	 // ----------------------------------------------
	 getTextItemValue : function(ElementID)
	 {
		return document.getElementById(ElementID).value;
	 },
	 
	 // ----------------------------------------------
	 // Hide an element
	 // ----------------------------------------------
	 hide : function(ElementID)
	 {
		var obj = document.getElementById(ElementID);
		
		obj.style.display = 'none';
	 },
	
	 // ----------------------------------------------
	 // Is a checkbox checked
	 // ----------------------------------------------
	 isChecked : function(ElementID)
	 {
		var obj = document.getElementById(ElementID);
		
		return obj.checked;
	 },
	 
	 // ----------------------------------------------
	 // navigate : (Application Path, Pagepath,param) Navigates to specific page
	 // ----------------------------------------------
	 navigate : function(path,params)
	 {
		window.location = path + "?" + params;
	 },
	 
	 // ----------------------------------------------
	 // reflection : (object) Reflects the properties and methods of an object
	 // ----------------------------------------------
	 reflection : function(tmpObj)
     {
         var tmp = new Object;
         if (typeof tmpObj != typeof tmp)
	          document.write(typeof tmpObj);
         else
         {
	          tmp="";
	          for (i in tmpObj)
		           tmp += '<br>'+i+':'+tmpObj[i];
	          document.write(tmp);
         }
     },
	 
	 //-----------------------------------------------
	 // Removes All items from a select List
	 // ----------------------------------------------
	 removeAllSelectItems : function(ElementID)
	 {
	    var obj = document.getElementById(ElementID);

        for (j = 0; j < obj.options.length;j++)
        {
            obj.options[j] = null;
        }
        
        obj.options.length = 0;
	 },
	 
	  //-----------------------------------------------
	 // Set the innerHTML attribute of an HTML Element
	 // ----------------------------------------------
	 setInnerHTML : function (ElementID, value)
	 {
	    var obj = document.getElementById(ElementID);
	    
	    obj.innerHTML = value;
	 },
	 
	 
	 //-----------------------------------------------
	 // Set the value attribute of an HTML Element
	 // ----------------------------------------------
	 setValue : function (ElementID, value)
	 {
	    var obj = document.getElementById(ElementID);
	    
	    obj.value = value;
	 },
	 
	 //-----------------------------------------------
	 // Show the Element
	 // ----------------------------------------------
	 show : function (ElementID)
	 {
	    var obj = document.getElementById(ElementID);
	    
	    obj.style.display = '';
	 },
	 
	 toggleValidatorEnabled : function (validatorID, enabled)
	 {
	    var myVal = document.getElementById(validatorID);
        ValidatorEnable(myVal, enabled);  
	 }	 
	 
	 

};

 
function saveCheckbox(obj)
{
    var parent = obj.parentNode;
    while(parent.tagName != 'TR')
    {
        parent = parent.parentNode;
    }
    var checkboxes = parent.getElementsByTagName('input');
    for(i = 0; i < checkboxes.length;i++)
    {
        checkboxes[i].checked = obj.checked;
    }
}

//-----------------------------------------------
// Get the mouse position on the mouse down event 
//  to use to display the component art grid
// ----------------------------------------------
var _MouseX = 0; var _MouseY = 0; 

var  CA_IE = document.all ? true : false; 
if (!CA_IE) document.captureEvents(Event.MOUSEMOVE); 
document.onmousedown = GetMouseCoordinates;

function GetMouseCoordinates(e)
{ 
  if (CA_IE) { _MouseX = event.clientX + document.body.scrollLeft; _MouseY = event.clientY + document.body.scrollTop; } 
  else { _MouseX = e.pageX; _MouseY = e.pageY; } 

	if (_MouseX < 0){ _MouseX = 0; } 
	if (_MouseY < 0){ _MouseY = 0; } 
	return true; 
} 

function moveUp(selectId) 
{
    var selectList = document.getElementById(selectId);
    var selectOptions = selectList.options;
    for (var i = 1; i < selectOptions.length; i++) 
    {
        var opt = selectOptions[i];
        if (opt.selected) 
        {
            selectList.removeChild(opt);
            selectList.insertBefore(opt, selectOptions[i - 1]);
        }
    }
    selectList.focus();
} 

function moveDown(selectId) 
{
   var selectList = document.getElementById(selectId);
   var selectOptions = selectList.options;
    for (var i = selectOptions.length - 2; i >= 0; i--) 
    {
        var opt = selectOptions[i];
        if (opt.selected) 
        {
            var nextOpt = selectOptions[i + 1];
            opt = selectList.removeChild(opt);
            nextOpt = selectList.replaceChild(opt, nextOpt);
            selectList.insertBefore(nextOpt, opt);
        }
    }
}

function removeFromList(selectId) 
{
   var listField = document.getElementById(selectId);
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}

function SelectAllItems(selectId)
{
    var listField = document.getElementById(selectId);
    var selectOptions = listField.options;
    var intCount = listField.options.length;
    for (i = 0; i < intCount; i++)
    {
        selectOptions[i].selected = true;
    }
}

function SelectNoItems(selectId)
{
    var listField = document.getElementById(selectId);
    var selectOptions = listField.options;
    var intCount = listField.options.length;
    for (i = 0; i < intCount; i++)
    {
        selectOptions[i].selected = false;
    }
}
