
// GenUtils.js
// All functions (should) have prefix of "gu"

function guBodyOnLoad( doc )
{
	MM_preloadImages( 'images/about-1.jpg', 'images/solution-1.jpg', 'images/servises_3.jpg', 'images/careers-4.jpg', 'images/contact-6.jpg');

	if ( typeof( window.treeInfoList ) != "undefined" )
	{
		for ( var i in treeInfoList.nodes )
		{
			if ( !treeInfoList.nodes[i].obj.el)
				treeInfoList.nodes[i].obj.init();

			treeInfoList.nodes[i].obj.move( treeInfoList.nodes[i].obj.x + Math.random() * 200, treeInfoList.nodes[i].obj.y + Math.random() * 200 );
		}

		window.treeInfoList.draw( 0 );

		if ( typeof( window.mlMenuAnimationInd ) != "undefined" )
			if ( !window.mlMenuAnimationInd )
				return;

		var tblInfo = window.document.getElementById( "tableDetailPageInfo" );

		if ( tblInfo )
			window.setTimeout( 'window.document.getElementById( "tableDetailPageInfo" ).style.visibility = "visible";', 500 );

		window.setTimeout( 'guToggleInfoNodes( "0" );', 750 );
	}

	return;
}

function guToggleInfoNodes( sNodeNo )
{
	window.Toggle( "tree2", sNodeNo, 0 );

	var nNodeNo = parseInt( sNodeNo );

	nNodeNo++;

	if ( nNodeNo > 0 )
		return;

	window.setTimeout( "guToggleInfoNodes( " + nNodeNo.toString() + " );", 500 );
}

function guRightString( sValue, nChars )
{
	var nStrlen = sValue.length;

	return sValue.substr( nStrlen - nChars, nChars )
}

function guTrim( sValue )
{
	return sValue.replace( / *$/, "" ).replace( /^ */, "" );
}

function guMaxLength( fld, maxLength )
{
	if ( !fld )
		return;

	if ( !maxLength )
		return;

	if ( fld.value.length > maxLength )
		fld.value = fld.value.substr( 0, maxLength );
}


function guValidDateSelectFields( fldMonth, fldDay, fldYear )
{
	if ( !fldMonth || !fldDay || !fldYear )
		return false;

	if ( fldMonth.type != "select-one" || fldDay.type != "select-one" || fldYear.type != "select-one" )
		return false;

	if ( fldMonth.selectedIndex == 0 ||
				fldDay.selectedIndex == 0 ||
				fldYear.selectedIndex == 0 )
		return false;

	if ( guValidDate( fldMonth.options[ fldMonth.selectedIndex ].value + "/" +
					fldDay.options[ fldDay.selectedIndex ].value + "/" +
					fldYear.options[ fldYear.selectedIndex ].value ) == "" )
		return false;

	return true
}

function guValidDate( sValue )
{
	var sScrubbedValue = sValue.replace( / /g, "" ).replace( /\-/g, "/" );

	if ( sScrubbedValue.search( /[^0-9\/]/ ) >= 0 )
		return "";

	var nMonth = 0;
	var nDay = 0;
	var nYear = 0;
	var arrayDateParts = sScrubbedValue.split( "/" );

	if ( arrayDateParts.length == 1 )
	{
		if ( sScrubbedValue.length == 8 )
		{
			nMonth = parseInt( sScrubbedValue.substr( 0, 2 ).replace( /^0*/, "" ) );
			nDay = parseInt( sScrubbedValue.substr( 2, 2 ).replace( /^0*/, "" ) );
			nYear = parseInt( sScrubbedValue.substr( 4, 4 ).replace( /^0*/, "" ) );
		}
		else
		if ( sScrubbedValue.length == 6 )
		{
			nMonth = parseInt( sScrubbedValue.substr( 0, 2 ).replace( /^0*/, "" ) );
			nDay = parseInt( sScrubbedValue.substr( 2, 2 ).replace( /^0*/, "" ) );
			nYear = parseInt( sScrubbedValue.substr( 4, 2 ).replace( /^0*/, "" ) );
		}
		else
		{
			return "";
		}
	}
	else
	if ( arrayDateParts.length != 3 )
	{
		return "";
	}
	else
	{
		nMonth = parseInt( arrayDateParts[0].replace( /^0*/, "" ) );
		nDay = parseInt( arrayDateParts[1].replace( /^0*/, "" ) );
		nYear = parseInt( arrayDateParts[2].replace( /^0*/, "" ) );
	}

	if ( nYear < 100 )
	{
		if ( nYear < 30 )
			nYear += 2000;
		else
			nYear += 1900;
	}

	var newDate = new Date( nYear, nMonth - 1, nDay );

	if ( newDate.getFullYear() != nYear )
		return "";
	else
	if ( newDate.getMonth() + 1 != nMonth )
		return "";
	else
	if ( newDate.getDate() != nDay )
		return "";

	return guFormatMMDDYYYY( newDate );
}

function guFormatMMDDYYYY( objDate )
{
	var sDate;

	sDate = ( objDate.getMonth() < 9 ? "0" : "" ) + ( objDate.getMonth() + 1 ) + "/" +
			( objDate.getDate() < 10 ? "0" : "" ) + objDate.getDate() + "/" +
			objDate.getFullYear();

	return sDate;
}

function guValidInteger( sValue )
{
	if ( sValue == "" )
		return "";

	if ( sValue.search( /[^\-^0-9^.^,]/ ) >= 0 ) // only allow #'s, commas and a decimal point
		return "";

	var strCleanNumber = sValue.replace( /,/g, "" ); // strip out the commas

	var nValue = parseInt( strCleanNumber );

	if ( isNaN( nValue ) )
		return "";

	return nValue.toString();
}

function guValidDecimal( sValue )
{
	if ( sValue == "" )
		return "";

	if ( sValue.search( /[^\-^0-9^.^,]/ ) >= 0 ) // only allow #'s, commas and a decimal point
		return "";

	var strCleanNumber = sValue.replace( /,/g, "" ); // strip out the commas

	var nValue = parseFloat( strCleanNumber );

	if ( isNaN( nValue ) )
		return "";

	var nBigNumber = Math.round( ( nValue * 100 ) );
	var sAbsNumber = "00" + Math.abs( nBigNumber ).toString();
	var sFormattedAbsNumber = sAbsNumber.substr( 0, sAbsNumber.length - 2 ) + "." + guRightString( sAbsNumber, 2 );

	sFormattedAbsNumber = sFormattedAbsNumber.replace( /^0*/, "" ); // remove any left-over leading zeroes

	strFormattedNumber = ( nBigNumber < 0 ? "-" + sFormattedAbsNumber : sFormattedAbsNumber );

	return strFormattedNumber;
}

function guAddService()
{
	if ( typeof( document.f ) == "undefined" )
	{
		alert( "Internal Error: 'document.f' not defined - please report to Support." );
		return;
	}

	if ( typeof( document.f.doAction ) == "undefined" )
	{
		alert( "Internal Error: 'document.f.doAction' not defined - please report to Support." );
		return;
	}

	document.f.doAction.value = "add";
	document.f.submit();
}

function guRemoveService()
{
	if ( typeof( document.f ) == "undefined" )
	{
		alert( "Internal Error: 'document.f' not defined - please report to Support." );
		return;
	}

	if ( typeof( document.f.doAction ) == "undefined" )
	{
		alert( "Internal Error: 'document.f.doAction' not defined - please report to Support." );
		return;
	}

	document.f.doAction.value = "remove";
	document.f.submit();
}

// functions born elsewhere...

// standard image-management functions, used to turn image buttons on and off
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var guSelectFieldNameYear = "";
var guSelectFieldNameMonth = "";
var guSelectFieldNameDay = "";
var guInputFieldDate = "";

function guPopCalendarWidgetForField( sInputFieldName )
{
	guInputFieldDate = sInputFieldName;

	var sHref = "Calendar/DatePicker.asp?f=guPushCalendarWidgetDateToInput";
	var w = window.open( sHref, "DatePicker",
				"height=200,width=185,status=no,toolbar=no,menubar=no,location=no,resizable=no", true );

	if ( w )
		w.focus();
}

function guPopCalendarWidget( sSelectFieldNameYear, sSelectFieldNameMonth, sSelectFieldNameDay )
{
	guSelectFieldNameYear = sSelectFieldNameYear;
	guSelectFieldNameMonth = sSelectFieldNameMonth;
	guSelectFieldNameDay = sSelectFieldNameDay;

	var sHref = "Calendar/DatePicker.asp?f=guPushCalendarWidgetDate";
	var w = window.open( sHref, "DatePicker",
				"height=200,width=185,status=no,toolbar=no,menubar=no,location=no,resizable=no", true );

	if ( w )
		w.focus();
}

function guPushCalendarWidgetDateToInput( y, m, d )
{
	var i;
	var fldDate = window.document.getElementById( guInputFieldDate );

	guInputFieldDate = "";

	if ( !fldDate )
		return;

	var sDate = m.toString() + "/" + d.toString() + "/" + y.toString();

	fldDate.value = guValidDate( sDate );
}

function guPushCalendarWidgetDate( y, m, d )
{
	var i;
	var fldYear = window.document.getElementById( guSelectFieldNameYear );
	var fldMonth = window.document.getElementById( guSelectFieldNameMonth );
	var fldDay = window.document.getElementById( guSelectFieldNameDay );

	guSelectFieldNameYear = "";
	guSelectFieldNameMonth = "";
	guSelectFieldNameDay = "";

	if ( !fldYear || !fldMonth || !fldDay )
		return;

	for ( i = 0; i < fldMonth.options.length; i++ )
		if ( fldMonth.options[ i ].value == m.toString() )
		{
			fldMonth.selectedIndex = i;
			break;
		}

	for ( i = 0; i < fldDay.options.length; i++ )
		if ( fldDay.options[ i ].value == d.toString() )
		{
			fldDay.selectedIndex = i;
			break;
		}

	for ( i = 0; i < fldYear.options.length; i++ )
		if ( fldYear.options[ i ].value == y.toString() )
		{
			fldYear.selectedIndex = i;
			break;
		}
}

// Returns the "top" coordinate of the object in absolute terms to the
// browser window (every object is only aware of its offset
// to its parent)
function guGetAbsoluteTop( obj )
{
	if ( !obj )
		return 0;

	if ( obj.tagName == "BODY" )
		return 0;

	var nTop = obj.offsetTop - obj.scrollTop + guGetAbsoluteTop( obj.offsetParent );

	return nTop;
}

// Returns the "left" coordinate of the object in absolute terms to the
// browser window (every object is only aware of its offset
// to its parent)
function guGetAbsoluteLeft( obj )
{
	if ( !obj )
		return 0;

	if ( obj.tagName == "BODY" )
		return 0;

	var nLeft = obj.offsetLeft + guGetAbsoluteLeft( obj.offsetParent );

	return nLeft;
}

function guGetFirstElementByName( doc, sFieldName )
{
	if ( !doc )
		return null;

	if ( !sFieldName )
		return null;

	if ( sFieldName.replace( / /g, "" ) == "" )
		return null;

	var fldTarget = doc.getElementsByName( sFieldName );

	if ( !fldTarget )
		return null;

	if ( fldTarget.length < 1 )
		return null;

	return fldTarget[0];
}

function guTryToSetFocus( doc, fldWithName )
{
	if ( !doc )
		return;

	if ( !fldWithName )
		return;

	if ( typeof( fldWithName.type ) == "undefined" )
		return;

	if ( fldWithName.type != "hidden" )
		return;

	if ( fldWithName.value == "" )
		return;

	var fldTarget = guGetFirstElementByName( doc, fldWithName.value );

	fldWithName.value = "";

	if ( fldTarget )
		if ( fldTarget.type != "hidden" )
			fldTarget.focus();
}

function guOnBlurZipLookup( fldZip )
{
	if ( !fldZip )
		return;

	if ( fldZip.value == fldZip.defaultValue )
		return;

	fldZip.value = fldZip.value.replace( /[^0-9]/g, "" );

	if ( fldZip.value.length != 5 )
	{
		fldZip.value = "";
		return;
	}

	var f = fldZip.form;

	if ( !f )
		return;

	var nTop = guGetAbsoluteTop( fldZip );
	var nLeft = guGetAbsoluteLeft( fldZip );

	overlib( 'Please Wait - ZIP Code Lookup', WIDTH, 250, FIXX, nLeft - 25, FIXY, nTop - 5 );

	if ( typeof( f.FirstField ) != "undefined" )
		f.FirstField.value = fldZip.name;

	if ( typeof( f.doAction ) != "undefined" )
		f.doAction.value = "update zip";

	f.submit();
	return;
}

function guOnChangeCountry( fldCountry, sPrefix )
{
	if ( !fldCountry )
		return;

	if ( fldCountry.type != "select-one" )
		return;

	var doc = fldCountry.ownerDocument;

	if ( !doc )
		return;

	var f = fldCountry.form;

	if ( !f )
		return;

	var fldCheck = null;

	if ( ( fldCheck = guGetFirstElementByName( doc, sPrefix + "Zip" ) ) )
		fldCheck.value = "";

	if ( ( fldCheck = guGetFirstElementByName( doc, sPrefix + "State" ) ) )
		fldCheck.value = "";

	if ( ( fldCheck = guGetFirstElementByName( doc, sPrefix + "CityState" ) ) )
		fldCheck.value = "";

	if ( ( fldCheck = guGetFirstElementByName( doc, sPrefix + "City" ) ) )
		fldCheck.value = "";

	var sCountry = fldCountry.options[ fldCountry.selectedIndex ].value;

	if ( ( fldCheck = guGetFirstElementByName( doc, "FirstField" ) ) )
		if ( sCountry == "U.S.A." )
			fldCheck.value = sPrefix + "Zip";
		else
			fldCheck.value = sPrefix + "City";

	if ( ( fldCheck = guGetFirstElementByName( doc, "doAction" ) ) )
		fldCheck.value = "update country";

	f.submit();

	return;
}

// guOnChangeAddressSelection
// function is tightly bound with HTML-generating
// code found in clsAddress.asp
function guOnChangeAddressSelection( fld, sPrefix )
{
	if ( !fld )
		return;

	var f = fld.form;

	if ( !f )
		return;

	if ( typeof( f.doAction ) == "undefined" )
		return;

	var nTop = guGetAbsoluteTop( fld );
	var nLeft = guGetAbsoluteLeft( fld );

	overlib( 'Please Wait - Preparing Address Fields', WIDTH, 250, FIXX, nLeft - 35, FIXY, nTop + 21 );

	var fldCheck = null;

	if ( ( fldCheck = guGetFirstElementByName( fld.ownerDocument, sPrefix + "EditToggle" ) ) )
		fldCheck.value = "";

	f.doAction.value = "update address selection";
	f.submit();
	return;
}

// guDoSelectProfileAddress
// function is tightly bound with HTML-generating
// code found in clsAddress.asp
function guDoSelectProfileAddress( fld, sAddressPrefix, sTogglePrefix )
{
	if ( !fld )
		return;

	var f = fld.form;

	if ( !f )
		return;

	if ( typeof( f.doAction ) == "undefined" )
		return;

	var nTop = guGetAbsoluteTop( fld );
	var nLeft = guGetAbsoluteLeft( fld );

	overlib( 'Please Wait - Looking up Address', WIDTH, 250, FIXX, nLeft - 35, FIXY, nTop + 21 );

	var fldCheck = null;

	if ( ( fldCheck = guGetFirstElementByName( fld.ownerDocument, sTogglePrefix + "EditToggle" ) ) )
		fldCheck.value = "";

	f.doAction.value = "update address selection " + sAddressPrefix;
	f.submit();
}


function guGetElementInnerHTML( sElementId, sHtmlIfFldNotFound )
{
	var fld;

	if ( !sElementId )
		return sHtmlIfFldNotFound;

	if ( sElementId == "" )
		return sHtmlIfFldNotFound;

	if ( document.getElementById )
		fld = document.getElementById( sElementId );
	else
	if ( document.all )
	{
		fld = document.all[ sElementId ];
	}

	if ( !fld )
		return sHtmlIfFldNotFound;

	return fld.innerHTML;
}

// guVerifyEditAddress
// function is tightly bound with HTML-generating
// code found in clsAddress.asp
function guVerifyEditAddress( fld )
{
	if ( !fld )
		return;

	var f = fld.form;

	if ( !f )
		return;

	if ( typeof( f.doAction ) == "undefined" )
		return;

	if ( !confirm( "Are you sure you want to modify the current address?\n\n" +
				"Any changes made here will be reflected in the corresponding address in your User Profile.\n\n" +
				"If you want your change to only be reflected for this service, click Cancel and select 'Specified Location' on the page.\n\n" +
				"(OK = yes, Cancel = no)" ) )
	{
		fld.checked = false;
		return;
	}

	var nTop = guGetAbsoluteTop( fld );
	var nLeft = guGetAbsoluteLeft( fld );

	overlib( 'Please Wait - Preparing Address Fields', WIDTH, 150, FIXX, nLeft, FIXY, nTop + 21 );

	f.doAction.value = "update address selection";
	f.submit();
	return;
}

function guMinimumAddress( doc, sFieldPrefix, sDescPrefix )
{
	var sRequired = "";
	var fldCity = guGetFirstElementByName( doc, sFieldPrefix + "City" );
	var fldCityState = guGetFirstElementByName( doc, sFieldPrefix + "CityState" );
	var fldState = guGetFirstElementByName( doc, sFieldPrefix + "State" );
	var fldZip = guGetFirstElementByName( doc, sFieldPrefix + "Zip" );
	var fldCountry = guGetFirstElementByName( doc, sFieldPrefix + "Country" );

	var sCountry = "";

	if ( fldCountry )
	{
		if ( fldCountry.type.toLowerCase() == "select-one" )
			sCountry = fldCountry.options[ fldCountry.selectedIndex ].value;
		else
			sCountry = fldCountry.value;
	}

	if ( sCountry == "" )
		sCountry = "U.S.A."

	if ( sCountry == "U.S.A." )
	{
		if ( !fldZip )
			sRequired += "\n" + sDescPrefix + " Zip (field missing)";
		else
		if ( fldZip.value.replace( / /g, "" ) == "" )
			sRequired += "\n" + sDescPrefix + " Zip";
		else
		if ( !fldCityState )
			sRequired += "\nMove From Zip is invalid";
	}
	else
	if ( sCountry == "Canada" )
	{
		if ( !fldCity )
			sRequired += "\n" + sDescPrefix + " City (field missing)";
		else
		if ( fldCity.value.replace( / /g, "" ) == "" )
			sRequired += "\n" + sDescPrefix + " City";

		if ( !fldState )
			sRequired += "\n" + sDescPrefix + " Province (field missing)";
		else
		if ( fldState.selectedIndex == 0 )
			sRequired += "\n" + sDescPrefix + " Province";

		if ( !fldZip )
			sRequired += "\n" + sDescPrefix + " Postal Code (field missing)";
		else
		if ( fldZip.value.replace( / /g, "" ) == "" )
			sRequired += "\n" + sDescPrefix + " Postal Code";
	}
	else
	{
		if ( !fldCity )
			sRequired += "\n" + sDescPrefix + " City (field missing)";
		else
		if ( fldCity.value.replace( / /g, "" ) == "" )
			sRequired += "\n" + sDescPrefix + " City";
	}

	return sRequired;
}