/*Ian: Temporarily inserted the below functions since they were referenced by html files and 
existed in the script.js file included by a previous incarnation of the site*/

/*The top two functions are actually from javascript/login.js */

function attemptLogin()
{
	// Request salt value from server
	var frameWorker = document.getElementById('frameLogin');
	frameWorker.contentWindow.document.location = '/shared/login/getloginsalt.asp?database=cows';
}

function gotLoginSalt(intSalt, strSaltId)
{
	if (intSalt == null) {
		alert('Internal error: database access error');
		return;
	}

	var objElePasswordUnencoded = document.getElementById('loginPassword');
	var objElePassword = document.getElementById('loginPasswordEncoded');
	var objEleSaltId = document.getElementById('loginPasswordSalt');

	// Encode password with salt value
	var strPassword = objElePasswordUnencoded.value;
	strPassword = SHA1Encode(strPassword);
	strPassword = MD5Encode(strPassword + intSalt);

	// Update entries on form
	objElePasswordUnencoded.value = '';
	objElePassword.value = strPassword;
	objEleSaltId.value = strSaltId;

	// Submit form now the entries are ready
	var objForm = document.forms['formLogin'];

	// Ideally, open new window for COWS, but this may be blocked
	// in which case an error may be thrown (eg. 0x8000405 in Firefox).
	// If this is so, attempt to open in current window.
	try {
		objForm.submit();
	}
	catch(e) {
		objForm.target = '_self';
		objForm.submit();
	}
}


/* =============================================================
    CHAT: DISPLAY CHAT ELEMENTS AND INITIATE CONNECTION
    APPEAR IF JAVASCRIPT ENABLED)
============================================================= */
function enableChat()
{
    document.getElementById('chatFlashDisplay').style.visibility = 'hidden'
    document.getElementById('chatTrigger').style.visibility = 'hidden'
    document.getElementById('chatControls').style.visibility = 'visible'
    ChatPageHandler.Connect();
}


/* =============================================================
    PRODUCT SEARCH: DID WE GET A PART NO IN QUERYSTRING?
============================================================= */
function InitProductSearch()
{
    // Was PartNo specified in querystring?
    var arrParameters = GetQueryStringParameters();
    var strPartNo = null;
    for (var intLoop = 0; intLoop < arrParameters.length; intLoop++)
    {
        if (arrParameters[intLoop].Name.toLowerCase() == "partno")
        {
            strPartNo = arrParameters[intLoop].Value;
            break;
        }
    }
    if (strPartNo == null)
      return;

    // If so, push value into input and trigger request
    document.getElementById("SearchBox").value = strPartNo;
    RefreshFrame("SearchFrame");
}

/* =============================================================
    PRODUCT SEARCH: MAKE REQUEST
============================================================= */
function RefreshFrame(strFrameId)
{
    var MyFrame = document.getElementById(strFrameId);
    MyFrame.style.display = "block";
    var wController = "http://stockpso.onlinestocksupply.com/cgi/en/vstock.search/";
    var wSearchString = document.getElementById("SearchBox").value;
    var wClientNoString = "comm1";
    MyFrame.src = wController + wSearchString + "-" + wClientNoString + "-br";
}


/* =============================================================
    MISC: HIDE JAVASCRIPT WARNING ELEMENTS (SO THEY DON'T
    APPEAR IF JAVASCRIPT ENABLED)
============================================================= */
function HideJavascriptWarnings()
{
    var arrEles = document.getElementsByClassName("JavascriptWarning");
    for (i in arrEles)
        arrEles[i].style.display = "none";
}

document.getElementsByClassName = function(clsName)
{
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}



/*Ian: Temporarily inserted all the above functions since they were referenced by html files and 
existed in the script.js file included by a previous incarnation of the site*/

/************************************************************************************************************
(C) www.dhtmlgoodies.com, October 2005
	
This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.
	
Thank you!
	
www.dhtmlgoodies.com
Alf Magne Kalleland
	
************************************************************************************************************/

var expandFirstItemAutomatically = false;	// Expand first menu item automatically ?
var initMenuIdToExpand = 'initialExpandedMenuItem';	// Id of menu item that should be initially expanded. the id is defined in the <li> tag.
var expandMenuItemByUrl = true;	// Menu will automatically expand by url - i.e. if the href of the menu item is in the current location, it will expand


var initialMenuItemAlwaysExpanded = true;	// NOT IMPLEMENTED YET

var mymenu_slmenuObj;
var divToScroll = false;
var ulToScroll = false;	
var divCounter = 1;
var otherDivsToScroll = new Array();
var divToHide = false;
var parentDivToHide = new Array();
var ulToHide = false;
var offsetOpera = 0;
if(navigator.userAgent.indexOf('Opera')>=0)offsetOpera=1;	
var slideMenuHeightOfCurrentBox = 0;
var objectsToExpand = new Array();
var initExpandIndex = 0;
var alwaysExpanedItems = new Array();
	
function popMenusToShow()
{
	var obj = divToScroll;
	var endArray = new Array();
	while(obj && obj.tagName!='BODY'){
		if(obj.tagName=='DIV' && obj.id.indexOf('slideDiv')>=0){
			var objFound = -1;
			for(var no=0;no<otherDivsToScroll.length;no++){
				if(otherDivsToScroll[no]==obj){
					objFound = no;		
				}					
			}	
			if(objFound>=0){
				otherDivsToScroll.splice(objFound,1);	
			}		
		}	
		obj = obj.parentNode;
	}	
}

function showSubMenu(e,inputObj)
{
	if(this && this.tagName)inputObj = this.parentNode;
	if(inputObj && inputObj.tagName=='LI')
	{
		divToScroll = inputObj.getElementsByTagName('DIV')[0];
		for(var no=0;no<otherDivsToScroll.length;no++)
		{
			if(otherDivsToScroll[no]==divToScroll)
				return;
		}			
	}
	hidingInProcess = false;
	if(otherDivsToScroll.length>0)
	{
		if(divToScroll)
		{
			if(otherDivsToScroll.length>0)
			{
				popMenusToShow();
			}
			if(otherDivsToScroll.length>0)
			{
				autoHideMenus();
				hidingInProcess = true;
			}
		}	
	}		
	if(divToScroll && !hidingInProcess)
	{
		divToScroll.style.display='';
		otherDivsToScroll.length = 0;
		otherDivToScroll = divToScroll.parentNode;
		otherDivsToScroll.push(divToScroll);	
		while(otherDivToScroll && otherDivToScroll.tagName!='BODY')
		{
			if(otherDivToScroll.tagName=='DIV' && otherDivToScroll.id.indexOf('slideDiv')>=0)
			{
				otherDivsToScroll.push(otherDivToScroll);
			}
			otherDivToScroll = otherDivToScroll.parentNode;
		}			
		ulToScroll = divToScroll.getElementsByTagName('UL')[0];
		if(divToScroll.style.height.replace('px','')/1 <= 1)
		{
			scrollDownSub();
		}
	}	
}


function autoHideMenus()
{
	if(otherDivsToScroll.length>0){
		divToHide = otherDivsToScroll[otherDivsToScroll.length-1];
		parentDivToHide.length=0;
		var obj = divToHide.parentNode.parentNode.parentNode;
		while(obj && obj.tagName=='DIV'){			
			if(obj.id.indexOf('slideDiv')>=0)parentDivToHide.push(obj);
			obj = obj.parentNode.parentNode.parentNode;
		}
		var tmpHeight = (divToHide.style.height.replace('px','')/1 - slideMenuHeightOfCurrentBox);
		if(tmpHeight<0)tmpHeight=0;
		if(slideMenuHeightOfCurrentBox)divToHide.style.height = tmpHeight  + 'px';
		ulToHide = divToHide.getElementsByTagName('UL')[0];
		slideMenuHeightOfCurrentBox = ulToHide.offsetHeight;
		scrollUpMenu();		
	}else{
		slideMenuHeightOfCurrentBox = 0;
		showSubMenu();			
	}
}


function scrollUpMenu()
{

	var height = divToHide.offsetHeight;
	height-=15;
	if(height<0)height=0;
	divToHide.style.height = height + 'px';

	for(var no=0;no<parentDivToHide.length;no++){	
		parentDivToHide[no].style.height = parentDivToHide[no].getElementsByTagName('UL')[0].offsetHeight + 'px';
	}
	if(height>0){
		setTimeout('scrollUpMenu()',5);
	}else{
		divToHide.style.display='none';
		otherDivsToScroll.length = otherDivsToScroll.length-1;
		autoHideMenus();			
	}
}	

function scrollDownSub()
{
	if(divToScroll){			
		var height = divToScroll.offsetHeight/1;
		var offsetMove =Math.min(15,(ulToScroll.offsetHeight - height));
		height = height +offsetMove ;
		divToScroll.style.height = height + 'px';
		
		for(var no=1;no<otherDivsToScroll.length;no++){
			var tmpHeight = otherDivsToScroll[no].offsetHeight/1 + offsetMove;
			otherDivsToScroll[no].style.height = tmpHeight + 'px';
		}			
		if(height<ulToScroll.offsetHeight)setTimeout('scrollDownSub()',5); else {
			divToScroll = false;
			ulToScroll = false;
			if(objectsToExpand.length>0 && initExpandIndex<(objectsToExpand.length-1)){
				initExpandIndex++;
				
				showSubMenu(false,objectsToExpand[initExpandIndex]);
			}
		}
	}
}
	
function initSubItems(inputObj,currentDepth)
{		
	divCounter++;		
	var div = document.createElement('DIV');	// Creating new div		
	div.style.overflow = 'hidden';	
	div.style.position = 'relative';
	div.style.display='none';
	div.style.height = '1px';
	div.id = 'slideDiv' + divCounter;
	div.className = 'slideMenuDiv' + currentDepth;		
	inputObj.parentNode.appendChild(div);	// Appending DIV as child element of <LI> that is parent of input <UL>		
	div.appendChild(inputObj);	// Appending <UL> to the div
	var menuItem = inputObj.getElementsByTagName('LI')[0];
	var intItemIndex = 0;
	while(menuItem){
		if(menuItem.tagName=='LI'){
			intItemIndex++;
			var aTag = menuItem.getElementsByTagName('A')[0];
			aTag.className='slMenuItem_depth'+currentDepth+' item' + intItemIndex;	
			var subUl = menuItem.getElementsByTagName('UL');
			if(subUl.length>0){
				initSubItems(subUl[0],currentDepth+1);					
			}
			interceptClick(aTag, showSubMenu);
		}			
		menuItem = menuItem.nextSibling;						
	}		
}

function interceptClick(aTag, fncOnClick)
{
	if (typeof(aTag.onclick) == "undefined")
	{ 
		aTag.onclick = fncOnClick;
		return;
	}
	else
	{
		var fncOldOnClick = aTag.onclick;
		aTag.onclick = function(e)
		               {
		                   fncOnClick.apply(this, arguments);
		                   return fncOldOnClick(e);
		               }
	}
}

function initSlideDownMenu()
{
	mymenu_slmenuObj = document.getElementById('mymenu_slidedown_menu');
	mymenu_slmenuObj.style.visibility='visible';
	var mainUl = mymenu_slmenuObj.getElementsByTagName('UL')[0];		
	var mainMenuItem = mainUl.getElementsByTagName('LI')[0];
	mainItemCounter = 1;
	var nonMenuitemCounter = 0;
	var intItemIndex = 0;
	while(mainMenuItem){			
		if(mainMenuItem.tagName=='LI'){
			intItemIndex++;
			var aTag = mainMenuItem.getElementsByTagName('A')[0];
			aTag.className='slMenuItem_depth1 item' + intItemIndex;	
			var subUl = mainMenuItem.getElementsByTagName('UL');
			if(subUl.length>0){
				mainMenuItem.id = 'mainMenuItem' + mainItemCounter;
				initSubItems(subUl[0],2);
				interceptClick(aTag, showSubMenu);
				mainItemCounter++;
			}
			else
			{
				nonMenuitemCounter++;
				mainMenuItem.id = 'nonMenuItem' + nonMenuitemCounter;
			}
		}			
		mainMenuItem = mainMenuItem.nextSibling;	
	}		
	
	if(location.search.indexOf('mainMenuItemToSlide')>=0){
		var items = location.search.split('&');
		for(var no=0;no<items.length;no++){
			if(items[no].indexOf('mainMenuItemToSlide')>=0){
				values = items[no].split('=');
				showSubMenu(false,document.getElementById('mainMenuItem' + values[1]));	
				initMenuIdToExpand = false;				
			}
		}			
	}else if(expandFirstItemAutomatically>0){
		if(document.getElementById('mainMenuItem' + expandFirstItemAutomatically)){
			showSubMenu(false,document.getElementById('mainMenuItem' + expandFirstItemAutomatically));
			initMenuIdToExpand = false;
		}
	}

	if(expandMenuItemByUrl)
	{
		var aTags = mymenu_slmenuObj.getElementsByTagName('A');
		for(var no=0;no<aTags.length;no++){
			var hrefToCheckOn = aTags[no].href;				
			if(location.href.indexOf(hrefToCheckOn)>=0 && hrefToCheckOn.indexOf('#')<hrefToCheckOn.length-1){
				initMenuIdToExpand = false;
				var obj = aTags[no].parentNode;
				while(obj && obj.id!='mymenu_slidedown_menu'){
					if(obj.tagName=='LI'){							
						var subUl = obj.getElementsByTagName('UL');
						if(initialMenuItemAlwaysExpanded)alwaysExpanedItems[obj.parentNode] = true;
						if(subUl.length>0){								
							objectsToExpand.unshift(obj);
						}
					}
					obj = obj.parentNode;	
				}
				showSubMenu(false,objectsToExpand[0]);
				break;					
			}			
		}
	}
			
	if(initMenuIdToExpand)
	{
		objectsToExpand = new Array();
		var obj = document.getElementById(initMenuIdToExpand)
		while(obj && obj.id!='mymenu_slidedown_menu'){
			if(obj.tagName=='LI'){
				var subUl = obj.getElementsByTagName('UL');
				if(initialMenuItemAlwaysExpanded)alwaysExpanedItems[obj.parentNode] = true;
				if(subUl.length>0){						
					objectsToExpand.unshift(obj);
				}
			}
			obj = obj.parentNode;	
		}
		
		showSubMenu(false,objectsToExpand[0]);

	}
	

		
}
	
	