/* 
  ********************************* [ Simultaneous Browsing ToolKit] *******************************************
  *																											   *	
  *   Dated : 24.10.2002																					   *	
  *   Owner : EasyRun Ltd.																					   *	
  *																											   *	
  *   All rights reserved.																					   *	
  *   No part of this code may be copy,stored in a retrieval system											   *	
  *   or transmitted in any form or by any means.                                                              *
  *																											   *
  *   Compatible with IE4+ and Nav4+ Browsers.                                                                 *
  *																											   *
  **************************************************************************************************************

*/

var isIE4 = (document.all ? true : false);
var isNS4 = (document.layers ? true : false);

var DEBUG = false;

var MaxFldLng  = 0;
var MaxFormLng = 0;

var clientEventArray;

var currentDocument;
var currentWindow;

function fieldChange(obj)
{
  var strEvent = "" ;
  var index    = 0;

  /*********************************************************** No Frames *********************************************************/
  if ((top==self) && ( opener!=null))
  {

     switch (this.type)
     {
        case "select-one" : if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " + this.options.selectedIndex);
	                        currentWindow.opener.er_Change_field("NULL",this.form.name, this.name,this.options.selectedIndex);
							break;
 
        case "select-multiple" :	var ValueSelStr = "";
									for ( i=0; i < this.length ; i++ )
									{
									   if (this.options[i].selected)
									   {
										 if (ValueSelStr == "")
											   ValueSelStr = ValueSelStr + "1";
										 else
											   ValueSelStr = ValueSelStr + "$$1";
									   }
									   else
									   {
										 if (ValueSelStr == "")
											   ValueSelStr = ValueSelStr + "0";
										 else
											   ValueSelStr = ValueSelStr + "$$0";
									   }
									}
									if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " + ValueSelStr);
									currentWindow.opener.er_Change_field("NULL",this.form.name, this.name,ValueSelStr);
									break;
  
        case   "checkbox" : var val = (this.checked ? "1" : "0");
							if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " + val); 
	                        currentWindow.opener.er_Change_field("NULL",this.form.name, this.name,""+val);
							break;
 
        case      "radio" : Obj = eval("this.form." +this.name);
							if (Obj.length > 1)
							{
							   var count = 0;
							   while (count < Obj.length)
							   {	
							      var theObj = eval("this.form." +this.name+ "[" + count + "]");
								  if (theObj.value == this.value)
								  {
								     var val = (theObj.checked ? "1" : "0");
								     if(DEBUG) alert("WITHOUT FRAMES Group-Radio currentWindow.opener.er_Change_field('NULL'," + this.form.name + "," + this.name + "[" + count + "]," + val + ")");
								     currentWindow.opener.er_Change_field("NULL",this.form.name, this.name + "[" + count + "]",val);
								  }
		 
								  count++;
							   } 
							}
							else
							{
							     var val = (this.checked ? "1" : "0");
							     if(DEBUG) alert("WITHOUT FRAMES One-Radio currentWindow.opener.er_Change_field('NULL'," + this.form.name + "," + this.name + "," + val + ")");
							     currentWindow.opener.er_Change_field("NULL",this.form.name, this.name,val);
							}
							break;

        default           : if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " + this.value);
	                        currentWindow.opener.er_Change_field("NULL",this.form.name, this.name,this.value);
							break;
     }

  }
  /****************************************************** With Frames *******************************************************/
  else 
  {
	  while((currentWindow.parent.frames[index]!=self)&&(index < currentWindow.parent.length))
	  {
		 index++;
	  }
	  if (index != currentWindow.parent.length)
	  {
		  var frameName = currentWindow.parent.frames[index].name

		  switch(this.type)
		  {
			 case      "select-one" : if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " + this.options.selectedIndex);
									  currentWindow.parent.er_Change_field(frameName,this.form.name,this.name,this.options.selectedIndex);
									  break;

			 case "select-multiple" : var ValueSelStr = "";
									  for ( i=0; i < this.length ; i++ )
									  {
										 if (this.options[i].selected)
										 {
											 if (ValueSelStr == "")
												   ValueSelStr = ValueSelStr + "1";
											 else
												   ValueSelStr = ValueSelStr + "$$1";
										 }
										 else
										 {
											 if (ValueSelStr == "")
												   ValueSelStr = ValueSelStr + "0";
											 else
												   ValueSelStr = ValueSelStr + "$$0";
										 }
									  }
									  if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " + ValueSelStr);
									  currentWindow.parent.er_Change_field(frameName,this.form.name, this.name,ValueSelStr);
									  break;
		  
			 case        "checkbox" : var val = (this.checked ? "1" : "0");
									  if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " +val); 
									  currentWindow.parent.er_Change_field(frameName,this.form.name,this.name, ""+val);
									  break;
		  
			 case           "radio" : Obj = eval("this.form." +this.name);
									  if (Obj.length > 1)
									  {
									     var count = 0;
									     while (count < Obj.length)
									     {	
										    theObj = eval("this.form." +this.name+ "[" + count + "]");
										    if (theObj.value == this.value)
										    {
											   var val = (theObj.checked ? "1" : "0");
											   if(DEBUG) alert("WITH FRAMES Group-Radio \n\n currentWindow.parent.er_Change_field(" + frameName + "," + this.form.name + "," + this.name + "[" + count + "]," + val + ")");
											   currentWindow.parent.er_Change_field(frameName,this.form.name, this.name + "[" + count + "]",val);
										    }
										    count++;
									     } 
									  }
									  else
									  {
									       var val = (this.checked ? "1" : "0");
									       if(DEBUG) alert("WITH FRAMES One-Radio \n\n currentWindow.parent.er_Change_field(" + frameName + "," + this.form.name + "," + this.name + "," + val + ")");
										   currentWindow.parent.er_Change_field(frameName,this.form.name, this.name,val);
									  }
									  break;

			 default                : if(DEBUG) alert(this.name + " Changed \n\n" + "type: " + this.type + "\n\nValue: " + this.value);
									  currentWindow.parent.er_Change_field(frameName,this.form.name,this.name, this.value);
									  break;
		  }

	  }
  }
  /**************************************************************************************************************************/

  // Concatenation of HardCoded onChange Events

  strEvent = findMoreEventRef(this.name);
  if (strEvent != null)
  {
    eval(strEvent.substring(strEvent.indexOf("{")+1,strEvent.indexOf("}")));
  }
}

function er_set_field(FrameName, FormName, FieldName,FieldValue)
{
  Obj     = eval("currentDocument." + FormName + "." + FieldName);
  ObjForm = eval("currentDocument." + FormName);

  switch (Obj.type)
  {
	  case      "select-one" :   	Obj.options[FieldValue].selected = true;
									break;
	  
	  case "select-multiple" :	    var tmp = "" + FieldValue // For Netscape Bug
									var FldValArr = tmp.split("$$");	
									for ( i=0 ; i < Obj.options.length ; i++) 
									{
									  if (FldValArr[i] == "1")
										   Obj.options[i].selected = true;
									  else
										   Obj.options[i].selected = false;
									}
									break;
	  
	  case        "checkbox" :      var val = ((FieldValue == "1") ? true : false);
									Obj.checked = val;
									break;
	  
	 
	  case           "radio" :	    var val = ((FieldValue == "1") ? true : false);
									Obj.checked = val;
									break;
								 
	 default :					    Obj.value = FieldValue;
									break;
  }
}


function AddToolKit(win) //past the Window Object
{
  var index=0;
 
  if (win.top == win)
  {
	win.status="loading ... " + win.location.href ;
	if( win.opener!=null)
        {
			try{
			win.opener.er_displaying_page(win.name,win.location.href);
			}
			catch(error){}
        }
  }
  else 
  {
	while((win.parent.frames[index] != win)&&(index < win.parent.length))
	{
	  index++;
	}
	if (index!= win.parent.length)
		    win.parent.pageChangedInFrame(win.location.href,win.parent.frames[index].name);
  }
  currentDocument = win.document;
  currentWindow   = win;

  //find Max
  MaxFormLng = win.document.forms.length;
  if (MaxFormLng == 0) //regular file with no forms
        return false;
  for ( i=0 ; i < MaxFormLng ; i++ )
  {
     for ( j=0 ; j < win.document.forms[i].length ; j++)
     {
	    if (j > MaxFldLng)
	    {
		   MaxFldLng = j;
	    }
     }
  }

  clientEventArray = new Array(MaxFormLng);
  for ( i=0; i < MaxFormLng ; i++)
  {
     eval("clientEventArray[" + i + "] = new Array(" + MaxFldLng + ");");
     for ( j=0; j < MaxFldLng ; j++)
     {
	    eval("clientEventArray[" + i + "][" + j + "] = new Array(2);");
     }
  }

  for ( i=0 ; i < MaxFormLng ; i++ )
  {
     for ( j=0 ; j < MaxFldLng ; j++)
     {
	    if ( (win.document.forms[i].elements[j].type == "radio") || (win.document.forms[i].elements[j].type == "checkbox") )
		{
		   if (win.document.forms[i].elements[j].onclick != null)
	       {
              clientEventArray[i][j][0] = win.document.forms[i].elements[j].name;
		      clientEventArray[i][j][1] = win.document.forms[i].elements[j].onclick.toString();
	       }
		   else
	       {
              clientEventArray[i][j][0] = "";
		      clientEventArray[i][j][1] = "";
	       }
	       win.document.forms[i].elements[j].onclick = fieldChange;
		}
		else
		{
	       if (win.document.forms[i].elements[j].onchange != null)
	       {
              clientEventArray[i][j][0] = win.document.forms[i].elements[j].name;
		      clientEventArray[i][j][1] = win.document.forms[i].elements[j].onchange.toString();
	       }
		   else
	       {
              clientEventArray[i][j][0] = "";
		      clientEventArray[i][j][1] = "";
	       }
	       win.document.forms[i].elements[j].onchange = fieldChange;
		}
     }
  } 

}

function findMoreEventRef(name)
{
for ( i=0 ; i < MaxFormLng ; i++ )
  {
     for ( j=0 ; j < MaxFldLng ; j++)
     {
	    if (clientEventArray[i][j][0] == name)
	              return clientEventArray[i][j][1];   
     }
  }
}
