var offsetxpoint = 10; //Customize x offset of tooltip
var offsetypoint = 10; //Customize y offset of tooltip
var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;
var tipobj;

//	
function popTipObj()
	{
	if (ie||ns6)
		tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""
	}
//	


//	
function ietruebody()
	{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	}
//


//
function ddrivetip(thetext, thecolor, thewidth)
	{
	
	if (ns6||ie)
		{
		if (typeof thewidth!="undefined") 
			tipobj.style.width=thewidth+"px"

		tipobj.innerHTML=thetext
		enabletip=true
	
		return false
		}
	}
//

	
//	
function positiontip(e)
	{
	if (enabletip)
		{
		var curX=(ns6)?e.pageX : event.x+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.y+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
			//move the horizontal position of the menu to the left by it's width
			tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
				else if (curX<leftedge)
					tipobj.style.left="5px"
						else
						//position the horizontal position of the menu where the mouse is positioned
						tipobj.style.left=curX+offsetxpoint+"px"

		
		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
			tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
				else
					tipobj.style.top=curY+offsetypoint+"px"

		tipobj.style.visibility="visible"
		}
	}
//	


//	
function hideddrivetip()
	{
	if (ns6||ie)
		{
		enabletip=false
		tipobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
		}
	}
//


//	
document.onmousemove=positiontip


//
function gotoPage()
	{
	if( document.fastFind.to.options[document.fastFind.to.selectedIndex].value != "" )
		location.href = document.fastFind.to.options[document.fastFind.to.selectedIndex].value;
	}
//


//
function popHelp( helpText, width )
	{
	var helpStr = "<table class=\"helpPop\" cellpadding=\"10\" cellspacing=\"0\" width=\"100%\">";
		helpStr += "<tr><td>" + helpText + "</td></tr>";
		helpStr += "</table>";		

	ddrivetip( helpStr,'', width );
	return;
	}
//	





/*
the following methods are for the user details popups
they need the following on the page

<body onLoad="popTipObj()">
<div id="dhtmltooltip"></div>

*/	
//
var popupWidth = 400;

/* this method is use in the popup methods and independantly */
//
function getUtype( ut )
	{
	var uType = "";
	switch( ut )
		{
		case 1:
		uType = "Administrator";
		break;
		
		case 2:
		uType = "Member";
		break;
		
		case 3:
		uType = "Professional";
		break;
		
		default:
		uType = "Error: -" + ut +"-";
		}
	return uType;
	}
//

/* this method is use in the popup methods and independantly */
//
function getUstatus( uS)
	{
	var uStatus = "";
	switch( uS )
		{
		case 1:
		uStatus = "Live";
		break;
		
		case 2:
		uStatus = "Closed";
		break;
		
		default:
		uStatus = "Error: -" + ut +"-";
		}
	return uStatus;
	}
//	
	
//	
function formatPopUp( uDetailsObj )
	{
	var detailsStr = "<table class=\"detailsPop\" cellpadding=\"4\" cellspacing=\"0\" width=\"100%\">";
		detailsStr += "<tr><td bgcolor=\"#99cccc\" colspan=\"4\" align=\"center\" style=\"color:white\"><b>Member Details</b></td></tr>";

		detailsStr += "<tr><th valign=\"top\" align=\"right\">User ID:</th><td>" +uDetailsObj.userID+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Company:</th><td>" +uDetailsObj.company+ "</td></tr>";



		detailsStr += "<tr><th valign=\"top\" align=\"right\">User Name:</th><td>" +uDetailsObj.userName+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">First Name:</th><td>" +uDetailsObj.fName+ "</td></tr>";


		detailsStr += "<tr><th valign=\"top\" align=\"right\">PassWord:</th><td>" +uDetailsObj.passWord+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Last Name:</th><td>" +uDetailsObj.lName+ "</td></tr>";


		detailsStr += "<tr><th valign=\"top\" align=\"right\">Creation Date:</th><td>" +uDetailsObj.creationDate+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Address 1:</th><td>" +uDetailsObj.address1+ "</td></tr>";


		detailsStr += "<tr><th valign=\"top\" align=\"right\">Number Visits:</th><td>" +uDetailsObj.numVisits+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Address 2:</th><td>" +uDetailsObj.address2+ "</td></tr>";

		detailsStr += "<tr><th valign=\"top\" align=\"right\">User Level:</th><td>" +getUtype( parseInt(uDetailsObj.userLevel) )+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">City:</th><td>" +uDetailsObj.city+ "</td></tr>";

		detailsStr += "<tr><th valign=\"top\" align=\"right\">User Status:</th><td>" +getUstatus( parseInt(uDetailsObj.userStatus) )+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Province:</th><td>" +uDetailsObj.province+ "</td></tr>";



		detailsStr += "<tr><th valign=\"top\" align=\"right\">Email:</th><td>" +uDetailsObj.email+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Country:</th><td>" +uDetailsObj.country+ "</td></tr>";


		detailsStr += "<tr><th valign=\"top\" align=\"right\">Website:</th><td>" +uDetailsObj.website+ "</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">postal:</th><td>" +uDetailsObj.postal+ "</td></tr>";

//		detailsStr += "<tr><th valign=\"top\" align=\"right\">Category:</th><td>" +uDetailsObj.category+ "</td>";
		detailsStr += "<tr><th valign=\"top\" align=\"right\">&nbsp;</th><td>&nbsp;</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Home Phone:</th><td>" +uDetailsObj.hPhone+ "</td></tr>";
		
		
//		detailsStr += "<tr><th valign=\"top\" align=\"right\">Hear About Us: </th><td>" +uDetailsObj.hearAboutUs+ "</td>";
		detailsStr += "<tr><th valign=\"top\" align=\"right\">&nbsp;</th><td>&nbsp;</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Work Phone:</th><td>" +uDetailsObj.wPhone+ "</td></tr>";

		detailsStr += "<tr><th valign=\"top\" align=\"right\">&nbsp;</th><td>&nbsp;</td>";
		detailsStr += "<th valign=\"top\" align=\"right\">Cell Phone:</th><td>" +uDetailsObj.cPhone+ "</td></tr>";


		
		
		detailsStr += "<tr><td colspan=\"4\" height=\"10\"></td></tr>";


		detailsStr += "</table>";		


		
	return detailsStr;
	}
//

//
function details( id, uN, pW, cD, nV, uL, uS, comp, fN, lN, ad1, ad2, c, prov, count, p, hP, wP, cP, email, site, cat, hearAbout )
	{
	this.userID = id;
	this.userName = uN;
	this.passWord = pW;
	this.creationDate = cD;
	this.numVisits = nV;
	this.userLevel = uL;
	this.userStatus = uS;

	this.company = comp;
	this.fName = fN;
	this.lName = lN;
	this.address1 = ad1;
	this.address2 = ad2;
	this.city = c;
	this.province = prov;
	this.country = count;
	this.postal = p;
	this.hPhone = hP;
	this.wPhone = wP;
	this.cPhone = cP;
	this.email = email;
	this.website = site;
	this.category = cat;
	this.hearAboutUs = hearAbout;
	}
//
	

//
function popUp( ID )
	{
	for( var j = 0; j < detailsArray.length; j++ )
		{
		if( detailsArray[j].userID == ID )
			{
			ddrivetip( formatPopUp( detailsArray[j] ),'', popupWidth );
			return;
			}

		}	
	}
//		
	
	
//
function isEmail( emailAddr )
	{
	//This function validates the email address format
	//looking for "@" and at least one "." and that "@" is before "."
	var errMsg = "Your email address is not properly formatted.";	
	
	if( emailAddr.indexOf( "@" ) < 1 )
		{
		alert( errMsg );
		return false;
		}
		
	if( emailAddr.indexOf( "@" ) != emailAddr.lastIndexOf( "@" ) )
		{ 
		alert( errMsg );
		return false;
		}
		
	if(  (emailAddr.lastIndexOf( "@" )+1 ) >= emailAddr.lastIndexOf( "." ) )
		{
		alert( errMsg );
		return false;
		}
		
		
	if( ( emailAddr.lastIndexOf( "." )+1 ) == emailAddr.length || ( emailAddr.lastIndexOf( "." )+2 ) == emailAddr.length)
		{
		alert( errMsg );
		return false;
		}
		
		
	return true;
	}
//


//
	
function validateLogin( f )
	{
	
	if( f.userName.value == "" &&  f.password.value == "" )
		{
		alert( "You have to input a user name and password to login!")
		return false;
		}
	
	if( f.userName.value == "" )
		{
		alert( "You have to input a user name to login!")
		return false;
		}

	if( f.password.value == "" )
		{
		alert( "You have to input a password to login!")
		return false;
		}

	return true;
	}
//
	
//	
function validateGetPassword( f )	
	{
	if( f.email.value == "" )
		{
		alert( "You have to input your email address to retrieve your login information!")
		return false;
		}

	return true;
	}
//


//	
function validatePIN( f )	
	{
	if( f.join_PIN.value == "" )
		{
		alert( "You must input a PIN number to process to the Join Form.!")
		return false;
		}

	return true;
	}
//



function setTooLong( fieldLen, validLen )
	{
	// if the field is too long we have a positive value
	// otherwise it is negative.
	return ( fieldLen - validLen )
	}

function validateFieldLen( field, value )
	{

	var tooShort = 6;
	var tooLong = 0;
	var fieldName = "";	
	switch( field )
		{
		case "userName" :
			tooLong = setTooLong( value.length, 20 );
			fieldName = "User Name";
			break;
		case "password" :
			tooLong = setTooLong( value.length, 20 );
			tooShort = value.length;
			fieldName = "Password";
			break;
		case "company" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Company";
			break;
		case "fName" : 
			tooLong = setTooLong( value.length, 60 );
			fieldName = "Fisrt Name";
			break;
		case "lName" : 
			tooLong = setTooLong( value.length, 60 );
			fieldName = "Last Name";
			break;
		case "address1" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Address 1";
			break;
		case "address2" : 
			tooLong = setTooLong( value.length, 60 );
			fieldName = "Address 2";
			break;
		case "city" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "City";
			break;
		case "province" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Province/State";
			break;
		case "other_province" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Province/State";
			break;
		case "country" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Country";
			break;
		case "postal" : 
			tooLong = setTooLong( value.length, 20 );
			fieldName = "Postal Code";
			break;
		case "hPhone" : 
			tooLong = setTooLong( value.length, 20 );
			fieldName = "Home Phone Number";
			break;
		case "wPhone" : 
			tooLong = setTooLong( value.length, 20 );
			fieldName = "Work Phone Number";
			break;
		case "cPhone" : 
			tooLong = setTooLong( value.length, 20 );
			fieldName = "Work Phone Number";
			break;
		case "email" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Email Address";
			break;
		case "website" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Web Site";
			break;
		case "category" : 
			tooLong = setTooLong( value.length, 120 );
			fieldName = "Category";
			break;
			
		}
	
	if( tooLong > 0 )
		{
		var errMess = fieldName + " is " + tooLong + " characters to long.\n\n"
			errMess += value.substring( 0, value.length - tooLong ) + "  ( ";
			errMess += value.substring( value.length - tooLong, value.length ) + " )";

		alert( errMess );
		return false;
		}

	if( tooShort < 6 )
		{
		var errMess = fieldName + " is to short. It must be 6 or more characters.\n\n"

		alert( errMess );
		return false;
		}



	return true;		
	}
	
	
function validateMemberJoin( f )
	{


	for( var i = 0; i < f.elements.length; i++ )
		{
		if( !validateFieldLen( f.elements[i].name, f.elements[i].value ) )
			{
			f.elements[i].focus();
			f.elements[i].select();
			
			return false;
			}

		if( f.elements[i].name != "company" && f.elements[i].name != "hPhone" &&
			f.elements[i].name != "wPhone" &&
			f.elements[i].name != "cPhone" &&
			f.elements[i].name != "address2" &&
			f.elements[i].name != "province" &&
			f.elements[i].name != "website" &&
			f.elements[i].name != "__EVENTTARGET" &&
			f.elements[i].name != "__EVENTARGUMENT" &&
			f.elements[i].name != "__VIEWSTATE"
			)
			{
			if( f.elements[i].value == "" )
				{
				alert( "You have missed a field!" );
				f.elements[i].focus();
				if( f.elements[i].name != "country" && f.elements[i].name != "category" )
					{
					f.elements[i].select();
					}
	
				return false;
				}
			
			}
		}

	if( !isEmail( f.email.value ) )
		{
		f.email.focus();
		f.email.select();
		
		return false;
		}


/*		
	if(f.category )		
		if( f.category.value == "" && f.uLevel.value == "3" )
			{
			alert( "You must select a Category!" );
			f.category.focus();
			
			return false;
			}
*/		

	if( f.hPhone.value == "" && f.wPhone.value == "" && f.cPhone.value == "" )
		{
		alert( "You must supply at least one phone number!" );
		f.hPhone.focus();
		f.hPhone.select();
		return false;
		}
		
	return true;		
	}
	

//onKeyUp="formatPhoneNo( this )"
//onKeyUp="formatPostalCode( this, 'Canada' )"


function formatPostalCode( pCode, countryStr )
	{
	//onKeyUp="formatPostalCode( this, 'ca' )"

	if( countryStr.toLowerCase() != "ca" )
		return;
		
		
	var pCodeStr;
		pCodeStr = pCode.value;

	if( pCodeStr.length > 0 )
		{
		pCodeStr = pCodeStr.toUpperCase();
		pCodeStr = isPostalChar( pCodeStr, 0 );
		pCodeStr = isFormatNaN( pCodeStr, 1 );
		pCodeStr = isPostalChar( pCodeStr, 2 );
		pCodeStr = isPostalSpace( pCodeStr, 3 );
		pCodeStr = isFormatNaN( pCodeStr, 4 );			
		pCodeStr = isPostalChar( pCodeStr, 5 );
		pCodeStr = isFormatNaN( pCodeStr, 6 );			
		pCodeStr = isFormatLength( pCodeStr, 7 );			
		
		
		}

	pCode.value = pCodeStr;
	}

	

function formatPhoneNo( pNo )
	{
	//onKeyUp="formatPhoneNo( this )"
	var pNoStr;
		pNoStr = pNo.value;

	pNoStr = isFormatNaN( pNoStr, 0 );	
	pNoStr = isFormatNaN( pNoStr, 1 );	
	pNoStr = isFormatNaN( pNoStr, 2 );	
	pNoStr = isPhoneDash( pNoStr, 3 )
	
	pNoStr = isFormatNaN( pNoStr, 4 );	
	pNoStr = isFormatNaN( pNoStr, 5 );	
	pNoStr = isFormatNaN( pNoStr, 6 );	
	pNoStr = isPhoneDash( pNoStr, 7 )

	pNoStr = isFormatNaN( pNoStr, 8 );	
	pNoStr = isFormatNaN( pNoStr, 9 );	
	pNoStr = isFormatNaN( pNoStr, 10 );	
	pNoStr = isFormatNaN( pNoStr, 11 );	

	pNoStr = isFormatLength( pNoStr, 12 );	
	pNo.value = pNoStr;
	}


function isFormatNaN( pStr, positionNo )
	{
	if( pStr.length  >= ( positionNo + 1 ) )
		if( isNaN( pStr.charAt( positionNo ) ) ||  pStr.charAt( positionNo ) == " " )
			return pStr.substr( 0 , positionNo );


	return pStr;
	}
	

function isPostalChar( pStr, positionNo )
	{
	if( pStr.length  >= ( positionNo + 1 ) )
		if( !isNaN( pStr.charAt( positionNo ) ) )
			return pStr.substr( 0 , positionNo );


	return pStr;
	}


function isPostalSpace( pStr, positionNo )
	{
	if( pStr.length  >= ( positionNo + 1 ) )
		if( pStr.charAt( positionNo ) != " " )
			return pStr.substr( 0 , positionNo );


	return pStr;
	}



function isPhoneDash( pStr, positionNo )
	{
	if( pStr.length  == ( positionNo + 1 ) )
		if( pStr.charAt( positionNo ) != "-" )
			return pStr.substr( 0 , positionNo ) + "-";


	return pStr;
	}



function isFormatLength( pStr, pLen )
	{
	if( pStr.length  > pLen )
		return pStr.substr( 0 , pLen );


	return pStr;
	}

	
function setSwitchView( id )
	{
	if( id == "lost" )
		{
		document.getElementById("loginform").style.display="none";
		document.getElementById("lostform").style.display="block";

//		document.getElementById("loginform").style.visibility = 'hidden';
		document.getElementById("lostform").style.visibility = 'visible';
		
		}
		else
			{
			document.getElementById("lostform").style.display="none";
			document.getElementById("loginform").style.display="block";
	
//			document.getElementById("lostform").style.visibility = 'hidden';
	
			document.getElementById("loginform").style.visibility = 'visible';
			}

	}	
	