/**
Javascript functions used throughout
Mark Watts
31/07/2003
*/

//Do the rollovers for the top menu
function topRollover( targ )
{
	//Get the actual skin and image name
	currentSrc = targ.src.substring(targ.src.indexOf("skins/")+6,targ.src.length);

	//This is a rollover
	if(targ.src.indexOf("_over.gif") < 0)
	{
		//Now make the new image name
		newSrc = scriptDepth + "resources/skins/" + currentSrc.substring(0,currentSrc.indexOf(".gif")) + "_over.gif";

		//Ta da!
		targ.style.cursor = "hand";
		targ.src = newSrc;
	}
	//This is a rollout
	else
	{
		//Now, get rid of that "_over" lark
		newSrc = scriptDepth +  "resources/skins/" + currentSrc.substring(0,currentSrc.indexOf("_over.gif")) + ".gif";
		
		//Voila!
		targ.style.cursor = "default";
		targ.src = newSrc;
	}
}

//Field rollovers
function fieldActive(field)
{
	if(field.className.indexOf("_active") > 0)
	{
		field.className = "";
	}
	else
	{
		field.className = "field_active";
	}
}

//Returns true if the passed argument is numeric
function isNumeric( str )
{
	allowed = "0123456789";
	return isComposedOf( str, allowed );
}

//Check for telephones (allows + and brackets for international numbers)
function isTelephone( str )
{
	allowed = "+0123456789()";
	return isComposedOf( str, allowed );
}

//Checks for money
function isMoney( str )
{
	allowed = ".0123456789";
	return isComposedOf( str, allowed );
}

//Returns true if arg1 only contains elements of arg2
function isComposedOf( test, allowed )
{
	okString = true;
	
	for(z=0;z<test.length;z++)
	{
		if(allowed.indexOf(test.charAt(z)) < 0)
		{
			okString = false;
			z = test.length;
		}
	}
	
	return okString;
}

//Remove spaces from a field for validation
//Note: also removes brackets and plus signs
function stripSpaces( field )
{
	newContent = "";

	for(i=0;i<field.value.length;i++)
	{
		if(field.value.charAt(i) != " " && field.value.charAt(i) != "+" && field.value.charAt(i) != "(" && field.value.charAt(i) != ")")
		{
			newContent += field.value.charAt(i);
		}
	}
	
	field.value = newContent;
}

//Simple bit of scripting to try and cut down on repetitive code
function fieldError( field, error, txt )
{
	if(field != null)
	{
		field.className = "field_error";
	}
	
	if(error != null)
	{
		document.getElementById(error).innerText = txt;
	}
}

function fieldOkay( field, error )
{
	field.className = "";
	
	if(error != null)
	{
		document.getElementById(error).innerHTML = "&nbsp;";
	}
}

//Returns true if a value is alpha numeric
function isAlphaNumeric( str )
{
	alpha = "abcdefghijklmnopqrstuvwxyz";
	numeric = "0123456789";
	okAlpha = false;
	okNumeric = false;
	
	for(i=0;i<str.length;i++)
	{
		if(alpha.indexOf(str.charAt(i)) > -1)
		{
			okAlpha = true;
		}
		else if(numeric.indexOf(str.charAt(i)) > -1)
		{
			okNumeric = true;
		}
		
		if(okAlpha && okNumeric)
		{
			 i = str.length;
		}
	}
	
	return (okAlpha && okNumeric);	
}

//Returns true if the specified case is found within the passed string
function containsCase( str, c )
{
	okString = false;

	if(c == "upper")
	{
		chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	}
	else if(c == "lower")
	{
		chars = "abcdefghijklmnopqrstuvwxyz";
	}
	
	for(i=0;i<str.length;i++)
	{
		if(chars.indexOf(str.charAt(i)) > -1)
		{
			okString = true;
			i = str.length;
		}
	}
	
	return okString;
}

//Returns true if the passed argument is a valid password
function isPassword( str )
{
	minLength = 8;
	okPassword = true;
	
	if(str.length < minLength)
	{
		okPassword = false;
	}
	else if(!isAlphaNumeric(str))
	{
		okPassword = false;
	}
	else if(!containsCase(str,"upper"))
	{
		okPassword = false;
	}
	else if(!containsCase(str,"lower"))
	{
		okPassword = false;
	}
	
	return okPassword;
}

//Open a popup with a skin preview
function previewScheme( name )
{
	prev = window.open(scriptDepth + "resources/popups/preview.asp?Scheme=" + name, "previewWindow","width=550,height=550,toolbars=0,scrollbars=0");
	
	x = (screen.width-550)/2;
	y = (screen.height-550)/2;
	
	prev.moveTo(x,y);
}

//Go to a quick link
function quickLink( sel )
{
	//Make sure there's actually somewhere to go
	if(sel.value != "")
	{
		//Find out if this is an internal or external link
		if(sel.value.substring(0,4) == "Int:")
		{
			window.location = sel.value.substring(4,sel.value.length);
			sel.selectedIndex = 0;
		}
		else
		{
			window.open( sel.value.substring(4,sel.value.length) );
			sel.selectedIndex = 0;
		}
	}
}

//Login!
function doLogin()
{
	f = document.the_form;
	
	if(f.Login.value == "")
	{
		f.Password.className = "";
		f.Login.className = "field_error";
		document.getElementById("error").innerText = "Please enter a login.";
	}
	else if(f.Login.value.indexOf("/") < 0)
	{
		f.Password.className = "";
		f.Login.className = "field_error";
		document.getElementById("error").innerHTML = "Invalid login.<br>Must contain client code and username.";
	
	}
	else if(f.Password.value == "")
	{
		f.Password.className = "field_error";
		f.Login.className = "";
		document.getElementById("error").innerText = "Please enter a password.";
	}
	else
	{
		f.submit();
	}
}

//Reset login fields
function doLoginReset( login, password )
{
	f = document.the_form;
	
	f.Login.value = login;
	f.Password.value = password;
	
	if(f.Login.value.length > 0)
	{
		f.RememberLogin.checked;
	}
	
	if(f.Password.value.length > 0)
	{
		f.RememberPassword.checked;
	}
}

//Trigger next tip lookup
function nextTip( id, area )
{
	document.getElementById("lookupEngine").src = scriptDepth + "resources/lookup/tip.asp?Area=" + area + "&TipID=" + id;
	eval(document.getElementById("lookupEngine").location);
}

//Returns true when passed a valid date
function validDate( d, m, y )
{
	userDate = new Date(y,m-1,d);
	realDate = new Date(y,m-1,1);
	
	if(userDate.getMonth() == realDate.getMonth())
	{
		return true;
	}
	else
	{
		return false;
	}
}

//Compare two date objects
function compareDates( d1, d2 )
{
	//Normalise the two dates to the same time
	d1.setHours(0); d2.setHours(0);
	d1.setMinutes(0); d2.setMinutes(0);
	d1.setSeconds(0); d2.setSeconds(0);
	d1.setMilliseconds(0); d2.setMilliseconds(0);
	
	if(d1.getTime() > d2.getTime())
	{
		return 1;
	}
	else if(d2.getTime() > d1.getTime())
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

//Log into an event
function activeEvent()
{
	f = document.ActiveEventForm;
	
	if(f.ActiveEventID.value != "")
	{
		f.submit();
	}
}

//Rollover for help marks
function helpHilight(help)
{
	currentSrc = help.src.toString().split("/");
	
	if(currentSrc[currentSrc.length-1] == "question2.gif")
	{
		help.src = scriptDepth + "resources/skins/" + currentSrc[currentSrc.length-2] + "/question3.gif";
		help.style.cursor = "hand";
	}
	else
	{
		help.src = scriptDepth + "resources/skins/" + currentSrc[currentSrc.length-2] + "/question2.gif";
		help.style.cursor = "default";
	}
}


//Get the position of a given element
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;
}

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;
}

//Help menu display
function showMenu(menu,button,hideMe)
{
	closeAll(menu,button);

	if(menu.style.visibility == "hidden")
	{
		formElements(hideMe,"hidden");
		menu.style.left = findPosX(button)-2;
		menu.style.top = findPosY(button)-3;
		menu.style.visibility = "visible";
		button.style.visibility = "hidden";
		doShadow(menu,true);
	}
	else
	{
		formElements(hideMe,"visible");
		menu.style.left = -400;
		menu.style.top = -400;
		menu.style.visibility = "hidden";
		button.style.visibility = "visible";
		doShadow(menu,false);
	}
}

//Close all open menus
function closeAll(currentMenu,currentButton)
{
	for(i=0;i<allMenus.length;i++)
	{
		theMenu = document.getElementById(allMenus[i]);
		
		if(currentMenu != theMenu)
		{
			theMenu.style.visibility = "hidden";
			theMenu.style.top = -400;
			theMenu.style.left = -400;
		}

	}
	
	for(i=0;i<allButtons.length;i++)
	{
		theButton = document.getElementById(allButtons[i]);		
		
		if(theButton != currentButton)
		{
			theButton.style.visibility = "visible";
		}
	}
	
	if(allElements != null)
	{
		for(i=0;i<allElements.length;i++)
		{
			document.getElementById(allElements[i]).style.visibility = "visible";
		}
	}
}

//Hide those pesky drop downs
function formElements( hideMe, vis )
{
	if(hideMe != null)
	{
		for(i=0;i<hideMe.length;i++)
		{
			document.getElementById(hideMe[i]).style.visibility = vis;
		}
	}
}

//Setup the shadow
function doShadow(parent,mode)
{
	shadow = document.getElementById("shadow_div");
	parent_body = document.getElementById(parent.id + "_body");
	
	if(mode)
	{
		shadow.style.width = parent_body.offsetWidth;
		shadow.style.height = parent_body.offsetHeight;
		shadow.style.left = findPosX(parent_body)+5;
		shadow.style.top = findPosY(parent_body)+5;
		shadow.style.visibility = "visible";
	}
	else
	{
		shadow.style.visibility = "hidden";
		shadow.style.top = -400;
		shadow.style.left = -400;
	}
}

//Tour popup
function tourPopup(page)
{
	if(page == null)
	{
		page = "";
	}

	win = window.open(scriptDepth + "resources/tour/" + page,"tourWindow","height=420,width=450,toolbars=0,scrollbars=0");
}

//Displays basic details
function showArea(area,button)
{
	//Close other elements first
	if(closeFirst != null)
	{
		for(i=0;i<closeFirst.length;i++)
		{
			targ = document.getElementById(closeFirst[i]);
			targB = document.getElementById(allButtons[i]);
		
			if(targ != null)
			{
				if(targ != area && targ.style.display == "block")
				{
					targ.style.display = "none";
					targB.src = targB.src.substring(0,(targB.src.length-13)) + "show_icon.gif";
				}
			}
		}
	}

	if(area.style.display == "block")
	{
		area.style.display = "none";
		button.src = button.src.substring(0,(button.src.length-13)) + "show_icon.gif";
	}
	else
	{
		area.style.display = "block";
		button.src = button.src.substring(0,(button.src.length-13)) + "hide_icon.gif";
	}
}

//Help popup
function helpPopup(file,width,height)
{
	window.open(scriptDepth + "resources/help/" + file + ".asp","helpWindow","width=" + width + ",height=" + height + ",scrollbars=0,toolbars=0");
}

//Log the user out if they close the window
function windowClosed()
{
	window.open("","logoutWindow");
}
