// Validation Scripts
var DEFAULT_MAIN_CLASS = "default_mainClass";
var DEFAULT_SHADOW_CLASS = "default_shadowClass";
var DEFAULT_CLOSE_BAR_CLASS = "default_closeBarClass";

var DEFAULT_FRAME_BORDER="1"; // frame border for the iframe : 0 or 1
var DEFAULT_SCROLL_BAR="yes"; // scrollbar type : yes , no , auto
var ID_REFERENCE=0; // scrollbar type : yes , no , auto
var OBJ_CLICKED="";
var BODYISLOADED = false;

var DEFAULT_CLOSE_BAR_HEIGHT = 0; // displayCloseBar height

var MOUSEDOVER = false;

function isNum(argvalue) {
		argvalue = argvalue.toString();
		if (argvalue.length == 0)
		return false;
		for (var n = 0; n < argvalue.length; n++)
		if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
		return false;
	
		return true;
	}
	
function findStyle(StyleName) {
     ss = document.styleSheets;
	 isStyle = false;
	 s = document.styleSheets[0]; // check if netsCape or Explorer
	for(x=0;x<ss.length;x++)
	 {
	 	if (s.cssRules){ 
		  ssr = document.styleSheets[x].cssRules;
		}
		else if (s.rules){ 
		   ssr = document.styleSheets[x].rules
		}
			
		for(y=0;y<ssr.length;y++)
		{
			SELECTOR_TEXT = (s.rules)?document.styleSheets[x].rules[y].selectorText:document.styleSheets[x].cssRules[y].selectorText;
			if(SELECTOR_TEXT=="."+StyleName)
			{
				isStyle = (s.rules)?document.styleSheets[x].rules.item(y):document.styleSheets[x].cssRules.item(y);
				//setMainIframeCss(document.styleSheets[x].rules.item(y))
				break;
			}
		}
	 }
	 return isStyle;
}	

// SETTERS
function setBox(MAIN_CLASS,SHADOW_CLASS,CLOSEBAR_CLASS,OBJ,URL_IFRAME,URL_CLOSEBAR,FRAME_BORDER,SCROLLING,COLAPSE_EXP,ADD_TO_X,ADD_TO_Y,DDTYPE)
{
	//Checks if body is loaded -- on the body tag onLoad="javaScript:bodyIsLoaded();"
	if(BODYISLOADED)
	{
		//DDTYPE is to differenciate when it is a drop down menu or when it is a iframe with content called from onClick.
		if(DDTYPE)
		{
			hideFirst = false;	
		}
		
		if(DDTYPE&&DDTYPE=='iframe')
		{
			hideFirst = false;	
		}
		
		if(DDTYPE&&DDTYPE=='dropDown')
		{
			hideFirst = true;	
		}
		
		if(!DDTYPE)
		{
			hideFirst = true;	
		}
			
		if(OBJ)
		{
			OBJ.id = (!OBJ.id)?OBJ.id=ID_REFERENCE:OBJ.id;
			ID_REFERENCE++
		}
		
		dontOpen = (document.getElementById('DIF_iframeGetter_'+OBJ.id))?true:false;
		LastClicked = OBJ_CLICKED;
		OBJ_CLICKED = OBJ.id;
		
		if(COLAPSE_EXP)
		{
			if(document.getElementById('DIF_iframeGetter_'+OBJ.id))
			{
				killChildren(OBJ.id,"killOne");
				dontOpen = true;
			}
		}
		
		if(!dontOpen)
		{
			// adds x and y to compensate any problem with positioning the menus
			
			ADD_X = (ADD_TO_X)?ADD_TO_X:0;
			ADD_Y = (ADD_TO_Y)?ADD_TO_Y:0;
			
			DISPLAY_SC = (SHADOW_CLASS=='none')?false:true;
			DISPLAY_CB = (CLOSEBAR_CLASS=='none')?false:true;
			DISPLAY_SC_DEF = (SHADOW_CLASS=='DEF')?true:false;
			DISPLAY_CB_DEF = (CLOSEBAR_CLASS=='DEF')?true:false;
			
			//set position and main frame frameborder + main frame scrolling
			POSX = (OBJ)?findPosX(OBJ)+COMPENSATE_X(OBJ,ADD_X):false;
			POSY = (OBJ)?findPosY(OBJ)+COMPENSATE_Y(OBJ,ADD_Y):false;
			FRAME_B=(FRAME_BORDER&&(FRAME_BORDER<2&&FRAME_BORDER>=0))?FRAME_BORDER:DEFAULT_FRAME_BORDER; // frame border
			SCROLL_BAR=(SCROLLING&&(SCROLLING=='yes'||SCROLLING=='no'||SCROLLING=='auto'))?SCROLLING:DEFAULT_SCROLL_BAR; // stylescroll bar
				
			//DISPLAYS SHADOW REFFERED TO A CLASS
			if(DISPLAY_SC){setShadowClass = (findStyle(SHADOW_CLASS))?SHADOW_CLASS:DEFAULT_SHADOW_CLASS;}
			//DISPLAYS CLOSE BAR RFEFERED TO A CLASS
			if(DISPLAY_CB){setCloseBarClass = (findStyle(CLOSEBAR_CLASS))?CLOSEBAR_CLASS:DEFAULT_CLOSE_BAR_CLASS;}
			//DISPLAYS DEFAULT SHADOW
			if(DISPLAY_SC_DEF){setCloseBarClass =DEFAULT_CLOSE_BAR_CLASS;}
			//DISPLAYS DEFAULT CLOSE BAR
			if(DISPLAY_CB_DEF){	setCloseBarClass = DEFAULT_CLOSE_BAR_CLASS;}
			
			setMainClass = (findStyle(MAIN_CLASS))?MAIN_CLASS:DEFAULT_MAIN_CLASS;
			
			getterClassForFirstPositions = (!CLOSEBAR_CLASS)?setMainClass:setCloseBarClass;
			obj_class = findStyle(getterClassForFirstPositions);
			if(!POSX)
			{
				POSX = (obj_class.style.left)?obj_class.style.left:0;
				POSY = (obj_class.style.top)?obj_class.style.top:0;
			}
			
			COMPENSATE_NEXT_POSY = 0;
			
			if(CLOSEBAR_CLASS){
				createCloseBar(POSX,POSY,setCloseBarClass,URL_CLOSEBAR,OBJ.id,hideFirst);
				COMPENSATE_NEXT_POSY = parseInt(obj_class.style.height);
			}
			createMainIframe(POSX,POSY+COMPENSATE_NEXT_POSY,setMainClass,URL_IFRAME,FRAME_B,SCROLL_BAR,OBJ.id,hideFirst,COLAPSE_EXP);
			createShadow(POSX,POSY,COMPENSATE_NEXT_POSY,setShadowClass,findStyle(setShadowClass),OBJ.id,hideFirst);
		}
	}	
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
	
// finds position y (style.top) of any given obeject
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// This function will kill all the Elements that were appended to the page
function killChildren(OBJ_ID,ACTIONS)
{
	
	if(!OBJ_ID&&!ACTIONS)
	{
		ACTIONS = "killAll";
	}
	if(MOUSEDOVER&&window.Event)
	{
		ACTIONS = "killAll";
	}
	if(!MOUSEDOVER&&window.Event)
	{
		ACTIONS = "none"	
	}
	
	//if(ACTIONS||(!OBJ_ID&&!ACTIONS))
	//{
		if(ACTIONS == "killAll"||(!OBJ_ID&&!ACTIONS))
		{
			docQTY = (document.body.children)?document.body.children:document.body.childNodes;
			
			for(x=(docQTY.length-1);x>-1;x--)
			{
				if(docQTY[x].id)
					if(docQTY[x].id.split("_")[0]=="DIF"){
						//alert(docQTY[x].id)
						document.body.removeChild(docQTY[x]);
					}
			}
		}
		if(ACTIONS == "killOne")
		{
			if(document.getElementById('DIF_iframeGetter_'+OBJ_ID))
			{
				// IFRAME element
				document.body.removeChild(document.getElementById('DIF_iframeGetter_'+OBJ_ID));
			}
			if(document.getElementById('DIF_iframeShadow_'+OBJ_ID))
			{
				// SHADOW element
				document.body.removeChild(document.getElementById('DIF_iframeShadow_'+OBJ_ID));
			}
			if(document.getElementById('DIF_iframeCloseBar_'+OBJ_ID))
			{
				// Close Bar element
				document.body.removeChild(document.getElementById('DIF_iframeCloseBar_'+OBJ_ID));
			}
			if(document.getElementById('DIF_trans_back_'+OBJ_ID))
			{
				// Close Bar element
				document.body.removeChild(document.getElementById('DIF_trans_back_'+OBJ_ID));
			}
		}
	//}
}

function checkEvent()
{
	MOUSEDOVER = true;		
}

function createMainIframe(PASS_X,PASS_Y,PASS_CLASS,PASS_URL,PASS_FRAME_BORDER,PASS_SCROLLING,OBJ_ID,HIDEFIRST,COLAPSE_EXP)
{
	// creates new Element , sets attributes and positions
	newIframe = document.createElement("IFRAME");
	newIframe.className = PASS_CLASS;
	newIframe.setAttribute("id", "DIF_iframeGetter_"+OBJ_ID);
	newIframe.setAttribute("frameBorder", PASS_FRAME_BORDER);
	newIframe.setAttribute("scrolling", PASS_SCROLLING);
	//pass this REVISE
	//newIframe.setAttribute("allowtransparency",false);
	newIframe.style.top = PASS_Y+"px";
	newIframe.style.left = PASS_X+"px";
	
	//hides the iframe consider passing Y or N for this feature
	//newIframe.style.display = 'none';
	
	newIframe.setAttribute("src", PASS_URL);
	if(HIDEFIRST)
	{
		newIframe.style.visibility='hidden';
	}
	
	// Writes the element to the page
	document.body.appendChild(newIframe);
	
	
	//attaches onMouseOut event
	//should pass if wants to kill or not.
	if (!window.Event) 
	{
		if(!COLAPSE_EXP)
			newIframe.onmouseout = killChildren;
	}
	else
	{
		newIframe.onmouseover = checkEvent;
		if(!COLAPSE_EXP)
			newIframe.onmouseout = killChildren;
	}	
	
	
	/*attob = new getObj("DIF_iframeGetter_"+OBJ_ID);
	attob.obj.onmouseout = dd;
	if (attob.obj.captureEvents)
	{
		attob.obj.captureEvents(Event.MOUSEOUT);
	}*/	
	
	//THIS PART IS CUSTOM BUT CAN BE INCORPORATED TO THE CODE LATER
	// THIS WILL CREATE A DIV WITH A WHITE TRANSPARENT BACKGROUND THE SAME SIZE AS 
	//THE MAIN IFRAME
	
	trans_back = document.createElement("DIV");
	trans_back.className = 'opacity';
	trans_back.setAttribute("id", "DIF_trans_back_"+OBJ_ID);
	trans_back.style.top = PASS_Y+"px";
	trans_back.style.left = PASS_X+"px";
	trans_back.style.visibility='hidden';
	// Writes the element to the page
	document.body.appendChild(trans_back);
	
	
}
/* Not using but interesting
function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

*/

function findObj()
{
	docQTY = (document.body.children)?document.body.children:document.body.childNodes;
			
	for(x=(docQTY.length-1);x>-1;x--)
	{
		if(docQTY[x].id)
			if(docQTY[x].id.split("_")[0]=="DIF"){
				alert(docQTY[x].id)
				//document.body.removeChild(docQTY[x]);
			}
	}
}

function createShadow(PASS_X,PASS_Y,PASS_HEIGHT,PASS_CLASS,OBJ_CLASS,OBJ_ID,HIDEFIRST)
{
		// creates new Element , sets attributes and positions
		COMPENSATE_SHADOW_X = 0;
		COMPENSATE_SHADOW_Y = 0;
		COMPENSATE_HEIGHT = 0;
		
		newIframe1 = document.createElement("DIV");
		newIframe1.className = PASS_CLASS;
		newIframe1.setAttribute("id", "DIF_iframeShadow_"+OBJ_ID);
		if(OBJ_CLASS)
		{
			tempx = OBJ_CLASS.style.left;
			tempy = OBJ_CLASS.style.top;
			temph = OBJ_CLASS.style.height;
			COMPENSATE_SHADOW_X = (tempx.split("px")[0])?tempx.split("px")[0]:'5';
			COMPENSATE_SHADOW_Y = (tempy.split("px")[0])?tempy.split("px")[0]:'5';
			COMPENSATE_HEIGHT = parseInt(temph.split("px")[0])+PASS_HEIGHT;
		}
		newIframe1.style.top = (PASS_Y+parseInt(COMPENSATE_SHADOW_Y))+"px"; //shadow pos y
		newIframe1.style.left = (PASS_X+parseInt(COMPENSATE_SHADOW_X))+"px";//shadow pos x
		newIframe1.style.height = COMPENSATE_HEIGHT+"px"//shadow height
		
		//newIframe1.style.display = 'none';
		
		newIframe1.innerHTML = "&nbsp;";
		
		if(HIDEFIRST)
		{
			newIframe1.style.visibility='hidden';
		}
		// Writes the element to the page
		document.body.appendChild(newIframe1);
		
}

function createCloseBar(PASS_X,PASS_Y,PASS_CLASS,PASS_URL,OBJ_ID,HIDEFIRST)
{
	closeBar = document.createElement("IFRAME");
	closeBar.className = PASS_CLASS;
	closeBar.setAttribute("id", "DIF_iframeCloseBar_"+OBJ_ID);
	closeBar.setAttribute("frameBorder", 0);
	closeBar.setAttribute("scrolling", "no");
	closeBar.setAttribute("marginWidth", 0);
	closeBar.setAttribute("marginHeight", 0);
	closeBar.style.top = PASS_Y+"px";
	closeBar.style.left = PASS_X+"px";
	closeBar.setAttribute("src", PASS_URL);
	
	if(HIDEFIRST)
	{
		closeBar.style.visibility='hidden';
	}
	document.body.appendChild(closeBar);
}
function COMPENSATE_X(obj,ADD_X)
{
	return (document.all)?10+ADD_X:0+ADD_X;
}

function COMPENSATE_Y(obj,ADD_Y)
{
	
	if(obj.tagName == 'A')
	{
		addToY = (document.all)?35:20;
	}else
	{
		addToY = 0;
	}
	if(obj.tagName == 'INPUT'&&obj.type=='button')
	{
		addToY = (document.all)?40:22;
	}
	
	if(obj.tagName == 'INPUT'&&obj.type=='image')
	{
		addToY = (document.all)?obj.height+20:20;
	}
	
	if(obj.tagName == 'IMG')
	{
		addToY = (document.all)?obj.height+20:obj.height;
	}
	return ADD_Y+parseInt(addToY);
}