/*****************************************************************
* JavaScript Common Functions Copyright 1999-2000 by Maximo Piva *
*                             All Rights Reserved                *
*                             Distribution is forbidden          *
* Version : 1.2                                                  *
* Started : 17/Jun/99                                            *  
* Current : 2/Jul/99                                             *
*****************************************************************/

/************************** HideLayer ****************************
Input :
			frame(string):     Name of the frame or null
			name(string):      Name of the layer
			absolute(integer): 0-Relative Scroll, 1-Absolute Scroll
*****************************************************************/

function HideLayer(frame,name,absolute) {
	var cudoc;
	
	if (frame!=null)
		cudoc=parent.frames[frame].document;
	else
		cudoc=document;
		
	if ((navigator.appName == 'Netscape') && (cudoc.layers != null)) {
		if (cudoc.layers[name]) {
			cudoc.layers[name].visibility="hide";
			if ((absolute!=1) && (cudoc.layers[name].oldpageY!=null)) {
				cudoc.layers[name].pageX=cudoc.layers[name].oldpageX;
				cudoc.layers[name].pageY=cudoc.layers[name].oldpageY;
			}
		}
	}
	else if (cudoc.all != null) {
		if (cudoc.all[name]) {
			cudoc.all[name].style.visibility='hidden';
			if ((absolute!=1) && (parent.frames[frame].document.all[name].oldoffsetTop!=null)) {
				cudoc.all[name].style.pixelLeft=cudoc.all[name].oldoffsetLeft;
				cudoc.all[name].style.pixelTop=cudoc.all[name].oldoffsetTop;
			}
		}
	}
}

/************************** ShowLayer ****************************
Input :
			frame(string):     Name of the frame or null
			name(string):      Name of the layer
			absolute(integer): 0-Relative Scroll, 1-Absolute Scroll
*****************************************************************/

function ShowLayer(frame,name,absolute) {
	var cudoc,cu;

	if (frame!=null) {
		cudoc=parent.frames[frame].document;
		cu=parent.frames[frame];
	}
	else {
		cudoc=document;
		cu=window;
	}
	if ((navigator.appName == 'Netscape') && (cudoc.layers != null)) {
		if (cudoc.layers[name]) {
			cudoc.layers[name].visibility="show";
			if (absolute!=1) {			
				if (cudoc.layers[name].oldpageY!=null) {
					cudoc.layers[name].pageX=cudoc.layers[name].oldpageX+cu.pageXOffset;
					cudoc.layers[name].pageY=cudoc.layers[name].oldpageY+cu.pageYOffset;
				}
				else {
					cudoc.layers[name].oldpageX=cudoc.layers[name].pageX;
					cudoc.layers[name].oldpageY=cudoc.layers[name].pageY;
					cudoc.layers[name].pageX+=cu.pageXOffset;
					cudoc.layers[name].pageY+=cu.pageYOffset;
				}
			}
		}
	}
	else if (cudoc.all != null) {
		if (cudoc.all[name]) {
			cudoc.all[name].style.visibility='visible';
			if (absolute!=1) {
				if (cudoc.all[name].oldoffsetTop!=null) {
					cudoc.all[name].style.pixelLeft=cudoc.all[name].oldoffsetLeft+cudoc.body.scrollLeft;
					cudoc.all[name].style.pixelTop=cudoc.all[name].oldoffsetTop+cudoc.body.scrollTop;
				}
				else {
					cudoc.all[name].oldoffsetLeft=cudoc.all[name].offsetLeft;
					cudoc.all[name].oldoffsetTop=cudoc.all[name].offsetTop;
					cudoc.all[name].style.pixelLeft+=cudoc.body.scrollLeft;
					cudoc.all[name].style.pixelTop+=cudoc.body.scrollTop;
				}
			}
		}
	}
}

/************************** MoveLayer ****************************
Input :
			frame(string):     Name of the frame or null
			name(string):      Name of the layer
			xpos(integer):     New X Position
			ypos(integer):     New Y Position
			absolute(integer): 0-Relative Scroll, 1-Absolute Scroll
*****************************************************************/

function MoveLayer(frame,name,xpos,ypos,absolute) {

	var cudoc,cu;

	if (frame!=null) {
		cudoc=parent.frames[frame].document;
		cu=parent.frames[frame];
	}
	else {
		cudoc=document;
		cu=window;
	}
	if ((navigator.appName == 'Netscape') && (cudoc.layers != null)) {
		if (cudoc.layers[name]) {
			if ((absolute!=1) && (cudoc.layers[name].oldpageY!=null)) {
				cudoc.layers[name].pageX-=cudoc.layers[name].oldpageX+xpos;
				cudoc.layers[name].pageY-=cudoc.layers[name].oldpageY+ypos;
				cudoc.layers[name].oldpageX=xpos;
				cudoc.layers[name].oldpageY=ypos;
			}
			else {
				cudoc.layers[name].pageX=xpos;
				cudoc.layers[name].pageY=ypos;
			}
		}	
	}
	else if (cudoc.all != null) {
		if (cudoc.all[name]) {
			if ((absolute!=1) && (cudoc.all[name].oldoffsetTop!=null)) {
				cudoc.all[name].style.pixelLeft-=cudoc.all[name].oldoffsetLeft+xpos;
				cudoc.all[name].style.pixelTop-=cudoc.all[name].oldoffsetTop+ypos;
				cudoc.all[name].oldoffsetLeft=xpos;
				cudoc.all[name].oldoffsetTop=ypos;
			}
			else {
				cudoc.all[name].style.pixelLeft=xpos;
				cudoc.all[name].style.pixelTop=ypos;
			}
		}
	}
}

/************************* InitButton ****************************
Input :
			name(string):        Button Name
			group(string):       Family Name
			normalpic(string):   Normal Picture (; after for vspace & hspace)
			flybypic(string):    FlyBy Picture
			pressedpic(string):  Pressed Picture
			alternate(string):   Alternate Text
			link(string):        Link
			linktarget(string):  Link Target
			onmdowncode(string): On Mouse Down Extra Code
			onmovercode(string): On Mouse Over Extra Code
			onmoutcode(string):  On Mouse Out Extra Code
			linktags(string):    Link extra Tags
			imagetags(string):   Image extra Tags;
			begindata(string):   Begin extra data;
			linkdata(string):    Link extra data;
			imagedata(string):   Image extra data;
			enddata(string):     End extra data;
*****************************************************************/

function InitButton(name,group,normalpic,flybypic,pressedpic,
	alternate,link,linktarget,onmdowncode,onmovercode,onmoutcode,
	linktags,imagetags,begindata,linkdata,imagedata,enddata) {
	
	var docu,j,va,va2,na;
	
	if (document.butgrp==null) {
		document.butgrp = new Array;
		document.grps = new Array;
	}
	if(group==null)
		group='default';
	
	if (document.butgrp[group]!=null) {
		docu=document.butgrp[group];
	}
	else {
		document.butgrp[group] = new Array;
		document.grps[document.grps.length]=group;
		docu=document.butgrp[group];
	}
	
	if (docu.ImageNames==null) {		
		docu.LoadImages1 = new Array;
		docu.LoadImages2 = new Array;
		docu.LoadImages3 = new Array;
		docu.ImageSpaceWidth = new Array;
		docu.ImageSpaceHeight = new Array;
		docu.ImageNames = new Array;
		docu.Alternate = new Array;
		docu.SwapImageSrc = new Array;
		docu.Link = new Array;
		docu.Target = new Array;
		docu.Down = new Array;
		docu.Over = new Array;
		docu.Out = new Array;
		docu.LinkTags = new Array;
		docu.ImageTags = new Array;
		docu.BeginData = new Array;
		docu.LinkData = new Array;
		docu.ImageData = new Array;
		docu.EndData = new Array;
	}
  	j = docu.ImageNames.length;
	docu.ImageNames[j]=name;
	docu.LoadImages1[j]=new Image;
	va=normalpic.search(/;/);
	if (va!=-1) {
		docu.LoadImages1[j].src=normalpic.substr(0,va);
		na=normalpic.substr(va+1);
		va2=na.search(/;/);
		if (va2!=-1) {
			docu.ImageSpaceWidth[j]=parseInt(na.substr(0,va2));
			docu.ImageSpaceHeight[j]=parseInt(na.substr(va2+1));
		}
		else
			docu.ImageSpaceWidth[j]=parseInt(na);
	}
	else {
		docu.LoadImages1[j].src=normalpic;
		docu.ImageSpaceWidth[j]=0;
		docu.ImageSpaceHeight[j]=0;
	}
	docu.LoadImages2[j]=new Image;
	if (flybypic==null) {
		if (pressedpic==null)
			docu.LoadImages2[j].src=docu.LoadImages[j].src;
		else
			docu.LoadImages2[j].src=pressedpic;
	}
	else
		docu.LoadImages2[j].src=flybypic;
	docu.LoadImages3[j]=new Image;
	if (pressedpic==null) {
		if (flybypic==null)
			docu.LoadImages3[j].src=docu.LoadImages[j].src;
		else
			docu.LoadImages3[j].src=flybypic;
	}
	else
		docu.LoadImages3[j].src=pressedpic;
	docu.Alternate[j]=alternate;
	docu.Link[j]=link;
	docu.Target[j]=linktarget;
	docu.Down[j]=onmdowncode;
	docu.Over[j]=onmovercode;
	docu.Out[j]=onmoutcode;
	docu.LinkTags[j]=linktags;
	docu.ImageTags[j]=imagetags;
	docu.BeginData[j]=begindata;
	docu.LinkData[j]=linkdata;
	docu.ImageData[j]=imagedata;
	docu.EndData[j]=enddata;
}

/************************** AddButton ****************************
Input :
			name(string):        Button Name
*****************************************************************/

function AddButton(name) {

	var docu,idoc,x,i,texto,surc;
	
	docu=this.document;
	
/*
	if (document[name]==null) {
		if ((navigator.appName == 'Netscape') && (document.layers != null)) {
			if (document.layers[layer].document!=null)
				docu=document.layers[layer].document;
			else
				docu=document;
		}
		else
			return;
	}
	else
		docu=document;
*/
	for(x=0;x<document.grps.length;x++) {
		for(i=0;i<document.butgrp[document.grps[x]].ImageNames.length;i++) {
			if (document.butgrp[document.grps[x]].ImageNames[i]==name) {
				idoc=document.butgrp[document.grps[x]];
				texto="";
				if (idoc.BeginData[i]!=null)
					texto+=idoc.BeginData[i];
				if (idoc.Link[i]!="") {
					if (idoc.Target[i]==null)
						texto+="<a href=\""+idoc.Link[i]+"\" ";
					else
						texto+="<a href=\""+idoc.Link[i]+"\" target=\""+idoc.Target[i]+"\" ";
					texto+="onMouseDown=\"PutImage('"+name+"');";
				}
				else
					texto+="<a ";
				
				if (idoc.Down[i]!=null)
					texto+=idoc.Down[i];
				texto+="\" onMouseOut=\"RestoreImage('"+name+"');";
				if (idoc.Out[i]!=null)
					texto+=idoc.Out[i];
				texto+="\" onMouseOver=\"ChangeImage('"+name+"');";
				if (idoc.Over[i]!=null)
					texto+=idoc.Over[i];
				texto+="\" ";
				if (idoc.LinkTags[i]!=null)
					texto+=idoc.LinkTags[i];
				texto+=">";
				if (idoc.LinkData[i]!=null)
					texto+=idoc.LinkData[i];
				surc=idoc.LoadImages1[i].src;
				if (document.location.search!=null) {
					if (name==document.location.search.substr(1))
						surc=idoc.LoadImages3[i].src;
				}
				texto+="<img src=\""+surc+"\" name=\""+idoc.ImageNames[i]+"\" border=\"0\" ";
				if (idoc.Alternate[i]!=null)
					texto+="alt=\""+idoc.Alternate[i]+"\" ";
				if (idoc.ImageSpaceWidth[i]!=0)
					texto+="hspace=\""+idoc.ImageSpaceWidth[i]+"\" ";
				if (idoc.ImageSpaceHeight[i]!=0)
					texto+="vspace=\""+idoc.ImageSpaceHeight[i]+"\" ";
				if (idoc.ImageTags[i]!=null)
					texto+=idoc.ImageTags[i];
				texto+=">";
				if (idoc.ImageData[i]!=null)
					texto+=idoc.ImageData[i];
				texto+="</a>";
				if (idoc.EndData[i]!=null)
					texto+=idoc.EndData[i];
				docu.write(texto);
			}			
		}
	}
}

/************************** ChangeImage **************************
Input :
			image(string):       Button Name
*****************************************************************/

function ChangeImage(ime) {
	
  	var number,idoc,ima;
  	
  	idoc=FindGroup(ime);
	number=FindImageName(ime);
	ima=FindImage(number,idoc);
	idoc.SwapImageSrc[number]=ima.src;
	ima.src = idoc.LoadImages2[number].src;
}

/************************ ChangeImageName ************************
Input :
			image(string):       Button Name
			newimage(string):    New Image
*****************************************************************/

function ChangeImageName(image,newimage) {

  	var number,idoc,ima;

  	idoc=FindGroup(image);
  	number=FindImageName(image);
	ima=FindImage(number,idoc);
	idoc.SwapImageSrc[number]=ima.src;
	ima.src = newimage;
}

/*************************** PutImage ****************************
Input :
			image(string):       Button Name
*****************************************************************/

function PutImage(ime) {
	
	var number,idoc,ima,w;
  	idoc=FindGroup(ime);
  	number=FindImageName(ime);
	for(w=0;w<idoc.ImageNames.length;w++) {
		ima=FindImage(w,idoc);
		if (number==w) {
			idoc.SwapImageSrc[w] = idoc.LoadImages3[w].src;
			ima.src = idoc.LoadImages3[w].src;
		} else {
			idoc.SwapImageSrc[w] = idoc.LoadImages1[w].src;
			ima.src = idoc.LoadImages1[w].src;
		}
	}
}

/************************* PutImageName **************************
Input :
			image(string):       Button Name
			newimage(string):    New Image
*****************************************************************/

function PutImageName(ime,newime) {
	
	var number,idoc,ima,w;
  	idoc=FindGroup(ime);
  	number=FindImageName(ime);
  	
	for(w=0;w<idoc.ImageNames.length;w++) {
		ima=FindImage(w,idoc);
		if (number==w) {
			idoc.SwapImageSrc[w] = newime;
			ima.src = newime;
		} else {
			idoc.SwapImageSrc[w] = idoc.LoadImages1[w].src;
			ima.src = idoc.LoadImages1[w].src;
		}
	}
}

/************************* RestoreImage **************************
Input :
			image(string):       Button Name
*****************************************************************/

function RestoreImage(ime) {

  	var number,idoc,ima;

  	idoc=FindGroup(ime);
  	number=FindImageName(ime);
	ima=FindImage(number,idoc);
	ima.src = idoc.SwapImageSrc[number];
}

/************************** ChangeFrame **************************
Input :
			frm(string):       Frame Name
			src(string):       URL
			history(integer):  0 - No History change, 1 - History Change
			
*****************************************************************/

function ChangeFrame(frm,src,history) {
	if (history)	
		parent.frames[frm].location=src;
	else
		parent.frames[frm].location.replace(src);
}


/*****************************************************************
*********************** Internal Functions ***********************
*****************************************************************/
function RecLayer(lay,name) {
	
	var xxx,z;

	for(xxx=0;xxx<lay.length;xxx++) {
		if (lay[xxx].document[name]!=null)
			return lay[xxx].document[name];
		else {
			if (lay[xxx].layers!=null) {
				z=RecLayer(lay[xxx].layers,name);
				if (z!=-1) 
					return z;
			}
		}
	}
	return -1;
}



function FindImage(nam,doco) {
	var xxx,z;
	z=-1;
	if ((navigator.appName == 'Netscape') && (document.layers != null)) {
		z=RecLayer(document.layers,doco.ImageNames[nam]);
	}
	if (z==-1) {
		if (document[doco.ImageNames[nam]]==null) {
			for(z=0;z<parent.frames.length;z++) {
				if ((navigator.appName == 'Netscape') && (parent.frames[z].document.layers != null)) {
					n=RecLayer(parent.frames[z].document.layers,doco.ImageNames[nam]);
					if (n>=0) 
						return n;
				}
				else {
					if (parent.frames[z].document[doco.ImageNames[nam]]!=null)
			 			return parent.frames[z].document[doco.ImageNames[nam]];
				}		 

			}
		}
		else
			return document[doco.ImageNames[nam]];
	}
	else
		return z;
}
function FindGroup(name) {
	
	var x,i,z;
	if (document.butgrp!=null) {	
		for(x=0;x<document.grps.length;x++) {
			for(i=0;i<document.butgrp[document.grps[x]].ImageNames.length;i++) {
				if (document.butgrp[document.grps[x]].ImageNames[i]==name) {
					return document.butgrp[document.grps[x]];
				}
			}
		}
	}
	if (parent.frames!=null) {
		if (parent.frames.length!=null) {
			for(z=0;z<parent.frames.length;z++) {
				if (parent.frames[z].document.butgrp!=null) {	
					for(x=0;x<parent.frames[z].document.grps.length;x++) {
						for(i=0;i<parent.frames[z].document.butgrp[parent.frames[z].document.grps[x]].ImageNames.length;i++) {
							if (parent.frames[z].document.butgrp[parent.frames[z].document.grps[x]].ImageNames[i]==name)
								return parent.frames[z].document.butgrp[parent.frames[z].document.grps[x]];
						}
					}
				}
			}
		}		
	}
	return 0;
}
function FindImageName(name) {
	
	var x,i,z;
	if (document.butgrp!=null) {	
		for(x=0;x<document.grps.length;x++) {
			for(i=0;i<document.butgrp[document.grps[x]].ImageNames.length;i++) {
				if (document.butgrp[document.grps[x]].ImageNames[i]==name)
					return i;
			}
		}
	}
	if (parent.frames!=null) {	
		if (parent.frames.length!=null) {
			for(z=0;z<parent.frames.length;z++) {
				if (parent.frames[z].document.butgrp!=null) {	
					for(x=0;x<parent.frames[z].document.grps.length;x++) {
						for(i=0;i<parent.frames[z].document.butgrp[parent.frames[z].document.grps[x]].ImageNames.length;i++) {
							if (parent.frames[z].document.butgrp[parent.frames[z].document.grps[x]].ImageNames[i]==name)
								return i;
						}
					}
				}
			}
		}	
	}
	return 0;
}
