// Copyright 2009, KVK Consultancy - All Rights Reserved
///////////////////////////////////////////////////
 // Class Combinations

/* 

const	ButtonLeft	=	"button  company_font  any_end  small_left button_left  hot",
	ButtonFace		=	"button small_face button_face",
	ButtonRight		=	"button small_right button_right",
	IconLeft		=	"button company_font any_end big_left icon_left hot
",
	
IconFace			=	"button big_face button_face",
	IconRight		=	"button big_right icon_right",
	FreeLeft		=	"button company_font any_end big_left icon_free_left hot"
,

	FreeFace		=	IconFace,
	FreeRight		=	IconRight
,

	InfoLeft		=	"button company_font any_end big_left icon_info_left hot",
	InfoFace		=	IconFace,
	InfoRight		=	IconRight
,

	DisabledLeft	=	"button company_font any_end small_left button_disabled_left hot",
	DisabledFace	=	"button small_face button_disabled_face",
	DisabledRight	=	"button small_right button_disabled_right";

*/

var	strLeft		=	"left",
	strRight	=	"right",
	strFace		=	"face",
	strButton	=	"button",
	strIcon		=	"icon",
	strFree		=	"free",
	strInfo		=	"info",
	strForum	=	"forum",
	strDisabled	=	"disabled",
	strHover	=	"hover",
	
	strAnchorTag=	"name",
	strSpanTag	=	"name";

function	IsMozilla()
{
	return	(navigator.userAgent.indexOf("Firefox") != -1) || (navigator.userAgent.indexOf("Gecko") != -1);
}

function	fix(ob)
{
	if (top != self)
	{
		if (IsMozilla())	// FireFox Hack (Dumb Browser >.<!)
		{	
			if (ob.hasAttribute("href"))
			{
				var	strTemp	=	new String(ob.getAttribute("href"));
				strTemp	=	"Products/" + strTemp;
				ob.setAttribute("href",strTemp);
			}
		}
		 // For IE
		var	reAny	=	new RegExp("href=","gi");
		var	strAny	=	new String(ob.outerHTML);
		strAny		=	strAny.replace(reAny,  " target=\"_self\" href=\"" + document.URL + "\" java_href=");
		reAny		=	new RegExp("target=\"mainFrame\"","gi");
		strAny		=	strAny.replace(reAny,"");
//		alert(strAny);
		ob.outerHTML	=	strAny;  
	}
}

function ToggleHover(ob,strType,strPart,strFindMode,strReplaceMode)
{
	var	strFind, strReplace;
	strFind = strType + strPart + strFindMode;
	strReplace = strType + strPart + strReplaceMode;
	if (ob.getAttribute(strAnchorTag) == strFind)		// Try and find in the Anchor Tag
	{
		ob.setAttribute(strAnchorTag, strReplace);
		ob.className = GetClassName(strReplace);		// Get Class Name (other)
	} else
	{
		if (ob.getAttribute(strSpanTag) == strFind)
		{
			ob.setAttribute(strSpanTag, strReplace);
			ob.className = GetClassName(strReplace);		// Get Class Name (other)
			bDone = 1;
		} else
		{
	//		alert(strFind);
	//		alert("Found : " + ob.getAttribute(strAnchorTag) + ", Seeking: " + strFind);
			return false;
		}
	}
	return true;
}

 /* By examining the link name attribute, determin the button type */

function DeriveType(ob,strType,strPart)
{
	var strFind, 
		strTagValue;
	if ((strType == undefined)  || (strType == null))
	{
		strTagValue = ob.getAttribute(strAnchorTag);
		if (strTagValue == undefined)
			strTagValue = ob.getAttribute(strSpanTag);
		if (strTagValue != undefined)
		{
			strType = strButton;
			strFind = strType + strPart;
			if (strTagValue.substring(0,strFind .length)  != strFind)
				strType = strIcon;
			strFind = strType + strPart;
			if (strTagValue.substring(0,strFind.length)  != strFind)
				strType = strFree;
			strFind = strType + strPart;
			if (strTagValue.substring(0,strFind.length)  != strFind)
				strType = strInfo;
			strFind = strType + strPart;
			if (strTagValue.substring(0,strFind.length)  != strFind)
				strType = strForum;
			strFind = strType + strPart;
			if (strTagValue.substring(0,strFind.length)  != strFind)
				strType = strDisabled;
		}
	}
	return strType;

}

function GetClassName(strFind)
{
	var strClass, strEndSize, lngLead, strHotStyle;
	strClass = "";
	if (	(strFind.substring(0,strButton.length	) == strButton)	||
			(strFind.substring(0,strIcon.length		) == strIcon)	||
			(strFind.substring(0,strFree.length		) == strFree)	||
			(strFind.substring(0,strInfo.length		) == strInfo)	||
			(strFind.substring(0,strForum.length	) == strForum)	||
			(strFind.substring(0,strDisabled.length	) == strDisabled)
	   ) 
	{
		strBuildIcon = strButton;
		strHotStyle = "";
		strButtonOrDisabled = strButton;

		if (strFind.substring(strFind.length-strHover.length,strFind.length) == strHover)
			strHotStyle += "_" + strHover;

		lngLead = 6;
		if (strFind.substring(0,strFree.length) == strFree)
		{
			strBuildIcon = strIcon + "_" + strFree;
			lngLead = strFree.length;
		}
		if (strFind.substring(0,strInfo.length) == strInfo)
		{
			strBuildIcon = strIcon + "_" + strInfo;
			lngLead = strInfo.length;
		}
		if (strFind.substring(0,strForum.length) == strForum)
		{
			strBuildIcon = strIcon + "_" + strForum;
			lngLead = strForum.length;
		}
		if (strFind.substring(0,strDisabled.length) == strDisabled)
		{
			strBuildIcon = strIcon + "_" + strDisabled;
			strButtonOrDisabled = strDisabled;
			lngLead = strDisabled.length;
		}
		strEndSize = "big";
		if (strFind.substring(0,strButton.length)  == strButton)
			strEndSize = "small";
		strClass += strButton + " ";
		if (strFind.substring(lngLead,lngLead + strLeft.length) == strLeft)
			strClass += "company_font any_end " + strEndSize + "_left hot " + strBuildIcon + "_left";
		 else if(strFind.substring(lngLead,lngLead + strFace.length) == strFace)
			strClass += strEndSize + "_face "  + strButtonOrDisabled + "_face";
		else if(strFind.substring(lngLead,lngLead + strRight.length) == strRight)
			strClass += strEndSize + "_right " + strBuildIcon + "_right";
		strClass += strHotStyle
	} 
//	alert(strClass);
	return strClass;
}

function TryType(spans,strType,strFindMode,strReplaceMode)
{
	var	bDone, strFind;
	bDone = 1;
	for (iSpan = 0; iSpan < spans.length; iSpan++)
	{
		if (!ToggleHover(spans[iSpan],strType,strFace,strFindMode,strReplaceMode))		// Try Toggling
		{
			if (!ToggleHover(spans[iSpan],strType,strRight,strFindMode,strReplaceMode))		// Try Toggling
			{
				bDone = 0;
			}
		}
	}
	return bDone;
}

function hover(strType,strOff,strOn)
{
	var spans, iSpan, ob, strFind;
	ob = window.event.srcElement.parentElement;			// Get Target (Link Expected)
	strType = DeriveType(ob,strType,strLeft + strOff);	// Examine and Return Type
	strFind = strType + strLeft + strOff;				// Look for "Hover Off"
//	alert(window.event.srcElement.className);			// Gets the FIRST span
	if (ob.getAttribute(strAnchorTag) == strFind)		// If the "name" of the attribute is the pre-hover name
	{
		strFind = strType + strLeft + strOn;			// Generate the Hover active name string
		ob.setAttribute(strAnchorTag,strFind);			// Set the new name
		ob.className = GetClassName(strFind,strLeft);	// Generate the new class for the button

		spans = ob.getElementsByTagName("span");		// Get the spans contained within this link
		if (TryType(spans,strType,strOff,strOn) == 0)
		{
			if (TryType(spans,"Button",strOff,strOn) == 0)
			{
				if (TryType(spans,"Icon",strOff,strOn) == 0)
				{										// If we don't find anything, apply the standard names
					spans[0].setAttribute(strAnchorTag,strType + strFace);
					spans[1].setAttribute(strAnchorTag,strType + strRight);
					TryType(spans,strType,strOff,strOn);
				}
			}
		}
	}

//	alert(window.event.srcElement.parentElement.outerHTML);
//	alert(window.event.srcElement.parentNode.strAnchorTag);		// Gets the Link itself
//	alert(event.srcElement.getAttribute("class"));
//	event.srcElement
//	document.getElementById("ButtonMain").
//	alert(event.srcElement.innerHTML);
}

function end_hover(strType)
{
	hover(strType,strHover,"");

	 /* Handle Disable Hover */

	var spans, 
		iIndex, 
		ob;
	spans = window.event.srcElement.parentElement.getElementsByTagName("span");		// Get span collection
	for (iIndex = 0; iIndex < spans.length; iIndex++)
	{
		ob = spans[iIndex]; 	
		if ((ob.getAttribute("PreDisabled" + strSpanTag) != undefined) && (ob.innerHTML == ""))
		{
			ob.innerHTML= ob.getAttribute("PreDisabled" + strSpanTag)
		};
	};
};

function begin_hover(strType)
{
	hover(strType,"",strHover);

	 /* Handle Disabled Hover */

	var spans, 
		iIndex, 
		ob;
	spans = window.event.srcElement.parentElement.getElementsByTagName("span");		// Retreive the spans collection under the target item
	for (iIndex = 0; iIndex < spans.length; iIndex++)
	{
		ob = spans[iIndex]; 	
		if (ob.getAttribute(strSpanTag) == strDisabled + strFace + strHover)		// If this span is the "disabled face" span then
		{
			ob.setAttribute("PreDisabled" + strSpanTag,ob.innerHTML);						// 
			ob.innerHTML= "";
		};
	};
};

 /* Try and Keep Frames in their Frameset */

function go()
{
	if (event.srcElement.parentElement.getAttribute("java_href") != undefined) 		// From PRODUCT.HTM
	{
		if (top == self)
		{
			window.location = event.srcElement.parentElement.getAttribute("java_href");
		} else
		{
			window.parent.parent.frames[2].location = event.srcElement.parentElement.getAttribute("java_href");
		}
	} 
	if (event.srcElement.java_href != undefined)		// From INFO.HTM
	{
		window.parent.parent.frames.item[2].location = event.srcElement.java_href;
	};
};

 /* Address basic Browser Related Issues */

function browser_compatibility()
{
	if (top == self)
	{	
		if (document.URL.substring(7,7+9)!= "localhost")
		{
			self.location = "../Default.htm";
			location.replace("../Default.htm");
		}
	} else
	{
		var	ob;
		if (IsMozilla())		// Fix for FireFox
		{
			for (iIndex=0;iIndex<document.images.length;iIndex+=1)
			{
				ob	=	document.images[iIndex];
				if (ob.hasAttribute("src"))
				{
					var	strTemp	=	ob.getAttribute("src");
						strTemp	=	strTemp.substring(3,strTemp.length);
					ob.setAttribute("src",strTemp);
				}
			}
		}
		for (iIndex=0;iIndex<document.links.length;iIndex+=1)
		{
			fix(document.links[iIndex]);
		}
	//for (iIndex=6;iIndex<10;iIndex+=2)
	//	fix(document.all.item(iIndex));

	}

	//alert("Fixed UP: " + document.URL);
};

browser_compatibility();

 /* Setup Buttons */

