
//functions to access DOM from different browsers (NS4,NS6, IE4+, Mozilla 5, Opera 7, NS7)
//
// version 1.5 - 8-may-2006
// author: Radu Dudici
//

//FUNCTIONS
//
//Window
//	getWindowWidth()
//	getWindowHeight()
//  captureMouseXY(); //makes mouse position (xmouse,ymouse) available; should be called as initialization onload
//
//Layers:
//	getLayer(layername)
//	getLayerStyle(layername)
//	writeLayerDoc(layername,text)
//
//Frames:
//	getFrame(framename)
//
//Objects & forms:
//	getObject(objname)
//	getFormField(formname,fieldname)
//	getFormFieldValue(formname,fieldname) //read text value from text fields; read array of values from selected/checked items
//	setFormFieldValue(formname,fieldname,value) //set text value to text fields; select/check items that have the value equal to input "value"
//	getFormFieldItem(formname,fieldname,value) //returns array of index where selection/check is equal to input value; value = true | false (selected | unselected)
//	setFormFieldItem(formname,fieldname,index,value) //(un)select/(un)check items at index equal to input "index"; value=true|false
//
//General:
// inArray(value,array) //returns true if value is in array
// b64 = base64encode(data);
// data = base64decode(b64);


//----------------------------------------------------------------------------------------
//returns the window width
function getWindowWidth() 
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth )
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if ( document.body && document.body.clientWidth) 
  {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  
  return myWidth;
}
//------------------------------------
//returns the window height
function getWindowHeight() 
{
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) 
  {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) 
  {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if ( document.body && document.body.clientHeight) 
  {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  
  return myHeight;
}
//------------------------------------
//set the mouse move event to capture mouse X,Y
function captureMouseXY()
{
	if (window.Event) {
    document.captureEvents(Event.MOUSEMOVE);
  }
  document.onmousemove = getXY;
}
//-------------------------------------
//onmousemove event function
function getXY(e) 
{
  xmouse = (window.Event) ? e.pageX : event.clientX;
  ymouse = (window.Event) ? e.pageY : event.clientY;
  

  if (document.all)
  {
  	if (document.documentElement)
  	{
			xmouse = document.documentElement.scrollLeft + xmouse;
    	ymouse = document.documentElement.scrollTop + ymouse;	
  	} else {
			xmouse = document.body.scrollLeft + xmouse;
			ymouse = document.body.scrollTop + ymouse;
  	}
  			
  } else {
  	//apperantly this is not necessary for NS7 and Mozilla5
    //xmouse = window.pageXOffset + xmouse;
    //ymouse = window.pageYOffset + ymouse;
  }

}

//------------------------------------
//returns the layer object
function getLayer(layername)
{
	var obj = null;
	
	if (document.layers){
      //Netscape 4 specific code
      obj = eval("document." + layername);
      //obj = "document." + layername;
   }
   if (document.getElementById){
      //Netscape 6 specific code
      obj = document.getElementById(layername);
      //obj = "document.getElementById(" + layername + ")";
   }
   if (document.all){
      //IE4+ specific code
      obj = document.all[layername];
      //obj = "document.all[" + layername + "]";
   }
				
	return obj;
}
//------------------------------------------------
//returns the layer style object
function getLayerStyle(layername)
{
	var obj = null;
	
	if (document.layers){
      //Netscape 4 specific code
      obj = eval("document." + layername);
   }
   if (document.getElementById){
      //Netscape 6 specific code
      obj = document.getElementById(layername).style;
   }
   if (document.all){
      //IE4+ specific code
      obj = document.all[layername].style;
   }
				
	return obj;
}
//-----------------------------------------------------
//change the layer content
function writeLayerDoc(layername,text)
{
	var doc;
	
	if (document.layers){
      //Netscape 4 specific code
      doc = document.layers[layername].document;
      doc.open();
      doc.write(text);
      doc.close();
   }
   if (document.getElementById){
      //Netscape 6 specific code
      document.getElementById(layername).innerHTML = text;
   }
   if (document.all){
      //IE4+ specific code
      document.all[layername].innerHTML = text;
   }
				
}
//--------------------------------------------------------
//returns frame object
function getFrame(framename)
{
	var obj = null;
	
	obj = eval("parent." + framename);
	
	return obj;	
}
//-------------------------------------------------------
//returns the object
function getObject(objname)
{
	var obj = null;

	if (document.getElementById)
	{
      //Netscape 6 specific code
      return document.getElementById(objname);
  } 
  if (document.all)
  {
      //IE4+ specific code
      return document.all[objname];
  }
  
	obj = eval("document." + objname);
	
	return obj;
}
//-------------------------------------------------------
//returns the object (field) of a form
function getFormField(formname,fieldname)
{

	var obj = null;
	obj = eval("document." + formname + "[\"" + fieldname + "\"]");
	return obj;

}
//------------------------------------------------------




//returns form element value
//retuns text value of a text,password,hidden,button, texarea,submit,reset fields
//returns selected value from select-one fields
//returns array of selected values from select-multiple fields
//returns selected/checked value from radio/checkbox fields
//returns array of selected/checked values from array of radio/checkbox fields
function getFormFieldValue(formname,fieldname)
{
	var field = getFormField(formname,fieldname);
	
	switch(field.type)
  {
		case "text" :
    case "textarea" :
    case "password" :
    case "hidden" :
    case "button" :
    case "reset" :
    case "submit" :
         return field.value;

    case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   
         	return "";
         else
         	return field.options[i].value;

    case "select-multiple" :
         var allChecked = new Array();
         for(var i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = field.options[i].value;
         return allChecked;

    case "radio" :
    case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
    default :
         if(field[0].type == "radio")
         {
            for (var i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(var i = 0; i < field.length; i++)
            {
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;
            }

            return allChecked;
         }

  }
   
	return null;

}
//---------------------------------------------------------------------

//return true if value exists in the array
function inArray(value,var_array)
{
	for(var i=0;i<var_array.length;i++)
	{
		if (var_array[i] == value) return true;
	}
	
	return false;
}


//set form element value
//for text,password,hidden,buton,submit,reset,textarea field  -> set the text value to "value"
//for select-one fields,select-multiple fields -> select the item that has the value = "value"
//for array of radio/checkbox fields -> checks the item that has the value = "value"
function setFormFieldValue(formname,fieldname,value)
{
	var field = getFormField(formname,fieldname);
	
	switch(field.type)
  {
		case "text" :
    case "textarea" :
    case "password" :
    case "hidden" :
    case "button" :
    case "reset" :
    case "submit" :
         field.value = value;
         break;

    case "select-one" :
    			for(var i = 0; i < field.options.length; i++)
    				if (field.options[i].value == value)
    				{
         			field.options[i].selected = true;
         			field.selectedIndex = i;
         		}
					break;

    case "select-multiple" :
         for(var i = 0; i < field.options.length; i++)
            if( field.options[i].value == value)
            {
            	field.options[i].selected = true;
            }
         break;

    case "radio" :
    case "checkbox" :
         if (field.value == value ) { field.checked = true; }
         break;
    default :
         if(field[0].type == "radio")
         {
            for (var i = 0; i < field.length; i++)
               if ( field[i].value == value ) { field[i].checked = true; }
            
         }
         else if(field[0].type == "checkbox")
         {
            for(var i = 0; i < field.length; i++)
               if( field[i].value == value ) { field[i].checked = true; }
         }

  }
   

}
//---------------------------------------------------------------------

//for array of checkbox fields -> check/uncheck item having the index= "index"
//for array of radio -> check item at index = input "index"
//for select-one,select-multiple -> select/unselect item having index = input "index"
//value = true | false
function setFormFieldItem(formname,fieldname,index,value)
{
	var field = getFormField(formname,fieldname);

	switch(field.type)
  {
    case "select-one" :
    		if (index<0 || index >= field.options.length) return;
    		field.options[index].selected = value;
        field.selectedIndex = value?index:-1;
				break;

    case "select-multiple" :
   			if (index<0 || index >= field.options.length) return;
    				
       	field.options[index].selected = value;
        break;
        
    case "checkbox":
    case "radio":
    		field.checked = value;
    		break;

    default :
				if(field[0].type == "checkbox")
        {
   				if (index<0 || index >= field.length) return;
    				
       		field[index].checked = value;
        	
        } else if(field[0].type == "radio")
        {
         	if (index<0 || index >= field.length) return;
          field[index].checked = true;
        }
  }


}
//---------------------------------------------------------------------




//returns array of index where selection/check is equal to input value
//value = true | false (selected | unselected)
function getFormFieldItem(formname,fieldname,value)
{
	var field = getFormField(formname,fieldname);
	var allChecked = new Array();

	switch(field.type)
  {

    case "select-one" :
    		allChecked[allChecked.length] = field.selectedIndex;
        return allChecked;

    case "select-multiple" :
        for(var i = 0; i < field.options.length; i++)
         	if (field.options[i].selected == value) allChecked[allChecked.length] = i;
        return allChecked;

		case "checkbox":
		case "radio":
				if (field.checked == value) allChecked[allChecked.length] = 0;
				return allChecked;

    default :
        if(field[0].type == "radio")
        {
            for (var i = 0; i < field.length; i++)
               if (field[i].checked == value) allChecked[allChecked.length] = i;

        
        } else if(field[0].type == "checkbox") {
        
            for(var i = 0; i < field.length; i++)
            	if (field[i].checked == value) allChecked[allChecked.length] = i;

        }
        return allChecked;

  }
   
	return null;

}
//---------------------------------------------------------------------

/* Copyright (C) 1999 Masanao Izumo <mo@goice.co.jp>
 * Version: 1.0
 * LastModified: Dec 25 1999
 * This library is free.  You can redistribute it and/or modify it.
 */

/*
 * Interfaces:
 * b64 = base64encode(data);
 * data = base64decode(b64);
 */


var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	c1 = str.charCodeAt(i++) & 0xff;
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt((c1 & 0x3) << 4);
	    out += "==";
	    break;
	}
	c2 = str.charCodeAt(i++);
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	    out += base64EncodeChars.charAt((c2 & 0xF) << 2);
	    out += "=";
	    break;
	}
	c3 = str.charCodeAt(i++);
	out += base64EncodeChars.charAt(c1 >> 2);
	out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
	out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	/* c1 */
	do {
	    c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c1 == -1);
	if(c1 == -1)
	    break;

	/* c2 */
	do {
	    c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c2 == -1);
	if(c2 == -1)
	    break;

	out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

	/* c3 */
	do {
	    c3 = str.charCodeAt(i++) & 0xff;
	    if(c3 == 61)
		return out;
	    c3 = base64DecodeChars[c3];
	} while(i < len && c3 == -1);
	if(c3 == -1)
	    break;

	out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

	/* c4 */
	do {
	    c4 = str.charCodeAt(i++) & 0xff;
	    if(c4 == 61)
		return out;
	    c4 = base64DecodeChars[c4];
	} while(i < len && c4 == -1);
	if(c4 == -1)
	    break;
	out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}


